Python__模拟实现一个ATM+购物商城程序

需求:
模拟实现一个ATM+购物商城程序
1.额度1500或者自定义
2.实现购物商城,买东西加入购物车,调用信用卡接口
3.可以提现,手续费5%
4.支持账户登录
5.支持账户间转账
6.记录每日日常消费流水
7.提供还款接口
8.ATM记录操作日志
9.提供管理接口,包括添加账户,用户额度,冻结账户等
10.用户认证用装饰
  1 #Author wangmengzhu
  2 '''
  3 用户认证,确认是用户登录,判断用户登录的用户名和密码是否正确,判断用户认证的长度是否为0,使用装饰器
  4 用户登录认证,信用卡登录认证,管理员登录认证
  5 '''
  6 import time,os
  7 def auth(auth_type):
  8     '''
  9     用户的认证信息
 10     '''
 11     def my_wrapper(func):
 12         if auth_type == 'user_auth':
 13             def wrpper(*args,**kwargs):
 14                 name = input('请输入你的用户名>>: ').strip()
 15                 password = input('请输入你的密码>>: ').strip()
 16                 if len(name) > 0:
 17                     with open('用户名和密码.txt','r',encoding = 'utf-8') as f:
 18                         line = f.read()
 19                         my_dic = eval(line)
 20                     if name in my_dic and password == my_dic[name]:
 21                         print('%s欢迎登陆'%(name))
 22                         func(*args,**kwargs)
 23                     else:
 24                         print('%s认证失败'%(name))
 25                 else:
 26                     print('输入的用户名不能为空')
 27             return wrpper
 28         elif auth_type == 'credit_auth':
 29             def wrpper(*args, **kwargs):
 30                 credit_number = input('请输入你的信用卡号>>: ').strip()
 31                 credit_password = input('请输入你的信用卡密码>>: ').strip()
 32                 if len(credit_number) == 10:
 33                     with open('信用卡号和密码.txt','r',encoding = 'utf-8') as f:
 34                         line = f.read()
 35                         my_dic = eval(line)
 36                     if credit_number in my_dic and credit_password == my_dic[credit_number]:
 37                         print('认证成功,欢迎登陆')
 38                         func(*args, **kwargs)
 39                     else:
 40                         print('认证不成功')
 41                 else:
 42                     print('输入的信用卡号的长度不对')
 43             return wrpper
 44         elif auth_type == 'admin_auth':
 45             def wrpper(*args, **kwargs):
 46                 admin_name = input('请输入你的管理员用户名>>: ').strip()
 47                 admin_password = input('请输入你的管理员密码>>: ').strip()
 48                 if len(admin_name) > 0:
 49                     with open('管理员用户名和密码.txt', 'r', encoding='utf-8') as f:
 50                         line = f.read()
 51                         my_dic = eval(line)
 52                     if admin_name  in my_dic and admin_password== my_dic[admin_name]:
 53                         print('%s欢迎登陆' % (admin_name))
 54                         func(*args, **kwargs)
 55                         with open('日志模块.txt', 'a+', encoding='utf-8') as f:
 56                             f.write('%s %s run' % (time.strftime('%Y-%m-%d %X'), func.__name__))
 57                     else:
 58                         print('%s认证失败' % (admin_name))
 59                 else:
 60                     print('输入的用户名不能为空')
 61 
 62         return wrpper
 63     return my_wrapper
 64 @auth(auth_type = 'user_auth')
 65 def user_auth():
 66     print('用户登录认证'.center(20,'*'))
 67 @auth(auth_type = 'credit_auth')
 68 def credit_auth():
 69     print('信用卡认证'.center(20,'*'))
 70 @auth(auth_type = 'admin_auth')
 71 def admin_auth():
 72     print('管理员认证'.center(20,'*'))
 73 
 74 '''
 75 购物商城和购物车
 76 '''
 77 def shop_list():
 78     my_shop_list = []
 79     my_buy_list = []
 80     with open('商品列表.txt','r+',encoding = 'utf-8') as f:
 81         for item in f:
 82             item.split(',')
 83             my_shop_list.append(item)
 84         print('目前商品所在的信息'.center(20, '*'))
 85         for j in my_shop_list:
 86             print(j,end = '')
 87         print('
',end = '')
 88     while True:
 89         my_choice = input('请输入想要购买的商品序号>>: ').strip()
 90         if my_choice.isdigit():
 91             my_choice = int(my_choice)
 92             if my_choice < len(my_shop_list) and my_choice > 0:
 93                 my_buy = my_shop_list[my_choice]
 94                 print(my_buy)
 95                 with open('我的购物列表.txt', 'a+', encoding='utf-8') as f:
 96                     f.write(my_buy)
 97                 break
 98             else:
 99                 print('输入的商品编号错误')
100                 continue
101         else:
102             print('输入的数字不合法')
103             continue
104 
105 
106 '''
107 清空购物车
108 '''
109 def my_delete_cart():
110     while True:
111         my_cart = []#我的购物车
112         with open('我的购物列表.txt','r+',encoding = 'utf-8') as f:
113             for line in f:
114                 my_cart.append(line)
115         if my_cart != []:
116             my_choice = input('是否清空购物车,回答YES或者NO>>: ').strip()
117             print('我的购物车中的商品'.center(20,'*'))
118             for j in my_cart:
119                 print(j,end = '')
120             if my_choice =='YES':
121                 with open('我的购物列表.txt','w',encoding = 'utf-8') as f:
122                     pass
123                 print('购物车已经清空')
124             elif my_choice == 'NO':
125                 print('请继续购物吧!')
126             else:
127                 print('输入不正确,请重新输入')
128         else:
129             print('购物车里边还没有商品')
130             break
131 
132 
133 
134 '''
135 购物车结算
136 '''
137 def my_pay():
138     while True:
139         print('购物结算'.center(20,'*'))
140         my_cart = []
141         my_trans = []
142         with open('我的购物列表.txt','r+',encoding = 'utf-8') as f:
143             for line in f:
144                 my_cart.append(line)
145             if my_cart != []:
146                 print('购物车清单'.center(20,'*'))
147                 for j in my_cart:
148                     my_new = j.split(',')
149                     my_trans.append(my_new)
150                     money = sum([int(i[2]) for i in my_trans])
151                     print(money)
152             else:
153                 print('你还没有消费过,快去买点你心仪的商品吧!')
154                 break
155         my_choice = input('是否进行结账[合计%s人民币](请回答Y或者N)>>: '%(money)).strip()
156         if my_choice == 'N':
157             break
158         elif my_choice == 'Y':
159             credit_auth()#调用信用卡接口认证
160             my_credit_num = input('请输入你的信用卡号').strip()
161             with open('信用卡号和密码.txt','r+',encoding = 'utf-8') as f:
162                 line = f.read()
163                 my_dic = eval(line)
164                 if my_credit_num not in my_dic:
165                     print('认证的信用卡号不存在,请重新输入')
166                 else:
167                     with open('信用卡号和资金.txt','r+',encoding = 'utf-8') as f1:
168                         line1 = f1.read()
169                         my_dic1 = eval(line1)
170                         your_credit = input('请输入你的信用卡号>>: ').strip()
171                         your_password = input('请输入你的密码>>: ').strip()
172                         if your_credit in my_dic1 and your_password == my_dic1[your_credit]:
173                             print('你的账户总共的资金为%s'%(my_dic1[your_credit]))
174                             your_limit = my_dic1[your_credit] - money
175                             if your_limit >= 0:
176                                 my_dic1[your_credit] = your_limit
177                                 print('支付成功,当前信用卡余额为%s'%(your_limit))
178                             else:
179                                 print('余额不足,请充值')
180                         else:
181                             print('输入的密码错误,请重新输入')
182                             continue
183 '''
184 信用卡信息
185 '''
186 def credit_data():
187     while True:
188         with open('信用卡号和资金.txt','r+',encoding = 'utf-8') as f:
189             my_msg = f.read()
190             my_dic = eval(my_msg)
191             your_choice = input('请输入要查看信息的信用卡号>>: ').strip()
192             if your_choice in my_dic:
193                 print('我的信用卡信息'.center(20,'*'))
194                 print('持卡人信用卡号%s
持卡人余额%s'%(your_choice,my_dic[your_choice]))
195             else:
196                 print('你输入的信用卡号不存在,请重新输入')
197                 continue
198             choice = input('是否选择退出(是或者否)>>: ').strip()
199             if choice == '':
200                 print('欢迎下次查询')
201                 break
202             else:
203                 continue
204 
205 
206 
207 '''
208 信用卡转账
209 '''
210 def my_trans():
211     while True:
212         with open('信用卡号和资金.txt','r+',encoding = 'utf-8') as f:
213             line = f.read()
214             my_dict = eval(line)
215             choice = input('请输入信用卡号>>: ').strip()
216             if choice in my_dict:
217                 current_money = my_dict[choice]
218                 trans_account = input('请输入你要转账的账号>>: ').strip()
219                 if trans_account.isdigit():
220                     if len(trans_account) == 10:
221                         if trans_account in my_dict:
222                             your_trans_money = input('请输入你的转账金额>>: ').strip()
223                             if your_trans_money.isdigit():
224                                 money = int(your_trans_money)
225                                 with open('信用卡号和密码.txt','r+',encoding = 'utf-8') as f1:
226                                     line1 = f1.read()
227                                     my_dict1 = eval(line1)
228                                     your_password = input('请输入你的信用卡密码>>: ').strip()
229                                     if your_password == my_dict1[trans_account]:
230                                         if money < my_dict[trans_account]:
231                                             my_dict[trans_account] = my_dict[trans_account] - money
232                                             my_dict[choice] = my_dict[choice] + money
233                                             print('转账成功'.center(20,'*'))
234                                             break
235                                     else:
236                                         print('密码不正确,请重新输入')
237                             else:
238                                 print('输入的金额不合法')
239                         else:
240                             print('输入的账号不存在')
241                     else:
242                         print('信用卡号的长度不合法')
243 
244 '''
245 锁定用户
246 '''
247 def lock_user():
248     while True:
249         print('锁定用户'.center(20,'*'))
250         with open('用户.txt','r+',encoding = 'utf-8') as f:
251             line= f.read()
252             my_lst = eval(line)
253             choice = input('是否进行锁定用户(是或者否)>>: ').strip()
254             if choice == '':
255                 break
256             if choice == '':
257                 my_lock_user = input('请输入想要锁定的用户名>>: ').strip()
258                 for in_lst in my_lst:
259                     if my_lock_user in in_lst:
260                         in_lst[1] = '已锁定'
261                         print('%s用户已经锁定'%(my_lock_user))
262 
263 
264             else:
265                 print('输入的用户名不存在')
266 
267 '''
268 创建用户
269 '''
270 def new_user():
271     while True:
272         print('创建用户'.center(20,'*'))
273         with open('用户名和密码.txt','r+',encoding = 'utf-8') as f:
274             line = f.read()
275             my_dic = eval(line)
276             for key in my_dic:
277                 print('系统已经有的用户:%s'%(key))
278             my_choice = input('请输入你要创建的用户名(选择是或者否)>>: ').strip()
279             if my_choice == '':
280                 break
281             if my_choice == '':
282                 user_name = input('请输入你想要创建的用户名>>: ').strip()
283                 user_password = input('请输入创建用户的密码>>: ').strip()
284                 if user_name not in my_dic:
285                     if len(user_name) > 0 and len(user_password) > 0:
286                         my_dic[user_name] = user_password
287                         print('创建%s成功'%(user_name))
288                     else:
289                         print('输入的用户或者密码不能为空!')
290                 else:
291                     print('输入的用户已经存在')
292 '''
293 主函数接口
294 '''
295 def main():
296     print('购物商城ATM系统'.center(20,'*'))
297     while True:
298         print('欢迎来到ATM购物系统'.center(20,'*'))
299         choice = input('请输入选择的ID号>>: ').strip()
300         if choice == 'q':
301             print('再见'.center(20,'*'))
302             exit()
303         if choice.isdigit():
304             choice = int(choice)
305             if choice >= 1 and choice <= 4:
306                 while True:
307                     if choice == 1:
308                         print('欢迎来到信用卡中心'.center(20,'*'))
309                         credit_choice = input('请输入信用卡板块号>>: ').strip()
310                         if credit_choice.isdigit():
311                             credit_choice = int(credit_choice)
312                             if credit_choice >= 1 and credit_choice <= 2:
313                                 if credit_choice == 1:
314                                     credit_data()#信用卡信息
315                                     break
316                                 elif credit_choice == 2:
317                                     my_trans()#信用卡转账信息
318                                     break
319                     elif choice == 2:
320                         print('欢迎来到购物中心'.center(20,'*'))
321                         shopping_choice = input('请选择购物板块>>: ').strip()
322                         if shopping_choice.isdigit():
323                             shopping_choice = int(shopping_choice)
324                             if shopping_choice >= 1 and shopping_choice <= 3:
325                                 while True:
326                                     if shopping_choice == 1:
327                                         shop_list()#购物商城和购物车
328                                         break
329                                     elif shopping_choice == 2:
330                                         my_delete_cart()#清空购物车板块
331                                         break
332                                     elif shopping_choice == 3:
333                                         my_pay()#购物车结算版块
334                                         break
335                             else:
336                                 print('请输入正确的板块号')
337                     elif choice == 3:
338                         print('欢迎来到用户管理中心'.center(20,'*'))
339                         admin_choice = input('请输入选择的板块号>>: ').strip()
340                         if admin_choice.isdigit():
341                             admin_choice = int(admin_choice)
342                             if admin_choice >= 1 and admin_choice <= 3:
343                                 while True:
344                                     if admin_choice == 1:
345                                         user_auth()#用户认证板块
346                                         break
347                                     elif admin_choice == 2:
348                                         credit_auth()#信用卡认证
349                                         break
350                                     elif admin_choice == 3:
351                                         admin_auth()#管理员认证
352                                         break
353                     elif choice == 4:
354                         print('欢迎来到创建用户板块'.center(20,'*'))
355                         user_choice = input('请输入选择的板块号>>: ').strip()
356                         if user_choice.isdigit():
357                             user_choice = int(user_choice)
358                             if user_choice >= 1 and user_choice <= 2:
359                                 while True:
360                                     if user_choice == 1:
361                                         lock_user()#锁定用户板块
362                                         break
363                                     elif user_choice == 2:
364                                         new_user()#创建用户板块
365                                         break
366 
367             else:
368                 print('输入的板块号不正确')
369         break
370 
371 
372 main()
View Code
原文地址:https://www.cnblogs.com/wangmengzhu/p/7244665.html