Class StoreCart

java.lang.Object
edu.uw.tcss.model.StoreCart
All Implemented Interfaces:
Cart

public final class StoreCart extends Object implements Cart
Implementation of a shopping cart for the bookstore.

This cart stores ItemOrder objects, ensuring only one order exists per unique Item. When calculating totals, it delegates to each Item to calculate its own total based on quantity and membership status.

Version:
Winter 2026
Author:
Charles Bryan
  • Constructor Details

    • StoreCart

      public StoreCart()
      Constructs a new empty StoreCart.

      The customer does not have a membership by default.

  • Method Details

    • add

      public void add(ItemOrder theOrder)
      Adds an order to the cart.

      Replaces any previous order for the same item. If quantity is 0, removes the item from the cart.

      Specified by:
      add in interface Cart
      Parameters:
      theOrder - the ItemOrder to add
      Throws:
      NullPointerException - if theOrder is null
    • setMembership

      public void setMembership(boolean theMembership)
      Sets the customer's membership status.

      Members receive bulk pricing on eligible items.

      Specified by:
      setMembership in interface Cart
      Parameters:
      theMembership - true if the customer has a membership
    • calculateTotal

      public BigDecimal calculateTotal()
      Returns the total cost of all items in the cart.

      Uses HALF_EVEN (banker's) rounding with scale of 2.

      Specified by:
      calculateTotal in interface Cart
      Returns:
      the total cost of the shopping cart
    • clear

      public void clear()
      Removes all orders from the cart.
      Specified by:
      clear in interface Cart
    • getCartSize

      public Cart.CartSize getCartSize()
      Returns the current size of the shopping cart.
      Specified by:
      getCartSize in interface Cart
      Returns:
      a CartSize record with itemOrderCount (unique items) and itemCount (total quantity)
    • toString

      public String toString()
      Returns a debug representation of this cart.

      The exact format is not specified and should not be relied upon.

      Overrides:
      toString in class Object
      Returns:
      a string representation for debugging