Shopping Cart

Level: Intermediate

Concepts: State Business Rules


Implement a shopping cart system that handles products, quantities, pricing, and various types of discounts. The system should be able to calculate totals, apply discounts, and handle special cases like bulk pricing and promotional offers.

Requirements

  1. Add and remove items from the cart
  2. Update quantities of items
  3. Calculate subtotal for each item
  4. Calculate total for the entire cart
  5. Apply different types of discounts:
    • Percentage-based discounts
    • Fixed amount discounts
    • Buy X get Y free offers
    • Bulk pricing (e.g., buy 3 or more for a special price)
  6. Handle special cases:
    • Items with limited stock
    • Items that can't be combined with certain discounts
    • Maximum quantity limits per item

Hint

Start by implementing the basic cart operations (add, remove, update) and then gradually add the discount logic. Consider using the Strategy pattern for different types of discounts. Think about how to handle the order of discount application and how to validate that discounts are being applied correctly.

Bonus

  • Implement a loyalty points system
  • Add support for different currencies and exchange rates
  • Implement a tax calculation system
  • Add support for shipping costs and different shipping methods
  • Implement a coupon code system
  • Add support for saving carts and returning to them later