diff --git a/MCTG/ConsoleApp/IResearchMenuRecipe.cs b/MCTG/ConsoleApp/IResearchMenuRecipe.cs new file mode 100644 index 0000000..a1b3da8 --- /dev/null +++ b/MCTG/ConsoleApp/IResearchMenuRecipe.cs @@ -0,0 +1,13 @@ +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 773f61b..b746e54 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -2,7 +2,7 @@ using ConsoleApp; using Model; - +using System.Text; Console.WriteLine("Hello, World!\n\n"); @@ -14,8 +14,44 @@ Stub stub = new Stub(); List recipes = stub.LoadRecipes(); List recipeCollections = stub.LoadRecipeCollection(); -foreach (Recipe r in recipes) - Console.WriteLine(r); - -foreach (RecipeCollection r in recipeCollections) - Console.WriteLine(r); +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 diff --git a/MCTG/ConsoleApp/ResearchMenu.cs b/MCTG/ConsoleApp/ResearchMenu.cs new file mode 100644 index 0000000..f16cb02 --- /dev/null +++ b/MCTG/ConsoleApp/ResearchMenu.cs @@ -0,0 +1,71 @@ +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; + } + } +} diff --git a/MCTG/Model/IReasearch.cs b/MCTG/Model/IReasearch.cs new file mode 100644 index 0000000..74314a1 --- /dev/null +++ b/MCTG/Model/IReasearch.cs @@ -0,0 +1,13 @@ +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/Recipe.cs b/MCTG/Model/Recipe.cs index 9d65bf9..341383c 100644 --- a/MCTG/Model/Recipe.cs +++ b/MCTG/Model/Recipe.cs @@ -1,101 +1,101 @@ -using System; -using System.Collections.Generic; -using System.Runtime.CompilerServices; +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Security.Cryptography; -using System.Text; - -namespace Model -{ - /// - /// Define a Recipe for the preparation of a meal. - /// - public class Recipe : IEquatable - { - #region Attributes - private string _title = ""; - #endregion +using System.Text; - #region Properties - /// - /// The ID of the recipe - allows you to compare and/or get this item in an easier way. - /// - public int Id { get; init; } - - /// - /// The Title of the recipe.
- /// Set to "No title." when the value passed is null, empty or contain white spaces. - ///
- public string Title - { - get => _title; - set - { - if (string.IsNullOrWhiteSpace(value)) - _title = "No title."; - else - _title = value; - } - } - - /// - /// The steps of the preparation. See: . - /// - public List PreparationSteps { get; set; } - #endregion - - #region Constructors - /// - /// Construct a new recipe. - /// - /// The title of the recipe - /// The id of the recipe. If not given, get a new id. - /// The steps of the preparation of the meal - public Recipe(string title = "", int? id = null, - params PreparationStep[] preparationSteps) - { - Title = title; - PreparationSteps = new List(preparationSteps); +namespace Model +{ + /// + /// Define a Recipe for the preparation of a meal. + /// + public class Recipe : IEquatable + { + #region Attributes + private string _title = ""; + #endregion + + #region Properties + /// + /// The ID of the recipe - allows you to compare and/or get this item in an easier way. + /// + public int Id { get; init; } + + /// + /// The Title of the recipe.
+ /// Set to "No title." when the value passed is null, empty or contain white spaces. + ///
+ public string Title + { + get => _title; + set + { + if (string.IsNullOrWhiteSpace(value)) + _title = "No title."; + else + _title = value; + } + } + + /// + /// The steps of the preparation. See: . + /// + public List PreparationSteps { get; set; } + #endregion + + #region Constructors + /// + /// Construct a new recipe. + /// + /// The title of the recipe + /// The id of the recipe. If not given, get a new id. + /// The steps of the preparation of the meal + public Recipe(string title = "", int? id = null, + params PreparationStep[] preparationSteps) + { + Title = title; + PreparationSteps = new List(preparationSteps); if (id == null) { var randomGenerator = RandomNumberGenerator.Create(); byte[] data = new byte[16]; - randomGenerator.GetBytes(data); - Id = Math.Abs(BitConverter.ToInt16(data)); - } - else Id = (int)id; - } - #endregion - - #region Methods - public virtual bool Equals(Recipe? other) - { - if (other == null) return false; - if (other == this) return true; - return Title.Equals(other.Title) && PreparationSteps.Equals(other.PreparationSteps); - } - + randomGenerator.GetBytes(data); + Id = Math.Abs(BitConverter.ToInt16(data)); + } + else Id = (int)id; + } + #endregion + + #region Methods + public virtual bool Equals(Recipe? other) + { + if (other == null) return false; + if (other == this) return true; + return Title.Equals(other.Title) && PreparationSteps.Equals(other.PreparationSteps); + } + public override bool Equals(object? obj) { var item = obj as Recipe; if (item == null) return false; return Equals(obj); - } - - public override int GetHashCode() - { - return Id.GetHashCode(); - } - - public override string ToString() - { - StringBuilder sb = new StringBuilder($"[Recipe n°{Id}] - {Title}\n"); - foreach (PreparationStep ps in PreparationSteps) - { - sb.AppendFormat("\t* {0}\n", ps.ToString()); - } - return sb.ToString(); - } - #endregion - } -} + } + + public override int GetHashCode() + { + return Id.GetHashCode(); + } + + public override string ToString() + { + StringBuilder sb = new StringBuilder($"[Recipe n°{Id}] - {Title}\n"); + foreach (PreparationStep ps in PreparationSteps) + { + sb.AppendFormat("\t* {0}\n", ps.ToString()); + } + return sb.ToString(); + } + #endregion + } +} diff --git a/MCTG/Model/RecipeCollection.cs b/MCTG/Model/RecipeCollection.cs index 3592bb8..3da7d6d 100644 --- a/MCTG/Model/RecipeCollection.cs +++ b/MCTG/Model/RecipeCollection.cs @@ -148,7 +148,12 @@ namespace Model 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 } } diff --git a/MCTG/SAE-2.01.sln b/MCTG/SAE-2.01.sln index 81242a9..4e7f013 100644 --- a/MCTG/SAE-2.01.sln +++ b/MCTG/SAE-2.01.sln @@ -1,42 +1,42 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.5.33516.290 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{666C2211-8EBB-4FC8-9484-CB93BC854153}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{42FF86BD-92F9-4A32-A938-68515905378F}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model_UnitTests", "Tests\Model_UnitTests\Model_UnitTests.csproj", "{45AB746A-194B-4E43-81EB-83B06F35AA33}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{08B80CE8-A01D-4D86-8989-AF225D5DA48C}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {666C2211-8EBB-4FC8-9484-CB93BC854153}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {666C2211-8EBB-4FC8-9484-CB93BC854153}.Debug|Any CPU.Build.0 = Debug|Any CPU - {666C2211-8EBB-4FC8-9484-CB93BC854153}.Release|Any CPU.ActiveCfg = Release|Any CPU - {666C2211-8EBB-4FC8-9484-CB93BC854153}.Release|Any CPU.Build.0 = Release|Any CPU - {42FF86BD-92F9-4A32-A938-68515905378F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {42FF86BD-92F9-4A32-A938-68515905378F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {42FF86BD-92F9-4A32-A938-68515905378F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {42FF86BD-92F9-4A32-A938-68515905378F}.Release|Any CPU.Build.0 = Release|Any CPU - {45AB746A-194B-4E43-81EB-83B06F35AA33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {45AB746A-194B-4E43-81EB-83B06F35AA33}.Debug|Any CPU.Build.0 = Debug|Any CPU - {45AB746A-194B-4E43-81EB-83B06F35AA33}.Release|Any CPU.ActiveCfg = Release|Any CPU - {45AB746A-194B-4E43-81EB-83B06F35AA33}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {45AB746A-194B-4E43-81EB-83B06F35AA33} = {08B80CE8-A01D-4D86-8989-AF225D5DA48C} - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {ADEA5603-1EF6-4D43-9493-7D6D9DE7FA3F} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33516.290 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{666C2211-8EBB-4FC8-9484-CB93BC854153}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{42FF86BD-92F9-4A32-A938-68515905378F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model_UnitTests", "Tests\Model_UnitTests\Model_UnitTests.csproj", "{45AB746A-194B-4E43-81EB-83B06F35AA33}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{08B80CE8-A01D-4D86-8989-AF225D5DA48C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {666C2211-8EBB-4FC8-9484-CB93BC854153}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {666C2211-8EBB-4FC8-9484-CB93BC854153}.Debug|Any CPU.Build.0 = Debug|Any CPU + {666C2211-8EBB-4FC8-9484-CB93BC854153}.Release|Any CPU.ActiveCfg = Release|Any CPU + {666C2211-8EBB-4FC8-9484-CB93BC854153}.Release|Any CPU.Build.0 = Release|Any CPU + {42FF86BD-92F9-4A32-A938-68515905378F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {42FF86BD-92F9-4A32-A938-68515905378F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {42FF86BD-92F9-4A32-A938-68515905378F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {42FF86BD-92F9-4A32-A938-68515905378F}.Release|Any CPU.Build.0 = Release|Any CPU + {45AB746A-194B-4E43-81EB-83B06F35AA33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {45AB746A-194B-4E43-81EB-83B06F35AA33}.Debug|Any CPU.Build.0 = Debug|Any CPU + {45AB746A-194B-4E43-81EB-83B06F35AA33}.Release|Any CPU.ActiveCfg = Release|Any CPU + {45AB746A-194B-4E43-81EB-83B06F35AA33}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {45AB746A-194B-4E43-81EB-83B06F35AA33} = {08B80CE8-A01D-4D86-8989-AF225D5DA48C} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {ADEA5603-1EF6-4D43-9493-7D6D9DE7FA3F} + EndGlobalSection +EndGlobal