diff --git a/Locomotives/Locomotives/DrawningObjectLocomotive.cs b/Locomotives/Locomotives/DrawningObjectLocomotive.cs
index b92129e..b299bc8 100644
--- a/Locomotives/Locomotives/DrawningObjectLocomotive.cs
+++ b/Locomotives/Locomotives/DrawningObjectLocomotive.cs
@@ -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;
+ }
}
}
diff --git a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs
index a28f5a6..d498e08 100644
--- a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs
+++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs
@@ -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}");
diff --git a/Locomotives/Locomotives/IDrawningObject.cs b/Locomotives/Locomotives/IDrawningObject.cs
index 2c9c833..364a6e5 100644
--- a/Locomotives/Locomotives/IDrawningObject.cs
+++ b/Locomotives/Locomotives/IDrawningObject.cs
@@ -3,7 +3,7 @@
///
/// Интерфейс для отрисовки
///
- internal interface IDrawningObject
+ internal interface IDrawningObject : IEquatable
{
///
/// Шаг перемещения объекта
diff --git a/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs b/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs
index 4c9af41..75679a0 100644
--- a/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs
+++ b/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs
@@ -1,7 +1,7 @@
namespace Locomotives
{
internal class MapWithSetLocomotivesGeneric
- where T : class, IDrawningObject
+ where T : class, IDrawningObject, IEquatable
where U : AbstractMap
{
///
diff --git a/Locomotives/Locomotives/NotUniqueObjectException.cs b/Locomotives/Locomotives/NotUniqueObjectException.cs
new file mode 100644
index 0000000..163bf7c
--- /dev/null
+++ b/Locomotives/Locomotives/NotUniqueObjectException.cs
@@ -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) { }
+ }
+}
diff --git a/Locomotives/Locomotives/SetLocomotivesGeneric.cs b/Locomotives/Locomotives/SetLocomotivesGeneric.cs
index 987227c..dafe755 100644
--- a/Locomotives/Locomotives/SetLocomotivesGeneric.cs
+++ b/Locomotives/Locomotives/SetLocomotivesGeneric.cs
@@ -1,7 +1,7 @@
namespace Locomotives
{
internal class SetLocomotivesGeneric
- where T : class
+ where T : class, IEquatable
{
///
/// Список объектов, которые храним
@@ -28,6 +28,13 @@
///
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);