// CodeEval Cash Register
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
class Main {
public static void main(String[] args) {
cash_register.main(args);
}
}
class cash_register {
public static void main(String[] args) {
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
if (LOCAL_TEST)
CodeEvalGetInput("e:\\zin.txt");
else
CodeEvalGetInput(args[0]);
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
Initialize();
String line;
while ((line = in.readLine()) != null) {
String[] lineArray = line.split(";");
if (lineArray.length > 0) {
// Process line of input Here
Process(lineArray);
}
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
static Map<Double, String> coins;
static List<Double> coinvals;
private static void Initialize() {
coins = new LinkedHashMap<Double, String>();
coins.put(100.0d, "ONE HUNDRED");
coins.put(50.0d, "FIFTY");
coins.put(20.0d, "TWENTY");
coins.put(10.0d, "TEN");
coins.put(5.0d, "FIVE");
coins.put(2.0d, "TWO");
coins.put(1.0d, "ONE");
coins.put(0.5d, "HALF DOLLAR");
coins.put(0.25d, "QUARTER");
coins.put(0.1d, "DIME");
coins.put(0.05d, "NICKEL");
coins.put(0.01d, "PENNY");
coinvals = new ArrayList<Double>();
Set<Double> v = coins.keySet();
for (Double d : v) {
coinvals.add(d);
}
}
private static void Process(String[] lineArray) {
double PP = Double.valueOf(lineArray[0]);
double CH = Double.valueOf(lineArray[1]);
// System.out.println(PP + ";" + CH + " : " + (PP - CH));
if (CH < PP) {
System.out.println("ERROR");
return;
} else if (CH - PP < 0.001) {
System.out.println("ZERO");
return;
} else {
List<String> chg = new ArrayList<String>();
double money = CH - PP + 0.001;
while (money > 0.005) {
for (Double val : coinvals) {
if (money >= val) {
money -= val;
String c = coins.get(val);
chg.add(c);
break;
}
}
}
String[] achg = chg.toArray(new String[chg.size()]);
Arrays.sort(achg);
for (int i = 0; i < chg.size(); i++) {
if (i > 0)
System.out.print(",");
System.out.print(achg[i]);
}
System.out.println();
}
}
}
you forgot to close the ressource in after reading the file in method CodeEvalGetInput.
ReplyDelete