php编写atm系统,ATM(示例代码)

编写ATM程序实现下述功能,数据来源于文件db.txt

# coding:utf-8

user_info = {

'username':None

}

import os

BASE_PATH = os.path.dirname(__file__)

DB_PATH = os.path.join(BASE_PATH, 'db')

def save(user_name, user_write):

'''保存用户'''

user_path = os.path.join(DB_PATH, f'{user_name}.txt')

with open(f'{user_path}', 'w', encoding='utf-8')as f:

f.write(user_write)

def select(user_name):

'''查看用户'''

user_path = os.path.join(DB_PATH, f'{user_name}.txt')

if os.path.exists(user_path):

with open(f'{user_path}', 'r', encoding='utf-8')as f:

user_info = f.read().strip().split(':')

return True, user_info

else:

return False, '用户不存在'

def login():

'''用户登录'''

user_name = input('请输入用户名:').strip()

flag, msg = select(user_name)

if flag:

count = int(msg[3])

while True:

if count == 3:

res = f'{msg[0]}:{msg[1]}:{msg[2]}:{count}'

save(user_name, res)

print('该用户已被冻结!')

break

user_pwd = input('请输入密码:').strip()

if user_pwd == msg[1]:

user_info['username'] = user_name

print('登录成功!')

break

else:

print('密码错误!')

count += 1

# print(count)

else:

print(msg, '请先注册')

def check_balance():

'''查看余额'''

if user_info['username']:

username = user_info['username']

flag, msg = select(username)

print(f'{username}余额为:{msg[2]}')

else:

print('用户没有登录,请先登录!')

login()

def withdraw():

'''提现功能'''

if user_info['username']:

flag, msg = select(user_info['username'])

balance = msg[2]

balance = int(balance)

print(balance,type(balance))

while True:

with_money = input('请输入提现金额:').strip()

with_money = int(with_money)

if balance >= with_money:

balance -= with_money

res = f'{msg[0]}:{msg[1]}:{balance}:{msg[3]}'

save(user_info['username'], res)

print('提现成功')

break

else:

print('余额不足')

else:

print('用户没有登录,请先登录')

login()

def transfer():

'''转账功能'''

tag = True

if user_info['username']:

while tag:

to_user = input('请输入转账用户:').strip()

flag, msg = select(to_user)

if flag:

while True:

trans_money = input('请输入转账金额:').strip()

trans_money = int(trans_money)

flag1, msg1 = select(user_info['username'])

balance = int(msg1[2]) #转账用户余额

to_balance = int(msg[2]) #收账用户余额

if balance >= trans_money:

balance -= trans_money

res1 = f'{msg1[0]}:{msg1[1]}:{balance}:{msg1[3]}'

save(user_info['username'], res1)

to_balance += trans_money

res2 = f'{msg[0]}:{msg[1]}:{to_balance}:{msg[3]}'

save(to_user, res2)

tag = False

break

else:

print('余额不足')

else:

print('转账用户不存在')

else:

print('用户未登录,请先登录!')

login()

def recharge():

'''充值功能'''

if user_info['username']:

recharge_money = input('请输入充值金额:').strip()

recharge_money = int(recharge_money)

flag, msg = select(user_info['username'])

balance = int(msg[2])

balance += recharge_money

res = f'{msg[0]}:{msg[1]}:{balance}:{msg[3]}'

save(user_info['username'], res)

else:

print('用户未登录,请先登录')

login()

def logout():

if user_info['username']:

user_info['username']=None

def register():

while True:

user_name = input('请输入注册用户名:').strip()

flag, msg = select(user_name)

if flag:

print('该用户已存在!')

continue

else:

user_pwd = input('请输入密码:').strip()

user_info = f'{user_name}:{user_pwd}:1000:0'

save(user_name, user_info)

print('注册成功')

break

#功能字典

func_dic = {

'1':('登录',login),

'2':('查看余额',check_balance),

'3':('提现', withdraw),

'4':('转账', transfer),

'5':('充值', recharge),

'6':('注册', register),

'7':('退出登录', logout),

'q':('退出程序', exit)

}

def run():

'''主运行函数'''

while True:

print('=====function=======')

for i in func_dic:

print(i, func_dic[i][0])

print('=========end========')

choice = input('请输入功能编号:').strip()

if choice == 'q':

break

elif not choice.isdigit():

print('请输入数字!')

continue

elif choice not in func_dic:

print('请输入正确的功能编号')

continue

else:

func_dic.get(choice)[1]()

if __name__ == '__main__':

run()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值