Skip to content

Instantly share code, notes, and snippets.

@pbrewczynski
Created December 2, 2013 08:02
Show Gist options
  • Save pbrewczynski/7746449 to your computer and use it in GitHub Desktop.
Save pbrewczynski/7746449 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ERConsole.Menu
{
class MenuCollectionController
{
MenuCollection root;
public MenuCollectionController (MenuCollection root) {
this.root = root;
}
public void generate () {
int count = root.getCount() + 1;
for (int i = 1; i < count; i++)
{
Console.WriteLine(String.Format("{0}. {1}",i,this.root.getOptionDescription(i-1)));
}
}
private void wrongInput () {
Console.WriteLine("Something wrong with your input, try again!");
this.interact();
}
private void newMenu(MenuCollection root)
{
this.root = root;
if (this.root == null)
{
return;
}
else
{
this.generate();
this.interact();
}
}
private void selectItem(int i)
{
Console.WriteLine("Selected executed");
MenuItem selectedItem = this.root.getMenuItem(i);
if (selectedItem.subMenu == null) // How can I overcome this by encapsulation ?
{
selectedItem.action();
this.interact();
}
else
{
this.newMenu(selectedItem.subMenu);
}
}
public void interact()
{
Console.Write("--->");
string input = Console.ReadLine();
Console.WriteLine("After reading");
if (input == "q")
{
Console.WriteLine("It is q");
this.newMenu(this.root.parent);
return;
}
int itemNumber = 0;
try
{
itemNumber = int.Parse(input);
}
catch
{
this.wrongInput();
return;
}
itemNumber -= 1; // because interface
if (itemNumber >= this.root.getCount() || itemNumber < 0)
{
this.wrongInput(); // MAKE INTERACTION WITH USER... SO IT WILL STILL BE IN THE CALL STACK
return;
}
Console.WriteLine("Before select " + itemNumber);
this.selectItem(itemNumber);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment