ATM 1.0

Author: Zeno

date:2024/4/13

time:9:42

Author: Zeno

date:2024/4/13

time:8:00

注册 : 存储到文件中 用户名 - 登录密码 - 年龄 - 银行卡号(1314) - 取款密码 - 余额(1000)

登录 : 直接将用户信息从文件中取出,然后进行比对 用户名 - 密码

取款 : 验证你的取款密码,更改余额 余额(1000) ,记录你的提款信息 -- 文件里 - 加时间

转账 : 验证你的取款密码,更改余额 目标银行卡号去转 记录你的提款信息 -- 文件里

import time
import random
data_dict = {}
option_dict = {}
log_dict = {}
use_dict = {'count': 0, "countmax": 3}
print(time.time())
use_dict['count'] += 1

optime = time.strftime('%Y-%m-%d %X', time.localtime())

辅助函数

数据姓名,密码并校验

def getnp(func=None):
while use_dict['count'] < use_dict['countmax']:
name = input(f'alpha and num mixed len==6,please input your {func} name 😂.strip()
pwd = input(f'please input your {func} password:').strip()
if name.isalnum() and len(name) == 6 and pwd.isdigit() and len(pwd) == 3:
use_dict['count'] = 0
return name, pwd
elif use_dict['count'] == 3:
use_dict['count'] = 0
return None,f'password or account format wrong more than 3 times'

其它输入框

def inputbox(fun, tp=None):
if tp == None:
name = input(f'please input your {fun}😂.strip()
return True, name
elif tp == 'num':
while use_dict['count'] < use_dict['countmax']:
use_dict['count'] += 1
name = input(f'please input your {fun}😂
if name.isdigit():
use_dict['count'] = 0

            return True, name
        if use_dict['count'] == 3:
            use_dict['count'] = 0
            return False, f'{fun} need to be numbers only!'

判断是否存在

def isin(input, data):
for input in data:
return True, input
else:
return False, f'{input} not in {data}!'

读取数据

def read(file,encoding='utf8'):
if file =='atmdata.txt':
try:
with open(file, 'r', encoding='utf8') as f:
data = f.readlines()
datalist = [line.strip().split('--') for line in data]
if datalist:
for i in datalist:
if len(i) == 7:
name, lgpwd, age, cardnum, moneypwd, moneyleft, optime = i
data_dict[name] = {'name': name, "loging_password": lgpwd,
'age': age, 'card_number': cardnum,
'money_password': moneypwd, 'money_left': moneyleft, 'optime': optime}
return True,data_dict

    except Exception as e:
        print(e)
        return False,{}
if file =='optiondict.txt':
    try:
        with open(file, 'r', encoding='utf8') as f:
            data = f.readlines()
        datalist = [line.strip().split('--') for line in data]
        if datalist:
            for i in datalist:
                if len(i) == 6:
                    name, item,value, who, left, optime = i
                    option_dict[f'{name}  {optime}'] = {'name': name, 'item':item,"value": value,
                                                        'to who': who, 'money_left': left, 'optime': optime}
        return True, option_dict
    except Exception as e:
        print(e)
        return False, {}

a,b = read('atmdata.txt')

print(a,b)

c,d = read('optiondict.txt')

print(c,d)

数据写入文件

file = 'atmdata.txt'

print(read(file))

def write(mode, data, file='atmdata.txt'):
if mode =='w':
dct_list = [list(i.values()) for i in data.values()]
dct_str = '\n'.join('--'.join(i) for i in dct_list)+'\n'
with open(file, mode, encoding='utf8') as f:
f.write(dct_str)
return True,dct_str
if mode =='a':
dct_stra = '--'.join(list(data.values())) + '\n'
with open(f'{file}', f'{mode}', encoding='utf8') as f:
f.write(dct_stra)
return True,dct_stra

write('w',data_dict)

print(write('w',data_dict))

计算剩余金额

def restcal(outm, orgm=None):
rest = int(orgm) - int(outm)
if rest < 0:
return False, f'{orgm} not enough!'
else:
return True, str(rest)

校验密码是否正确

def verification(input, exist):
if input == exist:
return True, f'password right!'
else:
return False, f'wrong password'

功能函数

def regs(name, pwd):

age = input('please input your age: ')
cardnum = str(random.randint(66666666, 99999999))
moneypwd = input('please input your money_password:').strip()
money = input('please input your money_left:').strip()
data_dict_a = {'name': name, "loging_password": pwd,
                   'age': age, 'card_number': cardnum,
                   'money_password': moneypwd, 'money_left': money, 'optime': time.strftime('%Y-%m-%d %X', time.localtime())}
write('a', data_dict_a)
print(f'user {name} registered successfully!')

注册

def register():
name, pwd = getnp("register")
if name:
readtag, data_dict = read('atmdata.txt')
if readtag:
if name in data_dict:
print(f'name {name} already exist,please login!')
login()
else:
regs(name, pwd)
else:
regs(name, pwd)
else:
print(pwd)

登录

def login(func=None):
name, pwd = getnp('login')
if name:
readtag, data_dict = read('atmdata.txt')
if readtag:
if name in data_dict:
while use_dict['count'] < 3:
use_dict['countmax'] += 1
if pwd== data_dict[name]['loging_password']:
print(f'{name},login successfully!')
data_dict[name]['optime'] = time.strftime('%Y-%m-%d %X', time.localtime())
write('w',data_dict)
log_dict['name'] = name
use_dict['count'] = 0
if func:
func(name=name)
else:
return True, name
break
else:
print(f"{name},you have {use_dict['countmax'] - use_dict['count']} time left:")
pwd = input('please input your loging_password:😂
if use_dict['count'] == 3:
use_dict['count'] = 0
else:
print(f'user {name} not register yet2, please register first!')
register()
else:
print(f'user {name} not register yet1, please register first!')
register()
else:
print(pwd)

取款

def withdraw(name=None):
if not log_dict and not name:
print(f'please login first!')
login(withdraw)
else:
namep = log_dict['name'] or name
monpwd = input('please input your money_password:')
if monpwd == data_dict[namep]['money_password']:
while use_dict['count'] < use_dict['countmax']:
use_dict['count'] += 1
md = input(f'please input how much money want to withdraw:')
if md:
tgc, msgc = restcal(md, data_dict[namep]['money_left'])
if tgc:
data_dict[namep]['money_left'] = msgc
use_dict['count'] = 0
print(f'withdraw {md},please take care')
print(read('optiondict.txt'))

                    data_dict[namep]['money_left'] = str(msgc)
                    write('w',data_dict,file='atmdata.txt')
                    option_dict_a = {'name': namep,'item':'withdraw', "value": md,
                                                    'to who': "None", 'money_left': str(msgc), 'optime': time.strftime('%Y-%m-%d %X', time.localtime())}
                    write('a',option_dict_a,file='optiondict.txt')
                    # option_dict[f'{namep} {optime}'] = {'name': namep, 'item': 'withdraw', "value": md,
                    #                                     'to who': "None", 'money_left': str(msgc), 'optime': optime}

                    break
                else:
                    print(f'money not enough!try again')
            else:
                print(md)
            if use_dict['count'] == 3:
                use_dict['count'] = 0
    else:
        print('wrong pass word for money')

转账

def transfer(name=None):
if not log_dict and not name:
print(f'please login first!')
login(transfer)
else:
namep = log_dict['name'] or name
monpwd = input('please input money_password:')
if int(monpwd):
if monpwd == data_dict[namep]['money_password']:
while use_dict['count'] < use_dict['countmax']:
use_dict['count'] += 1
mtname = input(f'please input the name transfer to:')
if mtname in data_dict:
mtnum = input(f'please input money want to transfer:')
if mtnum.isdigit:
tgl, rst = restcal(mtnum, data_dict[namep]['money_left'])
if tgl:
getm = int(data_dict[mtname]['money_left']) + int(rst)
print(getm)
print(f'you have transfer {mtnum} to {mtname},please contact!')
data_dict[namep]['money_left'],data_dict[namep]['optime'] = rst,time.strftime('%Y-%m-%d %X', time.localtime())
data_dict[mtname]['money_left'] ,data_dict[mtname]['optime']= str(getm),time.strftime('%Y-%m-%d %X', time.localtime())
write('w',data_dict,file='atmdata.txt')
option_dict_a = {'name': namep,'item':'transfer', "value": f'-{mtnum}',
'to who': mtname, 'money_left': rst,
'optime': time.strftime('%Y-%m-%d %X', time.localtime())}
write('a', option_dict_a, file='optiondict.txt')
#
# write('a', data_dict, file='atmdata.txt')
option_dict_a = {'name': mtname,'item':'gettransfer', "value": f'+{mtnum}',
'from who': namep, 'money_left': str(getm),
'optime': time.strftime('%Y-%m-%d %X', time.localtime())}
# print(option_dict)
write('a', option_dict_a,file='optiondict.txt')
print(f'{mtnum} money transfer to {mtname} success ')
use_dict['count'] = 0
break
else:
print('balance not enough!')
else:
print(mtnum)
else:
print(f'{mtname} is not found!')
if use_dict['count'] == 3:
use_dict['count'] = 0
else:
print('wrong money password!')
else:
print(f'wrong format!')

定义全局变量

主函数

def main():
atm_menu = '''-------------Welcome to ATM--------------
1:register
2:login
3:withdraw
4:transfer
5:quit
------------------END-------------------'''
menu_dict = {'1': register, '2': login, '3': withdraw, '4': transfer}
print(atm_menu)
tag = True
while tag:

    menu_choice = input('please input your choice from above menu:').strip()
    if menu_choice == 'q':
        break
    if menu_choice in menu_dict:
        use_dict["count"] = 0
        print(f'*********welcome to {menu_dict[menu_choice].__name__}********')
        menu_dict[menu_choice]()
    else:
        print(f"wrong input,please try again,you have {use_dict['countmax'] - use_dict['count']} chance left!")

main()

posted @ 2024-04-15 08:59  zenopan  阅读(5)  评论(0编辑  收藏  举报