python---信用卡ATM
Posted 珠峰上吹泡泡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python---信用卡ATM相关的知识,希望对你有一定的参考价值。
一 需求
模拟实现一个ATM + 购物商城程序
- 额度 15000或自定义
- 实现购物商城,买东西加入 购物车,调用信用卡接口结账
- 可以提现,手续费5%
- 每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息(暂时未做)
- 支持多账户登录
- 支持账户间转账
- 记录每月日常消费流水
- 提供还款接口
- ATM记录操作日志
- 提供管理接口,包括添加账户、用户额度,冻结账户等
- 用户认证用装饰器
二 代码实现
2.1 框架
#项目名称: ATM + 购物商城程序 #作者: Terry #博客地址 https://www.cnblogs.com/Terrypython/ #实现功能 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录,登录错误三次以后会有提示,是否继续,并且保存到日志里边 支持账户间转账 记录每月日常消费流水 提供还款接口 ATM记录操作日志 提供管理接口,包括添加账户、用户额度,冻结账户等。。。 用户认证用装饰器 #目录结构: ├── ATM ├── bin #入口程序目录 │ ├── __init__.py │ └── atm.py #入口程序(启动程序) ├── conf #配置文件目录 │ ├── __init__.py │ └── setting.py ├── core #程序核心目录 │ ├── __init__.py │ ├── admincenter.py #管理模块 │ ├── authentication.py #认证模块 │ ├── creditcard.py #信用卡模块 │ ├── shopping.py #购物模块 ├── database #程序数据库 │ ├── creditcard_dict #信用卡数据库 │ ├── creditcard_record #信用卡流水记录数据库 │ ├── details_tip #提示信息 │ ├── product_list #商城产品数据库 │ └── shopping_car #购物车数据库 │ ├── shopping_record #购物记录 │ └── users_dict #用户数据库 └── log ├── __init__.py └── rz.py ├── README.md #运行说明: 见我的博客详解
2.2 启动模块(bin)
#__author__:小辉 #DATE:2018/7/3 import sys,os #程序主目录 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #添加环境变量 sys.path.append(BASE_DIR) from core import admincenter,shopping,authentication,creditcard while True: print("\\33[36;1m欢迎进入信用卡购物模拟程序\\33[0m".center(50, "#"), "\\n 1 购物中心\\n", "2 信用卡中心\\n", "3 后台管理\\n", "q 退出程序\\n") choice_id = input("\\33[34;0m请选择要进入项目\\33[0m:") if choice_id == "1": res = authentication.user_auth() if res != None : if res[0] == "True":# 认证模块返回的是True current_user = res[1]#持有者姓名 shopping.Empty_shopping_car() while True: print("\\33[36;1m欢迎进入购物中心\\33[0m".center(50,"*"), "\\n 1 购物商场\\n", "2 查看购物车\\n", "3 购物结算\\n", "4 个人中心\\n", "b 返回\\n") choice_id = input("\\33[34;0m选择要进入模式的ID\\33[0m:") if choice_id == "1": shopping.Shopping_mall() elif choice_id == "2": shopping.Shopping_car() elif choice_id == "3": shopping.Pay_shopping(current_user) elif choice_id == "4": while True: print("\\33[33;1m个人中心\\33[0m".center(50, "*"), "\\n1 购物历史记录\\n" "2 修改登录密码\\n" "3 修改个人信息\\n" "4 修改行用卡绑定\\n" "b 返回\\n") choice_id = input("\\33[34;0m选择要进入的项目\\33[0m:") if choice_id == "1": shopping.Catcar_record(current_user) elif choice_id == "2": shopping.Updata_password(current_user) elif choice_id == "3": shopping.Updata_address(current_user) elif choice_id == "4": shopping.Link_creditcard(current_user) elif choice_id == "b": break else: print("\\33[31;0m输入的ID无效,请重新选择\\33[0m") elif choice_id == "b": break else: print("\\33[31;0m输入的ID无效,请重新选择\\33[0m") else: print("您已经错误输入多次,还想继续尝试吗") login_again=input(\'请输入您的选择:【c】继续/【b】退出\') if login_again==\'c\': continue elif login_again==\'b\': break else: print(\'您输入的字符无效\') elif choice_id == "2": res = authentication.creditcard_auth() if res != None: if res[0] == "True": current_creditcard= res[1] while True: print("\\33[36;1m信用卡中心\\33[0m".center(50, "*"), "\\n1 我的信用卡\\n" "2 提现\\n" "3 转账\\n" "4 还款\\n" "5 流水记录\\n" "b 返回\\n") choice_id = input("\\33[34;0m选择要进入项目\\33[0m:") if choice_id == "1": creditcard.My_creditcard(current_creditcard) elif choice_id == "2": creditcard.Cash_advance(current_creditcard) elif choice_id == "3": creditcard.Transfer(current_creditcard) elif choice_id == "4": creditcard.Repayment(current_creditcard) elif choice_id == "5": creditcard.Catcard_record(current_creditcard) elif choice_id == "b": break else: print("\\33[31;0m输入的ID无效,请重新选择\\33[0m") elif choice_id == "3": res = authentication.admincenter_auth() if res != None: while True: print("\\33[36;1m管理中心\\33[0m".center(50, "*"), "\\n1 创建账号\\n" "2 锁定账号\\n" "3 解锁账号\\n" "4 发行信用卡\\n" "5 冻结信用卡\\n" "6 解冻信用卡\\n" "7 提升信用卡额度\\n" "b 返回\\n") choice_id = input("\\33[34;0m选择要进入模式的ID\\33[0m:") if choice_id == "1": admincenter.User_create() elif choice_id == "2": admincenter.Lock_user() elif choice_id == "3": admincenter.Unlock_user() elif choice_id == "4": admincenter.Creditcard_create() elif choice_id == "5": admincenter.Lock_creditcard() elif choice_id == "6": admincenter.Unlock_creditcard() elif choice_id == "7": admincenter.Updata_limit() elif choice_id == "b": break else: print("\\33[31;0m输入的ID无效,请重新选择\\33[0m") elif choice_id == "q": break else: print("\\33[31;0m输入的ID无效,请重新选择\\33[0m")
2.3 核心模块(core)
2.3.1 购物模块(shopping)
#__author__:小辉 #DATE:2018/7/4 import json,os,time BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) \'\'\'数据库文件相对路径\'\'\' __db_product = BASE_DIR + r"\\db\\product_list" __db_shoping_car = BASE_DIR + r"\\db\\shopping_car" __db_users_dict = BASE_DIR + r"\\db\\users_dict" __db_creditcard_dict = BASE_DIR + r"\\db\\creditcard_dict" __db_shopping_record = BASE_DIR + r"\\db\\shopping_record" __db_creditcard_record = BASE_DIR + r"\\db\\creditcard_record" \'\'\'购物商城\'\'\' def Shopping_mall(): shopping_list,pro_list = [],[] with open(__db_product, "r", encoding="utf-8") as f_product: for item in f_product: pro_list.append(item.strip("\\n").split()) def pro_inf(): print("编号\\t商品\\t\\t价格") for index, item in enumerate(pro_list): print("%s\\t\\t%s\\t\\t%s" % (index, item[0], item[1])) while True: print(("\\33[32;0m目前商城在售的商品信息\\33[0m").center(40, "-")) pro_inf() choice_id = input("\\n\\33[34;0m选择要购买的商品编号 【购买 ID】/【返回 b】\\33[0m:") if choice_id.isdigit(): choice_id = int(choice_id) if choice_id < len(pro_list) and choice_id >=0: pro_item = pro_list[choice_id] print("\\33[31;0m商品%s加入购物车 价格%s\\33[0m"%(pro_item[0],pro_item[1])) shopping_list.append(pro_item)#商品在shopping__list当中 else: print("\\33[31;0m错误:没有相应的编号 请重新输入:\\33[0m\\n") elif choice_id == "b": with open(__db_shoping_car, "r+") as f_shopping_car: list = json.loads(f_shopping_car.read()) list.extend(shopping_list) f_shopping_car.seek(0) f_shopping_car.truncate(0) list = json.dumps(list) f_shopping_car.write(list) break else: print("\\33[31;0m错误:没有相应的编号 请重新输入:\\33[0m\\n") \'\'\'清空购物车\'\'\' def Empty_shopping_car(): with open(__db_shoping_car, "w") as f_shopping_car: list = json.dumps([]) f_shopping_car.write(list) \'\'\'购物车\'\'\' def Shopping_car(): while True: with open(__db_shoping_car, "r+") as f_shopping_car: list = json.loads(f_shopping_car.read()) sum = 0 print("\\33[32;0m购物车信息清单\\33[0m".center(40,"-")) for index,item in enumerate(list): print(index,item[0],item[1]) sum +=int(item[1]) print("\\33[31;1m商品总额共计: %s\\33[0m"%(sum)) if_buy = input("\\n\\33[34;0m选择要进行的操作 返回【b】/清空【f】\\33[0m:") if if_buy == "b" : break if if_buy == "f": Empty_shopping_car() \'\'\'购物记录\'\'\' def Shoppingcar_record(current_user,value): with open(__db_shopping_record, "r+") as f_shoppingcar_record: record_dict = json.loads(f_shoppingcar_record.read()) month = time.strftime(\'%Y-%m-%d\', time.localtime()) times = time.strftime("%H:%M:%S") if str(current_user) not in record_dict.keys(): record_dict[current_user]={month:{times:value}} else: if month not in record_dict[current_user].keys(): record_dict[current_user][month] = {times: value} else: record_dict[current_user][month][times] = value dict = json.dumps(record_dict) f_shoppingcar_record.seek(0) f_shoppingcar_record.truncate(0) f_shoppingcar_record.write(dict) \'\'\'查看购物记录\'\'\' def Catcar_record(current_user): while True: print("\\33[32;0m用户 %s 购物记录\\33[0m".center(40, "-")%(current_user)) with open(__db_shopping_record, "r+") as f_shoppingcar_record: record_dict = json.loads(f_shoppingcar_record.read()) if current_user not in record_dict.keys(): print("\\33[31;0m用户 %s 还没有进行过消费\\33[0m\\n" % (current_user)) else: data = sorted(record_dict[current_user])#排序 for d in data: times = sorted(record_dict[current_user][d]) for t in times: print("\\33[31;0m【时间】 %s %s\\33[0m"%(d, t)) items =record_dict[current_user][d][t] print("\\33[31;0m【商品】 【价格】\\33[0m") for v in items: print("\\33[31;0m %s\\t\\t%s\\33[0m"%(v[0],v[1])) if_back = input("\\n\\33[34;0m是否返回 返回【b】\\33[0m:") if if_back == "b": break #信用卡记录 def Creditcard_record(creditcard,value): with open(__db_creditcard_record, "r+") as f_creditcard_record: record_dict = json.loads(f_creditcard_record.read()) month = time.strftime(\'%Y-%m-%d\', time.localtime()) times = time.strftime("%H:%M:%S") if str(creditcard) not in record_dict.keys(): record_dict[creditcard]={month:{times:value}} else: if month not in record_dict[creditcard].keys(): record_dict[creditcard][month] = {times: value} else: record_dict[creditcard][month][times] = value dict = json.dumps(record_dict) f_creditcard_record.seek(0) f_creditcard_record.truncate(0) f_creditcard_record.write(dict) \'\'\'信用卡密码认证\'\'\' def Auth_creditcard(creditcard): with open(__db_creditcard_dict, "r+") as f_creditcard_dict: creditcard_dict = json.loads(f_creditcard_dict.read()) passwd = input("\\33[34;0m当前信用卡【%s】 请输入支付密码:\\33[0m:"%(creditcard)) if passwd == creditcard_dict[creditcard]["password"]: return True else: print("\\33[31;0m密码输入错误,支付失败\\33[0m") \'\'\'购物结算\'\'\' def Pay_shopping(current_user): while True: sum = 0 print("\\33[32;0m购物结算\\33[0m".center(40, "-")) with open(__db_shoping_car, "r+") as f_shopping_car: list = json.loads(f_shopping_car.read()) for item in list: sum += int(item[1]) if_pay = input("\\n\\n\\33[34;0m当前商品总额:%s 是否进行支付 确定【y】/返回【b】\\33[0m:"%(sum)) if if_pay == "y": with open(__db_users_dict, "r+") as f_users_dict: users_dict = json.loads(f_users_dict.read()) creditcard=users_dict[current_user]["creditcard"] if creditcard == 0: print("\\33[31;0m账号 %s未绑定信用卡,请到个人中心里修改信用卡绑定\\33[0m\\n"%(current_user)) else: with open(__db_creditcard_dict, "r+") as f_creditcard_dict: creditcard_dict = json.loads(f_creditcard_dict.read()) limit = creditcard_dict[creditcard]["limit"] limit_new = limit - sum if limit_new >=0: res = Auth_creditcard(creditcard) #信用卡认证 if res == True:#认证成功 creditcard_dict[creditcard]["limit"]=limit_new dict=json.dumps(creditcard_dict) f_creditcard_dict.seek(0) f_creditcard_dict.truncate(0) f_creditcard_dict.write(dict) value = "购物支付 %s"%(sum) print("\\33[31;om支付成功,当前余额 %s元\\33[0m\\n"%(limit_new)) Shoppingcar_record(current_user,list) Creditcard_record(creditcard, value) Empty_shopping_car() else: print("\\33[31;0m当前信用卡额度 %s元 不足以支付购物款 可绑定其他信用卡支付\\33[0m\\n"%(limit)) if if_pay == "b": break \'\'\'修改信用卡绑定\'\'\' def Link_creditcard(current_user): while True: print("\\33[32;0m修改信用卡绑定\\33[0m".center(40, "-")) with open(__db_users_dict, "r+") as f_users_dict: users_dict = json.loads(f_users_dict.read()) creditcard = users_dict[current_user]["creditcard"] if creditcard == 0 : print("当前账号: \\t%s"%(current_user)) print("信用卡绑定:\\33[31;0m未绑定\\33[0m\\n") else: print("当前账号: \\t%s" %(current_user)) print("绑定的信用卡: %s\\n"%(creditcard)) if_updata = input("\\33[34;0m是否要修改信用卡绑定 确定【y】/返回【b】\\33[0m:") if if_updata == "y"以上是关于python---信用卡ATM的主要内容,如果未能解决你的问题,请参考以下文章python基础作业------模拟实现一个ATM + 购物商城程序