Exercise 1: Applying Static Members to a Design (Level 2)
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-2 shows 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)
-
Change all attributes to static.
-
Move the attribute initialization code from the constructor to either a static block or on the attribute declarations.
-
Change the constructor to be private and remove the body of the constructor.
-
Change all methods to static.
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.
-
Remove the bank attribute and the getBank and setBank methods.
-
Modify the generateReport method to use the static methods from the new Bank utility class design.
Task 3 - Copying the TestReport Program
Copy the TestReport .java file from the resources/mod07_class2/ 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. Yo
should see the same output from previous tests as shown in "Task 4 -
Compiling the TestReport Program" on page Lab 6-14.