parent
cb1e2838cc
commit
f494ca0e42
@ -0,0 +1,144 @@
|
|||||||
|
class Warehouse:
|
||||||
|
def __init__(self):
|
||||||
|
# 使用字典存储商品及其库存,键为商品名称,值为库存数量
|
||||||
|
self.inventory = {}
|
||||||
|
|
||||||
|
def add_item(self, item_name, quantity):
|
||||||
|
"""添加商品到仓库"""
|
||||||
|
if item_name in self.inventory:
|
||||||
|
self.inventory[item_name] += quantity
|
||||||
|
else:
|
||||||
|
self.inventory[item_name] = quantity
|
||||||
|
print(f"已添加 {quantity} 个 {item_name} 到仓库。")
|
||||||
|
|
||||||
|
def remove_item(self, item_name, quantity):
|
||||||
|
"""从仓库移除商品"""
|
||||||
|
if item_name not in self.inventory:
|
||||||
|
print(f"{item_name} 不在仓库中。")
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.inventory[item_name] < quantity:
|
||||||
|
print(f"库存不足,当前 {item_name} 只有 {self.inventory[item_name]} 个。")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.inventory[item_name] -= quantity
|
||||||
|
if self.inventory[item_name] == 0:
|
||||||
|
del self.inventory[item_name]
|
||||||
|
print(f"已从仓库移除 {quantity} 个 {item_name}。")
|
||||||
|
|
||||||
|
def update_stock(self, item_name, new_quantity):
|
||||||
|
"""更新商品的库存量"""
|
||||||
|
if item_name not in self.inventory:
|
||||||
|
print(f"{item_name} 不在仓库中,无法更新。")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.inventory[item_name] = new_quantity
|
||||||
|
print(f"{item_name} 的库存已更新为 {new_quantity}。")
|
||||||
|
|
||||||
|
def check_stock(self, item_name=None):
|
||||||
|
"""查询单个或所有商品的库存"""
|
||||||
|
if item_name is None:
|
||||||
|
# 打印所有库存
|
||||||
|
for item, quantity in self.inventory.items():
|
||||||
|
print(f"{item}: {quantity}")
|
||||||
|
elif item_name in self.inventory:
|
||||||
|
print(f"{item_name} 的库存为: {self.inventory[item_name]}")
|
||||||
|
else:
|
||||||
|
print(f"{item_name} 不在仓库中。")
|
||||||
|
|
||||||
|
|
||||||
|
# 示例使用
|
||||||
|
if __name__ == "__main__":
|
||||||
|
warehouse = Warehouse()
|
||||||
|
warehouse.add_item("苹果", 100)
|
||||||
|
warehouse.add_item("香蕉", 150)
|
||||||
|
warehouse.check_stock() # 查看所有库存
|
||||||
|
warehouse.remove_item("苹果", 30)
|
||||||
|
warehouse.update_stock("香蕉", 200)
|
||||||
|
warehouse.check_stock("苹果") # 查询特定商品库存t("Invalid choice. Please try again.")
|
||||||
|
import tkinter as tk
|
||||||
|
from tkinter import ttk
|
||||||
|
from tkinter import messagebox
|
||||||
|
|
||||||
|
class WarehouseGUI:
|
||||||
|
def __init__(self, master):
|
||||||
|
self.master = master
|
||||||
|
master.title("仓库管理系统")
|
||||||
|
|
||||||
|
# 初始化仓库类
|
||||||
|
self.warehouse = Warehouse()
|
||||||
|
|
||||||
|
# 商品名称输入框
|
||||||
|
self.item_label = tk.Label(master, text="商品名称:")
|
||||||
|
self.item_label.pack()
|
||||||
|
self.item_entry = tk.Entry(master)
|
||||||
|
self.item_entry.pack()
|
||||||
|
|
||||||
|
# 库存数量输入框
|
||||||
|
self.quantity_label = tk.Label(master, text="库存数量:")
|
||||||
|
self.quantity_label.pack()
|
||||||
|
self.quantity_entry = tk.Entry(master)
|
||||||
|
self.quantity_entry.pack()
|
||||||
|
|
||||||
|
# 操作选择下拉菜单
|
||||||
|
self.operation_var = tk.StringVar(value="添加商品")
|
||||||
|
self.operation_menu = tk.OptionMenu(master, self.operation_var, "添加商品", "减少库存", "更新库存", "查看库存")
|
||||||
|
self.operation_menu.pack()
|
||||||
|
|
||||||
|
# 执行按钮
|
||||||
|
self.execute_button = tk.Button(master, text="执行", command=self.execute_operation)
|
||||||
|
self.execute_button.pack()
|
||||||
|
self.tree = ttk.Treeview(master, columns=("Item", "Quantity"), show="headings")
|
||||||
|
self.tree.column("Item", width=100)
|
||||||
|
self.tree.column("Quantity", width=50)
|
||||||
|
self.tree.heading("Item", text="商品名称")
|
||||||
|
self.tree.heading("Quantity", text="库存数量")
|
||||||
|
self.tree.pack()
|
||||||
|
|
||||||
|
# 初始化时显示所有库存
|
||||||
|
self.update_treeview()
|
||||||
|
|
||||||
|
# 执行按钮的command修改为更新Treeview后再执行原操作
|
||||||
|
self.execute_button.config(command=lambda: (self.update_treeview(), self.execute_operation()))
|
||||||
|
|
||||||
|
def update_treeview(self):
|
||||||
|
"""更新Treeview显示最新的库存信息"""
|
||||||
|
self.tree.delete(*self.tree.get_children()) # 清空现有内容
|
||||||
|
for item, quantity in self.warehouse.inventory.items():
|
||||||
|
self.tree.insert("", "end", values=(item, quantity))
|
||||||
|
def execute_operation(self):
|
||||||
|
operation = self.operation_var.get()
|
||||||
|
item_name = self.item_entry.get()
|
||||||
|
try:
|
||||||
|
quantity = int(self.quantity_entry.get())
|
||||||
|
except ValueError:
|
||||||
|
messagebox.showerror("错误", "库存数量必须是整数!")
|
||||||
|
return
|
||||||
|
|
||||||
|
if operation == "添加商品":
|
||||||
|
self.warehouse.add_item(item_name, quantity)
|
||||||
|
elif operation == "减少库存":
|
||||||
|
self.warehouse.remove_item(item_name, quantity)
|
||||||
|
elif operation == "更新库存":
|
||||||
|
self.warehouse.update_stock(item_name, quantity)
|
||||||
|
elif operation == "查看库存":
|
||||||
|
if item_name:
|
||||||
|
self.show_stock(item_name)
|
||||||
|
else:
|
||||||
|
self.show_all_stock()
|
||||||
|
|
||||||
|
def show_stock(self, item_name):
|
||||||
|
stock = self.warehouse.check_stock(item_name)
|
||||||
|
messagebox.showinfo(f"{item_name} 的库存", f"{stock}")
|
||||||
|
|
||||||
|
def show_all_stock(self):
|
||||||
|
stock_info = "\n".join([f"{item}: {quantity}" for item, quantity in self.warehouse.inventory.items()])
|
||||||
|
messagebox.showinfo("所有库存", stock_info)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
root = tk.Tk()
|
||||||
|
app = WarehouseGUI(root)
|
||||||
|
root.mainloop()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in new issue