【一】需求
# ATM : 提款姬
# def save_data(file_path,)
# user_pwd_dict ={}
# 文本 : dream-521
# dream,521
# user_info.txt : 每一个人的信息就是一行
# dream_flow.txt :
# print(time.localtime())
# 注册 : 存储到文件中 用户名 - 登录密码 - 年龄 - 银行卡号(1314) - 取款密码 - 余额(1000)
# 登录 : 直接将用户信息从文件中取出,然后进行比对 用户名 - 密码
# 取款 : 验证你的取款密码,更改余额 余额(1000) ,记录你的提款信息 -- 文件里 - 加时间
# 转账 : 验证你的取款密码,更改余额 目标银行卡号去转 记录你的提款信息 -- 文件里
【二】代码
import time
login_user_dict = {'username': '',
'role': '',
"bank_id": '',
"bank_pay_pwd": "",
"balance": ""
}
def create_data_type(tag, *args, **kwargs):
# print(kwargs) # {'username': 'dream', 'password': '521', 'role': 'admin'}
if tag == 'pwd':
data = '|'.join(kwargs.values()) # dream|521|admin
return data
elif tag == 'bank':
data = '|'.join([str(value) for value in kwargs.values()])
return data
def read_data(path, tag=None):
user_pwd_dict = {}
user_bank_dict = {}
with open(path, 'r', encoding='utf8') as fp:
data = fp.read()
data_list = data.split('\n')
data_list.pop()
if tag == 'pwd':
for data in data_list:
username, password, role = data.split('|')
user_pwd_dict[username] = {'password': password, 'role': role}
# {'dream': {'password': '521', 'role': 'admin'}}
return user_pwd_dict
elif tag == 'bank':
for data in data_list:
username, pay_pwd, bank_id, balance = data.split('|')
user_bank_dict[username] = {'pay_pwd': pay_pwd, 'bank_id': bank_id, 'balance': balance}
return user_bank_dict
def save_data(path, data, mode='a'):
with open(path, mode, encoding='utf8') as fp:
fp.write(data + '\n')
def get_user_pwd():
# 【1】要获取用户名和密码
username = input("用户名 :>>>> ").strip()
password = input("密码 :>>>> ").strip()
return username, password
def register():
username, password = get_user_pwd()
# 【2】拼接存储数据的格式
if username == 'dream' and password == '521':
role = 'admin'
else:
role = 'normal'
pwd_data = create_data_type(tag='pwd', username=username, password=password, role=role)
bank_data = create_data_type(tag='pwd', username=username, bank_password='None', bank_id='0', balance='0')
# print(pwd_data) # dream|521|admin
# 【3】存储数据
# [3.1]用户名和密码
save_data(path='user_pwd.txt', data=pwd_data)
save_data(path='user_log.txt', data=f'当前用户 {username} 注册成功 | {time.strftime("%x %X")}')
# [3.2]银行信息
save_data(path='user_bank.txt', data=bank_data)
save_data(path='user_log.txt', data=f'当前用户 {username} 初始化银行卡成功 | {time.strftime("%x %X")}')
# print(bank_data)# dream|None|0|0
return True, f'{username} 注册成功!'
def init_bank_data(username):
user_bank_dict = read_data(tag='bank', path='user_bank.txt')
user_bank_data = user_bank_dict.get(username)
login_user_dict['bank_id'] = user_bank_data.get('bank_id')
login_user_dict['bank_pay_pwd'] = user_bank_data.get('pay_pwd')
login_user_dict['balance'] = user_bank_data.get('balance')
def login():
username, password = get_user_pwd()
user_pwd_dict = read_data(tag='pwd', path='user_pwd.txt')
user_data = user_pwd_dict.get(username)
if not user_data:
return False, '没有注册'
if password != user_data.get('password'):
return False, '密码失败!'
login_user_dict['username'] = username
login_user_dict['role'] = user_data.get('role')
init_bank_data(username)
save_data(path='user_log.txt', data=f'当前用户 {username} 登录成功 | {time.strftime("%x %X")}')
return True, f'用户 {username} 登陆成功'
def login_auth(func):
def inner(*args, **kwargs):
# print(func.__name__) # add_bank_info
# 【1】执行原函数的前提是已经登录过
if not login_user_dict['username']:
return False, f'必须先登录'
if not login_user_dict['bank_id'] and func.__name__ != 'add_bank_info':
return False, '必须先激活银行卡'
return func(*args, **kwargs)
return inner
@login_auth
def add_bank_info():
username = login_user_dict['username']
bank_pay_pwd = input("请输入支付密码 :>>>> ").strip()
bank_id = input("请输入银行卡号 :>>>> ").strip()
bank_data = create_data_type(tag='bank', username=username, bank_pay_pwd=bank_pay_pwd, bank_id=bank_id,
balance=1000)
save_data(path='user_bank.txt', data=bank_data)
init_bank_data(username)
print(login_user_dict)
return True, f'初始化银行信息完成!'
def input_money_info():
bank_pwd = input("请输入支付密码 :>>>> ").strip() # 确认是数字并且是三位
balance = input("请输入金额 :>>>> ").strip() # 余额是数字类型并且余额大于0小于你的余额
bank_id = input("请输入银行卡号 :>>>> ").strip() # 确认是数字并且是六位
return bank_pwd, balance, bank_id
# 在装饰器内部完成 --- 格式校验。位数校验。数字余额校验
def charge_auth(flag):
def wrapper(func):
def inner(*args, **kwargs):
if flag == 'charge':
bank_pwd, balance, bank_id = input_money_info()
if len(bank_pwd) != 3:
return False, f'支付密码必须是3位'
if not balance.isdigit():
return False, f'必须是数字'
if balance < '0' or balance > login_user_dict.get('balance'):
return False, f'输入金额格式错误'
if not bank_id.isdigit():
return False, f'必须是数字'
if len(bank_id) != 6:
return False, f'必须是六位'
result = func(*args, bank_pay_pwd=bank_pwd, bank_id=bank_id, balance=balance)
return result
if flag == 'recharge':
balance = input("请输入金额 :>>>> ").strip() # 余额是数字类型并且余额大于0小于你的余额
if not balance.isdigit():
return False, f'必须是数字'
if balance < '0':
return False, f'输入金额格式错误'
bank_id = input("请输入银行卡号 :>>>> ").strip() # 确认是数字并且是六位
if not bank_id.isdigit():
return False, f'必须是数字'
if len(bank_id) != 6:
return False, f'必须是六位'
result = func(*args, bank_id=bank_id, balance=balance)
return result
return inner
return wrapper
@login_auth
@charge_auth(flag='charge')
def charge(*args, **kwargs):
bank_info_dict = read_data('user_bank.txt', tag='bank')
# {'dream': {'password': '521', 'role': 'admin'}} 银行信息类似这种
bank_data_1 = bank_info_dict.get(login_user_dict.get('username'))
balance = int(bank_data_1.get('balance')) - int(kwargs.get('balance'))
bank_data = create_data_type(tag='bank', username=login_user_dict.get('username'),
bank_pay_pwd=kwargs.get('bank_pay_pwd'),
bank_id=kwargs.get('bank_id'), balance=balance)
save_data(path='user_bank.txt', data=bank_data)
save_data(path='user_log.txt', data=f"当前用户 {login_user_dict.get('username')} 取{kwargs.get('balance')}元子成功 | {time.strftime('%x %X')}")
return True, f"当前用户 {login_user_dict.get('username')} 取了{kwargs.get('balance')}元子"
@login_auth
@charge_auth(flag='recharge')
def recharge(*args, **kwargs):
user_bank_info = read_data('user_bank.txt', tag='bank')
bank_data_2 = user_bank_info.get(login_user_dict.get('username'))
balance = int(bank_data_2.get('balance')) + int(kwargs.get('balance'))
bank_data = create_data_type(tag='bank', username=login_user_dict.get('username'),
bank_pay_pwd=login_user_dict.get('bank_pay_pwd'),
bank_id=kwargs.get('bank_id'), balance=balance)
save_data(path='user_bank.txt', data=bank_data)
save_data(path='user_log.txt', data=f"当前用户 {login_user_dict.get('username')} 存{kwargs.get('balance')}元子成功 | {time.strftime('%x %X')}")
return True, f"当前用户 {login_user_dict.get('username')} 存了{kwargs.get('balance')}元子"
@login_auth
def check_flow():
with open('user_log.txt', 'r', encoding='utf8') as fp:
for line in fp:
print(line)
return True, f'打印成功'
@login_auth
def check_bank_info():
user_bank_info_dict = read_data('user_bank.txt', tag='bank')
bank_info_dict = user_bank_info_dict.get(login_user_dict.get('username'))
return True, f'''
姓名:{login_user_dict.get('username')}
支付密码:{bank_info_dict.get('pay_pwd')}
银行卡号:{bank_info_dict.get('bank_id')}
余额:{bank_info_dict.get('balance')}
'''
def money_auth(func):
def inner(*args, **kwargs):
bank_pwd, balance, bank_id = input_money_info()
target_bank_card_number = input('请输入目标银行卡号:').strip()
if target_bank_card_number == bank_id:
return False, f'不能转账给相同卡号'
if len(bank_pwd) != 3:
return False, f'支付密码必须是3位'
if not balance.isdigit():
return False, f'必须是数字'
if balance < '0' or balance > login_user_dict.get('balance'):
return False, f'输入金额格式错误'
if not bank_id.isdigit():
return False, f'必须是数字'
if len(bank_id) != 6:
return False, f'必须是六位'
result = func(*args, balance=balance, target_bank_card_number=target_bank_card_number)
return result
return inner
@login_auth
@money_auth
def transfer(*args, **kwargs):
user_bank_info = read_data('user_bank.txt', tag='bank')
bank_data = user_bank_info.get(login_user_dict.get('username'))
balance = int(bank_data.get('balance')) - int(kwargs.get('balance'))
bank_data = create_data_type(tag='bank', username=login_user_dict.get('username'),
bank_pay_pwd=login_user_dict.get('bank_pay_pwd'),
bank_id=kwargs.get('bank_id'), balance=balance)
save_data(path='user_bank.txt', data=bank_data)
save_data(path='user_log.txt',
data=(f"当前用户 {login_user_dict.get('username')} 给{kwargs.get('target_bank_card_number')}转了{kwargs.get('balance')}元子 | {time.strftime('%x %X')}"))
return True,f'转账成功'
def over():
return False, f'谢谢使用,欢迎下次光临'
func_menu = '''
===================用户功能菜单=====================
1.注册
2.登陆
3.初始化银行信息
4.取款
5.存款
6.查看流水
7.查看银行信息(查看自己的卡号、余额、流水等信息)
8.转账
9.退出
======================欢迎使用=======================
'''
func_dict = {
'1': register,
'2': login,
'3': add_bank_info,
'4': charge,
'5': recharge,
'6': check_flow,
'7': check_bank_info,
'8': transfer,
'9': over,
}
def main():
while True:
print(func_menu)
func_id = input("功能ID :>>>> ").strip()
if func_id not in func_dict.keys():
print(f"不存在")
continue
func = func_dict[func_id]
flag, msg = func()
if flag:
print(msg)
else:
print(msg)
break
continue
main()
标签:坤坤机,get,ATM,器版,dict,user,balance,data,bank
From: https://www.cnblogs.com/xiao01/p/17904235.html