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

This commit is contained in:
Марат Заргаров 2022-12-20 20:27:16 +04:00
parent ef6b1d2ccf
commit 11ba50c912
4 changed files with 65 additions and 3 deletions

View File

@ -36,5 +36,61 @@ namespace DoubleDeckerBus
{ {
return new DrawningObjectBus(data.CreateDrawningBus()); return new DrawningObjectBus(data.CreateDrawningBus());
} }
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherBus = other as DrawningObjectBus;
if (otherBus == null)
{
return false;
}
var bus = _bus.Bus;
var otherBusBus = otherBus._bus.Bus;
if (bus.GetType().Name != otherBusBus.GetType().Name)
{
return false;
}
if (bus.Speed != otherBusBus.Speed)
{
return false;
}
if (bus.Weight != otherBusBus.Weight)
{
return false;
}
if (bus.BodyColor != otherBusBus.BodyColor)
{
return false;
}
if (bus is EntityDoubleDeckerBus DoubleDeckerBus && otherBusBus is EntityDoubleDeckerBus otherDoubleDeckerBus)
{
if (DoubleDeckerBus.Stair != otherDoubleDeckerBus.Stair)
{
return false;
}
if (DoubleDeckerBus.SecondFloor != otherDoubleDeckerBus.SecondFloor)
{
return false;
}
if (DoubleDeckerBus.BodyKit != otherDoubleDeckerBus.BodyKit)
{
return false;
}
if (DoubleDeckerBus.DopColor != otherDoubleDeckerBus.DopColor)
{
return false;
}
}
return true;
}
} }
} }

View File

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

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace DoubleDeckerBus namespace DoubleDeckerBus
{ {
internal class MapWithSetBusesGeneric<T,U> internal class MapWithSetBusesGeneric<T,U>
where T : class, IDrawningObject where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap where U : AbstractMap
{ {
/// <summary> /// <summary>

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace DoubleDeckerBus namespace DoubleDeckerBus
{ {
internal class SetBusesGeneric<T> internal class SetBusesGeneric<T>
where T : class where T : class, IEquatable<T>
{ {
/// <summary> /// <summary>
/// Список объектов, которые храним /// Список объектов, которые храним
@ -45,11 +45,17 @@ namespace DoubleDeckerBus
/// <returns></returns> /// <returns></returns>
public int Insert(T bus, int position) public int Insert(T bus, int position)
{ {
if (_places.Contains(bus))
{
throw new ArgumentException("Такой автобус уже существует");
}
if (position < 0 || position >= _maxCount) if (position < 0 || position >= _maxCount)
{ {
throw new BusNotFoundException("Место указано неверно"); throw new BusNotFoundException("Место указано неверно");
} }
BusyPlaces++; BusyPlaces++;
_places.Insert(position, bus); _places.Insert(position, bus);
return position; return position;