Exercise 1: Reading a Data File (Level 3)

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.
    mkdir data

  2. Copy the teat.dat file from the resources/madO9_textapps/ directory into the data/ directory.
    cp ~/resources/mod09_textapps/test.dat data/

Task 2 - Creating the Datasource Class

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

mkdir src/com/mybank/data

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.
             package com.mybank.data;
             // insert import statements here
             public class DataSource { 
               // insert code here
             }
  2. Add the dataFile attribute to the DataSource class.
    private File dataFile;
  3. Add a public constructor that takes a string argument dataFilePath and initializes the dataFile attribute.

    public DataSource (String dataFilePath) {

    this.dataFile = new File(dataFi1ePath);

    }

  4. Add a public method, loadData, that populates the eank customer objects and each customer's account objects, shown as follows:
    public void loadData() throws IOExceptim {
    // Data source variables
    Scanner input = new Scanner(dataFi1e);
    // Domain variables
    Customer customer;
    int numOfCustomers = input.nextInt();
    for ( int idx = 0; idx < numOfCustomers; idx++ ) {
    // Create customer object
    String firstName = input.next();
    String lastName = input.next();
    Bank.addCustomer(firstName, lastName);
    customer = Bank.getCustomer(idx);
    // Create customer accounts
    int numOfAccounts = input.nextInt();
    while ( numOfAccounts --> 0 ) {
    // Create a specific type of account
    char accountType = input.next().charAt(0);
    switch ( accountType ) {
    // Savings account
    case 'S': {
    float initBalance = input.nextFloat();
    float interestRate = input.nextFloat();
    customer.addAccount (new SavingsAccount (initBalance,
    interestRate));
    break; }
    // Checking account
    case 'C' : {
    float initBalance = input.nextFloat();
    float overdraftProtection = input.nextFloat();
    customer.addAccount(new CheckingAccount(initBalance,
    overdraftProtection));
    break;
    }
    } // END of switch
    ) // END of create accounts loop
    } //END of create customers loop }

Task 3 - Copying the TestReport Program

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

cd ~/resources/mod09_textapps/TestReport.java
      src/com/mybank/test/

Task 4 - 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 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
../data/test.dat

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