用python实现银行管理系统
时间: 2023-09-19 07:08:22 浏览: 41
好的,下面是一个简单的银行管理系统的 Python 代码示例:
```python
class Account:
def __init__(self, name, balance):
self.name = name
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance < amount:
print("Withdrawal not possible, insufficient balance.")
else:
self.balance -= amount
def view_balance(self):
print("Current balance: ", self.balance)
class Bank:
def __init__(self):
self.accounts = []
def add_account(self, account):
self.accounts.append(account)
def remove_account(self, account):
self.accounts.remove(account)
def search_account(self, name):
for account in self.accounts:
if account.name == name:
return account
return None
if __name__ == "__main__":
bank = Bank()
account1 = Account("Alice", 1000)
account2 = Account("Bob", 2000)
bank.add_account(account1)
bank.add_account(account2)
alice = bank.search_account("Alice")
alice.deposit(500)
alice.view_balance()
bob = bank.search_account("Bob")
bob.withdraw(1500)
bob.view_balance()
```
这个代码示例包括一个 Account 类和一个 Bank 类。 Account 类表示银行账户,具有存款、取款和查看余额的功能。 Bank 类表示银行自身,具有添加、删除和搜索账户的功能。在主函数中,我们创建了两个银行账户,将它们添加到银行中,并对这两个帐户执行一些操作。