Nick Huemmer

02 Feb 2023

JavaScript OOP - Bank Account Class

Here’s an Object-Oriented Programming problem using Class syntax. Try to solve it yourself before looking at the solution.

The Problem:

Problem: Create a BankAccount class with properties balance and owner. The class should have methods deposit(amount) and withdraw(amount) that update the balance accordingly. The class should also have a method transfer(otherAccount, amount) that transfers the specified amount from the current account to the other account.

My Solution

Here’s my solution. It’s pretty straightforward.

Make sure to include edge cases so that accounts are not overdrawn for withdrawals or transfers!


//i: balance - number
//i: owner - string
//o: none returned, new instance of a BanKAccount

// declare class BankAccount, two params, balance and owner
class BankAccount {
  // in constructor - params balance and owner
  constructor(balance, owner) {
    // set this.balance to balance
    this.balance = balance;
    // set this.owner to owner
    this.owner = owner;
  }
  // outside constructor function, add three methods
  //delcare deposit method, takes one param amount - number
  deposit(amount) {
    // add amount to this.balance
    this.balance += amount;
  }
  //declare withdraw method, takes one param amount - number
  withdraw(amount) {
    // conditional to check if enough funds are present
    if (this.balance >= amount) {
      // subtract amount from this.balance
      this.balance -= amount;
    } else {
      console.log('insufficient funds');
    }
  }
  //declare transfer, two params, otherAccount - instance of a BankAccount object, amount, number
  transfer(otherAccount, amount) {
    // edge case to ensure that enough money is available to transfer
    if (this.balance >= amount) {
      //amount is subtracted from this.balance
      this.balance -= amount;
      //amount is added to otherAccount.balance
      otherAccount.balance += amount;
    } else {
      console.log('insufficient funds');
    }
  }
}
const boa = new BankAccount(1000, 'Mike Smith');
const wf = new BankAccount(1500, 'Sally Ride');

console.log(boa);
console.log(wf);

boa.transfer(wf, 500);

console.log(boa);
console.log(wf);

console.log(boa.withdraw(20000));

boa.transfer(wf, 20000);

And here’s an alternate solution, generated by ChatGPT:

class BankAccount {
  constructor(owner, balance) {
    this.owner = owner;
    this.balance = balance;
  }
  deposit(amount) {
    this.balance += amount;
  }
  withdraw(amount) {
    if (this.balance >= amount) {
      this.balance -= amount;
    } else {
      console.log('Insufficient funds.');
    }
  }
  transfer(otherAccount, amount) {
    if (this.balance >= amount) {
      this.withdraw(amount);
      otherAccount.deposit(amount);
    } else {
      console.log('Insufficient funds.');
    }
  }
}

let account1 = new BankAccount('Jane', 1000);
let account2 = new BankAccount('Bob', 500);
account1.transfer(account2, 250);
console.log(account1.balance); // 750
console.log(account2.balance); // 750