设计银行卡类并进行测试,该类有账户名、卡号、余额、状态(是否正常)等属性,并实现存、取、转账等操作。python
时间: 2024-09-26 13:03:54 浏览: 179
设计银行卡类(BankAccount)可以按照面向对象的原则进行,下面是一个简单的Python类实现:
```python
class BankAccount:
def __init__(self, account_name, card_number, balance=0, is_active=True):
self.account_name = account_name
self.card_number = card_number
self.balance = balance
self.is_active = is_active
# 存款方法
def deposit(self, amount):
if amount > 0:
self.balance += amount
print(f"存款{amount}成功,当前余额为{self.balance}")
else:
print("存款金额无效")
# 取款方法
def withdraw(self, amount):
if amount <= self.balance and amount > 0:
self.balance -= amount
print(f"取款{amount}成功,当前余额为{self.balance}")
elif amount == 0:
print("取款金额为零")
else:
print("余额不足,取款失败")
# 转账方法
def transfer(self, to_account, amount):
if amount > 0 and amount <= self.balance:
self.balance -= amount
to_account.deposit(amount)
print(f"向{to_account.account_name}转账{amount}成功,当前余额为{self.balance}")
else:
print("转账金额过大或余额不足")
# 测试部分
account1 = BankAccount("张三", "1234567890123456", 1000)
account2 = BankAccount("李四", "9876543210987654", 500)
# 测试存款
account1.deposit(500)
account2.deposit(-200) # 负数存款示例
# 测试取款
account1.withdraw(200)
account2.withdraw(0) # 取款金额为零
# 测试转账
account1.transfer(account2, 300)
```
阅读全文