python atm作业_python学习之ATM功能实现

# :编写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

‘‘‘

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python可以通过面向对象编程来实现ATM,以下是一个简单的实现方式: 1. 创建一个账户类(Account),用于存储账户信息、余额和交易记录等。 2. 创建一个ATM类(ATM),用于处理用户的交易请求,包括取款、存款、查询余额等操作。 3. 在ATM类中,需要实现用户认证、账户锁定、密码重置等功能。 4. 在主函数中,实例化ATM类对象,并提供一个简单的控制台界面,让用户选择不同的操作。 以下是一个简单的示例代码: ``` class Account: def __init__(self, name, account_number, password, balance=0): self.name = name self.account_number = account_number self.password = password self.balance = balance self.transaction_history = [] def deposit(self, amount): self.balance += amount self.transaction_history.append(f"Deposit: {amount}") def withdraw(self, amount): if amount > self.balance: return "Insufficient funds" else: self.balance -= amount self.transaction_history.append(f"Withdrawal: {amount}") def get_balance(self): return self.balance def get_transaction_history(self): return self.transaction_history class ATM: def __init__(self): self.accounts = [] def create_account(self, name, account_number, password): new_account = Account(name, account_number, password) self.accounts.append(new_account) def authenticate(self, account_number, password): for account in self.accounts: if account.account_number == account_number and account.password == password: return account return None def reset_password(self, account_number, new_password): account = self.authenticate(account_number, "") if account: account.password = new_password return True else: return False def lock_account(self, account_number): account = self.authenticate(account_number, "") if account: account.password = "" return True else: return False def unlock_account(self, account_number, password): account = self.authenticate(account_number, password) if account: return True else: return False def deposit(self, account_number, amount): account = self.authenticate(account_number, "") if account: account.deposit(amount) return True else: return False def withdraw(self, account_number, amount): account = self.authenticate(account_number, "") if account: result = account.withdraw(amount) if result == "Insufficient funds": return False else: return True else: return False def main(): atm = ATM() while True: print("1. Create Account") print("2. Login") print("3. Reset Password") print("4. Lock Account") print("5. Unlock Account") print("6. Deposit") print("7. Withdraw") print("8. Check Balance") print("9. Transaction History") print("0. Exit") choice = input("Enter choice: ") if choice == "1": name = input("Enter name: ") account_number = input("Enter account number: ") password = input("Enter password: ") atm.create_account(name, account_number, password) print("Account created successfully") elif choice == "2": account_number = input("Enter account number: ") password = input("Enter password: ") account = atm.authenticate(account_number, password) if account: while True: print("1. Reset Password") print("2. Lock Account") print("3. Unlock Account") print("4. Deposit") print("5. Withdraw") print("6. Check Balance") print("7. Transaction History") print("0. Exit") choice = input("Enter choice: ") if choice == "1": new_password = input("Enter new password: ") result = atm.reset_password(account_number, new_password) if result: print("Password reset successful") else: print("Account not found") elif choice == "2": result = atm.lock_account(account_number) if result: print("Account locked successfully") else: print("Account not found") elif choice == "3": password = input("Enter password: ") result = atm.unlock_account(account_number, password) if result: print("Account unlocked successfully") else: print("Invalid password") elif choice == "4": amount = int(input("Enter amount: ")) result = atm.deposit(account_number, amount) if result: print("Deposit successful") else: print("Account not found") elif choice == "5": amount = int(input("Enter amount: ")) result = atm.withdraw(account_number, amount) if result: print("Withdrawal successful") else: print("Insufficient funds or account not found") elif choice == "6": balance = account.get_balance() print(f"Balance: {balance}") elif choice == "7": transaction_history = account.get_transaction_history() for transaction in transaction_history: print(transaction) elif choice == "0": break else: print("Invalid choice") else: print("Invalid credentials") elif choice == "0": break else: print("Invalid choice") if __name__ == "__main__": main() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值