Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help - Return to menu screen

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help - Return to menu screen

    Hi!

    I'm quite a Java newb and doing a college assignment. I was just wondering if anyone could show me how to ask the user if they want to return to the main menu screen. Either ask the user if they want to, or just have it print out after each menuoption "Type [KEY] to return to main menu". Something simple... Here's my code:

    import java.util.Scanner;
     
    /** PROGRAMMING FUNDAMENTALS ASSIGNMENT 2009
     * Designing a program to record Taxi stuff
     */
     
    class C22Taxis
    {
        public static void main(String [] args)
        {   
            Scanner input = new Scanner(System.in);
            int menuin;
            int menuoption = 0;
            int selecttaxi = 0;
            String [] taxi = new String[3];
            String [] dest = new String[3];
            String [] name = new String[3];
            String [] service = new String[3];
     
            System.out.println("Welcome to C22 Taxis Menu");
            System.out.println("Select an option/number");
            System.out.println("");
            System.out.println("1) Change Taxi Booking State");
            System.out.println("2) Change Taxi Serviceability");
            System.out.println("3) Enter/Change Taxi Driver's Name");
            System.out.println("4) List All Taxi Details");
            System.out.println("  (Taxi number, driver, serviceability and availability)");
            System.out.println("");
     
            menuoption = input.nextInt();
     
            // BOOKING STATE - OPTION 1
     
            if (menuoption == 1)
            {
                System.out.println('\f');
     
                System.out.println("C22Taxi's Booking State Log");
                System.out.println("");
                System.out.print("Enter taxi number: ");
                selecttaxi = input.nextInt();
                {
                    if (selecttaxi == 1)
                    {
                       System.out.print("Enter destination for Taxi " + selecttaxi + " (if none enter NONE): ");
                       dest[0] = input.next();
                    }   else if (selecttaxi == 2)    {
                             System.out.print("Enter destination for Taxi " + selecttaxi + " (if none enter NONE): ");
                             dest[1] = input.next(); }
                        else if (selecttaxi == 3)    {
                             System.out.print("Enter destination for Taxi " + selecttaxi + " (if none enter NONE): ");
                             dest[2] = input.next(); }
     
                }
            }    
     
     
            // TAXI SERVICEABILITY - OPTION 2
     
            if (menuoption == 2)
            {
                System.out.println('\f');
     
                System.out.println("C22Taxi's Serviceability Log");
                System.out.println("");
                System.out.print("Enter taxi number: ");
                selecttaxi = input.nextInt();
                {
                    if (selecttaxi == 1)
                    {
                       System.out.print("Has Taxi " + selecttaxi + " the taxi been serviced? (Yes/No): ");
                       service[0] = input.next();
                       System.out.println("Current service state for Taxi " + selecttaxi + ": " + service[0]);
                    }   else if (selecttaxi == 2)    {
                             System.out.print("Has Taxi " + selecttaxi + " the taxi been serviced? (Yes/No): ");
                             service[1] = input.next();
                             System.out.println("Current service state for Taxi " + selecttaxi + ": " + service[1]); }
                        else if (selecttaxi == 3)    {
                             System.out.print("Has Taxi " + selecttaxi + " the taxi been serviced? (Yes/No): ");
                             service[2] = input.next();
                             System.out.println("Current service state for Taxi " + selecttaxi + ": " + service[2]); }
     
                }
     
     
            }
     
            // TAXI DRIVER NAME - OPTION 3
     
            if (menuoption == 3)
            {
                System.out.println('\f');
     
                System.out.println("C22Taxi's Driver Log");
                System.out.println("");
                System.out.print("Enter taxi number: ");
                selecttaxi = input.nextInt();
                {
                    if (selecttaxi == 1)
                    {
                        System.out.print("Enter current/new driver for Taxi " + selecttaxi + ": ");
                        name[0] = input.next();
                        System.out.println("Taxi " + selecttaxi + " current driver: " + name[0]);
                    }   else if (selecttaxi == 2)   {
                            System.out.print("Enter current/new driver for Taxi " + selecttaxi + ": ");
                            name[1] = input.next();
                            System.out.println("Taxi " + selecttaxi + " current driver: " + name[1]);  }  
                        else if (selecttaxi == 3)   {
                            System.out.print("Enter current/new driver for Taxi " + selecttaxi + ": ");
                            name[2] = input.next();
                            System.out.println("Taxi " + selecttaxi + " current driver: " + name[2]);  } 
     
                } 
     
            }
     
            // TAXI VEHICLE DETAILS - OPTION 3
     
            if (menuoption == 4)
            {
                System.out.println('\f');
     
                System.out.println("C22Taxi's Vehicle Details");
                System.out.println("");
                System.out.print("Enter taxi number: ");
                selecttaxi = input.nextInt();
                {
                    if (selecttaxi == 1)
                    {            
                        System.out.println("Taxi Infomation");
                        System.out.println("-----------------");
                        System.out.println("Number: " + selecttaxi);
                        System.out.println("Driver: " + name[0]);
                        System.out.println("Serviceability: " + service[0]);
                        System.out.println("Availability: " + dest[0]);
                    }   else if (selecttaxi == 2)   {
                            System.out.println("Taxi Infomation");
                            System.out.println("-----------------");
                            System.out.println("Number: " + selecttaxi);
                            System.out.println("Driver: " + name[1]);
                            System.out.println("Serviceability: " + service[1]);
                            System.out.println("Availability: " + dest[1]);  }
                       else if (selecttaxi == 3)   {
                            System.out.println("Taxi Infomation");
                            System.out.println("-----------------");
                            System.out.println("Number: " + selecttaxi);
                            System.out.println("Driver: " + name[2]);
                            System.out.println("Serviceability: " + service[2]);
                            System.out.println("Availability: " + dest[2]);  }
     
                        }
     
                    }
     
            // END OF OPTIONS
        }
     
    }

    Any help would be useful. Thanks.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help - Return to menu screen

    The best way to do this is with an event-driven model, but here it's sufficient to have a pseudo-event processor like such. I'll leave implementing your code into something like this up to you.

    public class MenuExample
    {
         public static int mainMenu()
         {
              System.out.println("This is the main menu");
              System.out.println("1. Go to menu 1");
              System.out.println("2. Go to menu 2");
              System.out.println("3. Quit");
              Scanner reader = new Scanner(System.in);
              int read;          
              do
              {
                   read = reader.nextInt();
              } while (read < 1 || read > 3);
              return read;
         }
     
         public static  int menu1()
         {
              System.out.println("Menu 1");
              System.out.println("1. Choose item A");
              System.out.println("2. Choose item B");
              System.out.println("3. Go to the main menu");
              Scanner reader = new Scanner(System.in);
              int read;          
              do
              {
                   read = reader.nextInt();
              } while (read < 1 || read > 3);
              return read;
         }
     
         public static int menu2()
         {
              System.out.println("Menu 2");
              System.out.println("1. Choose item A");
              System.out.println("2. Choose item B");
              System.out.println("3. Go to the main menu");
              Scanner reader = new Scanner(System.in);
              int read;          
              do
              {
                   read = reader.nextInt();
              } while (read < 1 || read > 3);
              return read;
         }
     
         public static void main(String[] args)
         {
              boolean quit = false;
              while (!quit)
              {
                   int action = mainMenu();
                   if (action == 1)
                   {
                        do
                        {
                             action = menu1();
                             if (action == 1)
                             {
                                  System.out.println("You chose item A in menu 1");
                             }
                             else if (action == 2)
                             {
                                  System.out.println("You chose item B in menu 1");
                             }
                   } while (action != 3)
              }
              else if (action == 2)
              {
     
                        do
                        {
                             action = menu1();
                             if (action == 1)
                             {
                                  System.out.println("You chose item A in menu 2");
                             }
                             else if (action == 2)
                             {
                                  System.out.println("You chose item B in menu 2");
                             }
                   } while (action != 3)
              }
              else
              {
                   quit = true;
              }
         }
    }

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help - Return to menu screen

    I'll do some experimenting, thanks for the heads up.

Similar Threads

  1. Building a Menu List
    By websey in forum AWT / Java Swing
    Replies: 1
    Last Post: November 15th, 2009, 10:34 AM
  2. Replies: 1
    Last Post: October 23rd, 2009, 03:17 PM
  3. Content positioning on the screen
    By Drakenmul in forum AWT / Java Swing
    Replies: 1
    Last Post: July 27th, 2009, 09:02 AM
  4. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM
  5. Java program for remote host screen capture
    By krishnaraoveera1294 in forum Java Networking
    Replies: 1
    Last Post: March 13th, 2009, 04:53 AM