标签:pms use val time() ftime 要求 draw strftime users
项目要求:
登录,注册,查看余额,转账,存款,取款,查看流水,购物,查看购买商品,退出
import os, time
class Util:
def inw(self):
name = input("请输入账号:").strip()
pwd = input("请输入密码:").strip()
return name, pwd
def checkuser(self, name):
flag = 0
userpwd = ""
money = ''
if os.path.exists("users.txt"):
with open("users.txt", mode="rt", encoding="utf-8") as f:
for line in f:
username, userpwd, money = line.strip().split(":")
if name == username:
flag = 1
break
return flag, userpwd, money
def changeusermsg(self, name, money):
with open("users.txt", mode='rt', encoding="utf-8") as f, open("users.txt.swap", mode="wt", encoding="utf-8") as ff:
for line in f:
uname, upwd, umoney = line.strip().split(":")
if uname == name:
umoney = round(eval(umoney+money),2)
ff.write(f'{uname}:{upwd}:{umoney}\n')
else:
ff.write(f'{uname}:{upwd}:{umoney}\n')
os.remove("users.txt")
os.rename("users.txt.swap", "users.txt")
def nowtime(self):
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def water(self, wstr, uname, money):
nowtime = self.nowtime()
with open(f'{uname}.log', mode="at", encoding="utf-8") as f:
f.write(f'{nowtime} {wstr} {money}\n')
def shopmsg(self, uname, shoplist, sum):
shoptime = self.nowtime()
with open(f'{uname}.shop', mode='at', encoding="utf-8") as f:
f.write(f'{shoptime}\n')
for k, v in shoplist.items():
f.write(f'商品名:{k} 单价:{dic_shop.get(k)}¥ 数量:{v}\n')
f.write(f'总额:{sum}¥\n')
def readwater_or_shopmsg(self, path):
if os.path.exists(path):
with open(path, mode="rt", encoding="utf-8") as f:
for line in f:
print(line, end="")
return 1
else:
return 0
class Atm(Util):
def view_account(self):
'''查看余额'''
print(f"{cookie}的余额".center(50, '='))
with open("users.txt", mode="rt", encoding="utf-8") as f:
for line in f:
username, _, money = line.strip().split(":")
if username == cookie:
print(f"余额:{money}¥")
break
return
def transfer_accounts(self):
'''转账'''
print('转账'.center(50, '='))
tname = input("请输入要转账的账号:").strip()
flag, *_ = self.checkuser(tname)
*_, money = self.checkuser(cookie)
if flag:
while 1:
tmoney = input("请输入转账金额:").strip()
if not tmoney.isdigit():
print("非法输入,请重新输入")
continue
else:
m = float(money) - float(tmoney)
if m < 0:
print("账号余额不足, 无法转账")
continue
break
self.changeusermsg(cookie, f'-{tmoney}')
time.sleep(1)
self.changeusermsg(tname, f'+{tmoney}')
print("转账成功".center(50, '='))
self.water("转账", cookie, f'-{tmoney}')
self.water("转账", tname, f'+{tmoney}')
return
else:
print("转账账号不存在")
return
def withdrawal(self):
'''存款'''
print("存款".center(50, '='))
while 1:
wmoney = input("请输入存款金额:").strip()
if not wmoney.isdigit():
print("非法输入,请重新输入")
continue
break
self.changeusermsg(cookie, f'+{wmoney}')
print("存款成功".center(50, '='))
self.water("存款", cookie, f'+{wmoney}')
return
def deposit(self):
'''取款'''
print("取款".center(50, '='))
*_, money = self.checkuser(cookie)
while 1:
dmoney = input("请输入取款金额:").strip()
if not dmoney.isdigit():
print("非法输入,请重新输入")
continue
if float(money) < float(dmoney):
print("余额不足")
continue
break
self.changeusermsg(cookie, f'-{dmoney}')
print("取款成功".center(50, '='))
self.water("取款", cookie, f'-{dmoney}')
return
def check_water(self):
'''查看流水'''
print(f'{cookie}的流水'.center(50, '='))
flag = self.readwater_or_shopmsg(f'{cookie}.log')
if not flag:
print("您没有流水可以查看")
return
return
class Shop(Util):
def shopping(self):
'''购物'''
print("购物".center(50, '='))
sum = 0
shop_car = {}
print(msg_shop)
while 1:
shopcmd = input("请输入命令(序号)>>>:")
if shopcmd.upper() == "Y":
for k, v in shop_car.items():
sum += dic_shop.get(k) * int(v)
*_, money = self.checkuser(cookie)
if float(money) >= sum:
self.changeusermsg(cookie, f'-{sum}')
self.water("购物", cookie, f'-{sum}')
print("购物成功".center(50, '='))
self.shopmsg(cookie, shop_car, sum)
return
else:
print("账号余额不足,购物失败")
return
if shoplist[int(shopcmd)] in dic_shop:
shopcount = input("请输入购买数量:")
shop_car[shoplist[int(shopcmd)]] = shopcount
else:
print("无效指令,请重新输入")
def check_shop(self):
'''查看购买商品'''
print(f'{cookie}的购物历史'.center(50, '='))
flag = self.readwater_or_shopmsg(f'{cookie}.shop')
if not flag:
print("您还没有购物历史哦!")
return
return
class User(Atm, Shop, Util):
def login(self):
print("登录界面".center(50, '='))
name, pwd = self.inw()
flag, userpwd, _ = self.checkuser(name)
if flag:
while 1:
if pwd == userpwd:
global cookie
cookie = name
print(f"欢迎您{name}".center(50, '='))
print(msg1)
cmd = input("请输入命令(序号)>>>:")
if not cmd.isdigit() or int(cmd) > 7:
print("命令无效,请重新输入")
continue
if cmd == "0":
self.exit()
return
else:
self.__getattribute__(dic.get(str(int(cmd)+2)))()
else:
print("密码错误,请从新输入")
pwd = input("请输入密码:")
else:
print("账号不存在,请先注册")
return
def regist(self):
print("注册界面".center(50, '='))
while 1:
name, pwd = self.inw()
flag, *_ = self.checkuser(name)
if not flag:
with open("users.txt", mode="at", encoding="utf-8") as f:
f.write(f'{name}:{pwd}:0\n')
print("注册成功".center(50, '='))
return
else:
print("账号以存在,请从新注册")
def exit(self):
global cookie
cookie = ''
cookie = ''
dic = {'0':'exit', '1':'login', '2':'regist', '3':'view_account', '4':'transfer_accounts', '5':'withdrawal', '6':'deposit', '7':'check_water', '8':'shopping', '9':'check_shop'}
dic_shop = {
"thinkpad":8888,
"小黄书":100,
"iphone":5500,
"辣条":2.5,
"衣服":199,
"水果":38.8
}
shoplist = ['','thinkpad','小黄书','iphone','辣条','衣服','水果']
msg = '''
1.登录
2.注册
'''
msg1 = '''
0.退出
1.查看余额
2.转账
3.存款
4.取款
5.查看流水
6.购物
7.查看购买商品
'''
msg_shop = '''
1. thinkpad 8888¥
2. 小黄书 100¥
3. iphone 5500¥
4. 辣条 2.5¥
5. 衣服 199¥
6. 水果 38.8¥
(购物请输入商品序号,结算请输入Y/y)
'''
if __name__ == '__main__':
print("欢迎使用001ATM机!".center(50, '='))
while 1:
print(msg)
cmd = input("输入指令(序号)>>>:").strip()
if cmd in dic:
if cmd == '1' or cmd == '2':
o = User()
res = o.__getattribute__(dic.get(cmd))
res()
else:
print("---->请先登录")
continue
else:
print("无效命令")
标签:pms use val time() ftime 要求 draw strftime users
原文地址:https://www.cnblogs.com/chenwenyin/p/12515967.html