You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SAE-2.01/MCTG/MCTGLib/RecipeCollection.cs

87 lines
3.0 KiB

using System;
using System.Collections.Generic;
namespace Model
{
/// <summary>
/// A Recipe collection is a group of recipe.<br/>
/// It is instantiated with a new unique four digit id, where the first one is 2.
/// </summary>
public class RecipeCollection : BaseItem
{
/// <summary>
/// This static atrribute stand for the creation of a new id. It is incremented each time a new RecipeCollection is instantiated.
/// </summary>
private static uint idIterator = 0;
/// <summary>
/// The main composent of a RecipeCollection. This contain the Recipes included in this collection.
/// </summary>
public List<Recipe> Collection { get; set; }
public RecipeCollection(string description = "", List<Recipe>? collection = null)
: base(idIterator + 2000, description)
{
if (idIterator == 1000) throw new OverflowException("id has reach the maximum value.");
if (collection == null)
Collection = new List<Recipe>();
else
Collection = collection;
idIterator++;
}
public override string ToString()
{
return
$"[ Class -RecipeCollection- ]\n\n" +
$"\t.Id - {Id}\n" +
$"\t.Description - {Description}\n" +
$"______\n\n";
}
/// <summary>
/// Overload the [] operator to access the Recipe directly without passing by the Collection.<br/>
/// It use the identifier of the Recipe to get the item.
/// </summary>
/// <param name="id">The id of the Recipe</param>
/// <returns></returns>
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.");
}
}
/// <summary>
/// Overload of + operator to add a Recipe to a RecipeCollection.
/// </summary>
/// <param name="coll">The recipe collection where the recipe will be added</param>
/// <param name="ri">The recipe to add</param>
/// <returns>The recipe collection where the recipe is added.</returns>
public static RecipeCollection operator + (RecipeCollection coll, Recipe ri)
{
coll.Collection.Add(ri); return coll;
}
/// <summary>
/// Overload of - operator to remove a Recipe to a RecipeCollection.
/// </summary>
/// <param name="coll">The recipe collection where the recipe will be removed</param>
/// <param name="ri">The recipe to remove</param>
/// <returns>The recipe collection where the recipe is removed.</returns>
public static RecipeCollection operator - (RecipeCollection coll, Recipe ri)
{
coll.Collection.Remove(ri); return coll;
}
}
}