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

Part 09 / Topic 10 of 10

Putting it together: a bank account hierarchy

Every tool from this part, modeling what Part 7's single BankAccount couldn't: an abstract class for the shared shape, an interface for a capability only some accounts have, a sealed TransactionResult carrying a real reason instead of a bare Boolean, an enum to pick a kind, and a companion-object factory.

sealed class TransactionResult {
    data class Success(val message: String, val newBalanceInr: Double) : TransactionResult()
    data class Failure(val reason: String) : TransactionResult()
}

interface InterestBearing {
    val annualRatePercent: Double
    fun applyMonthlyInterest(): TransactionResult
}

enum class AccountKind { SAVINGS, CURRENT }

abstract class Account(val accountNumber: String, val holderName: String, initialBalanceInr: Double) {
    protected var balanceInr: Double = initialBalanceInr
    abstract val accountType: String

    open fun deposit(amountInr: Double): TransactionResult {
        if (amountInr <= 0) return TransactionResult.Failure("Deposit amount must be positive")
        balanceInr += amountInr
        return TransactionResult.Success("Deposited ₹%.2f".format(amountInr), balanceInr)
    }

    abstract fun withdraw(amountInr: Double): TransactionResult   // the rule that differs per kind

    companion object {
        fun open(kind: AccountKind, accountNumber: String, holderName: String, initialBalanceInr: Double): Account =
            when (kind) {
                AccountKind.SAVINGS -> SavingsAccount(accountNumber, holderName, initialBalanceInr)
                AccountKind.CURRENT -> CurrentAccount(accountNumber, holderName, initialBalanceInr)
            }
    }
}

SavingsAccount implements both Account and InterestBearing; CurrentAccount implements only Account, with an overdraft limit instead of a minimum balance. Running the full program:

--- Opening balances ---
SB-1001  (Savings) Neha Joshi       balance ₹5000.00
CA-2047  (Current) Sameer Kulkarni  balance ₹2000.00

--- Savings account activity ---
deposit 2000 OK: Deposited ₹2000.00 -> new balance ₹7000.00
withdraw 7000 FAILED: Withdrawing ₹7000.00 would drop balance below the ₹1000.00 minimum
withdraw 5000 OK: Withdrew ₹5000.00 -> new balance ₹2000.00
interest OK: Monthly interest ₹6.67 credited -> new balance ₹2006.67

--- Current account activity ---
withdraw 10000 OK: Withdrew ₹10000.00 -> new balance ₹-8000.00
withdraw 45000 FAILED: Withdrawing ₹45000.00 would exceed the ₹50000.00 overdraft limit
deposit -500 FAILED: Deposit amount must be positive

--- Closing balances ---
SB-1001  (Savings) Neha Joshi       balance ₹2006.67
CA-2047  (Current) Sameer Kulkarni  balance ₹-8000.00

Two separately built TransactionRecords, same values: true
Is CurrentAccount also InterestBearing: false

A few design choices worth noticing:

  • withdraw is abstract on Account with no shared default — the minimum-balance rule and the overdraft rule have nothing in common worth writing once, unlike deposit, which every account type accepts the same way.
  • TransactionResult being sealed means every caller's when over it must handle both Success and Failure — there's no exception a caller could forget to catch, and no silent "returns null on failure" to misread.
  • Account.open(...) (the companion-object factory) is the only place that needs to know SavingsAccount and CurrentAccount exist by name — callers just pick an AccountKind.

Full program: examples/09_bank_account_system.kt.

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

Search the guide