Exercise
2: Creating a Heterogeneous Collection of Customer Accounts (Level
1)
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.
Figure 6-2 shows the UML class diagram of the relationships between bank, customers, and accounts. What has changed is that a Customer object may now have more than one account and these accounts may be of different types, subclasses of the Account class.
Your job is to modify the Customer class to support a heterogeneous collection of Account objects.
Preparation
Before
you begin, make sure that you have changed directories to projects/BankingPrj/ using the
cd command in the terminal de window.
cd ~/ projects/ BankingPrj/
Task
1 - Modifying the Customer
Class
Using
a text editor, modify the Customer
class source file in the
directory. Modify the src/com/mybank/domain 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:
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.
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 following output.
CUSTOMERS
REPORT
================
- Customer: Simms, Jane
- Savings Account: current balance is 500.0
- Checking Account: current balance is 200.0
- Customer: Bryant, Owen
- Checking Account: current balance is 200.0
- Customer: Soley, Tim
- Savings Account: current balance is 1500.0
- Checking Account: current balance is 200.0
- Customer: Soley, Maria
- Checking Account: current balance is 200.0
- Savings Account: current balance is 150.0