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

menu

parent eb284dfc
......@@ -7,68 +7,69 @@ namespace ConsoleApp
{
class Program
{
private static double CalculatorCurrentDisplay = 0.0;
static void Main(string[] args)
{
Console.Clear();
Console.WriteLine("Hello Calc!");
var mainMenu = new Menu(EMenuLevel.Root);
var mainMenu = new Menu("Calculator Main", EMenuLevel.Root);
mainMenu.AddMenuItems(new List<MenuItem>()
{
new MenuItem("A", "Aaaaaaaaa"),
new MenuItem("B", "BBBBB"),
new MenuItem("C", "Cccccc"),
new MenuItem("A", "Binary operations", SubmenuBinary),
new MenuItem("S", "Unary operations", SubmenuUnary),
});
mainMenu.Run();
}
var choice = MainMenu();
Console.WriteLine("done! " + choice);
public static string MethodA()
{
Console.WriteLine("Method A!!!!!");
return "";
}
static string MainMenu()
public static string SubmenuBinary()
{
var userChoice = "";
var done = false;
do
var menu = new Menu("Binary", EMenuLevel.First);
menu.AddMenuItems(new List<MenuItem>()
{
done = false;
Console.WriteLine("Current value: 0");
Console.WriteLine("C) Classical operations");
Console.WriteLine("W) Weird stuff");
Console.WriteLine("N) Enter number");
Console.WriteLine("----------------");
Console.WriteLine("E) Exit");
Console.WriteLine("================");
Console.Write("Your choice:>");
new MenuItem("+", "+", Add),
new MenuItem("-", "-", MethodA),
new MenuItem("/", "/", MethodA),
new MenuItem("*", "*", MethodA),
userChoice = Console
.ReadLine()?
.Trim()
.ToUpper()
?? "";
switch (userChoice)
{
case "C":
done = true;
break;
case "W":
done = true;
break;
case "N":
done = true;
break;
case "E":
done = true;
break;
}
} while (!done);
});
var res = menu.Run();
return res;
}
return userChoice;
public static string Add()
{
// CalculatorCurrentDisplay
Console.WriteLine("Current value: " + CalculatorCurrentDisplay);
Console.WriteLine("plus");
Console.Write("number: ");
var n = Console.ReadLine()?.Trim();
double.TryParse(n, out var converted);
CalculatorCurrentDisplay = CalculatorCurrentDisplay + converted;
return "";
}
public static string SubmenuUnary()
{
var menu = new Menu("Unary", EMenuLevel.First);
menu.AddMenuItems(new List<MenuItem>()
{
new MenuItem("Negate", "Negate", MethodA),
new MenuItem("Sqrt", "Sqrt", MethodA),
new MenuItem("Root", "Root", MethodA),
});
var res = menu.Run();
return res;
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
namespace MenuSystem
{
......@@ -8,30 +9,50 @@ namespace MenuSystem
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 MenuItem _menuItemExit = new MenuItem("E", "Exit", null);
private readonly MenuItem _menuItemReturn = new MenuItem("R", "Return", null);
private readonly MenuItem _menuItemMain = new MenuItem("M", "Main", null);
private readonly HashSet<string> _menuShortCuts = new HashSet<string>();
private readonly HashSet<string> _menuSpecialShortCuts = new HashSet<string>();
private readonly string _title;
public Menu(EMenuLevel menuLevel)
public Menu(string title, EMenuLevel menuLevel)
{
_title = title;
_menuLevel = menuLevel;
_menuShortCuts.Add(_menuItemExit.ShortCut.ToUpper());
_menuShortCuts.Add(_menuItemReturn.ShortCut.ToUpper());
_menuShortCuts.Add(_menuItemMain.ShortCut.ToUpper());
switch (_menuLevel)
{
case EMenuLevel.Root:
_menuSpecialShortCuts.Add(_menuItemExit.ShortCut.ToUpper());
break;
case EMenuLevel.First:
_menuSpecialShortCuts.Add(_menuItemReturn.ShortCut.ToUpper());
_menuSpecialShortCuts.Add(_menuItemMain.ShortCut.ToUpper());
_menuSpecialShortCuts.Add(_menuItemExit.ShortCut.ToUpper());
break;
case EMenuLevel.SecondOrMore:
_menuSpecialShortCuts.Add(_menuItemReturn.ShortCut.ToUpper());
_menuSpecialShortCuts.Add(_menuItemMain.ShortCut.ToUpper());
_menuSpecialShortCuts.Add(_menuItemExit.ShortCut.ToUpper());
break;
}
}
public void AddMenuItem(MenuItem item, int position = -1)
{
if (_menuSpecialShortCuts.Add(item.ShortCut.ToUpper()) == false)
{
throw new ApplicationException($"Conflicting menu shortcut {item.ShortCut.ToUpper()}");
}
if (_menuShortCuts.Add(item.ShortCut.ToUpper()) == false)
{
throw new ApplicationException($"Conflicting menu shortcut {item.ShortCut.ToUpper()}");
}
if (position == -1)
{
_menuItems.Add(item);
......@@ -57,16 +78,40 @@ namespace MenuSystem
public string Run()
{
OutputMenu();
Console.Write("Your choice:");
var input = Console.ReadLine();
var runDone = false;
var input = "";
do
{
OutputMenu();
Console.Write("Your choice:");
input = Console.ReadLine()?.Trim().ToUpper();
var isInputValid = _menuShortCuts.Contains(input);
if (isInputValid)
{
var item = _menuItems.FirstOrDefault(t => t.ShortCut.ToUpper() == input);
input = item?.RunMethod==null ? input : item.RunMethod();
}
runDone = _menuSpecialShortCuts.Contains(input);
if (!runDone && !isInputValid)
{
Console.WriteLine($"Unknown shortcut '{input}'!");
}
} while (!runDone);
if (input == _menuItemReturn.ShortCut.ToUpper()) return "";
return "";
return input;
}
private void OutputMenu()
{
Console.WriteLine("====> " + _title + " <====");
Console.WriteLine("-------------------");
foreach (var t in _menuItems)
{
Console.WriteLine(t);
......@@ -89,10 +134,8 @@ namespace MenuSystem
Console.WriteLine(_menuItemExit);
break;
}
Console.WriteLine("=====================");
Console.WriteLine("=====================");
}
}
}
\ No newline at end of file
......@@ -4,7 +4,7 @@ namespace MenuSystem
{
public class MenuItem
{
public MenuItem(string shortCut, string title)
public MenuItem(string shortCut, string title, Func<string> runMethod)
{
if (string.IsNullOrEmpty(shortCut))
{
......@@ -17,11 +17,14 @@ namespace MenuSystem
ShortCut = shortCut.Trim();
Title = title.Trim();
RunMethod = runMethod;
}
public string ShortCut { get; private set; }
public string Title { get; private set; }
public Func<string> RunMethod { get; private set; }
public override string ToString()
{
return ShortCut + ") " + Title;
......
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