标签:project strong none lse imp odi split user ram
# :编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额
# :登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
import os
user_staus = {‘username‘: None}
def ad_credit(username, amount):
"""
充值功能
:param username:
:param amount:
:return:
"""
if user_staus[‘username‘] is None:
login()
with open(‘db.txt‘, ‘rt‘, encoding=‘utf-8‘)as f1, \
open(‘db.txt.swap‘, ‘wt‘, encoding=‘utf-8‘)as f2:
while True:
cont = f1.readline()
if len(cont) == 0:
break
name, remain = cont.strip().split(‘:‘)
if username in name:
remain = int(remain) + int(amount)
f2.write(‘{}:{}\n‘.format(name, remain))
print(‘充值成功,{}的余额为{}‘.format(username, remain))
else:
f2.write(cont)
os.remove(‘db.txt‘)
os.rename(‘db.txt.swap‘, ‘db.txt‘)
def transfer(user_out, user_in, amount):
‘‘‘3
转账功能
:param user_out:
:param user_in:
:param amount:
:return:
‘‘‘
if user_staus[‘username‘] is None:
login()
with open(‘db.txt‘, ‘rt‘, encoding=‘utf-8‘)as f1, \
open(‘db.txt.swap‘, ‘wt‘, encoding=‘utf-8‘)as f2:
userinfo = {}
for line in f1:
name, remind = line.strip().split(‘:‘)
userinfo[name] = int(remind)
if user_out not in userinfo:
print(‘用户不存在‘)
return
if user_in not in userinfo:
print(‘收款方不存在‘)
return
if user_out in userinfo and user_in in userinfo:
if userinfo[user_out] >= int(amount):
userinfo[user_out] -= int(amount)
userinfo[user_in] += int(amount)
print(‘转账成功,已成功从{}向{}汇款{}‘.format(user_out, user_in, amount))
elif userinfo[user_out] < amount:
print(‘余额不足‘)
return
for name, remind in userinfo.items():
f2.write(‘{}:{}\n‘.format(name, remind))
os.remove(‘db.txt‘)
os.rename(‘db.txt.swap‘, ‘db.txt‘)
def cashon(username, amount):
‘‘‘
提现功能
:param username:
:param amount:
:return:
‘‘‘
if user_staus[‘username‘] is None:
login()
with open(‘db.txt‘, ‘rt‘, encoding=‘utf-8‘)as f1, \
open(‘db.txt.swap‘, ‘wt‘, encoding=‘utf-8‘)as f2:
userinfo = {}
for line in f1:
name, remind = line.strip().split(‘:‘)
userinfo[name] = int(remind)
if username not in userinfo:
print(‘用户不存在‘)
return
if username in userinfo and userinfo[username] >= int(amount):
userinfo[username] -= int(amount)
print(‘已从余额中取出{},现余额为{}‘.format(amount, userinfo[username]))
elif userinfo[username] < amount:
print(‘余额不足,提现失败‘)
return
for name, remind in userinfo.items():
f2.write(‘{}:{}\n‘.format(name, remind))
os.remove(‘db.txt‘)
os.rename(‘db.txt.swap‘, ‘db.txt‘)
def check(username):
‘‘‘
余额查询功能
:param username:
:return:
‘‘‘
if user_staus[‘username‘] is None:
login()
with open(‘db.txt‘, ‘rt‘, encoding=‘utf-8‘)as f:
userinfo = {}
for line in f:
name, remind = line.strip().split(‘:‘)
userinfo[name] = remind
if username not in userinfo:
print(‘用户不存在‘)
return
if username in userinfo:
print(‘当前余额为:{}‘.format(userinfo[username]))
def login():
username = input(‘输入用户名‘)
userpassword = input(‘输入密码‘)
with open(‘login.txt‘, ‘rt‘, encoding=‘utf-8‘)as login_f:
login = {}
for line in login_f:
name, psd = line.strip().split(‘:‘)
login[name] = psd
if username in login:
if login[username] == userpassword:
print(‘登陆成功‘)
user_staus[‘username‘] = username
break
elif username not in login:
print(‘用户名不存在‘)
return
def logout():
user_staus[‘username‘] = None
print(‘已成功登出‘)
return
login()
tag = True
while tag:
cmd = input(‘‘‘
请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
‘‘‘)
if cmd == ‘1‘:
username = input(‘输入用户名:‘)
amount = input(‘输入充值金额:‘)
ad_credit(username, amount)
elif cmd == ‘2‘:
user_out = input(‘请输入转账方:‘)
user_in = input(‘请输入接收方:‘)
amount = input(‘输入转账金额:‘)
transfer(user_out, user_in, amount)
elif cmd == ‘3‘:
username = input(‘输入用户名:‘)
amount = input(‘输入提现金额‘)
cashon(username, amount)
elif cmd == ‘4‘:
username = input(‘输入用户名:‘)
check(username)
elif cmd == ‘0‘:
logout()
tag = False
else:
print(‘请正确输入序号‘)
————————————————————————以下是模拟结果————————————————————————————————————————————
‘‘‘
/Users/chenfeng/PycharmProjects/ATM/venv/bin/python /Users/chenfeng/PycharmProjects/ATM/main.py
输入用户名xilou
输入密码666
登陆成功
请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
1
输入用户名:xilou
输入充值金额:200
充值成功,xilou的余额为700
请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
2
请输入转账方:xilou
请输入接收方:heiren
输入转账金额:200
转账成功,已成功从xilou向heiren汇款200
请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
3
输入用户名:xilou
输入提现金额100
已从余额中取出100,现余额为400
请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
4
输入用户名:xilou
当前余额为:400
请输入你想使用的功能序号
1:充值
2:转账
3:提现
4:查询余额
0:登出
0
已成功登出
Process finished with exit code 0
‘‘‘
标签:project strong none lse imp odi split user ram
原文地址:https://www.cnblogs.com/heirenxilou/p/12521102.html