用python写一个超市商品管理系统
时间: 2023-11-11 17:04:26 浏览: 347
python实现超市管理系统(后台管理).doc
5星 · 资源好评率100%
好的,下面是一个简单的超市商品管理系统的 Python 代码示例:
```
class Product:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
class Inventory:
def __init__(self):
self.products = []
def add_product(self, product):
self.products.append(product)
def remove_product(self, product):
self.products.remove(product)
def search_product(self, name):
for product in self.products:
if product.name == name:
return product
return None
class ShoppingCart:
def __init__(self):
self.products = []
def add_product(self, product):
self.products.append(product)
def remove_product(self, product):
self.products.remove(product)
def checkout(self):
total_price = 0
for product in self.products:
total_price += product.price * product.quantity
return total_price
inventory = Inventory()
shopping_cart = ShoppingCart()
while True:
print("1. Add product")
print("2. Remove product")
print("3. Search product")
print("4. Add to cart")
print("5. Remove from cart")
print("6. Checkout")
print("7. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter product name: ")
price = float(input("Enter product price: "))
quantity = int(input("Enter product quantity: "))
product = Product(name, price, quantity)
inventory.add_product(product)
elif choice == 2:
name = input("Enter product name: ")
product = inventory.search_product(name)
if product:
inventory.remove_product(product)
else:
print("Product not found")
elif choice == 3:
name = input("Enter product name: ")
product = inventory.search_product(name)
if product:
print("Name: ", product.name)
print("Price: ", product.price)
print("Quantity: ", product.quantity)
else:
print("Product not found")
elif choice == 4:
name = input("Enter product name: ")
product = inventory.search_product(name)
if product:
quantity = int(input("Enter quantity: "))
shopping_cart.add_product(Product(product.name, product.price, quantity))
else:
print("Product not found")
elif choice == 5:
name = input("Enter product name: ")
product = None
for p in shopping_cart.products:
if p.name == name:
product = p
break
if product:
shopping_cart.remove_product(product)
else:
print("Product not found")
elif choice == 6:
total_price = shopping_cart.checkout()
print("Total price: ", total_price)
shopping_cart = ShoppingCart()
elif choice == 7:
break
else:
print("Invalid choice")
```
这个示例程序允许用户添加、删除、搜索商品,以及向购物车中添加、移除商品,结账等功能。你可以根据自己的需求和业务逻辑进行修改和扩展。
阅读全文