|
|
@ -1,38 +1,66 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Model
|
|
|
|
namespace Model
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// A Recipe collection is a group of recipe.<br/>
|
|
|
|
/// 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.
|
|
|
|
/// It is instantiated with a new unique id, where the first number is 2.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class RecipeCollection : BaseItem
|
|
|
|
public class RecipeCollection : BaseItem, ICollection<Recipe>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
#region Private Attributes
|
|
|
|
/// This static atrribute stand for the creation of a new id. It is incremented each time a new RecipeCollection is instantiated.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
private const int CAT_ITEM = 2; // the first number of the item full id : the item category.
|
|
|
|
private static uint idIterator = 0;
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// The main composent of a RecipeCollection. This contain the Recipes included in this collection.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public List<Recipe> Collection { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Public Properties
|
|
|
|
|
|
|
|
#region ICollection Implementation
|
|
|
|
|
|
|
|
|
|
|
|
public RecipeCollection(string description = "", List<Recipe>? collection = null)
|
|
|
|
public int Count => _recipes.Count;
|
|
|
|
: base(idIterator + 2000, description)
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (idIterator == 1000) throw new OverflowException("id has reach the maximum value.");
|
|
|
|
_recipes.CopyTo(recipes, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
if (collection == null)
|
|
|
|
#region Private Methods
|
|
|
|
Collection = new List<Recipe>();
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
Collection = collection;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
idIterator++;
|
|
|
|
/// <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 = 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()
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -43,22 +71,66 @@ namespace Model
|
|
|
|
$"______\n\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>
|
|
|
|
/// <summary>
|
|
|
|
/// Overload the [] operator to access the Recipe directly without passing by the Collection.<br/>
|
|
|
|
/// 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.
|
|
|
|
/// It use the identifier of the Recipe to get the item.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The id of the Recipe</param>
|
|
|
|
/// <param name="id">The id of the Recipe</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <returns></returns>
|
|
|
|
public Recipe this[uint id]
|
|
|
|
public Recipe this[Index id]
|
|
|
|
{
|
|
|
|
{
|
|
|
|
get
|
|
|
|
get => this.ElementAt(id);
|
|
|
|
{
|
|
|
|
|
|
|
|
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>
|
|
|
|
/// <summary>
|
|
|
@ -69,7 +141,7 @@ namespace Model
|
|
|
|
/// <returns>The recipe collection where the recipe is added.</returns>
|
|
|
|
/// <returns>The recipe collection where the recipe is added.</returns>
|
|
|
|
public static RecipeCollection operator + (RecipeCollection coll, Recipe ri)
|
|
|
|
public static RecipeCollection operator + (RecipeCollection coll, Recipe ri)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
coll.Collection.Add(ri); return coll;
|
|
|
|
coll.Add(ri); return coll;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
@ -80,7 +152,9 @@ namespace Model
|
|
|
|
/// <returns>The recipe collection where the recipe is removed.</returns>
|
|
|
|
/// <returns>The recipe collection where the recipe is removed.</returns>
|
|
|
|
public static RecipeCollection operator - (RecipeCollection coll, Recipe ri)
|
|
|
|
public static RecipeCollection operator - (RecipeCollection coll, Recipe ri)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
coll.Collection.Remove(ri); return coll;
|
|
|
|
coll.Remove(ri); return coll;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|