Java - Need Helping With Coding The Logic For This Scenario

Associate
Joined
1 Mar 2004
Posts
1,790
Hey guys,

Basically I'm making a simulation of a ticket machine and I'm having trouble on the logic on how it should give change. So for example if the user puts say 50p but the ticket is 40p then 10p change will be given. In the case however the returned change is not a whole coin value and a composition of various coins then any ideas on how I should do this? Also the coins are taken from a container class which holds each coin value if a container is emtpy can it be skipped during the process, any ideas here?

I thought maybe there could be a way of working it out mathematically but I'm not too sure. Thanks in advance guys.
 
Hey guys,

Basically I'm making a simulation of a ticket machine and I'm having trouble on the logic on how it should give change. So for example if the user puts say 50p but the ticket is 40p then 10p change will be given. In the case however the returned change is not a whole coin value and a composition of various coins then any ideas on how I should do this?

Use the modulo operator in a loop,
Code:
for (;;;) {
    if (change % £2 = x) {
        do this
    }
    if (change % £1 = x) {
        do this
    }
}
etc

Also the coins are taken from a container class which holds each coin value if a container is emtpy can it be skipped during the process, any ideas here?

Create an acessor method in the container class and just call it when you need the number of coins in it.
 
How the container works?

For example,

Code:
while(container.twoPoundCoinsAvailable() && (change - 200) >= 0) {
       change -= 200;
       twoPoundCoins++;
       container.removeTwoPoundCoin();
    }
while(container.onePoundCoinsAvailable() && (change - 100) >= 0) {
       change -= 100;
       onePoundCoins++;
       container.removeOnePoundCoin();
}

will sort out the £2, £1 coins etc. However, container might not work this way.

Example,

Assume change = 300
No two pound coins available
10 one pound coins available

First loop does not execute, no coins and first part of statement fails. Since is an && java will not even bother checking the second part.

Second loop executes 3 times and even though one pound coins are still available, second part of statement fails since after 3 times change will go negative (-100).

The answer is 3 x £1 coins, which is what we need.

I would wrap up the whole thing in a helper method returning an int[] with position 0 being the £2 coins, position 1 being the £1 coins and so forth; and the method taking two arguments, pence and container.
 
Last edited:
Hi,

Here's another way of doing it.

Create a class to hold the value of a coin and the number in the container. Create an ArrayList of these sorted from the highest coin value to the lowest.

Use a HashMap to hold the details of the coins returned in the change, indexed by the unique coin value.

Use code that does the following:

Code:
int moneytogive=50; // Usually supplied by calling code.
int coin=0;

while (moneytogive > 0 and coin<coinlist.length()) {
    // Check that the coin we are looking at is less than the amount to give
        back and that there are some of this coin left.
    if (coinlist.item(coin).value > moneytogive 
              || coinlist.item(coin).noleft ==0) 
    {
        // Look at next coin value.
        coin++;
        next;
    } else {
        // Subtract the amount of this coin from the amount to return.
        moneytogive -= coinlist.item(coin).value--;
        
        // Add details of change given to item in map.
        (Integer)changemap.get(coinlist.item(coin).value.toString())+=1;
    }
}

That's probably not syntactically correct Java, but it's not too far off the mark hopefully.

Jim
 
Last edited:
Back
Top Bottom