Author: Zeno
date:2024/4/16
time:11:51
【1】註冊 :
(1)儲存到檔案中
(2)儲存資料格式:使用者名稱 - 登入密碼 - 年齡 - 銀行卡號(1314) - 取款密碼 - 餘額(1000) 預設餘額 1000
(3)校驗使用者名稱存在不允許註冊
(4)校驗密碼,必須是 6 位 ,並且字母和資料混合 ---> 提高資料的安全等級
(5)取款密碼,必須是數字且 是三位
【2】登入 :
(1)直接將使用者資訊從檔案中取出
(2)對使用者進行核對,使用者存在的情況下允許登入,否則不允許登入
(3)登入的時候的還是要校驗密碼 : 必須是 6 位 ,並且字母和資料混合 ---> 提高資料的安全等級
3 取款
4 轉賬
5 檢視資訊
6 檢視日誌
總要求:每次操作都有日誌記錄,每個使用者日誌資料分開記錄
函式多用return少用else
import time, random, re, datetime
user_dict = {}
card_dict = {}
login_now = ''
admin_code = 'qaz123'
def read(file, tag='user'):
if tag == 'user':
try:
with open(file, 'r', encoding='utf8') as f:
data = f.readlines()
for line in data:
data_list = line.strip().split('--')
name, pwd, born, balance, card_num, role, withdraw_password, locked, create_time = data_list
user_dict[name] = {'name': name, 'login_password': pwd, 'borned': born, 'balance': balance,
'card_num': card_num,
'role': role, 'withdraw_password': withdraw_password, 'locked': locked,
'create_time': create_time}
return True, user_dict
except:
return False, {}
if tag == 'diary':
try:
with open(file, 'r', encoding='utf8') as f:
data_str = f.readlines()
return True, data_str
except:
return False, ''
print(read('ATMuser.txt'))
def write(data, file, mode):
if mode == 'a':
with open(file, mode, encoding='utf8') as f:
data_str = '--'.join(i for i in list(data.values())) + '\n'
f.write(data_str)
elif mode == 'w':
with open(file, mode, encoding='utf8') as f:
data_list = [list(i.values()) for i in data.values()]
data_str = '\n'.join('--'.join(i) for i in data_list) + '\n'
f.write(data_str)
else:
with open(file, 'a', encoding='utf8') as f:
f.write(data)
def name_pass():
for i in range(3):
name = input(f'please input your username:').strip()
pwd = input(f'please input your password:').strip()
if len(name) < 3 or len(name) > 8:
print(f'''the length of username should be 3-8,
you have {2 - i} chances left, please try again:''')
continue
elif not re.match('(?😦?=.[a-zA-Z])(?=.[0-9_]))', name):
print(f'''the format of username should be mixed with alpha and numbers or ,
you have {2 - i} chances left, please try again:''')
continue
if len(pwd) != 6:
print(f'''the length of password should be 6,
you have {2 - i} chances left, please try again:''')
continue
elif not re.match(r'^\d{6}$', pwd):
print(f'''the format password should be 6 number,
you have {2 - i} chances left, please try again:''')
continue
if re.match('(?😦?=.[a-zA-Z])(?=.[0-9]))', name) and re.match(r'^\d{6}$', pwd):
return name, pwd
else:
print(f'failed username or password!')
return False, f'name and password input failed!'
name_pass()
def check_args(x, tag='num'):
if tag == 'num':
if re.match(r'[1]\d{1}\d{1,10}$', x):
return True, re.match(r'[2]\d{1}\d{1,10}$', x).group()
else:
return False, f'input should be number:'
if tag == 'code':
if re.match('(?😦?=.[A-Za-z])(?=.[0-9_-]))', x) and len(x) == 6:
return True, re.match('(?😦?=.[A-Za-z])(?=.[0-9_-]))', x).group()
else:
return False, f'input should be number:'
def register(func=None):
name, pwd = name_pass()
read('ATMuser.txt')
if not name:
return False, f'register failed for wrong input'
if name not in user_dict:
born = input('please input the year your're born:').strip()
if not born.isdigit() or (born.isdigit() and (int(born) > datetime.date.today().year or int(born) < 1900)):
return False, f'please input the right born year:1900-{datetime.date.today().year}'
balance = input('please input the money you save:').strip()
tag_balance, balance = check_args(balance)
if not tag_balance:
return balance
while True:
card_num = random.randint(666666, 999999)
if card_num not in card_dict.values():
break
role = 'admin' if input('please input the admin signal:').strip() == admin_code else 'normal'
withdraw_password = input('please input your withdraw password:').strip()
create_time = time.strftime('%Y-%m-%d %X', time.localtime())
user_dict[name] = {'name': name, 'login_password': pwd, 'borned': born, 'balance': balance,
'card_num': str(card_num),
'role': role, 'withdraw_password': withdraw_password, 'locked': 'normal',
'create_time': create_time}
write(user_dict[name], 'ATMuser.txt', 'a')
# print(f'user {name} registered successfully!')
if func:
func()
else:
return True, f'user {name} registered successfully!'
else:
return False, f'''user{name} already exist,can't be register covered.'''
def login(func=None):
log_name, log_pass = name_pass()
read('ATMuser.txt')
if not log_name:
return False, f'not legal username and password got!'
elif log_name not in user_dict:
print(f'user {log_name} not registered yet,please register first!')
register(login)
else:
if log_pass == user_dict[log_name]['login_password']:
print(f'user {log_name} login successfully')
global login_now
login_now = log_name
if func:
func()
else:
return True, f'user {log_name} login successfully'
else:
return False, f'login password not match!'
login()
def withdraw():
if not login_now:
print(f'please login first!')
login(withdraw)
else:
withdraw_password = input('please input your withdraw password:').strip()
# tag_with_pass, withdraw_password = check_args(withdraw_password, tag='code')
# if not tag_with_pass:
# return False, withdraw_password
if withdraw_password != user_dict[login_now]['withdraw_password']:
return False, f'withdraw password not match!'
else:
amount = input('please input the amount you want to withdraw:').strip()
# tag_amount, msg_amount = check_args(amount)
# if not tag_amount:
# return False, f'wrong input of amount!'
if int(amount) > int(user_dict[login_now]['balance']):
return False, f'not enough balance'
else:
user_dict[login_now]['balance'] = str(int(user_dict[login_now]['balance']) - int(amount))
write(user_dict, 'ATMuser.txt', 'w')
diary = f'''user {login_now} have withdrawed {amount} yuan at {time.strftime('%Y-%m-%d %X', time.localtime())},current balance :{user_dict[login_now]['balance']} yuan !\n'''
print(diary)
write(diary, f'{login_now}'s diary.txt', 'diary')
return True, diary
def transfer():
if not login_now:
print(f'please login first!')
login(transfer)
else:
withdraw_password = input('please input your withdraw password:').strip()
# tag_with_pass, withdraw_password = check_args(withdraw_password, tag='code')
# if not tag_with_pass:
# return False, withdraw_password
if withdraw_password != user_dict[login_now]['withdraw_password']:
print(withdraw_password,user_dict[login_now]['withdraw_password'])
return False, f'withdraw password not match'
else:
name_trans = input('please input the name you want to transfer to:').strip()
card_trans = input('please input the card_number:').strip()
if name_trans not in user_dict:
return False, f'user {name_trans} not found!'
elif card_trans != user_dict[name_trans]['card_num']:
return False, f' username and card number not match!'
else:
amount = input('please input the amount you want to transfer:').strip()
tag_amount, msg_amount = check_args(amount)
if not tag_amount:
return False, f'wrong input of amount!'
elif int(amount) > int(user_dict[login_now]['balance']):
return False, f'not enough balance'
else:
user_dict[login_now]['balance'] = str(int(user_dict[login_now]['balance']) - int(amount))
user_dict[name_trans]['balance'] = str(int(user_dict[name_trans]['balance']) + int(amount))
write(user_dict, 'ATMuser.txt', 'w')
diary_out = f'''user {login_now} have transfer {amount} yuan to {name_trans} at {time.strftime('%Y-%m-%d %X', time.localtime())},current balance :{user_dict[login_now]['balance']} yuan!\n'''
diary_in = f'''user {name_trans} have got transfer {amount} yuan from {login_now} at {time.strftime('%Y-%m-%d %X', time.localtime())},current balance :{user_dict[name_trans]['balance']} yuan!\n'''
# print(diary_out)
write(diary_out, f'{login_now}'s diary.txt', 'diary')
write(diary_in, f'{name_trans}'s diary.txt', 'diary')
return True, diary_out
def information_check():
if not login_now:
print(f'please login first!')
login(information_check)
else:
print(f'''--------------Dear User {login_now} information------------------''')
for k, v in user_dict[login_now].items():
print(f' {k}:{v} ')
print(f'''-------------------------The END-----------------------------------''')
return True,True
def diary_check():
if not login_now:
print(f'please login first!')
login(diary_check)
else:
data = read(f'{login_now}'s diary.txt', tag='diary')
for i in data:
print(i)
return True,read(f'{login_now}'s diary.txt', tag='diary')
def main():
func_dict = {'1': register, '2': login, '3': withdraw, '4': transfer, '5': information_check, '6': diary_check}
print(f'''-----------------Welcome to ATM-------------------''')
for k, v in func_dict.items():
print(f' {k}:{v.name} ')
print('-------------------------END---------------------------')
for i in range(3):
choice = input('please input your choice:').strip()
if choice == 'q' or choice == "Q":
break
if choice in func_dict:
print(f'''------Welcome to function {func_dict[choice].name}------''')
read('ATMuser.txt')
tag,msg = func_dictchoice
print(msg)
else:
print(f'this is your {i + 1} wrong choice,you have {2 - i} chance left,please input again:')
main()
1-9 ↩︎
1-9 ↩︎