private BigDecimal calculateDiscount() { List<Discount> discounts = order.getDiscounts(); BigDecimal totalDiscount = BigDecimal.ZERO; for (Discount discount : discounts) { BigDecimal totalCostForDiscountedItems = new BigDecimal(discount.getNumberOfBooks()).multiply(FakeDatabase.COST_OF_ONE_BOOK); BigDecimal discountPercent = new FakeDatabase().getDiscountForNumberOfUniqueBooks(discount.getNumberOfBooks()); BigDecimal discountAmount = discountPercent.multiply(totalCostForDiscountedItems); totalDiscount = totalDiscount.add(discountAmount); } return totalDiscount; }
// 测试PurchaseList类 @Test public void test_purchaselist() { PurchaseList list = new PurchaseList(); Discount dis = Discount.Initial(); dis.ClearDiscount(); list.AddPurchaseListItem("ITEM000001"); list.AddPurchaseListItem("ITEM000001"); list.AddPurchaseListItem("ITEM000001"); list.AddPurchaseListItem("ITEM000002-2"); list.AddPurchaseListItem("ITEM000005-3"); list.ParsePurchaseList(); Map<String, PurchaseItem> returnlist = list.getPurchaseList(); PurchaseItem item = returnlist.get("ITEM000001"); assertTrue(item.getIteminfo().getName().equals("篮球")); assertEquals(item.getItemnum(), 3); assertEquals(item.getRealprice(), 294, 0.1); assertEquals(item.getDiscountprice(), 0, 0.1); assertEquals(returnlist.get("ITEM000005").getItemnum(), 3); }
// 测试Discount的添加折扣商品函数 @Test public void AddDiscountItem_and_AddGiveItem() { Discount dis = Discount.Initial(); dis.ClearDiscount(); dis.AddDiscountItem("ITEM000001"); dis.AddDiscountItem("ITEM000002"); dis.AddGiveItem("ITEM000001"); dis.AddGiveItem("ITEM000003"); PromotionType type1 = dis.IsPromotion("ITEM000001"); assertEquals(type1, PromotionType.BUYTWOGIVEONE); PromotionType type2 = dis.IsPromotion("ITEM000002"); assertEquals(type2, PromotionType.DISCOUNT); PromotionType type3 = dis.IsPromotion("ITEM000003"); assertEquals(type3, PromotionType.BUYTWOGIVEONE); PromotionType type4 = dis.IsPromotion("ITEM000005"); assertEquals(type4, PromotionType.NOPROMOTION); }
public Stream<CompletableFuture<String>> findPriceStream(String product) { return shops .stream() .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor)) .map(future -> future.thenApply(Quote::parse)) .map( future -> future.thenCompose( quote -> CompletableFuture.supplyAsync( () -> Discount.applyDiscount(quote), executor))); }
@Test public void should_calculate_price_CalculatePrice() { Discount dis = Discount.Initial(); dis.ClearDiscount(); dis.AddGiveItem("ITEM000001"); dis.AddDiscountItem("ITEM000005"); PurchaseList list = new PurchaseList(); list.AddPurchaseListItem("ITEM000001"); list.AddPurchaseListItem("ITEM000001"); list.AddPurchaseListItem("ITEM000001"); list.AddPurchaseListItem("ITEM000002-2"); list.AddPurchaseListItem("ITEM000005-3"); list.ParsePurchaseList(); Map<String, PurchaseItem> returnlist = list.getPurchaseList(); PurchaseItem item1 = returnlist.get("ITEM000001"); assertEquals(item1.getDiscountprice(), 98, 0.1); assertEquals(item1.getRealprice(), 294, 0.1); PurchaseItem item2 = returnlist.get("ITEM000002"); assertEquals(item2.getDiscountprice(), 0, 0.1); assertEquals(item2.getRealprice(), 2, 0.1); PurchaseItem item3 = returnlist.get("ITEM000005"); assertEquals(item3.getRealprice(), 9, 0.1); assertEquals(item3.getDiscountprice(), 0.45, 0.1); }
public void removeDiscount(Discount discount) { discounts.remove(discount); if (discount.getCoupon() == this) { discount.setCoupon(null); } }
public void addDiscount(Discount discount) { discounts.add(discount); if (discount.getCoupon() != this) { discount.setCoupon(this); } }