Class ShoppingItem

java.lang.Object
com.goldenleaf.shop.model.ShoppingItem

@Entity public class ShoppingItem extends Object

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 Details

    • ShoppingItem

      public ShoppingItem()

      Default constructor required by JPA.

    • ShoppingItem

      public ShoppingItem(Product product, int quantity) throws EmptyProductException, IncorrectQuantityException

      Constructs a new ShoppingItem with the given product and quantity. Validates both parameters before assignment.

      Parameters:
      product - the product to assign; must not be null
      quantity - the quantity of the product; must be at least 1
      Throws:
      EmptyProductException - if product is null
      IncorrectQuantityException - if quantity is less than 1
      See Also:
  • Method Details

    • getId

      public Long getId()
      Returns:
      the unique ID of this shopping item
    • getProduct

      public Product getProduct()
      Returns:
      the product assigned to this item
      See Also:
    • getQuantity

      public int getQuantity()
      Returns:
      the quantity of the product
    • setProduct

      public void setProduct(Product product) throws EmptyProductException

      Sets a new product for this shopping item.

      Parameters:
      product - the product to assign; must not be null
      Throws:
      EmptyProductException - if the provided product is null
      See Also:
    • setQuantity

      public void setQuantity(int quantity) throws IncorrectQuantityException

      Updates the quantity of this item.

      Parameters:
      quantity - must be at least 1
      Throws:
      IncorrectQuantityException - if quantity is less than 1
    • getCart

      public ShoppingCart getCart()
      Returns:
      the shopping cart containing this item
      See Also:
    • setCart

      public void setCart(ShoppingCart cart)

      Sets the cart that contains this shopping item.

      Parameters:
      cart - the shopping cart to associate with this item
      See Also: