package com.bookshop.behavioral.strategy;
import java.math.BigDecimal;
import java.math.RoundingMode;
/** The shop's standard discount strategies. */
public final class Discounts {
private Discounts() {
}
public static DiscountStrategy none() {
return (subtotal, customer) -> BigDecimal.ZERO;
}
/** 2% per full year of loyalty, capped at 10%. */
public static DiscountStrategy loyalty() {
return (subtotal, customer) -> {
int percent = Math.min(customer.loyaltyYears() * 2, 10);
return percentOf(subtotal, percent);
};
}
/** 15% off orders of 50.00 or more. */
public static DiscountStrategy bulkOrder() {
return (subtotal, customer) -> subtotal.compareTo(new BigDecimal("50.00")) >= 0
? percentOf(subtotal, 15)
: BigDecimal.ZERO;
}
/** A flat percentage off everything, e.g. a summer sale. */
public static DiscountStrategy seasonalSale(int percent) {
return (subtotal, customer) -> percentOf(subtotal, percent);
}
private static BigDecimal percentOf(BigDecimal amount, int percent) {
return amount.multiply(BigDecimal.valueOf(percent))
.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
}
}