Class ShoppingCart
Represents a shopping cart belonging to a Customer. The cart contains
multiple ShoppingItem objects, each storing a product and its quantity.
The cart is responsible for maintaining item relationships, calculating the total price, and handling item additions or removals.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor required by JPA.ShoppingCart(List<ShoppingItem> items, Customer customer, double totalPrice) Constructs a newShoppingCartwith predefined items, customer and total price. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new item to the cart based on a product and quantity.doubleCalculates and returns the total price of all items in the cart.voidclear()Removes all items from the cart.getId()getItems()doublevoidremoveItem(Product product) Removes an item from the cart by its product.voidremoveItem(ShoppingItem item) Removes the specified item from the cart and clears its cart reference.voidsetCustomer(Customer customer) Assigns a customer to the cart.voidsetItems(List<ShoppingItem> items) Replaces the list of items with a new one and updates cart references.voidsetTotalPrice(double totalPrice) Sets a new total price for the cart.
-
Constructor Details
-
ShoppingCart
public ShoppingCart()Default constructor required by JPA.
-
ShoppingCart
Constructs a new
ShoppingCartwith predefined items, customer and total price.- Parameters:
items- list of shopping itemscustomer- owner of the carttotalPrice- pre-calculated total price
-
-
Method Details
-
getId
- Returns:
- the unique identifier of this cart
-
getItems
- Returns:
- a list of items inside the cart
- See Also:
-
setItems
Replaces the list of items with a new one and updates cart references.
- Parameters:
items- new list of items- Throws:
EmptyProductException- if any item's product is null- See Also:
-
addItem
public void addItem(Product product, int quantity) throws EmptyProductException, IncorrectQuantityException Adds a new item to the cart based on a product and quantity.
- Parameters:
product- the product to addquantity- number of units- Throws:
EmptyProductException- if the product is nullIncorrectQuantityException- if quantity invalid input: '<' 1- See Also:
-
removeItem
Removes the specified item from the cart and clears its cart reference.
- Parameters:
item- the item to remove
-
clear
public void clear()Removes all items from the cart.
-
removeItem
Removes an item from the cart by its product.
- Parameters:
product- the product whose item should be removed
-
setCustomer
Assigns a customer to the cart.
- Parameters:
customer- the customer to set- See Also:
-
calculateTotalPrice
public double calculateTotalPrice()Calculates and returns the total price of all items in the cart.
- Returns:
- sum of (product price × quantity) for every item
- See Also:
-
getTotalPrice
public double getTotalPrice()- Returns:
- the stored total price field
-
setTotalPrice
Sets a new total price for the cart.
- Parameters:
totalPrice- must be >= 0- Throws:
IncorrectPriceException- if price is negative
-