Class ShoppingCart

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

@Entity public class ShoppingCart extends Object

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 Details

    • ShoppingCart

      public ShoppingCart()

      Default constructor required by JPA.

    • ShoppingCart

      public ShoppingCart(List<ShoppingItem> items, Customer customer, double totalPrice)

      Constructs a new ShoppingCart with predefined items, customer and total price.

      Parameters:
      items - list of shopping items
      customer - owner of the cart
      totalPrice - pre-calculated total price
  • Method Details

    • getId

      public Long getId()
      Returns:
      the unique identifier of this cart
    • getItems

      public List<ShoppingItem> getItems()
      Returns:
      a list of items inside the cart
      See Also:
    • setItems

      public void setItems(List<ShoppingItem> items) throws EmptyProductException

      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 add
      quantity - number of units
      Throws:
      EmptyProductException - if the product is null
      IncorrectQuantityException - if quantity invalid input: '<' 1
      See Also:
    • removeItem

      public void removeItem(ShoppingItem item)

      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

      public void removeItem(Product product)

      Removes an item from the cart by its product.

      Parameters:
      product - the product whose item should be removed
    • setCustomer

      public void setCustomer(Customer customer)

      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

      public void setTotalPrice(double totalPrice) throws IncorrectPriceException

      Sets a new total price for the cart.

      Parameters:
      totalPrice - must be >= 0
      Throws:
      IncorrectPriceException - if price is negative