Сравнение объектов

This commit is contained in:
Nikita Potapov 2022-12-25 10:42:59 +04:00
parent 681b60f6a6
commit 9387eb7e2f
4 changed files with 51 additions and 3 deletions

View File

@ -32,5 +32,48 @@ namespace Boats
}
public string GetInfo() => _boat?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectBoat(data.CreateDrawingBoat());
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var otherBoat = other as DrawingObjectBoat;
if (otherBoat == null)
{
return false;
}
var boat = _boat.Boat;
var otherBoatBoat = otherBoat._boat.Boat;
if (boat.Speed != otherBoatBoat.Speed)
{
return false;
}
if (boat.Weight != otherBoatBoat.Weight)
{
return false;
}
if (boat.BodyColor != otherBoatBoat.BodyColor)
{
return false;
}
if (boat is EntityCatamaran catamaran && otherBoatBoat is EntityCatamaran otherCatamaran)
{
if (catamaran.Sail != otherCatamaran.Sail)
{
return false;
}
if (catamaran.Bobbers != otherCatamaran.Bobbers)
{
return false;
}
if (catamaran.DopColor != otherCatamaran.DopColor)
{
return false;
}
}
return true;
}
}
}

View File

@ -9,7 +9,7 @@ namespace Boats
/// <summary>
/// Интерфейс для работы с объектом, прорисовываемым на форме
/// </summary>
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@ -12,7 +12,7 @@ namespace Boats
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetBoatsGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>

View File

@ -11,7 +11,7 @@ namespace Boats
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetBoatsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Массив объектов, которые храним
@ -56,6 +56,11 @@ namespace Boats
/// <returns></returns>
public int Insert(T boat, int position)
{
// Проверка на уникальность
if (_places.Contains(boat))
{
throw new ArgumentException($"Объект {boat} уже есть");
}
// Проверка позиции
if (position < 0 || position >= _maxCount - 1)
return -1;