Skip to content

Instantly share code, notes, and snippets.

@ArseniySavin
Last active August 22, 2018 16:10
Show Gist options
  • Save ArseniySavin/cf02f1bf3fdef5721bcae3d33347fc31 to your computer and use it in GitHub Desktop.
Save ArseniySavin/cf02f1bf3fdef5721bcae3d33347fc31 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
RootMenu rootMenu = new RootMenu();
CategoryMenu category1 = new CategoryMenu { CategoryId = 1, Name = "telecom", Alias = "telecom-internet" };
category1.Add(new Service { ServiceId = 1, Alias = "mobile-altel", Name = "Altel", RegionName = "ALM" });
category1.Add(new Service { ServiceId = 2, Alias = "mobile-beeline", Name = "Beeline", RegionName = "AST" });
CategoryMenu category2 = new CategoryMenu { CategoryId = 3, Name = "internet", Alias = "internet" };
category2.Add(new Service { ServiceId = 1, Alias = "internet-altel", Name = "Altel-mobile", RegionName = "ALM" });
category2.Add(new Service { ServiceId = 2, Alias = "internet-beeline", Name = "Beeline-home", RegionName = "AST" });
category1.Add(category2);
rootMenu.Add(category1);
CategoryMenu category3 = new CategoryMenu { CategoryId = 2, Name = "comunalka", Alias = "comunalka" };
category3.Add(new Service { ServiceId = 3, Alias = "comunalka-ast-erc", Name = "Astana ERC", RegionName = "AST" });
category3.Add(new Service { ServiceId = 4, Alias = "comunalka-alseco", Name = "Alseco", RegionName = "ALM" });
rootMenu.Add(category3);
CategoryMenu category4 = new CategoryMenu { CategoryId = 4, Name = "other", Alias = "other" };
category4.Add(new CategoryMenu { CategoryId = 5, Name = "buty", Alias = "buty" });
category4.Add(new CategoryMenu { CategoryId = 6, Name = "air", Alias = "air" });
category4.Add(new CategoryMenu { CategoryId = 7, Name = "test", Alias = "test" });
category4.Add(new Service { ServiceId = 3, Alias = "hosting", Name = "hosting PS", RegionName = "AST" });
category4.Add(new Service { ServiceId = 4, Alias = "detsad", Name = "detsad", RegionName = "ALM" });
rootMenu.Add(category4);
var c1 = rootMenu.Chain<CategoryMenu>("telecom-internet");
var c2 = rootMenu.Chain<CategoryMenu>("internet");
var c4 = rootMenu.Chain<CategoryMenu>("comunalka");
var c3 = c4.Chain<Service>("comunalka-alseco");
var c5 = rootMenu.Chain<CategoryMenu>("comunalka").Chain<Service>("comunalka-ast-erc");
var c6 = rootMenu.Chain<CategoryMenu>("other").Chain<Service>("hosting").Chain<CategoryMenu>("buty");
// Test get node
var c = rootMenu.MenuNodes<CategoryMenu>();
var s = rootMenu.MenuNodes<Service>();
// Test composit
var t1 = rootMenu.MenuNode("telecom-internet");
var t2 = rootMenu.MenuNode("internet-beeline");
var t3 = rootMenu.MenuNode("comunalka-alseco");
var t4 = rootMenu.MenuNode("comunalka");
foreach (CategoryMenu item in rootMenu.Components)
{
Console.WriteLine("> " + item.Alias);
}
while (true)
{
string input = Console.ReadLine();
if (input == "end")
break;
var menu = rootMenu.MenuNode(input);
Console.WriteLine("-> " + menu.Alias);
foreach (var m in menu.Components)
{
Console.WriteLine("--> " + m.Alias);
}
}
Console.WriteLine("Test end. Press any key");
Console.ReadKey();
}
}
abstract class ComponentMenuBase
{
public abstract void Add(ComponentMenuBase c);
public abstract List<T> MenuNodes<T>();
public abstract bool IsComposit { get; }
public abstract List<ComponentMenuBase> Components { get; }
public string Alias { get; set; }
public abstract ComponentMenuBase Chain<T>(string id);
public virtual ComponentMenuBase MenuNode(string alias)
{
foreach (ComponentMenuBase component in Components)
{
var node = component.MenuNode(alias);
if (node.Alias == alias)
return node;
}
return this;
}
}
class RootMenu : ComponentMenuBase
{
List<ComponentMenuBase> componentMenu = new List<ComponentMenuBase>();
public override bool IsComposit { get { return true; } }
public override List<ComponentMenuBase> Components { get { return componentMenu; } }
public override void Add(ComponentMenuBase c)
{
componentMenu.Add(c);
}
public override List<T> MenuNodes<T>()
{
return componentMenu.FindAll(m => m is T).Cast<T>().ToList();
}
public override ComponentMenuBase Chain<T>(string id)
{
return componentMenu.Find(m => m.Alias == id) ?? componentMenu.Find(m => m.Chain<T>(id).Alias == id) ?? null;
}
}
class CategoryMenu : ComponentMenuBase
{
List<ComponentMenuBase> componentMenu = new List<ComponentMenuBase>();
public override bool IsComposit { get { return true; } }
public override List<ComponentMenuBase> Components { get { return componentMenu; } }
public override void Add(ComponentMenuBase c)
{
componentMenu.Add(c);
}
public override List<T> MenuNodes<T>()
{
return componentMenu.FindAll(m => m.MenuNodes<T>() is T).Cast<T>().ToList();
}
public override ComponentMenuBase Chain<T>(string id)
{
return componentMenu.Find(m => m.Alias == id) ?? componentMenu.Find(m => m.Chain<T>(id).Alias == id) ?? null;
}
public int CategoryId { get; set; }
public string Name { get; set; }
public char Type { get; set; }
}
class Service : ComponentMenuBase
{
List<ComponentMenuBase> componentMenu = new List<ComponentMenuBase>();
public override bool IsComposit { get { return false; } }
public override List<ComponentMenuBase> Components { get { return componentMenu; } }
public override void Add(ComponentMenuBase c)
{
componentMenu.Add(c);
}
public override List<T> MenuNodes<T>()
{
return componentMenu.FindAll(m => m is T).Cast<T>().ToList();
}
public override ComponentMenuBase Chain<T>(string id)
{
return componentMenu.Find(m => m.Alias == id);
}
public int ServiceId { get; set; }
public string Name { get; set; }
public string RegionName { get; set; }
public char Type { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment