Making Change

• 09-13-2011, 06:57 PM
iamNovember
Making Change
My second assignment for my intro to programming course is to make change:

Problem: Given the cost of an item, the number of items bought, and the amount tendered for the purchase, you should calculate the change to be given by specifying the number of each type of bill and coin to be used.

Input: The input to this program will consist of three values: the cost of an item, the number of items being bought, and the amount of money given to pay for the total purchase. You should have your program print out helpful prompts so that the user knows what values to input.

Output: You should echo the input data and print out the manner in which the change is to be given. For example,
Each item is \$0.46.
You bought 6 items.
You gave me \$20.00.
1 ten dollar bill
1 five dollar bill
2 one dollar bills
2 dimes
4 pennies

So far I have the input parts done, but I don't even know where to begin looking to start making change. I am so new to Java it hurts. Please help!
• 09-13-2011, 07:01 PM
Norm
Re: Making Change
How would you do it manually?
Given an amount you need to remove some number of the biggest valued money first.
Use division, multiplication and subtraction.
Then move down in value and continue.
• 09-13-2011, 07:26 PM
sunde887
Re: Making Change
One piece of advice: Be weary of using doubles or floats to represent money amounts. Try using DecimalFormat, or String manipulation and integers to represent all money in cents.
• 09-13-2011, 07:30 PM
JosAH
Re: Making Change
Quote:

Originally Posted by Norm
Given an amount you need to remove some number of the biggest valued money first.

That only applies if you want to return the minimum number of coins and bills and that only works for monetary units which are so called 'spanning bases'. e.g. if there were coins of 1, 2, 8 and 10 and you had to pay 16 (cents?) it'd be better to pay 2x8 cents instead of 1x10 and 4x2 cents. b.t.w. the 1,2,5 systems (US\$, euros etc.) are more efficient than e.g. the old Dutch guilder system (1, 2.5, 5)

kind regards,

Jos (<--- for all your useless knowledge ;-)
• 09-13-2011, 07:58 PM
Norm
Re: Making Change
How I hated the 2.5 guilders.
• 09-13-2011, 08:02 PM
JosAH
Re: Making Change
Quote:

Originally Posted by Norm
How I hated the 2.5 guilders.

Why? You couldn't see the difference between 1 guilder and 2.5 guilders?

kind regards,

Jos
• 09-13-2011, 08:05 PM
Norm
Re: Making Change
I'm not sure. Just when I got a handful of coins as change, I was never sure how much I had gotten.
• 09-13-2011, 08:10 PM
JosAH
Re: Making Change
Quote:

Originally Posted by Norm
I'm not sure. Just when I got a handful of coins as change, I was never sure how much I had gotten.

If it was a Dutchmen who gave you the change you could've counted on it that it wasn't enough ;-) In the old days we even had 1.5 guilder coins (just to upset the bloody foreigners ;-) Now we have those silly euros and I still don't know the difference between them ... given the sad state of the stock markets they're gone soon.

kind regards,

Jos