This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(MockitoJUnitRunner.class) | |
public class VoucherDiscountCalculationTest { | |
private static final String NO_VOUCHER = null; | |
private static final String TWENTY_POUNDS_VOUCHER = "20"; | |
@Mock | |
private VoucherService voucherService; | |
private VoucherDiscountCalculation voucherDiscountCalculation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(MockitoJUnitRunner.class) | |
public class VoucherPricingServiceTest { | |
private static final User UNUSED_USER = null; | |
private static final String NO_VOUCHER = null; | |
private static final String TWENTY_POUNDS_VOUCHER = "20"; | |
@Mock private VoucherService voucherService; | |
private TestableVoucherPricingService voucherPricingService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VoucherService { | |
public double getVoucherValue(String voucher) { | |
// Imagine that this calculate the voucher price. | |
// Keeping it simple so we can understand the approach. | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VoucherPricingService extends UserDiscountPricingService { | |
private VoucherService voucherService; | |
@Override | |
protected double applyAdditionalDiscounts(double total, User user, String voucher) { | |
double voucherValue = voucherService.getVoucherValue(voucher); | |
double totalAfterValue = total - voucherValue; | |
return (totalAfterValue > 0) ? totalAfterValue : 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ShoppingBasketBuilder { | |
private Map<Product, Integer> products = new HashMap<Product, Integer>(); | |
public static ShoppingBasketBuilder aShoppingBasket() { | |
return new ShoppingBasketBuilder(); | |
} | |
public ShoppingBasketBuilder with(Product product) { | |
this.products.put(product, 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductBuilder { | |
private double price; | |
private String name; | |
public static ProductBuilder aProduct() { | |
return new ProductBuilder(); | |
} | |
public ProductBuilder costing (double price) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(MockitoJUnitRunner.class) | |
public class PricingServiceTest { | |
private static final String NO_VOUCHER = ""; | |
private TestablePricingService pricingService = new TestablePricingService(); | |
private ShoppingBasket shoppingBasket; | |
@Mock private PriceCalculation priceCalculation; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class PricingService { | |
private PriceCalculation priceCalculation; | |
public double calculatePrice(ShoppingBasket shoppingBasket, User user, String voucher) { | |
double discount = calculateDiscount(user); | |
double total = 0; | |
for (ShoppingBasket.Item item : shoppingBasket.items()) { | |
total += priceCalculation.calculateProductPrice(item.getProduct(), item.getQuantity()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface PriceCalculation { | |
double calculateProductPrice(Product product, int quantity); | |
} | |
public class BoxingDayPriceCalculation implements PriceCalculation | |
public class StandardPriceCalculation implements PriceCalculation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BoxingDayPriceCalculationTest { | |
private BoxingDayPriceCalculation priceCalculation = new BoxingDayPriceCalculation(); | |
@Test public void | |
should_apply_boxing_day_discount_on_product_price() { | |
Product book = aProduct().costing(10).build(); | |
double price = priceCalculation.calculateProductPrice(book, 1); |