From 5e9a7f68604f13ef31ccdd0d07e6dd1cbcd2fa3f Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Sat, 6 May 2023 17:36:08 +0200 Subject: [PATCH] :recycle: refactor classes --- MCTG/ConsoleApp/ConsoleApp.csproj | 2 +- MCTG/ConsoleApp/ConsoleMenu.cs | 60 ++++ MCTG/ConsoleApp/IResearchMenuRecipe.cs | 13 - MCTG/ConsoleApp/Program.cs | 82 ++--- .../{ResearchMenu.cs => SearcherRecipe.cs} | 139 ++++---- MCTG/Model/IReasearch.cs | 13 - MCTG/Model/Manager.cs | 25 ++ MCTG/Model/RecipeCollection.cs | 320 +++++++++--------- 8 files changed, 341 insertions(+), 313 deletions(-) create mode 100644 MCTG/ConsoleApp/ConsoleMenu.cs delete mode 100644 MCTG/ConsoleApp/IResearchMenuRecipe.cs rename MCTG/ConsoleApp/{ResearchMenu.cs => SearcherRecipe.cs} (58%) delete mode 100644 MCTG/Model/IReasearch.cs create mode 100644 MCTG/Model/Manager.cs diff --git a/MCTG/ConsoleApp/ConsoleApp.csproj b/MCTG/ConsoleApp/ConsoleApp.csproj index c7f3ecd..7e8ef62 100644 --- a/MCTG/ConsoleApp/ConsoleApp.csproj +++ b/MCTG/ConsoleApp/ConsoleApp.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/MCTG/ConsoleApp/ConsoleMenu.cs b/MCTG/ConsoleApp/ConsoleMenu.cs new file mode 100644 index 0000000..5b561ee --- /dev/null +++ b/MCTG/ConsoleApp/ConsoleMenu.cs @@ -0,0 +1,60 @@ +using ConsoleApp; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace ConsoleApp +{ + public class ConsoleMenu : Manager + { + public ConsoleMenu(Manager manager) + { + AllRecipes = manager.AllRecipes; + } + + public Recipe? ResearchOnRecipe() + { + StringBuilder sb = new StringBuilder(); + SearcherRecipe searcherRecipe = new SearcherRecipe(); + RecipeCollection result = AllRecipes.ResearchByName(sb.ToString()); + searcherRecipe.UpdateDisplay(sb.ToString(), result); + + ConsoleKeyInfo cki; + + do + { + cki = Console.ReadKey(true); + + switch (cki.Key) + { + case ConsoleKey.UpArrow: + searcherRecipe.SelectPrevioustLine(); + break; + case ConsoleKey.DownArrow: + searcherRecipe.SelectNextLine(); + break; + case ConsoleKey.Backspace: + if (sb.Length > 0) + { + sb.Remove(sb.Length - 1, 1); + result = AllRecipes.ResearchByName(sb.ToString()); + } + break; + default: + sb.Append(cki.KeyChar); + result = AllRecipes.ResearchByName(sb.ToString()); + break; + } + + searcherRecipe.UpdateDisplay(sb.ToString(), result); + + } while (cki.Key != ConsoleKey.Enter); + + return searcherRecipe.CurrentSelected; + } + } +} diff --git a/MCTG/ConsoleApp/IResearchMenuRecipe.cs b/MCTG/ConsoleApp/IResearchMenuRecipe.cs deleted file mode 100644 index a1b3da8..0000000 --- a/MCTG/ConsoleApp/IResearchMenuRecipe.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ConsoleApp -{ - public interface IResearchMenu - { - void updateDisplay(string researchStr, T[] researchResult); - } -} diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index b746e54..909395f 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -1,57 +1,27 @@ -// See https://aka.ms/new-console-template for more information - +// See https://aka.ms/new-console-template for more information + using ConsoleApp; -using Model; -using System.Text; - -Console.WriteLine("Hello, World!\n\n"); - - -// TESTS: - -Stub stub = new Stub(); - -List recipes = stub.LoadRecipes(); -List recipeCollections = stub.LoadRecipeCollection(); - -RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals("All")); -if (allRecipe == null) - throw new ArgumentException("Load AllRecipe in stub: can't find 'All'."); - -StringBuilder sb = new StringBuilder(); -ResearchMenuRecipe researchMenu = new ResearchMenuRecipe(); -Recipe[] result = allRecipe.ResearchByName(sb.ToString()); -researchMenu.UpdateDisplay(sb.ToString(), result); - -ConsoleKeyInfo cki; - -do -{ - cki = Console.ReadKey(true); - - switch (cki.Key) - { - case ConsoleKey.UpArrow: - researchMenu.SelectPrevioustLine(); - break; - case ConsoleKey.DownArrow: - researchMenu.SelectNextLine(); - break; - case ConsoleKey.Backspace: - if (sb.Length > 0) - { - sb.Remove(sb.Length - 1, 1); - result = allRecipe.ResearchByName(sb.ToString()); - } - break; - default: - sb.Append(cki.KeyChar); - result = allRecipe.ResearchByName(sb.ToString()); - break; - } - - researchMenu.UpdateDisplay(sb.ToString(), result); - -} while (cki.Key != ConsoleKey.Enter); - -Console.WriteLine(researchMenu.ReturnSelected()); \ No newline at end of file +using Model; + +using System.Text; + +Console.WriteLine("Hello, World!\n\n"); + + +// TESTS: + +Stub stub = new Stub(); + +List recipes = stub.LoadRecipes(); +List recipeCollections = stub.LoadRecipeCollection(); + +RecipeCollection? allRecipe = recipeCollections.Find(x => x.Description.Equals("All")); +if (allRecipe == null) + throw new ArgumentException("Load AllRecipe in stub: can't find 'All'."); + +Manager manager = new Manager(allRecipe); +ConsoleMenu menu = new ConsoleMenu(manager); + +Recipe? recipeRetSearchMenu = menu.ResearchOnRecipe(); + +Console.WriteLine(recipeRetSearchMenu); diff --git a/MCTG/ConsoleApp/ResearchMenu.cs b/MCTG/ConsoleApp/SearcherRecipe.cs similarity index 58% rename from MCTG/ConsoleApp/ResearchMenu.cs rename to MCTG/ConsoleApp/SearcherRecipe.cs index f16cb02..321c14c 100644 --- a/MCTG/ConsoleApp/ResearchMenu.cs +++ b/MCTG/ConsoleApp/SearcherRecipe.cs @@ -1,71 +1,68 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Model; - -namespace ConsoleApp -{ - internal class ResearchMenuRecipe - { - internal string displayText = ""; - public int CurrentLine - { - get => currentLine; - set - { - currentLine = value; - if (currentLine > maxLines) - { - currentLine = maxLines; - } - if (currentLine < 0) - { - currentLine = 0; - } - return; - } - } - private int currentLine = 0; - private int maxLines = 0; - private Recipe? currentSelected; - - public void UpdateDisplay(string researchStr, Recipe[] researchResult) - { - maxLines = researchResult.Length - 1; - - StringBuilder sb = new StringBuilder(); - sb.AppendLine("---------------------------------------------------------"); - sb.AppendLine($" Research: {researchStr}"); - sb.AppendLine("---------------------------------------------------------"); - for (int i = 0; i < researchResult.Length; i++) - { - if (i == CurrentLine) - { - currentSelected = researchResult[i]; - sb.Append($">"); - } - sb.AppendLine($" [ {researchResult[i].Id} ]: {researchResult[i].Title} "); - } - Console.Clear(); - Console.WriteLine(sb); - } - - public int SelectNextLine() - { - return ++CurrentLine; - } - - public int SelectPrevioustLine() - { - return --CurrentLine; - } - - public Recipe? ReturnSelected() - { - return currentSelected; - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Model; + +namespace ConsoleApp +{ + internal class SearcherRecipe + { + public int CurrentLine + { + get => _currentLine; + set + { + _currentLine = value; + if (_currentLine > _maxLines) + { + _currentLine = _maxLines; + } + if (_currentLine < 0) + { + _currentLine = 0; + } + return; + } + } + + public Recipe? CurrentSelected { get; private set; } + + private int _currentLine = 0; + private int _maxLines = 0; + + + public void UpdateDisplay(string researchStr, RecipeCollection researchResult) + { + _maxLines = researchResult.Count - 1; + + StringBuilder sb = new StringBuilder(); + sb.AppendLine("---------------------------------------------------------"); + sb.AppendLine($" Research: {researchStr}"); + sb.AppendLine("---------------------------------------------------------"); + for (int i = 0; i < researchResult.Count; i++) + { + if (i == CurrentLine) + { + CurrentSelected = researchResult[i]; + sb.Append($">"); + } + sb.AppendLine($" [ {researchResult[i].Id} ]: {researchResult[i].Title} "); + } + Console.Clear(); + Console.WriteLine(sb); + } + + public int SelectNextLine() + { + return ++CurrentLine; + } + + public int SelectPrevioustLine() + { + return --CurrentLine; + } + } +} diff --git a/MCTG/Model/IReasearch.cs b/MCTG/Model/IReasearch.cs deleted file mode 100644 index 74314a1..0000000 --- a/MCTG/Model/IReasearch.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Model -{ - public interface IReasearch - { - public T ResearchByName(string str); - } -} diff --git a/MCTG/Model/Manager.cs b/MCTG/Model/Manager.cs new file mode 100644 index 0000000..0a233eb --- /dev/null +++ b/MCTG/Model/Manager.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + public partial class Manager + { + public RecipeCollection AllRecipes { get; protected set; } + + public Manager() + { + AllRecipes = new RecipeCollection(description: "All Recipes"); + } + + public Manager(RecipeCollection allRecipes) + { + AllRecipes = new RecipeCollection( + description: "All Recipes", + recipes: allRecipes.ToArray()); + } + } +} diff --git a/MCTG/Model/RecipeCollection.cs b/MCTG/Model/RecipeCollection.cs index 3da7d6d..3108a35 100644 --- a/MCTG/Model/RecipeCollection.cs +++ b/MCTG/Model/RecipeCollection.cs @@ -1,159 +1,161 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Text; - -namespace Model -{ - /// - /// Define a collection of . - ///
This class implement and . - ///
- public class RecipeCollection : IList, IEquatable - { - #region Attributes - private readonly List _recipes; - private string _description = ""; - #endregion - - #region Properties - /// - /// A short description of what this collection contain.
- /// Set to "No description." when the value passed is null, empty or contain white spaces. - ///
- public string Description - { - get => _description; - set - { - if (string.IsNullOrWhiteSpace(value)) - _description = "No description."; - else - _description = value; - } - } - - #region IList Prperties - public int Count => _recipes.Count; - public bool IsReadOnly => false; - public Recipe this[int index] { get => _recipes[index]; set => _recipes[index] = value; } - #endregion - #endregion - - #region Constructors - /// - /// Construct a new collection of _recipes. - /// - /// A short description of what this list will contain - /// Recipes to add in this new collection - public RecipeCollection(string description, params Recipe[] recipes) - { - _recipes = new List(recipes); - Description = description; - } - #endregion - - #region Methods - /// - /// Find a recipe in this list by giving the id. - /// - /// The id of the list we are looking for - /// The recipe of the id given - /// - public Recipe? GetRecipeById(int id) - { - Recipe? recipe = _recipes.Find(r => r.Id == id); - if (recipe == null) throw new ArgumentException("No _recipes match the given id."); - return recipe; - } - - #region IList Methods - public int IndexOf(Recipe item) - { - return _recipes.IndexOf(item); - } - - public void Insert(int index, Recipe item) - { - _recipes.Insert(index, item); - } - - public void RemoveAt(int index) - { - _recipes.RemoveAt(index); - } - - public void Add(Recipe item) - { - _recipes.Add(item); - } - - public void Clear() - { - _recipes.Clear(); - } - - public bool Contains(Recipe item) - { - return _recipes.Contains(item); - } - - public void CopyTo(Recipe[] array, int arrayIndex) - { - _recipes.CopyTo(array, arrayIndex); - } - - public bool Remove(Recipe item) - { - return _recipes.Remove(item); - } - - public IEnumerator GetEnumerator() - { - return _recipes.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return _recipes.GetEnumerator(); - } - #endregion - - public virtual bool Equals(RecipeCollection? other) - { - - if (other == null) return false; - if (other == this) return true; - return _description.Equals(other.Description) && _recipes.Equals(other._recipes); - } - - public override bool Equals(object? obj) - { - var item = obj as RecipeCollection; - if (item == null) return false; - return Equals(obj); - } - - public override int GetHashCode() - { - return _recipes.GetHashCode(); - } - - public override string ToString() - { - StringBuilder sb = new StringBuilder($"[RecipeCollection] - {Description}:\n"); - foreach (Recipe r in _recipes) - { - sb.AppendFormat("\t - {0}\n", r.ToString()); - } - return sb.ToString(); - } - - public Recipe[] ResearchByName(string str) - { - return _recipes.FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray(); - } - #endregion - } -} +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text; + +namespace Model +{ + /// + /// Define a collection of . + ///
This class implement and . + ///
+ public class RecipeCollection : IList, IEquatable + { + #region Attributes + private readonly List _recipes; + private string _description = ""; + #endregion + + #region Properties + /// + /// A short description of what this collection contain.
+ /// Set to "No description." when the value passed is null, empty or contain white spaces. + ///
+ public string Description + { + get => _description; + set + { + if (string.IsNullOrWhiteSpace(value)) + _description = "No description."; + else + _description = value; + } + } + + #region IList Prperties + public int Count => _recipes.Count; + public bool IsReadOnly => false; + public Recipe this[int index] { get => _recipes[index]; set => _recipes[index] = value; } + #endregion + #endregion + + #region Constructors + /// + /// Construct a new collection of _recipes. + /// + /// A short description of what this list will contain + /// Recipes to add in this new collection + public RecipeCollection(string description, params Recipe[] recipes) + { + _recipes = new List(recipes); + Description = description; + } + #endregion + + #region Methods + /// + /// Find a recipe in this list by giving the id. + /// + /// The id of the list we are looking for + /// The recipe of the id given + /// + public Recipe? GetRecipeById(int id) + { + Recipe? recipe = _recipes.Find(r => r.Id == id); + if (recipe == null) throw new ArgumentException("No _recipes match the given id."); + return recipe; + } + + public RecipeCollection ResearchByName(string str) + { + return new RecipeCollection( + description: $"Results of the research: {str}", + recipes: _recipes.FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray()); + } + + #region IList Methods + public int IndexOf(Recipe item) + { + return _recipes.IndexOf(item); + } + + public void Insert(int index, Recipe item) + { + _recipes.Insert(index, item); + } + + public void RemoveAt(int index) + { + _recipes.RemoveAt(index); + } + + public void Add(Recipe item) + { + _recipes.Add(item); + } + + public void Clear() + { + _recipes.Clear(); + } + + public bool Contains(Recipe item) + { + return _recipes.Contains(item); + } + + public void CopyTo(Recipe[] array, int arrayIndex) + { + _recipes.CopyTo(array, arrayIndex); + } + + public bool Remove(Recipe item) + { + return _recipes.Remove(item); + } + + public IEnumerator GetEnumerator() + { + return _recipes.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _recipes.GetEnumerator(); + } + #endregion + + public virtual bool Equals(RecipeCollection? other) + { + + if (other == null) return false; + if (other == this) return true; + return _description.Equals(other.Description) && _recipes.Equals(other._recipes); + } + + public override bool Equals(object? obj) + { + var item = obj as RecipeCollection; + if (item == null) return false; + return Equals(obj); + } + + public override int GetHashCode() + { + return _recipes.GetHashCode(); + } + + public override string ToString() + { + StringBuilder sb = new StringBuilder($"[RecipeCollection] - {Description}:\n"); + foreach (Recipe r in _recipes) + { + sb.AppendFormat("\t - {0}\n", r.ToString()); + } + return sb.ToString(); + } + #endregion + } +}