选择更有利可图的流结果

问题描述 投票:1回答:1

目前我在小型产品管理应用程序中遇到折扣计算问题。

public class Customer {
  private String name;
  private String surname;
  private LocalDate birthDate;
  private String email;
}

public class Order {
  private Customer customer;
  private Product product;
  private Integer quantity;
  private LocalDate estimatedRealizationDate;
}

public class Product {
  private String name;
  private BigDecimal price;
  private Category category;
}

使用lombok实用程序。

我有一个Orders类,其中包含一个订单列表。

public class Orders {

    private final List<Order> productList;

    private static final int MAXIMAL_AGE_WITH_DISCOUNT = 25;
    private static final BigDecimal DISCOUNT_RATIO_FOR_CUSTOMER_YOUNGER_THAN_25 = BigDecimal.valueOf(0.97);
    private static final BigDecimal DISCOUNT_RATIO_FOR_ESTIMATED_DELIVERY_DATE_SMALLER_THAN_2 = BigDecimal.valueOf(0.98);
    private static final int MAXIMAL_DATES_NUMBER_FOR_DISCOUNT = 2;
}

下面是一个订单示例:

Orders orderList =
        new Orders(
            newArrayList(
                Order.builder()
                    .product(new Product("LEVER", BigDecimal.valueOf(120), Category.C))
                    .customer(new Customer("JACK", "MULLER", LocalDate.of(1980, 7, 3), "[email protected]"))
                    .estimatedRealizationDate(LocalDate.now().plusDays(2))
                    .quantity(5)
                    .build());

我想为每个25岁以下的客户提供3%的折扣,并且对于订单的2%折扣,预计交付日期从此时起小于2天,但我想为客户选择更有利可图的折扣。

我写了我的代码片段,但据我所知,我的版本会在某些情况下结合折扣,这是不可取的。

BigDecimal totalPriceOfAllOrdersAfterPriceReduction() {
        return productList.stream().map(i -> {
            if (between(i.getCustomer().getBirthDate(), LocalDate.now()).getYears() < MAXIMAL_AGE_WITH_DISCOUNT) {
                return i.getProduct().getPrice().multiply(DISCOUNT_RATIO_FOR_CUSTOMER_YOUNGER_THAN_25).multiply(BigDecimal.valueOf(i.getQuantity()));
            }
            if (between(i.getEstimatedRealizationDate(), LocalDate.now()).getDays() < MAXIMAL_DATES_NUMBER_FOR_DISCOUNT) {
                return i.getProduct().getPrice().multiply(DISCOUNT_RATIO_FOR_ESTIMATED_DELIVERY_DATE_SMALLER_THAN_2).multiply(BigDecimal.valueOf(i.getQuantity()));
            }
            return i.getProduct().getPrice();
        }).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

在整个操作之后,我想总结所有订单的总价格(数量*价格)。

我想用Java流使用来实现它。

在此先感谢您的帮助。

java java-8 stream java-stream
1个回答
1
投票

把第二个if放在第一个的else部分

BigDecimal totalPriceOfAllOrdersAfterPriceReduction() {
        return productList.stream().map(i -> {
            if (between(i.getCustomer().getBirthDate(), LocalDate.now()).getYears() < MAXIMAL_AGE_WITH_DISCOUNT) {
                return i.getProduct().getPrice().multiply(DISCOUNT_RATIO_FOR_CUSTOMER_YOUNGER_THAN_25).multiply(BigDecimal.valueOf(i.getQuantity()));
            } else{
               if (between(i.getEstimatedRealizationDate(), LocalDate.now()).getDays() < MAXIMAL_DATES_NUMBER_FOR_DISCOUNT) {
                   return i.getProduct().getPrice().multiply(DISCOUNT_RATIO_FOR_ESTIMATED_DELIVERY_DATE_SMALLER_THAN_2).multiply(BigDecimal.valueOf(i.getQuantity()));
               }
            }
            return i.getProduct().getPrice();
        }).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

使其更具可读性和可追踪性的另一条轨道是在订单上添加“折扣”字段然后

 @Builder
    @Getter
    @ToString
    public static class Order {
        private Customer customer;
        private Product product;
        private Integer quantity;
        private LocalDate estimatedRealizationDate;
        private BigDecimal discount = BigDecimal.ZERO;

        public boolean threePercent(){
            return Period.between(this.getCustomer().birthDate, LocalDate.now()).getYears() < MAXIMAL_AGE_WITH_DISCOUNT;
        }

        public boolean twoPercent(){
            return Period.between(this.estimatedRealizationDate, LocalDate.now()).getYears() < MAXIMAL_DATES_NUMBER_FOR_DISCOUNT;
        }


    }

    public static Order update(Order o){
        if(o.threePercent()){
            o.discount = DISCOUNT_RATIO_FOR_CUSTOMER_YOUNGER_THAN_25;
        }else{
            if(o.twoPercent()){
                o.discount = DISCOUNT_RATIO_FOR_ESTIMATED_DELIVERY_DATE_SMALLER_THAN_2;
            }
        }
        return o;
    }

    public static BigDecimal totalPriceOfAllOrdersAfterPriceReduction(List<Order> orders){
        return orders
                .stream()
                .map(Scratch::update)
                .peek(System.out::println)
                .map(o -> o.product.price.multiply(o.discount).multiply(BigDecimal.valueOf(o.quantity)))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
© www.soinside.com 2019 - 2024. All rights reserved.