1

the following code calculates change dispensed by a vending machine. My problem? I cant get the change variable to work as the compiler wont let me due to two different data types (int & double conversion). Can anyone please help me solve this problem. I have tried casting "change" but then it wont print right amount. For example, if the change is 0.25 cents, change value remains zero..for obvious reasons of course. The problem begins at line 16. I have commented the part giving example as change = 0.25.

public String[] itemList = new String[] {"Water   ","Coke    ", "Diet Coke", "Iced Tea","Fanta   "};
public double[] priceList = new double[] {75,120, 120, 100, 150};
public int[] itemQty = new int[]{10,10,10,10,10};
public int[] coinList = new int[]{100,50,20,10,5};
public int[] coinQty = new int[]{10,10,10,10,10};
public double change;
public double paid;

    public void ReturnChange()
{
    int Denominations=5;
    int coins_dispensed = 0 ;
    int[] InitialArray = new int[Denominations];

    //My Problem begins here..for example if change is computed
      change = 0.25; //change is a global declaration of type double and carries values derived from different function
      int change1 = (int)change; //if i cast here, i get change as 0, thus the part that follows, fails to compute coins dispensed.

    for (int i=0; i < 5; i++)               
    {
    InitialArray[i] += coinQty[i];  // Copies Coin Quantity to Initial array for difference 

    }

    System.out.println("Your change is  "+NumberFormat.getCurrencyInstance().format(Math.abs(change1)) +" which comprises of:"); //OK till here


    for (int i=0; i<5; i++)
    {
        if (coinQty[i]>0) //if a particular denomination is available
        {
            coins_dispensed = (change1/coinList[i]); //dividing coins dispense with denomination
            coinQty[i] -= coins_dispensed; //reduce the quantity of the denomination dispensed
            change1 = change1 - (coinList[i] * coins_dispensed); //total the change
        }
        else                                            // Moves to next denomination if a particular coin runs out
        {
            coins_dispensed = (change1/coinList[i+1]);
            coinQty[i+1] -= coins_dispensed ;
            change1 = change1 - (coinList[i+1] * coins_dispensed);
        }
    }
    if (change1 != 0)                                   // In the case not enough coins to make change, selection is ignored. 
    {
        System.out.println("\n\n\t Sorry. The machine doesnt have enough coins to make up your change. Your last transaction has been ignored.");
    }
    else 
    {
        for (int i=0; i<Denominations; i++)
        {
            coins_dispensed = InitialArray[i] - coinQty[i];
            System.out.println( "\n\t\t\t" + coins_dispensed +" of "+ coinList[i] + "  cents coins");
        }
    }


}
3
  • Can you please indicate the line your problem occurs on and where to tried to cast?
    – Max Leske
    Aug 10, 2014 at 12:14
  • There are some parts missing in your code. Please also post the declaration and initialization code of coinList and coinQty. Aug 10, 2014 at 12:28
  • As requested, i have updated the codes. Please have a look at it again and let me know if you guys can help me out. Would indeed appreciate it.
    – Catherine
    Aug 10, 2014 at 12:35

2 Answers 2

2

You should use use integers everywhere but count in cents not dollars. Just divide your numbers by 100 when you print them.

This is because floats and doubles cannot accurately represent the base 10 multiples used for money and will introduce rounding errors, particularly when multiplying to calculate interest rates for example.

See Why not use Double or Float to represent currency? for more information and discussion.

3
  • Apparently, I have tried the method mentioned but it will not work for dispensing coins.
    – Catherine
    Aug 10, 2014 at 13:00
  • 1
    If you want more help you need to post compilable code so others can test it. See How to create a Minimal, Complete, and Verifiable example. Aug 10, 2014 at 13:04
  • +1 @Catherine Coins are described as whole number of cents in real life and this does work for computers are well. You might have had a bug in your code, but this doesn't mean that using double instead of int will mean less bugs in this case. Aug 10, 2014 at 14:13
0

It seems all your variables hold prices in cents (i guess a coke is not 120 $). But your change is apparently specified in dollars. So what you could do is multiply change by 100 and then cast it to int.

Like that:

int change1 = (int) (change * 100); // convert dollars to cents and cast to int

If you need to output change1 in dollars (and not cents) at some point, you have to convert it back:

float result = change1 / 100.0f;
1
  • Thank you for this answer. The code actually worked like a charm. Thanks once again..really appreciate it.
    – Catherine
    Aug 10, 2014 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.