AlgoMaster Logo
AlgoMasterDesign a Shopping Cartmedium

Design a Shopping Cart

medium

Design a ShoppingCart class that manages items, a single promotional discount, and checkout. The cart begins open for changes. Once checkout succeeds, it becomes a record of the order and must reject any further attempt to add items or apply a discount.

Implement the ShoppingCart class:

  • ShoppingCart() creates an empty cart that has not been checked out.
  • boolean addItem(String name, double price) adds an item and returns true. If the cart has already been checked out, the method leaves the cart unchanged and returns false.
  • boolean applyDiscount(String code) applies a 10% discount and returns true, but only when the code is "SAVE10", no discount has been applied before, and the cart is still open. If any condition fails, the method returns false without changing the cart. The discount may be applied even when the cart is empty.
  • double getTotal() returns the sum of all item prices, multiplied by 0.9 when the discount is active. Store the original item prices and apply the discount while calculating the total. This ensures that items added after the code was accepted receive the discount too.
  • boolean checkout() checks out a non-empty, open cart and returns true. If the cart is empty or has already been checked out, it returns false without changing anything.
  • boolean isCheckedOut() returns whether checkout has succeeded.

Keep the item collection, discount state, and checkout state encapsulated. Callers must not be able to modify any of them directly or bypass the rules above.

Example 1:

Input:

Output:

Explanation: The laptop and mouse bring the subtotal to 1030.0. The valid SAVE10 code is then accepted, so getTotal() applies a 10% discount and returns 927.0.

Example 2:

Input:

Output:

Explanation: Because the cart contains a book, checkout succeeds. The cart is now closed to changes, so adding the pen returns false and the total remains 20.0.

Constraints:
  • 1 <= name.length <= 40
  • 0 <= price <= 10^5
  • At most 100 calls will be made across all methods.

How the design is graded

needs 7/10 to pass
  • Encapsulation

    Full marks when the items collection, the discount state and the checked-out flag are all private, with no method that lets a caller set them directly. Lose points for exposing the item collection itself, or for a setter that bypasses the add, discount or checkout rules.

  • State transitions

    Full marks when checkout is a one-way door that every other operation respects, and when each rejected operation leaves the cart exactly as it was. Lose points if a refused addItem or applyDiscount still mutates state, if the discount can be applied twice, or if checkout succeeds on an empty cart.

  • Structure and naming

    Full marks for one cohesive class where the total is derived from the stored items rather than tracked in a separate field that can drift. Lose points for unused fields, printing to stdout, or a hard-coded total.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new ShoppingCart()null
addItem("Laptop", 1000)true
addItem("Mouse", 30)true
getTotal()1030
applyDiscount("SAVE10")true
getTotal()927

The two items produce a subtotal of 1030. The valid SAVE10 code applies a 10% discount, bringing the total to 927.

Run checks these cases. Submit also runs a larger hidden set.