Raznish Smith
Raznish Smith

Reputation: 67

Return to main menu from sub menu in java

So my current code has the problem that from the sub menu when I hit exit(4) it doesn't return me to the main menu, it just repeats the if statement within the main menu while loop which initiates the sub menu. How do I fix this?

import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;

public class StarberksInterface
{


public static void main(String args[])
{

    Scanner console = new Scanner(System.in);
    store = new Store();
    String str, sName1, sName2, name;
    char c;
    int n=0;
    sName1 = "Callahan";
    sName2 = "Lambton";


    //This is the main menu that will be displayed first.
    System.out.println("       MAIN MENU FOR MANAGEMENT SYSTEM");
    System.out.println("===============================================");
    System.out.println("1. CHOOSE STORE");
    System.out.println("2. DISPLAY STORES");
    System.out.println("3. LOAD STORE VIA FILE");
    System.out.println("4. SAVE STORE TO FILE ");
    System.out.println("5. EXIT PROGRAM");
    System.out.println("===============================================");

    while(n!=5)// Exits the program when 4 is pressed
        {
            System.out.print("\n Please enter option 1-4 to continue...: ");
            n = Integer.parseInt(System.console().readLine());
            // Reads user input and takes them to selected code.
            if (n>5||n<1)
            {
                System.out.print("Invalid input, please try again...");
                continue;
            }
            if (n==1)// Takes to option 1 or sub menu
            {
                str="y";

                while(str.equals("y")||str.equals("Y"))
                {
                    System.out.println("Enter a store name [Callahan or Lambton] ");
                    name = console.next();
                    if (sName1.equals(name)|| sName2.equals(name))
                    {
                        StarberksInterface.subMenu();
                        return;
                    }
                    else 
                    {
                        System.out.println("There is no store under this name. Please try again.");
                    }

                }

            }
            if (n==2)// Gathers products in stores and displays the number of products
            {
                System.out.println(" Store data is being displayed.");
                System.out.println("===============================");
                System.out.println("Store: Callahan");
                System.out.println("       Number of products: "+store.getProductListSize());

            }
        }
}




public static void subMenu()
{
Scanner console = new Scanner(System.in);
String str;
char c;
int n=0;


        // this will be the sub menu that gets displayed.
        System.out.println("        INVENTORY MANAGEMENT SYSTEM");
        System.out.println("===============================================");
        System.out.println("1. ADD PRODUCT DATA");
        System.out.println("2. VIEW SINGLE PRODUCT DATA");
        System.out.println("3. DELETE PRODUCT");
        System.out.println("4. DISPLAY ALL PRODUCTS IN STORE");
        System.out.println("===============================================");
        System.out.println("5. EXIT SUB MENU");

        while(n!=5)// Exits the program when 4 is pressed
        {
            System.out.print("\n Please enter option 1-5 to continue...: ");
            n = Integer.parseInt(System.console().readLine());
            // Reads user input and takes them to selected code.
            if (n>5||n<1)
            {
                System.out.print("Invalid input, please try again...");
                continue;
            }
            if (n==1)// Takes to option 1 or addItem()
            {
                str="y";
                while(str.equals("y")||str.equals("Y"))
                {

                    StarberksInterface.addItem();
                    System.out.print("Would you like to enter another product ? (Y or N) : ");
                    str = console.next();
                }   
                continue;
            }
            if (n==2)// Takes to option 2 or prodData
            {
                str="y";
                while(str.equals("y")||str.equals("Y"))
                {
                    StarberksInterface.prodData();
                    System.out.println("\n***************************************************\n");
                    System.out.print("Stay viewing this page? (Y or N) ");
                    str = console.next();

                }
                continue;
            }

            if (n==3)// Takes to option 3 or delete item
            {
                System.out.print("Delete a product");
                continue;
            }

            if (n==4)// Takes to option 4 or view all products in store
            {
                System.out.print("Displaying all products in store");
                continue;
            }
        }
            if (product != null)// If there is information on the system
            // then the user will have the option to view data, before the program quits
            {
                System.out.println("\n***************************************************\n");
                System.out.println("\nAre you sure you want to quit? There is information stored on a product. ");
                System.out.println("\nWould you like to view if information? (Y / N) ");
                str="";
                str = console.next();
                while(str.equals("y")||str.equals("Y"))
                {

                    StarberksInterface.prodData();
                    return;
                }

            }

            else
            {
                System.out.print("\nThank you for using this inventory management software.\n");
                System.out.print("Developed by Xavier Edwards");
                System.out.println("\n***************************************************\n");
            }

Upvotes: 1

Views: 4421

Answers (3)

try to replace

n = Integer.parseInt(System.console().readLine());

by

n = console.nextInt();

in both places

Upvotes: 0

Manish Kothari
Manish Kothari

Reputation: 1712

Change the return statement in

if (sName1.equals(name) || sName2.equals(name)) {
    StarberksInterface.subMenu();
    return;
}

to

if (sName1.equals(name) || sName2.equals(name)) {
    StarberksInterface.subMenu();
    break;
}

To Print the menu again you will need to move the main menu inside the loop :

while (n != 5)// Exits the program when 4 is pressed{
    .....
}

Upvotes: 2

You need to move the main menu inside this loop:

 while(n!=5)/
{
...
}

Upvotes: 0

Related Questions