i have learn this Java since last month for my work purposes.
i wanted do this exercise.
this is Vending Machine Program.
I don't if write it in a wrong way.
So i need somebody to show me the in my code.
i know there must be wrong somewhere in the code.
could somebody please help me to.

output for the program is supposed to be like this :

**********************************************************************************************

[Coins available : RM 1 [5] RM 0.50 [5] RM).20 [5] RM0.10 [5] ]
[Drinks available : Pepsi [5] Coke [5] Orange [5] 7up [5] ]


Input Coin ==>2
Invalid Coin ..
Input Coin ==>1
Input Coin ==>0.50

Drinks [RM 1.20]
1. Pepsi 2. Coke 3. Mirinda Orange 4. 7up
Choice==>1

Your balance is : 1x0.20 1x0.10

Do you want to continue y/n ==>Y

[Coins available : RM 1 [6] RM 0.50 [6] RM).20 [4] RM0.10 [4] ]
[Drinks available : Pepsi [4] Coke [5] Orange [5] 7up [5] ]

Input Coin ==>

**************************************************************************************

description about the program :


**************************************************************************************

This Vending Machine program is a program that allow user to buy a can drinks. There are four types of can drink which is Pepsi, Coke, Orange and 7up. For that purposes user have to insert coins whereby valid inputs for the coins are RM1, 50 cents, 20 cents and 10 cents. The price for the can drinks is RM1.20 each. This program will read in number of coins from user until it reach the price and then ask user to choose the types of can drink that available and give balance to user and will then output the number of coins available and the drinks available. After that user can choose either to repeat the purchase or end the program.

*************************************************************************************

and this is my code.

***************************************************************************************

import.java.util.scanner;

public class VendingMachine
{

public static void main (String args[]) 
{

int[4] coin = {5,5,5,5};//create an array to hold coin values
int[4] drink = {5,5,5,5}; //create an array to hold drink values
int choice;
char cont;
//String input1, input2, input3;
double price = 1.2, balance = 0, coinInsert = 0, sumCoininsert = 0;

showmenu(coin, drink);
sumCoininsert = inputcoin(coinInsert);
showdrinks(choice);
showbalace(sumCoininsert, price);
contmethod(cont);
} 


/*****************showmenu method*****************/

public static int showmenu (int coin, int drink)
{ 

int coin;
int drink;


System.out.println("[Coins available : RM 1 [" + coin[0] + "] RM0.50 [ " + coin[1] + "] RM 0.20 [ " + coin[2] + "] RM0.10 [" +coin[3] "] ]");
System.out.println("[Drinks available : Pepsi [" + drink[0] + "] Coke [" + drink[1] + "] Mirinda Orange [" + drink[2] + "] 7up [" + drink[3] + "] ]");
}

/********************showmenu method*****************/




/**************inputcoin method *******************/

public static double inputcoin (double coinInsert)
{
double coinInsert = 0, sumCoininsert = 0; 

Scanner sc = new Scanner(System.in);

System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();


while (coinInsert == '1' || coinInsert == '0.5' || coinInsert == '0.2' || coinInsert == '0.1')
{
coinInc(coinInsert); 

sumCoininsert = sumCoininsert + coinInsert;
while (sumCoinInsert < 1.20)
{
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();

}


}



System.out.println (" Total :" +sumCoinInsert);
}


/********************inputcoin method*******************/





/***************coinInc method************************/ 

public static double coinInc(int coinInsert)
{
double coinInsert;

Scanner sc = new Scanner(System.in);

switch (coinInsert) {

case coinInsert == '1': 
coin[0]++;
break;

case coinInsert == '0.5': 
coin[1]++;
break;

case coinInsert == '0.2': 

coin[2]++; 
break;

case coinInsert == '0.1': 

coin[3]++; 
break;

default: 

System.out.println("Invalid coin");
sumCoininsert = 0;
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();
break;
}


}

/******************coinInc method*********************/







/*****************showdrinks method **************/

public static int showdrinks (int choice)
{
int choice;


System.out.println("Drinks [RM1.20]");
System.out.println("1.Pepsi" "\t2.Coke" "\t3.Mirinda Orange" "\t4.7up");

Scanner sc = new Scanner(System.in);

System.out.println("Choice==>");
int choice = sc.nextInt();

while (choice == '1' || choice == '2' || choice == '3' || choice == '4')
{

if (choice =='1')
{
drink[0]--;

}else if (choice=='2')
{
drink[1]--;

}else if (choice =='3')
{
drink[2]--;

}else (choice == '4')
{
drink[3]--;

} 


}

System.out.println("Invalid choice");
System.out.println("Choice==>");
int choice = sc.nextInt(); 

}

/*****************showdrinks method **************/




/****************showbalance method*******************/

public static double showbalance(double sumCoininsert, double price)
{
double price = 1.2, balance = 0, sumCoininsert;


balance = sumCoininsert - price;

if (balance != 0.00)
{

countbalance (balance);
System.out.println(" Your balance is :" +balance)
System.out.println("\n:" +fifty+ "x50" " " +twenty+ "x20" " " +tenth+ "x10" );

}else //if (balance == 0.00)
{
System.out.println("Your balance is :" +balance);

}
}

/****************showbalance method*******************/




/****************countbalance method******************/

public static double countbalance(double balance) 
{
double balance;

int ibalance = (int)(balance);

int fifty = ibalance/50;
int cfifty = ibalance%50;

int twenty = cfifty/20;
int ctwenty = cfifty%20;

int tenth = ctwenty/10;
int ctenth = ctwenty%10;


if ( fifty != 0)
{
coin[1]--; 
}

if (twenty != 0)
{
coin[2]--;
}

if (tenth != 0)
{
coin[3]--;
}


}

/*************countbalance method*********************/





/************************contmethod*****************************/

public static String contmethod (char cont)
{
char cont;

Scanner sc = new Scanner(System.in);

System.out.println ("Do you want to continue? y/n :");
//input3 = in.readline();
cont = sc.charAt(0);

while (cont == 'y' || cont == 'Y')
{
continue ;
}

System.out.println("Thank You, Please come again.");
System.exit(0);

}

} 
/****************contmethod*******************/

