FreeGiftDiscount extends Discount { @Override public Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel) { Double originalPrice = lineItem.price(); int numberOfSets = lineItem.amount() / (bought + freeGift); int numberOfFreeGift = numberOfSets * freeGift; double freeGiftWorth = numberOfFreeGift * lineItem.product().singleUnitPrice(); double discountedPrice = originalPrice - freeGiftWorth; appendToViewModel(lineItem, plainTextViewModel, originalPrice, discountedPrice); return discountedPrice; } FreeGiftDiscount(int bought, int freeGift, int priority); @Override Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel);  }
@Test public void shouldCalculateDiscountByAmountBought() throws Exception { FreeGiftDiscount freeGiftDiscount = new FreeGiftDiscount(2, 1, 0); Double price = freeGiftDiscount.discountedPrice(new OrderLineItem(new Product("xyz", "ItemXXX", 50d, "pill", null), 10), new PlainTextViewModel()); assertThat(price, is(350d)); }
@Test public void shouldCalculateDiscountByAmountBoughtWithOddNumbers() throws Exception { FreeGiftDiscount freeGiftDiscount = new FreeGiftDiscount(7, 3, 0); Double price = freeGiftDiscount.discountedPrice(new OrderLineItem(new Product("xyz", "ItemXXX", 6d, "piece", null), 13), new PlainTextViewModel()); assertThat(price, is(60d)); }