CSC 224: Program 4, Part I

Due: Sixth Week

Objective of this Program: (a) Working with input from files, (b) handling file exceptions, and (c) command line input. Also (d) learning about incremental development and (e) arrays of objects.

This program is inspired by the examples in Chapter 7 on arrays and Chapter 11 on handling exceptions and on file input.

The Basic Assignment

In this program we will deal with an array of BankAccount, which is defined on page 287 of the text in Section 7.2 on ArrayLists. We will use a slightly different implementation then in the text. This is because we will use this class again when we discuss inheritance.

The program will consist of two parts. The first part will be reading in data from a file to populate an array of BankAccounts. The data will be composed of names and opening balances. You will need to handle various IO exceptions.

The second part will deal with manipulating with the array. You will read in data from another file which will consist of transactions to the BankAccounts, withdrawals, deposits, and transfers. You will have to handle exceptions, both input data exceptions and file exceptions as well for the second part. At the end of the transactions, you will output certain facts about the BankAccounts, e.g., the account with the largest balance or the account with the median balance.

However, we will break this assignment up into two parts. The first part, which is this assignment, is just to read in the data and print out the resulting list of BankAccounts. The second part, due next week, is to implement the processing of the data for processing of the accounts.

How to Get Input via the Command Line

The names of the input files will be given on the command line when one executes the program. In "true" java (i.e., not using an IDE like BlueJ), one runs a program from the command line, which on PC's means the DOS prompt:

C:\program4> java nameOfExecutable

The DOS prompt is C:\program4>. This means we are in the subdirectory (or folder) of program4 on the C drive, and the > is the "prompt", which means it is waiting for a command from the user. To execute a Java program, one types in java followed by the name of the executable (a compiled Java program). One can also follow it by variables, as:

C:\program4> java nameOfExecutable bankaccounts.txt transactions.txt

Note: Having variables or parameters on the command line is a UNIX feature, and MS-DOS was initially inspired from UNIX. (Actually, Gates, Allen, and Balman bought a UNIX flavored operating system and modified it to become MS-DOS.)

The variables which follow are read in as String into the program. When one types in the main program:

public static void main(String [] args)

the variables "bankaccounts.txt" and "transactions.txt" are read into the array of String as args[0] and args[1]. That is to say, in the method main, we can refer to the variable args[0], which will have the value of "bankaccounts.txt", and and the variable args[1], which will have the value of "transactions.txt".

To do this in BlueJ, we do the following. When we execute a program in BlueJ, i.e., click on the "run main" option in the menu, we get the following box opened:

The variables we want sent to the program are typed between the { and the }. However, we must put them in " and separate them by commas. (This is just an artifact of BlueJ):

After we type in the variables we want, we click on the OK button to continue execution of the program.

In our program, the first variable will be the name of the file where we can find the data to create the array of BankAccount. The file will have an int as the first value, telling how accounts are to be created. Then the next lines will have the name and opening balance of each of the accounts, one per line. Note that some lines will only have the name.

The second variable tells the name of the file where the transactions are. We will discuss this file in Part II of the assignment.

To simplify things, I suggest you put the data files in the project subdirectory. Then you just have to give the file name on the command line.

The BankAccount Class

The UML of the BankAccount Class is


	 __________________________________________________________________ 
	|	BankAccount 						   |
	|__________________________________________________________________|
	| - static long assignActNumber 				   | 
	| - double penalty 						   | 
	| - long accountNumber 						   | 
   	| - double balance  						   |
   	| - String name						  	   |
	|__________________________________________________________________|
	| < constructors>						   |
	| 	+ Account(String AccountName, double Balance)		   |	
	| 	+ Account(String AccountName )				   |
	| < mutators>							   |
	| 	+ deposit(double amount): int				   |
	| 	+ withdraw(double amount) : int 			   |
	| 	+ transfer(double amount,Account acc) :int 		   |
	| < accessors>							   |
	| 	+ getBalance() :double					   |
	| 	+ getAccountNumber() :long				   |
	| 	+ getName() :String					   |
	| 	+ setName(String newName) :void				   |
	| 	+ getPenalty()	:String					   |
	| 	+ setPenalty(double newPenalty) :void			   |
	| 	+ toString() :String					   |
	|__________________________________________________________________|

	The symbol "-" means it is private, and "+" means it is public.

To see the ADT of the BankAccount class click here.

You can download the BankAccount.java class, so you do not have to write the code to implement it.

Design of the ProcessBankAccounts

This is the class which will work with the array of BankAccounts. Its UML will look like:

	_________________________________________
       |		ProcessBankAccounts	 |
       |_________________________________________|
       |  -BankAccount[] accounts;		 |
       |  -int n; //number of elements in array	 |
       |  -FileWriter error;//for error in data  |
       |_________________________________________|
       |< constructor>				 |
       |  +ProcessBankAccounts()		 |
       |< accessor> 				 |
       |  + getArraySize():int			 |
       |  + getBankAccounts():BankAccount[]	 |
       |< mutators> 				 |
       |  + readArray(String fileName):void	 |
       |  + printArray():void			 |
       |  + processArray(String fileName):void	 |
       |  + overdrawn():void		 	 |
       |_________________________________________|

