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 a 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 print the account with the highest balance. If the file is not properly formatted, give the user a chance to select another file.

Complete the following files:

import java.util.ArrayList; import; import; 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. */ public Bank() { accountList = new ArrayList<BankAccount>(); } /** Read 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) throws IOException { . . . } /** Add an account to the bank. @param b the BankAccount reference */ public void addAccount(BankAccount b) { accountList.add(b); } /** Gets the account with the highest balance. @return the account with the highest balance */ public BankAccount getHighestBalance() { BankAccount max = accountList.get(0); for (BankAccount account : accountList) { if (account.getBalance() > max.getBalance()) max = account; } return max; } }

import java.util.Scanner; import; import java.util.InputMismatchException; /** 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. */ public BankAccount() { accountNumber = 0; balance = 0; } /** Constructs a bank account with a given balance. @param initialBalance the initial balance */ public BankAccount(int anAccountNumber, double initialBalance) { accountNumber = anAccountNumber; balance = initialBalance; } /** 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) throws IOException { . . . } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { balance = balance + amount; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { balance = balance - amount; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } /** Gets the account number of the bank account. @return the account number */ public int getAccountNumber() { return accountNumber; } }

Use the following file:

import java.util.Scanner;

   This program prompts the user to enter a file name
   with account information. A bank object is filled with
   the account numbers and balances specified in the file. 
   In case of an exception, the user can choose another file.
public class BankReader
   public static void main(String[] args)
      boolean done = false;
      Scanner in = new Scanner(;
      while (!done)
         System.out.print("Filename: ");
         String filename =;

            Bank bank = new Bank();
            BankAccount highest = bank.getHighestBalance();
                  + " " + highest.getBalance());
            done = true;
         catch (IOException e)