用Python写一个商品销售管理系统
时间: 2024-01-05 13:03:21 浏览: 86
产品销售管理系统(python)
好的,我可以为你提供一个简单的商品销售管理系统的Python代码实现,具体如下:
```
class Product:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
class Sales:
def __init__(self):
self.sales = []
def add_sale(self, product, quantity):
self.sales.append((product, quantity))
def get_total_sales(self):
total_sales = 0
for product, quantity in self.sales:
total_sales += product.price * quantity
return total_sales
class Inventory:
def __init__(self):
self.products = []
def add_product(self, product):
self.products.append(product)
def get_product(self, name):
for product in self.products:
if product.name == name:
return product
return None
def update_product_quantity(self, name, quantity):
product = self.get_product(name)
if product:
product.quantity += quantity
class Menu:
def __init__(self):
self.inventory = Inventory()
self.sales = Sales()
def add_product(self):
name = input("Enter product name: ")
price = float(input("Enter product price: "))
quantity = int(input("Enter product quantity: "))
product = Product(name, price, quantity)
self.inventory.add_product(product)
def sell_product(self):
name = input("Enter product name: ")
product = self.inventory.get_product(name)
if product:
quantity = int(input("Enter quantity to sell: "))
if product.quantity >= quantity:
self.sales.add_sale(product, quantity)
self.inventory.update_product_quantity(name, -quantity)
print("Sale successful!")
else:
print("Not enough stock!")
else:
print("Product not found!")
def view_inventory(self):
print("\n".join([f"{product.name}: {product.quantity}" for product in self.inventory.products]))
def view_sales(self):
total_sales = self.sales.get_total_sales()
print(f"Total sales: {total_sales}")
def show_menu(self):
while True:
print("\n1. Add product")
print("2. Sell product")
print("3. View inventory")
print("4. View sales")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
self.add_product()
elif choice == "2":
self.sell_product()
elif choice == "3":
self.view_inventory()
elif choice == "4":
self.view_sales()
elif choice == "5":
break
else:
print("Invalid choice!")
menu = Menu()
menu.show_menu()
```
以上代码实现了一个简单的商品销售管理系统,包括商品管理、销售管理和库存管理等功能。你可以根据自己的需要进行修改和扩展。
阅读全文