Keeping track of inserted coins.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 60
Reputation: Grub is an unknown quantity at this point 
Solved Threads: 0
Grub Grub is offline Offline
Junior Poster in Training

Keeping track of inserted coins.

 
0
  #1
Nov 16th, 2007
Hi I am creating a vending machine application that takes coins. It takes 5p, 10p 20p 50p £1 and £2 coins. Now I need to keep track of all the coins when inserted e.g. if I insert 2 pounds in 5p coins. When I chose to refund the money I need the machine to refund all the coins in random denominations but to the amount of £2. Or if I do buy something for say 25p, I need the machine to refund the change again in random denominations but to the correct change.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Keeping track of inserted coins.

 
0
  #2
Nov 16th, 2007
It depends on what you expect from the system. If you go the most simplistic route, you assume that the machine has a limitless supply of change in all denominations. In that case, you only need to keep the total amount entered. To make change you just use division and mod functions to return the most efficient change combination.

Obviously, that isn't very realistic though. Real machines have a finite pool of coins of each denomination, in which case you would need separate totals for each, perhaps in a HashMap, in addition to the amount the user had entered. The returnChange() method would attempt to return the most efficient combination of denominations with division and mod, but if those coins were not available it would need to split down to the next combination of coins available to return the required amount.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 795 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC