using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; namespace Model { /// /// A Recipe collection is a group of recipe.
/// It is instantiated with a new unique id, where the first number is 2. ///
public class RecipeCollection : BaseItem, ICollection { #region Private Attributes private const int CAT_ITEM = 2; // the first number of the item full id : the item category. private static uint _idCreator = 0; // stand for the creation of a new id. It is incremented each time a new Recipe is instantiated. private ICollection _recipes = new List(); // main composent of this class. #endregion #region Public Properties #region ICollection Implementation public int Count => _recipes.Count; public bool IsReadOnly => false; #endregion #endregion #region Constructors public RecipeCollection(string description="A recipe collection.") : base(ComputeId(), description) { } public RecipeCollection(params Recipe[] recipes) : base(ComputeId()) { _recipes.CopyTo(recipes, 0); } #endregion #region Private Methods /// /// Processi the unique identificator of an Item. The identificator is calculated to put the number representing /// the type of item before the number of its number in its type. /// private static uint ComputeId() { uint dec = 0, id = _idCreator; while ((_idCreator / (Math.Pow(10, dec)) > 10)) dec++; id += CAT_ITEM * (uint)(Math.Pow(10, dec)); return id; } #endregion #region Public Methods public override string ToString() { return $"[ Class -RecipeCollection- ]\n\n" + $"\t.Id - {Id}\n" + $"\t.Description - {Description}\n" + $"______\n\n"; } public Recipe GetById(uint id) { if (this._recipes is not List) throw new InvalidCastException("Need to verify the type of the private attribute '_recipes'."); return (this._recipes as List).Find(x => x.Id.Equals(Id)); } #region ICollection Implementation 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 #endregion #region Operators Overloading /// /// Overload the [] operator to access the Recipe directly without passing by the Collection.
/// It use the identifier of the Recipe to get the item. ///
/// The id of the Recipe /// public Recipe this[Index id] { get => this.ElementAt(id); } /// /// Overload of + operator to add a Recipe to a RecipeCollection. /// /// The recipe collection where the recipe will be added /// The recipe to add /// The recipe collection where the recipe is added. public static RecipeCollection operator + (RecipeCollection coll, Recipe ri) { coll.Add(ri); return coll; } /// /// Overload of - operator to remove a Recipe to a RecipeCollection. /// /// The recipe collection where the recipe will be removed /// The recipe to remove /// The recipe collection where the recipe is removed. public static RecipeCollection operator - (RecipeCollection coll, Recipe ri) { coll.Remove(ri); return coll; } #endregion } }