diff --git a/qwe.py b/qwe.py new file mode 100644 index 0000000..105ae04 --- /dev/null +++ b/qwe.py @@ -0,0 +1,167 @@ +import tkinter as tk +from tkinter import messagebox +import csv +import os + +class Goods: + def __init__(self, name, num, primecost, sellingprice): + self.name = name + self.num = num + self.primecost = primecost + self.sellingprice = sellingprice + + def __str__(self): + state = "已售罄" if self.num == 0 else "" + return '名称:%s,数量:%d %s,进货价格:%.2f,售出价格:%.2f' % ( + self.name, self.num, state, self.primecost, self.sellingprice) + +class SoldGoods: + def __init__(self, name, num, primecost, sellingprice): + self.name = name + self.num = num + self.primecost = primecost + self.sellingprice = sellingprice + + def __str__(self): + return '名称:%s,卖出数量:%d,进货价格:%.2f,售出价格:%.2f' % ( + self.name, self.num, self.primecost, self.sellingprice) + +class GoodsManager: + def __init__(self): + self.goods_list = [] + self.sold_list = [] + self.load_goods_from_file() + + def init(self): + self.goods_list.append(Goods("牛奶", 5, 40.00, 60.00)) + self.goods_list.append(Goods("盒饭", 5, 10.00, 60.00)) + self.goods_list.append(Goods("樱桃", 5, 20.00, 80.00)) + + def add_goods(self, name, num, primecost, sellingprice): + goods = Goods(name, num, primecost, sellingprice) + self.goods_list.append(goods) + self.save_goods_to_file() + messagebox.showinfo("提示", "添加商品成功") + + def show_goods(self): + return [str(goods) for goods in self.goods_list] + + def modify_goods(self, name, num, primecost, sellingprice): + for goods in self.goods_list: + if goods.name == name: + goods.num = num + goods.primecost = primecost + goods.sellingprice = sellingprice + self.save_goods_to_file() + messagebox.showinfo("提示", "修改商品成功") + return + messagebox.showerror("错误", "没有找到该商品") + + def delete_goods(self, name): + for goods in self.goods_list: + if goods.name == name: + self.goods_list.remove(goods) + self.save_goods_to_file() + messagebox.showinfo("提示", "删除商品成功") + return + messagebox.showerror("错误", "没有找到该商品") + + def sell_goods(self, name, num): + for goods in self.goods_list: + if goods.name == name: + if goods.num >= num: + goods.num -= num + self.sold_list.append(SoldGoods(name, num, goods.primecost * num, goods.sellingprice * num)) + self.save_goods_to_file() + messagebox.showinfo("提示", "卖出商品成功") + return + else: + messagebox.showerror("错误", "库存不足") + return + messagebox.showerror("错误", "没有找到该商品") + + def summary(self): + total_primecost = sum(goods.primecost for goods in self.sold_list) + total_sellingprice = sum(goods.sellingprice for goods in self.sold_list) + profit = total_sellingprice - total_primecost + return total_primecost, total_sellingprice, profit + + def save_goods_to_file(self, filename='goods.csv'): + with open(filename, 'w', newline='', encoding='utf-8') as file: + writer = csv.writer(file) + writer.writerow(['name', 'num', 'primecost', 'sellingprice']) + for goods in self.goods_list: + writer.writerow([goods.name, goods.num, goods.primecost, goods.sellingprice]) + + def load_goods_from_file(self, filename='goods.csv'): + if not os.path.exists(filename): + return + with open(filename, 'r', encoding='utf-8') as file: + reader = csv.reader(file) + next(reader) # 跳过标题行 + self.goods_list.clear() # 清空现有商品列表 + for row in reader: + name, num, primecost, sellingprice = row + goods = Goods(name, int(num), float(primecost), float(sellingprice)) + self.goods_list.append(goods) + +class MainFrame(tk.Tk): + def __init__(self): + super().__init__() + self.title("超市管理系统") + self.manager = GoodsManager() + self.manager.init() + self.create_widgets() + self.geometry('600x400') # 设置窗口尺寸 + self.update() # 更新窗口 + self.geometry(self.geometry()) # 获取并重新设置窗口尺寸,实现居中 + + def create_widgets(self): + tk.Label(self, text="商品名称").pack() + self.name_var = tk.StringVar() + tk.Entry(self, textvariable=self.name_var).pack() + + tk.Label(self, text="数量").pack() + self.num_var = tk.IntVar() + tk.Entry(self, textvariable=self.num_var).pack() + + tk.Label(self, text="进货价格").pack() + self.primecost_var = tk.DoubleVar() + tk.Entry(self, textvariable=self.primecost_var).pack() + + tk.Label(self, text="销售价格").pack() + self.sellingprice_var = tk.DoubleVar() + tk.Entry(self, textvariable=self.sellingprice_var).pack() + + tk.Button(self, text="添加商品", command=self.add_goods).pack() + tk.Button(self, text="显示商品", command=self.show_goods).pack() + tk.Button(self, text="修改商品", command=self.modify_goods).pack() + tk.Button(self, text="删除商品", command=self.delete_goods).pack() + tk.Button(self, text="卖出商品", command=self.sell_goods).pack() + tk.Button(self, text="统计信息", command=self.summary).pack() + + def add_goods(self): + self.manager.add_goods(self.name_var.get(), self.num_var.get(), self.primecost_var.get(), + self.sellingprice_var.get()) + + def show_goods(self): + goods_list = self.manager.show_goods() + messagebox.showinfo("商品列表", "\n".join(goods_list)) + + def modify_goods(self): + self.manager.modify_goods(self.name_var.get(), self.num_var.get(), self.primecost_var.get(), + self.sellingprice_var.get()) + + def delete_goods(self): + self.manager.delete_goods(self.name_var.get()) + + def sell_goods(self): + self.manager.sell_goods(self.name_var.get(), self.num_var.get()) + + def summary(self): + total_primecost, total_sellingprice, profit = self.manager.summary() + messagebox.showinfo("统计信息", f"进货总价:{total_primecost}\n销售总价:{total_sellingprice}\n利润:{profit}") + +if __name__ == '__main__': + app = MainFrame() + app.mainloop() \ No newline at end of file