目前实现的功能不多,目前具有的功能,用户的登录,转账,提现,查看余额。并用装饰器检测在各个接口用户是否有登录。
好了,上代码。
这是各个文件
==========>atm.py
#Auther: He Jianhan
import os,sys
routes = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(routes)
from core import main
#调入主运行文件并执行
main.run(routes)
==========>main.py
import os,sys,json
from core import operation
sign_in = {"name":None,"stata":False,"information":None}
#name;用于存储用户姓名
#stata:存储登录信息
#inforamtion :存储用户文件信息
def into_sign_in(routes,name):
#将储存用户信息的文件代入遇到sign_in
with open(routes + r"\user\%s" % (name), "r") as f:
username = json.load(f)
sign_in["name"] = name
sign_in["stata"] = True
sign_in["information"] = username
def run(routes):
#让用户输入登录信息
#登录成功执行选项操作模块
loop = True
while loop:
name = input("Your ID:")
password = input("Your password:")
if os.path.exists(routes+r"\user\%s"%(name)):
with open(routes+r"\user\%s"%(name),"r") as f:
username = json.load(f)
if password == username["password"]:
into_sign_in(routes, name)
print("welcom \033[31;1m%s\033[0m !!!"%(name))
operation.operations(sign_in)
loop = False
else:
print("密码错误")
elif name == "q":
loop = False
else:
print("找不到该用户")
==========>operation.py
from core import paragraph
def operations(sign_in):
#显示选项操作信息
#根据选择调取相应函数
operation_loop = True
while operation_loop:
print("你想要做哪些操作:"
"\n0 显示余额"
"\n1 转账"
"\n2 提现"
"\n3 退出")
operation_dict = {"0":"look",
"1":"repay",
"2":"take",
"3":"exit"}
choice_operation = input("选择你要的操作")
if choice_operation in operation_dict:
operation_dict_itmes = operation_dict[choice_operation]
if operation_dict_itmes == "look":
print("你的余额剩下\033[31;1m%s\033[0m"%(sign_in["information"]["money"]))
elif operation_dict_itmes == "take":
while True:
money = input("你要提现多少金额")
if money.isdigit():
money = float(money)
paragraph.paragraphs(sign_in,operation_dict_itmes,money)
elif money == "q":
break
else:
print("错误!!!!!请输入整数")
elif operation_dict_itmes == "repay":
while True:
name = input("你转账给谁")
if os.path.exists(routes+r"\user\%s"%(name)):
while True:
money = input("你要转多少金额")
if money.isdigit():
money = int(money)
go = paragraph.paragraphs(sign_in, operation_dict_itmes, money)
if go:
paragraph.paragraphs(name, "Receivables", money)
elif money == "q":
break
else:
print("请输入整数")
break
elif name == "q":
break
else:
print("没有该用户")
elif operation_dict_itmes == "exit":
operation_loop = False
else:
print("没有这个操作")
==========>paragraph.py
import os,json
routes = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
paragraph_dict = {"repay":{"plus_reduce":"reduce","interest":"0"},
"take":{"plus_reduce":"reduce","interest":"0.05"},
"Receivables":{"plus_reduce":"plus","interest":"0"}}
#根据相应的类型决定加减金额,还有利息
def load(name,result_plus_reduce):
#根据用户文件信息打开相关文件进行覆盖存储
name["money"] = result_plus_reduce
with open(routes+r"\user\%s"%(name["name"]),"w") as loadss:
json.dump(name,loadss)
def prov_login(paragraphs):
#装饰器,用于验证用户是否登录,防止未登录就进行金额加减操作
def proving(*args,**kwargs):
if type(args[0]) == dict:
if args[0]["stata"]:
paragraphs(*args)
return True
else:
print("账号没有登录")
return
else:
paragraphs(*args)
return proving
@prov_login
def paragraphs(sign_in,operation_dict_itmes,money):
#进行金额的加减操作
if paragraph_dict[operation_dict_itmes]["plus_reduce"] == "plus":
with open(routes + r"\user\%s" % (sign_in), "r") as dumpss:
dumpsss = json.load(dumpss)
result_plus_reduce = float(dumpsss["money"]) + money
result_plus_reduce = round(result_plus_reduce,2)
load(dumpsss, result_plus_reduce)
elif paragraph_dict[operation_dict_itmes]["plus_reduce"] == "reduce":
result_plus_reduce = float(sign_in["information"]["money"]) - money - \
float(paragraph_dict[operation_dict_itmes]["interest"])*money
result_plus_reduce = round(result_plus_reduce, 2)
if result_plus_reduce < 0 :
print("你的\033[31;1m金额\033[0m不足")
else:
load(sign_in["information"], result_plus_reduce)
print("你%s了\033[31;1m%s\033[0m"%(operation_dict_itmes,int(money)))