From 1a2ecceb258e241bd2d17e1f66ebfb9e3cca0a36 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Sun, 7 May 2023 02:10:15 +0200 Subject: [PATCH] change nested class 'ConsoleMenu' to namespace 'Menu' --- MCTG/ConsoleApp/Menu/ConsoleMenu.cs | 62 -------- MCTG/ConsoleApp/Menu/IMenuDisplay.cs | 2 +- MCTG/ConsoleApp/Menu/SearcherRecipe.cs | 187 ++++++++++++------------- MCTG/ConsoleApp/Menu/SelectMenu.cs | 52 +++++++ MCTG/ConsoleApp/Program.cs | 11 +- 5 files changed, 151 insertions(+), 163 deletions(-) delete mode 100644 MCTG/ConsoleApp/Menu/ConsoleMenu.cs create mode 100644 MCTG/ConsoleApp/Menu/SelectMenu.cs diff --git a/MCTG/ConsoleApp/Menu/ConsoleMenu.cs b/MCTG/ConsoleApp/Menu/ConsoleMenu.cs deleted file mode 100644 index 5e91b27..0000000 --- a/MCTG/ConsoleApp/Menu/ConsoleMenu.cs +++ /dev/null @@ -1,62 +0,0 @@ -using ConsoleApp; -using Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - - -namespace ConsoleApp -{ - /// - /// Define multiple type of menu - ///
See: - ///
- - ///
- - ///
- public partial class ConsoleMenu : Manager - { - /// - /// 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/IMenuDisplay.cs b/MCTG/ConsoleApp/Menu/IMenuDisplay.cs index 44341aa..572082c 100644 --- a/MCTG/ConsoleApp/Menu/IMenuDisplay.cs +++ b/MCTG/ConsoleApp/Menu/IMenuDisplay.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ConsoleApp +namespace ConsoleApp.Menu { internal interface IMenuDisplay { diff --git a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs index 97f5162..4e17512 100644 --- a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs +++ b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs @@ -7,122 +7,119 @@ using System.Threading.Tasks; using Model; -namespace ConsoleApp +namespace ConsoleApp.Menu { - public partial class ConsoleMenu : Manager + /// + /// An utility to find a recipe. + /// + public class SearcherRecipe : SelectMenu { + #region Attribute + private RecipeCollection _recipeOnSearch; + #endregion + + #region Properties /// - /// An utility to find a recipe. + /// A collection of recipe where the title contain the search string /// - public class SearcherRecipe : SelectMenu - { - #region Attribute - private RecipeCollection _recipeOnSearch; - #endregion + public RecipeCollection SearchResult { get; private set; } - #region Properties - /// - /// A collection of recipe where the title contain the search string - /// - public RecipeCollection SearchResult { get; private set; } - - /// - /// The search string - /// - public string ResearchStr { get; set; } - #endregion + /// + /// The search string + /// + public string ResearchStr { get; set; } + #endregion - /// - /// Constructor of the SearcherRecipe utility. - /// - /// The collection of recipe where to search - public SearcherRecipe(RecipeCollection recipeOnSearch) - { - _recipeOnSearch = recipeOnSearch; - SearchResult = _recipeOnSearch; - ResearchStr = ""; - } + /// + /// Constructor of the SearcherRecipe utility. + /// + /// The collection of recipe where to search + public SearcherRecipe(RecipeCollection recipeOnSearch) + { + _recipeOnSearch = recipeOnSearch; + SearchResult = _recipeOnSearch; + ResearchStr = ""; + } - #region Methodes - /// - /// Launch a search by name request in the collection of Recipe with with a string. - /// - /// The string for search - /// True if the result of the search gave at least 1 element. False otherwise. - public bool ComputeSearch(string researchStr = "") - { - ResearchStr = researchStr; - SearchResult = _recipeOnSearch.ResearchByName(ResearchStr.ToLower()); - _maxLines = SearchResult.Count - 1; + #region Methodes + /// + /// Launch a search by name request in the collection of Recipe with with a string. + /// + /// The string for search + /// True if the result of the search gave at least 1 element. False otherwise. + public bool ComputeSearch(string researchStr = "") + { + ResearchStr = researchStr; + SearchResult = _recipeOnSearch.ResearchByName(ResearchStr.ToLower()); + _maxLines = SearchResult.Count - 1; - return SearchResult.Count > 0; - } + return SearchResult.Count > 0; + } - public override void UpdateDisplay() + public override void UpdateDisplay() + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine("---------------------------------------------------------"); + sb.AppendLine($" Research: {ResearchStr}"); + sb.AppendLine("---------------------------------------------------------"); + for (int i = 0; i < SearchResult.Count; i++) { - StringBuilder sb = new StringBuilder(); - sb.AppendLine("---------------------------------------------------------"); - sb.AppendLine($" Research: {ResearchStr}"); - sb.AppendLine("---------------------------------------------------------"); - for (int i = 0; i < SearchResult.Count; i++) + if (i == CurrentLine) { - if (i == CurrentLine) - { - CurrentSelected = SearchResult[i]; - sb.Append($">"); - } - sb.AppendLine($" [ {SearchResult[i].Id} ]:\t{SearchResult[i].Title} "); + CurrentSelected = SearchResult[i]; + sb.Append($">"); } - Console.Clear(); - Console.WriteLine(sb); + sb.AppendLine($" [ {SearchResult[i].Id} ]:\t{SearchResult[i].Title} "); } + Console.Clear(); + Console.WriteLine(sb); + } - /// - /// Launch and pilot the search menu. - /// - /// The collection of recipe where to search - /// The recipe selected - public static Recipe? ResearchOn(RecipeCollection recipeOnSearch) - { - SearcherRecipe sr = new SearcherRecipe(recipeOnSearch); - StringBuilder sb = new StringBuilder(); - sr.ComputeSearch(); - sr.UpdateDisplay(); + /// + /// Launch and pilot the search menu. + /// + /// The collection of recipe where to search + /// The recipe selected + public static Recipe? ResearchOn(RecipeCollection recipeOnSearch) + { + SearcherRecipe sr = new SearcherRecipe(recipeOnSearch); + StringBuilder sb = new StringBuilder(); + sr.ComputeSearch(); + sr.UpdateDisplay(); - ConsoleKeyInfo cki; + ConsoleKeyInfo cki; - do - { - cki = Console.ReadKey(true); + do + { + cki = Console.ReadKey(true); - switch (cki.Key) - { - case ConsoleKey.UpArrow: - sr.SelectPrevioustLine(); - break; - case ConsoleKey.DownArrow: - sr.SelectNextLine(); - break; - case ConsoleKey.Backspace: - if (sb.Length > 0) - { - sb.Remove(sb.Length - 1, 1); - sr.ComputeSearch(sb.ToString()); - } - break; - default: - sb.Append(cki.KeyChar); + switch (cki.Key) + { + case ConsoleKey.UpArrow: + sr.SelectPrevioustLine(); + break; + case ConsoleKey.DownArrow: + sr.SelectNextLine(); + break; + case ConsoleKey.Backspace: + if (sb.Length > 0) + { + sb.Remove(sb.Length - 1, 1); sr.ComputeSearch(sb.ToString()); - break; - } + } + break; + default: + sb.Append(cki.KeyChar); + sr.ComputeSearch(sb.ToString()); + break; + } - sr.UpdateDisplay(); + sr.UpdateDisplay(); - } while (cki.Key != ConsoleKey.Enter); + } while (cki.Key != ConsoleKey.Enter); - return sr.CurrentSelected; - } - #endregion + return sr.CurrentSelected; } + #endregion } } diff --git a/MCTG/ConsoleApp/Menu/SelectMenu.cs b/MCTG/ConsoleApp/Menu/SelectMenu.cs new file mode 100644 index 0000000..0e331c6 --- /dev/null +++ b/MCTG/ConsoleApp/Menu/SelectMenu.cs @@ -0,0 +1,52 @@ +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/Program.cs b/MCTG/ConsoleApp/Program.cs index f3bccbd..ac9a1e9 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -1,6 +1,7 @@ // See https://aka.ms/new-console-template for more information using ConsoleApp; +using ConsoleApp.Menu; using Model; using System.Text; @@ -21,8 +22,8 @@ if (allRecipe == null) Manager manager = new Manager(allRecipe); -Console.WriteLine(ConsoleMenu.SearcherRecipe.ResearchOn(manager.AllRecipes)); - - -// press any key to quit -Console.ReadKey(); +Recipe? ret = SearcherRecipe.ResearchOn(allRecipe); +Console.WriteLine(ret); + +// press any key to quit +Console.ReadKey();