How to get back to the start menu of my program

Asked

Viewed 869 times

0

Guys, here’s the thing... I am doing a project for the POO chair and wanted every time I chose some option and completed the form that is within the classes, I returned to the main menu, until I typed 6 and ended the program. But every time I start the program and choose option 1, for example, when I finish the form the program sends me to the other option, ie option 2, and does not return to the main menu. How can I get back to the main menu right after choosing one of the options?

public class Menu {

public void menuInicial() {

    int opcao = 0;
    Scanner sc = new Scanner(System.in);
    InserirCliente inserirCliente = new InserirCliente();
    InserirFuncionario inserirFuncionario = new InserirFuncionario();
    InserirEstoque inserirEstoque = new InserirEstoque();
    InserirAgendamento inserirAgendamento = new InserirAgendamento();
    BuscarAgendamento buscarAgendamento = new BuscarAgendamento();
    Main main = new Main();
    Cliente cliente = new Cliente();

    do {

        System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
        System.out.println("                 BARBEARIA");
        System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
        System.out.println("\n"
                + "1 - Cadastro de Cliente.\n"
                + "2 - Cadastro de Funcionário.\n"
                + "3 - Venda de produtos.\n"
                + "4 - Agendamento de clientes.\n"
                + "5 - Estoque.\n"
                + "6 - Encerrar.");
        opcao = sc.nextInt();
        switch (opcao) {

            case 1: {
                inserirCliente.inserirCliente();
            }

            case 2: {
                inserirFuncionario.inserirFuncionario();
            }

            case 4: {
                System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
                System.out.println("                 AGENDAMENTO");
                System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
                System.out.println("\n"
                        + "1 - Novo agendamento.\n"
                        + "2 - Buscar agendamento.");
                int opcaoAgendamento = sc.nextInt();
                switch (opcaoAgendamento) {
                    case 1:
                        inserirAgendamento.inserirAgendamento();
                        break;
                    case 2:
                        buscarAgendamento.buscarAgendamento();
                        break;
                    default:
                        System.out.println("Opção inválida.");
                        break;
                }
            }

            case 5: {
                inserirEstoque.inserirEstoque();
            }

            sc.close();
        }

    } while (opcao != 6);
}

}

1 answer

2

Add the break at the end of each case, except in the case 5. This command is used to interrupt the switch and pass the execution flow to first instruction after the switch, which in your case is while.

To execute the method sc.close() in the end, you can leave the case 5 without the instruction break, because then he will be executed in the sequence.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.