How to display several options within a menu in cosola in java?

1

My question is how to make a kind of menu within another menu. For example, I display in my code 4 options to choose from, in one of them if it is chosen it would be necessary to also display several options to choose from. It would be like a nested menu. Below I leave my code:

import java.util.Scanner;

class Menu{

public static void main(String[] args){

    int opciones;
    Scanner leer = new Scanner(System.in);
    Opcion1 op1=null;
    Opcion2 op2=null;
    Opcion3 op3=null;
    Opcion4 op4=null;

    System.out.println("Menu por consola");
    System.out.println("1.- Registrar nuevo empleado");
    System.out.println("2.- Obtener datos de contacto de un empleado");
    System.out.println("3.- Listar todos los empleados que cumplan anios en cierta fecha");
    System.out.println("4.- Obtener el presupuesto total");
    System.out.println("Ingrese la accion a realizar: ") //Pretendo que el usuario escriba el numero de  opcion
    opciones=leer.nexInt();

    switch(opciones) {
    case 1: 
    op1=new Opcion1();
    op1.registrarNuevoEmpleado();
    break;

    case 2:
    op2=new Opcion2();
    op2.obtenerDatos();
    String variable = op1.variableOpcion1;
    break;

    case 3: 
    op3=new Opcion3();
    op3.listarEmpleadosCumpleaños();
    break;

    case 4: 
    op4=new Opcion4();
    op4.presupuestoPorArea();
    break;

    default:
    System.out.println ("Numero no valido"); 
    }

}
}
    
asked by Esteban Quito 26.10.2017 в 03:29
source

2 answers

2

What you ask is simple. You just have to create a new menu within the case of switch . Just like the main menu you have.

class Menu{

    public static void main(String[] args){

        ...

        System.out.println("Menu por consola");
        System.out.println("1.- Registrar nuevo empleado");
        System.out.println("2.- Obtener datos de contacto de un empleado");
        System.out.println("3.- Listar todos los empleados que cumplan anios en cierta fecha");
        System.out.println("4.- Obtener el presupuesto total");
        System.out.println("Ingrese la accion a realizar: ") //Pretendo que el usuario escriba el numero de  opcion
        opciones=leer.nexInt();
        leer.nexLine();

        switch(opciones) {
        case 1: 

        System.out.println("Menu registrar nuevo empleado");
        System.out.println("1.- Opcion 1");
        System.out.println("2.- Opcion 2");
        System.out.println("3.- Opcion 3");
        System.out.println("4.- Opcion 4");

        int opciones2 = leer.nexInt();
        leer.nexLine();

        switch (opciones2) {

            case 1:
                // codigo
                break;

            case 2:
                // codigo
                break;

            case 3:
                // codigo
                break;

            case 4:
                // codigo
                break;

        }

        op1=new Opcion1();
        op1.registrarNuevoEmpleado();
        break;

        case 2:
            menu3();
            ...
            break;

        ...
    }

    public static void menu3() {

        System.out.println("Menu registrar nuevo empleado");
        System.out.println("1.- Opcion 1");
        System.out.println("2.- Opcion 2");
        System.out.println("3.- Opcion 3");
        System.out.println("4.- Opcion 4");

        int opciones2 = leer.nexInt();
        leer.nexLine();

        switch (opciones2) {

            case 1:
                // codigo
                break;

            case 2:
                // codigo
                break;

            case 3:
                // codigo
                break;

            case 4:
                // codigo
                break;

        }
    }
}

Although I recommend that you create the menu within a method and call it within case . As done in the second case of switch main.

    
answered by 26.10.2017 в 04:10
1

You have to use a loop (I recommend the do-while):

import java.util.Scanner;

public class Editor {

public static void main(String[] args) {
    boolean sal = false;
    Scanner in = new Scanner(System.in);
    do{
        System.out.println("Ingresa A para saludarte, y B para despedirme: ");
        switch(in.next()){
            case "A":
                System.out.println("Hola");
                break;
            case "B":
                System.out.println("Adios");
                break;
        }
        System.out.println("¿Salir de este bucle?");
        if(in.next().equals("Si")) sal=true;
        else sal=false;
    }while(!sal);
}   
}

This small example is a do-while loop, in which the loop will run until the user wants to exit (entering any character or string that is different from "Yes").

  • The loop starts giving an instruction.
  • A switch is started, in which the values captured by the keyboard (Scanner) are evaluated. If the user enters A, then it will be printed by "Hello" console. If the user enters B, then it is printed by "Adios" console. (If you enter something other than A or B, simply do nothing and continue with the next part and last)

  • Now in the last part of the loop, the program asks the user if he wants to exit the loop. To continue, the user must enter "Yes" (when pressing Yes, simply our boolean variable is assigned "true" so that the condition of the loop is not met and broken).

The condition is that the loop will continue to run while our boolean variable is different from true.

Already with this you can make menus from console

    
answered by 26.10.2017 в 04:04