package com.bookshop.behavioral.strategy;
import com.bookshop.domain.Book;
import com.bookshop.domain.Customer;
import java.math.BigDecimal;
import java.util.List;
/**
* The context: computes totals without knowing which discount rule is in force.
* Swapping the strategy changes pricing behaviour with zero changes here.
*/
public class PriceCalculator {
private final DiscountStrategy discount;
public PriceCalculator(DiscountStrategy discount) {
this.discount = discount;
}
public BigDecimal totalFor(List<Book> books, Customer customer) {
BigDecimal subtotal = books.stream().map(Book::price).reduce(BigDecimal.ZERO, BigDecimal::add);
return subtotal.subtract(discount.discountFor(subtotal, customer));
}
}