Hey guys, I am making A Vending Machine Program and seem to be getting error's when trying to do things outside a class and stops the Program. (Vending machine should keep looping until Quit is typed, and it will display a summary of money and cans left.

******SODA MACHINE TESTER*******

public class SodaMachineTester {

	
	public static void main(String[] args) {
		Machine sim = new Machine(); //declare the reference
		sim.go(); //start Simulation
		sim.stop(); //end SImulation
		System.exit(0);

	}

}
****MACHINE*****

public class Machine {
	//The Machine class contains all the references to the objects
	private Display dialogbox; 
	private Counter coin;
	private Dispenser dispense;
	//Sets up relations so each class can talk to another
	Machine(){
		coin = new Counter(dispense, dialogbox);
		dialogbox = new Display(coin,dispense);
		dispense = new Dispenser(coin,dialogbox);
	
	}
	//Start up machine
	public void go(){
		dialogbox.getUserInput();
	
	}
	//Stop the Machine
	public void stop(){
		dialogbox.sayBye();
		
	}

}
*****DISPENSER******
import javax.swing.JOptionPane;


public class Dispenser {
	//Set the variables
	int coke = 5;
	int sprite = 3;
	String choice;
	
	private Display dialogbox; 
	Dispenser (Counter c, Display d)
	{
	dialogbox = d;
	
	}
	
	//Prompt and ask for user to pick a soda
	public void chooseSoda()
	{
		String choice;
		choice = JOptionPane.showInputDialog(null, "Coke Or Sprite?");
		if(choice.equals((Object)("Coke" )))
			coke();
		
		else
			sprite();
	}
	//When the user picks Coke
	public void coke()
	{
		//Error Check if Coke is Sold Out
		if (coke == 0){
			choice = JOptionPane.showInputDialog(null, "Coke is Sold Out. \n Would you Like A Sprite?");
			if(choice.equals((Object)("Yes")))
				sprite();
			//If they do not want the Sprite
			if(choice.equals((Object)("No")))
				JOptionPane.showMessageDialog(null,"Here is your Refund of 75 Cents");
		
		}
		else
		JOptionPane.showMessageDialog(null,"Here is your Coke!");
		//Take one coke out of the machine
		coke = (coke - 1);
		dialogbox.getUserInput();
	}
	
	
	public void sprite()
	{
		//Error check if Sprite is Sold Out
		if (sprite == 0){
			choice = JOptionPane.showInputDialog(null, "Sprite is Sold Out. \n Would you Like A Coke?");
			if(choice.equals((Object)("Yes")))
				coke();
			//If they do not want the Coke
			if(choice.equals((Object)("No")))
				JOptionPane.showMessageDialog(null,"Here is your Refund of 75 Cents");	
		}
		
		JOptionPane.showMessageDialog(null,"Here is your Sprite!");
		// Take one sprite out of the machine
		sprite = (sprite - 1);
		dialogbox.getUserInput();
	}
	
	//Output total Cokes
	public void getTotalCokes()
	{
		JOptionPane.showMessageDialog(null, "The Total Amount Of Cokes Left is "+ coke);
		
	}
	//Output total Sprites
	public void getTotalSprites()
	{
		JOptionPane.showMessageDialog(null, "The Total Amount Of Sprites Left is "+ sprite);
		
	}
	
	
	
	
}
*****DISPLAY*****
import javax.swing.JOptionPane;


public class Display {

	
	private Counter coin;
	private Dispenser dispense;
	Display(Counter c, Dispenser d)
	{
		coin = c;
		dispense = d;
	}
	
	public void getUserInput()
	{
		String choice;
		//Ask the user if they want a drink or not
		choice = JOptionPane.showInputDialog(null, "Do you want to Buy A Drink Or Quit?");
		//Ask for money if they want to buy a drink
		if(choice.equals((Object)("Buy A Drink")))
			coin.ask();
		
		else
			//When Manager Quits
			coin.quit();
		
	}
	//Prompt at end of machine
	public void sayBye(){
		JOptionPane.showMessageDialog(null, "Thank you!");
	}
}
******COUNTER*******
import javax.swing.JOptionPane;


public class Counter {
	//Initalize Variables
	int coin = 0;
	int total = 0;
	int nickel = 5;
	int dime = 10;
	int quarter = 25;	
	private Display dialogbox; 
	private Dispenser dispense;
	
	//Set Constructor
	Counter (Dispenser d, Display p)
	{
	dispense = d;
	dialogbox = p;
	
	}
	
	//Get Money from person
	public void ask()
	{

	    while( total != 75 )
	    {
		String c1= JOptionPane.showInputDialog(null,"Please insert 75 Cents" + "\nCurrent Money In Machine is " + total);
		coin = Integer.parseInt(c1);
		if ((coin != nickel)&&(coin!=dime)&&(coin != quarter))
		{
			wrongCoin();	
		} else
		
		{
		total = coin + total;}
		JOptionPane.showMessageDialog(null, "Money Inserted " + total + " cents");
		
	    }
	    
	    //When the user inputs 75 cents, prompt them to choose a Soda
	    if (total == 75)
	    {
		dispense.chooseSoda();	 
	    }
	    
	}
	
	//If User does not want to take the Soda, give change back
	public void getChange()
	{
		total = total - 75;
	}
	//If User inputs a wrong coin amount
	public void wrongCoin()
	{
		JOptionPane.showMessageDialog(null, "Incorrect Value");

	}
	// Set Coin
    public void setCoin(Integer c) 
    {
    coin = c;
    }
    //If Manager decides to quit and view log
    public void quit()
    {
    	//Display Total Money
    	JOptionPane.showMessageDialog(null, "Total Money in Machine is " + total);
    	//DIsplay Cokes and Sprites
    	dispense.getTotalCokes();
    	dispense.getTotalSprites();
    }
}

Program and seem to be getting error's when trying to do things outside a class and stops the Program

What is the error that you get??


Vending machine should keep looping until Quit is typed

For this what you can do is modify the code in the method getUserInput() class Display
as to include a do while loop as below

public void getUserInput()
{
do{
String choice;
//Ask the user if they want a drink or not
choice = JOptionPane.showInputDialog(null, "Do you want to Buy A Drink Or Quit?");
//Ask for money if they want to buy a drink
if(choice.equals((Object)("Buy A Drink")))
coin.ask();
 
else
//When Manager Quits
coin.quit();
}while(!choice.equals("Quit"));//loop until the user has entered "Quit"
}
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.