一  需求

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

  1. 额度 15000或自定义
  2. 实现购物商城,买东西加入 购物车,调用信用卡接口结账
  3. 可以提现,手续费5%
  4. 每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息(暂时未做)
  5. 支持多账户登录
  6. 支持账户间转账
  7. 记录每月日常消费流水
  8. 提供还款接口
  9. ATM记录操作日志
  10. 提供管理接口,包括添加账户、用户额度,冻结账户等
  11. 用户认证用装饰器

二 代码实现

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 #运行说明:
见我的博客详解

README

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 == "":
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 == "":
shopping.Shopping_mall()
elif choice_id == "":
shopping.Shopping_car()
elif choice_id == "":
shopping.Pay_shopping(current_user)
elif choice_id == "":
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 == "":
shopping.Catcar_record(current_user)
elif choice_id == "":
shopping.Updata_password(current_user)
elif choice_id == "":
shopping.Updata_address(current_user)
elif choice_id == "":
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 == "":
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 == "":
creditcard.My_creditcard(current_creditcard)
elif choice_id == "":
creditcard.Cash_advance(current_creditcard)
elif choice_id == "":
creditcard.Transfer(current_creditcard)
elif choice_id == "":
creditcard.Repayment(current_creditcard)
elif choice_id == "":
creditcard.Catcard_record(current_creditcard)
elif choice_id == "b":
break
else:
print("\33[31;0m输入的ID无效,请重新选择\33[0m")
elif choice_id == "":
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 == "":
admincenter.User_create()
elif choice_id == "":
admincenter.Lock_user()
elif choice_id == "":
admincenter.Unlock_user()
elif choice_id == "":
admincenter.Creditcard_create()
elif choice_id == "":
admincenter.Lock_creditcard()
elif choice_id == "":
admincenter.Unlock_creditcard()
elif choice_id == "":
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")

atm.py

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":
creditcard_new = input("\33[34;0m输入新的信用卡卡号(6位数字)\33[0m:")
if creditcard_new.isdigit() and len(creditcard_new) ==6:
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
if creditcard_new in creditcard_dict.keys():
users_dict[current_user]["creditcard"]=creditcard_new
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("\33[31;1m信用卡绑定成功\33[0m\n")
else:
print("\33[31;0m输入信用卡卡号不存在(未发行)\33[0m\n")
else:
print("\33[31;0m输入信用卡格式错误\33[0m\n")
if if_updata == "b":
break '''修改登录密码'''
def Updata_password(current_user):
while True:
print("\33[32;0m修改登录密码\33[0m".center(40, "-"))
print("当前账号:\t%s\n当前密码:\t**\n"%(current_user))
if_updata = input("\33[34;0m是否要修改 % s登录密码 确定【y】/返回【b】\33[0m:"%(current_user))
if if_updata == "y":
with open(__db_users_dict, "r+") as f_users_dict:
users_dict = json.loads(f_users_dict.read())
password = users_dict[current_user]["password"]
old_pwd = input("\33[34;0m输入原来的密码\33[0m:")
if old_pwd == password:
new_pwd = input("\33[34;0m输入新的密码\33[0m:")
agin_pwd = input("\33[34;0m再输入新的密码\33[0m:")
if new_pwd == agin_pwd:
users_dict[current_user]["password"]=new_pwd
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("\33[31;1m密码修改成功\33[0m\n")
else:
print("\33[31;0m两次密码不一致\33[0m\n")
else:
print("\33[31;0m密码不正确\33[0m\n")
if if_updata == "b":
break '''修改收货地址'''
def Updata_address(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())
address = users_dict[current_user]["address"]
print("当前账号:\t%s\n当前收货地址:\t%s\n" % (current_user,address))
if_updata = input("\33[34;0m是否要修改 % s收货地址 确定【y】/返回【b】\33[0m:" % (current_user))
if if_updata == "y":
new_address = input("\33[34;0m输入新的收货地址\33[0m:")
users_dict[current_user]["address"]=new_address
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("\33[31;1m收货地址修改成功\33[0m\n")
if if_updata == "b":
break

shopping.py

2.3.2信用卡模块(creditcard)
#__author__:小辉
#DATE:2018/7/4 import json,os,datetime,time
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) '''数据库文件相对路径'''
__db_creditcard_dict = BASE_DIR + r"\db\creditcard_dict"
__db_detail_tip = BASE_DIR + r"\db\detail_tip"
__db_creditcard_record = BASE_DIR + r"\db\creditcard_record" '''取现需知'''
def detail_tip():
with open(__db_detail_tip,"r",encoding="utf-8") as f_detail_tip:
print(f_detail_tip.read()) '''我的信用卡'''
def My_creditcard(current_creditcard):
while True:
print("\33[32;0m我的信用卡信息\33[0m".center(40, "-"))
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
print("卡号:\t【%s】\n额度:\t【¥%s】\n提现额度:\t【¥%s】\n持卡人:\t【%s】\n"%(current_creditcard,
creditcard_dict[current_creditcard]["limit"],creditcard_dict[current_creditcard]["limitcash"],
creditcard_dict[current_creditcard]["personinfo"]))
if_back = input("\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 Cash_advance(current_creditcard):
while True:
print("\33[32;0m提现\33[0m".center(40, "-"))
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
limit = creditcard_dict[current_creditcard]["limit"]
limitcash = creditcard_dict[current_creditcard]["limitcash"]
print("信用卡号:\t【%s】\n提现额度:\t【¥%s】" % (current_creditcard, limitcash))
if limit >= limitcash:
print("可提现金额:\t【¥%s】\n" % (limitcash))
detail_tip()
if_adv = input("\n\33[34;0m是否进行提现 确定【y】/返回【b】\33[0m:")
if if_adv == "y":
cash = input("\33[34;0m输入要提现的金额 收取%5手续费\33[0m:")
if cash.isdigit():
cash=int(cash)
if cash != 0:
if cash <= limitcash:
limitcash = limitcash - int(cash*1.05)
limit = limit - int(cash*1.05)
creditcard_dict[current_creditcard]["limit"]=limit
creditcard_dict[current_creditcard]["limitcash"] = limitcash
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
dict = json.dumps(creditcard_dict)
f_creditcard_dict.write(dict)
record = "\33[31;1m提现¥%s 手续费¥%s\33[0m"%(cash,int(cash*0.05))
print(record,"\n")
Creditcard_record(current_creditcard,record)
else:
print("\33[31;0m超出信用卡提现额度\33[0m\n")
else:
print("\33[31;0m提现额度不能为空\33[0m\n")
if if_adv == "b":
break if limit < limitcash:
print("可提现金额:\t¥【%s】\n" % (int(limit*0.95)))
detail_tip()
if_adv = input("\n\33[34;0m是否进行提现 确定【y】/返回【b】\33[0m:")
if if_adv == "y":
cash = input("\33[34;0m输入要提现的金额 收取%5手续费\33[0m:")
if cash.isdigit():
cash = int(cash)
if cash != 0:
if cash <= int(limit*0.95):
limit = limit - int(cash * 1.05)
limitcash = limitcash - int(cash * 1.05)
creditcard_dict[current_creditcard]["limitcash"] = limitcash
creditcard_dict[current_creditcard]["limit"] = limit
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
dict = json.dumps(creditcard_dict)
f_creditcard_dict.write(dict)
record= "\33[31;1m提现¥%s 手续费¥%s\33[0m" % (cash, int(cash * 0.05))
print(record,"\n")
Creditcard_record(current_creditcard, record)
else:
print("\33[31;0m信用卡没有足够的额度去支付提现,请去【我的信用卡】查看当前额度\33[0m\n")
else:
print("\33[31;0m提现额度不能为空\33[0m\n")
if if_adv == "b":
break '''转账'''
def Transfer(current_creditcard):
while True:
print("\33[32;0m转账\33[0m".center(40, "-"))
if_trans = input("\n\33[34;0m是否进行转账 确定【y】/返回【b】\33[0m:")
if if_trans == "y":
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
current_limit = creditcard_dict[current_creditcard]["limit"]
transfer_creditcard = input("\33[34;0m输入转账的银行卡卡号\33[0m:")
if transfer_creditcard.isdigit():
if len(transfer_creditcard) == 6:
if transfer_creditcard in creditcard_dict.keys():
again_creditcard = input("\33[34;0m再次确认转账的银行卡卡号\33[0m:")
if transfer_creditcard == again_creditcard:
transfer_cash = input("\33[34;0m输入转账的金额\33[0m:")
if transfer_cash.isdigit():
transfer_cash=int(transfer_cash)
if transfer_cash <= current_limit:
transfer_limit = creditcard_dict[current_creditcard]["limit"]
creditcard_dict[current_creditcard]["limit"] = current_limit-transfer_cash
creditcard_dict[transfer_creditcard]["limit"] = transfer_limit+transfer_cash
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
dict = json.dumps(creditcard_dict)
f_creditcard_dict.write(dict)
record="\33[31;1m转账卡号 %s 金额 ¥%s 转账成功\33[0m"%(transfer_creditcard,transfer_cash)
print(record,"\n")
Creditcard_record(current_creditcard, record)
else:
print("\33[31;0m金额不足 转账失败\33[0m\n")
else:
print("\33[31;0m输入金额有误\33[0m\n")
else:
print("\33[31;0m两次输入银行卡卡号不一致\33[0m\n")
else:
print("\33[31;0m输入银行卡不存在\33[0m\n")
else:
print("\33[31;0m输入银行卡有误\33[0m\n")
else:
print("\33[31;0m输入银行卡有误\33[0m\n")
if if_trans == "b":
break '''还款'''
def Repayment(current_creditcard):
while True:
print("\33[32;0m还款\33[0m".center(40, "-"))
if_repay = input("\n\33[34;0m是否进行还款 确定【y】/返回【b】\33[0m:")
if if_repay == "y":
repay_cash = input("\33[34;0m输入要还款的金额\33[0m:")
if repay_cash.isdigit():
repay_cash=int(repay_cash)
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
limit = creditcard_dict[current_creditcard]["limit"]
limit = limit+repay_cash
creditcard_dict[current_creditcard]["limit"]=limit
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
dict = json.dumps(creditcard_dict)
f_creditcard_dict.write(dict)
record="\33[31;1m信用卡 %s 还款金额 ¥%s 还款成功\33[0m" % (current_creditcard, repay_cash)
print(record,"\n")
Creditcard_record(current_creditcard, record)
else:
print("\33[31;0m输入金额格式有误\33[0m\n")
if if_repay == "b":
break '''查看信用卡流水'''
def Catcard_record(current_creditcard):
while True:
print("\33[32;0m信用卡流水单\33[0m".center(40, "-"))
with open(__db_creditcard_record, "r+") as f_creditcard_record:
f_creditcard_record.seek(0)
record_dict = json.loads(f_creditcard_record.read())
#print(record_dict)
print("\33[34;0m流水单日期\33[0m:")
if current_creditcard in record_dict.keys():
for key in record_dict[current_creditcard]:
print(key)
date = input("\n\33[34;0m流水单查询 返回【b】 / 输入流水单的日期【2000-01-01】\33[0m:")
if date == "b":
break
if date in record_dict[current_creditcard].keys():
keys = sorted(record_dict[current_creditcard][date])
print("\33[31;1m当前信用卡【%s】 交易记录-》》\33[0m"%(current_creditcard))
for key in keys:
print("\33[31;1m时间:%s %s\33[0m"%(key,record_dict[current_creditcard][date][key]))
print("")
else:
print("\33[31;0m输入的日期有误\33[0m\n")
else:
print("\33[31;0m信用卡 %s 还没有进行过消费\33[0m\n"%(current_creditcard))
break

creditcard.py

2.3.3 认证模块(authentication)
#__author__:小辉
#DATE:2018/7/4 import json
import os from ATM.log import rz BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) '''数据库文件相对路径'''
__db_users_dict = BASE_DIR + r"\db\users_dict"
__db_creditcard_dict = BASE_DIR + r"\db\creditcard_dict" '''认证装饰器'''
def auth(auth_type):#带参数的装饰器
def outer_wrapper(func):
if auth_type == "user_auth":
def wrapper():
res = func()
count=0
while True:
if count>2:
# new_logger= rz.loggers('account or password mistake at least three times')
new_logger=rz.loggers()
new_logger.warning('account or password mistake at least three times')
return False,username
username = input("\33[34;0m请输入用户名:\33[0m")
password = input("\33[34;0m请输入密码:\33[0m")
if len(username.strip()) > 0:
with open(__db_users_dict, "r+",encoding='utf8') as f_users_dict:#打开文件,可以不用f.close()语句 r+从头开始读,写的内容在文件内容之后
users_dict = json.loads(f_users_dict.read())
if username in users_dict.keys():# 查操作
if password == users_dict[username]["password"]:
if users_dict[username]["locked"] == 0:
print("\33[31;0m用户 %s 认证成功\33[0m"%(username))
return res,username
else:
print("\33[31;0m用户 %s 已经被锁定 认证失败\33[0m" % (username))
else:
print("\33[31;0m输入的密码不匹配 认证失败\33[0m")
else:
print("\33[31;0m输入的用户名不存在 认证失败\33[0m")
else:
print("\33[31;0m输入的用户名为空 认证失败\33[0m")
count+=1
return wrapper if auth_type == "creditcard_auth":
def wrapper():
res = func()
creditcard = input("\33[34;0m输入信用卡卡号(6位数字):\33[0m")
password = input("\33[34;0m输入信用卡的密码:\33[0m")
if len(creditcard.strip()) > 0:
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
if creditcard in creditcard_dict.keys():
if password == creditcard_dict[creditcard]["password"]:
if creditcard_dict[creditcard]["locked"] == 0:
print("\33[31;0m信用卡 %s 认证成功\33[0m" % (creditcard))
return res,creditcard
else:
print("\33[31;0m信用卡 %s 已经被冻结 认证失败\33[0m" % (creditcard))
else:
print("\33[31;0m输入的密码不匹配 认证失败\33[0m")
else:
print("\33[31;0m输入的信用卡卡号不存在 认证失败\33[0m")
else:
print("\33[31;0m输入的信用卡卡号为空 认证失败\33[0m")
return wrapper if auth_type == "admincenter_auth":
def wrapper():
res = func()
admincenter_dict ={"admin":"admin"}
username = input("\33[34;0m请输入管理用户名:\33[0m")
password = input("\33[34;0m请输入管理密码:\33[0m")
if len(username.strip()) > 0:
if username in admincenter_dict.keys():
if password == admincenter_dict[username]:
print("\33[31;0m管理用户 %s 认证成功\33[0m" % (username))
return res, username
else:
print("\33[31;0m输入的密码不匹配 认证失败\33[0m")
else:
print("\33[31;0m输入的用户名不存在 认证失败\33[0m")
else:
print("\33[31;0m输入的用户名为空 认证失败\33[0m") return wrapper
return outer_wrapper '''用户登录认证'''
@auth(auth_type="user_auth")
def user_auth():
print("\33[32;0m用户登录认证\33[0m".center(40,"-"))
return "True" '''信用卡认证'''
@auth(auth_type="creditcard_auth")
def creditcard_auth():
print("\33[32;0m信用卡登录认证\33[0m".center(40,"-"))
return "True" '''后台管理认证'''
@auth(auth_type="admincenter_auth")
def admincenter_auth():
print("\33[32;0m后台管理登录认证\33[0m".center(40,"-"))
return "True"

