import java.io.File;
import java.util.Scanner;
public class CountCoins {
public static boolean trace = false;
public static void processFile (Scanner input) {
int coins = 0;
String coin_type = "";
double total = 0.0;
while (input.hasNextLine()) {
String line = input.nextLine();
// split the line into an array of words parsing and whitespace
String[] arr = line.split("\\s");
if (trace)
for (int j=0; j< arr.length; j++)
System.out.println("Trace token["+j+"] = " + arr[j]);
total = 0.0;
int i = 0;
while(i < arr.length) {
coins = Integer.parseInt(arr[i]);
i++;
if (i < arr.length) { // get coint type
coin_type = arr[i].toLowerCase();
if (coin_type.equals("pennies"))
total = total + 0.01*coins;
else if (coin_type.equals("nickels"))
total = total + 0.05*coins;
else if (coin_type.equals("quarters"))
total = total + 0.25*coins;
else if (coin_type.equals("dimes"))
total = total + 0.10*coins;
else
System.out.println("Unknown coin type ignored: "+coin_type);
i++;
if (trace)
System.out.printf(" subTotal Money = $%1.2f \n",total);
} // processing the line of input
} // expecting 10 entries, of 5 pairs of # coin-type - process in pairs
System.out.printf("Total Money = $%1.2f \n",total);
} // while
}
public static void main(String[] args) {
fileUtils a = new fileUtils();
CountCoins b = new CountCoins();
Scanner sc = null;
if (args.length > 0) {
if (b.trace)
System.out.println("arglen = " + args.length + " " + args[0]);
sc = a.getScanner(args[0]);
}
else
sc = a.getScanner("");
b.processFile(sc);
sc.close();
} // main
} // CountCoins
|