You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yilai/FixedAmountDiscountStrategy...

56 lines
1.8 KiB

/**
* 固定金额折扣策略,提供固定金额的折扣
*/
package com.orderprocessing;
public class FixedAmountDiscountStrategy implements DiscountStrategy {
private double discountAmount;
private double minimumOrderAmount;
/**
* 构造方法
* @param discountAmount 固定折扣金额
* @param minimumOrderAmount 应用折扣的最小订单金额
*/
public FixedAmountDiscountStrategy(double discountAmount, double minimumOrderAmount) {
if (discountAmount < 0) {
throw new IllegalArgumentException("折扣金额不能为负数");
}
if (minimumOrderAmount < 0) {
throw new IllegalArgumentException("最小订单金额不能为负数");
}
this.discountAmount = discountAmount;
this.minimumOrderAmount = minimumOrderAmount;
}
@Override
public double calculateDiscount(Order order) {
if (order == null || order.getTotalAmount() < minimumOrderAmount) {
return 0;
}
// 确保折扣金额不超过订单总金额
return Math.min(discountAmount, order.getTotalAmount());
}
public double getDiscountAmount() {
return discountAmount;
}
public void setDiscountAmount(double discountAmount) {
if (discountAmount < 0) {
throw new IllegalArgumentException("折扣金额不能为负数");
}
this.discountAmount = discountAmount;
}
public double getMinimumOrderAmount() {
return minimumOrderAmount;
}
public void setMinimumOrderAmount(double minimumOrderAmount) {
if (minimumOrderAmount < 0) {
throw new IllegalArgumentException("最小订单金额不能为负数");
}
this.minimumOrderAmount = minimumOrderAmount;
}
}