Skip to main content
Course/Module 10/Topic 2 of 4Advanced

Bank Manager & Multiple Accounts

Build a Bank Manager class using HashMap to manage multiple accounts — create accounts, transfer money, and generate reports.

55 minBy Priygop TeamLast updated: Feb 2026

HashMap-Based Account Management

  • HashMap Storage: HashMap<String, BankAccount> accounts — key is account number (String), value is BankAccount object. O(1) lookup by account number for efficient operations
  • Create Account: String createAccount(String owner, double deposit) — create BankAccount, store in HashMap, return account number. Caller uses the returned number for future operations
  • Transfer Money: void transfer(String fromNum, String toNum, double amount) — look up both accounts in HashMap, validate existence and sufficient funds, subtract from source, add to destination
  • Null Checks: SimpleAccount from = accounts.get(fromNum) — HashMap returns null if key doesn't exist. Always check for null before using: if (from == null) { System.out.println('Account not found!'); return; }
  • Report Generation: printAllAccounts() iterates over accounts.values() — formatted table with System.out.printf('%-12s %-15s $%,10.2f%n', number, owner, balance). Calculate total across all accounts
  • HashMap vs ArrayList: HashMap provides O(1) lookup by account number (perfect for finding specific accounts). ArrayList would require O(n) linear search. Choose data structure based on access pattern

Account Types & Inheritance

  • Base Class: abstract class BankAccount — common fields (number, owner, balance, transactions) and methods (deposit, withdraw, getBalance). Abstract method: abstract double calculateInterest()
  • Savings Account: class SavingsAccount extends BankAccount — override calculateInterest() to return balance * 0.045 (4.5% annual). Add minimum balance requirement in withdraw validation
  • Checking Account: class CheckingAccount extends BankAccount — no/low interest (0.1%), no minimum balance, but overdraft protection up to a limit. Different fee structure
  • Polymorphism in Action: ArrayList<BankAccount> allAccounts — store both Savings and Checking accounts. Loop and call calculateInterest() — Java invokes the correct override per account type
  • instanceof Check: if (account instanceof SavingsAccount savings) { savings.applyInterest(); } — pattern matching (Java 16+) combines type check and cast in one expression
  • Design Pattern: Factory method — BankAccount createAccount(String type, String owner, double deposit) { return switch(type) { case 'savings' -> new SavingsAccount(...); case 'checking' -> new CheckingAccount(...); }; }
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep