Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 08 / Topic 12 of 12

Putting it together: a bank account system

Real-world example: combine every idea in this part into one small program — an abstract BankAccount that implements a Taxable interface, two concrete subclasses, an AccountType enum, and a TransactionRecord for each account's history.

enum AccountType {
    SAVINGS(500.0), CURRENT(0.0), SALARY(0.0);
    private final double minimumBalance;
    AccountType(double minimumBalance) { this.minimumBalance = minimumBalance; }
    double minimumBalance() { return minimumBalance; }
}

record TransactionRecord(String type, double amount) {
    TransactionRecord {
        if (amount <= 0) throw new IllegalArgumentException("amount must be positive, got " + amount);
    }
}

interface Taxable {
    double taxableAmount();
    default double tdsAmount() { return Math.round(taxableAmount() * 0.10 * 100) / 100.0; }
}

abstract static class BankAccount implements Taxable {
    protected final String accountNumber;
    protected final AccountType type;
    protected double balance;
    protected final List<TransactionRecord> history = new ArrayList<>();

    BankAccount(String accountNumber, AccountType type, double openingBalance) {
        if (openingBalance < type.minimumBalance()) {
            throw new IllegalArgumentException(
                    type + " needs at least ₹" + type.minimumBalance() + " to open, got ₹" + openingBalance);
        }
        this.accountNumber = accountNumber;
        this.type = type;
        this.balance = openingBalance;
    }

    void deposit(double amount) {
        balance += amount;
        history.add(new TransactionRecord("DEPOSIT", amount));
    }

    void withdraw(double amount) {
        if (amount > balance) throw new IllegalStateException(accountNumber + " has insufficient balance for ₹" + amount);
        balance -= amount;
        history.add(new TransactionRecord("WITHDRAWAL", amount));
    }

    abstract double monthlyFee();

    @Override
    public double taxableAmount() { return 0.0; }    // overridden by interest-bearing accounts

    String statementLine() {
        return String.format("%-12s %-7s balance ₹%-10.2f fee ₹%-6.2f TDS ₹%.2f",
                accountNumber, type, balance, monthlyFee(), tdsAmount());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BankAccount)) return false;
        BankAccount other = (BankAccount) o;
        return accountNumber.equals(other.accountNumber);
    }

    @Override
    public int hashCode() { return Objects.hash(accountNumber); }
}

SavingsAccount and CurrentAccount each extends BankAccount, filling in monthlyFee(); only SavingsAccount overrides taxableAmount(), since current accounts in this model earn no interest. Running it:

=== Monthly statement ===
SBIN0001234  SAVINGS balance ₹60000.00   fee ₹0.00   TDS ₹320.00
HDFC0005678  CURRENT balance ₹125000.00  fee ₹150.00 TDS ₹0.00
ICIC0009999  CURRENT balance ₹40000.00   fee ₹150.00 TDS ₹0.00
total monthly fees: ₹300.0
accounts.get(1).equals(sameAccount): true (equal by account number, even though opening balances differ)
SBIN0001234 history: [TransactionRecord[type=DEPOSIT, amount=10000.0]]
HDFC0005678 history: [TransactionRecord[type=DEPOSIT, amount=10000.0], TransactionRecord[type=WITHDRAWAL, amount=5000.0]]
ICIC0009999 history: [TransactionRecord[type=DEPOSIT, amount=10000.0]]

Every idea from this part shows up in that one run: List<BankAccount> holding three different concrete types and calling monthlyFee()/tdsAmount() polymorphically (sections 7, 9); AccountType's minimumBalance() enforced inside the constructor (section 10); a TransactionRecord appended to an ArrayList on every deposit and withdrawal, immutable once created (section 11); equals() based on accountNumber alone, so a second CurrentAccount object built with the same number counts as the same account even with a different opening balance (section 5). This is roughly the shape of the Stage 2 project, Library management system, waiting at the end of this stage — a few cooperating classes, not one giant one.

Full program: examples/BankAccountSystem.java.

Done here? The practice set for Part 8 — including the solutions — is on the part page.

Search the guide