♻️ changes on implementation. new structure described in #17
continuous-integration/drone/push Build is failing Details

pull/24/head
Alexandre AGOSTINHO 2 years ago
parent 522f169128
commit a38e35d1ed

@ -8,48 +8,5 @@ Console.WriteLine("Hello, World!\n");
// TESTS: // TESTS:
Console.WriteLine("tests on Recipe class:"); RecipeCollection rc = new RecipeCollection();
BaseItem r1 = new Recipe("A recipe..."); Console.WriteLine($"rc.Description: {rc.Description}");
r1.DisplayItem();
r1.DisplayId();
r1.DisplayDescription();
Console.WriteLine();
Console.WriteLine("tests on RecipeCollection class:");
RecipeCollection rc = new RecipeCollection("A recipe collection...");
for (uint i = 0; i < 10; i++)
{
rc += new Recipe($"Recipe number {i} in the collection.");
Console.WriteLine("test overload + operator");
}
Console.WriteLine("test overload of [] operator:");
try
{
rc[2].DisplayId();
rc.GetById(110).DisplayId();
}
catch (ArgumentNullException)
{
Console.Error.WriteLine("An index are incorrect!\n");
}
catch (Exception e)
{
Console.Error.WriteLine("Exception thrown: {0}", e);
}
Console.WriteLine("test of multiple params constructor:");
RecipeCollection rc2 = new RecipeCollection(
new Recipe(),
new Recipe(),
new Recipe(),
new Recipe(),
new Recipe());
Console.WriteLine("test of Enumerable property:");
foreach (Recipe r in rc2)
{
r.DisplayId();
}

@ -1,94 +0,0 @@

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
namespace MCTGLib
{
/// <summary>
/// Define the base structure of any Items.<br/>
/// An Item can be identifed and have a short description. It can also be displayed.
/// </summary>
public abstract class BaseItem : IDisplayable, IEquatable<BaseItem>
{
#region Private Attributes
/// <summary>
/// The identifier of an Item.<br/>
/// The first number correspond to the typs of the Item.
/// </summary>
virtual public uint Id
{
get => _id;
init => _id = value;
}
protected uint _id;
#endregion
#region Public Properties
/// <summary>
/// A short description of the Item. Useful to know what this Item stand for.
/// </summary>
public string Description { get; set; }
#endregion
#region Constructors
protected BaseItem(uint id, string description="Any Item.")
{
Id = id; Description = description;
}
#endregion
#region Public Methods
public override string ToString()
{
return
$"[ Class -BaseItem- ]\n\n" +
$"\t.Id - {Id}\n" +
$"\t.Description - {Description}\n" +
$"______\n\n";
}
#region IDisplayable Implementation
public void DisplayId()
{
Console.WriteLine($".Id - {Id}");
}
public void DisplayDescription()
{
Console.WriteLine($".Description - {Description}");
}
public void DisplayItem()
{
Console.WriteLine(this.ToString());
}
#endregion
#region IEquatable<BaseItem> implementation
public int GetHashCode([DisallowNull] BaseItem obj)
{
return obj.Id.GetHashCode() + obj.Description.GetHashCode();
}
public bool Equals(BaseItem? other)
{
if (other == null) return false;
return (this.Id == other.Id) && (this.Description == other.Description);
}
#endregion
#endregion
}
}

@ -1,23 +0,0 @@
namespace MCTGLib
{
/// <summary>
/// Define an Item that can be displayed.
/// </summary>
interface IDisplayable
{
/// <summary>
/// Display the Id of the Item
/// </summary>
void DisplayId();
/// <summary>
/// Display the Description of the Item
/// </summary>
void DisplayDescription();
/// <summary>
/// Display the entire Item (Id, Description and other...)
/// </summary>
void DisplayItem();
}
}

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MCTGLib
{
public class PreparationStep
{
#region Attributes
private string? _description;
#endregion
#region Properties
public int Order { get; init; }
public string? Description
{
get => _description;
set
{
if (string.IsNullOrEmpty(value))
{
_description = "No data."; return;
}
_description = value;
}
}
#endregion
#region Constructors
public PreparationStep(int order, string? description)
{
Order = order;
Description = description;
}
#endregion
}
}

