From 44cdb4a3f2114596ee8e699656d67169b9c8a8e5 Mon Sep 17 00:00:00 2001 From: Ben Greenberg <13892277+hummusonrails@users.noreply.github.com> Date: Tue, 28 Apr 2026 09:05:31 +0300 Subject: [PATCH] Implement apply_interest --- bank.py | 374 +++++++++++++++++++++-------------------- test_apply_interest.py | 31 ++++ 2 files changed, 221 insertions(+), 184 deletions(-) create mode 100644 test_apply_interest.py diff --git a/bank.py b/bank.py index ab74f6f..23b9ee1 100644 --- a/bank.py +++ b/bank.py @@ -1,184 +1,190 @@ -class Bank: - - def __init__(self, name): - # Manages accounts, transactions, and loans. Generates unique account numbers. - self.name = name - self.accounts = [] - self.transactions = [] - self.loans = [] - - - def create_account(self, account_holder, initial_balance): - # Creates a new account with a unique number and initial balance, add it to accounts array and return account number - pass - - - def get_account(self, account_number): - # Retrieves an account by its number, returns None if not found. - pass - - - def list_accounts(self): - # Lists all accounts with their details like account number, holder, balance - pass - - - def close_account(self, account_number): - # Closes an account by removing it from the list. - pass - - - def transfer_funds(self, from_account, to_account, amount): - # Transfers funds between accounts if sufficient balance is available, return boolean values depending on - # transfer success - pass - - - def generate_account_statement(self, account_number): - # Generates a transaction history for a given account number, return None if no account number found - pass - - - - def calculate_total_assets(self): - # Calculates the total assets by summing all account balances. - pass - - - def process_loan_payments(self): - # Processes loan payments by deducting the installment amount from the balance. - pass - - -class Account: - # Represents a bank account with balance and transactions. Default balance is 0. - def __init__(self, account_number, account_holder, balance=0.0): - self.account_number = account_number - self.account_holder = account_holder - self.balance = balance - self.transactions = [] - - - def deposit(self, amount): - # Deposits a positive amount to the account and records the transaction. When adding transaction to array specift "Deposit" and amount - pass - - - def withdraw(self, amount): - # Withdraws an amount if sufficient balance is available. When adding into transactions array, specify Withdraw and amount - pass - - - def get_balance(self): - # Returns the current balance of the account. - pass - - - def get_transaction_history(self): - # Retrieves the transaction history of the account. - pass - - - def apply_interest(self, rate): - # Applies interest based on a given rate. - pass - - - def overdraft_protection(self, amount): - # Checks if a withdrawal amount is within available balance. - pass - - - def update_contact_information(self, new_contact_info): - # Updates the contact information of the account holder. - pass - - -class Transaction: - # Represents a transaction with an ID, type, amount, and timestamp. - def __init__(self, transaction_id, account_number, amount, transaction_type, timestamp): - self.transaction_id = transaction_id - self.account_number = account_number - self.amount = amount - self.transaction_type = transaction_type - self.timestamp = timestamp - - - def process_transaction(self): - # Processes a transaction if the amount is positive. - return self.amount > 0 - - - def validate_transaction(self): - # Validates the transaction before processing. - return self.amount > 0 - - - def reverse_transaction(self): - # Reverses a transaction by negating the amount. - pass - - -class BankEmployee: - # Represents a bank employee with an ID, name, and role. - def __init__(self, employee_id, name, role): - self.employee_id = employee_id - self.name = name - self.role = role - - - def approve_loan(self, account_number, amount): - # Approves a loan request if the amount is positive. - return amount > 0 - - - def view_account_details(self, account_number): - # Views account details given an account number by returning account number - pass - - - def suspend_account(self, account_number): - # Return suspension message as string - pass - - - def generate_financial_report(self): - # Return string "Financial Report Generated" - return "Financial Report Generated" - - -class Loan: - # Represents a loan with an ID, amount, interest rate, and tenure. - def __init__(self, loan_id, account_number, amount, interest_rate, tenure): - self.loan_id = loan_id - self.account_number = account_number - self.amount = amount - self.interest_rate = interest_rate - self.tenure = tenure - self.remaining_balance = amount - - - def make_payment(self, amount): - # Makes a loan payment if the amount is within the remaining balance. - pass - - - def get_remaining_balance(self): - # Returns the remaining balance of the loan. - pass - - - def calculate_interest(self): - # Calculates interest on the remaining balance. - pass - - - def check_loan_eligibility(self, account_number, income, credit_score): - # Checks if an applicant is eligible for a loan based on income(>50k) and credit score(>650). Return boolean - pass - - # Allows restructuring of the loan with new terms. - def restructure_loan(self, new_terms): - self.tenure = new_terms.get("tenure", self.tenure) - self.interest_rate = new_terms.get("interest_rate", self.interest_rate) - return True +class Bank: + + def __init__(self, name): + # Manages accounts, transactions, and loans. Generates unique account numbers. + self.name = name + self.accounts = [] + self.transactions = [] + self.loans = [] + + + def create_account(self, account_holder, initial_balance): + # Creates a new account with a unique number and initial balance, add it to accounts array and return account number + pass + + + def get_account(self, account_number): + # Retrieves an account by its number, returns None if not found. + pass + + + def list_accounts(self): + # Lists all accounts with their details like account number, holder, balance + pass + + + def close_account(self, account_number): + # Closes an account by removing it from the list. + pass + + + def transfer_funds(self, from_account, to_account, amount): + # Transfers funds between accounts if sufficient balance is available, return boolean values depending on + # transfer success + pass + + + def generate_account_statement(self, account_number): + # Generates a transaction history for a given account number, return None if no account number found + pass + + + + def calculate_total_assets(self): + # Calculates the total assets by summing all account balances. + pass + + + def process_loan_payments(self): + # Processes loan payments by deducting the installment amount from the balance. + pass + + +class Account: + # Represents a bank account with balance and transactions. Default balance is 0. + def __init__(self, account_number, account_holder, balance=0.0): + self.account_number = account_number + self.account_holder = account_holder + self.balance = balance + self.transactions = [] + + + def deposit(self, amount): + # Deposits a positive amount to the account and records the transaction. When adding transaction to array specift "Deposit" and amount + pass + + + def withdraw(self, amount): + # Withdraws an amount if sufficient balance is available. When adding into transactions array, specify Withdraw and amount + pass + + + def get_balance(self): + # Returns the current balance of the account. + pass + + + def get_transaction_history(self): + # Retrieves the transaction history of the account. + pass + + + def apply_interest(self, rate): + # Applies interest based on a given rate. + if not isinstance(rate, (int, float)) or rate < 0: + return False + + interest = self.balance * rate + self.balance += interest + self.transactions.append(("Interest Applied", interest)) + return True + + + def overdraft_protection(self, amount): + # Checks if a withdrawal amount is within available balance. + pass + + + def update_contact_information(self, new_contact_info): + # Updates the contact information of the account holder. + pass + + +class Transaction: + # Represents a transaction with an ID, type, amount, and timestamp. + def __init__(self, transaction_id, account_number, amount, transaction_type, timestamp): + self.transaction_id = transaction_id + self.account_number = account_number + self.amount = amount + self.transaction_type = transaction_type + self.timestamp = timestamp + + + def process_transaction(self): + # Processes a transaction if the amount is positive. + return self.amount > 0 + + + def validate_transaction(self): + # Validates the transaction before processing. + return self.amount > 0 + + + def reverse_transaction(self): + # Reverses a transaction by negating the amount. + pass + + +class BankEmployee: + # Represents a bank employee with an ID, name, and role. + def __init__(self, employee_id, name, role): + self.employee_id = employee_id + self.name = name + self.role = role + + + def approve_loan(self, account_number, amount): + # Approves a loan request if the amount is positive. + return amount > 0 + + + def view_account_details(self, account_number): + # Views account details given an account number by returning account number + pass + + + def suspend_account(self, account_number): + # Return suspension message as string + pass + + + def generate_financial_report(self): + # Return string "Financial Report Generated" + return "Financial Report Generated" + + +class Loan: + # Represents a loan with an ID, amount, interest rate, and tenure. + def __init__(self, loan_id, account_number, amount, interest_rate, tenure): + self.loan_id = loan_id + self.account_number = account_number + self.amount = amount + self.interest_rate = interest_rate + self.tenure = tenure + self.remaining_balance = amount + + + def make_payment(self, amount): + # Makes a loan payment if the amount is within the remaining balance. + pass + + + def get_remaining_balance(self): + # Returns the remaining balance of the loan. + pass + + + def calculate_interest(self): + # Calculates interest on the remaining balance. + pass + + + def check_loan_eligibility(self, account_number, income, credit_score): + # Checks if an applicant is eligible for a loan based on income(>50k) and credit score(>650). Return boolean + pass + + # Allows restructuring of the loan with new terms. + def restructure_loan(self, new_terms): + self.tenure = new_terms.get("tenure", self.tenure) + self.interest_rate = new_terms.get("interest_rate", self.interest_rate) + return True diff --git a/test_apply_interest.py b/test_apply_interest.py new file mode 100644 index 0000000..d1c098f --- /dev/null +++ b/test_apply_interest.py @@ -0,0 +1,31 @@ +from bank import Account + + +def test_apply_interest_increases_balance_and_records_transaction(): + account = Account("A-1", "Ada Lovelace", 100.0) + + assert account.apply_interest(0.05) is True + assert account.balance == 105.0 + assert account.transactions[-1] == ("Interest Applied", 5.0) + + +def test_apply_interest_handles_zero_balance_and_zero_rate(): + empty_account = Account("A-2", "Grace Hopper", 0.0) + funded_account = Account("A-3", "Katherine Johnson", 250.0) + + assert empty_account.apply_interest(0.05) is True + assert empty_account.balance == 0.0 + assert empty_account.transactions[-1] == ("Interest Applied", 0.0) + + assert funded_account.apply_interest(0) is True + assert funded_account.balance == 250.0 + assert funded_account.transactions[-1] == ("Interest Applied", 0.0) + + +def test_apply_interest_rejects_invalid_rates(): + account = Account("A-4", "Dorothy Vaughan", 100.0) + + assert account.apply_interest(-0.01) is False + assert account.apply_interest("0.05") is False + assert account.balance == 100.0 + assert account.transactions == []