Python__模拟实现一个ATM+购物商城程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python__模拟实现一个ATM+购物商城程序相关的知识,希望对你有一定的参考价值。
需求:
模拟实现一个ATM+购物商城程序
1.额度1500或者自定义
2.实现购物商城,买东西加入购物车,调用信用卡接口
3.可以提现,手续费5%
4.支持账户登录
5.支持账户间转账
6.记录每日日常消费流水
7.提供还款接口
8.ATM记录操作日志
9.提供管理接口,包括添加账户,用户额度,冻结账户等
10.用户认证用装饰
#Author wangmengzhu ‘‘‘ 用户认证,确认是用户登录,判断用户登录的用户名和密码是否正确,判断用户认证的长度是否为0,使用装饰器 用户登录认证,信用卡登录认证,管理员登录认证 ‘‘‘ import time,os def auth(auth_type): ‘‘‘ 用户的认证信息 ‘‘‘ def my_wrapper(func): if auth_type == ‘user_auth‘: def wrpper(*args,**kwargs): name = input(‘请输入你的用户名>>: ‘).strip() password = input(‘请输入你的密码>>: ‘).strip() if len(name) > 0: with open(‘用户名和密码.txt‘,‘r‘,encoding = ‘utf-8‘) as f: line = f.read() my_dic = eval(line) if name in my_dic and password == my_dic[name]: print(‘%s欢迎登陆‘%(name)) func(*args,**kwargs) else: print(‘%s认证失败‘%(name)) else: print(‘输入的用户名不能为空‘) return wrpper elif auth_type == ‘credit_auth‘: def wrpper(*args, **kwargs): credit_number = input(‘请输入你的信用卡号>>: ‘).strip() credit_password = input(‘请输入你的信用卡密码>>: ‘).strip() if len(credit_number) == 10: with open(‘信用卡号和密码.txt‘,‘r‘,encoding = ‘utf-8‘) as f: line = f.read() my_dic = eval(line) if credit_number in my_dic and credit_password == my_dic[credit_number]: print(‘认证成功,欢迎登陆‘) func(*args, **kwargs) else: print(‘认证不成功‘) else: print(‘输入的信用卡号的长度不对‘) return wrpper elif auth_type == ‘admin_auth‘: def wrpper(*args, **kwargs): admin_name = input(‘请输入你的管理员用户名>>: ‘).strip() admin_password = input(‘请输入你的管理员密码>>: ‘).strip() if len(admin_name) > 0: with open(‘管理员用户名和密码.txt‘, ‘r‘, encoding=‘utf-8‘) as f: line = f.read() my_dic = eval(line) if admin_name in my_dic and admin_password== my_dic[admin_name]: print(‘%s欢迎登陆‘ % (admin_name)) func(*args, **kwargs) with open(‘日志模块.txt‘, ‘a+‘, encoding=‘utf-8‘) as f: f.write(‘%s %s run‘ % (time.strftime(‘%Y-%m-%d %X‘), func.__name__)) else: print(‘%s认证失败‘ % (admin_name)) else: print(‘输入的用户名不能为空‘) return wrpper return my_wrapper @auth(auth_type = ‘user_auth‘) def user_auth(): print(‘用户登录认证‘.center(20,‘*‘)) @auth(auth_type = ‘credit_auth‘) def credit_auth(): print(‘信用卡认证‘.center(20,‘*‘)) @auth(auth_type = ‘admin_auth‘) def admin_auth(): print(‘管理员认证‘.center(20,‘*‘)) ‘‘‘ 购物商城和购物车 ‘‘‘ def shop_list(): my_shop_list = [] my_buy_list = [] with open(‘商品列表.txt‘,‘r+‘,encoding = ‘utf-8‘) as f: for item in f: item.split(‘,‘) my_shop_list.append(item) print(‘目前商品所在的信息‘.center(20, ‘*‘)) for j in my_shop_list: print(j,end = ‘‘) print(‘\n‘,end = ‘‘) while True: my_choice = input(‘请输入想要购买的商品序号>>: ‘).strip() if my_choice.isdigit(): my_choice = int(my_choice) if my_choice < len(my_shop_list) and my_choice > 0: my_buy = my_shop_list[my_choice] print(my_buy) with open(‘我的购物列表.txt‘, ‘a+‘, encoding=‘utf-8‘) as f: f.write(my_buy) break else: print(‘输入的商品编号错误‘) continue else: print(‘输入的数字不合法‘) continue ‘‘‘ 清空购物车 ‘‘‘ def my_delete_cart(): while True: my_cart = []#我的购物车 with open(‘我的购物列表.txt‘,‘r+‘,encoding = ‘utf-8‘) as f: for line in f: my_cart.append(line) if my_cart != []: my_choice = input(‘是否清空购物车,回答YES或者NO>>: ‘).strip() print(‘我的购物车中的商品‘.center(20,‘*‘)) for j in my_cart: print(j,end = ‘‘) if my_choice ==‘YES‘: with open(‘我的购物列表.txt‘,‘w‘,encoding = ‘utf-8‘) as f: pass print(‘购物车已经清空‘) elif my_choice == ‘NO‘: print(‘请继续购物吧!‘) else: print(‘输入不正确,请重新输入‘) else: print(‘购物车里边还没有商品‘) break ‘‘‘ 购物车结算 ‘‘‘ def my_pay(): while True: print(‘购物结算‘.center(20,‘*‘)) my_cart = [] my_trans = [] with open(‘我的购物列表.txt‘,‘r+‘,encoding = ‘utf-8‘) as f: for line in f: my_cart.append(line) if my_cart != []: print(‘购物车清单‘.center(20,‘*‘)) for j in my_cart: my_new = j.split(‘,‘) my_trans.append(my_new) money = sum([int(i[2]) for i in my_trans]) print(money) else: print(‘你还没有消费过,快去买点你心仪的商品吧!‘) break my_choice = input(‘是否进行结账[合计%s人民币](请回答Y或者N)>>: ‘%(money)).strip() if my_choice == ‘N‘: break elif my_choice == ‘Y‘: credit_auth()#调用信用卡接口认证 my_credit_num = input(‘请输入你的信用卡号‘).strip() with open(‘信用卡号和密码.txt‘,‘r+‘,encoding = ‘utf-8‘) as f: line = f.read() my_dic = eval(line) if my_credit_num not in my_dic: print(‘认证的信用卡号不存在,请重新输入‘) else: with open(‘信用卡号和资金.txt‘,‘r+‘,encoding = ‘utf-8‘) as f1: line1 = f1.read() my_dic1 = eval(line1) your_credit = input(‘请输入你的信用卡号>>: ‘).strip() your_password = input(‘请输入你的密码>>: ‘).strip() if your_credit in my_dic1 and your_password == my_dic1[your_credit]: print(‘你的账户总共的资金为%s‘%(my_dic1[your_credit])) your_limit = my_dic1[your_credit] - money if your_limit >= 0: my_dic1[your_credit] = your_limit print(‘支付成功,当前信用卡余额为%s‘%(your_limit)) else: print(‘余额不足,请充值‘) else: print(‘输入的密码错误,请重新输入‘) continue ‘‘‘ 信用卡信息 ‘‘‘ def credit_data(): while True: with open(‘信用卡号和资金.txt‘,‘r+‘,encoding = ‘utf-8‘) as f: my_msg = f.read() my_dic = eval(my_msg) your_choice = input(‘请输入要查看信息的信用卡号>>: ‘).strip() if your_choice in my_dic: print(‘我的信用卡信息‘.center(20,‘*‘)) print(‘持卡人信用卡号%s\n持卡人余额%s‘%(your_choice,my_dic[your_choice])) else: print(‘你输入的信用卡号不存在,请重新输入‘) continue choice = input(‘是否选择退出(是或者否)>>: ‘).strip() if choice == ‘是‘: print(‘欢迎下次查询‘) break else: continue ‘‘‘ 信用卡转账 ‘‘‘ def my_trans(): while True: with open(‘信用卡号和资金.txt‘,‘r+‘,encoding = ‘utf-8‘) as f: line = f.read() my_dict = eval(line) choice = input(‘请输入信用卡号>>: ‘).strip() if choice in my_dict: current_money = my_dict[choice] trans_account = input(‘请输入你要转账的账号>>: ‘).strip() if trans_account.isdigit(): if len(trans_account) == 10: if trans_account in my_dict: your_trans_money = input(‘请输入你的转账金额>>: ‘).strip() if your_trans_money.isdigit(): money = int(your_trans_money) with open(‘信用卡号和密码.txt‘,‘r+‘,encoding = ‘utf-8‘) as f1: line1 = f1.read() my_dict1 = eval(line1) your_password = input(‘请输入你的信用卡密码>>: ‘).strip() if your_password == my_dict1[trans_account]: if money < my_dict[trans_account]: my_dict[trans_account] = my_dict[trans_account] - money my_dict[choice] = my_dict[choice] + money print(‘转账成功‘.center(20,‘*‘)) break else: print(‘密码不正确,请重新输入‘) else: print(‘输入的金额不合法‘) else: print(‘输入的账号不存在‘) else: print(‘信用卡号的长度不合法‘) ‘‘‘ 锁定用户 ‘‘‘ def lock_user(): while True: print(‘锁定用户‘.center(20,‘*‘)) with open(‘用户.txt‘,‘r+‘,encoding = ‘utf-8‘) as f: line= f.read() my_lst = eval(line) choice = input(‘是否进行锁定用户(是或者否)>>: ‘).strip() if choice == ‘否‘: break if choice == ‘是‘: my_lock_user = input(‘请输入想要锁定的用户名>>: ‘).strip() for in_lst in my_lst: if my_lock_user in in_lst: in_lst[1] = ‘已锁定‘ print(‘%s用户已经锁定‘%(my_lock_user)) else: print(‘输入的用户名不存在‘) ‘‘‘ 创建用户 ‘‘‘ def new_user(): while True: print(‘创建用户‘.center(20,‘*‘)) with open(‘用户名和密码.txt‘,‘r+‘,encoding = ‘utf-8‘) as f: line = f.read() my_dic = eval(line) for key in my_dic: print(‘系统已经有的用户:%s‘%(key)) my_choice = input(‘请输入你要创建的用户名(选择是或者否)>>: ‘).strip() if my_choice == ‘否‘: break if my_choice == ‘是‘: user_name = input(‘请输入你想要创建的用户名>>: ‘).strip() user_password = input(‘请输入创建用户的密码>>: ‘).strip() if user_name not in my_dic: if len(user_name) > 0 and len(user_password) > 0: my_dic[user_name] = user_password print(‘创建%s成功‘%(user_name)) else: print(‘输入的用户或者密码不能为空!‘) else: print(‘输入的用户已经存在‘) ‘‘‘ 主函数接口 ‘‘‘ def main(): print(‘购物商城ATM系统‘.center(20,‘*‘)) while True: print(‘欢迎来到ATM购物系统‘.center(20,‘*‘)) choice = input(‘请输入选择的ID号>>: ‘).strip() if choice == ‘q‘: print(‘再见‘.center(20,‘*‘)) exit() if choice.isdigit(): choice = int(choice) if choice >= 1 and choice <= 4: while True: if choice == 1: print(‘欢迎来到信用卡中心‘.center(20,‘*‘)) credit_choice = input(‘请输入信用卡板块号>>: ‘).strip() if credit_choice.isdigit(): credit_choice = int(credit_choice) if credit_choice >= 1 and credit_choice <= 2: if credit_choice == 1: credit_data()#信用卡信息 break elif credit_choice == 2: my_trans()#信用卡转账信息 break elif choice == 2: print(‘欢迎来到购物中心‘.center(20,‘*‘)) shopping_choice = input(‘请选择购物板块>>: ‘).strip() if shopping_choice.isdigit(): shopping_choice = int(shopping_choice) if shopping_choice >= 1 and shopping_choice <= 3: while True: if shopping_choice == 1: shop_list()#购物商城和购物车 break elif shopping_choice == 2: my_delete_cart()#清空购物车板块 break elif shopping_choice == 3: my_pay()#购物车结算版块 break else: print(‘请输入正确的板块号‘) elif choice == 3: print(‘欢迎来到用户管理中心‘.center(20,‘*‘)) admin_choice = input(‘请输入选择的板块号>>: ‘).strip() if admin_choice.isdigit(): admin_choice = int(admin_choice) if admin_choice >= 1 and admin_choice <= 3: while True: if admin_choice == 1: user_auth()#用户认证板块 break elif admin_choice == 2: credit_auth()#信用卡认证 break elif admin_choice == 3: admin_auth()#管理员认证 break elif choice == 4: print(‘欢迎来到创建用户板块‘.center(20,‘*‘)) user_choice = input(‘请输入选择的板块号>>: ‘).strip() if user_choice.isdigit(): user_choice = int(user_choice) if user_choice >= 1 and user_choice <= 2: while True: if user_choice == 1: lock_user()#锁定用户板块 break elif user_choice == 2: new_user()#创建用户板块 break else: print(‘输入的板块号不正确‘) break main()
以上是关于Python__模拟实现一个ATM+购物商城程序的主要内容,如果未能解决你的问题,请参考以下文章