Grocery Cart Checkout

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-02-25

Problem Statement

Designing the Low-Level Design (LLD) for a Grocery Cart Checkout System, similar to what’s used in online grocery platforms like BigBasket, Blinkit, Amazon Fresh, or Instacart.


The system allows customers to add grocery items to their cart, apply offers or coupons, calculate the total bill (including taxes and discounts), and finally proceed to checkout and payment.


The focus is on the object-oriented design — defining entities, relationships, and workflows between components.


🎯 Goal


Design a system that can:


•Manage a customer’s grocery cart.
•Allow adding, removing, or updating item quantities.
•Apply discounts, coupons, and taxes.
•Compute total payable amount.
•Proceed to checkout with multiple payment methods.
•Generate a summary/receipt for the user.


✅ Functional Requirements


1. Cart Management


•Each Customer has one Cart at a time.
•A Cart can contain multiple CartItems.
•Each CartItem contains:
•Product details (name, price, unit)
•Quantity
•Subtotal (price × quantity)


Operations:


•addItem(Product, quantity)
•removeItem(Product)
•updateQuantity(Product, newQuantity)
•viewCart()


2. Product Details


•Each Product has:
•id
•name
•category (e.g., Fruits, Dairy, Beverages)
•pricePerUnit
•taxRate
•availableQuantity


3. Discounts and Coupons


•Discounts can apply:
•On individual products (e.g., 10% off on Apples)
•On the total bill (e.g., ₹100 off for orders above ₹1000)
•Via promo codes (e.g., “FRESH10” gives 10% off)
•Support for multiple discount types:

Flat discount

Percentage discount

BOGO (Buy One Get One) (optional)


4. Tax Calculation


•Taxes may vary by product category:
•Grocery → 5%
•Packaged food → 12%
•Beverages → 18%
•Tax is applied after discounts are applied.


5. Checkout and Payment


•Once cart is ready:
•Calculate subtotal, discounts, tax, and total payable amount.
•Proceed to checkout.
•Supported payment methods:
•Cash
•Card
•UPI/Wallet
•On successful payment, generate Order and clear the cart.


6. Invoice / Receipt


•Should display:
•List of items with price, quantity, and subtotal
•Applied discounts and taxes
•Total amount payable
•Payment details


⚙️ Non-Functional Requirements


•Scalable: Should handle many items efficiently.
•Extensible: Should allow adding new discount/tax rules easily.
•Testable: Components should be independently testable.
•Accurate: Must ensure precise rounding and billing.
•Maintainable: Clean, modular, and SRP-compliant design.


🧱 Key Classes to Design


ClassResponsibility


Product


Represents grocery items with attributes like name, price, and tax rate.


CartItem


Represents an item and its quantity inside a cart.


Cart


Holds list of CartItems and handles add/remove/update operations.


Discount


Abstract class/interface for different discount types.


FlatDiscount, PercentageDiscount, CouponDiscount


Implement discount logic.


TaxCalculator


Calculates applicable tax on items.


CartCalculator


Computes subtotal, discount total, tax, and final total.


Payment


Represents payment transaction details.


CheckoutService


Handles final checkout, applies discounts/taxes, triggers payment.


InvoiceGenerator


Generates the final bill/receipt.


🧩 Suggested Class Structure

1 2Customer 3 ├── String id 4 ├── String name 5 ├── Cart cart 6 7Cart 8 ├── List<CartItem> items 9 ├── void addItem(Product, quantity) 10 ├── void removeItem(Product) 11 ├── void updateQuantity(Product, newQuantity) 12 ├── double getTotalBeforeDiscount() 13 ├── double getTotalAfterDiscountAndTax() 14 15CartItem 16 ├── Product product 17 ├── int quantity 18 ├── double getItemTotal() 19 20Product 21 ├── String id 22 ├── String name 23 ├── double pricePerUnit 24 ├── double taxRate 25 ├── String category 26 ├── int availableQuantity 27 28Discount (interface) 29 ├── double apply(Cart cart) 30 31FlatDiscount, PercentageDiscount, CouponDiscount 32 ├── override apply() 33 34TaxCalculator 35 ├── double calculateTax(CartItem) 36 37CheckoutService 38 ├── CartCalculator cartCalculator 39 ├── Payment processCheckout(Cart) 40 41Payment 42 ├── PaymentType type (CASH, CARD, WALLET) 43 ├── double amountPaid 44 ├── PaymentStatus status (SUCCESS, FAILED) 45 46InvoiceGenerator 47 ├── String generateInvoice(Cart, Payment)


🔄 Example Flow


Add Items to Cart

•2× Milk @ ₹50 → ₹100
•3× Apple @ ₹30 → ₹90
1.Subtotal = ₹190
2.Apply 10% Discount (Coupon: “FRESH10”)
•Discount = ₹19
•After Discount = ₹171
3.Apply Tax (5%)
•Tax = ₹8.55
•Total Payable = ₹179.55

Payment

•User pays ₹179.55 via UPI.
•Payment successful.

Invoice Generated

1 2------------------------------ 3Grocery Cart Receipt 4------------------------------ 5Milk (2) ₹100 6Apple (3) ₹90 7Discount (10%) -₹19 8Tax (5%) ₹8.55 9------------------------------ 10Total: ₹179.55 11Paid via: UPI 12------------------------------


🧠 Design Patterns


•Strategy Pattern → for discounts and tax calculation.
•Factory Pattern → for payment type creation.
•Builder Pattern → for creating invoices.
•Observer Pattern (optional) → notify inventory system after checkout.


⚡ Possible Extensions


•Integrate with inventory service to update stock after checkout.
•Support loyalty points or membership discounts.
•Allow multi-cart per user (e.g., grocery + electronics).
•Add order history tracking.
•Add delivery time slot selection.


✅ Final Summary:


Design a Grocery Cart Checkout System (LLD) that:


•Manages cart operations, discounts, and tax calculations.
•Supports checkout and payment flow.
•Generates a detailed invoice.
•Follows modular, extensible, and object-oriented principles.


Solution

Comments