目前,我在小型产品管理应用程序中遇到折扣计算问题。
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;
}
使用龙目岛实用程序。
我有一个包含订单列表的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流使用实现它。
在此先感谢您的帮助。
最佳答案
只需将第二个(如果放在第一个的其他部分中)
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);
}