不能增加存储在嵌套字典中的乘积计数- python OOP



所以,我正在通过创建虚拟项目来研究我的OOP概念和知识。现在我正在创建一个在线购物系统,管理员可以上传不同的产品,用户可以购买产品。

我在使用这个特定的功能时遇到了问题,我想增加产品计数。

产品类:

class Product:
def __init__(self, product_name, description, price, company, product_count):
self.product_name = product_name
self.description = description
self.price = price
self.company = company
self.product_count = product_count

,这就是你如何创建一个产品:

#CREATE PRODUCT
laptop = Product('Dell Inspiron', 'Dell Inspiron with 8GB ram', 1500, 'DELL', 100)
tv = Product('Sony TV', 'Best Sony TV Ever', 2000, 'Sony', 100)
smartwatch = Product('Fossil 6 Gen Smartwatch', 'Fossil watch with wear OS', 300, 'Fossil',100)
shoes = Product('Running shoes', 'Medium size running shoes', 100, 'Nike',100)
gloves = Product('Rock CLimbing gloves', 'Medium gloves for rock clmbing', 250, 'Timberland',100)

只有管理员可以上传产品,我将它们存储在字典中:

管理类:

class Admin:
def __init__(self, name):
self.name = name
self.products = {}
self.order = {}
self.total_products = 0
self.orders_recieved = {}
self.order_status = 'Order Recieved'

add_products的逻辑:

def add_products(self, product):
if product not in self.products:
self.products[product.product_name] = {
'name':product.product_name,
'description':product.description,
'price': product.price,
'company':product.company,
'count':product.product_count
}
if product not in s.site_products:  #I am also storing all products in different dict() ignore this
s.site_products[product.product_name] = {
'name':product.product_name,
'description':product.description,
'price': product.price,
'company':product.company,
'count':product.product_count
}
s.product_total += 1
self.total_products += 1
else:
product.product_count += product.product_count

print('Product already exists')

#这是添加产品的方式:

admin1.add_products(laptop)
admin1.add_products(laptop)
admin1.add_products(tv)
admin1.add_products(smartwatch)
admin2.add_products(shoes)
admin2.add_products(gloves)

Admin必须传入使用add_Product函数()中的product类创建的产品。我想做的是,当管理员尝试两次上传相同的产品时,只有其计数通过添加前一个计数和新的计数而改变。

但由于某种原因,我无法做到这一点,我已经尝试了多种方法,但仍然坚持下去。我可能错过了一些非常简单的东西。

提前感谢!

检查产品是否已经在字典中,使用product对象而不是其product_name属性,后者是用于向字典中添加条目的键。

如果你改变那一行,它应该可以工作。

测试代码:

class Product:
def __init__(self, product_name, description, price, company, product_count):
self.product_name = product_name
self.description = description
self.price = price
self.company = company
self.product_count = product_count
class Admin:
def __init__(self, name):
self.name = name
self.products = {}
self.total_products = 0
def add_products(self, product):
if product.product_name not in self.products:
self.products[product.product_name] = {
'name':product.product_name,
'description':product.description,
'price': product.price,
'company':product.company,
'count':product.product_count
}
self.total_products += 1
else:
self.products[product.product_name]['count'] += product.product_count
print('Product already exists')
a = Admin("foo")
print('try a product')
a.add_products(Product('Dell Inspiron', 'Dell Inspiron with 8GB ram', 1500, 'DELL', 100))
print('try it again')
a.add_products(Product('Dell Inspiron', 'Dell Inspiron with 8GB ram', 1500, 'DELL', 100))
输出:

try a product
try it again
Product already exists

最新更新