1 """作业:
2 写一个商品管理的程序,数据在product.json 里面
3 1、4个功能,增、删、改、查
4 1、新增
5 输入 商品名称、价格、数量
6 商品不存在才可以添加
7 价格必须是大于0的整数/小数
8 数量必须是大于0的整数
9 2、查询
10 如果输入的是all,那么print 所有商品的信息
11 如果输入的是商品名称,打印某个商品的信息
12 如果输入为的商品名称不存在,那么提示
13 3、删除,输入商品名称,存在就删除
14 4、修改
15 输入 商品名称、价格、数量
16 商品存在才可以修改
17 价格必须是大于0的整数/小数
18 数量必须是大于0的整数
19 choice = input("请输入你的选择:1、 查询商品 2新增产品 3修改商品新 4、删除商品 5、退出")"""
20
21 import json
22 FILENAME = "product.json"
23
24 def op_file(file_name=FILENAME,content=None):
25 with open(file_name,'a+',encoding="utf-8") as f:
26 f.seek(0)
27 if content:
28 f.truncate()
29 json.dump(content,f,ensure_ascii=False)
30 # f.write(json.dumps(content,ensure_ascii=False,indent=4))
31 else:
32 content = f.read()
33 if content: #这一步是为了判断空文件的
34 return json.loads(content)
35 return {}
36
37 def is_count(s):
38 s = str(s)
39 if s.isdigit() and int(s)>0:
40 return True
41
42 def is_price(s):
43 s = str(s)
44 if s.count('.') == 1:
45 left,right = s.split('.') #[1,1]#-s.2
46 if left.isdigit() and right.isdigit():#0.0
47 return float(s) > 0
48 return is_count(s)
49
50
51
52
53
54 def add():
55 for i in range(3):
56 name = input("name:").strip()
57 price = input("price:").strip()
58 count = input("count:").strip()
59 if not name or not price or not count:
60 print("name/price/count不能为空")
61 elif not is_price(price):
62 print("价格不合法,必须是大于0的整数/小数")
63 elif not is_count(count):
64 print("数量不合法,必须是大于0的整数")
65 else:
66 products = op_file()
67 if name in products:
68 print("商品已经存在")
69 else:
70 products[name] = {"price":float(price),"count":int(count)}
71 op_file(content=products)
72 print("添加成功")
73 break
74
75 def update():
76 for i in range(3):
77 name = input("name:").strip()
78 price = input("price:").strip()
79 count = input("count:").strip()
80 if not name or not price or not count:
81 print("name/price/count不能为空")
82 elif not is_price(price):
83 print("价格不合法,必须是大于0的整数/小数")
84 elif not is_count(count):
85 print("数量不合法,必须是大于0的整数")
86 else:
87 products = op_file()
88 if name in products:
89 products[name] = {"price": float(price), "count": int(count)}
90 op_file(content=products)
91 print("修改成功")
92 break
93 else:
94 print("商品不存在,无法修改")
95
96
97
98 def show():
99 for i in range(3):
100 name = input("name:").strip()
101 if not name:
102 print("name/price/count不能为空")
103 else:
104 products = op_file()
105 if name in products:
106 print(products[name])
107 break
108 elif name == "all":
109 print(products)
110 break
111 else:
112 print("商品不存在")
113
114
115 def delete():
116 for i in range(3):
117 name = input("name:").strip()
118 if not name:
119 print("name不能为空")
120 else:
121 products = op_file()
122 if name in products:
123 products.pop(name)
124 op_file(content=products)
125 print("删除成功!")
126 break
127 else:
128 print("商品不存在")
129
130
131 def start():
132 while True:
133 choice = input("请输入你的选择:1、查询商品 2新增产品 3修改商品 4、删除商品 5、退出:").strip()
134 if choice == "1":
135 show()
136 elif choice == "2":
137 add()
138 elif choice == "3":
139 update()
140 elif choice == "4":
141 delete()
142 elif choice == "5":
143 break
144 else:
145 print("输入错误!")
146
147 def start_new():
148 func_map = {"1":show,"2":add,"3":update,"4":delete,"5":quit}
149 while True:
150 choice = input("请输入你的选择:1、查询商品 2新增产品 3修改商品 4、删除商品 5、退出:").strip()
151 if choice in func_map:
152 func_map[choice]()
153 else:
154 print("输入选项不合法")
155
156
157
158 start_new()