购物车+ATM终极黄金版

  1 def waicen():
  2     '''商城主页'''
  3     print('商城主页面'.center(50,'*'))
  4     dic123 = {'1':register,'2':login,'3':ATM}
  5     print('1.注册\n2.登录(已登录无需再登)\n3.ATM\n4.退出')
  6     user_choose = input('请输入操作的编号>>:').strip()
  7     if user_choose == '4':
  8         exit()
  9     try:
 10         dic123[user_choose]()
 11     except:
 12         KeyError
 13         print('请输入正确的编号')
 14 
 15 
 16 def neicen():
 17     '''商城主页内菜单'''
 18     dic123 = {'1':shopping,'2':recharge}
 19     print('1.购物\n2.充值\n3.退出账号')
 20     user_choose3 = input('【请选择操作编号:').strip()
 21     if dic123[user_choose3] == '3':
 22         dic00['scuser'] = 0
 23         waicen()
 24     try:
 25         dic123[user_choose3]()
 26     except:
 27         KeyError
 28         print('请输入正确的编号')
 29         neicen()
 30 
 31 
 32 # def open_file():
 33 #     global yedic
 34 #     with open('ye.txt') as ye_f:
 35 #         for data1 in ye_f:
 36 #             yedic = {}
 37 #             user_ye = data1.split(',')
 38 #             yedic[user_ye[0]] = int(user_ye[1].strip('\n'))
 39 #     return yedic
 40 
 41 
 42 def register():
 43     '''商城注册'''
 44     l1 = []
 45     name1 = input('【请输入要注册的用户名:').strip()
 46     with open('db.txt') as f:
 47         for i in f:
 48             l1.append(i.split(','))
 49     if name1 in l1:
 50         print('输入的用户名已被注册!')
 51     else:
 52         password1 = input('【请输入密码:')
 53         if ' ' in list(password1):
 54             print('密码中不能有空格')
 55         else:
 56             with open('db.txt', 'a') as f:
 57                 f.write('%s,%s\n' % (name1, password1))
 58 
 59             with open('ye.txt', 'a') as zj_f:
 60                 zj_f.write('%s,0\n' % name1)
 61             print('注册成功')
 62             waicen()
 63 
 64 
 65 def login():
 66     '''商城登录'''
 67     global count
 68     count = 0
 69     if dic00['scuser'] == 1:
 70         neicen()
 71     if dic00['scuser'] == 0:
 72         while True:
 73             l2 = []
 74             global name2
 75             name2 = input('【请输入用户名:').strip()
 76             password2 = input('【请输入密码:')
 77             with open('db.txt', 'r') as f:
 78                 data = f.read().split('\n')
 79                 for i in data:
 80                     l2.append(i.split(','))
 81             if [name2, password2] not in l2:
 82                 print('用户名或密码错误')
 83                 count += 1
 84             if count == 3:
 85                 print('错误三次程序退出')
 86                 exit()
 87             if [name2, password2] in l2:
 88                 global order ,order_number,shopping_cart
 89                 shopping_cart = []
 90                 order = []
 91                 order_number = 0
 92                 dic00['scuser'] = 1
 93                 print('登录成功,尽情购物吧!')
 94                 neicen()
 95 
 96 
 97 def shopping():
 98     '''商城购物'''
 99     global zj2, sy,balance, msg_dic
