public static void calculateTotal(ShoppingCartStructure shoppingCartStructure) { Double total = 0.0; for (Product product : shoppingCartStructure.getProducts()) { Integer count = product.getCount(); Double price = product.getPrice(); total = total + count * price; } shoppingCartStructure.setTotal(total); calculateOffers(shoppingCartStructure); shoppingCartStructure.setGrandTotal( shoppingCartStructure.getTotal() + shoppingCartStructure.getOffer()); }
public static void calculateOffers(ShoppingCartStructure shoppingCartStructure) { Map<String, ArrayList<Product>> sameTypeProductsMap = new HashMap<String, ArrayList<Product>>(); for (Product product : shoppingCartStructure.getProducts()) { String productId = product.getProductId(); ArrayList<Product> sameTypeProducts = new ArrayList<Product>(); if (sameTypeProductsMap.containsKey(productId)) { sameTypeProducts = sameTypeProductsMap.get(productId); } sameTypeProducts.add(product); sameTypeProductsMap.put(productId, sameTypeProducts); } Map<String, ProductOffer> possibleOffersMap = shoppingCartStructure.getPossibleOffersMap(); if (!possibleOffersMap.isEmpty()) { Double offer = 0.0; for (String productId : sameTypeProductsMap.keySet()) { if (possibleOffersMap.containsKey(productId) && sameTypeProductsMap.containsKey(productId)) { Integer sameProductCount = 0; ArrayList<Product> sameProducts = sameTypeProductsMap.get(productId); if (!sameProducts.isEmpty()) { for (Product sameProduct : sameProducts) { sameProductCount = sameProductCount + sameProduct.getCount(); } ProductOffer productOffer = possibleOffersMap.get(productId); Double offerCondition = productOffer.getOfferCondition(); int offerApplyTimes = (int) (sameProductCount / offerCondition); if (offerApplyTimes > 0) { Double price = sameProducts.get(0).getPrice(); Double offerFormula = productOffer.getOfferFormula(); offer = offer + offerApplyTimes * price * offerFormula; shoppingCartStructure.getAppliedOffers().add(productOffer); } } } } shoppingCartStructure.setOffer(offer); } }