Coin.java 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:
CoinReader.java
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();
try
{
list = Coin.readFile(filename);
done = true;
}
catch (IOException e)
{
System.out.println(e);
}
}
double total = 0;
for(Coin c : list)
total = total + c.getValue();
System.out.println("Total=" + total);
}
}