I'm currently working on an assignment for school where I need to write a program that it needs to return your change in this mode
Input: purchaseAmount: $12.45 givenAmount: $15.00
Output: Change: $2.55 2 one dollar bills 2 quarters 1 nickel
But, if I work with this case scenario it works fine. Now, If I want this scenario to work this what I get
Input: purchaseAmount: $19.51 givenAmount: $50.01
Output:
Twenty dollar bill 1 Ten dollar bill: 1 Quarters: 1 Dimes: 2 Pennies: 4
Which is incorrect since I'm returning to the costumer 30.49 instead of 30.5
Can't figure out whats the issue here.
Thanks
boolean isFiveBillOn = false, isTwentybillOn = false, isTenbillOn = false, isDollarOn = false, isQuartersOn = false, isDimesOn = false, isNicklesOn = false, isPenniesOn = false;
//retrieve amount due
String moneyd = JOptionPane.showInputDialog("Enter the amount due");
double amountd = Double.parseDouble(moneyd);
String moneyr = JOptionPane.showInputDialog("Enter amount you would like to pay");
double amountr = Double.parseDouble(moneyr);
//calculate difference
double difference = (amountr - amountd);
//convert to pennies
int balance = (int)(difference * 100);
//twenty dollar bill
int twentydollars = balance / 2000;
balance = (balance % 2000);
if (twentydollars > 0) {
isTwentybillOn = true;
}
//ten dollar bill
int tendollars = balance / 1000;
balance = (balance % 1000);
if (tendollars > 0) {
isTenbillOn = true;
}
//five dollar bill
int fivedollars = balance / 500;
balance = (balance % 500);
if (fivedollars > 0) {
isFiveBillOn = true;
}
//dollar bill
int dollars = balance / 100;
balance = (balance % 100);
if (dollars > 0) {
isDollarOn = true;
}
//quartes
int quarters = balance / 25;
balance = (balance % 25);
if (quarters > 0) {
isQuartersOn = true;
}
//dimes
int dimes = balance / 10;
balance = (balance % 10);
if (dimes > 0) {
isDimesOn = true;
}
//nickles
int nickles = balance / 5;
balance = (balance % 5);
if (nickles > 0){
isNicklesOn = true;
}
//pennies
int pennies = balance / 1;
balance = (balance % 1);
if (pennies > 0){
isPenniesOn = true;
}
//checking for difference over 0
if (difference < 0) {
JOptionPane.showMessageDialog(null, "The amount received can't be less than the amount owned");
} else {
// Returning Message
} if (isTwentybillOn) {
System.out.println("Twenty dollar bill " + twentydollars);
} if (isTenbillOn) {
System.out.println("Ten dollar bill: " + tendollars);
} if (isDollarOn) {
System.out.println("One Dollar Bill: " + dollars);
} if (isQuartersOn) {
System.out.println("Quarters: " + quarters);
} if (isDimesOn) {
System.out.println("Dimes: " + dimes);
} if (isNicklesOn) {
System.out.println("Nickles: " + nickles);
} if (isPenniesOn) {
System.out.println("Pennies: " + pennies);
} else {
System.out.println("Thanks! have a good day");
}
}