Design a Grocery Store System

Medium
Real World SystemsObserverStrategyFactory

Problem Statement

Design a grocery store checkout system that handles:

  • Product catalog with categories and prices
  • Shopping cart management
  • Multiple checkout lanes (regular, self-checkout, express)
  • Promotions and discounts (BOGO, percentage off, bulk discount)
  • Loyalty points system
  • Inventory management

Design Requirements

Key Entities:

  • Product — SKU, name, price, category, stock
  • ShoppingCart — Holds CartItems, calculates totals
  • CheckoutLane — Processes payment, applies discounts
  • Promotion — Various discount strategies
  • LoyaltyAccount — Tracks and redeems points
  • Inventory — Manages stock levels, alerts on low stock

Key Behaviors:

  • addToCart(productId, quantity) → adds to cart if in stock
  • applyPromotion(promotionCode) → applies discount
  • checkout(cart, paymentMethod) → processes sale, updates inventory
  • getReceipt() → itemized receipt with discounts

!Constraints

- Product price precision: 2 decimal places - Promotions: BOGO (buy one get one), 10-50% off, bulk (buy 3 get 20% off) - Loyalty points: earn 1 point per $1 spent, redeem 100 points = $1 - Express lane: max 15 items - Inventory alert when stock < 10 units
Output

Run your code to see output here.