1、 编写一个银行账户管理系统
时间: 2024-01-15 15:04:48 浏览: 69
抱歉,我是一名语言模型,无法编写实际的程序。以下是一个简单的银行账户管理系统的伪代码,供您参考:
定义一个Account类:
- 属性:账户号、账户余额
- 方法:存款、取款、查询余额
定义一个Bank类:
- 属性:账户列表
- 方法:添加账户、删除账户、查询账户、显示所有账户信息
主程序:
- 创建Bank对象
- 显示菜单,提供以下选项:
1. 添加账户
2. 删除账户
3. 查询账户
4. 显示所有账户信息
5. 退出程序
- 根据用户选择执行相应操作,直到用户选择退出程序
示例代码:
class Account:
def __init__(self, account_number, balance):
self.account_number = account_number
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
else:
print("余额不足!")
def get_balance(self):
return self.balance
class Bank:
def __init__(self):
self.accounts = []
def add_account(self, account):
self.accounts.append(account)
def remove_account(self, account_number):
for account in self.accounts:
if account.account_number == account_number:
self.accounts.remove(account)
break
def get_account(self, account_number):
for account in self.accounts:
if account.account_number == account_number:
return account
return None
def display_accounts(self):
for account in self.accounts:
print("账户号:{},余额:{}".format(account.account_number, account.balance))
bank = Bank()
while True:
print("请选择操作:")
print("1. 添加账户")
print("2. 删除账户")
print("3. 查询账户")
print("4. 显示所有账户信息")
print("5. 退出程序")
choice = input()
if choice == "1":
account_number = input("请输入账户号:")
balance = float(input("请输入账户余额:"))
account = Account(account_number, balance)
bank.add_account(account)
print("账户添加成功!")
elif choice == "2":
account_number = input("请输入要删除的账户号:")
bank.remove_account(account_number)
print("账户删除成功!")
elif choice == "3":
account_number = input("请输入要查询的账户号:")
account = bank.get_account(account_number)
if account:
print("账户余额为:{}".format(account.get_balance()))
else:
print("账户不存在!")
elif choice == "4":
bank.display_accounts()
elif choice == "5":
print("程序已退出。")
break
else:
print("无效的选择!")
阅读全文