Write a program that reads in a set of coin descriptions from a file. The input file has the format

coinName1 coinValue1
coinName2 coinValue2
. . .

Add a method

void read(Scanner in) throws FileNotFoundException

to the Coin class. Throw an exception if the current line is not properly formatted. Then implement a method

static ArrayList<Coin> readFile(String filename)
   throws FileNotFoundException

In the main method, call readFile. If an exception is thrown, give the user a chance to select another file. If you read all coins successfully, print the total value.

Complete the following file:


import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.ArrayList; /** A coin with a monetary value. */ public class Coin { private double value; private String name; /** Constructs a default coin. Use the read method to fill in the value and name. */ public Coin() { value = 0; name = ""; } /** Constructs a coin. @param aValue the monetary value of the coin @param aName the name of the coin */ public Coin(double aValue, String aName) { value = aValue; name = aName; } /** Gets the coin value. @return the value */ public double getValue() { return value; } /** Gets the coin name. @return the name */ public String getName() { return name; } /** Reads a coin value and name. @param in the Scanner */ public void read(Scanner in) throws IOException { . . . } /** Reads a file with coin values. @param filename the file name @return an array of coins in the file */ public static ArrayList<Coin> readFile(String filename) throws IOException { . . . } }

Use the following file:


import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;

public class CoinReader
   public static void main(String[] args)
      boolean done = false;
      Scanner in = new Scanner(System.in);
      ArrayList<Coin> list = null;
      while (!done)
         System.out.print("Filename: ");
         String filename = in.next();

            list = Coin.readFile(filename);
            done = true;
         catch (IOException e)

      double total = 0;
      for(Coin c : list)
         total = total + c.getValue();
      System.out.println("Total=" + total);