声明银行账户类Account,成员变量包括账号、储户姓名、开户时间、身份证号码、存款余额等账户信息,成员方法包括开户、存款、取款、查询(余额、明细)、销户等操作。
时间: 2023-07-04 08:03:38 浏览: 128
以下是银行账户类Account的示例代码:
```python
class Account:
def __init__(self, account_num, name, id_num, balance=0.0):
self.account_num = account_num
self.name = name
self.id_num = id_num
self.balance = balance
self.open_date = datetime.datetime.today()
def deposit(self, amount):
self.balance += amount
print("成功存入%.2f元,当前余额为%.2f元。" % (amount, self.balance))
def withdraw(self, amount):
if amount > self.balance:
print("余额不足,取款失败。")
else:
self.balance -= amount
print("成功取出%.2f元,当前余额为%.2f元。" % (amount, self.balance))
def query_balance(self):
print("当前余额为%.2f元。" % self.balance)
def query_detail(self):
print("账户信息:")
print("账号:", self.account_num)
print("姓名:", self.name)
print("身份证号码:", self.id_num)
print("开户时间:", self.open_date.strftime("%Y-%m-%d %H:%M:%S"))
print("当前余额:%.2f元。" % self.balance)
def close_account(self):
self.balance = 0.0
print("账户已成功销户。")
@staticmethod
def create_account():
account_num = input("请输入账号:")
name = input("请输入储户姓名:")
id_num = input("请输入身份证号码:")
balance = float(input("请输入开户金额:"))
account = Account(account_num, name, id_num, balance)
print("账户创建成功!")
account.query_detail()
return account
```
使用示例:
```python
# 创建账户
account = Account.create_account()
# 存款
account.deposit(1000.0)
# 取款
account.withdraw(500.0)
# 查询余额和明细
account.query_balance()
account.query_detail()
# 销户
account.close_account()
```
阅读全文