Untitled

 avatar
unknown
plain_text
3 months ago
3.0 kB
5
Indexable
class Account:
    def __init__(self, account_number, holder_name, initial_balance=0):
        # Initializing account details
        self.account_number = account_number
        self.holder_name = holder_name
        self.balance = initial_balance
    
    def deposit(self, amount):
        # Adds amount to the balance
        if amount > 0:
            self.balance += amount
            print(f"Deposited ${amount} to account {self.account_number}")
        else:
            print("Deposit amount must be positive")

    def withdraw(self, amount):
        # Subtracts amount if sufficient funds are available
        if 0 < amount <= self.balance:
            self.balance -= amount
            print(f"Withdrew ${amount} from account {self.account_number}")
        else:
            print("Insufficient balance or invalid amount")

    def get_balance(self):
        return self.balance

    def display_info(self):
        print(f"Account Number: {self.account_number}")
        print(f"Holder Name: {self.holder_name}")
        print(f"Balance: ${self.balance}")


class SavingsAccount(Account):
    def __init__(self, account_number, holder_name, initial_balance=0, interest_rate=0.01):
        # Initialize using Account's constructor
        super().__init__(account_number, holder_name, initial_balance)
        self.interest_rate = interest_rate

    def apply_interest(self):
        # Applies interest to the current balance
        interest = self.balance * self.interest_rate
        self.balance += interest
        print(f"Applied interest of ${interest:.2f} to account {self.account_number}")


class Bank:
    def __init__(self):
        # Dictionary to store accounts with account_number as the key
        self.accounts = {}

    def add_account(self, account):
        # Adds an account to the bank
        self.accounts[account.account_number] = account
        print(f"Added account {account.account_number}")

    def find_account(self, account_number):
        # Finds and returns an account by account_number
        return self.accounts.get(account_number)

    def transfer(self, from_account_number, to_account_number, amount):
        # Transfers funds between accounts
        from_account = self.find_account(from_account_number)
        to_account = self.find_account(to_account_number)
        
        if from_account and to_account:
            if from_account.balance >= amount and amount > 0:
                from_account.withdraw(amount)
                to_account.deposit(amount)
                print(f"Transferred ${amount} from {from_account_number} to {to_account_number}")
            else:
                print("Insufficient balance or invalid transfer amount")
        else:
            print("One or both accounts not found")


# Example usage
test_bank = Bank()

# Create and add accounts
test_savings = SavingsAccount("SA001", "John Doe", 1000, 0.02)
test_bank.add_account(test_savings)

test_savings.display_info()
test_savings.apply_interest()
test_savings.display_info()
Editor is loading...
Leave a Comment