Design a Shopping Cart System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-04-26

Problem Statement

Design the Low-Level Design (LLD) of a shopping cart system for an e-commerce platform. The system should allow users to add/remove products, view their cart, and calculate the total amount before checkout.


Functional Requirements


1.Cart Operations
•Add an item to the cart.
•Remove an item from the cart.
•Update quantity of an item in the cart.
•View all items in the cart with details (name, price, quantity).
2.Pricing & Discounts
•Compute total cart value (price × quantity).
•Apply discount coupons/offers (if applicable).
•Support taxes or shipping charges (configurable).
3.User Association
•Each user should have a unique cart.
•Cart should persist for the session (and optionally across sessions).
4.Checkout Support
•Validate stock availability before checkout.
•Generate an order summary from the cart.


Non-Functional Requirements


•Clean OOP design using classes like Cart, CartItem, Product, User, etc.
•Ensure extensibility (support for future features like loyalty points, multiple sellers).
•Keep code modular, testable, and reusable.


Design Hints


•Product → contains id, name, price, category.
•CartItem → wraps a Product and its quantity.
•Cart → maintains a collection of CartItems and provides APIs (addItem, removeItem, getTotal).
•User → linked to a Cart.
•Consider Strategy Pattern for applying discounts and promotions.


Solution

Comments