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

This commit is contained in:
Semka 2022-12-12 23:46:17 +04:00
parent 7e5c3bbec2
commit aa244afca7
4 changed files with 60 additions and 3 deletions

View File

@ -38,5 +38,58 @@ namespace GasolineTanker
} }
public string GetInfo() => _Tanker?.GetDataForSave(); public string GetInfo() => _Tanker?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectTanker(data.CreateDrawningTanker()); public static IDrawningObject Create(string data) => new DrawningObjectTanker(data.CreateDrawningTanker());
public bool Equals(IDrawningObject? other)
{
{
if (other == null)
{
return false;
}
var otherTanker = other as DrawningObjectTanker;
if (otherTanker == null)
{
return false;
}
var tanker = _Tanker.Tanker;
var otherTankerTanker = otherTanker._Tanker.Tanker;
if (tanker.GetType().Name != otherTankerTanker.GetType().Name)
{
return false;
}
if (tanker.Speed != otherTankerTanker.Speed)
{
return false;
}
if (tanker.Weight != otherTankerTanker.Weight)
{
return false;
}
if (tanker.BodyColor != otherTankerTanker.BodyColor)
{
return false;
}
if (tanker is EntityGasolineTanker gasolinetanker && otherTankerTanker is EntityGasolineTanker otherGasolineTanker)
{
if (gasolinetanker.DopColor != otherGasolineTanker.DopColor)
{
return false;
}
if (gasolinetanker.Cabin != otherGasolineTanker.Cabin)
{
return false;
}
if (gasolinetanker.Signal != otherGasolineTanker.Signal)
{
return false;
}
if (gasolinetanker.BenzoBack != otherGasolineTanker.BenzoBack)
{
return false;
}
}
return true;
}
}
} }
} }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace GasolineTanker namespace GasolineTanker
{ {
internal interface IDrawningObject internal interface IDrawningObject : IEquatable<IDrawningObject>
{ {
/// <summary> /// <summary>
/// Шаг перемещения объекта /// Шаг перемещения объекта

View File

@ -1,7 +1,7 @@
namespace GasolineTanker namespace GasolineTanker
{ {
internal class MapWithSetTankersGeneric<T, U> internal class MapWithSetTankersGeneric<T, U>
where T : class, IDrawningObject where T : class, IDrawningObject,IEquatable<T>
where U : AbstractMap where U : AbstractMap
{ {
private readonly int _pictureWidth; private readonly int _pictureWidth;

View File

@ -1,7 +1,7 @@
namespace GasolineTanker namespace GasolineTanker
{ {
internal class SetTankersGeneric<T> internal class SetTankersGeneric<T>
where T : class where T : class, IEquatable<T>
{ {
private readonly List<T> _places; private readonly List<T> _places;
public int Count => _places.Count; public int Count => _places.Count;
@ -20,6 +20,10 @@
public int Insert(T tanker, int position) public int Insert(T tanker, int position)
{ {
if (_places.Contains(tanker))
{
return -1;
}
if (Count == _maxCount) if (Count == _maxCount)
{ {
throw new StorageOverflowException(_maxCount); throw new StorageOverflowException(_maxCount);