diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index ac6bfb0..4a2aedd 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -9,13 +9,24 @@ Console.WriteLine("Hello, World!\n\n"); // TESTS: RecipeCollection rc = new RecipeCollection("Desserts", - new Recipe("Cookies", "Cookies au chocolats", null, + new Recipe("Cookies", null, new PreparationStep(1, "Faire cuire le truc."), new PreparationStep(2, "Elaboré un autre truc"), new PreparationStep(3, null)), - new Recipe(null, null, 99, + new Recipe(null, 99, new PreparationStep(1, null), new PreparationStep(1, null)), new Recipe()); Console.WriteLine(rc.ToString()); + +Console.WriteLine(rc.GetRecipeById(99).ToString()); + +try +{ + Console.WriteLine(rc.GetRecipeById(23).ToString()); +} +catch (ArgumentException e) +{ + Console.Error.WriteLine(e.Message); +} diff --git a/MCTG/Model/PreparationStep.cs b/MCTG/Model/PreparationStep.cs index 6057273..1301dac 100644 --- a/MCTG/Model/PreparationStep.cs +++ b/MCTG/Model/PreparationStep.cs @@ -6,14 +6,25 @@ using System.Threading.Tasks; namespace Model { - public class PreparationStep + /// + /// Define a step of the preparation of a recipe. + /// + public class PreparationStep : IEquatable { #region Attributes private string? _description; #endregion #region Properties + /// + /// The order of this step in the preparation of the meal. + /// public int Order { get; init; } + + /// + /// The description of the task the user need to do for this step of the preparation.
+ /// Set to "No description." when the value passed is null, empty or contain white spaces. + ///
public string? Description { get => _description; @@ -28,6 +39,11 @@ namespace Model #endregion #region Constructors + /// + /// Construct a new step of preparation. + /// + /// The number of the order in preparation + /// The description of the task public PreparationStep(int order, string? description) { Order = order; @@ -36,6 +52,11 @@ namespace Model #endregion #region Methods + public bool Equals(PreparationStep? other) + { + return Order.Equals(other.Order) && Description.Equals(other.Description); + } + public override string ToString() { return $"{Order}- {Description}"; diff --git a/MCTG/Model/Recipe.cs b/MCTG/Model/Recipe.cs index e493212..b575c6a 100644 --- a/MCTG/Model/Recipe.cs +++ b/MCTG/Model/Recipe.cs @@ -5,7 +5,9 @@ using System.Text; namespace Model { - + /// + /// Define a Recipe for the preparation of a meal. + /// public class Recipe : IEquatable { #region Attributes @@ -15,7 +17,15 @@ namespace Model #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; @@ -27,28 +37,26 @@ namespace Model _title = value; } } - public string? Description - { - get => _description; - set - { - if (string.IsNullOrWhiteSpace(value)) - _description = "No description."; - else - _description = value; - } - } + + /// + /// The steps of the preparation. See: . + /// public List? PreparationSteps { get; private set; } #endregion #region Constructors - public Recipe(string? title = null, string? description = null, int? id = null, + /// + /// 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 = null, int? id = null, params PreparationStep[] preparationSteps) { if (id == null) Id = idCreator++; else Id = (int)id; Title = title; - Description = description; PreparationSteps = new List(preparationSteps); } #endregion @@ -58,7 +66,7 @@ namespace Model { if (other == null) return false; if (other == this) return true; - return Title.Equals(other.Title) && Description.Equals(other.Description); + return Title.Equals(other.Title) && PreparationSteps.Equals(other.PreparationSteps); } public override int GetHashCode() @@ -68,7 +76,7 @@ namespace Model public override string ToString() { - StringBuilder sb = new StringBuilder($"[Recipe] - {Title}: {Description}\n"); + StringBuilder sb = new StringBuilder($"[Recipe n°{Id}] - {Title}\n"); foreach (PreparationStep ps in PreparationSteps) { sb.AppendFormat("\t* {0}\n", ps.ToString()); diff --git a/MCTG/Model/RecipeCollection.cs b/MCTG/Model/RecipeCollection.cs index 283af8e..2de66d6 100644 --- a/MCTG/Model/RecipeCollection.cs +++ b/MCTG/Model/RecipeCollection.cs @@ -6,7 +6,10 @@ using System.Text; namespace Model { - + /// + /// Define a collection of . + ///
This class implement and . + ///
public class RecipeCollection : IList, IEquatable { #region Attributes @@ -15,6 +18,10 @@ namespace Model #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; @@ -35,6 +42,11 @@ namespace Model #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 = null, params Recipe[] recipes) { this.recipes = new List(recipes); @@ -43,6 +55,19 @@ namespace Model #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) { diff --git a/MCTG/Tests/Model_UnitTests/Recipe_UT.cs b/MCTG/Tests/Model_UnitTests/Recipe_UT.cs index e1d6720..205d6cd 100644 --- a/MCTG/Tests/Model_UnitTests/Recipe_UT.cs +++ b/MCTG/Tests/Model_UnitTests/Recipe_UT.cs @@ -9,25 +9,20 @@ namespace Model_UnitTests { Recipe r = new Recipe(id: 999); // id is given to avoid tests errors with the static atrribute 'idCreator'. Assert.NotNull(r.Title); - Assert.NotNull(r.Description); } [Theory] - [InlineData("Cookies", "Choco Cookies", 23, "Cookies", "Choco Cookies", 23)] - [InlineData("Cookies", "No description.", 1, "Cookies", "", 1)] - [InlineData("No title.", "Choco Cookies", 1, "", "Choco Cookies", 1)] - [InlineData("Cookies", "Choco Cookies", 0, "Cookies", "Choco Cookies", null)] - [InlineData("Cookies", "No description.", 1, "Cookies", null, 1)] - [InlineData("No title.", "Choco Cookies", 1, null, "Choco Cookies", 1)] - public void TestConstructor(string expectedTitle, string expectedDescription, int expectedId, - string title, string description, int? id) + [InlineData("Cookies", 23, "Cookies", 23)] + [InlineData("Cookies", 1, "Cookies", 1)] + [InlineData("No title.", 1, "", 1)] + [InlineData("Cookies", 0, "Cookies", null)] + [InlineData("No title.", 1, null, 1)] + public void TestConstructor(string expectedTitle, int expectedId, string title, int? id) { - Recipe r = new Recipe(title, description, id); + Recipe r = new Recipe(title, id); Assert.NotNull(r.Title); - Assert.NotNull(r.Description); Assert.Equal(expectedId, r.Id); Assert.Equal(expectedTitle, r.Title); - Assert.Equal(expectedDescription, r.Description); } } }