首页 > 编程语言 >优先级退金额 小算法

优先级退金额 小算法

时间:2023-09-14 15:13:12浏览次数:28  
标签:优先级 name CostInfoTO List 金额 算法 costInfoTOList type useNum

优先级退金额 小算法

背景 :用户需要 退钱 按照对应的规则优先级 退,例如 用户最大要退 50 ,这个时候 让优先级 现金 福利卡 礼包 这几个优先退 其他次之。例如 用户 混合支付 用了 20 现金 20 福利卡 20 礼包,这个时候要退 50,应该是 依次 退 现金 20 福利卡 20 礼包 10

思路
  1. 首先获取 用户每种 类型的最大 退款数据。

  2. 让优先级 获取最大 退款数据,获取到 存在新的 List 里面 (这样就保证顺序) ,在把 其他的数据 不相关的数据 排除 优先级的 加入。

  3. 遍历 新的List 依次 相减,并且记录到 对应的相减的结果,并且判断 是不是已经足够了,把结果加入到新的List 。

showcode
/**
 * 运费优先级计算
 * @param workOrderMaximumRefundTO
 */
private void setUserFreightCostInfoList(WorkOrderMaximumRefundTO workOrderMaximumRefundTO) {
    BigDecimal maxUserFreight = workOrderMaximumRefundTO.getMaxUserFreight();
    List<CostInfoTO> maxCostInfoTOList = workOrderMaximumRefundTO.getMaxCostInfoTOList();
    // 默认优先退现金、其次礼品余额、最后福利卡券
    // CASH GIFT_BALANCE WELFARE_CARD
    Map<String, CostInfoTO> costInfoTOMap = maxCostInfoTOList
            .stream()
            .collect(Collectors.toMap(e -> e.getType(), t -> t));

    CostInfoTO cashCostInfoTO = costInfoTOMap.get(CostTypeEnum.CASH.getCode());
    CostInfoTO giftCostInfoTO = costInfoTOMap.get(CostTypeEnum.GIFT_BALANCE.getCode());
    CostInfoTO cardCostInfoTO = costInfoTOMap.get(CostTypeEnum.WELFARE_CARD.getCode());
    List<String> exists = Lists.newArrayList(CostTypeEnum.CASH.getCode(),CostTypeEnum.GIFT_BALANCE.getCode(),CostTypeEnum.WELFARE_CARD.getCode());

    List<CostInfoTO> costInfoTOList = Lists.newArrayList();
    if(null != cashCostInfoTO){
        costInfoTOList.add(cashCostInfoTO);
    }
    if(null != giftCostInfoTO){
        costInfoTOList.add(giftCostInfoTO);
    }
    if(null != cardCostInfoTO){
        costInfoTOList.add(cardCostInfoTO);
    }
    costInfoTOMap.forEach((k,v)->{
        if(!exists.contains(k)){
            costInfoTOList.add(v);
        }
    });

    if(CollectionUtils.isEmpty(costInfoTOList)) {
        List<CostInfoTO> costInfoTOS = maxCostInfoTOList.stream().map(e -> {
            CostInfoTO costInfo = new CostInfoTO();
            costInfo.setName(e.getName());
            String type = e.getType();
            costInfo.setType(type);
            costInfo.setUseNum(BigDecimal.ZERO);
            return costInfo;
        }).collect(Collectors.toList());
        workOrderMaximumRefundTO.setUserFreightCostInfoList(costInfoTOS);
        return;
    }
    boolean flag = false;
    List<CostInfoTO> costInfoTOS = new ArrayList<>();

    for (int i = 0; i < costInfoTOList.size(); i++) {
        CostInfoTO newCostInfoTO = new CostInfoTO();
        CostInfoTO costInfoTO = costInfoTOList.get(i);

        newCostInfoTO.setName(costInfoTO.getName());
        newCostInfoTO.setType(costInfoTO.getType());
        BigDecimal useNum = costInfoTO.getUseNum();
        //是否已经有足够
        if(flag){
            newCostInfoTO.setUseNum(BigDecimal.ZERO);
            costInfoTOS.add(newCostInfoTO);
            continue;
        }
        // 如果现金足够
        if(useNum.compareTo(maxUserFreight) >= CommonConstants.ZERO){
            flag = true;
            newCostInfoTO.setUseNum(maxUserFreight);
        }else {
            // 减去现金 剩余多少
            newCostInfoTO.setUseNum(useNum);
            maxUserFreight = NumberUtil.sub(maxUserFreight, useNum);
        }
        costInfoTOS.add(newCostInfoTO);
    }
    workOrderMaximumRefundTO.setUserFreightCostInfoList(costInfoTOS);

}

