From 8dd8105e4d8831f5c2e3c6a9a929e77d56b5bae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Tue, 6 Dec 2022 20:59:41 +0400 Subject: [PATCH] IEqutable --- .../Locomotives/DrawningObjectLocomotive.cs | 51 +++++++++++++++++++ .../Locomotives/FormMapWithSetLocomotives.cs | 5 ++ Locomotives/Locomotives/IDrawningObject.cs | 2 +- .../MapWithSetLocomotivesGeneric.cs | 2 +- .../Locomotives/NotUniqueObjectException.cs | 13 +++++ .../Locomotives/SetLocomotivesGeneric.cs | 9 +++- 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 Locomotives/Locomotives/NotUniqueObjectException.cs 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);