Exercise 1:Creating Bank Account Subclasses (Level  3)


In this exercise, you will create two subclasses of the Account class in the Banking project:
SavingsAccount  and CheckingAccount



 

Task 1 - Modifying the Account Class

Using a text editor, modify the Account class source file in the src/com/mybank/domain directory. This class must satisfy the UML diagram in Figure 6-1 on page Lab 6-2; in particular, the balance attribute and Account class constructor are now  protected (indicated by the # character instead of the   -  character ).

  1. Change the  balance attribute from private to protected.
    protectec double balance;

  1. Change the Account constructor from private to protected.

           protected Account ( double initBalance ) {
           balance = initBalance;
    }



Task 2 - Creating the SavingsAccount Class

Using a text editor, create the SavingsAccount class source file in the  src/com/mybank/domain/ directory. This class must satisfy the UML diagram in Figure 6-1 on page Lab 6- 2 and the business rules defined in the introduction to "Exercise 1: Creating Bank Account Subclasses (Level 1)" on page  Lab 6-2.

  1. Declare the SavingsAccount class in the com.mybank.domain package. The SavingsAccount class inherits from Account class.

package com.mybank.domain;
public class SavingsAccount extends Account {
    //insert code here
}

  1. Add the interestRate attribute to the SavinsAccount class. 

private double interestRate;

  1. Add a public constructor that take two arguments. initBalance and interestRate. Pass the initBalance parameter to the super constructor. Save the interestRate parameter to the instance variable.

public SavingsAccount( double initBalance, double interestRate ){
      super( initBalance );
      this.interestRate = interestRate;        
}


Task 3 - Creating the CheckingAccount Class

Using a text editor, create the CheckingAccount class source file in the  src/com/mybank/domain/ directory. This class must satisfy the UML diagram in Figure 6-1 on page Lab 6- 2 and the business rules defined in the introduction to "Exercise 1: Creating Bank Account Subclasses (Level 1)" on page  Lab 6-2.

    1. Declare  the CheckingAccount  class in the  com.mybank.domain package. The CheckingAccount class inherits from the Account class.

package com.mybank.domain;
public class CheckingAccount extends Account {
    //insert code here
}

    1. Add the overdraftAmount atribute to the CheckingAccount class.

     private double overdraftAmount;

    1. Add a public constructor that takes two arguments: initBalance and overdraftAmount. Pass the initBalance parameter to the super constructor. Save the overdraftAmount parameter to the instance variable.

 

public CheckingAccount ( double initBalance, double overdraftAmount ) {
    super ( initBalance );
    this.overdraftAmount = overdraftAmount;
}

    1. Add a second  public constructor that takes only one argument: initBalance. Call the first constructor with the initBalance parameter and use the default value 0.0   for the overdraftAmount parameter.

 
public CheckingAccount ( double initBalance ) {                     this ( initbalance, 0.0 );
}

    1. Override the withdraw method to use the overdraftAmount variable.

   

 public boolean withdraw( double amount ) {
    boolean result = true;
    if( balance < amount) {
        double overdraftNeeded = amount - balance;
        if( overdraftAmount < overdraftNeeded ) {
            result = false;
        } else {
            balance = 0.0;
            overdraftAmount -=overdraftNeeded;
        }
    } else {
        balance -= amount;
    }
    return result;
 }



Task 4 - Copying the TestBanking Class

Copy the TestBanking.java file file from the resources/mod06_class1/ directory into the src/com/mybank/test directory.

cp ~/resources/mod06_class1/TestBanking.java   src/com/mybank/test/


Task 5 - Compiling the  TestBanking Program

On the command line, use the javac command to compile the test program.

 cd src
 javac -d ../classes com/mybank/test/TestBanking.java


Task 6 - Running the  TestBanking Program

On the command line, use the java command to run the test program

java -cp ../classes com.mybank.test.TestBanking

You should see the output listed on page Lab 6-4.: