实现功能:
1用户登陆:用户名或密码错误判断
2商品清单展示
3查看购物车
4商品加入购物车并更新商品清单
5删除购物车中商品并更新商品清单
6查看已购买记录
7购物车商品支付
8支付时余额不足判断
9支付成功删除购物车中对应商品并更新已购买记录
10用户账户余额查看
11充值功能
1用户登陆:用户名或密码错误判断
2商品清单展示
3查看购物车
4商品加入购物车并更新商品清单
5删除购物车中商品并更新商品清单
6查看已购买记录
7购物车商品支付
8支付时余额不足判断
9支付成功删除购物车中对应商品并更新已购买记录
10用户账户余额查看
11充值功能
涉及到4个文件作为库:
goods_file #商品清单
1:小米5:1999:9
2:三星S7:4999:15
3:华为Mate8:3999:12
4:魅族pro6:2499:21
5:苹果6plus:5999:11
gouwuche #购物车清单
zhy:魅族pro6:1:2499
alex:魅族pro6:1:2499
user_fiel #用户密码清单
zhy:zhyzhy:2501
alex:123456:5001
yigoushangpin #已经购买的商品记录
zhy:小米5:3:5997
alex:苹果6plus:2:11998
zhy:苹果6plus:1:5999
zhy:魅族pro6:1:2499
注:每个文件中的每行结尾必须要有换行符'\n'
1:小米5:1999:9
2:三星S7:4999:15
3:华为Mate8:3999:12
4:魅族pro6:2499:21
5:苹果6plus:5999:11
gouwuche #购物车清单
zhy:魅族pro6:1:2499
alex:魅族pro6:1:2499
user_fiel #用户密码清单
zhy:zhyzhy:2501
alex:123456:5001
yigoushangpin #已经购买的商品记录
zhy:小米5:3:5997
alex:苹果6plus:2:11998
zhy:苹果6plus:1:5999
zhy:魅族pro6:1:2499
注:每个文件中的每行结尾必须要有换行符'\n'
流程图
python代码
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 import codecs
4
5 # 登陆接口,用户名密码都正确登录成功,否则失败
6 def login(your_name,your_password):
7 with open('user_file','r') as l:
8 for line in l:
9 user_name, user_password, user_balance = line.strip().split(':')
10 if your_name == user_name and your_password == user_password:
11 flag = 1
12 break
13 else:
14 print('用户名或密码错误')
15 flag = 0
16
17 return flag
18
19 # 展示商品列表
20 def show_goods():
21 print(10*'*' + '商品列表' + 10*'*')
22 print(30 * '-')
23 print('| 编号 | 名称 | 价格 | 数量 | ')
24 with codecs.open('goods_file','r','utf-8') as f1:
25 for line in f1:
26 goods_id, goods_name, goods_rate, goods_num = line.strip().split(':')
27 print(30 * '-')
28 print('| %s | %s | %s | %s |' %(goods_id, goods_name, goods_rate, goods_num))
29 print(30*'-')
30 # print('购买商品请输入产品编号/返回请输入b/退出请输入q')
31 # the_num = input('请选择:')
32
33 # 展示购物车
34 def show_gouwuche(your_name):
35 with codecs.open('gouwuche', 'r', 'utf-8') as f2:
36 n = 0
37 print(30 * '-')
38 print('| 编号 | 名称 | 数量 | 总价 | ')
39 for line in f2:
40 user_name, goods_name, goods_num, goods_rate = line.strip().split(':')
41 if user_name == your_name:
42 n +=1
43 print(30*'-')
44 print('| %s | %s | %s | %s |' % (n, goods_name, goods_num, goods_rate))
45 print(30 * '-')
46
47 # 查看当前用户已购产品
48 def show_yigou(your_name):
49 with codecs.open('yigoushangpin', 'r', 'utf-8') as f3:
50 n = 0
51 print(30 * '-')
52 print('| 编号 | 名称 | 数量 | 总价 | ')
53 for line in f3:
54 user_name, goods_name, goods_num, goods_rate = line.strip().split(':')
55 if user_name == your_name:
56 n += 1
57 print(30 * '-')
58 print('| %s | %s | %s | %s |' % (n, goods_name, goods_num, goods_rate))
59 print(30 * '-')
60
61 # 账户余额查看
62 def show_money(your_name):
63 with open('user_file', 'r') as l:
64 for line in l:
65 user_name, user_password, user_balance = line.strip().split(':')
66 if your_name == user_name:
67 print('您当前账户余额为:%s' % user_balance)
68 break
69
70 # 加入购物车,如果购物车中已经存在该商品则将商品数量相加,商品价格相加;如果购物车中不存在该商品,则添加到购物车中。并将可购买商品的库存量
71 def add_gouwuche(n,your_name):
72 with codecs.open('goods_file', 'r+', 'utf-8') as f1:
73 for line1 in f1:
74 goods_id, goods_name, goods_rate, goods_num = line1.strip().split(':')
75 if n == goods_id :
76 goods_quantity = input('请输入数量:')
77 #如果输入的非数字则不合法,输入的数量大于库存,均需要重新输入
78 while True:
79 if goods_quantity.isdigit():
80 if int(goods_num) < int(goods_quantity):
81 goods_quantity =input("您输入的数量大于当前库存数量,请重新输入数量:")
82 else:
83 break
84 else:
85 goods_quantity = input("您的输入不合法,请重新输入数量:")
86 with codecs.open('gouwuche', 'r+', 'utf-8') as f2:
87 for line2 in f2:
88 che_user_name, che_goods_name, che_goods_num, che_goods_rate = line2.strip().split(':')
89 # 判断当前选择的商品是否在购物车存在,如果存在标志位che_flag=1
90 if your_name == che_user_name and goods_name == che_goods_name:
91 che_flag =1
92 che_goods_num = str(int(che_goods_num) + int(goods_quantity))
93 che_goods_rate = str(int(che_goods_rate)+int(goods_rate)*int(goods_quantity))
94 old_line = line2
95 new_line = che_user_name + ':'+ che_goods_name + ':' + che_goods_num + ':' + che_goods_rate+'\n'
96 break
97 # 当前选择的商品购物车中不存在,标志位che_flag=0
98 else:
99 che_flag = 0
100 che_goods_rate = str(int(goods_rate) * int(goods_quantity))
101 che_goods_num = goods_quantity
102 che_goods_name = goods_name
103 che_user_name = your_name
104 new_line = che_user_name + ':' + che_goods_name + ':' + che_goods_num + ':' + che_goods_rate + '\n'
105 # 购物车中存在,则将购物车中的此商品数量更新,总价格更新
106 if che_flag == 1:
107 f2.seek(0)
108 old_gouwuche = f2.read()
109 new_gouwuche = old_gouwuche.replace(old_line,new_line)
110 f2.seek(0)
111 f2.truncate()
112 f2.write(new_gouwuche)
113 # 购物车中不存在,则在购物车添加
114 elif che_flag == 0:
115 f2.seek(0)
116 old_gouwuche = f2.read()
117 new_gouwuche = old_gouwuche+new_line
118 f2.seek(0)
119 f2.truncate()
120 f2.write(new_gouwuche)
121 #加入购物车后,将商品列表中的数量更新
122 new_goods_num = str(int(goods_num) - int(goods_quantity))
123 old_goods_line = line1
124 new_goods_line = goods_id + ':' + goods_name + ':'+ goods_rate + ':' + new_goods_num + '\n'
125 f1.seek(0)
126 old_goods_file=f1.read()
127 new_goods_file=old_goods_file.replace(old_goods_line,new_goods_line)
128 f1.seek(0)
129 f1.truncate()
130 f1.write(new_goods_file)
131 goods_flag = True
132 break
133 else:
134 goods_flag = False
135 print('抱歉您选择的商品不存在,请重新选择!')
136 return goods_flag
137
138 # 删除购物车的商品
139 def del_gouwuche(del_gowuche_num,your_name):
140 with codecs.open('gouwuche','r+','utf-8') as f1:
141 while True:
142 n=0
143 for line1 in f1:
144 user_name, goods_name, goods_num, goods_rate = line1.strip().split(':')
145 # 判断删除的商品时否存在,若存在删除标志位del_flag = 1
146 if user_name == your_name:
147 n += 1
148 if int(del_gowuche_num) == n:
149 del_goods_name = goods_name
150 del_goods_num = goods_num
151 del_goods_line = line1
152 del_flag = 1
153 break
154
155 # 商品存在,进行删除
156 if del_flag == 1:
157 f1.seek(0)
158 gouwuche_file = f1.readlines()
159 gouwuche_file.remove(del_goods_line)
160 f1.seek(0)
161 f1.truncate()
162 for line in gouwuche_file:
163 f1.write(line)
164 # 更新商品清单,将从购物车删除的加到商品清单中
165 with codecs.open("goods_file",'r+','utf-8') as f2:
166 for line2 in f2:
167 old_goods_line = list(line2.strip().split(':'))
168 old_line=line2.strip()
169 if old_goods_line[1] == del_goods_name :
170 old_goods_line[3] = str(int(old_goods_line[3]) + int(del_goods_num))
171 new_goods_line = old_goods_line
172 del_flag =1
173 break
174 if del_flag == 1:
175 f2.seek(0)
176 new_line= ':'.join(new_goods_line)
177 old_goods_file = f2.read()
178 new_goods_file = old_goods_file.replace(old_line,new_line)
179 f2.seek(0)
180 f2.truncate()
181 f2.write(new_goods_file)
182
183 break
184 # 要删除的商品不存在,标志位del_flag = 0
185 else:
186 del_flag = 0
187 print("您要删除的商品不存在,请重新输入编号!")
188 break
189 return del_flag
190
191 # 支付购物车商品
192 def pay_gouwuche(gowuche_num,your_name):
193 with codecs.open('gouwuche','r+','utf-8') as f1:
194 while True:
195 n=0
196 for line1 in f1:
197 user_name, goods_name, goods_num, goods_rate = line1.strip().split(':')
198 # 判断删除的商品时否存在,若存在删除标志位del_flag = 1
199 if user_name == your_name:
200 n += 1
201 if int(gowuche_num) == n:
202 pay_goods_name = goods_name
203 pay_goods_num = goods_num
204 pay_goods_rate = goods_rate
205 pay_goods_line = line1
206 # 查看当前用户余额是否能够支付商品
207 with open('user_file', 'r+') as user_file:
208 for line in user_file:
209 user_name, user_password, user_balance = line.strip().split(':')
210 if your_name == user_name:
211 old_balance_line = line
212 new_balance = int(user_balance) - int(goods_rate)
213 break
214 # 当最终余额小于0时,则表示余额不足
215 if new_balance < 0:
216 print('您的当前余额不足,请充值后再支付!')
217 print('\n')
218 pay_flag = False
219 break
220 #大于等于0时,则表示可以支付,并更新账户余额
221 else:
222 pay_flag = True
223 new_balance_line=user_name + ':' +user_password + ':' + str(new_balance) + '\n'
224 user_file.seek(0)
225 old_user_file = user_file.read()
226 new_user_file = old_user_file.replace(old_balance_line, new_balance_line)
227 user_file.seek(0)
228 user_file.truncate()
229 user_file.write(new_user_file)
230 break
231 else:
232 pay_flag= ''
233 # 商品存在,支付成功后进行删除
234 if pay_flag == True:
235 f1.seek(0)
236 gouwuche_file = f1.readlines()
237 gouwuche_file.remove(pay_goods_line)
238 f1.seek(0)
239 f1.truncate()
240 for line in gouwuche_file:
241 f1.write(line)
242 # 更新已购买商品清单,将已支付的商品加入到已购买的清单中
243 with codecs.open("yigoushangpin",'r+','utf-8') as f2:
244 for line2 in f2:
245 old_yigou_line = list(line2.strip().split(':'))
246 old_line=line2.strip()
247 # 当已购商品清单存在该商品购买记录则将数量相加,总价格相加
248 if old_yigou_line[1] == pay_goods_name and old_yigou_line[0] == your_name:
249 old_yigou_line[2] = str(int(old_yigou_line[2]) + int(pay_goods_num))
250 old_yigou_line[3] = str(int(old_yigou_line[3]) + int(pay_goods_rate))
251 new_yigou_line = old_yigou_line
252 f2.seek(0)
253 new_line = ':'.join(new_yigou_line)
254 old_yigou_file = f2.read()
255 new_yigou_file = old_yigou_file.replace(old_line, new_line)
256 f2.seek(0)
257 f2.truncate()
258 f2.write(new_yigou_file)
259 break
260 # 当已购商品清单没有购买记录,则新加购买记录
261 else:
262 f2.seek(0)
263 old_yigou_file = f2.read()
264 new_yigou_file = old_yigou_file + pay_goods_line
265 f2.seek(0)
266 f2.truncate()
267 f2.write(new_yigou_file)
268 print('\n')
269 print("恭喜您,支付成功!")
270 break
271 elif pay_flag == False:
272 flag = True
273 break
274 # 要删除的商品不存在,标志位del_flag = 0
275 else:
276 pay_flag = False
277 print("您要支付的商品不存在,请重新输入编号!")
278 break
279 return pay_flag
280
281 # 账户充值
282 def add_money(balance_num,your_name):
283 with open('user_file','r+') as f:
284 for line in f:
285 user_name,user_passwd,user_balance=line.strip().split(':')
286 if user_name == your_name:
287 old_user_line = line
288 new_user_balance = str(int(user_balance) + int(balance_num))
289 new_user_line = user_name + ':' +user_passwd + ':' + new_user_balance + '\n'
290 break
291 f.seek(0)
292 old_user_file =f.read()
293 new_user_file = old_user_file.replace(old_user_line,new_user_line)
294 f.seek(0)
295 f.truncate()
296 f.write(new_user_file)
297
298
299 def main():
300 your_name = input("请输入用户名:")
301 your_password = input("请输入密码:")
302 menu_num = ''
303 # 登陆,用户名或密码错误重新输入
304 while True:
305 if login(your_name,your_password):
306 print('欢迎%s登录购物商城' % your_name)
307 print(30 * '*')
308 break
309 else:
310 your_name = input("请重新输入用户名:")
311 your_password = input("请重新输入密码:")
312 while True:
313 print(30 * '*')
314 print('欢迎来到主菜单,您可进行如下操作!')
315 print(30 * '*')
316 print('| 1 商品列表 | 2 查看购物车 | 3 查看已购商品 | 4 账户余额 | q 退出商城 |')
317 if menu_num == '':
318 menu_num = input('请输入:')
319
320 # 进入商品列表
321 elif menu_num == "1":
322 while True:
323 show_goods()
324 print('| 输入商品编号加入购物车| 输入b返回主菜单| 输入q退出商城 |')
325 your_goods_num = input('请选择:')
326 print('\n')
327 # 判断输入的是否为纯数字,如果是
328 if your_goods_num.isdigit():
329 while add_gouwuche(your_goods_num,your_name):
330 # 当商品加入购物车后直接返回给用户购物车
331 menu_num = '2'
332 break
333 break
334 elif your_goods_num == "b":
335 menu_num = ''
336 break
337 elif your_goods_num == "q":
338 print("您已退出商城!")
339 exit()
340 else:
341 print("您的输入不合法,请重新输入!")
342 print('\n')
343 # 查看购物车
344 elif menu_num == '2':
345 while True:
346 print(30 * '-')
347 print("您当前购物车商品如下所示:")
348 show_gouwuche(your_name)
349 print("| 1 返回主菜单 | 2 删除商品 | 3 支付商品 | q 退出商城 |")
350 gouwuche_menu_num = input("请输入:")
351 print('\n')
352 # 返回主菜单
353 if gouwuche_menu_num == '1':
354 menu_num = ''
355 break
356 # 删除商品
357 elif gouwuche_menu_num == '2':
358 while True:
359 del_gouwuche_num = input('请输入要删除的商品编号,或者输入b返回:')
360 if del_gouwuche_num == 'b':
361 break
362 elif not del_gouwuche_num.isdigit():
363 print("您的输入不合法,请重新输入!")
364 print('\n')
365 continue
366 elif del_gouwuche(del_gouwuche_num,your_name):
367 print("删除成功!")
368 print('\n')
369 break
370 # 支付商品
371 elif gouwuche_menu_num == '3':
372 while True:
373 pay_gouwuche_num = input('请输入要支付的商品编号,返回请输入b:')
374 if pay_gouwuche_num.isdigit():
375 if pay_gouwuche(pay_gouwuche_num,your_name):
376 break
377 elif pay_gouwuche_num == 'b':
378 break
379 else:
380 print("您的输入不合法,请重新输入!")
381
382 elif gouwuche_menu_num == 'q':
383 print("您已退出商城!")
384 exit()
385 else:
386 print("您的输入不合法,请重新输入!")
387 print('\n')
388
389 #查看已购商品记录
390 elif menu_num == '3':
391 print(30 * '-')
392 print("您已够买商品如下所示:")
393 show_yigou(your_name)
394 print('| 输入b返回主菜单| 输入q退出程序 |')
395 while True:
396 yigou_input=input("请输入:")
397 if yigou_input == 'b':
398 menu_num =''
399 print('\n')
400 break
401 elif yigou_input == 'q':
402 print("您已退出商城!")
403 exit()
404 else:
405 print("您的输入不合法,请重新输入!")
406 print('\n')
407
408 # 查看账户信息
409 elif menu_num == '4':
410 while True:
411 print(30 * '-')
412 print("您的账户信息如下所示:")
413 show_money(your_name)
414 print('| 1 充值账户 | b 返回主菜单 | q 退出商城 | ')
415 money_num =input('请输入:')
416 if money_num == '1':
417 print(30*'*')
418 add_money_num = input('请输入充值金额,输入b返回:')
419 if add_money_num.isdigit():
420 add_money(add_money_num,your_name)
421 print('恭喜您,充值成功!')
422 print('\n')
423 continue
424 elif add_money_num == 'b':
425 continue
426 else:
427 print("您的输入不合法,请重新输入!")
428 print('\n')
429 continue
430 elif money_num == 'b':
431 menu_num = ''
432 break
433 elif money_num == 'q':
434 print("您已退出商城!")
435 exit()
436 else:
437 print("您的输入不合法,请重新输入!")
438 print('\n')
439
440 elif menu_num == 'q':
441 print("您已退出商城!")
442 exit()
443 else:
444 print("您的输入不合法,请重新输入!c")
445 print('\n')
446
447 if __name__ == '__main__':
448 main()
2 # -*- coding: utf-8 -*-
3 import codecs
4
5 # 登陆接口,用户名密码都正确登录成功,否则失败
6 def login(your_name,your_password):
7 with open('user_file','r') as l:
8 for line in l:
9 user_name, user_password, user_balance = line.strip().split(':')
10 if your_name == user_name and your_password == user_password:
11 flag = 1
12 break
13 else:
14 print('用户名或密码错误')
15 flag = 0
16
17 return flag
18
19 # 展示商品列表
20 def show_goods():
21 print(10*'*' + '商品列表' + 10*'*')
22 print(30 * '-')
23 print('| 编号 | 名称 | 价格 | 数量 | ')
24 with codecs.open('goods_file','r','utf-8') as f1:
25 for line in f1:
26 goods_id, goods_name, goods_rate, goods_num = line.strip().split(':')
27 print(30 * '-')
28 print('| %s | %s | %s | %s |' %(goods_id, goods_name, goods_rate, goods_num))
29 print(30*'-')
30 # print('购买商品请输入产品编号/返回请输入b/退出请输入q')
31 # the_num = input('请选择:')
32
33 # 展示购物车
34 def show_gouwuche(your_name):
35 with codecs.open('gouwuche', 'r', 'utf-8') as f2:
36 n = 0
37 print(30 * '-')
38 print('| 编号 | 名称 | 数量 | 总价 | ')
39 for line in f2:
40 user_name, goods_name, goods_num, goods_rate = line.strip().split(':')
41 if user_name == your_name:
42 n +=1
43 print(30*'-')
44 print('| %s | %s | %s | %s |' % (n, goods_name, goods_num, goods_rate))
45 print(30 * '-')
46
47 # 查看当前用户已购产品
48 def show_yigou(your_name):
49 with codecs.open('yigoushangpin', 'r', 'utf-8') as f3:
50 n = 0
51 print(30 * '-')
52 print('| 编号 | 名称 | 数量 | 总价 | ')
53 for line in f3:
54 user_name, goods_name, goods_num, goods_rate = line.strip().split(':')
55 if user_name == your_name:
56 n += 1
57 print(30 * '-')
58 print('| %s | %s | %s | %s |' % (n, goods_name, goods_num, goods_rate))
59 print(30 * '-')
60
61 # 账户余额查看
62 def show_money(your_name):
63 with open('user_file', 'r') as l:
64 for line in l:
65 user_name, user_password, user_balance = line.strip().split(':')
66 if your_name == user_name:
67 print('您当前账户余额为:%s' % user_balance)
68 break
69
70 # 加入购物车,如果购物车中已经存在该商品则将商品数量相加,商品价格相加;如果购物车中不存在该商品,则添加到购物车中。并将可购买商品的库存量
71 def add_gouwuche(n,your_name):
72 with codecs.open('goods_file', 'r+', 'utf-8') as f1:
73 for line1 in f1:
74 goods_id, goods_name, goods_rate, goods_num = line1.strip().split(':')
75 if n == goods_id :
76 goods_quantity = input('请输入数量:')
77 #如果输入的非数字则不合法,输入的数量大于库存,均需要重新输入
78 while True:
79 if goods_quantity.isdigit():
80 if int(goods_num) < int(goods_quantity):
81 goods_quantity =input("您输入的数量大于当前库存数量,请重新输入数量:")
82 else:
83 break
84 else:
85 goods_quantity = input("您的输入不合法,请重新输入数量:")
86 with codecs.open('gouwuche', 'r+', 'utf-8') as f2:
87 for line2 in f2:
88 che_user_name, che_goods_name, che_goods_num, che_goods_rate = line2.strip().split(':')
89 # 判断当前选择的商品是否在购物车存在,如果存在标志位che_flag=1
90 if your_name == che_user_name and goods_name == che_goods_name:
91 che_flag =1
92 che_goods_num = str(int(che_goods_num) + int(goods_quantity))
93 che_goods_rate = str(int(che_goods_rate)+int(goods_rate)*int(goods_quantity))
94 old_line = line2
95 new_line = che_user_name + ':'+ che_goods_name + ':' + che_goods_num + ':' + che_goods_rate+'\n'
96 break
97 # 当前选择的商品购物车中不存在,标志位che_flag=0
98 else:
99 che_flag = 0
100 che_goods_rate = str(int(goods_rate) * int(goods_quantity))
101 che_goods_num = goods_quantity
102 che_goods_name = goods_name
103 che_user_name = your_name
104 new_line = che_user_name + ':' + che_goods_name + ':' + che_goods_num + ':' + che_goods_rate + '\n'
105 # 购物车中存在,则将购物车中的此商品数量更新,总价格更新
106 if che_flag == 1:
107 f2.seek(0)
108 old_gouwuche = f2.read()
109 new_gouwuche = old_gouwuche.replace(old_line,new_line)
110 f2.seek(0)
111 f2.truncate()
112 f2.write(new_gouwuche)
113 # 购物车中不存在,则在购物车添加
114 elif che_flag == 0:
115 f2.seek(0)
116 old_gouwuche = f2.read()
117 new_gouwuche = old_gouwuche+new_line
118 f2.seek(0)
119 f2.truncate()
120 f2.write(new_gouwuche)
121 #加入购物车后,将商品列表中的数量更新
122 new_goods_num = str(int(goods_num) - int(goods_quantity))
123 old_goods_line = line1
124 new_goods_line = goods_id + ':' + goods_name + ':'+ goods_rate + ':' + new_goods_num + '\n'
125 f1.seek(0)
126 old_goods_file=f1.read()
127 new_goods_file=old_goods_file.replace(old_goods_line,new_goods_line)
128 f1.seek(0)
129 f1.truncate()
130 f1.write(new_goods_file)
131 goods_flag = True
132 break
133 else:
134 goods_flag = False
135 print('抱歉您选择的商品不存在,请重新选择!')
136 return goods_flag
137
138 # 删除购物车的商品
139 def del_gouwuche(del_gowuche_num,your_name):
140 with codecs.open('gouwuche','r+','utf-8') as f1:
141 while True:
142 n=0
143 for line1 in f1:
144 user_name, goods_name, goods_num, goods_rate = line1.strip().split(':')
145 # 判断删除的商品时否存在,若存在删除标志位del_flag = 1
146 if user_name == your_name:
147 n += 1
148 if int(del_gowuche_num) == n:
149 del_goods_name = goods_name
150 del_goods_num = goods_num
151 del_goods_line = line1
152 del_flag = 1
153 break
154
155 # 商品存在,进行删除
156 if del_flag == 1:
157 f1.seek(0)
158 gouwuche_file = f1.readlines()
159 gouwuche_file.remove(del_goods_line)
160 f1.seek(0)
161 f1.truncate()
162 for line in gouwuche_file:
163 f1.write(line)
164 # 更新商品清单,将从购物车删除的加到商品清单中
165 with codecs.open("goods_file",'r+','utf-8') as f2:
166 for line2 in f2:
167 old_goods_line = list(line2.strip().split(':'))
168 old_line=line2.strip()
169 if old_goods_line[1] == del_goods_name :
170 old_goods_line[3] = str(int(old_goods_line[3]) + int(del_goods_num))
171 new_goods_line = old_goods_line
172 del_flag =1
173 break
174 if del_flag == 1:
175 f2.seek(0)
176 new_line= ':'.join(new_goods_line)
177 old_goods_file = f2.read()
178 new_goods_file = old_goods_file.replace(old_line,new_line)
179 f2.seek(0)
180 f2.truncate()
181 f2.write(new_goods_file)
182
183 break
184 # 要删除的商品不存在,标志位del_flag = 0
185 else:
186 del_flag = 0
187 print("您要删除的商品不存在,请重新输入编号!")
188 break
189 return del_flag
190
191 # 支付购物车商品
192 def pay_gouwuche(gowuche_num,your_name):
193 with codecs.open('gouwuche','r+','utf-8') as f1:
194 while True:
195 n=0
196 for line1 in f1:
197 user_name, goods_name, goods_num, goods_rate = line1.strip().split(':')
198 # 判断删除的商品时否存在,若存在删除标志位del_flag = 1
199 if user_name == your_name:
200 n += 1
201 if int(gowuche_num) == n:
202 pay_goods_name = goods_name
203 pay_goods_num = goods_num
204 pay_goods_rate = goods_rate
205 pay_goods_line = line1
206 # 查看当前用户余额是否能够支付商品
207 with open('user_file', 'r+') as user_file:
208 for line in user_file:
209 user_name, user_password, user_balance = line.strip().split(':')
210 if your_name == user_name:
211 old_balance_line = line
212 new_balance = int(user_balance) - int(goods_rate)
213 break
214 # 当最终余额小于0时,则表示余额不足
215 if new_balance < 0:
216 print('您的当前余额不足,请充值后再支付!')
217 print('\n')
218 pay_flag = False
219 break
220 #大于等于0时,则表示可以支付,并更新账户余额
221 else:
222 pay_flag = True
223 new_balance_line=user_name + ':' +user_password + ':' + str(new_balance) + '\n'
224 user_file.seek(0)
225 old_user_file = user_file.read()
226 new_user_file = old_user_file.replace(old_balance_line, new_balance_line)
227 user_file.seek(0)
228 user_file.truncate()
229 user_file.write(new_user_file)
230 break
231 else:
232 pay_flag= ''
233 # 商品存在,支付成功后进行删除
234 if pay_flag == True:
235 f1.seek(0)
236 gouwuche_file = f1.readlines()
237 gouwuche_file.remove(pay_goods_line)
238 f1.seek(0)
239 f1.truncate()
240 for line in gouwuche_file:
241 f1.write(line)
242 # 更新已购买商品清单,将已支付的商品加入到已购买的清单中
243 with codecs.open("yigoushangpin",'r+','utf-8') as f2:
244 for line2 in f2:
245 old_yigou_line = list(line2.strip().split(':'))
246 old_line=line2.strip()
247 # 当已购商品清单存在该商品购买记录则将数量相加,总价格相加
248 if old_yigou_line[1] == pay_goods_name and old_yigou_line[0] == your_name:
249 old_yigou_line[2] = str(int(old_yigou_line[2]) + int(pay_goods_num))
250 old_yigou_line[3] = str(int(old_yigou_line[3]) + int(pay_goods_rate))
251 new_yigou_line = old_yigou_line
252 f2.seek(0)
253 new_line = ':'.join(new_yigou_line)
254 old_yigou_file = f2.read()
255 new_yigou_file = old_yigou_file.replace(old_line, new_line)
256 f2.seek(0)
257 f2.truncate()
258 f2.write(new_yigou_file)
259 break
260 # 当已购商品清单没有购买记录,则新加购买记录
261 else:
262 f2.seek(0)
263 old_yigou_file = f2.read()
264 new_yigou_file = old_yigou_file + pay_goods_line
265 f2.seek(0)
266 f2.truncate()
267 f2.write(new_yigou_file)
268 print('\n')
269 print("恭喜您,支付成功!")
270 break
271 elif pay_flag == False:
272 flag = True
273 break
274 # 要删除的商品不存在,标志位del_flag = 0
275 else:
276 pay_flag = False
277 print("您要支付的商品不存在,请重新输入编号!")
278 break
279 return pay_flag
280
281 # 账户充值
282 def add_money(balance_num,your_name):
283 with open('user_file','r+') as f:
284 for line in f:
285 user_name,user_passwd,user_balance=line.strip().split(':')
286 if user_name == your_name:
287 old_user_line = line
288 new_user_balance = str(int(user_balance) + int(balance_num))
289 new_user_line = user_name + ':' +user_passwd + ':' + new_user_balance + '\n'
290 break
291 f.seek(0)
292 old_user_file =f.read()
293 new_user_file = old_user_file.replace(old_user_line,new_user_line)
294 f.seek(0)
295 f.truncate()
296 f.write(new_user_file)
297
298
299 def main():
300 your_name = input("请输入用户名:")
301 your_password = input("请输入密码:")
302 menu_num = ''
303 # 登陆,用户名或密码错误重新输入
304 while True:
305 if login(your_name,your_password):
306 print('欢迎%s登录购物商城' % your_name)
307 print(30 * '*')
308 break
309 else:
310 your_name = input("请重新输入用户名:")
311 your_password = input("请重新输入密码:")
312 while True:
313 print(30 * '*')
314 print('欢迎来到主菜单,您可进行如下操作!')
315 print(30 * '*')
316 print('| 1 商品列表 | 2 查看购物车 | 3 查看已购商品 | 4 账户余额 | q 退出商城 |')
317 if menu_num == '':
318 menu_num = input('请输入:')
319
320 # 进入商品列表
321 elif menu_num == "1":
322 while True:
323 show_goods()
324 print('| 输入商品编号加入购物车| 输入b返回主菜单| 输入q退出商城 |')
325 your_goods_num = input('请选择:')
326 print('\n')
327 # 判断输入的是否为纯数字,如果是
328 if your_goods_num.isdigit():
329 while add_gouwuche(your_goods_num,your_name):
330 # 当商品加入购物车后直接返回给用户购物车
331 menu_num = '2'
332 break
333 break
334 elif your_goods_num == "b":
335 menu_num = ''
336 break
337 elif your_goods_num == "q":
338 print("您已退出商城!")
339 exit()
340 else:
341 print("您的输入不合法,请重新输入!")
342 print('\n')
343 # 查看购物车
344 elif menu_num == '2':
345 while True:
346 print(30 * '-')
347 print("您当前购物车商品如下所示:")
348 show_gouwuche(your_name)
349 print("| 1 返回主菜单 | 2 删除商品 | 3 支付商品 | q 退出商城 |")
350 gouwuche_menu_num = input("请输入:")
351 print('\n')
352 # 返回主菜单
353 if gouwuche_menu_num == '1':
354 menu_num = ''
355 break
356 # 删除商品
357 elif gouwuche_menu_num == '2':
358 while True:
359 del_gouwuche_num = input('请输入要删除的商品编号,或者输入b返回:')
360 if del_gouwuche_num == 'b':
361 break
362 elif not del_gouwuche_num.isdigit():
363 print("您的输入不合法,请重新输入!")
364 print('\n')
365 continue
366 elif del_gouwuche(del_gouwuche_num,your_name):
367 print("删除成功!")
368 print('\n')
369 break
370 # 支付商品
371 elif gouwuche_menu_num == '3':
372 while True:
373 pay_gouwuche_num = input('请输入要支付的商品编号,返回请输入b:')
374 if pay_gouwuche_num.isdigit():
375 if pay_gouwuche(pay_gouwuche_num,your_name):
376 break
377 elif pay_gouwuche_num == 'b':
378 break
379 else:
380 print("您的输入不合法,请重新输入!")
381
382 elif gouwuche_menu_num == 'q':
383 print("您已退出商城!")
384 exit()
385 else:
386 print("您的输入不合法,请重新输入!")
387 print('\n')
388
389 #查看已购商品记录
390 elif menu_num == '3':
391 print(30 * '-')
392 print("您已够买商品如下所示:")
393 show_yigou(your_name)
394 print('| 输入b返回主菜单| 输入q退出程序 |')
395 while True:
396 yigou_input=input("请输入:")
397 if yigou_input == 'b':
398 menu_num =''
399 print('\n')
400 break
401 elif yigou_input == 'q':
402 print("您已退出商城!")
403 exit()
404 else:
405 print("您的输入不合法,请重新输入!")
406 print('\n')
407
408 # 查看账户信息
409 elif menu_num == '4':
410 while True:
411 print(30 * '-')
412 print("您的账户信息如下所示:")
413 show_money(your_name)
414 print('| 1 充值账户 | b 返回主菜单 | q 退出商城 | ')
415 money_num =input('请输入:')
416 if money_num == '1':
417 print(30*'*')
418 add_money_num = input('请输入充值金额,输入b返回:')
419 if add_money_num.isdigit():
420 add_money(add_money_num,your_name)
421 print('恭喜您,充值成功!')
422 print('\n')
423 continue
424 elif add_money_num == 'b':
425 continue
426 else:
427 print("您的输入不合法,请重新输入!")
428 print('\n')
429 continue
430 elif money_num == 'b':
431 menu_num = ''
432 break
433 elif money_num == 'q':
434 print("您已退出商城!")
435 exit()
436 else:
437 print("您的输入不合法,请重新输入!")
438 print('\n')
439
440 elif menu_num == 'q':
441 print("您已退出商城!")
442 exit()
443 else:
444 print("您的输入不合法,请重新输入!c")
445 print('\n')
446
447 if __name__ == '__main__':
448 main()
来源:https://www.cnblogs.com/zhyErick/p/5419708.html