Results 1 to 3 of 3
  1. #1
    dead1900 is offline Member
    Join Date
    Oct 2014
    Posts
    11
    Rep Power
    0

    Question [Question] How to go back to main menu and terminate a program?

    How to go back to main menu and terminate a program?

    I just want a Simple code. don't want complicated please help. beginner in java programming

    [Question] How to go back to main menu and terminate a program?-b269czi.png

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: [Question] How to go back to main menu and terminate a program?

    There are no implemented "goto" statements in Java. You need to control execution via
    while loops, if else statements, and break or continue statements. Your main execution control is
    a while loop. If else statements help determine intra-loop activity. Check out the tutorials in my signature
    for more info.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    dead1900 is offline Member
    Join Date
    Oct 2014
    Posts
    11
    Rep Power
    0

    Default Re: [Question] How to go back to main menu and terminate a program?

    Ok sir thanks for that. I got this code. But what's the problem with my Machine after I run it. I think the codes are correct but the problem is?

    [Question] How to go back to main menu and terminate a program?-n2w7xbx.png

    [spoiler]
    Java Code:
    package candyMachine;
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class CandyMachine {
    	public static void main(String[] args) {
    		System.out.println("*** Welcome to Shelly's Candy Shop ***");
    
    		// Create dispenser objects:
    		ProductDispenser Candy = new ProductDispenser(0.50, 12);
    		ProductDispenser Chips = new ProductDispenser(0.65, 12);
    		ProductDispenser Gum = new ProductDispenser(0.45, 12);
    		ProductDispenser Cookies = new ProductDispenser(0.85, 12);
    
    		// Create cash register object:
    		CashRegister CR = new CashRegister();
    
    		int select = 0;
    		do {
    			select = getIntInput("nTo select an item, entern" + "1 for Candyn"
    					+ "2 for Chipsn" + "3 for Gumn" + "4 for Cookiesn"
    					+ "9 to exitn");
    			switch (select) {
    			case 1:
    				purchase(Candy, CR);
    				break;
    			case 2:
    				purchase(Chips, CR);
    				break;
    			case 3:
    				purchase(Gum, CR);
    				break;
    			case 4:
    				purchase(Cookies, CR);
    				break;
    			case 9:
    				break;
    			default:
    				System.out.println("Invalid Selection");
    			}
    		} while (select != 9);
    	}
    
    	// Method to execute purchase:
    	public static void purchase(ProductDispenser product, CashRegister cashreg) {
    		Scanner input = new Scanner(System.in);
    		if (product.getProdQty() > 0) {
    			double pmt = 0;
    			// Collect enough coin to purchase product.
    			do {
    				System.out.print("Please deposit "
    						+ (product.getProdCost() * 100 - pmt) + " cents.");
    				pmt += input.nextDouble();
    			} while (pmt < product.getProdCost() * 100);
    			product.setProdQty(1);
    			cashreg.setCashOnHand(product.getProdCost());
    			System.out.println("Collect your item at the bottom and enjoy.");
    		}
    	}
    
    	// Method to handle integer input:
    	public static int getIntInput(String prompt) {
    		Scanner input = new Scanner(System.in);
    		System.out.print(prompt);
    		int intValue = 0;
    		try {
    			intValue = input.nextInt();
    		} catch (InputMismatchException e) {
    			System.out.println("*** That is not a number. ***");
    		}
    		return intValue;
    	}
    }
    
    
    class CashRegister {
    	// Data elements:
    	private double cashOnHand;
    
    	// Methods:
    	CashRegister() { 						// Constructor
    		cashOnHand = 0;
    	}
    	double getCashOnHand() {
    		return cashOnHand;
    	}
    	void setCashOnHand(double purchase) {
    		cashOnHand += purchase;
    	}
    }
    
    
    class ProductDispenser {
    	// Data elements:
    	private double prodCost;
    	private int prodQty;
    
    	// Methods:
    	ProductDispenser(double cost, int qty) { // Constructor
    		prodCost = cost;
    		prodQty = qty;
    	}
    	double getProdCost() {
    		return prodCost;
    	}
    	int getProdQty() {
    		return prodQty;
    	}
    	void setProdQty(int qty) {
    		prodQty -= qty;
    	}
    }
    [/spoiler]

Similar Threads

  1. passing from methods back to main.
    By downWithJava in forum New To Java
    Replies: 5
    Last Post: 04-18-2013, 09:47 PM
  2. Replies: 2
    Last Post: 11-06-2011, 09:32 AM
  3. Creating a Main Menu - Importing
    By l3eater in forum Java Applets
    Replies: 2
    Last Post: 12-28-2010, 06:41 AM
  4. Help making a main menu.
    By pbd2 in forum Java Applets
    Replies: 2
    Last Post: 10-17-2008, 11:53 AM
  5. 3 errors and then terminate program
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 05-20-2008, 02:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •