Exercise 2: Creating a Heterogeneous Collection of Customer Accounts (Level  3)


In this exercise, you will create a heterogeneous array to represent the aggregation of customers to accounts. That is, a given customer can have several accounts of different types.





Task 1 - Modifying the Customer Class

Using a text editor, modify the Customer class source file in the src/com/mybank/domain directory. Modify the Customer class to handle the accounts association with generic multiplicity; just as you did in the "Exercise 2 - Using Arrays to Represent One-to-Many Associations ( Level 1 )" on page Lab 5-6. It must include the public methods:

addAccount(  Account ), getAccount( int ), and getNumOfAccounts().

 

  1. Add two attributes to the Customer class; accounts (an array of Account objects ) and  numberOfAccounts (an integer that keeps track of the next accounts array index). This replaces the single account reference variable, which should be removed.

private Account[] accounts;
private int numberOfAccounts;



  1.  Modify the constructor to initialize the accounts array.

 

public Customer( String f, String l ) {
    firstName = f;
    lastName = l;
    // initialize accounts array
    accounts = new Account[10];
    numberOfAccounts = 0;
}

 

  1.  Add the addAccount method. This method takes a single parameter, an Account object, and stores it  in the accounts array. It must also increment the numberOfAccounts atribute. This method replaces the setAccount method, which should be removed.

public void addAccount( Account acct ){
    int i = numberOfAccounts++;
    accounts[i] = acct;
}

  1. Add the getNumOfAccounts accessor  method, which returns the numberOfAccounts attribute.

public int getNumOfAccounts(){
    return numberOfAccounts;
}

  1. Add the getAccount method. This method returns the account associated with the given index parameter. This method replaces the previous getAccount method, which should be removed.

 

public Account getAccount( int account_index ){
    return accounts[account_index];
}


Task 2 - Copying and Completing the CustomerReport Class

Perform the following:
  1. Make the src/com/mybank/report/ directory.

mkdir src/com/mybank/report

  1. Copy the CustomerReport.java file from the resources/mod06_class1/ directory into the src/com/mybank/report/ directory.

cp ~/resources/mod06_class1/CustomerReport.java  src/ om/mybank/report

  1. Using a text editor, complete the CustomerReport.java code. You will find comments block that start and end with   /*** ... ***/. These comments indicate the location in the code that you must supply.

 

a.   Use the intanceof operator to test what type of account this is and set account_type to an appropriate value, such as Savings Account or Checking Account.

if( account instanceof SavingsAccount ){
    account_type = “Savings Account”;
}else if( account instanceof CheckingAccount ){
    account_type = “Checking Account”;
} else {
    account_type = “Unknown Account Type”;
}

 

b.    Print out the type of account and the balance.

System.out.println( “    + account_type + “: current balance is ” + account.getBalance() );


Task 3 - Copying  the TestReport Class

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

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

Task 4 - Compiling  the TestReport Program

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

cd src
javac -d ../classes  com.mybank.test.TestReport

Task 5 - Running  the TestReport Program

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

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

You should see the output on page Lab6-15.