Design Stateful Shopping Cart Lines
Model adding, updating and removing cart lines with captured prices, then handle later catalogue price changes.
Design an ecommerce shopping-cart object model for a shopper who adds products, changes quantities, removes products and views the cart.
For this exercise, assume one sequential in-memory cart and a product catalogue keyed by immutable SKU. Prices are nonnegative integer cents in one currency. A cart has at most one line per SKU.
Adding a known product with integer quantity 1 through 99 creates a line or increases its existing quantity;the resulting quantity must also be at most99. Setting quantity is absolute and requires an existing line and value 1 through 99.
Invalid inputs, unknown products, absent-line quantity updates and over-limit additions reject without changing any cart state. Removing a missing line is a harmless no-op.
For this exercise, assume a line captures its unit price on first add and keeps that price through later adds or quantity changes. Removing and re-adding the product creates a new line at its current catalogue price.
A view is immutable and contains coherent line values and the exact sum of quantity times stored unit price;an empty cart totals zero. Use exact integer arithmetic.
Design the objects, operation contracts and interaction trace. No implementation code is required. Inventory, checkout, payment, discounts, tax, persistence, UI and concurrency are outside scope.
For the follow-up, assume catalogue prices may change after a line is created. Capture one coherent current price when creating a new line, and preserve the existing captured-price rules. Explain how the object boundaries support that behavior and how you test it;automatic repricing remains excluded.