1 #Joe Young
2
3 importgetpass4 importos5
6 #调用os模块的system方法传入'cls'参数,清屏
7 os.system('cls')8
9 whileTrue:10 entrance = input('请选择:\n\t1. 用户登陆\n\t2. 商家登陆\n>>>')11 if entrance != '1' and entrance != '2':12 print('\n输入有误,请重试...\n')13 else:14 break
15
16 #打印商品列表
17 defprint_product_list():18 index = 1
19 with open('product_list.txt', 'r') as product_file:20 for product_line inproduct_file:21 L = [commodity, price, stock] = product_line.strip('\n').split(',')22 commodity_list.append(L)23 print((str(index) + '.' + commodity).ljust(20) + ('单价:' + price + '元').ljust(15) + '库存:' +stock)24 index += 1
25 return
26
27 #用户入口
28 if entrance == '1':29
30 info = [] #存放用户的信息,初始为空
31 if_payed = True #if_payed 表示订单是否已支付
32 username = ''
33
34 #登录接口
35 count =036 while count < 3:37 username = input('\n用户名:')38
39 #打开锁定列表文件
40 with open('lock_list.txt', 'r+') as lock_file:41 for lock_line inlock_file:42 #用户名在锁定名单里面,则退出程序
43 if username == lock_line.strip('\n'):44 exit('\n用户名 %s 已被锁定,请联系管理员...' %username)45
46 login = False #登录标志,初始为False
47
48 #打开用户名列表文件,读权限
49 user_file = open('user_list.txt', 'r')50
51 for user_line inuser_file:52 #获取每行的用户信息,用户名、密码、余额、支付密码,存入info列表
53 info = [user, passwd, balance, pay_passwd] = user_line.strip('\n').split(',')54 #用户名匹配,则进入密码输入环节
55 if user ==username:56 n =057 #3次输入机会
58 while n < 3:59 password = getpass.getpass('密码:')60 #密码匹配,显示登录成功
61 if passwd ==password:62 print('\n欢迎 %s 登录商城,祝您购物愉快!\n' %username)63 login = True #登录标志赋值为True
64 break
65 #密码不匹配
66 else:67 #n = 2 时是最后一次机会,不必提示还剩下0次机会
68 if n != 2:69 print('\n密码错误,请重新输入,您还有 %d 次机会\n' % (2-n))70 n += 1
71 #密码错误次数达到3次,锁定用户名,退出程序
72 else:73 open('lock_list.txt', 'w').write(username + '\n')74 exit('\n错误次数过多,账户已被锁定...')75
76 #登录成功,跳出for循环
77 iflogin:78 break
79 else:80 if count != 2:81 print('\n用户名不存在,请重试,您还有 %d 次机会' % (2-count))82
83 user_file.close()84
85 count += 1
86
87 #登录成功,跳出while循环
88 iflogin:89 break
90
91 else:92 exit('\n错误次数过多,程序已退出...')93
94 #购买程序
95 shopping_cart = [] #购物车初始为空
96 commodity_list =[]97
98 print_product_list()99
100 whileTrue:101 i = input('\n请选择商品(输入序号),或输入 c 取消购买:')102
103 if i == 'c':104 whileTrue:105 a = input('\n是否继续购买?(Y/N):')106 if a == 'n' or a == 'N':107 exit('\n交易结束...')108 elif a == 'y' or a == 'Y':109 break
110 else:111 print('\n输入格式有误,请重试...')112 continue
113
114 if noti.isdigit():115 print('\n输入格式有误,请重试...')116 continue
117
118 i =int(i)119
120 if i <= 0 or i >len(commodity_list):121 print('\n此商品不存在,请重试...')122 continue
123
124 item_name = commodity_list[i-1][0] #商品名称
125 item_price = commodity_list[i-1][1] #商品价格
126 item_stock = commodity_list[i-1][2] #商品库存
127
128 print('\n您已选择了 %s ,请输入购买的数量,或输入 b 重新选择:' %item_name)129
130 back =False131
132 whileTrue:133 num = input('>>>')134 if num == 'b':135 back =True136 break
137 if notnum.isdigit():138 print('输入格式有误,请重试...')139 continue
140 if int(num) >int(item_stock):141 print('数量大于库存,请重试...')142 continue
143 if int(num) ==0:144 print('数量应大于0,请重试...')145 break
146 ifback:147 continue
148
149 item =[item_name, item_price, num]150
151 print('\n您已选择了 %s,单价:%s 元,数量:%s,您想立即购买还是加入购物车?\n' %(item_name, item_price, num))152 print('\t1. 立即购买\n\t2. 加入购物车\n')153
154 whileTrue:155 choice = input('>>>')156 if not (choice == '1' or choice == '2'):157 print('输入有误,请重试...')158 continue
159 break
160
161 user_balance = int(info[2])162
163 #立即购买
164 if choice == '1':165 amount = int(item_price) *int(num)166 count =0167 cancel =False168
169 while count < 3:170 user_pay_passwd = getpass.getpass('\n请输入支付密码,或输入 c 放弃支付:')171 if user_pay_passwd == 'c':172 print('\n取消支付成功...')173 cancel =True174 break
175 elif user_pay_passwd != info[3]:176 if count != 2:177 print('\n密码错误,请重试,您还有 %d 次机会...' % (2-count))178 count += 1
179 else:180 break
181
182 if count == 3:183 with open('lock_list.txt', 'w') as lock_file:184 lock_file.write(username + '\n')185 exit('密码错误,账户已被锁定...')186
187 ifcancel:188 whileTrue:189 choice = input('\n是否继续购买?(Y/N):')190 if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):191 print('\n输入格式有误,请重试...')192 continue
193 break
194 if choice == 'Y' or choice == 'y':195 continue
196 else:197 break
198
199 #如果用户的账户余额大于总金额
200 if user_balance >=amount:201 user_balance -=amount202 print('\n支付成功!您已成功购买 %s ,单价:%s 元,数量:%s,总金额:%s 元,账户余额:%s 元'
203 %(item_name, item_price, num, amount, user_balance))204 lines = open('product_list.txt', 'r').readlines()205 #定位到用户所购买的商品所在行,分割成列表赋值给select
206 select = lines[i-1].strip('\n').split(',')207 #修改商品的库存
208 select[-1] = (str(int(select[-1]) - int(num)) + '\n')209 #拼接成字符串
210 lines[i-1] = ','.join(select)211 #将修改写回文件
212 open('product_list.txt', 'w').writelines(lines)213
214 lines = open('user_list.txt', 'r').readlines()215 #修改用户余额
216 for line inlines:217 if username in line.split(','): #定位到用户名所在行
218 j = lines.index(line) #获取用户名所在行的行号索引
219 select = line.split(',') #分割用户名所在行赋值给列表select
220 select[-2] = str(user_balance) #修改用户余额
221 lines[j] = ','.join(select) #修改后的列表拼接成字符串,覆盖用户名所在行
222 open('user_list.txt', 'w').writelines(lines) #将修改写回文件
223 else:224 print('\n对不起,您的余额不足...')225
226 else: #加入购物车
227 j =0228 for j inrange(len(shopping_cart)):229 #如果商品在购物车里面,更新商品数量
230 if item_name inshopping_cart[j]:231 shopping_cart[j][2] = str(int(shopping_cart[j][2]) +int(num))232 break
233 #商品若不在购物车,则添加到购物车
234 else:235 shopping_cart.append(item)236 print('\n成功加入购物车!')237
238 whileTrue:239 choice = input('\n是否继续购买?(Y/N):')240 if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):241 print('\n输入格式有误,请重试...')242 continue
243 break
244
245 if choice == 'Y' or choice == 'y':246 continue
247 else:248 break
249
250 #如果购物车不为空
251 ifshopping_cart:252 print('\n您的购物车里有以下宝贝:\n')253 i = 1
254 total_sum =0255 for item inshopping_cart:256 (commodity, price, number) = (item[0], item[1], item[2])257 print((str(i) + '.' + commodity).ljust(20) + ('单价:' + price + '元').ljust(15) + '数量:' +number)258 total_sum += int(price) *int(number)259 i += 1
260 print('\n合计:%d 元' %total_sum)261
262 whileTrue:263 if_buy = input('\n是否结算?(Y/N):')264 if not (if_buy == 'Y' or if_buy == 'y' or if_buy == 'N' or if_buy == 'n'):265 print('\n输入有误,请重试...')266 continue
267 break
268
269 whileTrue:270 #结算
271 if if_buy == 'Y' or if_buy == 'y':272 count =0273 cancel =False274
275 while count < 3:276 user_pay_passwd = getpass.getpass('\n请输入支付密码,或输入 c 放弃支付:')277 if user_pay_passwd == 'c':278 print('\n取消支付成功...')279 cancel =True280 break
281 elif user_pay_passwd != info[3]:282 if count != 2:283 print('\n密码错误,请重试,您还有 %d 次机会...' % (2-count))284 count += 1
285 else:286 break
287
288 ifcancel:289 if_payed =False290
291 elif count == 3:292 with open('lock_list.txt', 'w') as lock_file:293 lock_file.write(username + '\n')294 exit('\n密码错误,账户已被锁定...')295
296 else:297 if total_sum <=user_balance:298 user_balance -=total_sum299 print('\n支付成功!您已成功购买以下商品:\n')300 i = 1
301 for item inshopping_cart:302 (commodity, price, number) = (item[0], item[1], item[2])303 print((str(i) + '.' + commodity).ljust(20) +
304 ('单价:' + price + '元').ljust(15) + '数量:' +number)305 lines = open('product_list.txt', 'r').readlines()306 for line in lines: #修改商品库存
307 if commodity in line.split(','): #定位到商品所在行
308 j = lines.index(line) #获取商品所在行的行号索引
309 select = line.split(',') #商品所在行分割为字符串列表
310 select[-1] = (str(int(select[-1]) - int(number)) + '\n') #修改商品库存
311 lines[j] = ','.join(select) #将修改后的字符串列表组成字符串
312 open('product_list.txt', 'w').writelines(lines) #把修改写回文件
313 i += 1
314
315 lines = open('user_list.txt', 'r').readlines()316
317 for line in lines: #用户余额写入文件
318 if username in line.split(','):319 j =lines.index(line)320 select = line.split(',')321 select[-2] =str(user_balance)322 lines[j] = ','.join(select)323 open('user_list.txt', 'w').writelines(lines)324
325 exit('\n合计:%d 元, 账户余额:%d 元' %(total_sum, user_balance))326
327 #不结算
328 else:329 print('\n您有一笔未支付订单...')330 whileTrue:331 choice = input('\n是否进行支付?(Y/N):')332 if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):333 print('\n输入有误,请重试...')334 continue
335 break
336 if choice == 'n' or choice == 'N':337 exit('\n订单已取消,感谢光临购物商城,再见...')338 else:339 if_buy = 'Y'
340 continue
341
342 if notif_payed:343 print('\n您有一笔未支付订单...')344 whileTrue:345 choice = input('\n是否进行支付?(Y/N):')346 if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):347 print('\n输入有误,请重试...')348 continue
349 break
350
351 if choice == 'n' or choice == 'N':352 exit('\n订单已取消,感谢光临购物商城,再见...')353 else:354 if_buy = 'Y'
355 continue
356
357 #商家入口
358 if entrance == '2':359
360 seller_name = ''
361
362 #登录接口
363 count =0364 while count < 3:365 seller_name = input('\n用户名:')366
367 with open('lock_list.txt', 'r') as lock_file:368 for lock_line inlock_file:369 if seller_name == lock_line.strip('\n'):370 exit('\n用户名 %s 已被锁定,请联系管理员...' %seller_name)371
372 seller_file = open('seller_list.txt', 'r')373 login =False374
375 for seller_line inseller_file:376 (seller, passwd) = seller_line.strip('\n').split(',')377 if seller_name ==seller:378 n =0379 while n < 3:380 password = getpass.getpass('密码:')381 #登录成功,跳出while循环
382 if password ==passwd:383 print('\n欢迎 %s 登录商城' %seller_name)384 login =True385 break
386 else:387 if n != 2:388 print('\n密码错误,请重试,您还有 %d 次机会' % (2-n))389 n += 1
390 #n = 3,锁定用户名
391 else:392 open('lock_list.txt', 'w').write(seller_name + '\n')393 exit('\n错误次数过多,账户已被锁定...')394 #登录成功,跳出for循环
395 iflogin:396 break
397
398 #用户名不存在
399 else:400 if count != 2:401 print('\n用户名不存在,请重试,您还有 %d 次机会' % (2-count))402
403 #登录成功,跳出while循环
404 iflogin:405 break
406
407 count += 1
408
409 else:410 exit('\n错误次数过多,程序已退出...')411
412
413 #商品列表编辑程序
414
415 L =[]416 #存放商品列表,初始为空
417 commodity_list =[]418 index = 1
419
420 print('\n您的货架上有以下商品:\n')421
422 print_product_list()423
424 whileTrue:425 choice = input('\n是否编辑您的商品列表?(Y/N):')426 if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'):427 print('\n输入有误,请重试...')428 continue
429 break
430
431 if choice == 'Y' or choice == 'y':432 whileTrue:433 print('\n请选择(输入 q 退出):\n')434 print('1. 上架新品\n\n2. 下架商品\n\n3. 修改商品信息')435 choice = input('\n>>>')436 if not (choice == '1' or choice == '2' or choice == '3' or choice == 'q'):437 print('输入有误,请重试...')438 continue
439
440 #上架新品
441 if choice == '1':442 whileTrue:443 if_add = False #是否添加商品的标志,初始为False
444 new_commodity = input('\n输入商品名:')445
446 product_file = open('product_list.txt', 'r')447
448 for product_line inproduct_file:449 commodity = product_line.strip('\n').split(',')[0] #获取商品列表中的商品名
450 if new_commodity ==commodity:451 print('\n此商品已在货架上...')452 continue
453 else:454 whileTrue:455 if_sure = input('\n确定上架新品 %s 吗?(Y/N):' %new_commodity)456 if not (if_sure == 'Y' or if_sure == 'y' or if_sure == 'N' or if_sure == 'n'):457 print('\n输入有误,请重试...')458 continue
459 break
460 #确定上架新品
461 if if_sure == 'Y' or if_sure == 'y':462 while True: #输入单价
463 price = input('\n请输入单价:')464 if notprice.isdigit():465 print('\n输入有误,请重试...')466 continue
467 break
468 while True: #输入库存
469 stock = input('\n请输入库存:')470 if notstock.isdigit():471 print('\n输入有误,请重试...')472 continue
473 break
474 new_line = '\n' + new_commodity + ',' + price + ',' +stock475 open('product_list.txt', 'a').writelines(new_line)476 print('\n成功上架新品 %s ,单价 %s 元,库存 %s 件' %(new_commodity, price, stock))477
478 whileTrue:479 option = input('\n是否继续添加?(Y/N):')480 if not (option == 'Y' or option or option == 'N' or option == 'n'):481 print('\n输入有误,请重试...')482 continue
483 break
484 if option == 'Y' or option == 'y':485 if_add =True486 break #跳出for循环
487 else:488 break
489 #取消上架新品
490 else:491 if_add =False492 break #跳出for循环
493 product_file.close()494
495 if if_add isTrue:496 continue
497 else:498 break #跳出while循环
499
500 #下架商品
501 elif choice == '2':502 whileTrue:503 del_num = input('\n请输入您想下架商品的序号:')504 if not (del_num.isdigit() or int(del_num) > 0 and int(del_num) <=len(commodity_list)):505 print('\n输入有误,请重试...')506 continue
507 break
508
509 del_num =int(del_num)510 del_commodity = commodity_list[del_num - 1][0]511
512 with open('product_list.txt', 'r') as old_file:513 with open('product_list.txt', 'r+') as new_file:514
515 current_line =0516
517 #定位到需要删除的行
518 while current_line < (del_num - 1):519 old_file.readline()520 current_line += 1
521
522 #当前光标在被删除行的行首,记录该位置
523 seek_point =old_file.tell()524
525 #设置光标位置
526 new_file.seek(seek_point, 0)527
528 #读需要删除的行,光标移到下一行行首
529 old_file.readline()530
531 #被删除行的下一行读给 next_line
532 next_line =old_file.readline()533
534 #连续覆盖剩余行,后面所有行上移一行
535 whilenext_line:536 new_file.write(next_line)537 next_line =old_file.readline()538
539 #写完最后一行后截断文件,因为删除操作,文件整体少了一行,原文件最后一行需要去掉
540 new_file.truncate()541
542 print('\n您已成功下架 %s !' %del_commodity)543
544 #修改商品信息
545 elif choice == '3':546
547 #修改商品信息
548 defmod_commodity_info(i, j):549 i =int(i)550 j =int(j)551 with open('product_list.txt', 'r+') as f:552 current_line =0553 while current_line < i - 1:554 f.readline()555 current_line += 1
556 seek_point =f.tell()557 f.seek(seek_point, 0)558
559 #修改商品名
560 if j == 1:561 update_line = mod_name() + ',' + commodity_list[i-1][1] + ',' + commodity_list[i-1][2] + '\n'
562 #修改商品价格
563 elif j == 2:564 update_line = commodity_list[i-1][0] + ',' + mod_price() + ',' + commodity_list[i-1][2] + '\n'
565 #修改商品库存
566 else:567 update_line = commodity_list[i-1][0] + ',' + commodity_list[i-1][1] + ',' + mod_stock() + '\n'
568
569 f.write(update_line)570 return
571
572 defmod_name():573 new_name = input("\n请输入新的商品名:")574 returnnew_name575
576 defmod_price():577 new_price = input("\n请输入新的商品单价:")578 returnnew_price579
580 defmod_stock():581 new_stock = input("\n请输入新的商品库存:")582 returnnew_stock583
584 #修改商品单价
585 defmod_commodity_price(i):586 i =int(i)587 with open('product_list.txt', 'r+') as f:588 current_line =0589 while current_line < i -1:590 f.readline()591 current_line += 1
592 seek_point =f.tell()593 f.seek(seek_point, 0)594 new_price =input()595
596
597 whileTrue:598 i = input("\n请输入需要编辑的商品序号(输入 c 取消):")599 if not (i.isdigit or i == 'c' or int(i) > 0 and int(i) <=len(commodity_list)):600 print("\n输入有误,请重试...")601 continue
602 elif i == 'c':603 break
604 else:605 whileTrue:606 j = input("\n请选择需要编辑的选项(输入 c 取消):\n\n1. 商品名\n\n2. 单价\n\n3. 库存\n\n>>>")607 if not (j == 'c' or j == '1' or j == '2' or j == '3'):608 print("\n输入有误,请重试...")609 continue
610 break
611 if j == 'c':612 break
613 else:614 mod_commodity_info(i, j)615
616 else:617 exit('\n您已退出商城...')618 else:619 exit('\n您已退出商城...')