100     zj2 = 0
101     for k, v in msg_dic.items():
102         print('商品名:%s   单价%s' % (k, v))
103     op_file('ye.txt')
104     op2_file()
105     print(yedic)
106     balance = yedic[name2]
107     print('余额:%s'.center(20, '*') %balance)
108     while True:
109         sy = int(balance) - zj2
110         goods = input('【请输入要购买的商品名或输入 1 返回主菜单:').strip()
111         if goods == '1':
112             neicen()
113         elif goods not in msg_dic:
114             print('商品不存在')
115             continue
116         if goods in msg_dic and goods not in order:
117             while True:
118                 num = input('【请输入要购买的数量:').strip()
119                 if num.isdigit() is True:
120                     shopping_cart.append([goods, msg_dic[goods], num, msg_dic[goods] * int(num)])
121                     print('购物车:')
122                     for goods_line in shopping_cart:
123                         print('商品名:{n} 单价{d} 数量{s} 总价{z}'.format(n=goods_line[0], d=goods_line[1], s=goods_line[2],
124                                                                  z=goods_line[3]))
125                     zj2 += goods_line[3]
126                     order.append(goods_line[0])
127                     order_number = order.index(goods)
128                     print('购买的所有商品总价格为¥%s' % zj2)
129                     break
130 
131                 else:
132                     print('请确认输入的是数字')
133 
134 
135         elif goods in order:
136             while True:
137                 num1 = input('1【请输入要购买的个数:').strip()
138                 order_number = order.index(goods)
139                 if num1.isdigit() is True:
140                     num2 = shopping_cart[order_number][2]
141                     total = shopping_cart[order_number][3]
142                     shopping_cart[order_number][2] = int(num2) + int(num1)
143                     shopping_cart[order_number][3] = int(total) + int(num1) * msg_dic[goods]
144                     print('购物车:')
145                     for i in shopping_cart:
146                         print('商品名:{name} 单价:{price} 数量:{number} 总价:{total}'.format(name=i[0], price=i[1], number=i[2],
147                                                                                     total=i[3]))
148                     zj2 += i[3]
149                     print('购买的所有商品总价格为¥%s' % zj2)
150                     break
151                 elif num1.isdigit() is False:
152                     print('请确认输入的是数字。')
153 
154         user_choose1 = input('【是否付款(是\否)').strip()
155         if user_choose1 == '':
156             pay()
157             if sy < 0:
158                 print('余额不足,请充值')
159                 user_choose4 = input('请选择1.充值2.退出')
160                 if user_choose4 == '1':
161                     with open('ye.txt', 'w') as rep_f:
162                         yedic[name2] = int(yedic[name2]) - sy
163                         for k, v in yedic.items():
164                             rep_f.write('%s,%s\n' % (k, v))
165                     recharge()
166                 if user_choose4 == '2':
167                     waicen()
168         else:
169             print('继续购物!')
170 
171 
172 def pay():
173     '''商城付款'''
174     op_file('ye.txt')
175     op2_file()
176     if int(yedic[name2]) < int(zj2):
177         print('余额不足,请选择操作\n1.充值\n2.ATM支付\n3.退出')
178         user_choose5 = input('>>:')
179         if user_choose5 == '1':
180             recharge()
181         elif user_choose5 == '2':
182             repayment()
183         elif user_choose5 == '3':
184             waicen()
185     elif int(yedic[name2]) > int(zj2):
186         yedic[name2] -= zj2
187         with open('ye.txt','w') as zf_f:
188             for k, v in yedic.items():
189                 zf_f.write('%s,%s\n' % (k, v))
190         print('支付成功')
191         op_file('ye.txt')
192         op2_file()
193         print('余额还剩%s'%yedic[name2])
194         neicen()
195 
196 
197 
198 
199 
200 
201 
202 
203 def recharge():
204     '''商城充值'''
205     if dic00['user'] == 1:
206         balance1 = input('*请输入要充值的金额:').strip()
207         op_file('atmye.txt')
208         op1_file()
209         op_file('ye.txt')
210         op2_file()
211         dic[name3] -= int(balance1)
212         yedic[name2] += int(balance1)
213         print(dic)
214         print(yedic)
215         with open('ye.txt','w') as w_f,open('atmye.txt','w')as wr_f:
216             for k, v in yedic.items():
217                 w_f.write('%s,%s\n' % (k, v))
218             for k, v in dic.items():
219                 wr_f.write('%s,%s\n' % (k, v))
220         print('充值成功,返回购物')
221         shopping()
222     elif dic00['user'] == 0:
223         print('未登陆ATM')
224         user()
225 
226 
227 
228 #*************************************************************************************************************************
229 
230 
231 
232 def op_file(x):
233     global l2,dic
234     with open(x,'rt',encoding='utf-8') as read_f:
235         l2 = [i.strip('\n').split(',') for i in read_f]
236     return l2
237 
238 def op1_file():
239     global dic
240     dic = {k:int(v) for k,v in l2}
241     return dic
242 def op2_file():
243     global yedic
244     yedic = {k:int(v) for k,v in l2}
245     return yedic
246 
247 
248 
249 
250 
251 def ATM():
252     '''ATM主页'''
253     print('ATM页面'.center(50,'*'))
254     dic2 = {'1':user,'2':administrators,}
255     print('ATM页面,请登录支付账户')
256     print('1.登录普通账户\n2.登录管理员账户\n3.退出')
257     user_choose1 = input('输入操作编号>>:').strip()
258     dic2[user_choose1]()
259     if user_choose1 == '3':
260         exit()
261     else:
262         print('请输入正确操作')
263 
264 
265 def login1(func1):
266     '''ATM普通账户登录'''
267     def inner1():
268         global name3
269         count = 0
270         if dic00['user'] == 1:
271             res = func1()
272             return res
273         while True:
274             name3 = input('【请输入用户名:').strip()
275             password3 = input('【请输入密码:')
276             op_file('atmuser.txt')
277             with open('frozen.txt') as r_f:
278                 data = r_f.read()
279                 l1 = data.split(',')
280             if name3 in l1:
281                 print('该用户已被冻结!!')
282                 continue
283             if [name3, password3] not in l2:
284                 print('用户名或密码错误')
285                 count += 1
286             if count == 3:
287                 print('错误三次退出到主页')
288 
289             elif [name3, password3] in l2:
290                 dic00['user'] = 1
291                 print('登录成功')
292                 res = func1()
293                 return res
294     return inner1
295 
296 
297 def glylogin(func):
298     '''ATM管理员登录'''
299     def inner():
300         count = 0
301         if dic00['admin'] == 1:
302             res = func()
303             return res
304         while True:
305             name0 = input('【请输入管理员用户名:').strip()
306             password0 = input('【请输入管理员密码:')
307             op_file('gly.txt')
308             if [name0,password0] not in l2:
309                 print('用户名或密码错误')
310                 count += 1
311             if count == 3:
312                 print('错误三次程序退出')
313                 exit()
314             elif [name0,password0] in l2:
315                 dic00['admin'] = 1
316                 print('登录成功,进入管理员操作页面')
317                 res = func()
318                 return res
319     return inner
320 
321 
322 @glylogin
323 def administrators():
324     '''ATM管理员主页'''
325     dic123 = {'1':add_administrators, '2':user_balance, '3':frozen_account,'4':ATM_recharge}
326     print('1.添加账户\n2.查询用户余额\n3.冻结用户\n4.账户余额充值')
327     user_choice = input('输入操作编号:')
328     if user_choice == '5':
329         dic00['user'] = 0
330         ATM()
331     dic123[user_choice]()
332 
333 @login1
334 def user():
335     '''ATM普通账户主页'''
336     dic123 = {'1':repayment, '2':put_forward, '3':transfer_accounts,'4':user_info}
337     print('1.还款|2.提现|3.转账\n4.查询用户信息|5.返回商城|6.退出')
338     user_choice = input('输入操作编号:')
339     if user_choice == '5':
340         waicen()
341     elif user_choice == '6':
342         dic00['admin'] = 0
343         ATM()
344     dic123[user_choice]()
345 
346 def ATM_recharge():
347     '''ATM账户充值'''
348     op_file('atmye.txt')
349     op1_file()
350     for k1,v1 in dic.items():
351         print('用户名:%s|余额:%s'%(k1,v1))
352     name = input('请选择要充值的账户:').strip()
353     money = input('请选择充值金额:').strip()
354     if name in dic.keys() and money.isdigit() is True:
355         res = dic[name]
356         dic[name] = res + int(money)
357         with open('atmye.txt','w',encoding='utf-8') as w_f:
358             for k2,v2 in dic.items():
359                 w_f.write('%s,%s\n'%(k2,v2))
360     else:
361         print('输入有误')
362         ATM_recharge()
363 
364 
365 def add_administrators():
366     '''添加用户'''
367     while True:
368         user_choice = input('1.添加普通账户、2.添加管理员>>:').strip()
369         if user_choice == '1':
370             name = input('输入要添加的用户名:')
371             with open('atmuser.txt') as r_f:
372                 l3 = [i.strip('\n').split(',')[0] for i in r_f]
373             if name in l3:
374                 print('该账户已存在')
375                 continue
376             if name not in l3:
377                 pwd = input('输入添加账户的密码:')
378                 user_choice = input(r'是否添加该账户(是\否\1.保存后退出):')
379                 if user_choice == '':
380                     with open('atmuser.txt', 'a') as w_f,open('atmye.txt','a') as wr_f:
381                         w_f.write('%s,%s\n' % (name, pwd))
382                         wr_f.write('%s,0'%name)
383                 elif user_choice == '':
384                     administrators()
385                 elif user_choice == '1':
386                     with open('atmuser.txt', 'a') as w_f,open('atmye.txt','a') as wr_f:
387                         w_f.write('%s,%s\n' % (name, pwd))
388                         wr_f.write('%s,0'%name)
389                     administrators()
390                 else:
391                     print('输入正确的选择')
392         if user_choice == '2':
393             name = input('输入要添加的管理员用户名:')
394             with open('gly.txt') as r_f:
395                 l3 = [i.strip('\n').split(',')[0] for i in r_f]
396             if name in l3:
397                 print('该管理员账户已存在')
398                 continue
399             if name not in l3:
400                 pwd = input('输入添加的管理员密码:')
401                 user_choice = input(r'是否添加该账户(是\否\1.保存后退出):')
402                 if user_choice == '':
403                     with open('gly.txt','a') as w_f:
404                         w_f.write('%s,%s\n'%(name,pwd))
405                 elif user_choice == '':
406                     administrators()
407                 elif user_choice == '1':
408                     with open('gly.txt','a') as w_f:
409                         w_f.write('%s,%s\n'%(name,pwd))
410                     administrators()
411                 else:
412                     print('输入正确的选择')
413         else:
414             print('请输入正确的选择')
415 
416 
417 def user_balance():
418     '''查询用户余额'''
419     while True:
420         dic123 = {}
421         name = input('输入要查询的账户用户名或输‘1’退出:').strip()
422         if name == '1':
423             administrators()
424         with open('atmye.txt') as r_f:
425             for i in r_f:
426                 rs = i.strip('\n').split(',')
427                 dic123[rs[0]] = rs[1]
428         if name in dic123.keys():
429             print('%s的余额为:%s'%(name,dic123[name]))
430         else:
431             print('该用户名不存在')
432 
433 def frozen_account():
434     '''冻结账户'''
435     with open('frozen.txt') as r_f:
436         data = r_f.read().split(',')
437     op_file('atmye.txt')
438     op1_file()
439     for k,v in dic.items():
440         print('用户:%s,余额:%s'%(k,v))
441     print('冻结账户名单:%s'%data)
442     while True:
443         name = input('输入要冻结的用户名或‘1’退出:').strip()
444         op_file('db.txt')
445         if name == '1':
446             administrators()
447         if name not in l2:
448             print('该用户名不存在')
449             continue
450         if name in l2:
451             if name in data:
452                 print('该账户之前已被冻结')
453             elif name not in data:
454                 with open('frozen','a') as w_f:
455                     w_f.write('%s,'%name)
456 
457 def repayment():
458     '''ATM支付'''
459     if dic00['user'] == 0:
460         print('未登陆ATM,进入登陆界面')
461         user()
462     elif zj2 == 0 :
463         print('没有待还款的账单')
464         user()
465     elif dic00['user'] == 1:
466         op_file('atmye.txt')
467         op1_file()
468         print('要支付的商品总价:%s\n账户余额:%s'%(zj2,dic[name3]))
469         user_choice = input('确认支付(是\否):')
470         if user_choice == '':
471             if zj2 > dic[name3]:
472                 print('账户余额不足返回商城')
473                 neicen()
474             else:
475                 res = dic[name3]
476                 dic[name3] = res - zj2
477                 with open('atmye.txt','w') as w_f,open('%sls'%name3,'a') as wr_f:
478                     for k,v in dic.items():
479                         w_f.write('%s,%s\n'%(k,v))
480                     res1 = time.strftime('%Y-%m-%d %X')
481                     res2 = '购买商品支出:¥%s\n' % zj2
482                     wr_f.write('%s---%s' % (res1, res2))
483                 print('支付成功')
484                 user()
485         if user_choice == '':
486             user()
487 def put_forward():
488     '''ATM提现'''
489     op_file('atmye.txt')
490     op1_file()
491     money = input('请输入提取的金额(收取5%手续费):')
492     if int(money)>dic[name3]:
493         print('账户余额不足')
494         user()
495     else:
496         res = dic[name3]
497         dic[name3] = res - int(money) - int(money)*0.05
498         with open('atmye.txt','w') as w_f,open('%sls'%name3,'a') as wr_f:
499             for k1,v1 in dic.items():
500                 w_f.write('%s,%s\n' %(k1,v1))
501             res1 = time.strftime('%Y-%m-%d %X')
502             res2 = '提现:¥%s|收取手续费:¥%s\n'%(money,int(money)*0.05)
503             wr_f.write('%s---%s' %(res1,res2))
504         print('%s---%s' %(res1,res2))
505         user()
506 def transfer_accounts():
507     '''ATM转账'''
508     username = input('请输入要转账对象的用户名:')
509     op_file('atmye.txt')
510     op1_file()
511     while True:
512         if username not in dic.keys():
513             print('该用户名不存在')
514             transfer_accounts()
515         if username in dic.keys():
516             inpumoney = input('请输入转账金额:').strip()
517             if int(inpumoney)>dic[name3]:
518                 print('你的余额不足!')
519                 user()
520             elif inpumoney.isdigit() is True:
521                 res = dic[username]
522                 res1 = dic[name3]
523                 dic[username] = res + int(inpumoney)
524                 dic[name3] = res1 - int(inpumoney)
525                 with open('atmye.txt','w') as w_f,open('%sls'%name3,'a') as wr_f:
526                     for k,v in dic.items():
527                         w_f.write('%s,%s\n' %(k,v))
528                     res1 = time.strftime('%Y-%m-%d %X')
529                     res2 = '向%s转账¥%s' %(username,inpumoney)
530                     wr_f.write('%s---%s' % (res1, res2))
531                 print('转账成功')
532                 user()
533             else:
534                 print('请输入正确金额')
535 
536 def user_info():
537     '''ATM用户信息(流水账单)'''
538     user_choice = input('1.查询账户余额2.打印流水3.返回:').strip()
539     if user_choice == '1':
540         op_file('atmye.txt')
541         op1_file()
542         print('【用户名:%s余额:%s】'%(name3,dic[name3]))
543         user()
544     if user_choice == '2':
545         with open('%sls'%name3) as r_f:
546             data = r_f.read()
547         print(data)
548         user()
549     if user_choice == '3':
550         user()
551     else:
552         print('请输入正确编号')
553 
554 
555 
556 
557 import time
558 import os
559 
560 
561 msg_dic = {
562     '苹果': 10,
563     '特斯拉': 100000,
564     'mac': 3000,
565     'lenovo': 30000,
566     '鸡肉': 10,
567     'iphonex':5800,
568     '咖啡':30,
569     'vivo':2499,
570     '自行车':200
571 
572 }
573 
574 f=open('db.txt','a')
575 f.close()
576 f= open('ye.txt','a')
577 f.close()
578 f=open('gwc.txt','a')
579 f.close()
580 
581 shopping_cart = []
582 order = []
583 order_number = 0
584 zj2 = 0
585 balance = 0
586 dic00 = {'user':0,'admin':0,'scuser':0}
587 
588 waicen()

猜你喜欢

转载自www.cnblogs.com/sw-z/p/9724314.html