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.
164 lines
4.8 KiB
164 lines
4.8 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace Model
|
|
{
|
|
/// <summary>
|
|
/// A Recipe collection is a group of recipe.<br/>
|
|
/// It is instantiated with a new unique id, where the first number is 2.
|
|
/// </summary>
|
|
public class RecipeCollection : BaseItem, ICollection<Recipe>
|
|
{
|
|
#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<Recipe> _recipes = new List<Recipe>(); // 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
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
private static uint ComputeId()
|
|
{
|
|
uint dec = 1;
|
|
while ((_idCreator / (Math.Pow(10, dec)) >= 1)) dec++;
|
|
|
|
uint id = (CAT_ITEM * (uint)(Math.Pow(10, dec))) + _idCreator++;
|
|
Console.WriteLine("[recipe collection] new computed id: {0}", id);
|
|
|
|
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<Recipe>)
|
|
throw new InvalidCastException("Need to verify the type of the private attribute '_recipes'.");
|
|
|
|
return (this._recipes as List<Recipe>).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<Recipe> GetEnumerator()
|
|
{
|
|
return _recipes.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return _recipes.GetEnumerator();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region Operators Overloading
|
|
|
|
/// <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[Index id]
|
|
{
|
|
get => this.ElementAt(id);
|
|
}
|
|
|
|
/// <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.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.Remove(ri); return coll;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|