Wait wait. Here's problem numero 2.
If you compile this and try to enter say uh 45.55 for cost and 120.65 for payment amount, you will get a slightly wrong answer for the amount of change required. You wont really know what I mean unless you compile and run it yourself.
I don't really know why the double isn't specific. Like instead of returning something like $56.55c, it would give me something like $56.55000000006c. Which makes no sense if you look at the math I used...
import io.*;
public class thecode {
public static void main (String [] args) {
double cost, payment, change;
cost = ConsoleInput.readDouble ("Enter the cost of the product");
payment = ConsoleInput.readDouble ("Enter your payment amount");
change = calcChange (cost, payment);
calcNotes (change);
}
public static double calcChange (double costIN, double paymentIN) {
double changeOUT = paymentIN - costIN;
return changeOUT;
}
public static void calcNotes (double changeIN) {
double changeReal = changeIN;
int fivecent, tencent, twentycent, fiftycent, onedollar, twodollar, fivedollar, tendollar, twentydollar, fiftydollar;
fivecent = tencent = twentycent = fiftycent = onedollar = twodollar = fivedollar = tendollar = twentydollar = fiftydollar = 0;
while (changeIN > 0.0) {
if ((50.0 / changeIN) <= 1.0) {
++fiftydollar;
changeIN = changeIN - 50.0;
}else if ((20.0 / changeIN) <= 1.0) {
++twentydollar;
changeIN = changeIN - 20.0;
}else if ((10.0 / changeIN) <= 1.0) {
++tendollar;
changeIN = changeIN - 10.0;
}else if ((5.0 / changeIN) <= 1.0) {
++fivedollar;
changeIN = changeIN - 5.0;
}else if ((2.0 / changeIN) <= 1.0) {
++twodollar;
changeIN = changeIN - 2.0;
}else if ((1.0 / changeIN) <= 1.0) {
++onedollar;
changeIN = changeIN - 1.0;
}else if ((0.5 / changeIN) <= 0.5) {
++fiftycent;
changeIN = changeIN - 0.5;
}else if ((0.2 / changeIN) <= 1.0) {
++twentycent;
changeIN = changeIN - 0.2;
}else if ((0.1 / changeIN) <= 1.0) {
++tencent;
changeIN = changeIN - 0.1;
}else{
++fivecent;
changeIN = changeIN - 0.05;
}
}
outputAll (fiftydollar, twentydollar, tendollar, fivedollar, twodollar, onedollar, fiftycent, twentycent, tencent, fivecent, changeReal);
}
public static void outputAll (int fiftydollarIN, int twentydollarIN, int tendollarIN, int fivedollarIN, int twodollarIN, int onedollarIN, int fiftycentIN, int twentycentIN, int tencentIN, int fivecentIN, double changeRealIN) {
System.out.println ("Change required to give to customer: $" +changeRealIN +"c");
System.out.println ("$50: " +fiftydollarIN);
System.out.println ("$20: " +twentydollarIN);
System.out.println ("$10: " +tendollarIN);
System.out.println ("$5 : " +fivedollarIN);
System.out.println ("$2 : " +twodollarIN);
System.out.println ("$1 : " +onedollarIN);
System.out.println ("50c: " +fiftycentIN);
System.out.println ("20c: " +twentycentIN);
System.out.println ("10c: " +tencentIN);
System.out.println ("5c : " +fivecentIN);
}
}
I also had to change the last else if to an else. Because otherwise if it were the same as the others, the program would loop forever. Because the double returns a slightly off value for no apparent reason...