Commit eb284dfc authored by Andres Käver's avatar Andres Käver
Browse files

menu

parent f212cc65
......@@ -12,17 +12,20 @@ namespace ConsoleApp
Console.Clear();
Console.WriteLine("Hello Calc!");
var mainMenu = new Menu();
mainMenu.MenuItems = new List<MenuItem>()
var mainMenu = new Menu(EMenuLevel.Root);
mainMenu.AddMenuItems(new List<MenuItem>()
{
new MenuItem("A", "Aaaaaaaaa"),
new MenuItem("B", "BBBBB"),
new MenuItem("C", "Cccccc"),
};
});
mainMenu.Run();
var choice = MainMenu();
Console.WriteLine("done! " + choice);
}
......@@ -63,7 +66,6 @@ namespace ConsoleApp
done = true;
break;
}
} while (!done);
return userChoice;
......
namespace MenuSystem
{
public enum EMenuLevel
{
Root,
First,
SecondOrMore
}
}
\ No newline at end of file
......@@ -5,22 +5,94 @@ namespace MenuSystem
{
public class Menu
{
public List<MenuItem> MenuItems { get; set; } = new List<MenuItem>();
private readonly EMenuLevel _menuLevel;
private readonly List<MenuItem> _menuItems = new List<MenuItem>();
private readonly MenuItem _menuItemExit = new MenuItem("E", "Exit");
private readonly MenuItem _menuItemReturn = new MenuItem("R", "Return");
private readonly MenuItem _menuItemMain = new MenuItem("M", "Main");
private readonly HashSet<string> _menuShortCuts = new HashSet<string>();
public Menu(EMenuLevel menuLevel)
{
_menuLevel = menuLevel;
_menuShortCuts.Add(_menuItemExit.ShortCut.ToUpper());
_menuShortCuts.Add(_menuItemReturn.ShortCut.ToUpper());
_menuShortCuts.Add(_menuItemMain.ShortCut.ToUpper());
}
public void AddMenuItem(MenuItem item, int position = -1)
{
if (_menuShortCuts.Add(item.ShortCut.ToUpper()) == false)
{
throw new ApplicationException($"Conflicting menu shortcut {item.ShortCut.ToUpper()}");
}
if (position == -1)
{
_menuItems.Add(item);
}
else
{
_menuItems.Insert(position, item);
}
}
public void DeleteMenuItem(int position = 0)
{
_menuItems.RemoveAt(position);
}
public void AddMenuItems(List<MenuItem> items)
{
foreach (var menuItem in items)
{
AddMenuItem(menuItem);
}
}
public string Run()
{
OutputMenu();
Console.Write("Your choice:");
var input = Console.ReadLine();
return "";
}
private void OutputMenu()
{
foreach (var t in MenuItems)
foreach (var t in _menuItems)
{
Console.WriteLine(t);
}
Console.WriteLine("-------------------");
switch (_menuLevel)
{
case EMenuLevel.Root:
Console.WriteLine(_menuItemExit);
break;
case EMenuLevel.First:
Console.WriteLine(_menuItemReturn);
Console.WriteLine(_menuItemExit);
break;
case EMenuLevel.SecondOrMore:
Console.WriteLine(_menuItemReturn);
Console.WriteLine(_menuItemMain);
Console.WriteLine(_menuItemExit);
break;
}
Console.WriteLine("=====================");
}
}
}
\ No newline at end of file
......@@ -18,8 +18,9 @@ namespace MenuSystem
ShortCut = shortCut.Trim();
Title = title.Trim();
}
public string Title { get; private set; }
public string ShortCut { get; private set; }
public string Title { get; private set; }
public override string ToString()
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment