Design a Shopping Cart with Coupons
Model a shopper's cart that adds items, applies one percentage or fixed-amount coupon, and shows its subtotal, discount and total.
Design the classes for a shopping cart that supports coupons. A shopper adds items to the cart, applies a coupon code, and views the cart's items, total price and discounts.
For this exercise, assume one shopper uses one in-memory cart, one call at a time. Products come from a fixed catalog keyed by SKU, and every price is a non-negative whole number of cents in one currency.
Adding an item takes a SKU and a whole-number quantity of at least 1; adding a SKU that is already in the cart increases that item's quantity. An unknown SKU or a quantity below 1 is rejected.
For this exercise, assume the shopper applies a coupon by typing its code, and codes are looked up in a fixed list of valid coupons. A coupon is either a percentage off the subtotal (a whole percent from 1 to 100) or a fixed amount off (a whole number of cents). An unknown code is rejected.
A cart holds at most one coupon at a time: applying a code while a coupon is already applied is rejected, and the shopper can remove the applied coupon to try a different one.
For this exercise, assume the discount is always worked out from the cart's current subtotal, so adding items after applying a coupon updates it. A percentage discount is rounded down to a whole cent. A discount never exceeds the subtotal, so the total never goes below zero.
Viewing the cart returns its items with quantity, unit price and line total, the subtotal, a discount line naming the applied coupon and its amount, and the final total. A rejected call leaves the cart unchanged.
Removing items or changing quantities, tax, shipping, checkout, payment, inventory, coupon expiry or usage limits, persistence and concurrent access are outside scope. Design the objects, their method contracts and one interaction walkthrough. No implementation code is required.