作业需求:
模拟实现一个ATM + 购物商城程序
额度 15000或自定义
实现购物商城,买东西加入 购物车,调用信用卡接口结账
可以提现,手续费5%
每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息
支持多账户登录
支持账户间转账
记录每月日常消费流水
提供还款接口
ATM记录操作日志
提供管理接口,包括添加账户、用户额度,冻结账户等。。。
用户认证用装饰器
流程图:
import os
# 获取商品数据
def get_product():
pr = []
with open('product.txt', mode='rt', encoding='utf-8') as f:
for fi in f:
l = fi.strip().split(",")
if "." in l[2]:
l[2] = float(l[2])
else:
l[2] = int(l[2])
pr.append(l)
return pr
# 获取用户数据
def get_db():
with open('db.txt', mode='rt', encoding='utf-8') as f:
return f.read().splitlines()
# 登陆验证
def check_login(user, pwd):
db = get_db()
for ui in db:
u = ui.split(",")
if user == u[0] and pwd == u[1]:
return 1
# 生成商品列表
def show_product():
p = get_product()
print("商品列表".center(50, "="))
for pi in p:
print("{p_id}\t{p_name}\t\t\t{price}".format(p_id=pi[0], p_name=pi[1], price=pi[2]))
# 添加购物车
def add_cart(p_id, cartd, action="add"):
p_db = get_product()
for pp in p_db:
if p_id in pp:
if action == "add":
for cd in cartd:
if p_id == cd[0]:
cd[3] += 1
break
else:
tp = pp
tp.append(1)
cart.append(tp)
# print(cart[-1])
return pp
return 0
# 银行认证及扣款
def bank(money_list, user_id, buy_money):
users, moneys = money
if user_id == users:
if buy_money <= money[1]:
money[1] -= buy_money
return 1
print("余额不足")
return 0
print("用户认证失败")
return 0
# 打印购物车
def show_cart(s):
print("已购商品".center(50, "="))
sum = 0
for cart_end in s:
sum += (cart_end[2] * cart_end[3])
print("{p_id}\t{p_name}\t\t\t{price}\t\t{num}\t\t\t{sum_price}".format(p_id=cart_end[0], p_name=cart_end[1],
price=cart_end[2], num=cart_end[3],
sum_price=cart_end[2] * cart_end[3]))
print("总花费:{}".format(str(sum)))
# ========================银行相关
def load_bank_db():
lbd = []
with open('bank.txt', "rt", encoding="utf-8") as f1:
for bd in f1:
l1 = bd.strip().split(",")
if l1[2].isdigit():
l1[2] = int(l1[2])
else:
l1[2] = float(l1[2])
lbd.append(l1)
return lbd
def login_bank(user, pwd):
for buser in db:
if user in buser:
if pwd == buser[1]:
return 1
break
else:
return 0
# 显示帐户信息
def show_bk(b_id):
for i in db:
if b_id in i:
print("卡号:{c_id}\n余额:{c_price}".format(c_id=i[0], c_price=str(i[2])))
def get_money(b_id, moneyss):
for i in db:
if b_id in i:
if int(moneyss) > i[2]:
# print("余额不足")
return "-1",i[2]
else:
i[2] -= int(moneyss)
return "ok",i[2]
def save_bank(b_id,l8):
with open("bank.txt","rt",encoding="utf-8") as f1,open("bank.txt.swap","wt",encoding="utf-8") as f2:
for i in f1:
res=i.strip().split(",")
if b_id == res[0]:
res[2]=l8
newstr=",".join(str(j) for j in res)
f2.write("%s\n"%newstr)
continue
f2.write(i)
os.remove("bank.txt")
os.rename("bank.txt.swap","bank.txt")
tag = True
cart = []
money = []
print("""
功能菜单
=================
1 进入购物
2 进入ATM
b 退出
""")
cmd_num = 0
while tag:
if cmd_num >= 3:
print("就两个功能也能输错,还是走吧")
tag = False
break
cmd = input("请选择功能编号:").strip()
if cmd == "b":
tag = False
break
if not cmd.isdigit():
print("无此功能,请重新输入")
cmd_num += 1
continue
if cmd == "1":
while tag:
in_user = input("请输入用户名:").strip()
in_pwd = input("请输入密码:").strip()
res = check_login(in_user, in_pwd)
if res:
money = [in_user, 15000]
show_product()
while tag:
in_buy = input("请输入要购买的商品:").strip()
if in_buy == "b":
tag = False
break
buy_res = add_cart(in_buy, cart, "check")
if buy_res != 0:
if bank(money, in_user, buy_res[2]) != 0:
add_cart(in_buy, cart)
print("你的购物车添加成功,当前余额为:%s" % money[1])
else:
print("商品不存在,请重新选择")
else:
print("登陆失败")
show_cart(cart)
print("帐户余额为:%s" % money[1])
if cmd == "2":
while tag:
db = load_bank_db()
print(db)
b_user = input("请输入银行卡号:").strip()
b_pwd = input("请输入卡密码:").strip()
if login_bank(b_user, b_pwd):
print("认证通过")
print("""
1 查询帐户
2 取现
3 还款
4 转账
""")
while tag:
cmd = input("请选择功能").strip()
if cmd == "1":
show_bk(b_user)
if cmd == "2":
moneeys = input("请输入取款金额:").strip()
if not moneeys.replace(".", "", 1).isdigit():
print("输入的金额错误,请重新输入")
continue
res=get_money(b_user, moneeys)
if res[0] == "ok":
print("取款:{c_id}元\n余额:{c_price}".format(c_id=moneeys, c_price=res[1]))
save_bank(b_user,res[1])
else:
print("认证失败")
tag = False