Set-up Main Menu


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Set-up Main Menu

  1. #1
    Join Date
    Sep 2002
    Posts
    6

    Question Set-up Main Menu

    Hi everyone

    I have created separate four class:

    and I want to setup a main menu, so if a user press 1, then it goes to the Annulus Class, if a user press 2, it goes to Diameters Class and if a user press 3, it goes to the Circumference class.

    I've spent all day trying to figure it out how I can set up a menu. I've attach my work what I've done so far. Can you help me please?

    Many thanks!

    Code:
    public class MainMenu
    
    {
    	public static CheckLinesIn CLI = new CheckLinesIn();
    	public static Annulus A = new Annulus();
    	public static Diameters D = new Diameters();
    	public static Circumferences C = new Circumferences();
    	
    
    public static void menu (String[] args)
    {

  2. #2
    Join Date
    Sep 2002
    Posts
    78
    well you're on the right track but just a note your public static void menu (String[] args) is suppose to be main not menu. here's the basic code of how u can do this. have fun coding

    Code:
    import java.io.*;
    
    public class MainMenu 
    {
          public static CheckLinesIn CLI = new CheckLinesIn();
          public static Annulus A = new Annulus();
          public static Diameters D = new Diameters();
          public static Circumferences C = new Circumferences();
          
          public static void main (String[] args) throws IOException  //throws IOException doesn't report any erros
          {           //main not menu!
                String input = new String();
                
                System.out.println("Please enter a choice");
                System.out.println("1. CheckLinesIn");
                System.out.println("2. Annulus");
                System.out.println("3. Diameters");
                System.out.println("4. Circumferences");
                
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                input = br.readLine();
                
                if(input.equals("1"))
                {
                      CLI.menu();
                }
                else if (input.equals("2"))
                {
                      A.menu();
                }
                else if (input.equals("3"))
                {
                      D.menu();
                }
                else if (input.equals("4"))
                {
                      C.menu();
                }
          }
    }
    
    class CheckLinesIn
    {
          public static void menu ()
          {
                System.out.println("In class CheckLinesIn");
          }
    }
    
    class Annulus
    {
          public static void menu ()
          {
                System.out.println("In class Annulus");
          }
    }
    
    class Diameters
    {
          public static void menu ()
          {
                System.out.println("In class Diameters");
          }
    }
    
    class Circumferences
    {
          public static void menu ()
          {
                System.out.println("In class Circumferences");
          }
    }
    ¿Red Baron?
    [code] /* dont forget code tags! */ [/code]
    Come visit the Café!

  3. #3
    Join Date
    Sep 2002
    Posts
    6
    Red Baron,

    Many thanks for the coding, I now realised how the main menu works!

    Now, I have one more feature that requires to add is that after a user selected a menu, and perform the calculation, I want the program to ask the user if they wish to perform another calculation.

    Eg: 'Do you want to perform another calculation? (y/n)' and if the user press y or Y then it starts to the main menu asking the user to select which calculation. If the user press n or N then program stops.

    I can't figure out how I can do this?

    Can you please help?

  4. #4
    Join Date
    Sep 2002
    Posts
    78
    Code:
    import java.io.*;
    
    public class MainMenu 
    {
          public static CheckLinesIn CLI = new CheckLinesIn();
          public static Annulus A = new Annulus();
          public static Diameters D = new Diameters();
          public static Circumferences C = new Circumferences();
          
          public static void main (String[] args) throws IOException  //throws IOException doesn't report any erros
          {           //main not menu!
                String input = new String();
                
                for(;;)
                {
                      System.out.println("Please enter a choice");
                      System.out.println("1. CheckLinesIn");
                      System.out.println("2. Annulus");
                      System.out.println("3. Diameters");
                      System.out.println("4. Circumferences");
                
                      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                      input = br.readLine();
                
                      if(input.equals("1"))
                      {
                            CLI.menu();
                      }
                      else if (input.equals("2"))
                      {
                            A.menu();
                      }
                      else if (input.equals("3"))
                      {
                            D.menu();
                      }
                      else if (input.equals("4"))
                      {
                            C.menu();
                      }
                
                      System.out.println("Do you want to perform another calculation? y/n");
                      input = br.readLine();
                
                      if (input.equals("n") || input.equals("N"))
                      {
                            break;
                      }
                      //no need for continue cause of infinite loop
                }
          }
    }
    
    class CheckLinesIn
    {
          public static void menu ()
          {
                System.out.println("In class CheckLinesIn");
          }
    }
    
    class Annulus
    {
          public static void menu ()
          {
                System.out.println("In class Annulus");
          }
    }
    
    class Diameters
    {
          public static void menu ()
          {
                System.out.println("In class Diameters");
          }
    }
    
    class Circumferences
    {
          public static void menu ()
          {
                System.out.println("In class Circumferences");
          }
    }
    ¿Red Baron?
    [code] /* dont forget code tags! */ [/code]
    Come visit the Café!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center