Создание меню без делегатов - C#

Узнай цену своей работы

Формулировка задачи:

нужно сделать тоже самое но в одном классе
Листинг программы
  1. class Program
  2. {
  3. delegate void method();
  4. static void Main(string[] args)
  5. {
  6. string[] items = { "Действие 1", "Действие 2", "Действие 3", "Выход" };
  7. method[] methods = new method[] { Method1, Method2, Method3, Exit };
  8. ConsoleMenu menu = new ConsoleMenu(items);
  9. int menuResult;
  10. do
  11. {
  12. menuResult = menu.PrintMenu();
  13. methods[menuResult]();
  14. Console.WriteLine("Для продолжения нажмите любую клавишу");
  15. Console.ReadKey();
  16. } while (menuResult != items.Length - 1);
  17. }
  18. static void Method1()
  19. {
  20. Console.WriteLine("Выбрано действие 1");
  21. }
  22. static void Method2()
  23. {
  24. Console.WriteLine("Выбрано действие 2");
  25. }
  26. static void Method3()
  27. {
  28. Console.WriteLine("Выбрано действие 3");
  29. }
  30. static void Exit()
  31. {
  32. Console.WriteLine("Приложение заканчивает работу!");
  33. }
  34. }
  35.  
  36. class ConsoleMenu
  37. {
  38. string[] menuItems;
  39. int counter = 0;
  40. public ConsoleMenu(string[] menuItems)
  41. {
  42. this.menuItems = menuItems;
  43. }
  44. public int PrintMenu()
  45. {
  46. ConsoleKeyInfo key;
  47. do
  48. {
  49. Console.Clear();
  50. for (int i = 0; i < menuItems.Length; i++)
  51. {
  52. if (counter == i)
  53. {
  54. Console.BackgroundColor = ConsoleColor.Cyan;
  55. Console.ForegroundColor = ConsoleColor.Black;
  56. Console.WriteLine(menuItems[i]);
  57. Console.BackgroundColor = ConsoleColor.Black;
  58. Console.ForegroundColor = ConsoleColor.White;
  59. }
  60. else
  61. Console.WriteLine(menuItems[i]);
  62. }
  63. key = Console.ReadKey();
  64. if (key.Key == ConsoleKey.UpArrow)
  65. {
  66. counter--;
  67. if (counter == -1) counter = menuItems.Length - 1;
  68. }
  69. if (key.Key == ConsoleKey.DownArrow)
  70. {
  71. counter++;
  72. if (counter == menuItems.Length) counter = 0;
  73. }
  74. }
  75. while (key.Key != ConsoleKey.Enter);
  76. return counter;
  77. }
  78. }

Решение задачи: «Создание меню без делегатов»

textual
Листинг программы
  1.  class Program
  2.     {
  3.         static void Method1()
  4.         {
  5.             Console.WriteLine("Выбрано действие 1");
  6.         }
  7.         static void Method2()
  8.         {
  9.             Console.WriteLine("Выбрано действие 2");
  10.         }
  11.         static void Method3()
  12.         {
  13.             Console.WriteLine("Выбрано действие 3");
  14.         }
  15.         static void Exit()
  16.         {
  17.             Console.WriteLine("Приложение заканчивает работу!");
  18.             Environment.Exit(0);
  19.         }
  20.         static void methods (int counter)
  21.         {
  22.             if (counter == 0)
  23.                 Method1();
  24.             else if (counter == 1)
  25.                 Method2();
  26.             else if (counter == 2)
  27.                 Method3();
  28.             else Exit();
  29.          
  30.         }
  31.         static void Main(string[] args)
  32.         {
  33.             while (true)
  34.             {
  35.                 int counter;
  36.                 string[] items = { "Действие 1", "Действие 2", "Действие 3", "Выход" };
  37.  
  38.                 PrintMenu(items, out counter);
  39.                 methods(counter);
  40.                 Console.ReadKey();
  41.             }
  42.  
  43.         }
  44.         static int PrintMenu(string[] menuitems,out int counter)
  45.         {
  46.  
  47.             counter = 0;
  48.             ConsoleKeyInfo key;
  49.             do
  50.             {
  51.                 Console.Clear();
  52.                 for (int i = 0; i < menuitems.Length; i++)
  53.                 {
  54.                     if (counter == i)
  55.                     {
  56.                         Console.BackgroundColor = ConsoleColor.White;
  57.                         Console.ForegroundColor = ConsoleColor.Black;
  58.                         Console.WriteLine(menuitems[i]);
  59.                         Console.BackgroundColor = ConsoleColor.Black;
  60.                         Console.ForegroundColor = ConsoleColor.White;
  61.                     }
  62.                     else
  63.                         Console.WriteLine(menuitems[i]);
  64.  
  65.                 }
  66.                 key = Console.ReadKey();
  67.                 if (key.Key == ConsoleKey.UpArrow)
  68.                 {
  69.                     counter--;
  70.                     if (counter == -1) counter = menuitems.Length - 1;
  71.                 }
  72.                 if (key.Key == ConsoleKey.DownArrow)
  73.                 {
  74.                     counter++;
  75.                     if (counter == menuitems.Length) counter = 0;
  76.                 }
  77.             }
  78.             while (key.Key != ConsoleKey.Enter);
  79.             return counter;
  80.         }
  81.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.2 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы