Python实现简易进销存系统
在企业管理中,进销存系统是不可或缺的工具。它能帮助企业高效管理库存、销售和采购。今天,我们将用Python编写一个简单的进销存系统,适合初学者学习和实践。

系统设计
首先,我们需要明确系统的基本功能:
- 库存管理:添加、查询、更新和删除商品。
- 销售管理:记录销售信息。
- 采购管理:记录采购信息。
我们将使用Python的内置数据结构来存储数据,无需安装额外的库。
数据结构
为了简化,我们使用字典来存储商品信息和销售采购记录。
inventory = {} # 存储商品信息,格式:{商品ID: [名称, 数量, 单价]}
sales = [] # 存储销售记录,格式:[商品ID, 数量, 金额]
purchases = [] # 存储采购记录,格式:[商品ID, 数量, 金额]
功能实现
1. 库存管理
添加商品
def add_product(product_id, name, quantity, price):
if product_id in inventory:
print("商品已存在")
else:
inventory[product_id] = [name, quantity, price]
print("商品添加成功")
查询商品
def query_product(product_id):
if product_id in inventory:
name, quantity, price = inventory[product_id]
print(f"商品名称: {name}, 数量: {quantity}, 单价: {price}")
else:
print("商品不存在")
更新商品
def update_product(product_id, name=None, quantity=None, price=None):
if product_id in inventory:
if name:
inventory[product_id][0] = name
if quantity:
inventory[product_id][1] = quantity
if price:
inventory[product_id][2] = price
print("商品更新成功")
else:
print("商品不存在")
删除商品
def delete_product(product_id):
if product_id in inventory:
del inventory[product_id]
print("商品删除成功")
else:
print("商品不存在")
2. 销售管理
记录销售
def record_sale(product_id, quantity):
if product_id in inventory and inventory[product_id][1] >= quantity:
name, stock, price = inventory[product_id]
inventory[product_id][1] -= quantity
amount = quantity * price
sales.append([product_id, quantity, amount])
print(f"销售成功,金额: {amount}")
else:
print("库存不足或商品不存在")
3. 采购管理
记录采购
def record_purchase(product_id, quantity, price):
if product_id in inventory:
inventory[product_id][1] += quantity
amount = quantity * price
purchases.append([product_id, quantity, amount])
print(f"采购成功,金额: {amount}")
else:
print("商品不存在")
用户界面
为了方便使用,我们设计一个简单的命令行界面。
def main_menu():
while True:
print("\n1. 库存管理")
print("2. 销售管理")
print("3. 采购管理")
print("4. 退出")
choice = input("请选择操作: ")
if choice == '1':
inventory_menu()
elif choice == '2':
sales_menu()
elif choice == '3':
purchase_menu()
elif choice == '4':
break
else:
print("无效选择")
def inventory_menu():
while True:
print("\n1. 添加商品")
print("2. 查询商品")
print("3. 更新商品")
print("4. 删除商品")
print("5. 返回主菜单")
choice = input("请选择操作: ")
if choice == '1':
product_id = input("输入商品ID: ")
name = input("输入商品名称: ")
quantity = int(input("输入数量: "))
price = float(input("输入单价: "))
add_product(product_id, name, quantity, price)
elif choice == '2':
product_id = input("输入商品ID: ")
query_product(product_id)
elif choice == '3':
product_id = input("输入商品ID: ")
name = input("输入新名称(留空不修改): ")
quantity = input("输入新数量(留空不修改): ")
price = input("输入新
« 上一篇
Python入门:轻松实现数据可视化
下一篇 »
Python并发编程入门指南