The constructor ProcessBankAccounts will just be a default constructor. Nothing will be passed to it and nothing will be done.

The two methods of getArraySize and getBankAccounts will return the values of n and BankAccounts, respectively.

The calling procedure is able to access this array by:

ProcessBankAccounts account = new ProcessBankAccounts();
int n = account.getArraySize();
BankAccount [] X = new BankAccount[n];

X = account.getBankAccounts();
Then X is a array of n BankAccount numbers. It can be used in the method which creates the ProcessBankAccounts to find the account with the largest balance or the accounts which exceed a given amount,etc.

One can look at the ADT of ProcessBankAccounts for more details.

Design of the readArray method

The input method readArray will read in the primitive data and create the array of BankAccounts. It will use the BufferedReader for input. This is because each line could have either one or two inputs: (1) just the name on the account or (2) the name and the opening balance. See the handout on files for some examples on how to use the BufferedReader class. The examples are given in Section III.

The protocol for this will be

public void readArray(String fileName)

You will send it the name of the file where the data is stored (as a String).

Be sure to catch both FileNotFoundException and IOException. An example of using both of the exceptions is found on page 492 of the text (Section 11.6). The example is using the Scanner class, but it is the same for BufferedReader.

One handles the the FileNotFoundException as:

try
{
 
 //try and open file here

}
catch(FileNotFoundException event)
{
	System.out.println("Unable to open file" + file.getAbsolutepath() +
		".  Check if file exists or path is incorrect.");
        System.exit(0);
}

The handout on files has some more examples on how do deal with files.

readArray will first open the file. The name of the file as a String has been passed. It will then read in the first data item, which is an integer, n, which tells how many data items will follow. It will assign this value to the class variable n. It will then create an array of BankAccounts of size n.

accounts = new BankAccount[n];

Next it will read in the data one line at a time for the next n lines. This should be done with a for loop discussed in Section 6.2. Then use the StringTokenizer class to get the name and (perhaps) opening balance.

BufferedReader bufReader;
StringTokenizer tokenizer;
String line,name;
double openingBalance;

line = bufReader.readLine();
tokenizer = new StringTokenizer(line);
name = tokenizer.nextToken();
openingBalance = Double.parseDouble(tokenizer.nextToken());

If there is no number after the name, the line of
openingBalance = Double.parseDouble(tokenizer.nextToken());
will throw an NoSuchElementException which must be caught with a try...catch clause.

The other way to test if there is a second element is the method of hasMoreTokens method. It will return true if there is a token (in this case a number) on the line. Otherwise it returns false.

Once you have the data you will create an element of the array of BankAccounts via a

accounts[i] = new BankAccount(name,openingBalance);

or

accounts[i] = new BankAccount(name);

depending on the type of input data.

Design of the printArray method

The printArray method will print out the list of accounts with their account number, name, and current balance. It will do with one one account per line and with a header line before the output. So a typical output would look like:

Account Number		Name		Balance
1000			account1	$4100.30
1001			account2        ($100.00)
1002			account3	$0.00
1003			account3	$873.46
....
It's protocol is

public void printArray()

and writes the output to the terminal window.

Design of the overdrawn method

The overdrawn method will print out the list of accounts with their account number, name, and current balance, whose balance is less than zero. It will do with one one account per line and with a header line before the output. So a typical output would look like:

Account Number		Name		Balance
1001			account2        ($100.00)
....
You should use the enhanced for loop to print these out. See page 292 in Section 7.4 for an example of this.

The method of processing data

This procedure will read in the data from a file and do the appropriate action asked for on the array of BankAccounts. We will not implement this method now. It will be implemented in part II of the assignment. However, we will just have a stub there. This is a common program technique in incremental development.

A stub has the prototype of the method, but it's body is just a statement that one has arrived there. In our case, we will put out the statement to the console window via System.out.println.

Thus, when the program runs, the method processArray will be called, but only a statement to the console window will be printed when the method is called. In part II, we will write the code for this method. However, the test class will not be modified, and only the processing method will be modified in part II.

The main Testing Method

This procedure will

Note that for (7) above, you should have code as follows:

BankAccounts [] accounts;
int n;
ProcessBankAccounts banking = new ProcesBankAccounts();

n = banking.getArraySize();
accounts = banking.getBankAccounts();

Note that for (8) above, you will need to traverse the array twice: (1) once to find the largest amount and (2) print out those accounts whose balance equals the largest amount.

For (8), you must use the enhanced for loop. An example for BankAccounts is given on page 292 of the text in section 7.4. You also should use it elsewhere, e.g., in the printArrays method.

You should have header statements before any of the calls to output data. E.g., the accounts before processing are, etc.

The Test Data Set

There are 2 data test sets data for creating the array and data for processing the array .

What to Hand-in

You should post your project as a 'zip' file on the COL website. It should be posted under the appropriate assignment. Also indicate how many hours you worked on the program in the Readme.txt file.


Homepage of CTI School back to 211 homepage