Search code examples
javanewlineadditionshopping-cart

Increase Quantity in ShoppingCart instead of adding a new line-JAVA


I am trying to create a shoppiing cart that when you addItem if it already exists in cart only increase the quantity and don't add a new line. Currently I have got the the quantity increasing right but it is adding the new quantity as a new line rather than updating the pervious line. I would be greatful for any tips on how to fix this issue.

Here is my code. This is the CartItem:

public class CartItem implements ICartItem {
private int productID;
private String productName;
private BigDecimal retailPrice;
private int quantity;
private int formatID;
private String description;

public CartItem(int productID, String productName, BigDecimal retailPrice, int quantity, int formatID, String description) {
    this.setProductID(productID);
    this.setProductName(productName);
    this.setRetailPrice(retailPrice);
    this.setQuantity(quantity);
    this.setFormatID(formatID);
    this.setDescription(description);
}

public int getProductID(){
    return productID;
}

private void setProductID(int productID) {
    this.productID = productID;
}

public String getProductName() {
    return productName;
}

private void setProductName(String productName) {
    this.productName = productName;
}


public BigDecimal getRetailPrice() {
    return recommendedRetailPrice;
}

private void setRetailPrice(BigDecimal retailPrice) {
    if (retailPrice.compareTo(new BigDecimal(0)) >= 0)
    this.retailPrice = retailPrice;
}

public int getQuantity() {
    if(this.quantity <1 || this.quantity > 800) {
        return -1;}     
    return quantity;
}

private void setQuantity(int quantity) {
    
    this.quantity = quantity;
     
}

public int getFormatID() {
    return formatID;
}

private void setFormatID(int formatID) {
    this.formatID = formatID;
}

public String getDescription() {
    return description;
}
 
private void setDescription(String description) {
    this.description = description;
}

public BigDecimal getTotalValueOfBasketItem(){
    BigDecimal totalPrice = wholesalePrice.multiply(new BigDecimal(quantity));
    return totalPrice;
 }

public int increaseQuantity(int quantity) {
    if (quantity <0) {
        return -1;}
    this.setQuantity(this.getQuantity() + quantity);
    return this.getQuantity();}
         
public int decreaseQuantity(int quantity) {
    if (quantity <0) {
        return -1;}
    this.setQuantity(this.getQuantity() - quantity);
    return this.getQuantity();
    }

This is the interface :

public interface ICartItem {
    int getProductID();
    String getProductName();
    BigDecimal getRetailPrice();
    int getQuantity();
    int increaseQuantity(int quantity);
    int decreaseQuantity(int quantity);
    int getFormatID();
    String getDescription();
    BigDecimal getTotalValueOfBasketItem();

}

This is the cartBasket:

public class CartBasket implements ICartBasket {

Customer customer = null;
private List<ICartItem> cartItems = new ArrayList<ICartItem>();


public CartBasket(Customer customer) {
    this.customer = customer;
}

public List<ICartItem> getcartItems() {
    return cartItems;
}

private void setcartItems(List<ICartItem> cartItems) {
    this.cartItems = cartItems;
}

public int getNumberOfProducts(){
    return  cartItems.size();
}

public BigDecimal getBasketTotal() {
    BigDecimal totalPrice = new BigDecimal(0);
    for (ICartItem item : cartItems){
        totalPrice = totalPrice.add(item.getTotalValueOfcartItem());    
    }
    return totalPrice;
}

public int getNumberOfItems(){
    int numberOfcartItems = 0;
    for (ICartItem item : cartItems){
        numberOfcartItems += item.getQuantity();
    }
    return numberOfcartItems;
}

public void addItem(ICartItem cartItem){

for (ICartItem item : cartItems){

if (item.getProductID()==cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID())
    item.increaseQuantity(cartItem.increaseQuantity(0));
}
cartItems.add(cartItem); 


public void removeItem(ICartItem cartItem){
    ICartItem matchingItem = findcartItem(cartItem);
    
    if (matchingItem != null) {
        cartItems.remove(matchingItem);
    }
}

public void clearBasket(){
    cartItems.clear();
}
public boolean isProductNameInBasket(String productName)
   { for (ICartItem item : cartItems){
       if (item.getProductName().equals(productName)) {
           return true;
       }
   }
   return false;

}

private ICartItem findcartItem(ICartItem cartItem){
    for (ICartItem item : cartItems){
        if (item.getProductID() == cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID() && item.getDegreeOfRoastID() == cartItem.getDegreeOfRoastID() ) {
            return item;
        }
    }
    return null;

Solution

  • You just need to check if your item already exists before adding it. You can do it either using a boolean or just making the items a set of items and defining the equals method.

    Here's an example with using a boolean:

    public void addItem(ICartItem cartItem){
        boolean exists = false;
        for (ICartItem item : cartItems){
    
            if (item.getProductID()==cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID()) {
                item.increaseQuantity(cartItem.increaseQuantity(0));
                exists = true;
            }
        }
        if (!exists) {
            cartItems.add(cartItem); 
        }
    }