Bank account

BankAccount keeps a balance: deposit and withdraw need a positive amount, and withdrawing more than the balance is refused without touching it. Your tests must pass on the correct code and catch the 5 bugs.

Concept: lesson A class with state: check the balance after each action

Exercise

class BankAccount {
  /** Starts with the given balance (default 0). */
  constructor(initialBalance = 0)

  /** Adds a positive amount. Throws Error("Amount must be positive") for 0 or less. */
  deposit(amount)

  /**
   * Removes a positive amount. Throws Error("Amount must be positive") for 0 or less,
   * and Error("Insufficient funds") when amount is more than the balance.
   * Withdrawing the whole balance is allowed.
   */
  withdraw(amount)

  /** Current balance. */
  getBalance()
}

Your tests

0 of 5 bugs caught

  • Bug 1: hidden
  • Bug 2: hidden
  • Bug 3: hidden
  • Bug 4: hidden
  • Bug 5: hidden