using System;
using System.Collections.Generic;
namespace Model
{
///
/// A Recipe collection is a group of recipe.
/// It is instantiated with a new unique four digit id, where the first one is 2.
///
public class RecipeCollection : BaseItem
{
///
/// This static atrribute stand for the creation of a new id. It is incremented each time a new RecipeCollection is instantiated.
///
private static uint idIterator = 0;
///
/// The main composent of a RecipeCollection. This contain the Recipes included in this collection.
///
public List Collection { get; set; }
public RecipeCollection(string description = "", List? collection = null)
: base(idIterator + 2000, description)
{
if (idIterator == 1000) throw new OverflowException("id has reach the maximum value.");
if (collection == null)
Collection = new List();
else
Collection = collection;
idIterator++;
}
public override string ToString()
{
return
$"[ Class -RecipeCollection- ]\n\n" +
$"\t.Id - {Id}\n" +
$"\t.Description - {Description}\n" +
$"______\n\n";
}
///
/// 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[uint id]
{
get
{
foreach (Recipe r in this.Collection)
{
if (r.Id == id) return r;
}
throw new ArgumentNullException("The id given dosen't correspond to any recipes in the collection.");
}
}
///
/// 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.Collection.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.Collection.Remove(ri); return coll;
}
}
}