Keeron Keeron - 24 days ago 18
Java Question

Another Cash Register Program

I want to write a program that takes in the price, entered by the user, how much they pay and what their change is. I also want the program to specify the coins and notes due for their change.

I think I am pretty close, however when I run the program I just get 0 for the amount of coins and notes due.

I'm not looking for someone to write the program for me, I just to know what I'm doing wrong, no doubt it is something simple that I overlooked but I can't figure it out.

This is what I've done so far, I know it's not very pretty but I'll work on that as I get more experienced.

Thanks.

Scanner cashregister = new Scanner(System.in);

System.out.println("Thank you for your purchase!");


int amountDue = 0, amountPaid = 0, changeDue = 0;


int fiftyEuro = 5000;

int twentyEuro = 2000;

int tenner = 1000;

int fiver = 500;

int two = 200;

int one = 100;

int fiftyCent = 50;

int twentyCent = 20;

int tenCent = 10;

int fiveCent = 5;

int twoCent = 2;

int oneCent = 1;



fiftyEuro = changeDue / 5000;

changeDue = changeDue %= 5000;


twentyEuro = changeDue / 2000;

changeDue = changeDue %= 2000;


tenner = changeDue / 1000;

changeDue = changeDue %= 1000;


fiver = changeDue / 500;

changeDue = changeDue %= 500;


two = changeDue / 200;

changeDue = changeDue %= 200;


one = changeDue / 100;

changeDue = changeDue %= 100;


fiftyCent = changeDue / 50;

changeDue = changeDue %= 50;


twentyCent = changeDue / 20;

changeDue = changeDue %= 20;


tenCent = changeDue / 10;

changeDue = changeDue %= 10;


fiveCent = changeDue / 5;

changeDue = changeDue %= 5;


twoCent = changeDue / 2;

changeDue = changeDue %= 2;


oneCent = changeDue / 1;

changeDue = changeDue %= 1;


if (amountDue > amountPaid)

System.out.println("\nThe amount due must be less then or equal to the amount you are paying");


System.out.println("\nThe amount due, in cents: ");

amountDue = cashregister.nextInt();


System.out.println("\nThe amount paid, in cents: ");

amountPaid = cashregister.nextInt();

changeDue = amountPaid - amountDue;


System.out.println("\nYour change, in cents is: " + changeDue);


System.out.println("\nThe change for " + changeDue + " is: ");


System.out.println(fiftyEuro + " \u20ac" + "50"); //€50 notes due

System.out.println(twentyEuro + " \u20ac" + "20"); //€20 notes due

System.out.println(tenner + " \u20ac" + "10"); //€10 notes due

System.out.println(fiver + " \u20ac" + "5"); //€5 notes due

System.out.println(two + " \u20ac" + "2"); //€2 coins due

System.out.println(one + " \u20ac" + "1"); //€1 coins due

System.out.println(fiftyCent + " 50" + "c"); //50c coins due

System.out.println(twentyCent + " 20" + "c"); //20c coins due

System.out.println(tenCent + " 10" + "c"); //10c coins due

System.out.println(fiveCent + " 5" + "c"); //5c coins due

System.out.println(twoCent + " 2" + "c"); //2c coins due

System.out.println(oneCent + " 1" + "c"); //1c coins due


cashregister.close();

return;

}


}

Here is an example of my output

Thank you for your purchase!

The amount due, in cents:
2450

The amount paid, in cents:
5000

Your change, in cents is: 2550

The change for 2550 is:
0 €50
0 €20
0 €10
0 €5
0 €2
0 €1
0 50c
0 20c
0 10c
0 5c
0 2c
0 1c

As you can see it doesn't say how much of each note / coin the customer should get.

Answer

move these codes after

    fiftyEuro = changeDue / 5000;
    changeDue = changeDue %= 5000;
    twentyEuro = changeDue / 2000;
    changeDue = changeDue %= 2000;
    tenner = changeDue / 1000;
    changeDue = changeDue %= 1000;
    fiver = changeDue / 500;
    changeDue = changeDue %= 500;
    two = changeDue / 200;
    changeDue = changeDue %= 200;
    one = changeDue / 100;
    changeDue = changeDue %= 100;
    fiftyCent = changeDue / 50;
    changeDue = changeDue %= 50;
    twentyCent = changeDue / 20;
    changeDue = changeDue %= 20;
    tenCent = changeDue / 10;
    changeDue = changeDue %= 10;
    fiveCent = changeDue / 5;
    changeDue = changeDue %= 5;
    twoCent = changeDue / 2;
    changeDue = changeDue %= 2;
    oneCent = changeDue / 1;
    changeDue = changeDue %= 1;

after these line..

    System.out.println("\nThe amount paid, in cents: ");
    amountPaid = cashregister.nextInt();
    changeDue = amountPaid - amountDue;
Comments