In this exercise, you will create a class that reads customer and account data from a flat file.
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.
package com.mybank.data;
// insert import statements here
public class DataSource {
// insert code here
}
public DataSource (String dataFilePath) {
this.dataFile = new File(dataFi1ePath);
}
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 }
cd ~/resources/mod09_textapps/TestReport.java
src/com/mybank/test/
On the command line, use the java command to run the test program.
java -cp ../classes com.mybank.test.TestReport
../data/test.dat