BankAccount

 

 

Develop a new class called BankAccount. A bank account has an owner’s name and a balance. Be sure to include a constructor that allows a client to supply the owner’s name and an initial balance. A bank account needs accessors for the name and balance, mutators for making deposits and withdrawals, and a toString method.

 

Class:

BankAccount(String owner, int balance)

 

Method(s):

   public double getBalance()

   public String getName()

   public String toString()

   public String withdraw(double amount)

   public String deposit(double amount)

 

 

The TestBankAccount class has already been created to check various methods of BankAccount. Additional methods could easily be added to check additional possibilities.

 


Hint: A nice trick to use to compare a double result of money to an expected value is to multiply the account balance by 100 and cat it to an int.  For example, I expect the balance to be $500.23.   So I can convert that to an int like this: (int) (a.getBalance() *100).  Now also multiply the expected amount by 100, producing 50023.  Now compare the two:

    assertEquals(“Balance is incorrect”, 50023, (int) (b.getBalance() * 100));

 

Modified from Fundamentals of Java – Third Edition – Lambert/Osborn – 2007, Project 5-5 p. 197.