1、定义银行帐户类,变量有“帐号”和“存款余额”,“存取历史〞,方法有“存 款”、“取款”、“查询余额”、〞品示帐号”以及存储功能,能查询历史存取记录
时间: 2023-06-13 07:08:14 浏览: 72
以下是一个简单的Python代码实现,定义了一个BankAccount类,包括帐号、存款余额、存取历史等属性和方法:
```python
class BankAccount:
def __init__(self, account_number, balance):
self.account_number = account_number
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:
self.balance -= amount
self.transaction_history.append(f"Withdraw: {amount}")
else:
print("Insufficient balance!")
def check_balance(self):
print(f"Account balance: {self.balance}")
def display_account_number(self):
print(f"Account number: {self.account_number}")
def display_transaction_history(self):
if len(self.transaction_history) == 0:
print("No transaction history!")
else:
print("Transaction history:")
for transaction in self.transaction_history:
print(transaction)
```
使用示例:
```python
# 创建一个帐户
account = BankAccount("123456789", 1000)
# 存款
account.deposit(500)
account.check_balance() # 输出: Account balance: 1500
# 取款
account.withdraw(200)
account.check_balance() # 输出: Account balance: 1300
# 查询帐号和历史记录
account.display_account_number() # 输出: Account number: 123456789
account.display_transaction_history() # 输出: Transaction history: Deposit: 500, Withdraw: 200
```
阅读全文