🐛 fix: ComputeId now compute correct ids

pull/24/head
Alexandre Agostinho 2 years ago
parent fa25dbf757
commit e68a9ac48d

@ -7,10 +7,6 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MCTGLib\MCTGLib.csproj" /> <ProjectReference Include="..\MCTGLib\MCTGLib.csproj" />
</ItemGroup> </ItemGroup>

@ -8,7 +8,7 @@ Console.WriteLine("Hello, World!\n");
// TESTS: // TESTS:
// tests on Recipe class Console.WriteLine("tests on Recipe class:");
BaseItem r1 = new Recipe("A recipe..."); BaseItem r1 = new Recipe("A recipe...");
r1.DisplayItem(); r1.DisplayItem();
@ -17,24 +17,30 @@ r1.DisplayDescription();
Console.WriteLine(); Console.WriteLine();
// tests on RecipeCollection class Console.WriteLine("tests on RecipeCollection class:");
RecipeCollection rc = new RecipeCollection("A recipe collection..."); RecipeCollection rc = new RecipeCollection("A recipe collection...");
for (uint i = 0; i < 10; i++) 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[2].DisplayId();
rc.GetById(1002); rc.GetById(14).DisplayId();
} }
catch (ArgumentNullException) catch (ArgumentNullException)
{ {
Console.Error.WriteLine("An index are incorrect!\n"); 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( RecipeCollection rc2 = new RecipeCollection(
new Recipe(), new Recipe(),
new Recipe(), new Recipe(),
@ -42,9 +48,8 @@ RecipeCollection rc2 = new RecipeCollection(
new Recipe(), new Recipe(),
new Recipe()); new Recipe());
// test of Enumerable property Console.WriteLine("test of Enumerable property:");
foreach (Recipe r in rc2) foreach (Recipe r in rc2)
{ {
r.DisplayId(); r.DisplayId(); Console.Write(" - ");
r.DisplayDescription();
} }

@ -1,6 +1,7 @@
 
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
namespace Model namespace Model
{ {
@ -10,6 +11,8 @@ namespace Model
/// </summary> /// </summary>
public abstract class BaseItem : IDisplayable, IEquatable<BaseItem> public abstract class BaseItem : IDisplayable, IEquatable<BaseItem>
{ {
#region Private Attributes
/// <summary> /// <summary>
/// The identifier of an Item.<br/> /// The identifier of an Item.<br/>
/// The first number correspond to the typs of the Item. /// The first number correspond to the typs of the Item.
@ -20,19 +23,28 @@ namespace Model
init => _id = value; init => _id = value;
} }
protected uint _id; protected uint _id;
#endregion
#region Public Properties
/// <summary> /// <summary>
/// A short description of the Item. Useful to know what this Item stand for. /// A short description of the Item. Useful to know what this Item stand for.
/// </summary> /// </summary>
public string Description { get; set; } public string Description { get; set; }
#endregion
#region Constructors
protected BaseItem(uint id, string description="Any Item.") protected BaseItem(uint id, string description="Any Item.")
{ {
Id = id; Description = description; Id = id; Description = description;
} }
#endregion
#region Public Methods
public override string ToString() public override string ToString()
{ {
@ -43,7 +55,8 @@ namespace Model
$"______\n\n"; $"______\n\n";
} }
// IDisplayable Implementation #region IDisplayable Implementation
public void DisplayId() public void DisplayId()
{ {
Console.WriteLine($".Id - {Id}"); Console.WriteLine($".Id - {Id}");
@ -59,27 +72,23 @@ namespace Model
Console.WriteLine(this.ToString()); Console.WriteLine(this.ToString());
} }
// IEquatable<BaseItem> implementation #endregion
public bool Equals(BaseItem? other)
{
if (other != null)
return this.Id.Equals(other.Id);
return false; #region IEquatable<BaseItem> implementation
}
public override bool Equals(object? obj) public int GetHashCode([DisallowNull] BaseItem obj)
{ {
BaseItem? baseItem = obj as BaseItem; return obj.Id.GetHashCode() + obj.Description.GetHashCode();
if (baseItem == null) return false;
if (baseItem == this) return true;
return this.Id.Equals(baseItem.Id);
} }
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
} }
} }

@ -11,6 +11,7 @@ namespace Model
{ {
#region Private Attributes #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 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 = ""; private string title = "";
@ -57,9 +58,11 @@ namespace Model
/// </summary> /// </summary>
private static uint ComputeId() private static uint ComputeId()
{ {
uint dec = 0, id = _idCreator; uint dec = 1;
while ((_idCreator / (Math.Pow(10, dec)) > 10)) dec++; while ((_idCreator / (Math.Pow(10, dec)) >= 1)) dec++;
id += 1 * (uint)(Math.Pow(10, dec));
uint id = (CAT_ITEM * (uint)(Math.Pow(10, dec))) + _idCreator++;
Console.WriteLine("[recipe] new computed id: {0}", id);
return id; return id;
} }

@ -19,14 +19,15 @@ namespace Model
#endregion #endregion
#region Public Properties #region Public Properties
#region ICollection Implementation #region ICollection Implementation
public int Count => _recipes.Count; public int Count => _recipes.Count;
public bool IsReadOnly => false; public bool IsReadOnly => false;
#endregion #endregion
#endregion #endregion
#region Constructors #region Constructors
@ -51,9 +52,11 @@ namespace Model
/// </summary> /// </summary>
private static uint ComputeId() private static uint ComputeId()
{ {
uint dec = 0, id = _idCreator; uint dec = 1;
while ((_idCreator / (Math.Pow(10, dec)) > 10)) dec++; while ((_idCreator / (Math.Pow(10, dec)) >= 1)) dec++;
id += CAT_ITEM * (uint)(Math.Pow(10, dec));
uint id = (CAT_ITEM * (uint)(Math.Pow(10, dec))) + _idCreator++;
Console.WriteLine("[recipe collection] new computed id: {0}", id);
return id; return id;
} }
@ -76,7 +79,7 @@ namespace Model
if (this._recipes is not List<Recipe>) if (this._recipes is not List<Recipe>)
throw new InvalidCastException("Need to verify the type of the private attribute '_recipes'."); 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)); return (this._recipes as List<Recipe>).Find(x => x.Id.Equals(id));
} }
#region ICollection Implementation #region ICollection Implementation

Loading…
Cancel
Save