So I need help on how to calculate how much change to give back to a customer. I need the program to give change in the least bills and coins as possible.
Ex. Customer's item costs $13.37.
He/She pays $20.
Program gives back:
One $5 Bill
One $1 Bill
Two Quarters
One Ten Cent
Three One Cents
Bills used:
Any kind of bills/coins used regularly today in the United States up to $20.
No Half Dollars, $2 dollar bills, and Dollar coins.
I've gotten most of my code down, but the math portion of it is really, really bad!
import javax.swing.JOptionPane;
public class Project {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double amountGiven = 0;
double changeGiven = 0;
double amountNeeded = 0;
final double ONE_CENT = 0.01;
final double FIVE_CENT = 0.05;
final double TEN_CENT = 0.10;
final double TWENTYFIVE_CENT = 0.25;
final int ONE_DOLLAR = 1;
final int FIVE_DOLLAR = 5;
final int TEN_DOLLAR = 10;
final int TWENTY_DOLLAR = 20;
amountNeeded = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the cost for an item!"));
JOptionPane.showMessageDialog(null, "The customer needs to pay: $" + amountNeeded);
amountGiven = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter an amount a customer payed!"));
final double tempDouble = amountGiven;
changeGiven = Math.round(tempDouble - amountNeeded);
int billBack20 = (int) (tempDouble / TWENTY_DOLLAR);
int billBack10 = (int) (tempDouble / TEN_DOLLAR);
int billBack5 = (int) (tempDouble / FIVE_DOLLAR);
int billBack1 = (int) (tempDouble / ONE_DOLLAR);
int coinBack25 = (int) (tempDouble / TWENTYFIVE_CENT);
int coinBack10 = (int) (tempDouble / TEN_CENT);
int coinBack5 = (int) (tempDouble / FIVE_CENT);
int coinBack1 = (int) (tempDouble / ONE_CENT);
if ( changeGiven < 0 == true ) { JOptionPane.showMessageDialog(null, "Stop being cheap, and pay up!");
if ( changeGiven < 0 == false ) JOptionPane.showMessageDialog(null, "The amount you will get back is: " + changeGiven);}
JOptionPane.showMessageDialog(null,
"You will be given back:\n" +
"Twenty Dollar Bills:" + billBack20 +
"\nTen Dollar Bills:" + billBack10 +
"\nFive Dollar Bills:" + billBack5 +
"\nOne Dollar Bills:" + billBack1 +
"\nand" +
"\nTwentyfive Cents:" + coinBack25 +
"\nTen Cents:" + coinBack10 +
"\nFive Cents:" + coinBack5 +
"\nOne Cents:" + coinBack1);
}
}
Thanks!