我想使用类和数据结构创建一个简单的库存管理系统.我是python中OOP的新手



代码应捕获库存产品(即名称、价格、供应日期、供应商名称、数量(,在每次购买时指定名称时检索价格,并将数量扣除库存,计算每次购买的总价,并打印显示日期和时间的值购买,当数量达到5时向店主发送警报,并向供应商下订单。

我现在的代码没有循环,所以我可以添加一些产品,然后可以访问它们,我尝试过使用while循环,但它一直在运行。请帮助

import datetime
class Stock:
def __init__(self, name, price, supplier, quantity,date):
self.name = name
self.price = price
self.date = date
self.supplier = supplier
self. quantity = quantity
def check_item(self, name):
for i in range(len(ls)):
if (ls[i].name == name):
return i
def sale(self):
n = int(input("How many products to sale: "))
total = 0
for j in range(n):
name = input("Enter Product name : ")
quantity = int(input("Enter quantity: "))
i = obj.check_item(name)
if i and ls[i].quantity >= quantity:
ls[i].quantity -= quantity
if ls[i].quantity < 5:
print("Low Stock! Low Stock! Order Placed")
obj.place_order(name, ls[i].supplier, quantity+10)
print("....Uncle G Shop....")
print(datetime.date.today())
print("Product Name  | Quantity  | Cost $")
print(ls[i].name, end=" ")
print(quantity, end=" ")
print(ls[i].price * quantity)
total += ls[i].price * quantity
print("n")
print("Total Cost----->", "$" + total)
else:
print("Product out of stock or not enough quantity")

def purchase(self):
name = input("Enter Product name: ")
date = datetime.date.today()
i = obj.check_item(name)
if i:
ls[i].quantity += int(input("Enter quantity: "))
ls[i].price = int(input("Enter product price: "))
ls[i].date = date
else:
quantity = int(input("Enter quantity: "))
price = int(input("Enter product price: "))
supplier = input("Enter Supplier: ")
ob = Stock(name, price, supplier, quantity, date)
ls.append(ob)

def place_order(self,name, supplier, quantity):
return name, supplier, quantity
def print_products(self):
def __repr__(self):
return str(self.name) + str(self.price) + str(supplier) + str(self.quantity) + str(self.date)
return __repr__
def main(self):
print("Welcome To Uncle G Shop")
print("choose an option below")
print("n1.Enter a Productn2.Make a sale n3.See all Productsn4.Exit")
option = int(input("Enter option here: "))
while True:

if option == 1:
obj.purchase()
elif option == 2:
obj.sale()
elif option == 3:
obj.print_products()
elif option == 4:
print("Have a good day")
break
else:
print("Enter a valid input!")
# A list to add Products
ls = []
# an object of Stock class
obj = Stock('', 0, 0, 0, '')
obj.main()

您的main菜单没有来自选项4的while循环的break

你还需要重新思考如何使用你的课堂。目前,有多个方法引用在类之外创建的变量ls。要么将Stock作为一个类来处理单个采购记录,要么处理整个库存。您可以在类本身中维护该类实例的list,并提供类方法和实例方法来管理整个库存。

关于循环问题,您只需要在while循环中向下移动option = int(input("Enter option here: "))行。

除此之外,你还有很多其他问题,你可能已经解决或完全忘记了,因为这已经一年多了。。。Lol

最新更新