Recommended Answers

All 10 Replies

Error check your code and tell us at what point your code doesn't work and show us the method where it doesn't work. If you can't do that then you won't get any help.

ok i have break down the code to several part.
to make it easier to trace the error.
i break it into three part.
Coins.java and Drinks.java

for Drinks.java

import java.util.Scanner;

public class Coins {


    public static void main (String args[])
    {
        int coin[] = {5,5,5,5};
        int coinInsert = 0;
        //double sumCoininsert = 0;

        showmenu(coin);
        inputcoin(coinInsert, coin);
        //showbalance(sumCoininsert, price);
    }



    public static void showmenu (int coin[])
    {
            //System.out.println("[Coins available : RM 1 [" + coin[0] + "] RM0.50 [ " + coin[1] + "] RM 0.20 [ " + coin[2] + "] RM0.10 [" +coin[3] "] ]");
            System.out.println("Coins available: RM1[" +coin[0]+ "]" + "  RM0.50[" +coin[1]+ "]" + "  RM0.20[" +coin[2]+ "]" + "  RM0.10[" +coin[3] +"]");
    }



/***********************inputcoin method *******************/

    public static int inputcoin (int coinInsert, int coin[])
    {
        int sumCoininsert=0;

        Scanner sc = new Scanner(System.in);

        System.out.println("Insert Coin==>");
        coinInsert = sc.nextInt();


            while (coinInsert == 1 || coinInsert == 50 || coinInsert == 20 || coinInsert == 10)
            {
                //coinInc(coinInsert);


                if (coinInsert == 1)
                {
                    coin[0]++;
                }else if (coinInsert == 50)
                {
                    //double icoin = (int)(coin);
                    //ccoin = icoin/100;
                    coin[1]++;
                }else if (coinInsert == 20)
                {
                    //double icoin = (int)(coin);
                    //ccoin = icoin/100;
                    coin[2]++;
                }else if (coinInsert == 10)
                {
                    //double icoin = (int)(coin);
                    //ccoin = icoin/100;
                    coin[3]++;
                } else
                {
                    System.out.println("Invalid type");
                    sumCoininsert=0;
                    System.out.println("Insert Coin==>");
                    coinInsert = sc.nextInt();
                }

                sumCoininsert += coinInsert;

                while (sumCoininsert < 120)
                {
                    sumCoininsert = sumCoininsert + coinInsert;

                    System.out.println("Insert Coin==>");
                    coinInsert = sc.nextInt();

                }

            }


            System.out.println (" Total :" +sumCoininsert);
            System.out.println("Coins available: RM1[" +coin[0]+ "]" + "  RM0.50[" +coin[1]+ "]" + "  RM0.20[" +coin[2]+ "]" + "  RM0.10[" +coin[3] +"]");

    return(0);
    }


}
/***********************inputcoin method*******************/

[/QUOTE]

It runs but it keep on looping asking for 'Coin Insert' without doing any task or calculation. And it receive any value that i entered. How actually the right way to write it?

p/s: u can ignore all the comments. I forgot to delete it.

what are your inputs?

you do realise you have 2 while loops to satisfy?

yes. 2 while loops? is that wrong?

the output supposed like this:

Coins available: RM1[5]    RM0.50[5]    RM0.20[5]    RM0.10[5] ]

Input Coin ==>2
Invalid Coin ..
Input Coin ==>1
Input Coin ==>50

Total = 1.50

Coins available:  RM1[6]    RM0.50[6]    RM0.20[5]    RM0.10[5] ]

but i get :

Coins available: RM1[5]    RM0.50[5]    RM0.20[5]    RM0.10[5] ]

Input Coin ==>20
Input Coin ==>30
Input Coin ==>50
Input Coin ==>70
Input Coin ==>100

Total :180


Coins available: RM1[5]    RM0.50[5]    RM0.20[6]    RM0.10[5] ]

the calculation didn't work.
increment of coins available also didn't work.
and also it accept any value that i insert.

How to improve it?

while (coinInsert == '1' || coinInsert == '0.5' || coinInsert == '0.2' || coinInsert == '0.1')
{
coinInc(coinInsert);

sumCoininsert = sumCoininsert + coinInsert;
while (sumCoinInsert < 1.20)
{
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();

}

That code doesn't make sense. Think about it logically. You want to do the following:

While the user hasn't yet paid for their drink, wait for them to put in money, and add up the money that they put in. Stop once they put in enough money and give them their change.

Your code doesn't do this because it has two while loops and you are adding up the money in the wrong place. You can combine your while loops and add up the money properly like this:

while (sumCoinInsert < 1.20 && (coinInsert == '1' || coinInsert == '0.5' || coinInsert == '0.2' || coinInsert == '0.1'))
{
System.out.println("Insert Coin==>");
double coinInsert = sc.nextDouble();
sumCoininsert = sumCoininsert + coinInsert;
}

There may still be some issues (such as the fact that I didn't tell you what to do with your coinInc() method -- I'll leave that to you to figure out) and the fact that the code I gave you will not work since you'll need to modify it to prompt the user to tell you the first coin they are putting in -- but that is the general idea. I'm also not sure how you are giving the user their change - that is up to you, and you might have handled it elsewhere in the program. I'm not sure, but again, just make sure everything is handled. Feel free to PM me or anything if you don't understand, but I'll respond in this thread.

to everyone.

thanks a lot.

problem solved.

cheers!

No problem. Don't forget to mark as solved if you don't have any more questions.

what is the software you use for java programming apart from command prompt

Hi Amazing_2

This thread is 8 years old, and not related to your question, so the right people won't see it.

Please start a new topic to ask your question and I'm sure you will get some good answers.

JC
Moderator

commented: Good answer. +12
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.