满减活动基础算法-java-类似淘宝满200减30
public static void main(String[] args) {
//先封装⼀个总体的需要满减的⾦额
Map<Integer, BigDecimal> skuToMap = new HashMap<>();
for (int i = 1; i < 100; i++) {
skuToMap.put(i, RandomUtil.randomBigDecimal(new BigDecimal(10), new BigDecimal(40)).setScale(2, BigDecimal.ROUND_DOWN));
}
BigDecimal totalFullAmount = BigDecimal.ZERO;
for (Map.Entry<Integer, BigDecimal> longBigDecimalEntry : Set()) {
totalFullAmount = totalFullAmount.Value());
}
//满减满⾜条件减去的⾦额
BigDecimal totalReducePrice = RandomUtil.randomBigDecimal(new BigDecimal(10), new BigDecimal(30)).setScale(2, BigDecimal.ROUND_DOWN);
if (totalFullAmountpareTo(totalReducePrice) > 0) {
//循环满减⽅式向下取整类似天猫满200减30
BigDecimal averageReducePrice = totalReducePrice.divide(totalFullAmount, 2, BigDecimal.ROUND_DOWN);
int mapSize = skuToMap.size();
int i = 0;
BigDecimal alreadyReducePrice = BigDecimal.ZERO;
Map<Integer, BigDecimal> sssss = new LinkedHashMap<>();
Set().stream().sorted(Comparatorparing(Map.Entry::getValue)).List()).forEach(ele -bigdecimal转换为integer
> sssss.Key(), ele.g            for (Map.Entry<Integer, BigDecimal> longBigDecimalEntry : Set()) {
Integer skuId = Key();
BigDecimal price = Value();
if (mapSize - i == 1) {
//TODO 这⾥存在问题,如果这个商品的⾦额⼩于或者等于最后的满减⾦额这⾥会为负值或者为0的情况
BigDecimal skuReducePrice = totalReducePrice.subtract(alreadyReducePrice).setScale(2, BigDecimal.ROUND_DOWN);
alreadyReducePrice = alreadyReducePrice.add(skuReducePrice);
System.out.println(skuReducePrice);
} else {
BigDecimal skuReducePrice = price.multiply(averageReducePrice).setScale(2, BigDecimal.ROUND_DOWN);
alreadyReducePrice = alreadyReducePrice.add(skuReducePrice);
System.out.println(skuReducePrice);
}
i++;
}
System.out.println(alreadyReducePricepareTo(totalReducePrice) == 0);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。