From e68a9ac48dfd84dc3dc35f38703c3387a376c785 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Mon, 10 Apr 2023 01:21:36 +0200 Subject: [PATCH] :bug: fix: ComputeId now compute correct ids --- MCTG/MCTGApp/MCTGApp.csproj | 4 --- MCTG/MCTGApp/Program.cs | 23 +++++++++------- MCTG/MCTGLib/BaseItem.cs | 45 +++++++++++++++++++------------- MCTG/MCTGLib/Recipe.cs | 9 ++++--- MCTG/MCTGLib/RecipeCollection.cs | 13 +++++---- 5 files changed, 55 insertions(+), 39 deletions(-) diff --git a/MCTG/MCTGApp/MCTGApp.csproj b/MCTG/MCTGApp/MCTGApp.csproj index 79a52db..f184388 100644 --- a/MCTG/MCTGApp/MCTGApp.csproj +++ b/MCTG/MCTGApp/MCTGApp.csproj @@ -7,10 +7,6 @@ enable - - - - diff --git a/MCTG/MCTGApp/Program.cs b/MCTG/MCTGApp/Program.cs index 021e938..57a3a18 100644 --- a/MCTG/MCTGApp/Program.cs +++ b/MCTG/MCTGApp/Program.cs @@ -8,7 +8,7 @@ Console.WriteLine("Hello, World!\n"); // TESTS: -// tests on Recipe class +Console.WriteLine("tests on Recipe class:"); BaseItem r1 = new Recipe("A recipe..."); r1.DisplayItem(); @@ -17,24 +17,30 @@ r1.DisplayDescription(); Console.WriteLine(); -// tests on RecipeCollection class +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."); // test overload + operator + rc += new Recipe($"Recipe number {i} in the collection."); + Console.WriteLine("test overload + operator"); } -try // test overload of [] operator +Console.WriteLine("test overload of [] operator:"); +try { rc[2].DisplayId(); - rc.GetById(1002); + rc.GetById(14).DisplayId(); } catch (ArgumentNullException) { Console.Error.WriteLine("An index are incorrect!\n"); } +catch (Exception e) +{ + Console.Error.WriteLine("Exception thrown: {0}", e); +} -// test of multiple params constructor +Console.WriteLine("test of multiple params constructor:"); RecipeCollection rc2 = new RecipeCollection( new Recipe(), new Recipe(), @@ -42,9 +48,8 @@ RecipeCollection rc2 = new RecipeCollection( new Recipe(), new Recipe()); -// test of Enumerable property +Console.WriteLine("test of Enumerable property:"); foreach (Recipe r in rc2) { - r.DisplayId(); - r.DisplayDescription(); + r.DisplayId(); Console.Write(" - "); } diff --git a/MCTG/MCTGLib/BaseItem.cs b/MCTG/MCTGLib/BaseItem.cs index f031d66..8d8cd81 100644 --- a/MCTG/MCTGLib/BaseItem.cs +++ b/MCTG/MCTGLib/BaseItem.cs @@ -1,6 +1,7 @@  using System; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; namespace Model { @@ -10,6 +11,8 @@ namespace Model /// public abstract class BaseItem : IDisplayable, IEquatable { + #region Private Attributes + /// /// The identifier of an Item.
/// The first number correspond to the typs of the Item. @@ -20,19 +23,28 @@ namespace Model 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() { @@ -43,7 +55,8 @@ namespace Model $"______\n\n"; } - // IDisplayable Implementation + #region IDisplayable Implementation + public void DisplayId() { Console.WriteLine($".Id - {Id}"); @@ -59,27 +72,23 @@ namespace Model Console.WriteLine(this.ToString()); } - // IEquatable implementation - public bool Equals(BaseItem? other) - { - if (other != null) - return this.Id.Equals(other.Id); + #endregion - return false; - } + #region IEquatable implementation - public override bool Equals(object? obj) + public int GetHashCode([DisallowNull] BaseItem obj) { - BaseItem? baseItem = obj as BaseItem; - if (baseItem == null) return false; - if (baseItem == this) return true; - - return this.Id.Equals(baseItem.Id); + return obj.Id.GetHashCode() + obj.Description.GetHashCode(); } - public override int GetHashCode() + public bool Equals(BaseItem? other) { - return this.Id.GetHashCode(); + if (other == null) return false; + return (this.Id == other.Id) && (this.Description == other.Description); } + + #endregion + + #endregion } } diff --git a/MCTG/MCTGLib/Recipe.cs b/MCTG/MCTGLib/Recipe.cs index 2ab5420..f5cb00d 100644 --- a/MCTG/MCTGLib/Recipe.cs +++ b/MCTG/MCTGLib/Recipe.cs @@ -11,6 +11,7 @@ namespace Model { #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 = ""; @@ -57,9 +58,11 @@ namespace Model ///
private static uint ComputeId() { - uint dec = 0, id = _idCreator; - while ((_idCreator / (Math.Pow(10, dec)) > 10)) dec++; - id += 1 * (uint)(Math.Pow(10, dec)); + 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; } diff --git a/MCTG/MCTGLib/RecipeCollection.cs b/MCTG/MCTGLib/RecipeCollection.cs index f93227d..c9ed6be 100644 --- a/MCTG/MCTGLib/RecipeCollection.cs +++ b/MCTG/MCTGLib/RecipeCollection.cs @@ -19,14 +19,15 @@ namespace Model #endregion - #region Public Properties + #region ICollection Implementation public int Count => _recipes.Count; public bool IsReadOnly => false; #endregion + #endregion #region Constructors @@ -51,9 +52,11 @@ namespace Model /// 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)); + 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; } @@ -76,7 +79,7 @@ namespace Model 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)); + return (this._recipes as List).Find(x => x.Id.Equals(id)); } #region ICollection Implementation