0

I am displaying menu in the console based application using C#. I want that if user press enter key then it should display same menu. Application should not break. I am new to C# If anyone has idea please share it. It will be helpful to me.

Below is the code.

public static void HospitalMenu()
{            
    string answer = "";
       
    do
    {
        Console.Clear();
           
                
        Console.WriteLine("=============Hospital Management System===================");
        Console.WriteLine("1...............Add Patient Information");
        Console.WriteLine("2...............Modify Patient Information");
        Console.WriteLine("3...............Add Patient Treatment Information");
        Console.WriteLine("4...............View Patient History");
        Console.WriteLine("5...............Search Patient Info");
        Console.WriteLine("6...............Generate Lab Report");
        Console.WriteLine("7...............Generate Medical Bills");
        Console.WriteLine("8...............Exit");


        Console.WriteLine("Select option (between 1 to 8)");
        int option = Convert.ToInt32(Console.ReadLine());
            

        switch (option)
        {
            case 1:
                Patient.InsertPatient();
                break;
            case 2:
                Patient.UpdatePatient();
                break;
            case 3:
                PatientTreatment();
                break;
             case 4:
                ViewMenu();
                break;
            case 5:
                SearchMenu();
                break;
            case 6:
                LabMenu();
                break;
            case 7:
                BillMenu();
                break;
            default:
                Environment.Exit(0);                       
                break;
        }
          

        Console.WriteLine("Do you want to continue ? (Enter y if yes)");
        answer = Console.ReadLine();
            

    } while (answer == "y" || answer == "Y");
}


   

Thanks in advance.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

1 Answers1

0

I'll suggest some changes in your code.

Let's create an Interface that you'll be a contract responsible to show and handle inputs

IMenuHandler.cs

    public interface IMenuHandler<TInput>
    {
        void Show( Action stopApplicationHandler, IMenuHandler<TInput> callerMenu=null);
        void HandleInput(TInput input, Action stopApplication, IMenuHandler<TInput> callerMenu);
    }

Then let's create your menus.. .

As an example you'll create two, you should modify it for your needs.

MainMenu.cs

 public class MainMenu:IMenuHandler<ConsoleKeyInfo>
    {
        
        public void Show(Action stopApplicationHandler, IMenuHandler<ConsoleKeyInfo> callerMenu = null)
        {
            Console.Clear();
            Console.WriteLine("=============Hospital Management System===================");
            Console.WriteLine("1...............Add Patient Information");
            Console.WriteLine("2...............Modify Patient Information");
            Console.WriteLine("3...............Add Patient Treatment Information");
            Console.WriteLine("4...............View Patient History");
            Console.WriteLine("5...............Search Patient Info");
            Console.WriteLine("6...............Generate Lab Report");
            Console.WriteLine("7...............Generate Medical Bills");
            Console.WriteLine("8...............Exit");
            Console.WriteLine("Select option (between 1 to 8)");
            HandleInput(Console.ReadKey(), stopApplicationHandler, callerMenu ?? this);
        }

        public void HandleInput(ConsoleKeyInfo input, Action stopApplication, IMenuHandler<ConsoleKeyInfo> callerMenu)
        {
            switch (input.Key)
            {
                case ConsoleKey.D1:
                    new HelpMenu().Show(stopApplication, callerMenu);
                    break;
                case ConsoleKey.D8:
                    stopApplication.Invoke();
                    break;
                default:
                    Show(stopApplication, this);
                    break;
            }
        }
    }

HelpMenu.cs

public class HelpMenu:IMenuHandler<ConsoleKeyInfo>
    {


        public void Show(Action stopApplicationHandler, IMenuHandler<ConsoleKeyInfo> callerMenu = null)
        {
            Console.Clear();
            Console.WriteLine("Help Menu Example...");
            Console.WriteLine("1...............Go back");
            Console.WriteLine("2...............Exit");
            HandleInput(Console.ReadKey(), stopApplicationHandler, callerMenu );
        }

        public void HandleInput(ConsoleKeyInfo input, Action stopApplication, IMenuHandler<ConsoleKeyInfo> callerMenu)
        {
            Console.WriteLine("Help menu handler...");
            switch (input.Key)
            {
                case ConsoleKey.D1:
                    callerMenu.Show(stopApplication, this);
                    break;
                case ConsoleKey.D2:
                    stopApplication.Invoke();
                    break;
                default:
                    Show(stopApplication, callerMenu);
                    break;
            }
        }
    }

Now we are going to create an wrapper for your application.

Application.cs

 public class Application
    {
        public delegate void OnStopApplicationRequestHandler();
        public event OnStopApplicationRequestHandler StopApplicationRequest;
        private readonly CancellationTokenSource _cancellationTokenSource;

        public Application(CancellationToken? cancellationToken=null, OnStopApplicationRequestHandler? stopApplicationRequestHandler=null)
        {
            _cancellationTokenSource = cancellationToken != null 
                ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken.Value) 
                : new CancellationTokenSource();
            StopApplicationRequest += stopApplicationRequestHandler ?? ConfigureDefaultStopApplicationRequestHandler();
        }

        private OnStopApplicationRequestHandler ConfigureDefaultStopApplicationRequestHandler()
           => () =>
           {
               Console.WriteLine("Stopping application...");
               _cancellationTokenSource.Cancel();
           };
        

        public void Run()
        {
            try
            {
                while (!_cancellationTokenSource.Token.IsCancellationRequested)
                    new MainMenu().Show(Stop);
                Console.WriteLine("Program has been stopped");
            }
            //.... You should handle other custom exceptions here
            catch (Exception ex)
            {
                // I'll assume that you will stop your application case it hits this unhandled exception
                Console.WriteLine(ex);
                Stop();
            }
        }

        private void Stop()
        {
            StopApplicationRequest?.Invoke();
        }

    }

Note that this class has an event that will be responsible to handle the application exits.

You should modify it for your needs.

Last but not least

Call your application wrapper in your Program.cs

    internal class Program
    {
        static void Main(string[] args)
        {

           new Application().Run();

        }
    }

PS: Don't forget the correct usings....

Hope this helps

Vinicius Andrade
  • 151
  • 1
  • 4
  • 22