🐛 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>
</PropertyGroup>
<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MCTGLib\MCTGLib.csproj" />
</ItemGroup>

@ -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(" - ");
}

@ -1,6 +1,7 @@

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
namespace Model
{
@ -10,6 +11,8 @@ namespace Model
/// </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.
@ -20,19 +23,28 @@ namespace Model
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()
{
@ -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<BaseItem> implementation
public bool Equals(BaseItem? other)
{
if (other != null)
return this.Id.Equals(other.Id);
#endregion
return false;
}
#region IEquatable<BaseItem> 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
}
}

@ -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
/// </summary>
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;
}

@ -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
/// </summary>
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<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));
return (this._recipes as List<Recipe>).Find(x => x.Id.Equals(id));
}
#region ICollection Implementation

Loading…
Cancel
Save