用python的运用构造方法,类和对象,继承知识完成银行账号管理,要求具有用户界面,可以创建用户账户、查询用户账户、删除用户账户、存钱(存钱后账号金额增加)、取钱(取钱后账号金额减少)等功能
时间: 2023-08-21 18:05:03 浏览: 111
好的,我可以为您提供一个更完整的代码示例,使用了构造方法、类和对象、继承等知识,实现银行账号管理系统,具有用户界面,可以创建用户账户、查询用户账户、删除用户账户、存钱、取钱等功能。
```
class Account:
def __init__(self, name, balance=0):
self.name = name
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
return "Insufficient balance"
else:
self.balance -= amount
def __str__(self):
return f"Account name: {self.name}\nBalance: {self.balance}"
class SavingsAccount(Account):
def __init__(self, name, balance=0, interest_rate=0.01):
super().__init__(name, balance)
self.interest_rate = interest_rate
def add_interest(self):
interest = self.balance * self.interest_rate
self.deposit(interest)
class Bank:
def __init__(self):
self.accounts = {}
def create_account(self, name, balance=0, account_type="savings"):
if name in self.accounts:
return "Account already exists"
else:
if account_type == "savings":
self.accounts[name] = SavingsAccount(name, balance)
else:
self.accounts[name] = Account(name, balance)
return "Account created successfully"
def delete_account(self, name):
if name in self.accounts:
del self.accounts[name]
return "Account deleted successfully"
else:
return "Account does not exist"
def display_account(self, name):
if name in self.accounts:
return str(self.accounts[name])
else:
return "Account does not exist"
def deposit(self, name, amount):
if name in self.accounts:
self.accounts[name].deposit(amount)
return "Amount deposited successfully"
else:
return "Account does not exist"
def withdraw(self, name, amount):
if name in self.accounts:
return self.accounts[name].withdraw(amount)
else:
return "Account does not exist"
def add_interest(self, name):
if name in self.accounts:
if isinstance(self.accounts[name], SavingsAccount):
self.accounts[name].add_interest()
return "Interest added successfully"
else:
return "Account is not a savings account"
else:
return "Account does not exist"
bank = Bank()
while True:
print("1. Create account")
print("2. Delete account")
print("3. Display account")
print("4. Deposit")
print("5. Withdraw")
print("6. Add interest")
print("7. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter account name: ")
balance = int(input("Enter initial balance (optional): "))
account_type = input("Enter account type (savings/current): ")
print(bank.create_account(name, balance, account_type))
elif choice == 2:
name = input("Enter account name: ")
print(bank.delete_account(name))
elif choice == 3:
name = input("Enter account name: ")
print(bank.display_account(name))
elif choice == 4:
name = input("Enter account name: ")
amount = int(input("Enter amount to deposit: "))
print(bank.deposit(name, amount))
elif choice == 5:
name = input("Enter account name: ")
amount = int(input("Enter amount to withdraw: "))
print(bank.withdraw(name, amount))
elif choice == 6:
name = input("Enter account name: ")
print(bank.add_interest(name))
elif choice == 7:
break
else:
print("Invalid choice")
```
在这个代码示例中,我们定义了两个类 `Account` 和 `SavingsAccount`,`SavingsAccount` 继承自 `Account`,表示储蓄账户,具有年利率这个额外的属性,以及添加利息的方法。`Bank` 类表示银行,具有创建账户、删除账户、查询账户、存款、取款和添加利息等功能。
在主程序中,我们使用一个 while 循环来显示用户界面,并根据用户输入的操作来调用相应的方法。用户可以通过输入数字来执行不同的操作,比如创建账户、删除账户、查询账户、存款、取款和添加利息等。用户还可以选择账户类型,如果是储蓄账户,可以添加利息。
阅读全文