程序需求
程序思维导图
Readme.md
作者:Replaceroot
版本:示例版本 v0.1
程序介绍:
实现ATM常用功能
功能全部用python的基础知识实现,用到time\os\sys\json\open\loggin\函数\模块知识.
功能介绍:
模拟实现一个ATM + 购物商城程序
额度 15000或自定义
实现购物商城,买东西加入 购物车,调用信用卡接口结账
可以提现,手续费5%
支持多账户登录
支持账户间转账
记录每月日常消费流水
提供还款接口
ATM记录操作日志
提供管理接口,包括添加账户、提升账户额度,冻结账户等
用户认证用装饰器
程序结构:
atm_project/
├── README
├── atm #ATM主程目录
│ ├── __init__.py
│ ├── bin #ATM 执行文件 目录
│ │ ├── __init__.py
│ │ ├── atm.py #ATM 执行程序
│ ├── conf #配置文件
│ │ ├── __init__.py
│ ├── modules #主要程序逻辑都在这个目录
│ │ ├── __init__.py
│ │ ├── admincenter.py #后台管理
│ │ ├── authentication.py #用户认证模块
│ │ ├── creditcard.py #信用卡模块
│ │ ├── shopping.py #购物车模块
│ ├── db #用户数据存储的地方
│ │ ├── __init__.py
│ │ ├── admincenter_dict #存储后台管理员账号密码的文件
│ │ └── creditcard_dict #存储信用卡信息的文件
│ │ ├── creditcard_record #存储信用卡消费记录
│ │ ├── details #提现须知文件
│ │ ├── product_list #存储商品列表
│ │ ├── shopping_car #存储购物车信息
│ │ ├── shopping_record #存储购物记录信息
│ │ ├── users_dict #存储用户账号信息
bin/atm.py
#!/usr/bin/env python
#_*_coding:utf-8_*_
__author__ = 'replaceroot'
import sys,os
'''设置路径为my_atm'''
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from modules import authentication,shopping,creditcard,admincenter
while True:
print("\33[35;1m欢迎进入爱购商城\33[0m".center(50,'*'),
"\n1 购物中心\n"
"2 信用卡中心\n"
"3 后台管理\n"
"q 退出程序\n")
choice_id = input("请选择ID:").strip()
if choice_id == '1':
res = authentication.user_auth()
print(res)
if res != None:
if res[0] == 'True':
current_user = res[1]
shopping.Empty_shopping_car()
while True:
print("\33[35;1m欢迎进入购物中心\33[0m"
"\n1 购物商城\n"
"2 查看购物车\n"
"3 购物结算\n"
"4 个人中心\n"
"\33[32;0mb 返回\33[0m\n")
choice_id = input("请选择: ").strip()
if choice_id.isdigit():
choice_id = int(choice_id)
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[35;0m个人中心\33[0m".center(50,'-'),
"\n1 购物历史记录\n"
"2 修改登陆密码\n"
"3 修改个人信息\n"
"4 修改信用卡绑定\n"
"b 返回")
choice_id = input("请选择要进入模式的ID:")
if choice_id.isdigit():
if choice_id == '1':
shopping.Shopping_card_view(current_user)
elif choice_id == '2':
shopping.Change_password(current_user)
elif choice_id == '3':
shopping.Change_address(current_user)
elif choice_id == '4':
shopping.Change_creditcard(current_user)
else:
print("\33[31;0m请输入正确的模式ID!\33[0m")
elif choice_id == 'b':
break
else:
print("\33[31;0m请输入正确的模式ID!\33[0m")
elif choice_id == 'b':
break
elif choice_id == '2':
res = authentication.creditcard_auth()
print(res)
if res != None:
if res[0] == "True":
current_creditcard = res[1]
while True:
print("\33[35;1m信用卡中心\33[0m".center(50,'-'),
"\n1 我的信用卡\n"
"2 提现\n"
"3 转账\n"
"4 还款\n"
"5 流水记录\n"
"b 返回")
choice_id = input("请选择要进入模式的ID:").strip()
if choice_id.isdigit():
choice_id = int(choice_id)
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.View_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[35;1m后台管理中心\33[0m".center(50,'-'),
"\n1 创建账号\n"
"2 锁定账号\n"
"3 解锁账号\n"
"4 发行信用卡\n"
"5 冻结卡号\n"
"6 解冻卡号\n"
"7 调整信用卡额度\n"
"b 返回上一级\n")
choice_id = input("选择要进入模式的ID:")
if choice_id.isdigit():
choice_id = int(choice_id)
if choice_id == 1:
admincenter.Account_create()
elif choice_id == 2:
admincenter.Account_lock()
elif choice_id == 3:
admincenter.Account_unlock()
elif choice_id == 4:
admincenter.Creditcard_create()
elif choice_id == 5:
admincenter.Creditcard_lock()
elif choice_id == 6:
admincenter.Creditcard_unlock()
elif choice_id == 7:
admincenter.Limit_update()
else:
print("\33[31;0m请输入正确的ID!\33[0m")
elif choice_id == 'b':
break
elif choice_id == 'q':
break
modules/admincenter.py
#!/usr/bin/env python
#_*_coding:utf-8_*_
__author__ = 'replaceroot'
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_shopping_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_creditcard_record = BASE_DIR + r"\db\creditcard_record" #信用卡消费记录文件
__db_shopping_record = BASE_DIR + r"\db\shopping_record" #购物记录文件
__db_details = BASE_DIR + r"\db\details" #信用卡提现提示
'''创建账号'''
def Account_create(address='None', locked=0, creditcard="0"):
while True:
print("\33[35;1m创建用户\33[0m".center(50,'-'))
choice = input("是否要创建用户? 是[y],返回[b]")
if choice == 'y':
with open(__db_users_dict,'r+') as f_users_dict:
users_dict = json.loads(f_users_dict.read())
username = input("用户名:").strip()
password = input("密码:").strip()
pwd_again = input("再次输入密码:").strip()
if username in users_dict.keys():
print("用户名\33[31;1m%s\33[0m已被注册!")
else:
if len(username.strip()) > 0 and len(password.strip()) > 0:
if password == pwd_again:
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[32;1m%s\33[0m,密码:\33[32;1m%s\33[0m" %(username,password))
else:
print("\33[31;0m两次密码不匹配!\33[0m")
else:
print("\33[31;0m用户名或密码不能为空!\33[0m")
elif choice == 'b':
break
"""锁定账号"""
def Account_lock():
while True:
print("\33[35;1m锁定账号\33[0m".center(50,'-'))
choice = input("是否要锁定账号? 是[y],否[b]")
if choice == 'y':
with open(__db_users_dict,'r+') as f_users_dict:
users_dict = json.loads(f_users_dict.read())
username = input("请输入需要锁定的用户名:")
if len(username) > 0:
if username in users_dict.keys():
if users_dict[username]["locked"] == 0:
users_dict[username]["locked"] = 1
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
elif users_dict[username]["locked"] == 1:
print("\33[31;1m此用户已被锁定!\33[0m")
else:
print("\33[32;1m用户名不存在!\33[0m")
else:
print("\33[31;1m用户名不能为空!\33[0m")
elif choice == 'b':
break
else:
print("\33[31;1m兄弟,你的输入有误!\33[0m")
'''解锁账号'''
def Account_unlock():
'''此函数用于解锁账号'''
while True:
print("\33[35;1m解锁账号\33[0m".center(50,'-'))
choice = input("是否要解锁账号? 是[y],否[b]")
if choice == 'y':
with open(__db_users_dict,'r+') as f_users_dict:
users_dict = json.loads(f_users_dict.read())
username = input("请输入要解锁的用户名: ")
if len(username) > 0:
if username in users_dict.keys():
if users_dict[username]["locked"] == 0:
print("\33[31;1m账号为锁定,无法解锁.\33[0m")
elif users_dict[username]["locked"] == 1:
users_dict[username]["locked"] = 0
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("账号\33[32;1m%s\33[0m解锁成功." %username)
else:
print("\33[31;1m您输入的用户名不存在.\33[0m")
else:
print("\33[31;1m用户名不能为空!\33[0m")
elif choice == 'b':
break
'''发行信用卡'''
def Creditcard_create(limit=15000, limitcash=7500, deflimit=15000, locked=0):
'''此函数用于发行信用卡'''
while True:
print("\33[35;1m发行信用卡\33[0m".center(50,'-'))
choice = input("是否要发行信用卡? 是[y],否[b]")
if choice == 'b':
break
elif choice == 'y':
with open(__db_creditcard_dict,'r+') as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
creditcard_id = input("请输入信用卡号(6位数字):").strip()
if creditcard_id.isdigit():
if len(creditcard_id) > 0 and len(creditcard_id) == 6:
if creditcard_id not in creditcard_dict.keys():
password = input("请设置信用卡密码: ")
pwd_again = input("请再次输入信用卡密码: ")
if len(password) > 0:
if password == pwd_again:
personinfo = input("请输入持卡人信息: ")
if len(personinfo) > 0:
creditcard_dict[creditcard_id] = {"personinfo":personinfo, "limit":limit,
"limitcash":limitcash, "deflimit":deflimit, "locked":locked, "creditcard":creditcard_id}
dict = json.dumps(creditcard_dict)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
print("发行信用卡成功,新的卡号为:\33[32;1m%s\33[0m,密码: \33[32;1m%s\33[0m,请到个人中心进行绑定!" %(creditcard_id, password))
else:
print("\33[31;1m持卡人信息不能为空.\33[0m")
else:
print("\33[31;1m两次密码不匹配!\33[0m")
else:
print("\33[31;1mPassword不能为空!\33[0m")
else:
print("\33[31;1m您输入的卡号已存在!\33[0m")
else:
print("\33[31;1m请输入6位数字的卡号!\33[0m")
else:
print("\33[31;1m请输入6位数字的卡号!\33[0m")
else:
print("\33[31;1m您的输入有误!\33[0m")
'''冻结信用卡'''
def Creditcard_lock():
'''此函数用于冻结信用卡'''
while True:
print("\33[35;1m冻结信用卡\33[0m".center(50,'-'))
choice = input("是否冻结信用卡? 是[y],否[b]")
if choice == 'b':
break
elif choice == 'y':
with open(__db_creditcard_dict, 'r+') as f_creditcard_dict:
creditcard = json.loads(f_creditcard_dict.read())
card_id = input("请输入要冻结卡号的ID(6位数字): ")
if card_id.isdigit():
if len(card_id) == 6:
if card_id in creditcard.keys():
if creditcard[card_id]["locked"] == 0:
creditcard[card_id]["locked"] = 1
dict = json.dumps(creditcard)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
print("卡号\33[32;1m%s\33[0m已成功被冻结!" %card_id)
elif creditcard[card_id]["locked"] == 1:
print("卡号\33[31;1m%s\33[0m已被冻结,无法再次冻结.")
else:
print("\33[31;1m您输入的卡号不存在\33[0m")
else:
print("\33[31;1m您输入的卡号有误!\33[0m")
else:
print("\33[31;1m您输入的卡号有误!\33[0m")
else:
print("\33[31;1m您的输入有误!\33[0m")
'''信用卡解冻'''
def Creditcard_unlock():
'''此函数用于信用卡解冻'''
while True:
print("\33[35;1m信用卡解冻\33[0m".center(50,'-'))
choice = input("是否要解冻信用卡? 是[y],否[b]")
if choice == 'b':
break
elif choice == 'y':
with open(__db_creditcard_dict, 'r+') as f_creditcard_dict:
creditcard = json.loads(f_creditcard_dict.read())
card_id = input("请输入要解冻的卡号(6为数字):")
if card_id.isdigit() and len(card_id) == 6:
if card_id in creditcard.keys():
if creditcard[card_id]["locked"] == 0:
print("\33[31;1m此卡号未被冻结,无法解冻!\33[0m")
elif creditcard[card_id]["locked"] == 1:
creditcard[card_id]["locked"] = 0
dict = json.dumps(creditcard)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
print("卡号:\33[32;1m%s\33[0m解冻成功!" %card_id)
else:
print("\33[31;1m要解冻的卡号不存在!\33[0m")
else:
print("\33[31;1m请输入正确的卡号!\33[0m")
else:
print("\33[31;1m您的输入有误!\33[0m")
'''调整信用卡额度'''
def Limit_update():
'''此函数用于调整信用卡额度'''
while True:
print("\33[35;1m调整信用卡额度\33[0m".center(50,'-'))
choice = input("是否要调整信用卡额度? 是[y],否[b]")
if choice == 'b':
break
elif choice == 'y':
with open(__db_creditcard_dict, 'r+') as f_creditcard_dict:
creditcard = json.loads(f_creditcard_dict.read())
card_id = input("请输入需要调整额度的信用卡号: ")
if card_id.isdigit() and len(card_id) == 6:
if card_id in creditcard.keys():
cash = input("请输入调整后的额度: ")
if cash.isdigit():
cash = int(cash)
if cash > 5000:
creditcard[card_id]["limit"] = cash
creditcard[card_id]["limitcash"] = int(cash/2)
dict = json.dumps(creditcard)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
print("\33[32;1m%s\33[0m卡号调整后的额度为:\33[32;1m%s\33[0m,可提现额度为:\33[32;1m%s\33[0m" %(card_id, cash, (cash/2)))
else:
print("\33[31;1m调整后的额度必须大于5000.\33[0m")
else:
print("\33[31;1m您的输入有误!\33[0m")
else:
print("\33[31;1m您输入的卡号不存在!\33[0m")
else:
print("\33[31;1m请输入正确的卡号!\33[0m")
else:
print("\33[31;1m您的输入有误!\33[0m")
modules/authentication.py
#!/usr/bin/env python
#_*_coding:utf-8_*_
__author__ = 'replaceroot'
import os,json
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)
__db_users_dict = BASE_DIR + r"\db\users_dict"
__db_creditcard_dict = BASE_DIR + r"\db\creditcard_dict"
__db_admincenter_dict = BASE_DIR + r"\db\admincenter_dict"
'''认证装饰器'''
def auth(auth_type):
def outer_wrapper(func) :
if auth_type == 'user_auth':
def wrapper():
res = func() #调用函数并赋值给res变量
username = input("\33[34;0m请输入用户名: \33[0m").strip()
password = input("\33[34;0m请输入密码: \33[0m").strip()
if len(username) > 0:
with open(__db_users_dict,'r+') as f_users_dict:
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[32;1m用户%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")
else:
print("\33[31;0m用户名不能为空,认证失败.\33[0m")
return wrapper
if auth_type == 'creditcard_auth':
def wrapper():
res = func()
creditcard = input("\33[34;0m请输入信用卡号:\33[0m").strip()
password = input("\33[34;0m请输入信用卡密码:\33[0m").strip()
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[32;1m信用卡%s认证成功\33[0m" %creditcard)
return res,creditcard
else:
print("\33[31;0m信用卡已被锁定,认证失败\33[0m")
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()
username = input("\33[34;0m请输入用户名:\33[0m").strip()
password = input("\33[34;0m请输入密码:\33[0m").strip()
if len(username.strip()) > 0:
with open(__db_admincenter_dict,'r+') as f_admincenter_dict:
admincenter_dict = json.loads(f_admincenter_dict.read()) #反序列化操作并赋值给admincenter_dict变量
if username in admincenter_dict.keys():
if password == admincenter_dict[username]["password"]:
print("\33[32;1m管理员%s认证成功\033[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;1m用户登陆认证\33[0m".center(50,'-'))
return "True"
'''信用卡登陆认证'''
@auth(auth_type="creditcard_auth")
def creditcard_auth():
print("\33[32;1m信用卡登陆认证\33[0m".center(50,'-'))
return "True"
'''后台登陆认证'''
@auth(auth_type="admincenter_auth")
def admincenter_auth():
print("\33[32;1m后台登陆认证\33[0m".center(50,'-'))
return "True"
modules/authentication.py
#!/usr/bin/env python
#_*_coding:utf-8_*_
__author__ = 'replaceroot'
import os,json
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)
__db_users_dict = BASE_DIR + r"\db\users_dict"
__db_creditcard_dict = BASE_DIR + r"\db\creditcard_dict"
__db_admincenter_dict = BASE_DIR + r"\db\admincenter_dict"
'''认证装饰器'''
def auth(auth_type):
def outer_wrapper(func) :
if auth_type == 'user_auth':
def wrapper():
res = func() #调用函数并赋值给res变量
username = input("\33[34;0m请输入用户名: \33[0m").strip()
password = input("\33[34;0m请输入密码: \33[0m").strip()
if len(username) > 0:
with open(__db_users_dict,'r+') as f_users_dict:
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[32;1m用户%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")
else:
print("\33[31;0m用户名不能为空,认证失败.\33[0m")
return wrapper
if auth_type == 'creditcard_auth':
def wrapper():
res = func()
creditcard = input("\33[34;0m请输入信用卡号:\33[0m").strip()
password = input("\33[34;0m请输入信用卡密码:\33[0m").strip()
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[32;1m信用卡%s认证成功\33[0m" %creditcard)
return res,creditcard
else:
print("\33[31;0m信用卡已被锁定,认证失败\33[0m")
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()
username = input("\33[34;0m请输入用户名:\33[0m").strip()
password = input("\33[34;0m请输入密码:\33[0m").strip()
if len(username.strip()) > 0:
with open(__db_admincenter_dict,'r+') as f_admincenter_dict:
admincenter_dict = json.loads(f_admincenter_dict.read()) #反序列化操作并赋值给admincenter_dict变量
if username in admincenter_dict.keys():
if password == admincenter_dict[username]["password"]:
print("\33[32;1m管理员%s认证成功\033[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;1m用户登陆认证\33[0m".center(50,'-'))
return "True"
'''信用卡登陆认证'''
@auth(auth_type="creditcard_auth")
def creditcard_auth():
print("\33[32;1m信用卡登陆认证\33[0m".center(50,'-'))
return "True"
'''后台登陆认证'''
@auth(auth_type="admincenter_auth")
def admincenter_auth():
print("\33[32;1m后台登陆认证\33[0m".center(50,'-'))
return "True"
modules/creditcard.py
#!/usr/bin/env python
#_*_coding:utf-8_*_
__author__ = 'replaceroot'
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_shopping_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_creditcard_record = BASE_DIR + r"\db\creditcard_record" #信用卡消费记录文件
__db_shopping_record = BASE_DIR + r"\db\shopping_record" #购物记录文件
__db_details = BASE_DIR + r"\db\details" #信用卡提现提示
'''我的信用卡'''
def My_creditcard(current_creditcard):
while True:
with open(__db_creditcard_dict,'r+') as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
print("卡号:\33[32;1m[%s]\33[0m,额度:\33[32;1m[$%s]\33[32;1m,提现额度:[$%s]\33[0m,持卡人:\33[32;1m[%s]\33[0m" %(current_creditcard,
creditcard_dict[current_creditcard]["limit"], creditcard_dict[current_creditcard]["limitcash"],
creditcard_dict[current_creditcard]["personinfo"]))
if_back = input("\33[35;0m返回请输入b:\33[0m")
if if_back == 'b':
break
'''提现须知'''
def Details():
with open(__db_details,'r',encoding='utf-8') as f_details:
print(f_details.read())
'''信用卡流水'''
def Creditcard_record(current_creditcard,record):
with open(__db_creditcard_record,'r+') as f_creditcard:
creditcard_record = json.loads(f_creditcard.read())
month = time.strftime("%Y-%m-%d",time.localtime())
times = time.strftime("%H-%M-%S")
if current_creditcard not in creditcard_record.keys():
creditcard_record[current_creditcard] = {month:{times:record}}
else:
if month not in creditcard_record[current_creditcard]:
creditcard_record[current_creditcard][month] = {times:record}
else:
creditcard_record[current_creditcard][month][times] = record
dict = json.dumps(creditcard_record)
f_creditcard.seek(0)
f_creditcard.truncate(0)
f_creditcard.write(dict)
'''提现'''
def Cash_advance(current_creditcard):
while True:
print("\33[35;0m提现\33[0m".center(50,'-'))
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_cash = creditcard_dict[current_creditcard]["limitcash"] #可提现额度,为总额度的50%
print("信用卡号:\33[32;0m%s\33[0m\n可提现额度:\33[32;0m%s\33[0m" %(current_creditcard,limit_cash))
if limit >= limit_cash: #总额度大于可提现额度
Details()
if_advance = input("\33[32;0m请确认是否提现: 确认[y],取消[b]\33[0m")
if if_advance == 'b':
break
elif if_advance == 'y':
cash = input("请输入要提现的金额,收取5%手续费:")
if cash.isdigit():
cash = int(cash)
if cash != 0:
if cash <= limit_cash:
limit_cash = limit_cash - int(cash * 1.05) #可提现额度
limit = limit - int(cash * 1.05) #总额度
creditcard_dict[current_creditcard]["limit_cash"] = limit_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 = "提现:$%s,手续费$%s" %(cash, cash*0.05)
print(record,'\n')
#Creditcard_record(current_creditcard,record)
else:
print("\33[31;0m超出信用卡提现额度\33[0m")
else:
print("\33[31;0m提现金额不能为空\33[0m")
else:
print("\33[31;0m请输入正确的提现金额\33[0m")
else:
print("\33[31;0m您的输入有误,请根据提示重新输入!\33[0m")
if limit < limit_cash:
print("可提现金额:\33[32;1m$%s\33[0m" %(int(limit*0.95)))
Details()
if_advance = input("是否进行提现: 是[y],否[b]:")
if if_advance == 'b':
break
elif if_advance == 'y':
cash = input("请输入提现金额:")
if cash.isdigit():
cash = int(cash)
if cash != 0:
if cash <= int(limit*0.95):
limit = limit - int(cash * 1.05)
limit_cash = limit_cash - int(cash * 1.05)
creditcard_dict[current_creditcard]["limit"] = limit
creditcard_dict[current_creditcard]["limit_cash"] = limit_cash
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
dict = json.dumps(creditcard_dict)
f_creditcard_dict.write(dict)
record = "\33[32;0m提现:$[%s],手续费:$[%s]" %(cash, int(cash*0.05))
print(record,'\n')
Creditcard_record(current_creditcard, record)
else:
print("\33[31;0m信用卡没有足够的额度去支付提现,请去【我的信用卡】查看当前额度\33[0m")
else:
print("\33[31;0m提现额度不能为空\33[0m")
else:
print("\33[31;0m您的输入有误!\33[0m")
else:
print("\33[31;0m您的输入有误!\33[0m")
'''转账'''
def Transfer(current_creditcard):
'''此函数用于转账'''
while True:
print("\33[35;1m转账\33[0m".center(50,'-'))
choice = input("是否要进行转账? 是[y],否[b]")
if choice == 'b':
break
elif choice == 'y':
Details()
with open(__db_creditcard_dict,'r+') as f_creditcard_dict:
creditcard = json.loads(f_creditcard_dict.read())
card_id = input("请输入要转账的卡号: ")
if card_id.isdigit() and len(card_id) == 6:
if card_id in creditcard.keys():
cash = input("请输入要转账的金额: ")
if cash.isdigit() and len(cash) > 0:
cash = int(cash)
if cash <= creditcard[current_creditcard]["limit"]:
current_limit = creditcard[current_creditcard]["limit"]
transfer = creditcard[card_id]["limit"]
current_limit -= cash
transfer += cash
current_limitcash = int((current_limit/2))
transfer_limitcash = int((transfer/2))
creditcard[current_creditcard]["limit"] = current_limit
creditcard[current_creditcard]["limitcash"] = current_limitcash
creditcard[card_id]["limit"] = transfer
creditcard[card_id]["limitcash"] = transfer_limitcash
dict = json.dumps(creditcard)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
record = "成功向\33[32;1m%s\33[0m卡号转入\33[32;1m%s\33[0m元." %(card_id, cash)
print(record,'\n')
Creditcard_record(current_creditcard, record)
else:
print("您信用卡的余额只剩\33[31;1m%s\33[0m元了,不足以转账." %creditcard[current_creditcard]["limit"])
else:
print("\33[31;1m转账金额必须为数字且不能为空!\33[0m")
else:
print("\33[31;1m您要转账的卡号不存在!\33[0m")
else:
print("\33[31;1m转账卡号必须为6位数字!")
else:
print("\33[31;1m您的输入有误!\33[0m")
'''还款'''
def Repayment(current_user):
'''此函数用于还款'''
while True:
print("\33[35;1m还款\33[0m".center(50,'-'))
choice = input("是否进行还款吗? 是[y],否[b]")
if choice == 'b':
break
elif choice == 'y':
with open(__db_creditcard_dict,'r+') as f_creditcard_dict:
creditcard = json.loads(f_creditcard_dict.read())
repay_cash = input("请输入需要还款的金额: ")
if repay_cash.isdigit() and len(repay_cash) > 0:
repay_cash = int(repay_cash)
creditcard[current_user]["limit"] += repay_cash
limitcash = int(creditcard[current_user]["limitcash"] / 2)
creditcard[current_user]["limitcash"] = limitcash
dict = json.dumps(creditcard)
f_creditcard_dict.seek(0)
f_creditcard_dict.truncate(0)
f_creditcard_dict.write(dict)
record = "\33[32;1m%s\33[0m还款成功,还款金额:\33[32;1m%s\33[0m,额度剩余:\33[32;1m%s\33[0m" %(current_user, repay_cash ,creditcard[current_user]["limit"])
print(record,'\n')
Creditcard_record(current_user, record)
else:
print("\33[31;1m请输入正确的还款金额!\33[0m")
else:
print("\33[31;1m您的输入有误!\33[0m")
'''查看信用卡流水'''
def View_record(current_creditcard):
while True:
print("\33[35;1m查看信用卡流水\33[0m".center(50,'-'))
choice = input("是否要查看信用卡流水? 是[y],否[b]")
if choice == 'b':
break
elif choice == 'y':
with open(__db_creditcard_record,'r') as f_creditcard_record:
f_creditcard_record.seek(0)
record_dict = json.loads(f_creditcard_record.read())
if current_creditcard in record_dict.keys():
print("信用卡流水查询.")
for k in record_dict[current_creditcard]:
print(k)
date = input("\33[35;1m请输入要查询的日期: 返回[b] / 输入流水单日期[2018-01-01]\33[0m:")
if date == 'b':
break
elif 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])) #key代表时间,current_creditcard[date][key]代表record信息.
print("")
else:
print("输入的格式有误!")
else:
print("此卡号未购物,没有流水记录.")
else:
print("您的输入有误!")
modules/shopping.py
#!/usr/bin/env python
#_*_coding:utf-8_*_
__author__ = 'replaceroot'
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_shopping_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_creditcard_record = BASE_DIR + r"\db\creditcard_record" #信用卡消费记录文件
__db_shopping_record = BASE_DIR + r"\db\shopping_record" #购物记录文件
'''清空购物车'''
def Empty_shopping_car():
with open(__db_shopping_car,'w') as f_shopping_car:
shopping_car = json.dumps([])
f_shopping_car.write(shopping_car)
'''商品信息'''
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_info():
print("编号\t\t商品\t\t\t价格")
for index,item in enumerate(pro_list):
print('%s\t\t%s\t\t\33[34;0m%s\33[0m' %(index,item[0],item[1]))
while True:
print("\33[32;0m目前商城在售商品\33[0m")
pro_info()
choice_id = input("\33[34;0m请输入要购买的商品编号:\33[0m")
if choice_id.isdigit():
choice_id = int(choice_id)
if choice_id >= 0 and choice_id < len(pro_list):
pro_item = pro_list[choice_id] #将用户选择的商品信息保存到此变量中
print("商品\33[32;1m[%s]\33[0m加入购物车,价格为\33[32;1m[%s]\33[0m" %(pro_item[0],pro_item[1]))
shopping_list.append(pro_item)
else:
print("\33[31;0m您输入的商品编号有误,请重新输入!\33[0m")
elif choice_id == 'b':
with open(__db_shopping_car,'r+') as f_shopping_car:
list = json.loads(f_shopping_car.read())
list.extend(shopping_list) #通过extend函数延伸列表
f_shopping_car.seek(0) #将文件指针移至最前
f_shopping_car.truncate(0) #从当前位置开始截断
list = json.dumps(list) #序列化操作并赋值给list函数
f_shopping_car.write(list) #将list中的内容写入文件
break
else:
print("\33[31;0m您输入的商品编号有误,请重新输入!\33[0m")
'''查看购物车'''
def Shopping_car():
print("\33[35;0m查看购物车\33[0m".center(50, '-'))
while True:
with open(__db_shopping_car,'r+') as f_shopping_car:
list = json.loads(f_shopping_car.read())
print("\33[35;0m购物车清单\33[0m")
sum = 0
for index,item in enumerate(list):
print(index,item[0],item[1])
sum += int(item[1]) #将商品的价格加起来汇总
print("购物车商品总额为:\33[34;0m%s\33[0m" %sum)
choice = input("请选择要进行的操作>>>[b-返回][c-清空购物车]")
if choice == 'b':
break
if choice == 'c':
Empty_shopping_car()
'''购物结算'''
def Pay_shopping(current_user):
while True:
sum = 0
with open(__db_shopping_car,'r') as f_shopping_car:
list = json.loads(f_shopping_car.read())
for item in list:
sum += int(item[1])
if_pay = input("购物车商品总额为:\33[32;0m%s\33[0m,是否进行支付? 是[y],返回[b]" %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" %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"]
new_limit = limit - sum
if new_limit > 0:
res = Auth_creditcard(creditcard)
if res == True:
creditcard_dict[creditcard]["limit"] = new_limit
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[32;0m支付成功,当前余额:%s\33[0m" %new_limit)
Shoppingcar_record(current_user, list)
Creditcard_record(creditcard, value)
Empty_shopping_car()
else:
print("\33[31;0m当前卡号的额度:[%s],无法进行支付!\33[0m" %limit)
if if_pay == 'b':
break
'''信用卡密码认证'''
def Auth_creditcard(creditcard):
with open(__db_creditcard_dict) as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
password = input("请输入信用卡密码:")
pwd_again = input("请再次输入信用卡密码:")
if password == pwd_again:
if password == creditcard_dict[creditcard]["password"]:
print("信用卡:\33[32;0m[%s]\33[0m认证成功" %creditcard)
return True
else:
print("\33[31;0m信用卡密码不正确,支付失败!\33[0m")
else:
print("\33[31;0m两次密码不匹配,支付失败!\33[0m")
'''购物记录'''
def Shoppingcar_record(current_user,list):
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 current_user not in record_dict.keys():
record_dict[current_user] = {month:{times:list}}
else:
if month not in record_dict[current_user].keys():
record_dict[current_user][month] = {times:list}
else:
record_dict[current_user][month][times] = list
dict = json.dumps(record_dict)
f_shoppingcar_record.seek(0)
f_shoppingcar_record.truncate(0)
f_shoppingcar_record.write(dict)
'''信用卡消费记录'''
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 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 Shopping_card_view(current_user):
print("\33[35;0m查看购物记录\33[0m".center(50, '-'))
while True:
print("\33[32;0m用户[%s]购物记录\33[0m" %current_user)
with open(__db_shopping_record,'r') as f_shopping_record:
record_dict = json.loads(f_shopping_record.read())
if current_user not in record_dict.keys():
print("\33[31;0m用户%s还没有消费过.\33[0m")
else:
data = sorted(record_dict[current_user])
for d in data:
times = sorted(record_dict[current_user][d])
for t in times:
print("\33[33;0m时间: %s %s\33[0m" %(d,t))
items = record_dict[current_user][d][t]
print("\33[32;1m商品\t\t价格\33[0m")
for v in items:
print("\33[31;0m%s\t\t%s\33[0m" %(v[0],v[1]))
if_back = input("是否返回,返回[b]")
if if_back == "b":
break
'''修改登陆密码'''
def Change_password(current_user):
print("\33[35;0m修改登陆密码\33[0m".center(50, '-'))
while True:
with open(__db_users_dict,'r+') as f_users_dict:
users_dict = json.loads(f_users_dict.read())
choice = input("\33[34;0m您确定要修改%s账号的密码吗? 确定[y],取消[b]\33[0m" %current_user)
if choice == 'b':
break
elif choice == 'y':
old_pwd = input("请输入旧密码:")
new_pwd = input("请输入新密码:")
new_pwd_again = input("请再次输入新密码:")
if old_pwd == users_dict[current_user]["password"]:
if new_pwd == new_pwd_again:
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)
time.sleep(2)
print("\33[32;0m密码修改成功,新密码为:[%s]\33[0m" %users_dict[current_user]["password"])
else:
print("\33[31;0m两次密码不匹配,修改失败!\33[0m")
else:
print("\33[31;0m旧密码错误,修改失败!\33[0m")
else:
print("\33[31;0m您的输入有误,请重新输入!\33[0m")
'''修改收货地址'''
def Change_address(current_user):
print("\33[35;0m修改收货地址\33[0m".center(50,'-'))
while True:
choice = input("您确定要修改收货地址吗? 确定[y],取消[b]")
if choice == 'b':
break
elif choice == 'y':
with open(__db_users_dict,'r+') as f_users_dict:
users_dict = json.loads(f_users_dict.read())
address = input("请输入新的收货地址:")
users_dict[current_user]["address"] = address
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("用户:[%s]更新后的收货地址为:%s" %(current_user, users_dict[current_user]["address"]))
else:
print("\33[31;0m您的输入有误!\33[0m")
'''修改信用卡绑定'''
def Change_creditcard(current_user):
print("\33[35;0m修改信用卡绑定\33[0m".center(50, '-'))
while True:
choice = input("您确定要修改信用卡绑定吗? 确定[y],取消[b]")
if choice == 'b':
break
elif choice == 'y':
with open(__db_users_dict,'r+') as f_users_dict:
users_dict = json.loads(f_users_dict.read())
print("当前用户[%s]绑定的信用卡为:%s" %(current_user, users_dict[current_user]["creditcard"]))
new_creditcard = input("请输入需要绑定的信用卡号:").strip()
if new_creditcard.isdigit() and len(new_creditcard) == 6:
with open(__db_creditcard_dict,'r') as f_creditcard_dict:
creditcard_dict = json.loads(f_creditcard_dict.read())
if new_creditcard in creditcard_dict.keys(): #判断输入的卡号是否存在
users_dict[current_user]["creditcard"] = new_creditcard
dict = json.dumps(users_dict)
f_users_dict.seek(0)
f_users_dict.truncate(0)
f_users_dict.write(dict)
print("用户[%s]新绑定的信用卡为:[%s]" %(current_user,users_dict[current_user]["creditcard"]))
else:
print("信用卡[%s]还未发行" %s(new_creditcard))
else:
print("\33[31;0m信用卡必须为6位数字!\33[0m")
else:
print("\33[31;0m您的输入有误!\33[0m")
用户信息文件
admincenter_dict
{"admin":{"username":"admin","password":"admin.1234"}}
creditcard_dict
{"111111": {"personinfo": "\u5929\u68da\u5143\u5e05", "password": "123", "limit": 27150, "locked": 0, "limitcash": 29, "deflimit": 15000, "creditcard": "111111"}, "222222": {"personinfo": "\u94f6\u89d2\u5927\u738b", "password": "123", "limit": 14849, "locked": 0, "limitcash": 7500, "deflimit": 15000, "creditcard": "222222"}, "888888": {"personinfo": "\u91d1\u89d2\u5927\u738b", "password": "123", "limit": 14288, "locked": 0, "limitcash": 7585, "deflimit": 20000, "creditcard": "888888"}, "123456": {"creditcard": "123456", "password": "1234567", "personinfo": "\u5f20\u6ce2", "limit": 15000, "limitcash": 7500, "locked": 0, "deflimit": 15000}}
creditcard_record
{"888888": {"2016-08-19": {"09:59:31": "\u8d2d\u7269\u652f\u4ed8 6297", "10:24:55": "\u001b[31;1m\u4fe1\u7528\u5361 888888 \u8fd8\u6b3e\u91d1\u989d \uffe55000 \u8fd8\u6b3e\u6210\u529f\u001b[0m", "10:22:48": "\u001b[31;1m\u8f6c\u8d26\u5361\u53f7 666666 \u91d1\u989d \uffe51000 \u8f6c\u8d26\u6210\u529f\u001b[0m", "11:14:54": "\u001b[31;1m\u8f6c\u8d26\u5361\u53f7 666666 \u91d1\u989d \uffe51000 \u8f6c\u8d26\u6210\u529f\u001b[0m", "10:19:55": "\u001b[31;1m\u63d0\u73b0\uffe52000 \u624b\u7eed\u8d39\uffe5100\u001b[0m", "10:19:45": "\u001b[31;1m\u63d0\u73b0\uffe5300 \u624b\u7eed\u8d39\uffe515\u001b[0m"}}, "666666": {"2018-06-03": {"15:26:31": "\u001b[31;1m\u8f6c\u8d26\u5361\u53f7 666666 \u91d1\u989d \uffe510000 \u8f6c\u8d26\u6210\u529f\u001b[0m"}}, "111111": {"2018-06-10": {"10-49-24": "\u001b[32;1m111111\u001b[0m\u8fd8\u6b3e\u6210\u529f,\u989d\u5ea6\u5269\u4f59:\u001b[32;1m27000\u001b[0m", "10-49-44": "\u001b[32;1m111111\u001b[0m\u8fd8\u6b3e\u6210\u529f,\u989d\u5ea6\u5269\u4f59:\u001b[32;1m27100\u001b[0m", "10-50-09": "\u001b[32;1m111111\u001b[0m\u8fd8\u6b3e\u6210\u529f,\u989d\u5ea6\u5269\u4f59:\u001b[32;1m27150\u001b[0m"}}, "222222": {"2018-06-11": {"15-10-34": "\u652f\u4ed8\u8d2d\u7269:151"}}}
details
信用卡取现需知-》》
①信用卡默认取现额度为信用卡额度的50%(此系统默认取现额度为7500)
②信用卡进行消费时(未取现),取现额度不会变化
③信用卡额度低于取现额度时,可取现金额以当前额度为准(可取现金额=当前额度*0.95)
④取现额度只有两种状况会发生变化,一种进行取现,第二是对信用卡额度进行调整
⑤每次取现扣除5%的手续费
product_list
Iphone 8000
MacBook 12000
Iwatch 4000
Ipad 2000
Ipod 2399
MSFBook 80
Android 71
Python 99
Coffee 23
shopping_car
[]
shopping_record
{"replaceroot": {"2018-06-11": {"15-10-34": [["MSFBook", "80"], ["Android", "71"]]}}}
users_dict
{"replaceroot": {"username": "replaceroot", "password": "replaceroot", "locked": 0, "creditcard": "222222", "address": "\u5e7f\u4e1c\u7701\u4e2d\u5c71\u5e02"}, "metasploit": {"username": "metasploit", "password": "123456", "locked": 0, "address": "None"}, "admin": {"username": "admin", "password": "admin", "creditcard": "444444", "address": "\u5e7f\u5dde", "locked": 0}}
部分功能效果演示
参考blog:
http://www.cnblogs.com/lianzhilei/p/5786223.html#3989544
http://www.cnblogs.com/alex3714/articles/5765046.html