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

This commit is contained in:
Никита Чернышов 2022-12-22 21:51:04 +04:00
parent 60da240f7a
commit 9d3083ae94
4 changed files with 44 additions and 3 deletions

View File

@ -33,5 +33,32 @@ namespace AircraftCarrier
}
public string GetInfo() => _warship?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship());
public bool Equals(IDrawingObject? other)
{
if (other is not DrawingObjectWarship otherWarship)
{
return false;
}
var warship = _warship.Ship;
var otherEntity = otherWarship._warship.Ship;
if (warship.GetType() != otherEntity.GetType() ||
warship.Speed != otherEntity.Speed ||
warship.Weight != otherEntity.Weight ||
warship.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (warship is EntityPlaneWarship entityPlaneWarship &&
otherEntity is EntityPlaneWarship otherEntityPlaneWarship && (
entityPlaneWarship.BodyKit != otherEntityPlaneWarship.BodyKit ||
entityPlaneWarship.СontrolPlace != otherEntityPlaneWarship.СontrolPlace ||
entityPlaneWarship.RunWay != otherEntityPlaneWarship.RunWay ||
entityPlaneWarship.DopColor != otherEntityPlaneWarship.DopColor))
{
return false;
}
return true;
}
}
}

View File

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

View File

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

View File

@ -11,7 +11,7 @@ namespace AircraftCarrier
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetWarshipsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -53,6 +53,8 @@ namespace AircraftCarrier
/// <returns></returns>
public int Insert(T ship, int position)
{
if (_places.Contains(ship))
return -1;
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!isCorrectPosition(position))
@ -107,6 +109,18 @@ namespace AircraftCarrier
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}