Hi I am doing a vending Machine application. Now I can insert denominations from 5 10 20 50 1pound and 2 pounds. Is there a way of filtering the most efficent coin combination to pay for the snack. e.g. I have a snack for 45 pence. I insert 2 pounds and 50 pence. I want the machine to be able to determine which is the most efficen coins to use.

Recommended Answers

All 3 Replies

Start taking out from the largest to the smallest coin.

It is precisely what I have done. But if something is say 45 pence. And I had earlier inserted 2 pounds and a fifty pence piece only. Then it takes the 2 pound I would rather the machine took the fifty. Hence I made adjustments, however the adjustments are not as robust. I need to be careful with the prices of the snacks and the coins I insert in order for harmony but as I said, this is not a robust machine. Any other suggestions?

The code is depicted below

First I insert money say five 50 pence coins = 2.50, and two 2pouns coins to make £6.50.
Now the vending machine method purchases takes an int as a parameter to specify the slot.

public static final int FIVE_VALUE = 5;
   public static final int TEN_VALUE = 10;
   public static final int TWENTY_VALUE = 20;
   public static final int FIFTY_VALUE = 50;
   public static final int POUND_VALUE = 100;
   public static final int TWOPOUND_VALUE = 200;

// insert coin method. I insert say £6.50 as above. 

 protected void insertCoins(int insert)
    {
        int i = 0;
        if(insert == 5)
        {
            credit[0] = credit[0] + 1;    
        }
        else if(insert == 10)
        {
            credit[1] = credit[1] + 1;
        }
        else if(insert == 20)
        {
            credit[2] = credit[2] + 1;
        }
        else if(insert == 50)
        {
            credit[3] = credit[3] + 1;
        }
        else if(insert == 100)
        {
            credit[4] = credit[4] + 1;
        }
        else if(insert == 200)
        {
            credit[5] = credit[5] + 1;
        }
        else
        {
            System.out.println("This Machine only accepts 5, 10, 20 and 50 pence and 1 and 2 pound coins");
        }
    }


public void purchase(int choice)
    {
        // Calling the dispenseSnack(i) method with the parsed integer parameter so that the snack is dispensed provided the snackObject is not null.       
        if(choice < snackObject.length && choice >= 0)
        {
                if(snackObject[choice] == null)
                {
                   System.out.println(" SLOT " + choice + " IS EMPTY");
                }
                if(calculateTotal() < snackObject[choice].price)
                {
                    System.out.println("Insufficient credit");
                }
                if(calculateTotal() >= snackObject[choice].price)
                {
                   dispenseSnack(choice);
                }
           }                   
    }

    //displayCredit method display the current credit held in the vending machine
    protected void displayCredit()
    {
        System.out.println("-----Current Credit in Pence :----- " + calculateTotal());
    }
    // The total method calculates the total active credit held in the vending machine
    public int calculateTotal()
    {
        total = credit[0] * FIVE_VALUE + credit[1] * TEN_VALUE + credit[2] * TWENTY_VALUE + credit[3] * FIFTY_VALUE + credit[4] * POUND_VALUE + credit[5] * TWOPOUND_VALUE;  
        return total;
    }

    //The dispense(i) method takes a parameter which is the option selected by the user.    
    private void dispenseSnack(int i)
    {     

            total = snackObject[i].price;
        //Loop is to ensure that the all the coins are are emptied 
        do
        {
            if(snackObject[i].price / TWOPOUND_VALUE >= 1 && credit[5] > 0)
            {
                    change[5] = change[5] + 1;   
                    credit[5] = credit[5] - 1;   
                    total = total - 200;
            }
            if(snackObject[i].price / POUND_VALUE >= 1 && credit[4] > 0)
            {
                    change[4] = change[4] + 1;   
                    credit[4] = credit[4] - 1;   
                    total = total - 100;
            }
            if(snackObject[i].price / FIFTY_VALUE >= 1 && credit[3] > 0)
            {
                    change[3] = change[3] + 1;   
                    credit[3] = credit[3] - 1;   
                   total = total - 50;
            }
            if(snackObject[i].price / TWENTY_VALUE >= 1 && credit[2] > 0)
            {
                    change[2] = change[2] + 1;   
                    credit[2] = credit[2] - 1;   
                    total = total - 20;
            }
            if(snackObject[i].price / TEN_VALUE >= 1 && credit[1] > 0)
            {
                    change[1] = change[1] + 1;   
                    credit[1] = credit[1] - 1;   
                    total = total - 10;
            }
            if(snackObject[i].price / FIVE_VALUE >= 1 && credit[0] > 0)
            {

                    change[0] = change[0] + 1;   
                    credit[0] = credit[0] - 1;
                    total = total - 5;
            }  
          }
        while(total > 0);
            System.out.println(snackObject[i].snackName + " :- Is despensed");
            snackObject[i] = null;
            calculateTotal();

    }

If you have put the coins into the machine (which is how I read the post since you said "insert 2 po....") then you are only worried about the calculation of the change. So start with the biggest and work your way down and you will come up with 2 pound and 5 pence coins as change.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.