【问题标题】:Shopping cart not updating the quantity from second attempt onwards购物车未从第二次尝试更新数量
【发布时间】:2016-03-27 16:07:36
【问题描述】:

我正在尝试使用两个 java 类来实现购物车流程。一个是CartItem,另一个是ShoppingCart,它包含购物车的所有进程。

这是我的CartItem:

public class CartItem implements Serializable {

    private int pro_id;
    private int qnty;


    public CartItem(int pro_id, int qnty) {
        this.pro_id = pro_id;
        this.qnty = qnty;

    }
    // getters and setters
    ....
        // checking the item is already in the cart or not
        public boolean isSameProduct(CartItem item) {
        return this.pro_id == item.getPro_id();

    }

我的ShoppingCart 班级:

public class ShoppingCart {

    private CopyOnWriteArrayList<CartItem> itemList;


    public ShoppingCart() {
        this.itemList = new CopyOnWriteArrayList<CartItem>();

    }
    // adding item something wrong here I think..
    public boolean addItem(CartItem item) {
        boolean flag = false;
        if (!this.itemList.isEmpty()) {
            for (CartItem itm : this.itemList) {
                if (itm.isSameProduct(item)) {
                    itm.setQnty(item.getQnty() + itm.getQnty());
                    flag = true;
                   break;

                } else {
                    this.itemList.add(item);
                    flag = true;
                    break;
                }

            }
        } else {
            this.itemList.add(item);
            flag = true;
        }
        return flag;

    }

    public void removeItem(int item_id) {
        for (CartItem item : this.itemList) {
            if (item.getPro_id() == item_id) {
                this.itemList.remove(item);
            }
        }
    }

这是我添加产品和输出的方式:

ShoppingCart sc = new ShoppingCart();
sc.addItem(new CartItem(1, 1));
sc.addItem(new CartItem(1, 2));
sc.addItem(new CartItem(1, 3));
sc.addItem(new CartItem(1, 1));
sc.addItem(new CartItem(2, 1));
sc.addItem(new CartItem(2, 2));
sc.addItem(new CartItem(3, 4));
sc.addItem(new CartItem(3, 1));
 // sc.removeItem(3);

for (CartItem item : sc.itemList) {
    System.out.println("Item id - " + item.getPro_id() + " : Item Qnty - " + item.getQnty());
}

输出:

Item id - 1 : Item Qnty - 7
Item id - 2 : Item Qnty - 1
Item id - 2 : Item Qnty - 2
Item id - 3 : Item Qnty - 4
Item id - 3 : Item Qnty - 1

但意料之中:

Item id - 1 : Item Qnty - 7
Item id - 2 : Item Qnty - 3
Item id - 3 : Item Qnty - 5

提前致谢。

【问题讨论】:

  • 你试过调试代码吗?可以在调试模式下逐行执行,也可以使用打印语句跟踪程序在每一步的行为。

标签: java list generics arraylist copyonwritearraylist


【解决方案1】:

一旦您在购物车中有一个商品,您就不会在购物车中的第一个商品之后真正检查任何内容,因为您在遍历列表元素时总是会中断:

        for (CartItem itm : this.itemList) {
            if (itm.isSameProduct(item)) {
                itm.setQnty(item.getQnty() + itm.getQnty());
                flag = true;
               break;  // It was the same product; break.

            } else {
                this.itemList.add(item);
                flag = true;
                break;  // It wasn't the same product; break.
            }
        }

相反,您需要使用flag 的值,并且仅当flag 为false 时才添加项目:

public boolean addItem(CartItem item) {
    boolean flag = false;
    for (CartItem itm : this.itemList) {
        if (itm.isSameProduct(item)) {
            itm.setQnty(item.getQnty() + itm.getQnty());
            flag = true;
            break;
        }
    }
    if (!flag) {
        this.itemList.add(item);
        flag = true;
    }
    return flag;
}

【讨论】:

    猜你喜欢
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2021-04-19
    • 2021-01-29
    相关资源
    最近更新 更多