当前,我在小型产品管理应用程序中遇到折扣计算问题。
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), "jackmuller@gmail.com"))
.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流使用实现它。
在此先感谢您的帮助。
答案 0 :(得分:1)
只需将第二个(如果放在第一个的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);
}