Exercise 1: Reading a Data File (Level 1)

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


Figure 9-1 shows the UML diagram for the DataSource class that you will create for the TestReport program.


Figure 9-1

Figure 9-1 The DataSource Class Loads Customer Data from a Flat File

Code 9-1 shows an example of the format of the customer data file. The first lime contains an integer, which determines the number of customers in the data file. A customer record contains the first name, last name, and the number of accounts, separated by tab characters. Each account record contains a single-character code that determines the type of account and also the data in that record.


Code 9-1 Data File Format

<number-of-customers>

<first-name> <last-name> <number-of-accounts>
<account-type-code> <datum1> <datum2>

Code 9-2 shows an example of the format of the customer data file. This data file contains four customer records. The first is for Jane Simms; Jane has two bank accounts. The first account is a savings account, with an initial balance of 500.00 and an interest rate of 5 percent (0.05). The second account is a checking account with an initial balance of 200.00 and overdraft protection of 400.00.

Code 9-2 Example Test Data File

4

Jane    Simns     2
	S       500.00    0.05
	C       200.00    400.00
	
	
	Owen    Bryant    1
	C       200.00    0.00
	
	
	Tim     Soley     2
	S       1500.00   0.05
	C       200.00    0.00
	
	
	Maria   Soley     1
	S       150.00    0.05

Preparation

Before you begin, make sure that you have changed directories to projects/BankingPrj/ using the cd command in the terminal window.

cd ~/projects/ BankingPrj/

Task 1 - Copying Resource Files

Perform the following:
	   1.   Create the data/ directory.
	   2.   Copy the test .dat file from the resources/mod09_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/can/mybank/data/ directory. This class must satisfy the UML diagram in Figure 9-1 on page Lab 9-2. The loadData method must use Bank utility methods (addCustomer and getastaner) to popdate the customers recorded in the data file. Furthermore, the Customer class has the addAccount method to add the accounts from the data file.

Task 3 - Copying the TestReport Program

Copy the TestReport.java file from the resources/modO9_textapps/ directory into the src/com/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 line, use the java command to run the test program. You should see the following output:
	   
	   Reading data file: ../data/teat.dat
							CUSTOMERS REPORT
							================
	 Customer: Simms, Jane
		 Savings Account: current balance is 500.0
		 Checking Account: current balance is 200.0
	
	 Customer: Bryant, Owen
		 Checking Account: current balance is 200.0
	
	 Customer: Soley, Tim
		 Savings Account: current balance is 1500.0
		 Checking Account: current balance is 200.0
	 
	 Customer: Soley, Maria
		 Savings Account: current balance is 150.0