|
|
|
@ -5,159 +5,103 @@ using System.Collections.ObjectModel;
|
|
|
|
|
|
|
|
|
|
namespace MCTGLib
|
|
|
|
|
{
|
|
|
|
|
/// <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 readonly 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 = new List<Recipe>(recipes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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 this.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public class RecipeCollection : IList<Recipe>, IEquatable<RecipeCollection>
|
|
|
|
|
{
|
|
|
|
|
#region Attributes
|
|
|
|
|
private List<Recipe> _recipes;
|
|
|
|
|
#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;
|
|
|
|
|
#region Properties
|
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
|
|
|
|
|
#region IList Prperties
|
|
|
|
|
public int Count => _recipes.Count;
|
|
|
|
|
public bool IsReadOnly => false;
|
|
|
|
|
public Recipe this[int index] { get => _recipes[index]; set => _recipes[index] = value; }
|
|
|
|
|
#endregion
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
public RecipeCollection(string description = "No Description.")
|
|
|
|
|
{
|
|
|
|
|
_recipes = new List<Recipe>();
|
|
|
|
|
Description = description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
public RecipeCollection(params Recipe[] recipes)
|
|
|
|
|
{
|
|
|
|
|
_recipes = new List<Recipe>(recipes);
|
|
|
|
|
Description = "No Description.";
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
#region IList Methods
|
|
|
|
|
public int IndexOf(Recipe item)
|
|
|
|
|
{
|
|
|
|
|
return _recipes.IndexOf(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Insert(int index, Recipe item)
|
|
|
|
|
{
|
|
|
|
|
_recipes.Insert(index, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveAt(int index)
|
|
|
|
|
{
|
|
|
|
|
_recipes.RemoveAt(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
#region IEquatable Mathods
|
|
|
|
|
public bool Equals(RecipeCollection? other)
|
|
|
|
|
{
|
|
|
|
|
if (other == null) return false;
|
|
|
|
|
if (other == this) return true;
|
|
|
|
|
return Description.Equals(other.Description) && _recipes.Equals(other._recipes);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return Description.GetHashCode() + _recipes.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|