authentication.py

2.3.4 管理员模块(admincenter)
#__author__:小辉
#DATE:2018/7/4 import os,json BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) '''数据库文件相对路径'''
__db_users_dict = BASE_DIR + r"\db\users_dict"
__db_creditcard_dict = BASE_DIR + r"\db\creditcard_dict" '''创建用户'''
def User_create(address="None",locked=0,creditcard=0):
while True:
print("开始创建用户".center(50,"-"))
with open(__db_users_dict, "r+") as f_users_dict:
users_dict = json.loads(f_users_dict.read())
for key in users_dict:
print("系统已有用户 【%s】" % (key))
if_create = input("\n\33[34;0m是否创建新的用户 确定【y】/返回【b】\33[0m:")
if if_create == "y":
username = input("\33[34;0m输入要添加账户的用户名:\33[0m")
password = input("\33[34;0m输入添加账户的密码:\33[0m")
if username not in users_dict.keys():
if len(username.strip()) > 0:
if len(password.strip()) > 0:
users_dict[username] = {"username":username,"password":password,"creditcard":creditcard,"address":address,
"locked":locked}
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("\33[31;1m创建用户 %s 成功\33[0m\n"%(username))
else:
print("\33[31;0m输入的密码为空\33[0m\n")
else:
print("\33[31;0m输入的用户名为空\33[0m\n")
else:
print("\33[31;0m用户名 %s 已经存在\33[0m\n"%(username))
if if_create == "b":
break '''发行信用卡'''
def Creditcard_create(limit=15000,locked=0):
while True:
print("发行信用卡".center(50, "-"))
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
for key in creditcard_dict:
print("系统已有信用卡 【%s】 \t持卡人 【%s】" % (key,creditcard_dict[key]["personinfo"]))
if_create = input("\n\33[34;0m是否发行新的信用卡 确定【y】/返回【b】\33[0m:")
if if_create == "y":
creditcard = input("\33[34;0m输入要发行信用卡卡号(6位数字):\33[0m")
if creditcard not in creditcard_dict.keys():
if creditcard.isdigit() and len(creditcard) == 6:
password = input("\33[34;0m输入要发行信用卡的密码:\33[0m")
if len(password.strip()) > 0:
personinfo = input("\33[34;0m输入要发行信用卡申请人:\33[0m")
if len(personinfo.strip()) > 0:
creditcard_dict[creditcard] = {"creditcard":creditcard, "password":password, "personinfo":personinfo,
"limit":limit,"limitcash":limit//2,"locked":locked,"deflimit":limit}
dict = json.dumps(creditcard_dict)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
print("\33[31;0m发行信用卡 %s 成功 额度 %s\33[0m\n"%(creditcard,limit))
else:
print("\33[31;0m信用卡申请人不能为空\33[0m\n")
else:
print("\33[31;0m输入的密码为空\33[0m\n")
else:
print("\33[31;0m信用卡 %s 卡号不符合规范\33[0m\n" % (creditcard))
else:
print("\33[31;0m信用卡 %s 已经存在\33[0m\n" % (creditcard))
if if_create == "b":
break '''锁定用户'''
def Lock_user():
while True:
print("\33[32;0m锁定用户\33[0m".center(50, "-"))
with open(__db_users_dict, "r+") as f_users_dict:
users_dict = json.loads(f_users_dict.read())
for key in users_dict:
if users_dict[key]["locked"] == 0:
print("系统用户 【%s】\t\t锁定状态:【未锁定】"%(key))
else:
print("系统用户 【%s】\t\t锁定状态:\33[7m【已锁定】\33[0m" % (key))
if_lock = input("\n\33[34;0m是否进行用户锁定 确定【y】/返回【b】\33[0m:")
if if_lock == "y":
lock_user = input("\33[34;0m输入要锁定的用户名\33[0m:")
if lock_user in users_dict.keys():
if users_dict[lock_user]["locked"] == 0:
users_dict[lock_user]["locked"] = 1
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("\33[31;1m用户 %s 锁定成功\33[0m\n" % (lock_user))
else:
print("\33[31;0m用户 %s 锁定失败 之前已经被锁定\33[0m\n" % (lock_user))
else:
print("\33[31;0m用户 %s 不存在\33[0m\n"%(lock_user))
if if_lock == "b":
break '''解锁用户'''
def Unlock_user():
while True:
print("\33[32;0m解锁用户\33[0m".center(50, "-"))
with open(__db_users_dict, "r+") as f_users_dict:
users_dict = json.loads(f_users_dict.read())
for key in users_dict:
if users_dict[key]["locked"] == 0:
print("系统用户 【%s】\t\t锁定状态:【未锁定】" % (key))
else:
print("系统用户 【%s】\t\t锁定状态:\33[7m【已锁定】\33[0m" % (key))
if_lock = input("\n\33[34;0m是否进行用户解锁 确定【y】/返回【b】\33[0m:")
if if_lock == "y":
unlock_user = input("\33[34;0m输入要解锁的用户名\33[0m:")
if unlock_user in users_dict.keys():
if users_dict[unlock_user]["locked"] == 1:
users_dict[unlock_user]["locked"] = 0
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("\33[31;1m用户 %s 解锁成功\33[0m\n" % (unlock_user))
else:
print("\33[31;0m用户 %s 解锁失败 用户未被锁定\33[0m\n" % (unlock_user))
else:
print("\33[31;0m用户 %s 不存在\33[0m\n"%(unlock_user))
if if_lock == "b":
break '''冻结信用卡'''
def Lock_creditcard():
while True:
print("\33[32;0m冻结信用卡\33[0m".center(50, "-"))
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
for key in creditcard_dict:
if creditcard_dict[key]["locked"] == 0:
print("信用卡 【%s】\t\t冻结状态:【未冻结】" % (key))
else:
print("信用卡 【%s】\t\t冻结状态:\33[7m【已冻结】\33[0m" % (key))
if_Unlock = input("\n\33[34;0m是否进行信用卡冻结 确定【y】/返回【b】\33[0m:")
if if_Unlock == "y":
creditcard = input("\33[34;0m输入要冻结的信用卡卡号\33[0m:")
if creditcard in creditcard_dict.keys():
if creditcard_dict[creditcard]["locked"] == 0:
creditcard_dict[creditcard]["locked"] = 1
dict = json.dumps(creditcard_dict)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
print("\33[31;1m信用卡 %s 冻结成功\33[0m\n" % (creditcard))
else:
print("\33[31;0m信用卡 %s 冻结失败 之前已经被冻结\33[0m\n" % (creditcard))
else:
print("\33[31;0m信用卡 %s 不存在\33[0m\n" %(creditcard))
if if_Unlock == "b":
break '''解冻信用卡'''
def Unlock_creditcard():
while True:
print("\33[32;0m解冻信用卡\33[0m".center(50, "-"))
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
for key in creditcard_dict:
if creditcard_dict[key]["locked"] == 0:
print("信用卡 【%s】\t\t冻结状态:【未冻结】" % (key))
else:
print("信用卡 【%s】\t\t冻结状态:\33[7m【已冻结】\33[0m" % (key))
if_Unlock = input("\n\33[34;0m是否进行信用卡解冻 确定【y】/返回【b】\33[0m:")
if if_Unlock == "y":
creditcard = input("\33[34;0m输入要解冻的信用卡卡号\33[0m:")
if creditcard in creditcard_dict.keys():
if creditcard_dict[creditcard]["locked"] == 1:
creditcard_dict[creditcard]["locked"] = 0
dict = json.dumps(creditcard_dict)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
print("\33[31;1m信用卡 %s 解冻成功\33[0m\n" % (creditcard))
else:
print("\33[31;0m信用卡 %s 解冻失败 之前未被冻结\33[0m\n" % (creditcard))
else:
print("\33[31;0m信用卡 %s 不存在\33[0m\n" % (creditcard))
if if_Unlock == "b":
break '''修改信用卡额度'''
def Updata_limit():
while True:
print("\33[32;0m修改信用卡额度\33[0m".center(70, "-"))
with open(__db_creditcard_dict, "r+") as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
for key in creditcard_dict:
limitcash = creditcard_dict[key]["limitcash"]
print("信用卡 【%s】\t目前可用额度:【¥%s】\t取现额度:【¥%s】" %
(key,creditcard_dict[key]["limit"],limitcash))
if_Updata = input("\n\33[34;0m是否进行信用卡额度调整 确定【y】/返回【b】\33[0m:")
if if_Updata == "y":
creditcard = input("\33[34;0m输入要修改额度的信用卡卡号\33[0m:")
if creditcard in creditcard_dict.keys():
limit = input("\33[34;0m输入额度修改后的金额(至少¥5000)\33[0m:")
if limit.isdigit():
limit_default = creditcard_dict[creditcard]["deflimit"]
limit = int(limit)
if limit >=5000:
updata = limit - limit_default
creditcard_dict[creditcard]["limit"] +=updata
creditcard_dict[creditcard]["limitcash"] += updata//2
creditcard_dict[creditcard]["deflimit"]=limit
dict = json.dumps(creditcard_dict)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
print("\33[31;1m信用卡 %s 额度修改成功 额度 %s \33[0m\n" % (creditcard,limit))
else:
print("\33[31;0m输入金额 ¥%s 小于¥5000\33[0m\n" % (limit))
else:
print("\33[31;0m输入金额 ¥%s 格式错误\33[0m\n" % (limit))
else:
print("\33[31;0m信用卡 【%s】 不存在\33[0m\n" % (creditcard))
if if_Updata == "b":
break

admincenter.py

2.4 日志模块(log)

#__author__:小辉
#DATE:2018/7/5
#__author__:小辉
#DATE:2018/7/5 import os,logging
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
__db_logger_dict=BASE_DIR+r"\db\logger_dict" def loggers():
logger = logging.getLogger()
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler(__db_logger_dict)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger

rz.py

2.5 db数据库(db)

2.5.1 用户数据库(user_dict)
{"zhaosi": {"password": "", "locked": 0, "username": "zhaosi", "creditcard": 0, "address": "None"}, "wangwu": {"password": "", "locked": 0, "username": "wangwu", "creditcard": 0, "address": "None"}, "laoqiqi": {"password": "", "locked": 1, "username": "laoqiqi", "creditcard": 0, "address": "None"}, "Terry": {"password": "", "locked": 0, "username": "Terry", "creditcard": 0, "address": "None"}, "zhangsan": {"password": "", "locked": 0, "username": "zhangsan", "creditcard": "", "address": "\u4e2d\u539f\u5de5\u5b66\u9662"}}

user_dict

2.5.2 信用卡数据库(credictcard_dict)
{"": {"locked": 0, "deflimit": 15000, "personinfo": "\u5218\u96e8\u6674", "creditcard": "", "password": "", "limit": 15000, "limitcash": 7500}, "": {"locked": 0, "deflimit": 15000, "personinfo": "\u5929\u68da\u5143\u5e05", "creditcard": "", "password": "", "limit": 16288, "limitcash": 7500}, "": {"locked": 1, "deflimit": 15000, "personinfo": "\u94f6\u89d2\u5927\u738b", "creditcard": "", "password": "", "limit": 13696, "limitcash": 7500}, "": {"locked": 0, "deflimit": 20000, "personinfo": "\u5218\u5c0f\u8f89", "creditcard": "", "password": "", "limit": 13596, "limitcash": 7480}}

credictcard_dict

2.5.3 商品信息数据库(product_list)
iPhone    1299
iWatch 2999
MacBo 1999
iPad 2199
Bicyc 999
X-box 1199
Letv 819
Book 599

product_list

2.5.4 购物车数据库(shopping_car)
[]

shopping_car

2.5.5 取款注意事项(detail_tip)
信用卡取现需知-》》
①信用卡默认取现额度为信用卡额度的50%(此系统默认取现额度为7500)
②信用卡进行消费时(未取现),取现额度不会变化
③信用卡额度低于取现额度时,可取现金额以当前额度为准(可取现金额=当前额度*0.95)
④取现额度只有两种状况会发生变化,一种进行取现,第二是对信用卡额度进行调整
⑤每次取现扣除5%的手续费

detail_tip

2.5.6 日志库(logger_dict)
2018-07-05 10:53:09,269 - root - WARNING - account or password mistake at least three times
2018-07-05 11:01:34,437 - root - WARNING - account or password mistake at least three times
2018-07-05 11:03:49,550 - root - WARNING - account or password mistake at least three times
2018-07-05 11:10:16,636 - root - WARNING - account or password mistake at least three times

logger_dict

2.5.7 信用卡变动记录库(credictcard_record)
{"": {"2018-07-04": {"21:39:28": "\u001b[31;1m\u4fe1\u7528\u5361 888888 \u8fd8\u6b3e\u91d1\u989d \uffe5100 \u8fd8\u6b3e\u6210\u529f\u001b[0m", "21:32:03": "\u001b[31;1m\u63d0\u73b0\uffe5100 \u624b\u7eed\u8d39\uffe55\u001b[0m", "20:52:16": "\u8d2d\u7269\u652f\u4ed8 1299", "21:37:57": "\u001b[31;1m\u8f6c\u8d26\u5361\u53f7 222222 \u91d1\u989d \uffe5100 \u8f6c\u8d26\u6210\u529f\u001b[0m"}}}

credictcard_record

2.5.8 购物记录库(shopping_record)
{"zhangsan": {"2018-07-04": {"20:52:16": [["iPhone", ""]], "20:49:52": [["iPhone", ""]]}}}

shopping_record

2.6 配置模块(conf模块)

待扩展

settings.py

三 运行结果

运行atm.py文件,首先显示如下界面:

python---信用卡ATM-LMLPHP

3.1 购物中心

python---信用卡ATM-LMLPHP

3.1.1 购物商场

python---信用卡ATM-LMLPHP

3.1.2 查看购物车

python---信用卡ATM-LMLPHP

3.1.3 购物结算

python---信用卡ATM-LMLPHP

3.1..4 个人中心

python---信用卡ATM-LMLPHP

(1) 购物历史记录

python---信用卡ATM-LMLPHP

(2)修改登录密码

python---信用卡ATM-LMLPHP

(3)修改个人信息

python---信用卡ATM-LMLPHP

(4)修改银行卡绑定

python---信用卡ATM-LMLPHP

3.2 信用卡中心

python---信用卡ATM-LMLPHP

3.2.1 我的信用卡

python---信用卡ATM-LMLPHP

3.2.2 提现

python---信用卡ATM-LMLPHP

3.2.3 转账

python---信用卡ATM-LMLPHP

3.2.4 还款

python---信用卡ATM-LMLPHP

3.2.5 流水记录

python---信用卡ATM-LMLPHP

3.3 后台管理

python---信用卡ATM-LMLPHP

3.3.1 创建账号

python---信用卡ATM-LMLPHP

3.2.2 锁定账户

python---信用卡ATM-LMLPHP

3.2.3 解锁账户

python---信用卡ATM-LMLPHP

3.2.4 发行信用卡

python---信用卡ATM-LMLPHP

3.2.5 冻结信用卡

python---信用卡ATM-LMLPHP

3.2.6 解冻信用卡

python---信用卡ATM-LMLPHP

3.2.7 提升信用卡额度

python---信用卡ATM-LMLPHP

3.3 退出程序

python---信用卡ATM-LMLPHP

参考:https://www.cnblogs.com/lianzhilei/p/5786223.html

05-26 11:27