Home > Software engineering >  Is switch-case sufficient to create multiple steps in a console app for modularization?
Is switch-case sufficient to create multiple steps in a console app for modularization?

Time:02-16

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?

CodePudding user response:

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.

CodePudding user response:

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
  • Related