Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.IO;
- using System;
- using Lab2.MenuTest;
- namespace Lab2
- {
- namespace MenuTest
- {
- class Menu
- {
- public string Title { get; }
- public Dictionary<string, Action> Commands { get; } = new();
- public Menu(string title) => Title = title;
- public void ProcessMenu()
- {
- Console.WriteLine($"{Title}:");
- for(int index = 0; index < Commands.Count; ++index)
- Console.WriteLine($"{index} - {Commands.ElementAt(index).Key}");
- Console.Write("Enter a command: ");
- var command = int.Parse(Console.ReadLine());
- if(command < Commands.Count && command >= 0)
- {
- Commands.ElementAt(command).Value.Invoke();
- }
- else
- {
- Console.WriteLine($"Invalid command: {command}");
- }
- }
- }
- class MainWindow
- {
- private Menu? CurrentMenu { get; set; } = null;
- public MainWindow()
- {
- var mainMenu = new Menu("Main menu");
- var helloworldMenu = new Menu("Helloworld");
- mainMenu.Commands.Add("Exit", () => { System.Environment.Exit(0); });
- mainMenu.Commands.Add("To hello world", () => { SetCurrentMenu(helloworldMenu); });
- helloworldMenu.Commands.Add("Print", () => { Console.WriteLine("Hello, world"); });
- helloworldMenu.Commands.Add("To main menu", () => { SetCurrentMenu(mainMenu); });
- SetCurrentMenu(mainMenu);
- }
- private void SetCurrentMenu(Menu menu) => CurrentMenu = menu;
- public void ProcessEvents() => CurrentMenu?.ProcessMenu();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var window = new MainWindow();
- while(true)
- window.ProcessEvents();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement