Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
Is switch-case sufficient to create multiple steps in a console app for modularization?

I want to modify a console app, to modularize it (in C#). The output should ask input by module so that there are two steps before reaching operation. First is Module and second is Operation on that Module. For example current output is:

1. Add a member to A
2. View all members of A
3. Add a member to B
4. View all members of B
5. Add a member to C
6. View all members of C

I am using switch case statement to choose an option in user input and perform operation on it. After modularization, I want to get an output like:

1. A
2. B
3. C

After selecting 1 as user input, the output should be like:

1.1. Add element to A
1.2. View all elements of A

and so on for the rest of the user inputs. Now again when I enter 1.1. or 1.2., the final operation gets performed and the method is called. How shall I proceed?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If i got that correctly, you want to create an interactive CLI (Command Line Interface) menu with submenus.

For that purpose you could look online for a C# MVC CLI framework with the features you want (some good googling keywords might be "c# cli menu" and equivalent phrasing)

If you want to implement it yourself, consider first a separation of UI and Logic. Your UI classes should only be concerned with the UI current state and user inputs. In order to call the logic dynamically, a dictionary of callbacks in the View class might be a simple solution, as in upon confirmation, given the current state call callbacks[selection] if criterias are met

As an ending note, i think you're overengineering it. A simpler indentation could do the trick easier, since we're talking about a CLI.

1. A
   1.1 view member
   1.2 add member
2. B
   2.1 whatever
over 4 years ago · Santiago Trujillo Relatório

0

If you just want a simple switch menu, just see my demo:

Feel free to modify this code according to your needs.

using System;

namespace ConsoleApp5
{
    internal class Program
    {
        static void Main(string[] args)
        {
            MainMenu();
            Console.ReadLine();
            int MainMenu()
            {
                while (true)
                {
                    Console.WriteLine("\nPlease select menu item (key in 1-3): ");
                    Console.WriteLine("1.A");
                    Console.WriteLine("2.B");
                    Console.WriteLine("3.C");
                    int choice; 
                    int.TryParse(Console.ReadLine(),out choice);
                    switch (choice)
                    {
                        case 1: AMenu(); break;
                        case 2://to do
                                break;
                        case 3://to do
                                break;

                        default: Console.WriteLine("\nInvalid input!\n"); break;
                    }
                    Console.WriteLine("\nIf you want to continue, press y, otherwise, press any other key!");

                    string ct = Console.ReadLine();
                    if (ct != "y")
                        break;
                }
                return 0;
            }

            int AMenu()
            {
                while (true)
                {
                    Console.WriteLine("\nPlease select menu item (key in 1.1-1.2): ");
                    Console.WriteLine("1.1. Add element to A");
                    Console.WriteLine("1.2. View all elements of A");
                    double choice;
                    double.TryParse(Console.ReadLine(), out choice);
                    if (choice == 1.1) choice = 1;
                    else if (choice == 1.2) choice = 2;
                    switch (choice)
                    {
                        case 1: Console.WriteLine("Add element to A"); ; break;
                        case 2: Console.WriteLine("View all elements of A"); break;
                        default: Console.WriteLine("\nInvalid input!\n"); break;
                    }
                    Console.WriteLine("\nIf you want to continue, press y, otherwise, press any other key!");
                    string ct = Console.ReadLine();
                    if (ct != "y")
                        break;
                }
                return 0;
            }
        }
    }
}

Output:

enter image description here

If you have questions about my code, please comment below.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda