Was given the below excercise in college but can’t get my head around the last bit. How do you brake the answer down to notes. what if the answer can’t be devided by 50 etc…
I’ve tried modulus & Math class….
Mr Cashman, the owner of the local corner shop is having trouble with
his staff giving the wrong change. To tackle this he has commisioned
you to create a program that allows the staff member to enter the
total cost of the shopping and the amount the customer has paid. The
program should then calculate and output how many €50, €20, €10, €5
notes, and how many €2, and €1 coins to give the customer as change.
Mr. Cashman refuses to deal with small coins so although the staff
member enters a decimal value for the cost of the shopping he rounds
all of his prices down to the nearest euro and doesn’t bother with
50c, 20c, 10c, 5c, 2c or 1c coins.Write, compile and test the application as per Mr Cashmans
requirements and save the class as MakingChange.java
You have the right approach with modulo, but integer division is needed as well. Let’s examine the value €164.
If you divide 164 by 50 (with integer division), you get 3. So that’s 3 €50 notes needed.
If you work out 164 modulo 50, you end up with 14, the value left after you’ve taken out those three €50 notes. That’s you starting point for the next iteration with €20 notes.
Then you just keep doing it until you’ve exhausted all the currency types.
In other words, the pseudo-code could look something like: