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 : List, IEquatable { #region Attributes 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; } } #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) : base(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 = this.Find(r => r.Id == id); if (recipe == null) throw new ArgumentException("No _recipes match the given id."); return recipe; } /// /// Utility to find a recipe by his name. /// /// The string for the search /// A collection of Recipe where their Title contain the string. public RecipeCollection ResearchByName(string str) { return new RecipeCollection( description: $"Results of the research: {str}", recipes: this.FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray()); } public virtual bool Equals(RecipeCollection? other) { if (other == null) return false; if (other == this) return true; return this.Description.Equals(other.Description); } public override bool Equals(object? obj) { var item = obj as RecipeCollection; if (item == null) return false; return Equals(obj); } public override int GetHashCode() { return Description.GetHashCode(); } public override string ToString() { StringBuilder sb = new StringBuilder($"[RecipeCollection] - {Description}:\n"); foreach (Recipe r in this) { sb.AppendFormat("\t - {0}\n", r.ToString()); } return sb.ToString(); } #endregion } }