测试结果

用户运费信息[CostInfoTO(name=现金, type=CASH, useNum=12), CostInfoTO(name=福利卡, type=WELFARE_CARD, useNum=10), CostInfoTO(name=福利礼包, type=WELFARE_PACKAGE, useNum=10), CostInfoTO(name=能量, type=POWER, useNum=8)]

换成 15 算

用户运费信息[CostInfoTO(name=现金, type=CASH, useNum=12), CostInfoTO(name=福利卡, type=WELFARE_CARD, useNum=3), CostInfoTO(name=福利礼包, type=WELFARE_PACKAGE, useNum=0), CostInfoTO(name=能量, type=POWER, useNum=0)]

总结:有一点难度。

标签:优先级,name,CostInfoTO,List,金额,算法,costInfoTOList,type,useNum
From: https://www.cnblogs.com/lyc88/p/17702544.html

相关文章

  • 算法回顾之一:冒泡排序
    数据结构与算法是计算机本科相关专业学生的必修课,我当年自然也是学过的,而且印象考试成绩还不错。不过近期写了一个冒泡排序算法(不使用类库实现),竟然出现了Bug,实在惭愧。仔细想想工作这几年一直都是使用Java集合框架和类库,因此感觉还是有必要再重温一下。-------------------------......
  • 算法回顾之二:直接插入排序
    算法回顾系列第二篇:直接插入排序算法-------------------------------------------直接插入排序基本原理:把n个待排序的元素看成为一个有序表和一个无序表。开始时有序表中只包含一个元素,无序表中包含有n-1个元素。排序过程中每次从无序表中取出第一个元素,将它插入到有序表中的适当......
  • C#(4):语言基本元素、类型、变量、方法、算法
     穿插算法和数据结构var类型可以根据复制自动推断变量属性    应为get或set访问器:方法名没加括号变量和方法(循环,递归)usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceClassMethodExample{classProgra......
  • 算法回顾之三:二分查找
    算法回顾系列第三篇:二分查找算法------------------------------------------------二分查找算法 基本原理:首先,假设表中元素是按升序排列.将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功.否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键......
  • 代码随想录算法训练营第9天| ●28. 实现 strStr() ●459.重复的子字符串 ●字符串总结
    28.找出字符串中第一个匹配项的下标mydemo--(mythought)--(falied)classSolution{public:intstrStr(stringhaystack,stringneedle){for(inti=0;i<haystack.size();i++){if(haystack[i]!=needle[0])continue;......
  • 逆向使用的公共加密解密的方法与算法
    python的AES加密解密方法-ECB模式fromCrypto.CipherimportAESimportbase64fromCrypto.Util.Paddingimportunpad,paddefdecrypt_aes(ciphertext,key):ciphertext=base64.b64decode(ciphertext)#使用base64解码密文cipher......
  • 代码随想录算法训练营第8天| ● 344.反转字符串 ● 541. 反转字符串II ● 剑指Offer 0
    344.反转字符串mydemo--(一次就过)--(成功)classSolution{public:voidreverseString(vector<char>&s){intlen=s.size();chartmp;inti=0;intj=len-1;while(i<j){tmp=s[i];......
  • 数据挖掘的十大经典算法?
    数据挖掘是从大量数据中发现隐藏模式、关联和知识的过程。以下是十大经典算法,它们被广泛应用于数据挖掘任务,并且每个算法都有其独特的优势和适用场景。1.决策树(DecisionTree):决策树是一种基于树结构的分类和回归方法。它通过使用属性选择指标构建树,在每个节点上进行分裂,以递归......
  • 代码随想录算法训练营第七天
    代码随想录算法训练营第七天|LeetCode344(反转字符串)LeetCode541(反转字符串II)LeetCode剑指05(替换空格)LeetCode151(反转字符串中的单词)LeetCode剑指58(II.左旋转字符串)344:反转字符串LeetCode344(反转字符串)思路:双指针遍历直接交换元素classSolution......
  • 数据分享|R语言逻辑回归、Naive Bayes贝叶斯、决策树、随机森林算法预测心脏病|附代码
    全文链接:http://tecdat.cn/?p=23061最近我们被客户要求撰写关于预测心脏病的研究报告,包括一些图形和统计输出。这个数据集可以追溯到1988年,由四个数据库组成。克利夫兰、匈牙利、瑞士和长滩。"目标"字段是指病人是否有心脏病。它的数值为整数,0=无病,1=有病数据集信息:目标:主......