diff --git a/Bus/Bus/DrawingObjectBus.cs b/Bus/Bus/DrawingObjectBus.cs index a3b541d..9b35624 100644 --- a/Bus/Bus/DrawingObjectBus.cs +++ b/Bus/Bus/DrawingObjectBus.cs @@ -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; + } } } diff --git a/Bus/Bus/IDrawingObject.cs b/Bus/Bus/IDrawingObject.cs index 321b39f..f41e4b4 100644 --- a/Bus/Bus/IDrawingObject.cs +++ b/Bus/Bus/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Bus { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/Bus/Bus/SetDoubleDeckerBusGeneric.cs b/Bus/Bus/SetDoubleDeckerBusGeneric.cs index cae1e16..46fa0ba 100644 --- a/Bus/Bus/SetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/SetDoubleDeckerBusGeneric.cs @@ -6,8 +6,9 @@ using System.Threading.Tasks; namespace Bus { - internal class SetDoubleDeckerBusGeneric - where T : class + internal class SetDoubleDeckerBusGeneric + where T : class, IEquatable, IDrawingObject + where U : AbstractMap { private readonly List _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; }