Class Cart

java.lang.Object
org.salespointframework.order.Cart
All Implemented Interfaces:
Iterable<CartItem>, Supplier<Stream<CartItem>>, Streamable<CartItem>

public class Cart extends Object implements Streamable<CartItem>
Abstraction of a shopping cart.
Author:
Paul Henke, Oliver Gierke
  • Constructor Details

    • Cart

      public Cart()
  • Method Details

    • addOrUpdateItem

      public Optional<CartItem> addOrUpdateItem(Product product, Quantity quantity)
      Creates a CartItem for the given Product and Quantity. If a CartItem for the given Product already exists the Cart will be updated to reflect the combined Quantity for the backing CartItem. If the given Quantity adds up to zero and the item is removed from the Cart.
      Parameters:
      product - must not be null.
      quantity - must not be null.
      Returns:
      the created CartItem or an empty Optional if the given Quantity adds up to zero and the item is removed from the Cart.
    • addOrUpdateItem

      public Optional<CartItem> addOrUpdateItem(Product product, long amount)
      Creates a CartItem for the given Product and amount. If a CartItem for the given Product already exists the Cart will be updated to reflect the combined Quantity for the backing CartItem. If the given Quantity adds up to zero and the item is removed from the Cart.
      Parameters:
      product - must not be null.
      amount - must not be null.
      Returns:
      the created CartItem or an empty Optional if the given Quantity adds up to zero and the item is removed from the Cart.
    • addOrUpdateItem

      public Optional<CartItem> addOrUpdateItem(Product product, double amount)
      Creates a CartItem for the given Product and amount. If a CartItem for the given Product already exists the Cart will be updated to reflect the combined Quantity for the backing CartItem. If the given Quantity adds up to zero and the item is removed from the Cart.
      Parameters:
      product - must not be null.
      amount - must not be null.
      Returns:
      the created CartItem or an empty Optional if the given Quantity adds up to zero and the item is removed from the Cart.
    • removeItem

      public void removeItem(String identifier)
      Removes the CartItem with the given identifier.
      Parameters:
      identifier - must not be null.
    • getItem

      public Optional<CartItem> getItem(String identifier)
      Returns the CartItem for the given identifier.
      Parameters:
      identifier - must not be null.
      Returns:
    • clear

      public void clear()
      Clears the cart.
    • isEmpty

      public boolean isEmpty()
      Returns whether the Cart is currently empty.
      Specified by:
      isEmpty in interface Streamable<CartItem>
      Returns:
    • addItemsTo

      public Order addItemsTo(Order order)
      Adds all items currently contained in this cart to the given Order.
      Parameters:
      order - must not be null.
      Returns:
      the Order with the added items of this cart
      Throws:
      IllegalStateException - if the given Order is not OrderStatus.OPEN anymore.
    • createOrderFor

      public Order createOrderFor(UserAccount user)
      Creates a new Order for the given UserAccount from the current Cart.
      Parameters:
      user - must not be null.
      Returns:
      a new Order for the current Cart and given UserAccount.
    • getNumberOfItems

      public int getNumberOfItems()
      Returns the number of items currently in the cart. Sums up the contained Product's units defaulting to a single unit in case the Product is not handled in units.
      Returns:
      will never be null.
      Since:
      7.5
      See Also:
    • getQuantity

      public Quantity getQuantity(Product product)
      Returns the quantity for the given Product currently contained in the Cart.
      Parameters:
      product - must not be null.
      Returns:
      will never be null.
      Since:
      7.5
    • getQuantity

      public Quantity getQuantity(Product.ProductIdentifier identifier)
      Returns the quantity for the given Product.ProductIdentifier currently contained in the Cart.
      Parameters:
      identifier - must not be null.
      Returns:
      will never be null.
      Since:
      7.5
    • getPrice

      public javax.money.MonetaryAmount getPrice()
    • iterator

      public Iterator<CartItem> iterator()
      Specified by:
      iterator in interface Iterable<CartItem>