用python完成锁定、解锁、查询账户、存款取款、转账模块
时间: 2024-12-13 15:21:44 浏览: 5
在Python中,你可以设计一个简单的银行账户类来实现锁定、解锁、查询余额、存款、取款和转账的功能。这里是一个基本的示例:
```python
class BankAccount:
def __init__(self, account_number, is_locked=False, balance=0):
self.account_number = account_number
self.is_locked = is_locked
self.balance = balance
# 锁定账户
def lock_account(self):
self.is_locked = True
print(f"账户 {self.account_number} 已经锁定")
# 解锁账户
def unlock_account(self, password): # 假设需要密码才能解锁
if self.is_locked and password == "your_password":
self.is_locked = False
print(f"账户 {self.account_number} 已经解锁")
else:
print("无效密码或账户未锁定")
# 查询余额
def check_balance(self):
if not self.is_locked:
print(f"账户 {self.account_number} 的当前余额是:{self.balance}")
else:
print("账户已被锁定,无法查看余额")
# 存款
def deposit(self, amount):
if not self.is_locked:
self.balance += amount
print(f"已成功存款 {amount},新的余额是 {self.balance}")
else:
print("账户已被锁定,无法存款")
# 取款
def withdraw(self, amount):
if not self.is_locked and amount <= self.balance:
self.balance -= amount
print(f"已成功取款 {amount},新的余额是 {self.balance}")
else:
print("操作失败,可能是账户锁定或余额不足")
# 转账
def transfer(self, recipient_account, amount):
if not self.is_locked and amount <= self.balance:
recipient_account.deposit(amount)
self.balance -= amount
print(f"已向账户 {recipient_account.account_number} 转账 {amount},新的余额是 {self.balance}")
else:
print("转账失败,可能是账户锁定、余额不足或接收方信息错误")
# 使用示例
account1 = BankAccount(12345678, False, 1000)
account2 = BankAccount(98765432)
account1.deposit(500) # 存款
account1.transfer(account2, 200) # 转账
# 更多功能如查询、解锁等同理
```
阅读全文