@Test public void shouldCalculateDiscountByPercentage() throws Exception { PercentageDiscount percentageDiscount = new PercentageDiscount(0.95, 0); Double price = percentageDiscount.discountedPrice(new OrderLineItem(new Product("abc", "ItemXXX", 123d, "bottle", null), 10), new PlainTextViewModel()); assertThat(price, is(1168.5d)); }
@Override public Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel) { double discountedPrice = lineItem.price() * percentage; plainTextViewModel.addToOriginalTotal(lineItem.price()); plainTextViewModel.addToDiscountedTotal(discountedPrice); plainTextViewModel.addToLinesSection(lineSummary(lineItem, discountedPrice) + "，节省" + DECIMAL_FORMAT.format(lineItem.price() - discountedPrice) + "(元)"); return discountedPrice; }
PercentageDiscount extends Discount { @Override public Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel) { double discountedPrice = lineItem.price() * percentage; plainTextViewModel.addToOriginalTotal(lineItem.price()); plainTextViewModel.addToDiscountedTotal(discountedPrice); plainTextViewModel.addToLinesSection(lineSummary(lineItem, discountedPrice) + "，节省" + DECIMAL_FORMAT.format(lineItem.price() - discountedPrice) + "(元)"); return discountedPrice; } }
PercentageDiscount extends Discount { @Override public Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel) { double discountedPrice = lineItem.price() * percentage; plainTextViewModel.addToOriginalTotal(lineItem.price()); plainTextViewModel.addToDiscountedTotal(discountedPrice); plainTextViewModel.addToLinesSection(lineSummary(lineItem, discountedPrice) + "，节省" + DECIMAL_FORMAT.format(lineItem.price() - discountedPrice) + "(元)"); return discountedPrice; } PercentageDiscount(double percentage, int priority); }
PercentageDiscount extends Discount { @Override public Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel) { double discountedPrice = lineItem.price() * percentage; plainTextViewModel.addToOriginalTotal(lineItem.price()); plainTextViewModel.addToDiscountedTotal(discountedPrice); plainTextViewModel.addToLinesSection(lineSummary(lineItem, discountedPrice) + "，节省" + DECIMAL_FORMAT.format(lineItem.price() - discountedPrice) + "(元)"); return discountedPrice; } PercentageDiscount(double percentage, int priority); @Override Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel); }
PercentageDiscount extends Discount { @Override public Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel) { double discountedPrice = lineItem.price() * percentage; plainTextViewModel.addToOriginalTotal(lineItem.price()); plainTextViewModel.addToDiscountedTotal(discountedPrice); plainTextViewModel.addToLinesSection(lineSummary(lineItem, discountedPrice) + "，节省" + DECIMAL_FORMAT.format(lineItem.price() - discountedPrice) + "(元)"); return discountedPrice; } PercentageDiscount(double percentage, int priority); @Override Double discountedPrice(OrderLineItem lineItem, PlainTextViewModel plainTextViewModel);  }