代码之家  ›  专栏  ›  技术社区  ›  MarkS

购物车数量未更新

  •  0
  • MarkS  · 技术社区  · 8 年前

    我有一个基本的购物车。我原以为它能工作,但后来我发现,即使它运行时没有出错,但它并没有做我认为它应该做的事情,我在解决这个问题时遇到了困难。

    如果我再次实例化一个项目,如下所示:

        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]
    

    代码运行时没有错误,但我最终得到了cucumber的两个独立实例。

    完整代码:

    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 回复  |  直到 8 年前
        1
  •  0
  •   MarkS    8 年前

    解决方案是:

    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
    
    推荐文章