Создание меню без делегатов - C#
Формулировка задачи:
нужно сделать тоже самое но в одном классе
class Program
{
delegate void method();
static void Main(string[] args)
{
string[] items = { "Действие 1", "Действие 2", "Действие 3", "Выход" };
method[] methods = new method[] { Method1, Method2, Method3, Exit };
ConsoleMenu menu = new ConsoleMenu(items);
int menuResult;
do
{
menuResult = menu.PrintMenu();
methods[menuResult]();
Console.WriteLine("Для продолжения нажмите любую клавишу");
Console.ReadKey();
} while (menuResult != items.Length - 1);
}
static void Method1()
{
Console.WriteLine("Выбрано действие 1");
}
static void Method2()
{
Console.WriteLine("Выбрано действие 2");
}
static void Method3()
{
Console.WriteLine("Выбрано действие 3");
}
static void Exit()
{
Console.WriteLine("Приложение заканчивает работу!");
}
}
class ConsoleMenu
{
string[] menuItems;
int counter = 0;
public ConsoleMenu(string[] menuItems)
{
this.menuItems = menuItems;
}
public int PrintMenu()
{
ConsoleKeyInfo key;
do
{
Console.Clear();
for (int i = 0; i < menuItems.Length; i++)
{
if (counter == i)
{
Console.BackgroundColor = ConsoleColor.Cyan;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine(menuItems[i]);
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
else
Console.WriteLine(menuItems[i]);
}
key = Console.ReadKey();
if (key.Key == ConsoleKey.UpArrow)
{
counter--;
if (counter == -1) counter = menuItems.Length - 1;
}
if (key.Key == ConsoleKey.DownArrow)
{
counter++;
if (counter == menuItems.Length) counter = 0;
}
}
while (key.Key != ConsoleKey.Enter);
return counter;
}
}Решение задачи: «Создание меню без делегатов»
textual
Листинг программы
class Program
{
static void Method1()
{
Console.WriteLine("Выбрано действие 1");
}
static void Method2()
{
Console.WriteLine("Выбрано действие 2");
}
static void Method3()
{
Console.WriteLine("Выбрано действие 3");
}
static void Exit()
{
Console.WriteLine("Приложение заканчивает работу!");
Environment.Exit(0);
}
static void methods (int counter)
{
if (counter == 0)
Method1();
else if (counter == 1)
Method2();
else if (counter == 2)
Method3();
else Exit();
}
static void Main(string[] args)
{
while (true)
{
int counter;
string[] items = { "Действие 1", "Действие 2", "Действие 3", "Выход" };
PrintMenu(items, out counter);
methods(counter);
Console.ReadKey();
}
}
static int PrintMenu(string[] menuitems,out int counter)
{
counter = 0;
ConsoleKeyInfo key;
do
{
Console.Clear();
for (int i = 0; i < menuitems.Length; i++)
{
if (counter == i)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine(menuitems[i]);
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
else
Console.WriteLine(menuitems[i]);
}
key = Console.ReadKey();
if (key.Key == ConsoleKey.UpArrow)
{
counter--;
if (counter == -1) counter = menuitems.Length - 1;
}
if (key.Key == ConsoleKey.DownArrow)
{
counter++;
if (counter == menuitems.Length) counter = 0;
}
}
while (key.Key != ConsoleKey.Enter);
return counter;
}
}