Exercise 1: Applying Static Members to a Design (Level 3)

In this exercise, you will apply static class members to resolve a design decision. The Banking Project uses currently a concrete class to represent the concept of a bank, which contains the set of customers of the bank. The project team has decided that this is a risky design because it would be possible to instantiate multiple Bank objects each with the potential to contain different sets of customers.

The design team has decided to make the Bank class a utility class. A utility class is one that is not instantiated and all of its members are static. Figure 7-1 on page Lab 7-2shows the new design for the Bank class. Your job is to program these changes to the Bank class and to all of the classes tha use the Bank class.



Task 1 - Modifying Bank Class

Using a text editor, modify the Bank class source file un the src/com/mybank/domain/ directory. Modify the Bank class to convert instance members (both attributes and methods) to static members.Applying Static Members to a Design (Level 1)

1. Change all attributes to static.

private static Customer[] customers;
private static int  numberOfCustomers;

2. Move the attribute initialization code from the constructor to either a static block or on the attribute declarations.

static {
    customers = newCustomer[10];
    numberOfCustomers = 0;
}

3. Change the constructor to be private and remove the body of the constructor.

private Bank() {
    //this cosntructor should never be called
}

4. Change all methods to static.

public static void addCustomer(String f, String l) {
    int i = numberOfCustomers++;
    customers[i] = new Customer(f, l);
}
public static int getNumOfCustomers(){
    return numberOfCustomers;
}
public static Customers getCustomers(int customer_index) {
    return customers[customer_index];
}


Task 2 - Modifying the CustomerReport Class

Using a text editor, modify the Customerreport class source file in the src/com/mybank/report/ directory. Modify the CustomerReport class to use the Bank class a utility class.

1. Remove the bank attribute and the getBank and setBank methods.
2. Modify the generateReport method to use the static methods from the new Bank utility class design.

public void generateReport() {
    //Print report header
    System.out.println("CUSTOMERS REPORT");
    System.out.println("=================");

    //For each customer...
    for ( int cust_idx = 0; cust_idx < Bank.getNumOfCustomers(); cust_idx++ ) {
        Customer customer = Bank.getCustomer(cust_idex);
        //add so on...
    }
}


Task 3 - Copying the TestReport Program

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

cp ~/resources/mod07_class2/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.java


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

Yo should see the same output from previous tests as shown in "Task 4 - Compiling the TestReport Program" on page Lab 6-14.