Promotions on an order cart

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2025-04-30

Problem Statement

Design an object-oriented system to apply promotions on a user’s order cart and compute the final amount after applicable discounts. The cart contains multiple items, and the system must identify and apply valid promotions based on rules.

Solution

✅ Core Classes and Relationships

1. Cart

1java 2CopyEdit 3class Cart { 4 private List<CartItem> items; 5 private List<Promotion> appliedPromotions; 6 7 public void addItem(CartItem item) { ... } 8 public void removeItem(Product product) { ... } 9 public double getTotalAmount() { ... } 10 public List<CartItem> getItems() { ... } 11}

2. CartItem

1java 2CopyEdit 3class CartItem { 4 private Product product; 5 private int quantity; 6 7 public double getTotalPrice() { 8 return product.getPrice() * quantity; 9 } 10}

3. Product

1java 2CopyEdit 3class Product { 4 private String id; 5 private String name; 6 private String category; 7 private double price; 8}

4. Promotion (Interface)

1java 2CopyEdit 3interface Promotion { 4 boolean isApplicable(Cart cart); 5 Discount apply(Cart cart); 6 int getPriority(); // For ordering if needed 7}

✅ Concrete Promotions

A. FlatDiscountPromotion

1java 2CopyEdit 3class FlatDiscountPromotion implements Promotion { 4 private double threshold; 5 private double discountAmount; 6 7 public boolean isApplicable(Cart cart) { 8 return cart.getTotalAmount() >= threshold; 9 } 10 11 public Discount apply(Cart cart) { 12 return new Discount("Flat Discount", discountAmount); 13 } 14}

B. PercentageCategoryPromotion

1java 2CopyEdit 3class PercentageCategoryPromotion implements Promotion { 4 private String category; 5 private double percent; 6 7 public boolean isApplicable(Cart cart) { 8 return cart.getItems().stream().anyMatch(item -> item.getProduct().getCategory().equals(category)); 9 } 10 11 public Discount apply(Cart cart) { 12 double discount = cart.getItems().stream() 13 .filter(item -> item.getProduct().getCategory().equals(category)) 14 .mapToDouble(CartItem::getTotalPrice) 15 .sum() * (percent / 100.0); 16 17 return new Discount("Category Percentage Discount", discount); 18 } 19}

C. BuyXGetYFreePromotion

1java 2CopyEdit 3class BuyXGetYFreePromotion implements Promotion { 4 private String productId; 5 private int x; 6 private int y; 7 8 public boolean isApplicable(Cart cart) { 9 return cart.getItems().stream() 10 .anyMatch(item -> item.getProduct().getId().equals(productId) && item.getQuantity() >= x + y); 11 } 12 13 public Discount apply(Cart cart) { 14 for (CartItem item : cart.getItems()) { 15 if (item.getProduct().getId().equals(productId)) { 16 int sets = item.getQuantity() / (x + y); 17 double discount = sets * y * item.getProduct().getPrice(); 18 return new Discount("Buy X Get Y Free", discount); 19 } 20 } 21 return new Discount("Buy X Get Y Free", 0); 22 } 23}

5. Discount

1java 2CopyEdit 3class Discount { 4 private String description; 5 private double amount; 6 7 public Discount(String description, double amount) { 8 this.description = description; 9 this.amount = amount; 10 } 11 12 public double getAmount() { return amount; } 13}

6. PromotionEngine

1java 2CopyEdit 3class PromotionEngine { 4 private List<Promotion> promotions; 5 6 public void registerPromotion(Promotion promotion) { 7 promotions.add(promotion); 8 } 9 10 public List<Discount> applyPromotions(Cart cart) { 11 List<Discount> discounts = new ArrayList<>(); 12 for (Promotion promotion : promotions) { 13 if (promotion.isApplicable(cart)) { 14 discounts.add(promotion.apply(cart)); 15 } 16 } 17 return discounts; 18 } 19 20 public double getFinalAmount(Cart cart) { 21 double total = cart.getTotalAmount(); 22 double totalDiscount = applyPromotions(cart).stream().mapToDouble(Discount::getAmount).sum(); 23 return total - totalDiscount; 24 } 25}

🧠 Design Patterns Used

•Strategy Pattern: Promotion interface with multiple implementations.
•Open/Closed Principle: Add new promotions without changing existing logic.
•Factory/Registry (optional): To dynamically load promotions from config/db.

🧪 Example Usage

1java 2CopyEdit 3Cart cart = new Cart(); 4cart.addItem(new CartItem(productA, 2)); 5cart.addItem(new CartItem(productB, 3)); 6 7PromotionEngine engine = new PromotionEngine(); 8engine.registerPromotion(new FlatDiscountPromotion(1000, 100)); 9engine.registerPromotion(new PercentageCategoryPromotion("Electronics", 10)); 10engine.registerPromotion(new BuyXGetYFreePromotion("ABC123", 2, 1)); 11 12double finalAmount = engine.getFinalAmount(cart);


Comments