IEqutable

This commit is contained in:
Мельников Игорь 2022-12-06 20:59:41 +04:00
parent d18cbcd248
commit 8dd8105e4d
6 changed files with 79 additions and 3 deletions

View File

@ -32,5 +32,56 @@
} }
public string GetInfo() => _locomotive?.GetDataForSave(); public string GetInfo() => _locomotive?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.CreateDrawningLocomotive()); 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;
}
} }
} }

View File

@ -135,6 +135,11 @@ namespace Locomotives
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
} }
} }
catch (NotUniqueObjectException ex)
{
MessageBox.Show($"Ошибка добавления: {ex.Message}");
_logger.Warning($"Не удалось добавить объект: {ex.Message}");
}
catch (StorageOverflowException ex) catch (StorageOverflowException ex)
{ {
MessageBox.Show($"Ошибка добавления: {ex.Message}"); MessageBox.Show($"Ошибка добавления: {ex.Message}");

View File

@ -3,7 +3,7 @@
/// <summary> /// <summary>
/// Интерфейс для отрисовки /// Интерфейс для отрисовки
/// </summary> /// </summary>
internal interface IDrawningObject internal interface IDrawningObject : IEquatable<IDrawningObject>
{ {
/// <summary> /// <summary>
/// Шаг перемещения объекта /// Шаг перемещения объекта

View File

@ -1,7 +1,7 @@
namespace Locomotives namespace Locomotives
{ {
internal class MapWithSetLocomotivesGeneric<T, U> internal class MapWithSetLocomotivesGeneric<T, U>
where T : class, IDrawningObject where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap where U : AbstractMap
{ {
/// <summary> /// <summary>

View 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) { }
}
}

View File

@ -1,7 +1,7 @@
namespace Locomotives namespace Locomotives
{ {
internal class SetLocomotivesGeneric<T> internal class SetLocomotivesGeneric<T>
where T : class where T : class, IEquatable<T>
{ {
/// <summary> /// <summary>
/// Список объектов, которые храним /// Список объектов, которые храним
@ -28,6 +28,13 @@
/// <returns></returns> /// <returns></returns>
public int Insert(T locomotive) 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) if (_places.Count == 0)
{ {
_places.Add(locomotive); _places.Add(locomotive);