Python 写一个银行系统实现是一个用于模拟银行基本功能的程序,如开户、销户、查询余额、转账、取款、存款等
时间: 2024-08-12 22:09:41 浏览: 197
python 银行管理系统 实验报告及源码
5星 · 资源好评率100%
创建一个简单的银行系统模拟程序,通常会使用 Python 的面向对象编程(OOP)特性,尤其是类和方法来定义账户、客户和银行操作。以下是一个基础的示例,展示了如何实现这样的系统:
```python
class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"存入了 {amount} 元,当前余额为 {self.balance} 元.")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"取出了 {amount} 元,当前余额为 {self.balance} 元.")
else:
print("余额不足,请检查!")
def check_balance(self):
return self.balance
class Customer:
def __init__(self, name, accounts=None):
self.name = name
if accounts is None:
self.accounts = [BankAccount()]
else:
self.accounts = accounts
def open_account(self, initial_deposit=0):
new_account = BankAccount(len(self.accounts) + 1, initial_deposit)
self.accounts.append(new_account)
return new_account
def close_account(self, account_number):
for acc in self.accounts:
if acc.account_number == account_number:
self.accounts.remove(acc)
print(f"{self.name}关闭了账户{account_number}.")
break
def transfer(self, from_account, to_account, amount):
if from_account.check_balance() >= amount:
from_account.withdraw(amount)
to_account.deposit(amount)
print(f"{self.name}从{from_account.account_number}转出{amount}元到{to_account.account_number}.")
else:
print("转账金额超过余额!")
# 使用示例
customer = Customer("张三")
account1 = customer.open_account(1000)
account2 = customer.open_account()
customer.transfer(account1, account2, 500)
# 客户可以调用各个账户的方法进行操作
account1.check_balance()
account2.withdraw(200)
```
阅读全文