Class ShoppingItem
java.lang.Object
com.goldenleaf.shop.model.ShoppingItem
Represents an item inside a shopping cart. Each ShoppingItem binds a
specific Product with a ShoppingCart and stores the quantity
of that product selected by the user.
The class ensures that the assigned product is never null and that
the quantity is always at least 1.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor required by JPA.ShoppingItem(Product product, int quantity) Constructs a newShoppingItemwith the given product and quantity. -
Method Summary
Modifier and TypeMethodDescriptiongetCart()getId()intvoidsetCart(ShoppingCart cart) Sets the cart that contains this shopping item.voidsetProduct(Product product) Sets a new product for this shopping item.voidsetQuantity(int quantity) Updates the quantity of this item.
-
Constructor Details
-
ShoppingItem
public ShoppingItem()Default constructor required by JPA.
-
ShoppingItem
public ShoppingItem(Product product, int quantity) throws EmptyProductException, IncorrectQuantityException Constructs a new
ShoppingItemwith the given product and quantity. Validates both parameters before assignment.- Parameters:
product- the product to assign; must not benullquantity- the quantity of the product; must be at least 1- Throws:
EmptyProductException- ifproductisnullIncorrectQuantityException- ifquantityis less than 1- See Also:
-
-
Method Details
-
getId
- Returns:
- the unique ID of this shopping item
-
getProduct
- Returns:
- the product assigned to this item
- See Also:
-
getQuantity
public int getQuantity()- Returns:
- the quantity of the product
-
setProduct
Sets a new product for this shopping item.
- Parameters:
product- the product to assign; must not benull- Throws:
EmptyProductException- if the provided product isnull- See Also:
-
setQuantity
Updates the quantity of this item.
- Parameters:
quantity- must be at least 1- Throws:
IncorrectQuantityException- ifquantityis less than 1
-
getCart
- Returns:
- the shopping cart containing this item
- See Also:
-
setCart
Sets the cart that contains this shopping item.
- Parameters:
cart- the shopping cart to associate with this item- See Also:
-