Interface Cart
- All Known Implementing Classes:
StoreCart
public interface Cart
Represents a shopping cart that holds item orders.
A Cart manages a collection of ItemOrder objects and calculates the total cost based on whether the customer has a store membership. The cart ensures that only one order exists per unique Item - adding a new order for an existing item replaces the previous order.
- Version:
- Winter 2026
- Author:
- Charles Bryan
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordA record representing the size of a shopping cart. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an order to the shopping cart.Calculates the total cost of all items in the shopping cart.voidclear()Removes all orders from the shopping cart.Returns the current size of the shopping cart.voidsetMembership(boolean theMembership) Sets whether the customer has a store membership.
-
Method Details
-
add
Adds an order to the shopping cart.If an order for the same item already exists, it is replaced with the new order. If the order quantity is 0, the item is removed from the cart entirely.
- Parameters:
theOrder- the ItemOrder to add to the cart- Throws:
NullPointerException- if theOrder is null
-
setMembership
void setMembership(boolean theMembership) Sets whether the customer has a store membership.Membership status affects pricing - members receive bulk discounts on eligible items.
- Parameters:
theMembership- true if the customer has a membership, false otherwise
-
calculateTotal
BigDecimal calculateTotal()Calculates the total cost of all items in the shopping cart.The total is calculated by summing the cost of each ItemOrder, taking into account the membership status. The returned value has a scale of 2 and uses HALF_EVEN (banker's) rounding.
- Returns:
- the total cost of the shopping cart
-
clear
void clear()Removes all orders from the shopping cart. -
getCartSize
Cart.CartSize getCartSize()Returns the current size of the shopping cart.- Returns:
- a CartSize record containing the number of unique item orders and the total quantity of all items
-