Skip to content

Commit b961123

Browse files
authored
Create CashRegister.java
1 parent 916d267 commit b961123

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Moderate-Level/CashRegister.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
The goal of this challenge is to design a cash register program. You will be given two float numbers. The first is the purchase price (PP) of the item. The second is the cash (CH) given by the customer. Your register currently has the following bills/coins within it:
3+
*/
4+
5+
import java.io.*;
6+
import java.math.BigDecimal;
7+
import java.util.ArrayList;
8+
public class Main {
9+
ArrayList<String> al = new ArrayList<String>();
10+
public static void main (String[] args) throws IOException {
11+
File file = new File(args[0]);
12+
BufferedReader br = new BufferedReader(new FileReader(file));
13+
String line;
14+
Main m = new Main();
15+
while ((line = br.readLine()) != null) {
16+
m.al.clear();
17+
String[] s = line.split(";");
18+
BigDecimal i2 = new BigDecimal(s[1]);
19+
BigDecimal i1 = new BigDecimal(s[0]);
20+
BigDecimal diff = new BigDecimal(i2.subtract(i1).toString());
21+
if (diff.compareTo(BigDecimal.ZERO) < 0) {
22+
System.out.println("ERROR");
23+
}
24+
else if (diff.compareTo(BigDecimal.ZERO) == 0) {
25+
System.out.println("ZERO");
26+
}
27+
else {
28+
ArrayList<String> al2 = new ArrayList<String>();
29+
al2 = m.getChange(diff);
30+
String f = "";
31+
for (int i = 0; i < al2.size(); i ++) {
32+
f += al2.get(i) + ",";
33+
}
34+
System.out.println(f.substring(0, f.length()-1));
35+
}
36+
}
37+
br.close();
38+
}
39+
ArrayList<String> getChange(BigDecimal bd) {
40+
if ((bd.compareTo(new BigDecimal(100)) >= 0)) {
41+
al.add("ONE HUNDRED");
42+
return getChange(bd.subtract(BigDecimal.valueOf(100)));
43+
}
44+
else if ((bd.compareTo(BigDecimal.valueOf(50)) >= 0)) {
45+
al.add("FIFTY");
46+
return getChange(bd.subtract(BigDecimal.valueOf(50)));
47+
}
48+
else if ((bd.compareTo(BigDecimal.valueOf(20)) >= 0)) {
49+
al.add("TWENTY");
50+
return getChange(bd.subtract(BigDecimal.valueOf(20)));
51+
}
52+
else if ((bd.compareTo(BigDecimal.TEN) >= 0)) {
53+
al.add("TEN");
54+
return getChange(bd.subtract(BigDecimal.TEN));
55+
}
56+
else if ((bd.compareTo(BigDecimal.valueOf(5)) >= 0)) {
57+
al.add("FIVE");
58+
return getChange(bd.subtract(BigDecimal.valueOf(5)));
59+
}
60+
else if ((bd.compareTo(BigDecimal.valueOf(2)) >= 0)) {
61+
al.add("TWO");
62+
return getChange(bd.subtract(BigDecimal.valueOf(2)));
63+
}
64+
else if ((bd.compareTo(BigDecimal.valueOf(1)) >= 0)) {
65+
al.add("ONE");
66+
return getChange(bd.subtract(BigDecimal.valueOf(1)));
67+
}
68+
else if ((bd.compareTo(BigDecimal.valueOf(0.5)) >= 0)) {
69+
al.add("HALF DOLLAR");
70+
return getChange(bd.subtract(BigDecimal.valueOf(0.5)));
71+
}
72+
else if ((bd.compareTo(BigDecimal.valueOf(0.25)) >= 0)) {
73+
al.add("QUARTER");
74+
return getChange(bd.subtract(BigDecimal.valueOf(0.25)));
75+
}
76+
else if ((bd.compareTo(BigDecimal.valueOf(0.1)) >= 0)) {
77+
al.add("DIME");
78+
return getChange(bd.subtract(BigDecimal.valueOf(0.1)));
79+
}
80+
else if ((bd.compareTo(BigDecimal.valueOf(0.05)) >= 0)) {
81+
al.add("NICKEL");
82+
return getChange(bd.subtract(BigDecimal.valueOf(0.05)));
83+
}
84+
else if ((bd.compareTo(BigDecimal.valueOf(0.01)) >= 0)) {
85+
al.add("PENNY");
86+
return getChange(bd.subtract(BigDecimal.valueOf(0.01)));
87+
}
88+
else {
89+
return al;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)