From d0842fc00e29808f81c6b57f8614e42f42c40372 Mon Sep 17 00:00:00 2001 From: tellsense Date: Sun, 24 Dec 2023 14:25:05 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20DrawiningLocomo?= =?UTF-8?q?tiveEqutables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawiningLocomotiveEqutables.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs diff --git a/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs b/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs new file mode 100644 index 0000000..d34be07 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectElectricLocomotive.DrawingObjects; +using ProjectElectricLocomotive.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectElectricLocomotive.Generics +{ + internal class DrawingLocomotiveEqutables : IEqualityComparer + { + public bool Equals(DrawingLocomotive? x, DrawingLocomotive? y) + { + if (x == null || x.EntityLocomotive == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityLocomotive == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityLocomotive.Speed != y.EntityLocomotive.Speed) + { + return false; + } + if (x.EntityLocomotive.Weight != y.EntityLocomotive.Weight) + { + return false; + } + if (x.EntityLocomotive.BodyColor != y.EntityLocomotive.BodyColor) + { + return false; + } + if (x is DrawingElectricLocomotive && y is DrawingElectricLocomotive) + { + // TODO доделать логику сравнения дополнительных параметров + } + return true; + } + public int GetHashCode([DisallowNull] DrawingLocomotive obj) + { + return obj.GetHashCode(); + } + } +} -- 2.25.1 From eb6c80a47f4133c87c86545e593000bff49c3609 Mon Sep 17 00:00:00 2001 From: tellsense Date: Sun, 24 Dec 2023 14:40:44 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=B3=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1?= =?UTF-8?q?=D0=B0=20(=D0=B1=D0=B5=D0=B7=20TODO)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormLocomotiveCollection.Designer.cs | 46 ++++++++++++++----- .../FormLocomotiveCollection.cs | 4 +- .../LocomotiveCompareByColor.cs | 19 ++++++++ .../LocomotiveCompareByType.cs | 34 ++++++++++++++ .../LocomotivesGenericCollection.cs | 3 ++ .../ElectricLocomotive/SetGeneric.cs | 28 ++++++++++- 6 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs create mode 100644 ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs index a921ee7..ab1e7bf 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs @@ -46,6 +46,8 @@ saveFileDialog = new SaveFileDialog(); openFileDialog = new OpenFileDialog(); groupBoxMenu = new GroupBox(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); groupBoxSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); menuStrip.SuspendLayout(); @@ -54,10 +56,10 @@ // // groupBoxSets // + groupBoxSets.Controls.Add(ButtonSortByType); groupBoxSets.Controls.Add(textBoxSetName); groupBoxSets.Controls.Add(buttonDeleteSet); groupBoxSets.Controls.Add(listBoxStorages); - groupBoxSets.Controls.Add(ButtonAddLocomotive); groupBoxSets.Controls.Add(buttonAddSet); groupBoxSets.Location = new Point(0, 22); groupBoxSets.Name = "groupBoxSets"; @@ -70,14 +72,14 @@ // textBoxSetName.Location = new Point(6, 38); textBoxSetName.Name = "textBoxSetName"; - textBoxSetName.Size = new Size(226, 23); + textBoxSetName.Size = new Size(230, 23); textBoxSetName.TabIndex = 3; // // buttonDeleteSet // buttonDeleteSet.Location = new Point(6, 216); buttonDeleteSet.Name = "buttonDeleteSet"; - buttonDeleteSet.Size = new Size(226, 45); + buttonDeleteSet.Size = new Size(230, 45); buttonDeleteSet.TabIndex = 2; buttonDeleteSet.Text = "Удалить набор"; buttonDeleteSet.UseVisualStyleBackColor = true; @@ -89,15 +91,15 @@ listBoxStorages.ItemHeight = 15; listBoxStorages.Location = new Point(6, 116); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(226, 94); + listBoxStorages.Size = new Size(230, 94); listBoxStorages.TabIndex = 1; listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; // // ButtonAddLocomotive // - ButtonAddLocomotive.Location = new Point(6, 269); + ButtonAddLocomotive.Location = new Point(6, 395); ButtonAddLocomotive.Name = "ButtonAddLocomotive"; - ButtonAddLocomotive.Size = new Size(226, 39); + ButtonAddLocomotive.Size = new Size(230, 39); ButtonAddLocomotive.TabIndex = 0; ButtonAddLocomotive.Text = "Добавить локомотив"; ButtonAddLocomotive.UseVisualStyleBackColor = true; @@ -107,7 +109,7 @@ // buttonAddSet.Location = new Point(6, 67); buttonAddSet.Name = "buttonAddSet"; - buttonAddSet.Size = new Size(226, 34); + buttonAddSet.Size = new Size(230, 34); buttonAddSet.TabIndex = 0; buttonAddSet.Text = "Добавить набор"; buttonAddSet.UseVisualStyleBackColor = true; @@ -115,14 +117,14 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(6, 342); + maskedTextBoxNumber.Location = new Point(6, 440); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(226, 23); + maskedTextBoxNumber.Size = new Size(230, 23); maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(6, 440); + ButtonRefreshCollection.Location = new Point(6, 514); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(230, 43); ButtonRefreshCollection.TabIndex = 2; @@ -132,7 +134,7 @@ // // ButtonRemoveLocomotive // - ButtonRemoveLocomotive.Location = new Point(6, 388); + ButtonRemoveLocomotive.Location = new Point(6, 469); ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; ButtonRemoveLocomotive.Size = new Size(230, 39); ButtonRemoveLocomotive.TabIndex = 1; @@ -195,10 +197,12 @@ // groupBoxMenu // groupBoxMenu.AutoSize = true; + groupBoxMenu.Controls.Add(ButtonSortByColor); groupBoxMenu.Controls.Add(maskedTextBoxNumber); groupBoxMenu.Controls.Add(groupBoxSets); groupBoxMenu.Controls.Add(ButtonRemoveLocomotive); groupBoxMenu.Controls.Add(ButtonRefreshCollection); + groupBoxMenu.Controls.Add(ButtonAddLocomotive); groupBoxMenu.Dock = DockStyle.Right; groupBoxMenu.Location = new Point(661, 24); groupBoxMenu.Name = "groupBoxMenu"; @@ -207,6 +211,24 @@ groupBoxMenu.TabStop = false; groupBoxMenu.Text = "Инструменты"; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(6, 342); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(230, 38); + ButtonSortByColor.TabIndex = 4; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(6, 273); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(230, 41); + ButtonSortByType.TabIndex = 4; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + // // FormLocomotiveCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -247,5 +269,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem SaveToolStripMenuItem; private GroupBox groupBoxMenu; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs index 64a4713..d88fc9f 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs @@ -53,7 +53,7 @@ namespace ProjectElectricLocomotive _logger.LogWarning("Неудачная попытка. Коллекция не добавлена, не все данные заполнены"); return; } - _storage.AddSet(textBoxSetName.Text); + _storage.AddSet(textBoxSetName.Text); ReloadObjects(); _logger.LogInformation($"Добавлен набор: {textBoxSetName.Text}"); @@ -109,7 +109,7 @@ namespace ProjectElectricLocomotive ReloadObjects(); _logger.LogInformation($"Набор '{name}' удален"); } - _logger.LogWarning("Отмена удаления набора"); + _logger.LogWarning("Отмена удаления набора"); } private void buttonRemoveLocomotive_Click(object sender, EventArgs e) { diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs new file mode 100644 index 0000000..5097ffc --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectElectricLocomotive.DrawingObjects; + +namespace ProjectElectricLocomotive.Generics +{ + internal interface LocomotiveCompareByColor + { + public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) + { + // TODO реализовать логику сравнения + throw new NotImplementedException(); + } + + } +} diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs new file mode 100644 index 0000000..656238e --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectElectricLocomotive.DrawingObjects; + +namespace ProjectElectricLocomotive.Generics +{ + internal interface LocomotiveCompareByType : IComparer + { + public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) + { + if (x == null || x.EntityLocomotive == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityLocomotive == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight); + } + } +} diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs index 611b823..5f100eb 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs @@ -17,6 +17,9 @@ namespace ProjectElectricLocomotive.Generics private readonly int _placeSizeWidth = 200; private readonly int _placeSizeHeight = 130; private readonly SetGeneric _collection; + + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + public LocomotivesGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; diff --git a/ElectricLocomotive/ElectricLocomotive/SetGeneric.cs b/ElectricLocomotive/ElectricLocomotive/SetGeneric.cs index e5b63dd..abc00e0 100644 --- a/ElectricLocomotive/ElectricLocomotive/SetGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/SetGeneric.cs @@ -16,6 +16,8 @@ namespace ProjectElectricLocomotive.Generics /// Максимальное количество объектов в списке private readonly int _maxCount; + + public void SortSet(IComparer comparer) => _places.Sort(comparer); public SetGeneric(int count) { _maxCount = count; @@ -23,12 +25,22 @@ namespace ProjectElectricLocomotive.Generics } /// Добавление объекта в набор - public int Insert(T loco) + public int Insert(T loco, IEqualityComparer? equal = null) { + if (equal != null) + { + foreach (var secondLoco in _places) + { + if (equal.Equals(loco, secondLoco)) + { + throw new Exception("Такой объект уже есть в коллекции"); + } + } + } return Insert(loco, 0); } - public int Insert(T loco, int position) + public int Insert(T loco, int position, IEqualityComparer? equal = null) { if (_places.Count >= _maxCount) throw new StorageOverflowException(_maxCount); @@ -37,6 +49,18 @@ namespace ProjectElectricLocomotive.Generics { return -1; } + + if (equal != null) + { + foreach (var secondLoco in _places) + { + if (equal.Equals(loco, secondLoco)) + { + throw new ApplicationException("Такой объект уже есть в коллекции"); + } + } + } + _places.Insert(position, loco); return position; } -- 2.25.1 From e23ed22c729ecfc2e2ba987389986537ba025732 Mon Sep 17 00:00:00 2001 From: tellsense Date: Sun, 24 Dec 2023 15:35:14 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=208=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=87=D1=82=D0=B8=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormLocomotiveCollection.Designer.cs | 48 ++++++++++--------- .../FormLocomotiveCollection.cs | 30 ++++++++++++ .../LocomotiveCompareByColor.cs | 24 ++++++++-- .../LocomotiveCompareByType.cs | 2 +- .../LocomotivesCollectionInfo.cs | 35 ++++++++++++++ .../LocomotivesGenericStorage.cs | 25 +++++----- 6 files changed, 124 insertions(+), 40 deletions(-) create mode 100644 ElectricLocomotive/ElectricLocomotive/LocomotivesCollectionInfo.cs diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs index ab1e7bf..23bc19d 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs @@ -30,11 +30,12 @@ { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormLocomotiveCollection)); groupBoxSets = new GroupBox(); + ButtonSortByType = new Button(); textBoxSetName = new TextBox(); buttonDeleteSet = new Button(); listBoxStorages = new ListBox(); - ButtonAddLocomotive = new Button(); buttonAddSet = new Button(); + ButtonAddLocomotive = new Button(); maskedTextBoxNumber = new MaskedTextBox(); ButtonRefreshCollection = new Button(); ButtonRemoveLocomotive = new Button(); @@ -47,7 +48,6 @@ openFileDialog = new OpenFileDialog(); groupBoxMenu = new GroupBox(); ButtonSortByColor = new Button(); - ButtonSortByType = new Button(); groupBoxSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); menuStrip.SuspendLayout(); @@ -56,18 +56,27 @@ // // groupBoxSets // - groupBoxSets.Controls.Add(ButtonSortByType); groupBoxSets.Controls.Add(textBoxSetName); groupBoxSets.Controls.Add(buttonDeleteSet); groupBoxSets.Controls.Add(listBoxStorages); groupBoxSets.Controls.Add(buttonAddSet); groupBoxSets.Location = new Point(0, 22); groupBoxSets.Name = "groupBoxSets"; - groupBoxSets.Size = new Size(242, 314); + groupBoxSets.Size = new Size(242, 264); groupBoxSets.TabIndex = 5; groupBoxSets.TabStop = false; groupBoxSets.Text = "Наборы"; // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(6, 295); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(230, 41); + ButtonSortByType.TabIndex = 4; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // textBoxSetName // textBoxSetName.Location = new Point(6, 38); @@ -95,16 +104,6 @@ listBoxStorages.TabIndex = 1; listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; // - // ButtonAddLocomotive - // - ButtonAddLocomotive.Location = new Point(6, 395); - ButtonAddLocomotive.Name = "ButtonAddLocomotive"; - ButtonAddLocomotive.Size = new Size(230, 39); - ButtonAddLocomotive.TabIndex = 0; - ButtonAddLocomotive.Text = "Добавить локомотив"; - ButtonAddLocomotive.UseVisualStyleBackColor = true; - ButtonAddLocomotive.Click += buttonAddLocomotive_Click; - // // buttonAddSet // buttonAddSet.Location = new Point(6, 67); @@ -115,6 +114,16 @@ buttonAddSet.UseVisualStyleBackColor = true; buttonAddSet.Click += ButtonAddObject_Click; // + // ButtonAddLocomotive + // + ButtonAddLocomotive.Location = new Point(6, 395); + ButtonAddLocomotive.Name = "ButtonAddLocomotive"; + ButtonAddLocomotive.Size = new Size(230, 39); + ButtonAddLocomotive.TabIndex = 0; + ButtonAddLocomotive.Text = "Добавить локомотив"; + ButtonAddLocomotive.UseVisualStyleBackColor = true; + ButtonAddLocomotive.Click += buttonAddLocomotive_Click; + // // maskedTextBoxNumber // maskedTextBoxNumber.Location = new Point(6, 440); @@ -197,6 +206,7 @@ // groupBoxMenu // groupBoxMenu.AutoSize = true; + groupBoxMenu.Controls.Add(ButtonSortByType); groupBoxMenu.Controls.Add(ButtonSortByColor); groupBoxMenu.Controls.Add(maskedTextBoxNumber); groupBoxMenu.Controls.Add(groupBoxSets); @@ -219,15 +229,7 @@ ButtonSortByColor.TabIndex = 4; ButtonSortByColor.Text = "Сортировка по цвету"; ButtonSortByColor.UseVisualStyleBackColor = true; - // - // ButtonSortByType - // - ButtonSortByType.Location = new Point(6, 273); - ButtonSortByType.Name = "ButtonSortByType"; - ButtonSortByType.Size = new Size(230, 41); - ButtonSortByType.TabIndex = 4; - ButtonSortByType.Text = "Сортировка по типу"; - ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; // // FormLocomotiveCollection // diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs index d88fc9f..42af062 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs @@ -157,6 +157,36 @@ namespace ProjectElectricLocomotive } pictureBoxCollection.Image = obj.ShowLocomotives(); } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + // TODO продумать логику + } + /// + /// Сортировка по сравнителю + /// + /// + private void CompareLocomotives(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.ShowLocomotives(); + } + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) { pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives(); diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs index 5097ffc..026ea0e 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs @@ -4,16 +4,32 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ProjectElectricLocomotive.DrawingObjects; +using ProjectElectricLocomotive.Entities; namespace ProjectElectricLocomotive.Generics { - internal interface LocomotiveCompareByColor + internal class LocoCompareByColor : IComparer { public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) { - // TODO реализовать логику сравнения - throw new NotImplementedException(); - } + if (x == null || x.EntityLocomotive == null) + throw new NotImplementedException(nameof(x)); + + if (y == null || y.EntityLocomotive == null) + throw new NotImplementedException(nameof(y)); + + var bodyColor = x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name); + + if (bodyColor != 0) return bodyColor; + + if (x.EntityLocomotive is EntityElectricLocomotive && + y.EntityLocomotive is EntityElectricLocomotive) + { + var addcolor = (x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name.CompareTo((y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name); + if (addcolor != 0) return addcolor; + } + return 1; + } } } diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs index 656238e..ad4cafd 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByType.cs @@ -7,7 +7,7 @@ using ProjectElectricLocomotive.DrawingObjects; namespace ProjectElectricLocomotive.Generics { - internal interface LocomotiveCompareByType : IComparer + internal class LocomotiveCompareByType : IComparer { public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) { diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotivesCollectionInfo.cs b/ElectricLocomotive/ElectricLocomotive/LocomotivesCollectionInfo.cs new file mode 100644 index 0000000..6cc75f2 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/LocomotivesCollectionInfo.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Generics +{ + internal class LocomotivesCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public LocomotivesCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(LocomotivesCollectionInfo 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/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs index 6ddc96b..aec96cc 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs @@ -10,8 +10,8 @@ namespace ProjectElectricLocomotive.Generics { internal class LocomotivesGenericStorage { - readonly Dictionary> _locomotivesStorage; - public List Keys => _locomotivesStorage.Keys.ToList(); + readonly Dictionary> _locomotivesStorage; + public List Keys => _locomotivesStorage.Keys.ToList(); private static readonly char _separatorForKeyValue = '|'; private readonly char _separatorRecords = ';'; private static readonly char _separatorForObject = ':'; @@ -22,22 +22,23 @@ namespace ProjectElectricLocomotive.Generics public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) { - _locomotivesStorage = new Dictionary>(); + _locomotivesStorage = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { - if (!_locomotivesStorage.ContainsKey(name)) + if (!_locomotivesStorage.ContainsKey( new LocomotivesCollectionInfo(name, ""))) { - _locomotivesStorage.Add(name, new LocomotivesGenericCollection(_pictureWidth, _pictureHeight)); + _locomotivesStorage.Add(new LocomotivesCollectionInfo(name, ""), + new LocomotivesGenericCollection(_pictureWidth, _pictureHeight)); } } public void DelSet(string name) { - if (_locomotivesStorage.ContainsKey(name)) + if (_locomotivesStorage.ContainsKey(new LocomotivesCollectionInfo(name, ""))) { - _locomotivesStorage.Remove(name); + _locomotivesStorage.Remove(new LocomotivesCollectionInfo(name, "")); } } public LocomotivesGenericCollection? @@ -45,9 +46,9 @@ namespace ProjectElectricLocomotive.Generics { get { - if (_locomotivesStorage.ContainsKey(ind)) + if (_locomotivesStorage.ContainsKey(new LocomotivesCollectionInfo(ind, ""))) { - return _locomotivesStorage[ind]; + return _locomotivesStorage[new LocomotivesCollectionInfo(ind, "")]; } return null; } @@ -59,7 +60,7 @@ namespace ProjectElectricLocomotive.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _locomotivesStorage) + foreach (KeyValuePair> record in _locomotivesStorage) { StringBuilder records = new(); foreach (DrawingLocomotive? elem in record.Value.GetLocomotives) @@ -126,13 +127,13 @@ namespace ProjectElectricLocomotive.Generics DrawingLocomotive? loco = elem?.CreateDrawningLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); if (loco != null) { - if ((collection + loco) == -1) // for my realization it's -1, for eegov's realization it's boolean + if ((collection + loco) == -1) { throw new Exception("Ошибка добавления "); } } } - _locomotivesStorage.Add(record[0], collection); + _locomotivesStorage.Add(new LocomotivesCollectionInfo(record[0], string.Empty), collection); } return; } -- 2.25.1 From 806b57c7fd0d565bf6e41ef5fddc38d0262f746f Mon Sep 17 00:00:00 2001 From: tellsense Date: Sun, 24 Dec 2023 17:27:07 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=208=20=D0=BB=D0=B0=D0=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawiningLocomotiveEqutables.cs | 13 ++++- .../FormLocomotiveCollection.cs | 20 +++----- .../LocomotiveCompareByColor.cs | 49 +++++++++++++------ .../LocomotivesGenericCollection.cs | 2 +- 4 files changed, 53 insertions(+), 31 deletions(-) diff --git a/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs b/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs index d34be07..bd16cf8 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawiningLocomotiveEqutables.cs @@ -39,7 +39,18 @@ namespace ProjectElectricLocomotive.Generics } if (x is DrawingElectricLocomotive && y is DrawingElectricLocomotive) { - // TODO доделать логику сравнения дополнительных параметров + if ((x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor != (y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor) + { + return false; + } + if ((x.EntityLocomotive as EntityElectricLocomotive).Pantograph != (y.EntityLocomotive as EntityElectricLocomotive).Pantograph) + { + return false; + } + if ((x.EntityLocomotive as EntityElectricLocomotive).Compartment != (y.EntityLocomotive as EntityElectricLocomotive).Compartment) + { + return false; + } } return true; } diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs index 42af062..5136c5f 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs @@ -94,6 +94,10 @@ namespace ProjectElectricLocomotive MessageBox.Show(ex.Message); _logger.LogWarning("Не удалось добавить объект"); } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } } private void ButtonRemoveObject_Click(object sender, EventArgs e) { @@ -158,19 +162,9 @@ namespace ProjectElectricLocomotive pictureBoxCollection.Image = obj.ShowLocomotives(); } private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByType()); - /// - /// Сортировка по цвету - /// - /// - /// - private void ButtonSortByColor_Click(object sender, EventArgs e) - { - // TODO продумать логику - } - /// - /// Сортировка по сравнителю - /// - /// + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByColor()); + private void CompareLocomotives(IComparer comparer) { if (listBoxStorages.SelectedIndex == -1) diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs index 026ea0e..632c195 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotiveCompareByColor.cs @@ -8,28 +8,45 @@ using ProjectElectricLocomotive.Entities; namespace ProjectElectricLocomotive.Generics { - internal class LocoCompareByColor : IComparer + internal class LocomotiveCompareByColor : IComparer { public int Compare(DrawingLocomotive? x, DrawingLocomotive? y) { - if (x == null || x.EntityLocomotive == null) - throw new NotImplementedException(nameof(x)); - - if (y == null || y.EntityLocomotive == null) - throw new NotImplementedException(nameof(y)); - - var bodyColor = x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name); - - if (bodyColor != 0) return bodyColor; - - if (x.EntityLocomotive is EntityElectricLocomotive && - y.EntityLocomotive is EntityElectricLocomotive) { - var addcolor = (x.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name.CompareTo((y.EntityLocomotive as EntityElectricLocomotive).AdditionalColor.Name); - if (addcolor != 0) return addcolor; + throw new ArgumentNullException(nameof(x)); } - return 1; + if (y == null || y.EntityLocomotive == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityLocomotive.BodyColor.Name != y.EntityLocomotive.BodyColor.Name) + { + return x.EntityLocomotive.BodyColor.Name.CompareTo(y.EntityLocomotive.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawingLocomotive) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawingElectricLocomotive) + { + EntityElectricLocomotive entityX = (EntityElectricLocomotive)x.EntityLocomotive; + EntityElectricLocomotive entityY = (EntityElectricLocomotive)y.EntityLocomotive; + if (entityX.AdditionalColor.Name != entityY.AdditionalColor.Name) + { + return entityX.AdditionalColor.Name.CompareTo(entityY.AdditionalColor.Name); + } + } + var speedCompare = + x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight); } } } diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs index 5f100eb..011d197 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs @@ -34,7 +34,7 @@ namespace ProjectElectricLocomotive.Generics { return -1; } - return collect._collection.Insert(locomotive); + return collect._collection.Insert(locomotive, new DrawingLocomotiveEqutables()); } public static T? operator -(LocomotivesGenericCollection collect, int pos) { -- 2.25.1