CECS 277
LAB ASSIGNMENT #3
Assigned date: 2/20
Due date: Mon 3/13
25 points
1. [10 points] Rewrite the lab assignment 2 by reading the data from a file. You need to create a class that has a method to read data from a file and then load them in a list collection. Decide your own design on how store data on the file. You might want to use different files to store different types of employees. Implement exceptions in your program.
Grading
2. [15 points] Design a class Bank that contains a number of bank accounts. Each account has an account number and a current balance. Add an accountNumber field to the BankAccount class. Store the bank accounts in an array list. Write a readFile method of the Bank class for reading the file with the format:
accountNumber1 balance1
accountNumber2 balance2
Implement read methods for the Bank and BankAccount classes. Write a sample program to read in a file with bank accounts, then prints the account with the highest balance. If the file is not properly formatted, give the user a chance to selecct another file.
Partial design of classes BankAcount, Bank, and BankReader.
import java.util.Scanner;
import java.io.IOException;
import java.util.NoSuchElementException;
/**
A bank account has a balance that can be changed by deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
private int accountNumber;
/**
Constructs a bank account with a zero balance.
*/
/**
Constructs a bank account with a given account number and a initial balance.
*/
/**
Reads an account number and balance.
@param in the scanner
@return true if the data was read
false if the end of the stream was reached
*/
public void read(Scanner in) ___________________________
{
try
{
} catch
{
throw new _____________________________
}
}
Note: You need to check for incompatible input.
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
/**
Gets the current balance of the bank account.
@return the current balance
*/
/**
Gets the account number of the bank account.
@return the account number
*/
}
___________________________________________________
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
import java.util.NoSuchElementException;
/**
A bank contains account numbers and balances of each customer.
*/
public class Bank
{
private ArrayList<BankAccount> accountList;
/**
Construct a Bank object.
*/
/**
Reads a file with account numbers and balances and adds the accounts
to the bank.
@param filename the name of the file
*/
public void readFile(String filename) ________________________
{ //Create File object, a scanner object to read data from the file.
//call the method read
File reader = new File(filename);
Scanner in = new Scanner(reader);
try
{
}
finally
{
}
}
Note: call the method read
/**
Read a file with account numbers and balances and adds the accounts
to the bank.
@param in the scanner for reading the input
*/
private void read(Scanner in)___________________________
{
while (in.hasNext())
{//Create a BankAccount object
//input data for the BankAccount object
//Add the BankAccount object to the accountlist
}
}
/**
Add an account to the bank.
@param b the BankAccount reference
*/
/**
Gets the account with the highest balance.
@return the account with the highest balance
*/
public BankAccount getHighestBalance()
{
}
}
________________________________________________
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
/**
Reads bank accounts from a file and prints highest balance.
*/
public class BankReader
{
/**
Prompts for and reads name of file to process.
@param in Scanner from which to read
@return file name
*/
private static String getFileName(Scanner in)
{
String filename = null;
System.out.println("Enter file to process: ");
if (in.hasNext())
{
filename = in.next();
}
return filename;
}
public static void main(String[] args)
{
Bank bank = new Bank();
Scanner in = new Scanner(System.in);
String filename = getFileName(in);
boolean done = false;
while (____________________________________)
{
try
{//read the file
//display the highest balance
}
//catch file not found exception andget the file name again
catch
{
System.err.println(filename + " not found");
__________________________________
}
//catch for improperly formatted line and get the file name again
catch (IOException e)
{
System.err
.println(filename + " contains improperly formatted line");
_______________________________________
}
}
}
}
Grading