day3 ATM机加购物车
run_case
from day04.day4.Atm import ATM
from day04.day4.shopping import shoppin
from day04.day4.admin import admin_view
def run_case():
run={"1":ATM,"2":shoppin,"3":admin_view}
while True:
r = '''
"1":ATM功能
"2":购物功能
"3":admin功能
'''
print(r)
index = input("请输入要使用功能的,/n退出")
if index=="n":
return
if index in run:
run[index]()
else:
print("输入错误")
run_case()
ATM
import os
import time,random
def aout(func):
def wrepper(*args,**kwargs):
login_user = input("bank_account:").strip()
login_paw = input("bank_paw").strip()
with open("register.txt", mode='rt', encoding="utf-8") as login:
for line in login.readlines():
line = line.strip('\n').split(',')
if login_user == line[0] and login_paw == line[1]:
ret = func(*args, **kwargs)
return ret
elif line[2]=="1":
print("账号已冻结")
return
else:
print("密码错误,请从新输入...")
return wrepper
def Log(func):
def wrepper(*args,**kwargs):
ret = func(*args, **kwargs)
func_name=func.__name__
tims= time.strftime('%Y-%m-%d %X')
with open("access.log",mode="a",encoding="utf-8")as w:
a = "当时时间%s执行函数%s当前记录%s" % (tims, func_name, ret)
w.write(a+'\n')
return ret
return wrepper
def register():
# 注册
user=input("请输入注册用户:")
pwd=input("请输入注册密码:")
status="0"
with open("register.txt",mode="at",encoding="utf-8")as regit:
regit.write(user+','+pwd+","+status)
print("注册成功,前去登录吧")
return
@aout
def ATM_login():
# 登录
print("登录成功")
@aout
@Log
def credit():
"""申请信用卡 """
with open("register.txt", mode="r", encoding="utf-8")as r:
for i in r.readlines():
line=i.strip().split(',')
print(line[0])
user=input("user:")
if user in line[0]:
cret = '''
请选择您要申请的信用卡类型,
1: 可用额度15000,
2: 自定义额度
'''
print(cret)
credit_type = input("请选择...")
with open("credit_money.txt", mode="a", encoding="utf-8")as credit:
if credit_type == "1":
money=15000
data={"user":user,"money":money,"rfund":None}
credit.write(data)
print("恭喜您申请信用卡成功\n,信用额度为15000")
return user,money
elif credit_type == "2":
money = input("请输入您要申请的额度")
data={"user":user,"money":money,"rfund":None}
credit.write(data)
print("您的额度已成功申请到您的账户")
return user,money
else:
print("请从新选择...")
else:
print("请从新输入您的账号")
@aout
@Log
def carry_money():
user = input("请输入您的账号")
with open("credit_money.txt",mode="r",encoding="utf-8")as carry,\
open("new_credit_money.txt", mode="a", encoding="utf-8")as a:
carry=carry.read()
line=eval(carry)
if user == line["user"]: #如果用户在信用卡用户里面
CarryMoney = int(input("请输入要提现的金额"))
if CarryMoney+(CarryMoney*0.05)> line["money"]:
print("提现金额超出信用卡可提额度")
else:
new_money= int(line["money"]) - (CarryMoney + (CarryMoney * 0.05))
rfund=CarryMoney + (CarryMoney * 0.05)
data = {"user": user, "money": new_money, "rfund": rfund}
a.write(data)
return data
else:
print("用户账号不存在")
os.remove("credit_money.txt")
os.rename("new_credit_money.txt", "credit_money.txt")
@aout
@Log
def transfer():
# 转账
account = input("请输入您的账号:")
transfer_user = input("请输入要转账的账号")
with open("credit_money.txt", mode="r", encoding="utf-8")as transfer,\
open("new_credit_money.txt",mode="at",encoding="utf-8")as transfer1:
for line in transfer1:
line = eval(line)
if account ==line["user"]:
money = int(input("请输入要转账的金额"))
if money <=line["money"]:
transfer_money=line["money"]-money
# 转账另存
transfer.write(transfer_user+','+transfer_money)
return account,transfer_money
else:
print("输入金额大于余额,请从新输入")
else:
print("账号不存在")
os.remove("credit_money.txt")
os.rename("new_credit_money.txt", "credit_money.txt")
@aout
@Log
def refund():
user=input("请输入要还款的账号:")
with open("credit_money.txt", mode="r", encoding="utf-8")as refund,\
open("new_credit_money.txt", mode="at", encoding="utf-8") as a:
refud=refund.read()
refud=eval(refud)
if user in refud["user"]:
refund_money=int(input("请输入要还款的金额:"))
if refund_money >refud["refund"]:
print("输入金额超出返款金额")
else:
new_refund=refud["refund"]-refund_money
money_refund=new_refund+refund["money"]
data = {"user": user, "money": money_refund, "rfund": new_refund}
a.write(data)
return user,data
os.remove("credit_money.txt")
os.rename("new_credit_money.txt","credit_money.txt")
@aout
@Log
def bill():
'''每月22号出账单,每月10号为还款日,过期未还,按欠款总额万分之5每日计息'''
with open("credit_money.txt", mode="r", encoding="utf-8")as r_bill:
r_bill = r_bill.read()
r_bill = eval(r_bill)
user=input("user")
if user in r_bill["user"]:
if r_bill["rfund"] !=0:
t = time.strftime("%d")
ti=22
tm=10
with open("bill_detill.txt",mode="at",encoding="utf-8")as a:
if int(t)>tm:
newt=int(time.strftime("%d")) - int(t)
new_bill=int(r_bill["rfund"])*0.05*newt
if time.strftime("%d")==ti:
b="用户%s,的账单为%s,需要还款金额为%s"%(user,r_bill["rfund"],new_bill)
a.write(time.strftime("%Y-%m-%d %X")+','+b+'\n')
return user,b
def ATM():
# if not ATM_login():
# return
se={"3":carry_money,"4":transfer,"5":bill,"6":credit,"7":refund}
while True:
atm = '''
"3":提现
"4":转账
"5":出账单
"6":申请信用卡
"7":还款
'''
print(atm)
index = input("请输入要使用功能的,/n退出")
if index=="n":
return
if index in se:
se[index]()
else:
print("输入错误")
if __name__ == '__main__':
ATM()
shopping
import os
from day04.day4.Atm import ATM_login
# 购物中心========================================
# 商品信息
products = [{"name":"挨粪叉","price":6888},
{"name":"锤子TNT","price":10888},
{"name":"小米mix2","price":2888}]
# 购物车
car = {}
# 购物
def shopping():
while True:
# 打印出所有商品信息
count=1
for i in products:
print(f"序号",{count},"商品",{i["name"]},"价格",{i["price"]})
count+=1
select=input("请选择要添加的商品序号 ,按n退出")
if select=="n":
return
if select.isdigit() and int(select) >=1 and int(select) <= len(products):
idex=products[int(select)-1]
print("购物商品已添加成功%s"%idex["name"])
if idex["name"] in car:
car[idex["name"]]["count"]+=1
else:
car[idex["name"]]={"price":idex["price"],"count":1}
else:
print("输入有误 请从新输入")
def show_goods():
if not car:
s=input("您的购物车是空的,输入y/n")
if s=="y":
shopping()
return
else:
return
print("您的购物车信息:")
for name in car:
print("名称:%-10s 价格:%-8s 数量:%-3s 总价:%-10s" % (name,
car[name]["price"],
car[name]["count"],
car[name]["price"] * car[name]["count"]))
select = input("输入y调整商品数量!(数量为0则删除商品!)(数量为1则购买商品!) 输入其他退出!")
if select == "y":
modify_count()
def modify_count():
name=input("请输入商品名称,q则退出")
while True:
if name=="q":
return
if name not in car:
continue
while True:
count=int(input("请输入数量:"))
if count==0:
car.pop(name)
print("%s已删除"%name)
else:
car[name]["count"]=count
print("数量更改成功")
return
def pay():
sum=0
for name in car:
sum+=car[name]["price"] * car[name]["count"]
user = input("请输入您的账号")
with open("credit_money.txt", mode="r", encoding="utf-8")as carry, \
open("new_credit_money.txt", mode="a", encoding="utf-8")as a:
carry = carry.read()
line = eval(carry)
if user == line["user"]: # 如果用户在信用卡用户里面
if sum>line["money"]:
print("额度不足。。")
else:
money=line["money"]-sum
mo=line.replace(["money"],money)
a.write(mo)
os.remove("credit_money.txt")
os.rename("new_credit_money.tx","credit_money.txt")
def shoppin():
# if not ATM_login():
# return
while True:
numbr='''
"1":购物,
"2":查看购物车,
"3":修改购物车商品,
"4":结算
'''
print(numbr)
index=input("请输入要使用功能的序号")
goods={"1":shopping,"2":show_goods,"3":modify_count,"4":pay}
if index in goods:
goods[index]()
else:
print("请从新输入")
if __name__ == '__main__':
shoppin()
admin
import os
import time
from day04.day4.Atm import ATM_login
def admin_add_user():
user=input("请输入要添加的用户:")
with open("register.txt", mode='rt', encoding="utf-8") as add_user,\
open("register.txt", mode='at', encoding="utf-8")as writ:
for line in add_user.readlines():
line = line.strip('\n').split(',')
if user == line[0]:
print("用户已存在")
else:
pwd=input("请输入您的密码")
writ.write(user+','+pwd+'\n')
print("添加用户成功")
def lock_user():
user=input("请输入要冻结的账号:")
with open("register.txt", mode='rt', encoding="utf-8") as lock_r,\
open("new_register.txt", mode='at', encoding="utf-8")as lock_w:
for line in lock_r.readlines():
line = line.strip('\n').split(',')
if user == line[0]:
lock_w.write(str(line).replace(line[2],"1"))
else:
print("user不正确")
os.remove("register.txt")
os.rename("new_register.txt","register.txt")
def unlock_user():
user=input("请输入要冻结的账号:")
with open("register.txt", mode='rt', encoding="utf-8") as lock_r,\
open("new_register.txt", mode='at', encoding="utf-8")as lock_w:
for line in lock_r.readlines():
line = line.strip('\n').split(',')
if user == line[0]:
lock_w.write(str(line).replace(line[2],"0"))
else:
print("user不正确")
os.remove("register.txt")
os.rename("new_register.txt","register.txt")
def adjust_quota():
adjust=input("请输入要调整额度的用户")
with open("credit_money.txt", mode="rt", encoding="utf-8")as r,\
open("new_credit_money.txt", mode="rt", encoding="utf-8")as w:
for i in r.readlines():
if adjust==i[0]:
us=input("请输入新的额度")
i=i.replace(i[1],us)
w.write(i)
os.remove("credit_money.txt")
os.rename("new_credit_money.txt","credit_money.txt")
def admin_view():
# 调用登录功能验证是否有权限执行操作
# if not ATM_login():
# return
admin_funcs = {"1":admin_add_user,"2":lock_user,"3":adjust_quota,"4":unlock_user}
while True:
print("""
1.添加账户
2.修改额度
3.冻结账户
4.解冻账户
(q.退出!)""")
index = input("请选择:\n")
if index == "q":
return
if index in admin_funcs:
admin_funcs[index]()
else:
print("输入有误请重试!")
if __name__ == '__main__':
admin_view()

浙公网安备 33010602011771号