From 2df795a3472b7e4e3711695bb1f8e77a057793b3 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Sat, 13 May 2023 11:55:28 +0200 Subject: [PATCH] select recipe on screen work --- MCTG/ConsoleApp/Menu/Menu.cs | 28 ++++++++++----- MCTG/ConsoleApp/Menu/SearcherRecipe.cs | 48 +++++++++++--------------- MCTG/ConsoleApp/MenuManager.cs | 2 +- MCTG/ConsoleApp/Program.cs | 11 +++--- MCTG/Model/Recipes/RecipeCollection.cs | 2 +- 5 files changed, 49 insertions(+), 42 deletions(-) diff --git a/MCTG/ConsoleApp/Menu/Menu.cs b/MCTG/ConsoleApp/Menu/Menu.cs index b18da5a..594cbb4 100644 --- a/MCTG/ConsoleApp/Menu/Menu.cs +++ b/MCTG/ConsoleApp/Menu/Menu.cs @@ -12,15 +12,26 @@ namespace ConsoleApp.Menu where T : notnull { protected StringBuilder _screenDisplay; - protected readonly List>? _selectList; + protected List> _selectList = new List>(); public string Title { get; private set; } public StringBuilder InputStr { get; set; } - public int CurrentLine { get; private set; } - public T? CurrentSelected { get; private set; } + + private int _currentLine; + public int CurrentLine { + get => _currentLine; + private set + { + _currentLine = value; + if (_currentLine < 0) _currentLine = 0; + if (_currentLine >= _selectList.Count) _currentLine = _selectList.Count-1; + } + } + + public T? CurrentSelected { get; protected set; } public bool WriteMode { get; set; } - private Menu(string title) + protected Menu(string title) { Title = title; CurrentLine = 0; @@ -29,7 +40,7 @@ namespace ConsoleApp.Menu InputStr = new StringBuilder(); } - public Menu(string title, params Selector[] selections ) : this(title) + protected Menu(string title, params Selector[] selections ) : this(title) { if (selections == null || selections.Length == 0) throw new ArgumentException("Error: a menu must contain at least 1 selection"); @@ -40,6 +51,7 @@ namespace ConsoleApp.Menu public virtual void Display() { + Console.Clear(); Console.WriteLine(_screenDisplay); } @@ -48,14 +60,14 @@ namespace ConsoleApp.Menu _screenDisplay.AppendLine($"[ {Title} ]"); _screenDisplay.AppendLine("-------------------------------------------\n"); - foreach (Selector selector in _selectList) + for (int i = 0; i < _selectList.Count; i++) { - if (selector.Equals(CurrentSelected)) + if (CurrentLine == i) _screenDisplay.Append($"> "); else _screenDisplay.Append($" "); - _screenDisplay.AppendLine($"{selector.Line}"); + _screenDisplay.AppendLine($"{_selectList[i].Line}"); } } diff --git a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs index 5386f83..334754a 100644 --- a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs +++ b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs @@ -1,32 +1,26 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography.X509Certificates; -using System.Text; -using System.Threading.Tasks; - -using Model; - -namespace ConsoleApp.Menu -{ - /// - /// An utility to find a recipe. - /// - internal class SearcherRecipe : Menu - { - public SearcherRecipe() : base("Search recipe") - { - - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; - public static SearcherRecipe SearchInRecipeCollection(string title, RecipeCollection recipeCollection) +using Model; + +namespace ConsoleApp.Menu +{ + /// + /// An utility to find a recipe. + /// + internal class SearcherRecipe : Menu + { + public SearcherRecipe(RecipeCollection recipeCollection) : base("Search recipe") { - Dictionary recipesSelectors = new Dictionary(); foreach (Recipe recipe in recipeCollection) { - recipesSelectors.Add(recipe, recipe.Title); + _selectList.Add(new Selector(recipe, $"[{recipe.Id}]: {recipe.Title}")); } - return new SearcherRecipe(title, CreateSelection(recipesSelectors)); + CurrentSelected = _selectList[0].Item; } #region Methodes @@ -34,6 +28,6 @@ namespace ConsoleApp.Menu { Console.WriteLine(CurrentSelected); } - #endregion - } -} + #endregion + } +} diff --git a/MCTG/ConsoleApp/MenuManager.cs b/MCTG/ConsoleApp/MenuManager.cs index 4bdfa76..790d261 100644 --- a/MCTG/ConsoleApp/MenuManager.cs +++ b/MCTG/ConsoleApp/MenuManager.cs @@ -17,7 +17,7 @@ namespace ConsoleApp DataManager = dataManager; MenuCallStack = new Stack(); - MenuCallStack.Append(firstMenu); + MenuCallStack.Push(firstMenu); } public void Loop() diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index f32038b..1863be2 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -20,9 +20,10 @@ RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals(" if (allRecipe == null) throw new ArgumentException("Load AllRecipe in stub: can't find 'All'."); -DataManager manager = new DataManager(allRecipe); +DataManager dataMgr = new DataManager(allRecipe); -MenuManager menuManager = new MenuManager(manager, ConsoleApp.Menu.SearcherRecipe.SearchInRecipeCollection("search recipe", manager.AllRecipes)); - -// press any key to quit -Console.ReadKey(); +MenuManager menuMgr = new MenuManager(dataMgr, new SearcherRecipe(dataMgr.AllRecipes)); +menuMgr.Loop(); + +// press any key to quit +Console.ReadKey(); diff --git a/MCTG/Model/Recipes/RecipeCollection.cs b/MCTG/Model/Recipes/RecipeCollection.cs index 42d4971..a532edb 100644 --- a/MCTG/Model/Recipes/RecipeCollection.cs +++ b/MCTG/Model/Recipes/RecipeCollection.cs @@ -77,7 +77,7 @@ namespace Model { if (other == null) return false; if (other == this) return true; - return _description.Equals(other.Description) && this.Equals(other.this); + return this.Description.Equals(other.Description); } public override bool Equals(object? obj)