添加 wulayaha

This commit is contained in:
xiaoyuyouyou 2025-04-30 12:24:26 +08:00
commit 2600781b62

80
wulayaha Normal file
View File

@ -0,0 +1,80 @@
class Account:
def __init__(self, account_number, account_holder, balance=0):
self.account_number = account_number
self.account_holder = account_holder
self.balance = balance
def deposit(self, amount):
if amount > 0:
self.balance += amount
print(f"成功存入 {amount} 元,当前余额为 {self.balance} 元。")
else:
print("存款金额必须大于 0。")
def withdraw(self, amount):
if 0 < amount <= self.balance:
self.balance -= amount
print(f"成功取出 {amount} 元,当前余额为 {self.balance} 元。")
else:
print("余额不足或取款金额无效。")
def get_balance(self):
return self.balance
def display_info(self):
print(f"账户号码: {self.account_number}")
print(f"账户持有人: {self.account_holder}")
print(f"当前余额: {self.balance} 元")
class SavingAccount(Account):
def __init__(self, account_number, account_holder, balance=0, interest_rate=0.01):
super().__init__(account_number, account_holder, balance)
self.interest_rate = interest_rate
def calculate_interest(self):
interest = self.balance * self.interest_rate
self.balance += interest
print(f"利息计算完成,利息为 {interest} 元,当前余额为 {self.balance} 元。")
class Bank:
def __init__(self):
self.accounts = []
def add_account(self, account):
self.accounts.append(account)
print(f"账户 {account.account_number} 已成功添加到银行系统。")
def find_account(self, account_number):
for account in self.accounts:
if account.account_number == account_number:
return account
return None
def display_all_accounts(self):
for account in self.accounts:
account.display_info()
print("-" * 20)
# 测试代码
if __name__ == "__main__":
bank = Bank()
account1 = Account("123456", "John Doe", 1000)
saving_account1 = SavingAccount("789012", "Jane Smith", 2000)
bank.add_account(account1)
bank.add_account(saving_account1)
account = bank.find_account("123456")
if account:
account.deposit(500)
account.withdraw(200)
saving_account = bank.find_account("789012")
if saving_account:
saving_account.calculate_interest()
bank.display_all_accounts()