购物车数量不更新

时间:2017-10-18 20:36:37

标签: python-3.6

我有一个基本的购物车。我认为它有效但我已经发现即使它没有错误运行也没有做我认为应该做的事情而且我在解决问题时遇到了麻烦。

如果我第二次实例化一个项目,就像这样:

    item1 = Item(1, "Cucumbers", 1., 1, 'kg')
    item2 = Item(2, "Tissues", 2., 2, 'dozen')
    item3 = Item(3, "Tomatoes", 3., 5, 'pound')
    # item4 = Item(4, "Toothpaste", 1., 5, 'box')
    item4 = Item(4, "Cucumbers", 1., 2, 'kg')

我想要的是第1项黄瓜的数量增加到'3' 并且要自动删除第4项。

我认为这是按照我的意图行事,但事实并非如此:

            elif k == 'name':
                total_qty = v.qty + item.qty
                if total_qty:
                    v.qty = total_qty
                    continue
                self.remove_item(k)
            else:
                v[k] = item[k]

代码运行没有错误,但我最终得到了两个单独的黄瓜实例。

完整代码:

class Item(object):
    def __init__(self, unq_id, name, price, qty, measure):
        self.unq_id = unq_id
        self.product_name = name
        self.price = price
        self.qty = qty
        self.measure = measure


class Cart(object):
    def __init__(self):
        self.content = dict()

    def __format__(self, format_type):
        if format_type == 'short':
            return ', '.join(item.product_name for item in self.content.values())

        elif format_type == 'long':
            return '\n'.join(f'\t\t{item.qty:2} {item.measure:7} {item.product_name:12} @ '
                             f'${item.price:1.2f} ... ${item.qty * item.price:1.2f}'
                             for item in self.content.values())

    def add(self, item):
        if item.unq_id not in self.content:
            self.content.update({item.unq_id: item})
            return
        for k, v in self.content.get(item.unq_id).items():
            if k == 'unq_id':
                continue
            elif k == 'name':
                total_qty = v.qty + item.qty
                if total_qty:
                    v.qty = total_qty
                    continue
                self.remove_item(k)
            else:
                v[k] = item[k]

    def get_total(self):
        return sum([v.price * v.qty for _, v in self.content.items()])

    def get_num_items(self):
        return sum([v.qty for _, v in self.content.items()])

    def remove_item(self, key):
        self.content.pop(key)


if __name__ == '__main__':
    item1 = Item(1, "Cucumbers", 1., 1, 'kg')
    item2 = Item(2, "Tissues", 2., 2, 'dozen')
    item3 = Item(3, "Tomatoes", 3., 5, 'pound')
    # item4 = Item(4, "Toothpaste", 1., 5, 'box')
    item4 = Item(4, "Cucumbers", 1., 2, 'kg')
    cart = Cart()
    cart.add(item1)
    cart.add(item2)
    cart.add(item3)
    cart.add(item4)
    print("Your cart contains: {0:short}".format(cart))
    # cart.remove_item(1)
    print()
    print("Your cart contains: \n {0:long}".format(cart))
    print()
    print("The total number of items in your cart is: ", cart.get_num_items())
    print()
    print("The total cost of the items in your cart is: ", cart.get_total())
    print()
    cart.remove_item(3)
    print("Your cart contains: {0:short}".format(cart))
    print()
    print("Your cart contains: \n {0:long}".format(cart))
    print()
    print("The total number of items in your cart is: ", cart.get_num_items())
    print()
    print("The total cost of the items in your cart is: ", cart.get_total())

1 个答案:

答案 0 :(得分:0)

修复方法是:

def add(self, item):
    if item.unq_id not in self.content:
        self.content.update({item.unq_id: item})
        return
    else:
        self.content[item.unq_id].qty = self.content[item.unq_id].qty + item.qty