因此,我正在通過創建虛擬專案來研究我的 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)
管理員必須在 add_Product function() 中傳入使用 Product 類創建的產品。我想要做的是當管理員嘗試兩次上傳相同的產品時,只有通過添加以前的計數和新的計數來改變它的計數。
但由于某種原因,我無法做到這一點,我嘗試了多種方法,但仍然堅持下去。這可能是我錯過的非常簡單的事情。
提前致謝!
uj5u.com熱心網友回復:
您檢查產品是否已在字典中使用產品物件而不是其 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/496393.html
上一篇:PythonOOP分離一個類
下一篇:嘗試PythonRPG的OOP