@ -1,85 +1,35 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace MCTGLib namespace MCTGLib
{ {
/// <summary>
/// A Recipe is a description of step and maybe some techniques, with an ingredient list to make a meal.<br/>
/// It is instantiated with a new unique id, where the first number is 1.
/// </summary>
public class Recipe : BaseItem
{
#region Private Attributes
private const int CAT_ITEM = 1;
private static uint _idCreator = 0; // stand for the creation of a new id. It is incremented each time a new Recipe is instantiated.
private string title = "";
#endregion
#region Public Properties
/// <summary> public class Recipe : IEquatable<Recipe>
/// The title of the recipe.
/// </summary>
public string Title
{ {
get => title; private static int idCreator = 0;
set
{
title = value;
if (string.IsNullOrEmpty(title)) title = $"Recipe n{this.Id}";
}
}
/// <summary>
/// All the details about the preparation of the meal.
/// </summary>
public string Preparation { get; set; }
#endregion public int Id { get; init; }
public string Description { get; init; }
public string Title { get; set; }
#region Constructors public Recipe(string title, string description = "No Description.")
public Recipe(string title="", string preparation="")
: base(ComputeId(), "A recipe.")
{ {
Id = idCreator++;
Title = title; Title = title;
Preparation = preparation; Description = description;
} }
#endregion public bool Equals(Recipe? other)
#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; if (other == null) return false;
while ((_idCreator / (Math.Pow(10, dec)) >= 1)) dec++; if (other == this) return true;
return Title.Equals(other.Title) && Description.Equals(other.Description);
uint id = (CAT_ITEM * (uint)(Math.Pow(10, dec))) + _idCreator++;
Console.WriteLine("[recipe] new computed id: {0}", id);
return id;
} }
#endregion public override int GetHashCode()
#region Public Methods
public override string ToString()
{ {
return return Id.GetHashCode();
$"[ Class -Recipe- ]\n\n" +
$"\t.Id - {Id}\n" +
$"\t.Description - {Description}\n" +
$"______\n\n";
} }
#endregion
} }
} }

@ -5,85 +5,54 @@ using System.Collections.ObjectModel;
namespace MCTGLib 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.
public class RecipeCollection : IList<Recipe>, IEquatable<RecipeCollection>
{
#region Attributes
private List<Recipe> _recipes;
#endregion #endregion
#region Public Properties #region Properties
public string Description { get; set; }
#region ICollection Implementation
#region IList Prperties
public int Count => _recipes.Count; public int Count => _recipes.Count;
public bool IsReadOnly => false; public bool IsReadOnly => false;
public Recipe this[int index] { get => _recipes[index]; set => _recipes[index] = value; }
#endregion #endregion
#endregion #endregion
#region Constructors #region Constructors
public RecipeCollection(string description = "No Description.")
public RecipeCollection(string description="A recipe collection.") {
: base(ComputeId(), description) _recipes = new List<Recipe>();
{ } Description = description;
}
public RecipeCollection(params Recipe[] recipes) public RecipeCollection(params Recipe[] recipes)
: base(ComputeId())
{ {
_recipes = new List<Recipe>(recipes); _recipes = new List<Recipe>(recipes);
Description = "No Description.";
} }
#endregion #endregion
#region Private Methods #region Methods
#region IList Methods
/// <summary> public int IndexOf(Recipe item)
/// 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; return _recipes.IndexOf(item);
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 public void Insert(int index, Recipe item)
#region Public Methods
public override string ToString()
{ {
return _recipes.Insert(index, item);
$"[ Class -RecipeCollection- ]\n\n" +
$"\t.Id - {Id}\n" +
$"\t.Description - {Description}\n" +
$"______\n\n";
} }
public Recipe GetById(uint id) public void RemoveAt(int index)
{ {
if (this._recipes is not List<Recipe>) _recipes.RemoveAt(index);
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) public void Add(Recipe item)
{ {
_recipes.Add(item); _recipes.Add(item);
@ -116,48 +85,23 @@ namespace MCTGLib
IEnumerator IEnumerable.GetEnumerator() IEnumerator IEnumerable.GetEnumerator()
{ {
return this.GetEnumerator(); return _recipes.GetEnumerator();
} }
#endregion
#endregion #endregion
#region Operators Overloading #region IEquatable Mathods
public bool Equals(RecipeCollection? other)
/// <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); if (other == null) return false;
} if (other == this) return true;
return Description.Equals(other.Description) && _recipes.Equals(other._recipes);
/// <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;
} }
#endregion
/// <summary> public override int GetHashCode()
/// 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; return Description.GetHashCode() + _recipes.GetHashCode();
} }
#endregion #endregion
} }
} }

Loading…
Cancel
Save