From 075103c1162dbdfc3f5a6026a010e6a960da8f10 Mon Sep 17 00:00:00 2001 From: malimova Date: Mon, 25 Dec 2023 18:37:32 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20Lab8?= =?UTF-8?q?.=20=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Equatables,=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20?= =?UTF-8?q?=D0=B4=D1=80=D1=83=D0=B3=D0=B8=D1=85=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawningAirBomber.cs | 3 +- AirBomber/AirBomber/DrawningAirPlane.cs | 4 +- .../AirBomber/DrawningPlanesEquatables.cs | 62 +++++++++++++++++++ AirBomber/AirBomber/EntityAirBomber.cs | 2 +- AirBomber/AirBomber/EntityAirPlane.cs | 2 +- AirBomber/AirBomber/SetGeneric.cs | 13 ++-- 6 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 AirBomber/AirBomber/DrawningPlanesEquatables.cs diff --git a/AirBomber/AirBomber/DrawningAirBomber.cs b/AirBomber/AirBomber/DrawningAirBomber.cs index 003679d..4d09f40 100644 --- a/AirBomber/AirBomber/DrawningAirBomber.cs +++ b/AirBomber/AirBomber/DrawningAirBomber.cs @@ -5,8 +5,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using AirBomber; +using AirBomber.Entities; -namespace AirBomber +namespace AirBomber.DrawningObjects { public class DrawningAirBomber : DrawningAirPlane { diff --git a/AirBomber/AirBomber/DrawningAirPlane.cs b/AirBomber/AirBomber/DrawningAirPlane.cs index 56a60f7..869bff7 100644 --- a/AirBomber/AirBomber/DrawningAirPlane.cs +++ b/AirBomber/AirBomber/DrawningAirPlane.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using AirBomber; +using AirBomber.Entities; -namespace AirBomber +namespace AirBomber.DrawningObjects { public class DrawningAirPlane { diff --git a/AirBomber/AirBomber/DrawningPlanesEquatables.cs b/AirBomber/AirBomber/DrawningPlanesEquatables.cs new file mode 100644 index 0000000..7042fd2 --- /dev/null +++ b/AirBomber/AirBomber/DrawningPlanesEquatables.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.DrawningObjects; +using AirBomber.Entities; + +namespace AirBomber +{ + internal class DrawningPlanesEquatables: IEqualityComparer + { + public bool Equals(DrawningAirPlane? x, DrawningAirPlane? y) + { + if (x == null || x.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityAirPlane.Speed != y.EntityAirPlane.Speed) + { + return false; + } + if (x.EntityAirPlane.Weight != y.EntityAirPlane.Weight) + { + return false; + } + if (x.EntityAirPlane.BodyColor != y.EntityAirPlane.BodyColor) + { + return false; + } + if (x is DrawningAirBomber && y is DrawningAirBomber) + { + EntityAirBomber xAirBomber = (EntityAirBomber)x.EntityAirPlane; + EntityAirBomber yAirBomber = (EntityAirBomber)y.EntityAirPlane; + + if (xAirBomber.Bombs != yAirBomber.Bombs) + return false; + + if (xAirBomber.FuelTanks != yAirBomber.FuelTanks) + return false; + + if (xAirBomber.AdditionalColor != yAirBomber.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningAirPlane obj) + { + return obj.GetHashCode(); + } + } +} + diff --git a/AirBomber/AirBomber/EntityAirBomber.cs b/AirBomber/AirBomber/EntityAirBomber.cs index f8b76af..4efd868 100644 --- a/AirBomber/AirBomber/EntityAirBomber.cs +++ b/AirBomber/AirBomber/EntityAirBomber.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber; -namespace AirBomber +namespace AirBomber.Entities { public class EntityAirBomber : EntityAirPlane { diff --git a/AirBomber/AirBomber/EntityAirPlane.cs b/AirBomber/AirBomber/EntityAirPlane.cs index 418142f..d3650df 100644 --- a/AirBomber/AirBomber/EntityAirPlane.cs +++ b/AirBomber/AirBomber/EntityAirPlane.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber; -namespace AirBomber +namespace AirBomber.Entities { public class EntityAirPlane { diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index 0b1c8bd..168cf51 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber.Exceptions; -namespace AirBomber +namespace AirBomber.Generics { internal class SetGeneric where T : class @@ -31,14 +31,15 @@ namespace AirBomber _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемый самолет /// - public bool Insert(T plane) + public bool Insert(T plane, IEqualityComparer? equal = null) { - return Insert(plane, 0); + return Insert(plane, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -46,13 +47,17 @@ namespace AirBomber /// Добавляемый самолет /// Позиция /// - public bool Insert(T plane, int position) + public bool Insert(T plane, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new StorageOverflowException("Невозможно добавить"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + + if (equal != null && _places.Contains(plane, equal)) + throw new ArgumentException("Такой объект уже есть в коллекции"); + _places.Insert(0, plane); return true; } -- 2.25.1 From 950ea78e74dad8ade87e1d97945659610a68f11e Mon Sep 17 00:00:00 2001 From: malimova Date: Mon, 25 Dec 2023 18:52:55 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=20=D1=81=D1=80=D0=B0=D0=B2=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/PlaneCompareByColor.cs | 57 ++++++++++++++++++++++ AirBomber/AirBomber/PlaneCompareByType.cs | 39 +++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 AirBomber/AirBomber/PlaneCompareByColor.cs create mode 100644 AirBomber/AirBomber/PlaneCompareByType.cs diff --git a/AirBomber/AirBomber/PlaneCompareByColor.cs b/AirBomber/AirBomber/PlaneCompareByColor.cs new file mode 100644 index 0000000..9272b95 --- /dev/null +++ b/AirBomber/AirBomber/PlaneCompareByColor.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.DrawningObjects; +using AirBomber.Entities; + +namespace AirBomber +{ + internal class PlaneCompareByColor: IComparer + { + public int Compare(DrawningAirPlane? x, DrawningAirPlane? y) + { + if (x == null || x.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + + if (y == null || y.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + + var bodyColorCompare = x.EntityAirPlane.BodyColor.Name.CompareTo(y.EntityAirPlane.BodyColor.Name); + + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + + if (x.EntityAirPlane is EntityAirBomber xEntitySailCatamaran && y.EntityAirPlane is EntityAirBomber yEntitySailCatamaran) + { + var BodyColorCompare = xEntitySailCatamaran.BodyColor.Name.CompareTo(yEntitySailCatamaran.BodyColor.Name); + if (BodyColorCompare != 0) + { + return BodyColorCompare; + } + var AdditionalColorCompare = xEntitySailCatamaran.AdditionalColor.Name.CompareTo(yEntitySailCatamaran.AdditionalColor.Name); + if (AdditionalColorCompare != 0) + { + return AdditionalColorCompare; + } + } + + var speedCompare = x.EntityAirPlane.Speed.CompareTo(y.EntityAirPlane.Speed); + + if (speedCompare != 0) + { + return speedCompare; + } + + return x.EntityAirPlane.Weight.CompareTo(y.EntityAirPlane.Weight); + } + } +} + diff --git a/AirBomber/AirBomber/PlaneCompareByType.cs b/AirBomber/AirBomber/PlaneCompareByType.cs new file mode 100644 index 0000000..9ddc482 --- /dev/null +++ b/AirBomber/AirBomber/PlaneCompareByType.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber.DrawningObjects; + +namespace AirBomber.Generics +{ + internal class PlaneCompareByType: IComparer + { + public int Compare(DrawningAirPlane? x, DrawningAirPlane? y) + { + if (x == null || x.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + + if (y == null || y.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + + var speedCompare = x.EntityAirPlane.Speed.CompareTo(y.EntityAirPlane.Speed); + + if (speedCompare != 0) + { + return speedCompare; + } + + return x.EntityAirPlane.Weight.CompareTo(y.EntityAirPlane.Weight); + } + } +} -- 2.25.1 From e7f367dc4d1493ef330a19c17c1ddf763529febd Mon Sep 17 00:00:00 2001 From: malimova Date: Mon, 25 Dec 2023 19:03:46 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=D1=8B=20=D1=81=20?= =?UTF-8?q?=D0=B4=D0=B8=D1=80=D0=B5=D0=BA=D1=82=D0=B8=D0=B2=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawningAirBomber.cs | 3 +-- AirBomber/AirBomber/DrawningAirPlane.cs | 4 ++-- AirBomber/AirBomber/DrawningPlanesEquatables.cs | 3 +-- AirBomber/AirBomber/EntityAirBomber.cs | 2 +- AirBomber/AirBomber/EntityAirPlane.cs | 2 +- AirBomber/AirBomber/PlaneCompareByColor.cs | 3 +-- AirBomber/AirBomber/PlaneCompareByType.cs | 4 ++-- AirBomber/AirBomber/SetGeneric.cs | 2 +- 8 files changed, 10 insertions(+), 13 deletions(-) diff --git a/AirBomber/AirBomber/DrawningAirBomber.cs b/AirBomber/AirBomber/DrawningAirBomber.cs index 4d09f40..003679d 100644 --- a/AirBomber/AirBomber/DrawningAirBomber.cs +++ b/AirBomber/AirBomber/DrawningAirBomber.cs @@ -5,9 +5,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using AirBomber; -using AirBomber.Entities; -namespace AirBomber.DrawningObjects +namespace AirBomber { public class DrawningAirBomber : DrawningAirPlane { diff --git a/AirBomber/AirBomber/DrawningAirPlane.cs b/AirBomber/AirBomber/DrawningAirPlane.cs index 869bff7..56a60f7 100644 --- a/AirBomber/AirBomber/DrawningAirPlane.cs +++ b/AirBomber/AirBomber/DrawningAirPlane.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using AirBomber.Entities; +using AirBomber; -namespace AirBomber.DrawningObjects +namespace AirBomber { public class DrawningAirPlane { diff --git a/AirBomber/AirBomber/DrawningPlanesEquatables.cs b/AirBomber/AirBomber/DrawningPlanesEquatables.cs index 7042fd2..94d9c88 100644 --- a/AirBomber/AirBomber/DrawningPlanesEquatables.cs +++ b/AirBomber/AirBomber/DrawningPlanesEquatables.cs @@ -4,8 +4,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; -using AirBomber.DrawningObjects; -using AirBomber.Entities; +using AirBomber; namespace AirBomber { diff --git a/AirBomber/AirBomber/EntityAirBomber.cs b/AirBomber/AirBomber/EntityAirBomber.cs index 4efd868..f8b76af 100644 --- a/AirBomber/AirBomber/EntityAirBomber.cs +++ b/AirBomber/AirBomber/EntityAirBomber.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber; -namespace AirBomber.Entities +namespace AirBomber { public class EntityAirBomber : EntityAirPlane { diff --git a/AirBomber/AirBomber/EntityAirPlane.cs b/AirBomber/AirBomber/EntityAirPlane.cs index d3650df..418142f 100644 --- a/AirBomber/AirBomber/EntityAirPlane.cs +++ b/AirBomber/AirBomber/EntityAirPlane.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber; -namespace AirBomber.Entities +namespace AirBomber { public class EntityAirPlane { diff --git a/AirBomber/AirBomber/PlaneCompareByColor.cs b/AirBomber/AirBomber/PlaneCompareByColor.cs index 9272b95..bbfc64c 100644 --- a/AirBomber/AirBomber/PlaneCompareByColor.cs +++ b/AirBomber/AirBomber/PlaneCompareByColor.cs @@ -3,8 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using AirBomber.DrawningObjects; -using AirBomber.Entities; +using AirBomber; namespace AirBomber { diff --git a/AirBomber/AirBomber/PlaneCompareByType.cs b/AirBomber/AirBomber/PlaneCompareByType.cs index 9ddc482..7b64d8e 100644 --- a/AirBomber/AirBomber/PlaneCompareByType.cs +++ b/AirBomber/AirBomber/PlaneCompareByType.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using AirBomber.DrawningObjects; +using AirBomber; -namespace AirBomber.Generics +namespace AirBomber { internal class PlaneCompareByType: IComparer { diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index 168cf51..29feea0 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using AirBomber.Exceptions; -namespace AirBomber.Generics +namespace AirBomber { internal class SetGeneric where T : class -- 2.25.1 From 783ff07009ff2ff4609f2f928876a6d330aac900 Mon Sep 17 00:00:00 2001 From: malimova Date: Wed, 27 Dec 2023 21:40:45 +0400 Subject: [PATCH 4/4] =?UTF-8?q?Lab8=20=D1=81=D0=B4=D0=B0=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirBomber/FormPlaneCollection.Designer.cs | 42 +++++++++++++++---- AirBomber/AirBomber/FormPlaneCollection.cs | 35 +++++++++++++++- AirBomber/AirBomber/PlaneCollectionInfo.cs | 33 +++++++++++++++ .../AirBomber/PlanesGenericCollection.cs | 12 ++++-- AirBomber/AirBomber/PlanesGenericStorage.cs | 27 ++++++------ AirBomber/AirBomber/SetGeneric.cs | 36 +++++++++++----- 6 files changed, 149 insertions(+), 36 deletions(-) create mode 100644 AirBomber/AirBomber/PlaneCollectionInfo.cs diff --git a/AirBomber/AirBomber/FormPlaneCollection.Designer.cs b/AirBomber/AirBomber/FormPlaneCollection.Designer.cs index dfb5dfb..d5ca21c 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.Designer.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.Designer.cs @@ -34,6 +34,8 @@ buttonRefreshCollection = new Button(); maskedTextBoxNumber = new MaskedTextBox(); groupBoxTools = new GroupBox(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxStorages = new GroupBox(); textBoxStorageName = new TextBox(); buttonDelObject = new Button(); @@ -63,9 +65,9 @@ // // buttonAddPlane // - buttonAddPlane.Location = new Point(76, 417); + buttonAddPlane.Location = new Point(49, 525); buttonAddPlane.Name = "buttonAddPlane"; - buttonAddPlane.Size = new Size(160, 64); + buttonAddPlane.Size = new Size(209, 44); buttonAddPlane.TabIndex = 0; buttonAddPlane.Text = "Добавить самолет"; buttonAddPlane.UseVisualStyleBackColor = true; @@ -73,7 +75,7 @@ // // buttonRemovePlane // - buttonRemovePlane.Location = new Point(76, 549); + buttonRemovePlane.Location = new Point(76, 612); buttonRemovePlane.Name = "buttonRemovePlane"; buttonRemovePlane.Size = new Size(160, 60); buttonRemovePlane.TabIndex = 1; @@ -83,9 +85,9 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(76, 648); + buttonRefreshCollection.Location = new Point(49, 678); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(160, 86); + buttonRefreshCollection.Size = new Size(209, 56); buttonRefreshCollection.TabIndex = 2; buttonRefreshCollection.Text = "Обновить коллекцию"; buttonRefreshCollection.UseVisualStyleBackColor = true; @@ -93,7 +95,7 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(76, 501); + maskedTextBoxNumber.Location = new Point(76, 575); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(160, 31); @@ -102,6 +104,8 @@ // // groupBoxTools // + groupBoxTools.Controls.Add(buttonSortByColor); + groupBoxTools.Controls.Add(buttonSortByType); groupBoxTools.Controls.Add(groupBoxStorages); groupBoxTools.Controls.Add(buttonAddPlane); groupBoxTools.Controls.Add(buttonRefreshCollection); @@ -114,6 +118,26 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(49, 469); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(209, 48); + buttonSortByColor.TabIndex = 6; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(49, 415); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(209, 48); + buttonSortByType.TabIndex = 5; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // groupBoxStorages // groupBoxStorages.Controls.Add(textBoxStorageName); @@ -184,14 +208,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(270, 34); + SaveToolStripMenuItem.Size = new Size(212, 34); SaveToolStripMenuItem.Text = "Сохранение"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(270, 34); + LoadToolStripMenuItem.Size = new Size(212, 34); LoadToolStripMenuItem.Text = "Загрузка"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -245,5 +269,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/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs index 15fd2e7..4706d4f 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -94,6 +94,7 @@ namespace AirBomber MessageBox.Show(ex.Message); _logger.LogWarning($"Объект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}"); } + } /// /// Удаление объекта из набора @@ -134,7 +135,7 @@ namespace AirBomber { MessageBox.Show(ex.Message); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show("Неверный ввод"); _logger.LogWarning("Неверный ввод"); @@ -228,12 +229,42 @@ namespace AirBomber _logger.LogInformation("Загрузка прошла успешно"); ReloadObjects(); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning($"Не загрузилось: {ex.Message}"); } } } + /// + /// Сортировка по типу + /// + /// + /// + private void buttonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void buttonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor()); + /// + /// Сортировка по сравнителю + /// + /// + private void ComparePlanes(IComparer comparer) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowPlanes(); + } } } diff --git a/AirBomber/AirBomber/PlaneCollectionInfo.cs b/AirBomber/AirBomber/PlaneCollectionInfo.cs new file mode 100644 index 0000000..7dbb103 --- /dev/null +++ b/AirBomber/AirBomber/PlaneCollectionInfo.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class PlaneCollectionInfo: IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public PlaneCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(PlaneCollectionInfo other) + { + if (Name != other?.Name) + return false; + return true; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + public override string ToString() + { + return Name; + } + } +} diff --git a/AirBomber/AirBomber/PlanesGenericCollection.cs b/AirBomber/AirBomber/PlanesGenericCollection.cs index 5c38d3c..74f7279 100644 --- a/AirBomber/AirBomber/PlanesGenericCollection.cs +++ b/AirBomber/AirBomber/PlanesGenericCollection.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using AirBomber; namespace AirBomber { @@ -31,6 +32,11 @@ namespace AirBomber /// private readonly SetGeneric _collection; /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Конструктор /// /// @@ -49,13 +55,13 @@ namespace AirBomber /// /// /// - public static bool operator +(PlanesGenericCollection collect, T obj) + public static int operator +(PlanesGenericCollection collect, T obj) { if (obj == null) { - return false; + return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawningPlanesEquatables()); } /// /// Перегрузка оператора вычитания diff --git a/AirBomber/AirBomber/PlanesGenericStorage.cs b/AirBomber/AirBomber/PlanesGenericStorage.cs index 40f64d7..72e642d 100644 --- a/AirBomber/AirBomber/PlanesGenericStorage.cs +++ b/AirBomber/AirBomber/PlanesGenericStorage.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace AirBomber { @@ -12,11 +13,11 @@ namespace AirBomber /// /// Словарь (хранилище) /// - readonly Dictionary> _planeStorages; + readonly Dictionary> _planeStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _planeStorages.Keys.ToList(); + public List Keys => _planeStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -44,7 +45,7 @@ namespace AirBomber /// public PlanesGenericStorage(int pictureWidth, int pictureHeight) { - _planeStorages = new Dictionary>(); + _planeStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -55,9 +56,10 @@ namespace AirBomber public void AddSet(string name) { // TODO Прописать логику для добавления DONE - if (!_planeStorages.ContainsKey(name)) + if(!_planeStorages.ContainsKey(new PlaneCollectionInfo(name, string.Empty))) { - _planeStorages.Add(name, new PlanesGenericCollection(_pictureWidth, _pictureHeight)); + _planeStorages.Add(new PlaneCollectionInfo(name, string.Empty), + new PlanesGenericCollection(_pictureWidth, _pictureHeight)); } } /// @@ -67,10 +69,11 @@ namespace AirBomber public void DelSet(string name) { // TODO Прописать логику для удаления DONE - if (_planeStorages.ContainsKey(name)) + if (!_planeStorages.ContainsKey(new PlaneCollectionInfo(name, string.Empty))) { - _planeStorages.Remove(name); + return; } + _planeStorages.Remove(new PlaneCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -83,9 +86,9 @@ namespace AirBomber get { // TODO Продумать логику получения набора DONE - if (_planeStorages.ContainsKey(ind)) + if (_planeStorages.ContainsKey(new PlaneCollectionInfo(ind, string.Empty))) { - return _planeStorages[ind]; + return _planeStorages[new PlaneCollectionInfo(ind, string.Empty)]; } return null; } @@ -102,7 +105,7 @@ namespace AirBomber File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair>record in _planeStorages) + foreach (KeyValuePair>record in _planeStorages) { StringBuilder records = new(); foreach (DrawningAirPlane? elem in record.Value.GetPlanes) @@ -169,7 +172,7 @@ namespace AirBomber DrawningAirPlane? plane = elem?.CreateDrawningAirPlane(_separatorForObject, _pictureWidth, _pictureHeight); if (plane != null) { - if (!(collection + plane)) + if ((collection + plane) == -1) { try { @@ -186,7 +189,7 @@ namespace AirBomber } } } - _planeStorages.Add(record[0], collection); + _planeStorages.Add(new PlaneCollectionInfo(record[0], string.Empty), collection); } return true; } diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index 29feea0..2565729 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -37,9 +37,19 @@ namespace AirBomber /// /// Добавляемый самолет /// - public bool Insert(T plane, IEqualityComparer? equal = null) + public int Insert(T plane, IEqualityComparer? equal = null) { - return Insert(plane, 0, equal); + if (equal != null) + { + foreach (var secondLoco in _places) + { + if (equal.Equals(plane, secondLoco)) + { + throw new Exception("Такой объект уже есть в коллекции"); + } + } + } + return Insert(plane, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -47,19 +57,23 @@ namespace AirBomber /// Добавляемый самолет /// Позиция /// - public bool Insert(T plane, int position, IEqualityComparer? equal = null) + public int Insert(T plane, int position, IEqualityComparer? equal = null) { - if (position < 0 || position >= _maxCount) - throw new StorageOverflowException("Невозможно добавить"); - - if (Count >= _maxCount) + if (_places.Count >= _maxCount) throw new StorageOverflowException(_maxCount); - if (equal != null && _places.Contains(plane, equal)) - throw new ArgumentException("Такой объект уже есть в коллекции"); + if (position < 0 || position >= _maxCount) + { + return -1; + } - _places.Insert(0, plane); - return true; + if (equal != null) + { + if (_places.Contains(plane, equal)) + throw new ArgumentException(("Такой объект уже есть в коллекции")); + } + _places.Insert(position, plane); + return position; } /// /// Удаление объекта из набора с конкретной позиции -- 2.25.1