Start learning free

LESSON 410 OF 414 · PROJECTS: OBJECT-ORIENTED

Bank Account - Protect the Balance in Scala

Practice

Build a BankAccount class that keeps its balance private and lets other code change it only through methods that check the rules. This is encapsulation: the object protects its own data.

Private balance. Declare private var balance: Double = initialBalance inside the class, so only the class can change it.

What the class must have (Scala):

  • The primary constructor class BankAccount(initialBalance: Double): creates an account that starts with the given balance.
  • def deposit(amount: Double): Unit: adds amount to the balance. If amount is zero or negative, do nothing (no error, the balance stays the same).
  • def withdraw(amount: Double): Boolean: if amount is zero or negative, or larger than the balance, change nothing and return false. Otherwise subtract it from the balance and return true. Withdrawing exactly the whole balance is allowed (the balance becomes 0).
  • def getBalance(): Double: returns the current balance. It is the only way for outside code to read it.

Scala's Double prints as 120.0, so the driver uses .toInt to print 120.

The driver. The driver at the end of the starter code is already written and uses your class. It runs these checks:

class Solution {
    def mainFunction(): Unit = {
        val account = BankAccount(100.0)
        account.deposit(50.0)
        account.withdraw(30.0)
        account.withdraw(200.0)
        println("Balance: " + account.getBalance().toInt)
        account.deposit(-20.0)
        account.deposit(0.0)
        account.withdraw(-5.0)
        println("Balance: " + account.getBalance().toInt)
        if (account.withdraw(120.0)) {
            println("Withdrew all")
        }
        println("Balance: " + account.getBalance().toInt)
        if (!account.withdraw(1.0)) {
            println("Rejected")
        }
    }
}

Walk through it: start with 100, deposit 50 (150), withdraw 30 (120), withdraw 200 is refused (still 120). Then deposit -20, deposit 0 and withdraw -5 are all ignored (still 120). Withdrawing 120 succeeds and empties the account, so the balance is 0, and withdrawing 1 more is refused.

Expected output:

Balance: 120
Balance: 120
Withdrew all
Balance: 0
Rejected

Both deposit and withdraw start with a guard that rejects amount <= 0. Write the guard first, then the change to the balance.

private var balance: Double = initialBalance inside the class body.

if (amount <= 0 || amount > balance) return false, then balance -= amount and return true.

class BankAccount(initialBalance: Double) {
    private var balance: Double = initialBalance

    def deposit(amount: Double): Unit = {
        if (amount <= 0) return
        balance += amount
    }

    def withdraw(amount: Double): Boolean = {
        if (amount <= 0 || amount > balance) return false
        balance -= amount
        return true
    }

    def getBalance(): Double = {
        return balance
    }
}

class Solution {
    def mainFunction(): Unit = {
        val account = BankAccount(100.0)
        account.deposit(50.0)
        account.withdraw(30.0)
        account.withdraw(200.0)
        println("Balance: " + account.getBalance().toInt)
        account.deposit(-20.0)
        account.deposit(0.0)
        account.withdraw(-5.0)
        println("Balance: " + account.getBalance().toInt)
        if (account.withdraw(120.0)) {
            println("Withdrew all")
        }
        println("Balance: " + account.getBalance().toInt)
        if (!account.withdraw(1.0)) {
            println("Rejected")
        }
    }
}

STEP 1 OF 11
    Ctrl + Enter to check

    Bank Account in Scala

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free