From 352e38421f754345afb4bfea7b8150f7c0f601b2 Mon Sep 17 00:00:00 2001 From: Alexandre AGOSTINHO Date: Thu, 11 May 2023 15:15:09 +0200 Subject: [PATCH] partial commit (bf change of RecipeCollection) --- MCTG/ConsoleApp/Menu/IMenuDisplay.cs | 28 -------------- MCTG/ConsoleApp/Menu/Menu.cs | 41 +++++++++++++++----- MCTG/ConsoleApp/Menu/SearcherRecipe.cs | 22 ++++++++++- MCTG/ConsoleApp/Menu/SelectMenu.cs | 52 -------------------------- MCTG/ConsoleApp/Menu/Selector.cs | 26 ++++++++++++- MCTG/ConsoleApp/Program.cs | 3 +- 6 files changed, 78 insertions(+), 94 deletions(-) delete mode 100644 MCTG/ConsoleApp/Menu/IMenuDisplay.cs delete mode 100644 MCTG/ConsoleApp/Menu/SelectMenu.cs diff --git a/MCTG/ConsoleApp/Menu/IMenuDisplay.cs b/MCTG/ConsoleApp/Menu/IMenuDisplay.cs deleted file mode 100644 index 572082c..0000000 --- a/MCTG/ConsoleApp/Menu/IMenuDisplay.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ConsoleApp.Menu -{ - internal interface IMenuDisplay - { - /// - /// Update the menu display in the Console. - /// - void UpdateDisplay(); - - /// - /// Select the next line in the menu - /// - /// The current number of the new selected line - int SelectNextLine(); - - /// - /// Select the previous line in the menu - /// - /// The current number of the new selected line - int SelectPrevioustLine(); - } -} diff --git a/MCTG/ConsoleApp/Menu/Menu.cs b/MCTG/ConsoleApp/Menu/Menu.cs index c7ede43..fb6a09e 100644 --- a/MCTG/ConsoleApp/Menu/Menu.cs +++ b/MCTG/ConsoleApp/Menu/Menu.cs @@ -9,30 +9,53 @@ using System.Threading.Tasks; namespace ConsoleApp.Menu { internal abstract class Menu : IMenu + where T : notnull { protected StringBuilder _screenDisplay; - protected readonly List> _selectList; + protected readonly List>? _selectList; public string Title { get; private set; } public StringBuilder InputStr { get; set; } public int CurrentLine { get; private set; } - public T CurrentSelected { get; private set; } + public T? CurrentSelected { get; private set; } public bool WriteMode { get; set; } - public Menu(string title, params Selector[] selections ) + private Menu(string title) { - if (selections == null || selections.Length == 0) - throw new ArgumentException("Error: a menu must contain at least 1 selection"); - Title = title; CurrentLine = 0; - CurrentSelected = selections[0].Item; WriteMode = false; - _selectList = new List>(selections); _screenDisplay = new StringBuilder(); InputStr = new StringBuilder(); } + public 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"); + + _selectList = selections.ToList(); + CurrentSelected = _selectList[0].Item; + } + + public Menu(string title, Dictionary lines) : this(title, CreateSelection(lines)) + { } + + public static Selector[] CreateSelection(Dictionary lines) + { + if (lines is null) + { + throw new ArgumentNullException(nameof(lines)); + } + + Selector[] selections = new Selector[lines.Count]; + foreach (KeyValuePair line in lines) + { + selections.Append(new Selector(line.Key, line.Value)); + } + return selections; + } + public virtual void Display() { Console.WriteLine(_screenDisplay); @@ -61,7 +84,7 @@ namespace ConsoleApp.Menu public void SelectPrevious() { - CurrentSelected = _selectList[--CurrentLine].Item + CurrentSelected = _selectList[--CurrentLine].Item; } public void EnableWriteMode() diff --git a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs index a213e7d..b437757 100644 --- a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs +++ b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs @@ -14,10 +14,30 @@ namespace ConsoleApp.Menu /// internal class SearcherRecipe : Menu { - public SearcherRecipe(string title, params Selector[] selections) + private SearcherRecipe(string title, params Selector[] selections) : base(title, selections) { } + private SearcherRecipe(string title, Dictionary lines) + : base(title, lines) + { } + + public SearcherRecipe(RecipeCollection recipeCollection) + : this("Recipe search", recipeCollection.ToDictionary(recipe => recipe.GetHashCode())) + { + + } + + public static SearcherRecipe SearchInRecipeCollection(string title, RecipeCollection recipeCollection) + { + Dictionary recipesSelectors = new Dictionary(); + foreach (Recipe recipe in recipeCollection) + { + recipesSelectors.Add(recipe, recipe.Title); + } + return new SearcherRecipe(title, CreateSelection(recipesSelectors)); + } + #region Methodes public override void Return() { diff --git a/MCTG/ConsoleApp/Menu/SelectMenu.cs b/MCTG/ConsoleApp/Menu/SelectMenu.cs deleted file mode 100644 index 0e331c6..0000000 --- a/MCTG/ConsoleApp/Menu/SelectMenu.cs +++ /dev/null @@ -1,52 +0,0 @@ -using ConsoleApp; -using Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - - -namespace ConsoleApp.Menu -{ - /// - /// An abstract class that define the components of a selection-type menu. - /// - /// The return type of the currently selected item - public abstract class SelectMenu : IMenuDisplay - { - protected int _currentLine = 0; - protected int _maxLines = 0; - - /// - /// The currently selected item. - /// - public T? CurrentSelected { get; protected set; } - - /// - /// The current line selected in the menu. - /// - public int CurrentLine - { - get => _currentLine; - protected set - { - _currentLine = value; - if (_currentLine > _maxLines) - { - _currentLine = _maxLines; - } - if (_currentLine < 0) - { - _currentLine = 0; - } - return; - } - } - - public int SelectNextLine() { return ++CurrentLine; } - public int SelectPrevioustLine() { return --CurrentLine; } - - public abstract void UpdateDisplay(); - } -} diff --git a/MCTG/ConsoleApp/Menu/Selector.cs b/MCTG/ConsoleApp/Menu/Selector.cs index 9a398f4..7278db8 100644 --- a/MCTG/ConsoleApp/Menu/Selector.cs +++ b/MCTG/ConsoleApp/Menu/Selector.cs @@ -1,5 +1,7 @@ -using System; +using Model; +using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection.Emit; using System.Text; @@ -7,7 +9,8 @@ using System.Threading.Tasks; namespace ConsoleApp.Menu { - internal class Selector + internal class Selector : IEquatable> + where T : notnull { private string _line = ""; @@ -28,5 +31,24 @@ namespace ConsoleApp.Menu Line = line; Item = item; } + + public virtual bool Equals(Selector? other) + { + if (other == null) return false; + if (other == this) return true; + return other.Line.Equals(this.Line); + } + + public override bool Equals(object? obj) + { + var item = obj as Selector; + if (item == null) return false; + return Equals(obj); + } + + public override int GetHashCode() + { + return Line.GetHashCode(); + } } } diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index ebb65b1..f32038b 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -22,8 +22,7 @@ if (allRecipe == null) DataManager manager = new DataManager(allRecipe); -Recipe? ret = SearcherRecipe.ResearchOn(allRecipe); -Console.WriteLine(ret); +MenuManager menuManager = new MenuManager(manager, ConsoleApp.Menu.SearcherRecipe.SearchInRecipeCollection("search recipe", manager.AllRecipes)); // press any key to quit Console.ReadKey();