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

This commit is contained in:
Дамир Нугаев 2022-12-07 00:38:37 +04:00
parent 52eaba7622
commit 3ac973ae9d
3 changed files with 40 additions and 14 deletions

View File

@ -39,6 +39,31 @@ namespace Bus
public string GetInfo() => _bus?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectBus(data.CreateDrawingBus());
public bool Equals(IDrawingObject? other)
{
if (other is not DrawingObjectBus otherBus)
{
return false;
}
var entity = Bus.Bus;
var otherEntity = otherBus.Bus.Bus;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntitySportBus entityWarmlyShip &&
otherEntity is EntitySportBus otherEntityWarmlyShip && (
entityWarmlyShip.Wing != otherEntityWarmlyShip.Wing ||
entityWarmlyShip.DopColor != otherEntityWarmlyShip.DopColor ||
entityWarmlyShip.Sportline != otherEntityWarmlyShip.Sportline))
{
return false;
}
return true;
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Bus
{
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);

View File

@ -6,8 +6,9 @@ using System.Threading.Tasks;
namespace Bus
{
internal class SetDoubleDeckerBusGeneric<T>
where T : class
internal class SetDoubleDeckerBusGeneric<T, U>
where T : class, IEquatable<T>, IDrawingObject
where U : AbstractMap
{
private readonly List<T> _places;
public int Count => _places.Count;
@ -25,21 +26,21 @@ namespace Bus
{
return Insert(bus, 0);
}
private bool isCorrectPosition(int position)
{
return 0 <= position && position < _maxCount;
}
public int Insert(T bus, int position)
{
if (position > _maxCount && position < 0)
if (_places.Contains(bus))
throw new ArgumentException($"Объект {bus} уже есть в наборе");
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!isCorrectPosition(position))
{
return -1;
}
if (_places.Contains(bus))
{
throw new ArgumentException($"Объект {bus} уже есть в наборе");
}
if (Count == _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
_places.Insert(position, bus);
return position;
}