Exercise 2: Using Collections to Represent Association (Level 3)

In this exercise, you will use generic collections to represent class associations in the Bank project domain model

Task 1 - Modifying the Bank Class

Using a text editor, modify the Bank class source file in the src/com/mybank/domain/ directory. This class must satisfy the UML diagram in Figure 9-2 on page Lab 9-11.


  1. Modify the declaration for the customers attribute to be of type List<Customer>, and drop the numberOfCustomers attribute.
    private static List<Customer> customers;

  2. Modify the static block to initialize the customers attribute to be a new ArrayList object.
    static {
         customers = new ArrayList<Customer>(f, l);
    }           
  3. Modify the addcustomer method to use the add method.
    public static void addCustomer(String f, String l) {
       customers.add(new Cuetomer (f, l)) ;
    }
  4. Modify the getCustomer method to use the get method.
    public static Customer getCustomer(int customer_index) { 
         return customers.get(customer_index);
    }
    
  5. Modify the getNumOfCustomers method to use the size method.
    public static int getNumOfCustomers() {
         return customers.size();
    }

 

Task 2 - Modifying the Customer Class

Using a text editor, modify the Customer class source file in the src/com/mybank/domain/ directory. This class must satisfy the UML diagram in Figure 9-2 on page Lab 9-11.

  1. Modify the declaration for the accounts attribute to be of type List<Account>, and drop the numberOfAccounts attribute
    private List<Account> accounts;
  2. Modify the constructor to initialize the accounts attribute to be a new ArrayList object.

    public Custmor(String f, String l) {
    firsmame = f;
    lastName = l;
    // initialize accounts attribute
    accounts = new ArrayList<Account>(10);
    }

  3. Modify the addAccount method to use the add method.
             
    public void addAccount(Account acct) {
       accounts.add(acct);
    }
           
  4. Modify the getAccount method to use the get method.
    public Account getAccount(int account_index) {
       return accounts.get(account_index);
    }
             
           
  5. Modify the getNumOfAccounts method to use the size method.
    public int getNumOfAccounts(){
           return accounts.size();
    
    }

Task 3 - Compiling the TestReport Program

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

cd src
  javac ~d ../classes  /com/mybank/test/TestReport.java

Task 4 - Running the TestReport Program

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

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

You should see the output listed in "Task 4 - Running the TestReport Program" on page Lab 9-13.