Home > Mobile >  Is there a simple way for menu navigation logic (forward, rewind, exit) in C# console application?
Is there a simple way for menu navigation logic (forward, rewind, exit) in C# console application?

Time:04-18

I recently developed a small C# console application (approx. 6000 lines of code). So far it works perfect but sequential. I wanted to add an additional dimension.

Req. User must enter values. After entering the value XYZ the user should have the possibility to:

  1. rewind (enter code again)
  2. press ENTER so next promt will appear
  3. press ESC so program will close

I did a small mockup in order to show you what I tried. So far it works perfectly my question is simple:

Is there a more efficant way to code what I coded? I have a feeling that I did not program it in an very efficiant way in terms of logic. I will get more and more nested due to the fact that this was the first "prompt" of approx. 10 prompts...

Many thanks in advance...

// Getting car brand.
        try
        {
            while (programEnd == 0)
            {
                Console.Clear();
                model.Brand_definition(this.view.Brand_prompting());
                this.view.Selection();
                ConsoleKeyInfo select = Console.ReadKey(true);

                if (select.Key == ConsoleKey.Z)
                {
                }
                else if (select.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine(" NEXT ");
                    Console.ReadKey();
                }
                else if (select.Key == ConsoleKey.Escape)
                {
                    programEnd = 1;
                    Console.Clear();
                    Console.WriteLine("  ");
                    Console.WriteLine("  ");
                    Console.WriteLine(" TEST OUTPUT - QUIT APP 1 ");
                    Thread.Sleep(2000);
                    Environment.Exit(0);
                }
                else
                {
                    int exiter = 0;
                    while (exiter == 0)
                    {
                        if (select.Key == ConsoleKey.Escape)
                        {
                            Console.Clear();
                            Console.WriteLine("  ");
                            Console.WriteLine("  ");
                            Console.WriteLine(" TEST OUTPUT - QUIT APP 2 ");
                            Thread.Sleep(2000);
                            Environment.Exit(0);
                        }
                        else if (select.Key == ConsoleKey.Enter)
                        {
                            Thread.Sleep(2000);
                            Console.Clear();
                            Console.WriteLine(" NEXT 2 ");
                        }
                        else if (select.Key == ConsoleKey.Z)
                        {
                            exiter = 1;
                        }
                        else
                        {
                            select = Console.ReadKey(true);
                        }
                    }
                }
            }
        }
        catch (Exception exEnd)
        {
            Console.WriteLine(" ERROR! ", exEnd);
            programEnd = 1;
        }

CodePudding user response:

You can refactor your code and extract parts of it to separate methods. For example, you can extract the code inside of the try block:

try
    {
        RunApp();
    }
    catch (Exception exEnd)
    {
        Console.WriteLine(" ERROR! ", exEnd);
        programEnd = 1;
    }

Then in the RunApp method you can have something like that:

private void RunApp()
{
    while (programEnd == 0)
    {
        Console.Clear();
        model.Brand_definition(this.view.Brand_prompting());
        this.view.Selection();
        ConsoleKeyInfo select = Console.ReadKey(true);


        switch (select.Key)
        {
            case ConsoleKey.Z:
                Z_Case();
                break;
            case ConsoleKey.Enter:
                Enter_Case();
                break;
            case ConsoleKey.Escape:
                Escape_Case();
                break;
            default:
                Default_Case();
                break;
        }
    }
}

and you can pass to these methods whatever you need.

  • Related