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 1/7] 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); -- 2.25.1 From 86f03f05c5978ba752d6e830dad8f324165b73c8 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 21:21:18 +0400 Subject: [PATCH 2/7] =?UTF-8?q?=D0=98=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=D0=B0=20List.Contains?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotives/Locomotives/SetLocomotivesGeneric.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Locomotives/Locomotives/SetLocomotivesGeneric.cs b/Locomotives/Locomotives/SetLocomotivesGeneric.cs index dafe755..fe6a69d 100644 --- a/Locomotives/Locomotives/SetLocomotivesGeneric.cs +++ b/Locomotives/Locomotives/SetLocomotivesGeneric.cs @@ -28,12 +28,9 @@ /// public int Insert(T locomotive) { - for (int i = 0; i < _places.Count; i++) + if (_places.Contains(locomotive)) { - if (locomotive.Equals(_places[i])) - { - throw new NotUniqueObjectException(); - } + throw new NotUniqueObjectException(); } if (_places.Count == 0) { -- 2.25.1 From f866ee102458aed7c9cf8917332a1daa588d9bf7 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 22:11:56 +0400 Subject: [PATCH 3/7] =?UTF-8?q?=D0=A1=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Locomotives/DrawningObjectLocomotive.cs | 3 ++ .../FormMapWithSetLocomotives.Designer.cs | 36 ++++++++++++-- .../Locomotives/FormMapWithSetLocomotives.cs | 28 +++++++++++ .../Locomotives/LocomotiveCompareByColor.cs | 38 ++++++++++++++ .../Locomotives/LocomotiveCompareByType.cs | 49 +++++++++++++++++++ .../MapWithSetLocomotivesGeneric.cs | 4 ++ .../Locomotives/SetLocomotivesGeneric.cs | 8 +++ 7 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 Locomotives/Locomotives/LocomotiveCompareByColor.cs create mode 100644 Locomotives/Locomotives/LocomotiveCompareByType.cs diff --git a/Locomotives/Locomotives/DrawningObjectLocomotive.cs b/Locomotives/Locomotives/DrawningObjectLocomotive.cs index b299bc8..1c6086e 100644 --- a/Locomotives/Locomotives/DrawningObjectLocomotive.cs +++ b/Locomotives/Locomotives/DrawningObjectLocomotive.cs @@ -9,6 +9,9 @@ /// Объект от класса отрисовки локомотива /// private DrawningLocomotive _locomotive = null; + + public DrawningLocomotive GetLocomotive => _locomotive; + public DrawningObjectLocomotive(DrawningLocomotive locomotive) { _locomotive = locomotive; diff --git a/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs index 5126946..54f22f0 100644 --- a/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs +++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonDeleteMap = new System.Windows.Forms.Button(); this.listBoxMaps = new System.Windows.Forms.ListBox(); @@ -59,6 +61,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Controls.Add(this.buttonDown); @@ -77,6 +81,26 @@ this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(31, 341); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(164, 23); + this.buttonSortByColor.TabIndex = 3; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(31, 312); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(164, 23); + this.buttonSortByType.TabIndex = 3; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBoxMaps // this.groupBoxMaps.Anchor = System.Windows.Forms.AnchorStyles.Right; @@ -192,7 +216,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(31, 426); + this.buttonShowOnMap.Location = new System.Drawing.Point(31, 526); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(164, 26); this.buttonShowOnMap.TabIndex = 5; @@ -202,7 +226,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(31, 394); + this.buttonShowStorage.Location = new System.Drawing.Point(31, 494); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(164, 26); this.buttonShowStorage.TabIndex = 4; @@ -212,7 +236,7 @@ // // buttonRemoveLocomotive // - this.buttonRemoveLocomotive.Location = new System.Drawing.Point(31, 362); + this.buttonRemoveLocomotive.Location = new System.Drawing.Point(31, 462); this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; this.buttonRemoveLocomotive.Size = new System.Drawing.Size(164, 26); this.buttonRemoveLocomotive.TabIndex = 3; @@ -222,7 +246,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(31, 333); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(31, 433); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(164, 23); @@ -230,7 +254,7 @@ // // buttonAddCar // - this.buttonAddCar.Location = new System.Drawing.Point(31, 291); + this.buttonAddCar.Location = new System.Drawing.Point(31, 391); this.buttonAddCar.Name = "buttonAddCar"; this.buttonAddCar.Size = new System.Drawing.Size(164, 26); this.buttonAddCar.TabIndex = 1; @@ -336,5 +360,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs index d498e08..104d974 100644 --- a/Locomotives/Locomotives/FormMapWithSetLocomotives.cs +++ b/Locomotives/Locomotives/FormMapWithSetLocomotives.cs @@ -301,5 +301,33 @@ namespace Locomotives } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new LocomotiveCompareByType()); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new LocomotiveCompareByColor()); + pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/Locomotives/Locomotives/LocomotiveCompareByColor.cs b/Locomotives/Locomotives/LocomotiveCompareByColor.cs new file mode 100644 index 0000000..3e807a3 --- /dev/null +++ b/Locomotives/Locomotives/LocomotiveCompareByColor.cs @@ -0,0 +1,38 @@ +namespace Locomotives +{ + internal class LocomotiveCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xLocomotive = x as DrawningObjectLocomotive; + var yLocomotive = y as DrawningObjectLocomotive; + if (xLocomotive == null && yLocomotive == null) + { + return 0; + } + if (xLocomotive == null && yLocomotive != null) + { + return 1; + } + if (xLocomotive != null && yLocomotive == null) + { + return -1; + } + var xColorName = xLocomotive.GetLocomotive.Locomotive.BodyColor.Name; + var yColorName = yLocomotive.GetLocomotive.Locomotive.BodyColor.Name; + return xColorName.CompareTo(yColorName); + } + } +} diff --git a/Locomotives/Locomotives/LocomotiveCompareByType.cs b/Locomotives/Locomotives/LocomotiveCompareByType.cs new file mode 100644 index 0000000..9aa24ec --- /dev/null +++ b/Locomotives/Locomotives/LocomotiveCompareByType.cs @@ -0,0 +1,49 @@ +namespace Locomotives +{ + internal class LocomotiveCompareByType : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xLocomotive = x as DrawningObjectLocomotive; + var yLocomotive = y as DrawningObjectLocomotive; + if (xLocomotive == null && yLocomotive == null) + { + return 0; + } + if (xLocomotive == null && yLocomotive != null) + { + return 1; + } + if (xLocomotive != null && yLocomotive == null) + { + return -1; + } + if (xLocomotive?.GetLocomotive.GetType().Name != yLocomotive?.GetLocomotive.GetType().Name) + { + if (xLocomotive.GetLocomotive.GetType().Name == "DrawningLocomotive") + { + return -1; + } + return 1; + } + var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight); + } + } +} diff --git a/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs b/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs index 75679a0..732ddd8 100644 --- a/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs +++ b/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs @@ -201,5 +201,9 @@ _setLocomotives.Insert(DrawningObjectLocomotive.Create(record) as T); } } + public void Sort(IComparer comparer) + { + _setLocomotives.SortSet(comparer); + } } } diff --git a/Locomotives/Locomotives/SetLocomotivesGeneric.cs b/Locomotives/Locomotives/SetLocomotivesGeneric.cs index fe6a69d..fd289af 100644 --- a/Locomotives/Locomotives/SetLocomotivesGeneric.cs +++ b/Locomotives/Locomotives/SetLocomotivesGeneric.cs @@ -123,5 +123,13 @@ } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } -- 2.25.1 From 1bec916bb172304ba64b933024aec789e51792f5 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: Thu, 8 Dec 2022 18:19:03 +0400 Subject: [PATCH 4/7] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20IEquatable=20=D0=B4=D0=BB=D1=8F=20Abstract?= =?UTF-8?q?Map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotives/Locomotives/AbstractMap.cs | 34 ++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Locomotives/Locomotives/AbstractMap.cs b/Locomotives/Locomotives/AbstractMap.cs index 2708099..0aff92b 100644 --- a/Locomotives/Locomotives/AbstractMap.cs +++ b/Locomotives/Locomotives/AbstractMap.cs @@ -1,6 +1,6 @@ namespace Locomotives { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { /// /// Поле от интерфейса прорисовки @@ -60,7 +60,7 @@ /// public (int Top, int Bottom, int Left, int Right) GetObjectCoordinates() { - return + return ( (int)(_drawningObject.GetCurrentPosition().Top / _size_y), (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y), @@ -239,5 +239,35 @@ /// /// protected abstract void DrawBarrierPart(Graphics g, int i, int j); + /// + /// Реализация сравнения + /// + /// + /// + public bool Equals(AbstractMap? other) + { + if (other == null) + { + return false; + } + if (_map.GetLength(0) == other._map.GetLength(0) && _map.GetLength(1) == other._map.GetLength(1)) + { + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] != other._map[i, j]) + { + return false; + } + } + } + } + else + { + return false; + } + return true; + } } } -- 2.25.1 From a28fb7a3d06febb9f3e0e61a992e84d47810578a 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: Thu, 8 Dec 2022 18:40:09 +0400 Subject: [PATCH 5/7] =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=B4?= =?UTF-8?q?=D0=B0,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=D1=80=D0=B8=D0=B5=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Locomotives/DrawningObjectLocomotive.cs | 21 ++++++++++++------- .../Locomotives/LocomotiveCompareByColor.cs | 9 ++++++-- .../Locomotives/LocomotiveCompareByType.cs | 11 ++++++---- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Locomotives/Locomotives/DrawningObjectLocomotive.cs b/Locomotives/Locomotives/DrawningObjectLocomotive.cs index 1c6086e..ddba1ce 100644 --- a/Locomotives/Locomotives/DrawningObjectLocomotive.cs +++ b/Locomotives/Locomotives/DrawningObjectLocomotive.cs @@ -1,17 +1,14 @@ namespace Locomotives { -/// -/// Класс-наследник от интерфейса (реализация) -/// + /// + /// Класс-наследник от интерфейса (реализация) + /// internal class DrawningObjectLocomotive : IDrawningObject { /// /// Объект от класса отрисовки локомотива /// - private DrawningLocomotive _locomotive = null; - - public DrawningLocomotive GetLocomotive => _locomotive; - + public DrawningLocomotive _locomotive { get; set; } public DrawningObjectLocomotive(DrawningLocomotive locomotive) { _locomotive = locomotive; @@ -35,9 +32,14 @@ } 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; @@ -49,6 +51,7 @@ } var locomotive = _locomotive.Locomotive; var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive; + //проверка характеристик базовой сущности if (locomotive.Speed != otherLocomotiveLocomotive.Speed) { return false; @@ -61,6 +64,7 @@ { return false; } + //проверка на одинаковость типов первого и второго объекта (не является ли один из них наследником, а другой базовым классом) if (locomotive is EntityWarmlyLocomotive && otherLocomotiveLocomotive is not EntityWarmlyLocomotive) { return false; @@ -69,6 +73,7 @@ { return false; } + //если оба объекта являются продвинутыми, сравниваем дополнительные характеристики if (locomotive is EntityWarmlyLocomotive warmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive otherWarmlyLocomotive) { if (warmlyLocomotive.AdditionalColor != otherWarmlyLocomotive.AdditionalColor) diff --git a/Locomotives/Locomotives/LocomotiveCompareByColor.cs b/Locomotives/Locomotives/LocomotiveCompareByColor.cs index 3e807a3..78b84f3 100644 --- a/Locomotives/Locomotives/LocomotiveCompareByColor.cs +++ b/Locomotives/Locomotives/LocomotiveCompareByColor.cs @@ -1,9 +1,13 @@ namespace Locomotives { + /// + /// Реализация класса-компаратора для сравнения по цвету + /// internal class LocomotiveCompareByColor : IComparer { public int Compare(IDrawningObject? x, IDrawningObject? y) { + //проверяем оба объекта на существование if (x == null && y == null) { return 0; @@ -30,8 +34,9 @@ { return -1; } - var xColorName = xLocomotive.GetLocomotive.Locomotive.BodyColor.Name; - var yColorName = yLocomotive.GetLocomotive.Locomotive.BodyColor.Name; + //сравниваем цвета по названию + var xColorName = xLocomotive._locomotive.Locomotive.BodyColor.Name; + var yColorName = yLocomotive._locomotive.Locomotive.BodyColor.Name; return xColorName.CompareTo(yColorName); } } diff --git a/Locomotives/Locomotives/LocomotiveCompareByType.cs b/Locomotives/Locomotives/LocomotiveCompareByType.cs index 9aa24ec..4eb82a9 100644 --- a/Locomotives/Locomotives/LocomotiveCompareByType.cs +++ b/Locomotives/Locomotives/LocomotiveCompareByType.cs @@ -1,5 +1,8 @@ namespace Locomotives { + /// + /// Реализация класса-компаратора для сортировки, сравнение по типу. + /// internal class LocomotiveCompareByType : IComparer { public int Compare(IDrawningObject? x, IDrawningObject? y) @@ -30,20 +33,20 @@ { return -1; } - if (xLocomotive?.GetLocomotive.GetType().Name != yLocomotive?.GetLocomotive.GetType().Name) + if (xLocomotive?._locomotive.GetType().Name != yLocomotive?._locomotive.GetType().Name) { - if (xLocomotive.GetLocomotive.GetType().Name == "DrawningLocomotive") + if (xLocomotive?._locomotive.GetType().Name == "DrawningLocomotive") { return -1; } return 1; } - var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed); + var speedCompare = xLocomotive._locomotive.Locomotive.Speed.CompareTo(yLocomotive._locomotive.Locomotive.Speed); if (speedCompare != 0) { return speedCompare; } - return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight); + return xLocomotive._locomotive.Locomotive.Weight.CompareTo(yLocomotive?._locomotive.Locomotive.Weight); } } } -- 2.25.1 From ea86e0ece4e2a25810b94a466563b6e4c99a951e 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: Thu, 8 Dec 2022 18:44:57 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=B4?= =?UTF-8?q?=D0=B0=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotives/Locomotives/AbstractMap.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Locomotives/Locomotives/AbstractMap.cs b/Locomotives/Locomotives/AbstractMap.cs index 0aff92b..2b9f59c 100644 --- a/Locomotives/Locomotives/AbstractMap.cs +++ b/Locomotives/Locomotives/AbstractMap.cs @@ -61,12 +61,12 @@ public (int Top, int Bottom, int Left, int Right) GetObjectCoordinates() { return - ( - (int)(_drawningObject.GetCurrentPosition().Top / _size_y), - (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y), - (int)(_drawningObject.GetCurrentPosition().Left / _size_x), - (int)(_drawningObject.GetCurrentPosition().Right / _size_x) - ); + ( + (int)(_drawningObject.GetCurrentPosition().Top / _size_y), + (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y), + (int)(_drawningObject.GetCurrentPosition().Left / _size_x), + (int)(_drawningObject.GetCurrentPosition().Right / _size_x) + ); } /// /// Проверка возможности движения в данном направлении -- 2.25.1 From 950258932e463bab8c039d88f374b6e48ca9d9c8 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: Thu, 8 Dec 2022 18:48:05 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Locomotives/Locomotives/AbstractMap.cs | 14 +++++++------- .../Locomotives/DrawningObjectLocomotive.cs | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Locomotives/Locomotives/AbstractMap.cs b/Locomotives/Locomotives/AbstractMap.cs index 2b9f59c..a350ff0 100644 --- a/Locomotives/Locomotives/AbstractMap.cs +++ b/Locomotives/Locomotives/AbstractMap.cs @@ -60,13 +60,13 @@ /// public (int Top, int Bottom, int Left, int Right) GetObjectCoordinates() { - return - ( - (int)(_drawningObject.GetCurrentPosition().Top / _size_y), - (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y), - (int)(_drawningObject.GetCurrentPosition().Left / _size_x), - (int)(_drawningObject.GetCurrentPosition().Right / _size_x) - ); + return + ( + (int)(_drawningObject.GetCurrentPosition().Top / _size_y), + (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y), + (int)(_drawningObject.GetCurrentPosition().Left / _size_x), + (int)(_drawningObject.GetCurrentPosition().Right / _size_x) + ); } /// /// Проверка возможности движения в данном направлении diff --git a/Locomotives/Locomotives/DrawningObjectLocomotive.cs b/Locomotives/Locomotives/DrawningObjectLocomotive.cs index ddba1ce..596bd59 100644 --- a/Locomotives/Locomotives/DrawningObjectLocomotive.cs +++ b/Locomotives/Locomotives/DrawningObjectLocomotive.cs @@ -1,8 +1,8 @@ namespace Locomotives { - /// - /// Класс-наследник от интерфейса (реализация) - /// +/// +/// Класс-наследник от интерфейса (реализация) +/// internal class DrawningObjectLocomotive : IDrawningObject { /// -- 2.25.1