Bird
Raised Fist0

If a Cart contains multiple products with varying quantities, how should the Order class store this information to maintain data integrity after checkout?

hard📝 Trade-off Q9 of Q15
LLD - Design — Online Shopping Cart
If a Cart contains multiple products with varying quantities, how should the Order class store this information to maintain data integrity after checkout?
AStore only product IDs in Order
BStore a reference to the Cart object
CCopy product details and quantities into Order's own data structure
DClear Cart after checkout and keep no data in Order
Step-by-Step Solution
Solution:
  1. Step 1: Understand data integrity needs

    Order must keep a snapshot of products and quantities at checkout time.
  2. Step 2: Avoid referencing mutable Cart object

    Referencing Cart risks data changes after checkout, so copying data is safer.
  3. Final Answer:

    Copy product details and quantities into Order's own data structure -> Option C
  4. Quick Check:

    Order stores independent copy for integrity [OK]
Quick Trick: Copy data to Order to avoid changes after checkout [OK]
Common Mistakes:
MISTAKES
  • Referencing Cart directly
  • Storing only IDs without quantities
  • Clearing Cart without saving order data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes