Hi im having trouble with this Cash Register program im writing. Specifically the output2 method. Im attempting to to find a way to output the specific dollar amounts the user will receive in change without writing it as:
system.out.println("Your change will be "+hundred+" on hundred dollar bills "+fifty+" fifty dollar bills ETC");
But instead include the dollar value if it fits with the change the user will receive.
I hope this wasnt too confusing to read.
int ranNum, hundred, fifty, twenty, ten, five, one, quarter, dime, nickel, penny;
double original, discount, savings, salesprice, tax, total, payment, change, quarterV = .25, dimeV = .10, //V as in value
nickelV = .05, pennyV = .01;
DecimalFormat percent = new DecimalFormat("0%");
DecimalFormat money = new DecimalFormat("$0.00");
public void input()
{
System.out.println("Hello, this program will ask you for a price of an item you would like to purchase");
System.out.println("and return a random discount from 5-75 (multiple of 5). Then return the following:");
System.out.println("original price, discount percent, discount amount, sales price, tax and total price with 7% tax.\n");
System.out.println("Please give me the price of an item you would like to purchase");
original = scan.nextDouble();
scan.nextLine();
}
public void calculations()
{
//This will be used to find the random discount given to the user:
ranNum = random.nextInt(15)+1;
discount = ((ranNum*5)*.10)*.10;
//This will be used to find the amount the user will save:
savings = (discount*original);
//This will be used to find the salesprice of the item being purchased:
salesprice = original - savings;
//This will be used to find the total price of the item after 7% tax deductions
tax = (salesprice*7)/100;
//This will be used to find the final total the customer must pay
total = salesprice + tax;
}
public void change()
{
change = payment - total;
hundred = (int) Math.floor(change/100);
fifty = (int) Math.floor((change - hundred * 100)/50);
twenty = (int) Math.floor((change - hundred * 100 - fifty * 50) / 20);
ten = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20) / 10);
five = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10) / 5);
one = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5) / 1);
quarter = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1)
/ quarterV);
dime = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1
- quarter * quarterV) / dimeV);
nickel = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1
- quarter * quarterV - dime * dimeV) / nickelV);
penny = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1
- quarter * quarterV - dime * dimeV - nickel * nickelV) / penny);
}
public void output1()
{
System.out.println("The original price of your item was "+money.format(original));
System.out.println("You will be granted a " +percent.format(discount)+ " discount on your purchase.");
System.out.println("Your discount amount (amount you are saving) is "+money.format(savings)+".");
System.out.println("The sales price of your item is "+money.format(salesprice));
System.out.println("The 7% tax payment will come out to be "+money.format(tax));
System.out.println("Thus your total will be "+money.format(total)+"\n");
System.out.println("How much money are you using to purchase your item?");
payment = scan.nextDouble();
scan.nextLine();
}
public void output2()
{
System.out.println("Your change is");
if (change>=100)
{
System.out.println(hundred+" one hundred dollar bills");
}
else
{
if (change>=50)
{
System.out.println(fifty+" fifty dollar bills");
}
else
{
if (change>=20)
{
System.out.println(twenty+" twenty dollar bills");
}
else
{
if (change>=5)
{
System.out.println(five+" five dollar bills");
}
else
{
System.out.println(one+" one dollar bills");
}
}
}
}
}
public void run()
{
input();
calculations();
output1();
change();
output2();
}
public static void main(String[] args)
{
CashRegister phill = new CashRegister();
phill.run();
}
}
Try this. I only did your cash register for 100, 50, and 20 dollar bills because you can get the hang of it from here. All this code does is go through the highest denomination bill to the lowest, subtracting the number of bills from the amount of change to get the remaining amount of change.
public void output2()
{
int remainder = change; //make a copy of change variable so it remains final.
int count=0;
System.out.println("Your change is");
if(remainder>=100){
hundred = (int)remainder/100;
remainder %= 100;
}
if(remainder>=50){
fifty = (int)remainder/50;
remainder %= 50;
}
if(remainder>=20){
twenty = (int)remainder/20;
remainder %= 20;
}
...
System.out.print(hundred + " one hundred dollar bills,");
System.out.print(fifty + " fifty dollar bills,");
System.out.print(twenty + " twenty dollar bills,");
...
}