Exercise 1: Reading a Data File (Level 2)

In this exercise, you will create a class that reads customer and account data from a flat file.

Task 1 - Copying Resource Files

Perform the following:

  1. Create the data/ directory.
  2. Copy the test.dat file from the resources/mad09_textapps/ directory into the data/ directory.

Task 2 - Creating the Datasource Class

Create the src/com/mybank/data/ directory.


Using a text editor, create the Datasource class source file in the src/com/mybank/data/ directory. This class must satisfy the UML diagram in Figure 9-1 on page Lab 9-2.

  1. Declare the Datasource class in the com.mybank.data package.
  2. Add the dataFile attribute to the Datasource class.
  3. Add a public constructor that takes a string argument dataFilePath and initializes the dataFile attribute.
  4. Add a public method, loadData, that populates the Bank customer objects and each customer's account objects. Here is the pseudo-code for this method:
    read <number-of-cuetomers>
    for each customer record
      read <first-name> and <last-name>
      add customer to Bank
      read <number-of-accounts>
      for each account
        read <account-type-code>
        switch on <account-type-code>
          Savings :
            read <initial-balance> and <interest-rate)
            create account and add account to customer
          Checking :
            read <initial-balance> and <overdraft-amount)
            create account and add account to customer

Task 3 - Copying the TestReport Program

Copy the TestReport.java file from the resourcee/modO9_textapps/ directory into the src/cm/mybank/test/ directory. This version of the TestReport program uses the Datasource class to load the customer data.

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 lime, use the java command to run the test program. You should see the output listed in "Task 5 - Running the TestReport Program" on page Lab 9-5.