Exercise
2:Creating a Heterogeneous Collection of Customer Accounts (Level
2)
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().
- 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.
- Modify
the constructor to initialize the accounts
array.
- 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.
- Add
the getNumOfAccounts
accessor method, which returns the
numberOfAccounts
attribute.
- 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.
Task 2 - Copying and Completing the CustomerReport
Class
Perform the following:
- Make the src/com/mybank/report/ directory.
- Copy the CustomerReport.java file from the resources/mod06_class1/ directory into the src/com/mybank/report/ directory.
- Using a
text editor, complete the CustomerReport.java code. You will find
comment blocks that start and end with /*** ... ***/. These comments indicate
the location in the code that you must supply.
- 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.
- Print out the type of account and the balance.
Task 3 - Copying the TestReport
Class
Copy the TestReport.java file from the resources/mod06_class1/ directory into the src/com/mybank/test/ directory.
Task 4 - Compiling the TestReport Program
On the command line, use the javac command to compile the test program.
Task 5 - Running the TestReport Program
On the command line, use the java command to run the test program. You should see the output on page Lab6-15.