IEqutable
This commit is contained in:
parent
d18cbcd248
commit
8dd8105e4d
@ -32,5 +32,56 @@
|
||||
}
|
||||
public string GetInfo() => _locomotive?.GetDataForSave();
|
||||
public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningLocomotive());
|
||||
|
||||
public bool Equals(IDrawningObject? other)
|
||||
{
|
||||
if (other == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var otherLocomotive = other as DrawningObjectLocomotive;
|
||||
if (otherLocomotive == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var locomotive = _locomotive.Locomotive;
|
||||
var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive;
|
||||
if (locomotive.Speed != otherLocomotiveLocomotive.Speed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (locomotive.Weight != otherLocomotiveLocomotive.Weight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (locomotive.BodyColor != otherLocomotiveLocomotive.BodyColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (locomotive is EntityWarmlyLocomotive && otherLocomotiveLocomotive is not EntityWarmlyLocomotive)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (locomotive is not EntityWarmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (locomotive is EntityWarmlyLocomotive warmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive otherWarmlyLocomotive)
|
||||
{
|
||||
if (warmlyLocomotive.AdditionalColor != otherWarmlyLocomotive.AdditionalColor)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (warmlyLocomotive.HasPipe != otherWarmlyLocomotive.HasPipe)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (warmlyLocomotive.HasFuelTank != otherWarmlyLocomotive.HasFuelTank)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -135,6 +135,11 @@ namespace Locomotives
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
catch (NotUniqueObjectException ex)
|
||||
{
|
||||
MessageBox.Show($"Ошибка добавления: {ex.Message}");
|
||||
_logger.Warning($"Не удалось добавить объект: {ex.Message}");
|
||||
}
|
||||
catch (StorageOverflowException ex)
|
||||
{
|
||||
MessageBox.Show($"Ошибка добавления: {ex.Message}");
|
||||
|
@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// Интерфейс для отрисовки
|
||||
/// </summary>
|
||||
internal interface IDrawningObject
|
||||
internal interface IDrawningObject : IEquatable<IDrawningObject>
|
||||
{
|
||||
/// <summary>
|
||||
/// Шаг перемещения объекта
|
||||
|
@ -1,7 +1,7 @@
|
||||
namespace Locomotives
|
||||
{
|
||||
internal class MapWithSetLocomotivesGeneric<T, U>
|
||||
where T : class, IDrawningObject
|
||||
where T : class, IDrawningObject, IEquatable<T>
|
||||
where U : AbstractMap
|
||||
{
|
||||
/// <summary>
|
||||
|
13
Locomotives/Locomotives/NotUniqueObjectException.cs
Normal file
13
Locomotives/Locomotives/NotUniqueObjectException.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Locomotives
|
||||
{
|
||||
[Serializable]
|
||||
internal class NotUniqueObjectException : ApplicationException
|
||||
{
|
||||
public NotUniqueObjectException() : base("Такой объект уже есть в коллекции") { }
|
||||
public NotUniqueObjectException(string message) : base(message) { }
|
||||
public NotUniqueObjectException(string message, Exception Exception) : base(message, Exception) { }
|
||||
protected NotUniqueObjectException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
namespace Locomotives
|
||||
{
|
||||
internal class SetLocomotivesGeneric<T>
|
||||
where T : class
|
||||
where T : class, IEquatable<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Список объектов, которые храним
|
||||
@ -28,6 +28,13 @@
|
||||
/// <returns></returns>
|
||||
public int Insert(T locomotive)
|
||||
{
|
||||
for (int i = 0; i < _places.Count; i++)
|
||||
{
|
||||
if (locomotive.Equals(_places[i]))
|
||||
{
|
||||
throw new NotUniqueObjectException();
|
||||
}
|
||||
}
|
||||
if (_places.Count == 0)
|
||||
{
|
||||
_places.Add(locomotive);
|
||||
|
Loading…
Reference in New Issue
Block a user