From a38e35d1ed54fbe7a0c5df03792fd63248635181 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Thu, 27 Apr 2023 12:02:14 +0200 Subject: [PATCH] :recycle: changes on implementation. new structure described in #17 --- MCTG/MCTGApp/Program.cs | 47 +----- MCTG/MCTGLib/BaseItem.cs | 94 ------------ MCTG/MCTGLib/IDisplayable.cs | 23 --- MCTG/MCTGLib/PreparationStep.cs | 39 +++++ MCTG/MCTGLib/Recipe.cs | 106 ++++---------- MCTG/MCTGLib/RecipeCollection.cs | 242 ++++++++++++------------------- 6 files changed, 162 insertions(+), 389 deletions(-) delete mode 100644 MCTG/MCTGLib/BaseItem.cs delete mode 100644 MCTG/MCTGLib/IDisplayable.cs create mode 100644 MCTG/MCTGLib/PreparationStep.cs diff --git a/MCTG/MCTGApp/Program.cs b/MCTG/MCTGApp/Program.cs index 9196a9a..aa01b39 100644 --- a/MCTG/MCTGApp/Program.cs +++ b/MCTG/MCTGApp/Program.cs @@ -8,48 +8,5 @@ Console.WriteLine("Hello, World!\n"); // TESTS: -Console.WriteLine("tests on Recipe class:"); -BaseItem r1 = new Recipe("A recipe..."); - -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(); -} +RecipeCollection rc = new RecipeCollection(); +Console.WriteLine($"rc.Description: {rc.Description}"); \ No newline at end of file diff --git a/MCTG/MCTGLib/BaseItem.cs b/MCTG/MCTGLib/BaseItem.cs deleted file mode 100644 index 9d60520..0000000 --- a/MCTG/MCTGLib/BaseItem.cs +++ /dev/null @@ -1,94 +0,0 @@ - -using System; -using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; - -namespace MCTGLib -{ - /// - /// Define the base structure of any Items.
- /// An Item can be identifed and have a short description. It can also be displayed. - ///
- public abstract class BaseItem : IDisplayable, IEquatable - { - #region Private Attributes - - /// - /// The identifier of an Item.
- /// The first number correspond to the typs of the Item. - ///
- virtual public uint Id - { - get => _id; - init => _id = value; - } - protected uint _id; - - #endregion - - #region Public Properties - - /// - /// A short description of the Item. Useful to know what this Item stand for. - /// - 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 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 - } -} diff --git a/MCTG/MCTGLib/IDisplayable.cs b/MCTG/MCTGLib/IDisplayable.cs deleted file mode 100644 index 5de9319..0000000 --- a/MCTG/MCTGLib/IDisplayable.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace MCTGLib -{ - /// - /// Define an Item that can be displayed. - /// - interface IDisplayable - { - /// - /// Display the Id of the Item - /// - void DisplayId(); - - /// - /// Display the Description of the Item - /// - void DisplayDescription(); - - /// - /// Display the entire Item (Id, Description and other...) - /// - void DisplayItem(); - } -} diff --git a/MCTG/MCTGLib/PreparationStep.cs b/MCTG/MCTGLib/PreparationStep.cs new file mode 100644 index 0000000..13a7d7a --- /dev/null +++ b/MCTG/MCTGLib/PreparationStep.cs @@ -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 + } +} diff --git a/MCTG/MCTGLib/Recipe.cs b/MCTG/MCTGLib/Recipe.cs index 7e55030..ebbdac6 100644 --- a/MCTG/MCTGLib/Recipe.cs +++ b/MCTG/MCTGLib/Recipe.cs @@ -1,85 +1,35 @@ using System; using System.Collections.Generic; - +using System.Runtime.CompilerServices; + namespace MCTGLib { - /// - /// A Recipe is a description of step and maybe some techniques, with an ingredient list to make a meal.
- /// It is instantiated with a new unique id, where the first number is 1. - ///
- 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 - - /// - /// The title of the recipe. - /// - public string Title - { - get => title; - set - { - title = value; - if (string.IsNullOrEmpty(title)) title = $"Recipe n{this.Id}"; - } - } - - /// - /// All the details about the preparation of the meal. - /// - public string Preparation { get; set; } - - #endregion - - #region Constructors - - public Recipe(string title="", string preparation="") - : base(ComputeId(), "A recipe.") - { - Title = title; - Preparation = preparation; - } - #endregion - - #region Private Methods - - /// - /// 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. - /// - 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] new computed id: {0}", id); - - return id; - } - - #endregion - - #region Public Methods - - public override string ToString() - { - return - $"[ Class -Recipe- ]\n\n" + - $"\t.Id - {Id}\n" + - $"\t.Description - {Description}\n" + - $"______\n\n"; - } - - #endregion + public class Recipe : IEquatable + { + private static int idCreator = 0; + + public int Id { get; init; } + public string Description { get; init; } + public string Title { get; set; } + + public Recipe(string title, string description = "No Description.") + { + Id = idCreator++; + Title = title; + Description = description; + } + + public bool Equals(Recipe? other) + { + if (other == null) return false; + if (other == this) return true; + return Title.Equals(other.Title) && Description.Equals(other.Description); + } + + public override int GetHashCode() + { + return Id.GetHashCode(); + } } } diff --git a/MCTG/MCTGLib/RecipeCollection.cs b/MCTG/MCTGLib/RecipeCollection.cs index 4e799d3..95b5ff5 100644 --- a/MCTG/MCTGLib/RecipeCollection.cs +++ b/MCTG/MCTGLib/RecipeCollection.cs @@ -5,159 +5,103 @@ using System.Collections.ObjectModel; namespace MCTGLib { - /// - /// A Recipe collection is a group of recipe.
- /// It is instantiated with a new unique id, where the first number is 2. - ///
- public class RecipeCollection : BaseItem, ICollection - { - #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 _recipes = new List(); // 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(recipes); - } - - #endregion - - #region Private Methods - - /// - /// 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. - /// - 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) - throw new InvalidCastException("Need to verify the type of the private attribute '_recipes'."); - - return (this._recipes as List).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 GetEnumerator() - { - return _recipes.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return this.GetEnumerator(); - } - - #endregion + public class RecipeCollection : IList, IEquatable + { + #region Attributes + private List _recipes; #endregion - #region Operators Overloading - - /// - /// Overload the [] operator to access the Recipe directly without passing by the Collection.
- /// It use the identifier of the Recipe to get the item. - ///
- /// The id of the Recipe - /// - public Recipe this[Index id] - { - get => this.ElementAt(id); - } - - /// - /// Overload of + operator to add a Recipe to a RecipeCollection. - /// - /// The recipe collection where the recipe will be added - /// The recipe to add - /// The recipe collection where the recipe is added. - 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(); + Description = description; } - /// - /// Overload of - operator to remove a Recipe to a RecipeCollection. - /// - /// The recipe collection where the recipe will be removed - /// The recipe to remove - /// The recipe collection where the recipe is removed. - public static RecipeCollection operator - (RecipeCollection coll, Recipe ri) - { - coll.Remove(ri); return coll; - } - - #endregion + public RecipeCollection(params Recipe[] recipes) + { + _recipes = new List(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 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 } }