📝 add comments for the doc

pull/24/head
Alexandre Agostinho 2 years ago
parent 316cb66c74
commit 531cf092b9

@ -9,13 +9,24 @@ Console.WriteLine("Hello, World!\n\n");
// TESTS: // TESTS:
RecipeCollection rc = new RecipeCollection("Desserts", 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(1, "Faire cuire le truc."),
new PreparationStep(2, "Elaboré un autre truc"), new PreparationStep(2, "Elaboré un autre truc"),
new PreparationStep(3, null)), new PreparationStep(3, null)),
new Recipe(null, null, 99, new Recipe(null, 99,
new PreparationStep(1, null), new PreparationStep(1, null),
new PreparationStep(1, null)), new PreparationStep(1, null)),
new Recipe()); new Recipe());
Console.WriteLine(rc.ToString()); 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);
}

@ -6,14 +6,25 @@ using System.Threading.Tasks;
namespace Model namespace Model
{ {
public class PreparationStep /// <summary>
/// Define a step of the preparation of a recipe.
/// </summary>
public class PreparationStep : IEquatable<PreparationStep>
{ {
#region Attributes #region Attributes
private string? _description; private string? _description;
#endregion #endregion
#region Properties #region Properties
/// <summary>
/// The order of this step in the preparation of the meal.
/// </summary>
public int Order { get; init; } public int Order { get; init; }
/// <summary>
/// The description of the task the user need to do for this step of the preparation. <br/>
/// Set to "No description." when the value passed is null, empty or contain white spaces.
/// </summary>
public string? Description public string? Description
{ {
get => _description; get => _description;
@ -28,6 +39,11 @@ namespace Model
#endregion #endregion
#region Constructors #region Constructors
/// <summary>
/// Construct a new step of preparation.
/// </summary>
/// <param name="order">The number of the order in preparation</param>
/// <param name="description">The description of the task</param>
public PreparationStep(int order, string? description) public PreparationStep(int order, string? description)
{ {
Order = order; Order = order;
@ -36,6 +52,11 @@ namespace Model
#endregion #endregion
#region Methods #region Methods
public bool Equals(PreparationStep? other)
{
return Order.Equals(other.Order) && Description.Equals(other.Description);
}
public override string ToString() public override string ToString()
{ {
return $"{Order}- {Description}"; return $"{Order}- {Description}";

@ -5,7 +5,9 @@ using System.Text;
namespace Model namespace Model
{ {
/// <summary>
/// Define a Recipe for the preparation of a meal.
/// </summary>
public class Recipe : IEquatable<Recipe> public class Recipe : IEquatable<Recipe>
{ {
#region Attributes #region Attributes
@ -15,7 +17,15 @@ namespace Model
#endregion #endregion
#region Properties #region Properties
/// <summary>
/// The ID of the recipe - allows you to compare and/or get this item in an easier way.
/// </summary>
public int Id { get; init; } public int Id { get; init; }
/// <summary>
/// The Title of the recipe. <br/>
/// Set to "No title." when the value passed is null, empty or contain white spaces.
/// </summary>
public string? Title public string? Title
{ {
get => _title; get => _title;
@ -27,28 +37,26 @@ namespace Model
_title = value; _title = value;
} }
} }
public string? Description
{ /// <summary>
get => _description; /// The steps of the preparation. See: <see cref="PreparationStep"/>.
set /// </summary>
{
if (string.IsNullOrWhiteSpace(value))
_description = "No description.";
else
_description = value;
}
}
public List<PreparationStep>? PreparationSteps { get; private set; } public List<PreparationStep>? PreparationSteps { get; private set; }
#endregion #endregion
#region Constructors #region Constructors
public Recipe(string? title = null, string? description = null, int? id = null, /// <summary>
/// Construct a new recipe.
/// </summary>
/// <param name="title">The title of the recipe</param>
/// <param name="id">The id of the recipe. If not given, get a new id.</param>
/// <param name="preparationSteps">The steps of the preparation of the meal</param>
public Recipe(string? title = null, int? id = null,
params PreparationStep[] preparationSteps) params PreparationStep[] preparationSteps)
{ {
if (id == null) Id = idCreator++; if (id == null) Id = idCreator++;
else Id = (int)id; else Id = (int)id;
Title = title; Title = title;
Description = description;
PreparationSteps = new List<PreparationStep>(preparationSteps); PreparationSteps = new List<PreparationStep>(preparationSteps);
} }
#endregion #endregion
@ -58,7 +66,7 @@ namespace Model
{ {
if (other == null) return false; if (other == null) return false;
if (other == this) return true; 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() public override int GetHashCode()
@ -68,7 +76,7 @@ namespace Model
public override string ToString() 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) foreach (PreparationStep ps in PreparationSteps)
{ {
sb.AppendFormat("\t* {0}\n", ps.ToString()); sb.AppendFormat("\t* {0}\n", ps.ToString());

@ -6,7 +6,10 @@ using System.Text;
namespace Model namespace Model
{ {
/// <summary>
/// Define a collection of <see cref="Recipe"/>.
/// <br/>This class implement <see cref="IList"/> and <see cref="IEquatable{T}"/>.
/// </summary>
public class RecipeCollection : IList<Recipe>, IEquatable<RecipeCollection> public class RecipeCollection : IList<Recipe>, IEquatable<RecipeCollection>
{ {
#region Attributes #region Attributes
@ -15,6 +18,10 @@ namespace Model
#endregion #endregion
#region Properties #region Properties
/// <summary>
/// A short description of what this collection contain. <br/>
/// Set to "No description." when the value passed is null, empty or contain white spaces.
/// </summary>
public string? Description public string? Description
{ {
get => _description; get => _description;
@ -35,6 +42,11 @@ namespace Model
#endregion #endregion
#region Constructors #region Constructors
/// <summary>
/// Construct a new collection of recipes.
/// </summary>
/// <param name="description">A short description of what this list will contain</param>
/// <param name="recipes">Recipes to add in this new collection</param>
public RecipeCollection(string? description = null, params Recipe[] recipes) public RecipeCollection(string? description = null, params Recipe[] recipes)
{ {
this.recipes = new List<Recipe>(recipes); this.recipes = new List<Recipe>(recipes);
@ -43,6 +55,19 @@ namespace Model
#endregion #endregion
#region Methods #region Methods
/// <summary>
/// Find a recipe in this list by giving the id.
/// </summary>
/// <param name="id">The id of the list we are looking for</param>
/// <returns>The recipe of the id given</returns>
/// <exception cref="ArgumentException"/>
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 #region IList Methods
public int IndexOf(Recipe item) public int IndexOf(Recipe item)
{ {

@ -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'. 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.Title);
Assert.NotNull(r.Description);
} }
[Theory] [Theory]
[InlineData("Cookies", "Choco Cookies", 23, "Cookies", "Choco Cookies", 23)] [InlineData("Cookies", 23, "Cookies", 23)]
[InlineData("Cookies", "No description.", 1, "Cookies", "", 1)] [InlineData("Cookies", 1, "Cookies", 1)]
[InlineData("No title.", "Choco Cookies", 1, "", "Choco Cookies", 1)] [InlineData("No title.", 1, "", 1)]
[InlineData("Cookies", "Choco Cookies", 0, "Cookies", "Choco Cookies", null)] [InlineData("Cookies", 0, "Cookies", null)]
[InlineData("Cookies", "No description.", 1, "Cookies", null, 1)] [InlineData("No title.", 1, null, 1)]
[InlineData("No title.", "Choco Cookies", 1, null, "Choco Cookies", 1)] public void TestConstructor(string expectedTitle, int expectedId, string title, int? id)
public void TestConstructor(string expectedTitle, string expectedDescription, int expectedId,
string title, string description, int? id)
{ {
Recipe r = new Recipe(title, description, id); Recipe r = new Recipe(title, id);
Assert.NotNull(r.Title); Assert.NotNull(r.Title);
Assert.NotNull(r.Description);
Assert.Equal(expectedId, r.Id); Assert.Equal(expectedId, r.Id);
Assert.Equal(expectedTitle, r.Title); Assert.Equal(expectedTitle, r.Title);
Assert.Equal(expectedDescription, r.Description);
} }
} }
} }

Loading…
Cancel
Save