From 732526724dff6056da016aac0f8c5d2b0f851ed3 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 12 Dec 2023 18:40:27 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B08?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormLocomotiveCollection.Designer.cs | 86 ++++++++++++------- .../FormLocomotiveCollection.cs | 10 +++ .../FormLocomotiveCollection.resx | 3 + .../Generics/DrawiningLocomotiveEqutables.cs | 63 ++++++++++++++ .../Generics/LocomotiveCompareByColor.cs | 35 ++++++++ .../Generics/LocomotiveCompareByType.cs | 34 ++++++++ .../Generics/LocomotivesCollectionInfo.cs | 25 ++++++ .../Generics/LocomotivesGenericCollection.cs | 7 +- .../Generics/LocomotivesGenericStorage.cs | 28 +++--- ElectricLocomotive/Generics/SetGeneric.cs | 23 +++-- .../LogicFormLocomotiveCollection.cs | 38 +++++++- 11 files changed, 299 insertions(+), 53 deletions(-) create mode 100644 ElectricLocomotive/Generics/DrawiningLocomotiveEqutables.cs create mode 100644 ElectricLocomotive/Generics/LocomotiveCompareByColor.cs create mode 100644 ElectricLocomotive/Generics/LocomotiveCompareByType.cs create mode 100644 ElectricLocomotive/Generics/LocomotivesCollectionInfo.cs diff --git a/ElectricLocomotive/FormLocomotiveCollection.Designer.cs b/ElectricLocomotive/FormLocomotiveCollection.Designer.cs index bd9649b..330e77e 100644 --- a/ElectricLocomotive/FormLocomotiveCollection.Designer.cs +++ b/ElectricLocomotive/FormLocomotiveCollection.Designer.cs @@ -29,8 +29,9 @@ private void InitializeComponent() { tableLayoutPanel1 = new TableLayoutPanel(); - pictureBoxCollection = new PictureBox(); groupBox1 = new GroupBox(); + button2 = new Button(); + buttonSortByType = new Button(); groupBox2 = new GroupBox(); buttonDelObject = new Button(); listBoxStorages = new ListBox(); @@ -40,14 +41,15 @@ buttonRefreshCollection = new Button(); buttonRemoveLocomotive = new Button(); buttonAddLocomotive = new Button(); + pictureBoxCollection = new PictureBox(); menuStrip1 = new MenuStrip(); fileToolStripMenuItem = new ToolStripMenuItem(); saveToolStripMenuItem = new ToolStripMenuItem(); loadToolStripMenuItem = new ToolStripMenuItem(); tableLayoutPanel1.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); groupBox1.SuspendLayout(); groupBox2.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); menuStrip1.SuspendLayout(); SuspendLayout(); // @@ -57,29 +59,22 @@ tableLayoutPanel1.ColumnCount = 2; tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle()); - tableLayoutPanel1.Controls.Add(pictureBoxCollection, 0, 0); tableLayoutPanel1.Controls.Add(groupBox1, 1, 0); + tableLayoutPanel1.Controls.Add(pictureBoxCollection, 0, 0); tableLayoutPanel1.Dock = DockStyle.Fill; tableLayoutPanel1.Location = new Point(0, 28); tableLayoutPanel1.Name = "tableLayoutPanel1"; tableLayoutPanel1.RowCount = 1; tableLayoutPanel1.RowStyles.Add(new RowStyle()); - tableLayoutPanel1.Size = new Size(885, 453); + tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F)); + tableLayoutPanel1.Size = new Size(885, 499); tableLayoutPanel1.TabIndex = 0; // - // pictureBoxCollection - // - pictureBoxCollection.Dock = DockStyle.Fill; - pictureBoxCollection.Location = new Point(3, 3); - pictureBoxCollection.MinimumSize = new Size(100, 100); - pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(673, 447); - pictureBoxCollection.TabIndex = 1; - pictureBoxCollection.TabStop = false; - // // groupBox1 // groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right; + groupBox1.Controls.Add(button2); + groupBox1.Controls.Add(buttonSortByType); groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(buttonRefreshCollection); @@ -89,11 +84,31 @@ groupBox1.MaximumSize = new Size(200, 1000000); groupBox1.MinimumSize = new Size(200, 200); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(200, 444); + groupBox1.Size = new Size(200, 484); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // button2 + // + button2.Location = new Point(12, 311); + button2.Name = "button2"; + button2.Size = new Size(176, 29); + button2.TabIndex = 6; + button2.Text = "Сортировка по цвету"; + button2.UseVisualStyleBackColor = true; + button2.Click += button2_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(12, 276); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(176, 29); + buttonSortByType.TabIndex = 2; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // groupBox2 // groupBox2.Controls.Add(buttonDelObject); @@ -109,7 +124,7 @@ // // buttonDelObject // - buttonDelObject.Location = new Point(4, 204); + buttonDelObject.Location = new Point(6, 204); buttonDelObject.Name = "buttonDelObject"; buttonDelObject.Size = new Size(176, 29); buttonDelObject.TabIndex = 3; @@ -129,7 +144,7 @@ // // buttonAddObject // - buttonAddObject.Location = new Point(4, 59); + buttonAddObject.Location = new Point(6, 59); buttonAddObject.Name = "buttonAddObject"; buttonAddObject.Size = new Size(176, 29); buttonAddObject.TabIndex = 1; @@ -146,18 +161,18 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(10, 328); + maskedTextBoxNumber.Location = new Point(12, 381); maskedTextBoxNumber.Mask = "00000"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(184, 27); + maskedTextBoxNumber.Size = new Size(176, 27); maskedTextBoxNumber.TabIndex = 4; maskedTextBoxNumber.ValidatingType = typeof(int); // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(10, 396); + buttonRefreshCollection.Location = new Point(12, 449); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(184, 29); + buttonRefreshCollection.Size = new Size(176, 29); buttonRefreshCollection.TabIndex = 3; buttonRefreshCollection.Text = "Обновить колекцию"; buttonRefreshCollection.UseVisualStyleBackColor = true; @@ -165,9 +180,9 @@ // // buttonRemoveLocomotive // - buttonRemoveLocomotive.Location = new Point(10, 361); + buttonRemoveLocomotive.Location = new Point(12, 414); buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; - buttonRemoveLocomotive.Size = new Size(184, 29); + buttonRemoveLocomotive.Size = new Size(176, 29); buttonRemoveLocomotive.TabIndex = 2; buttonRemoveLocomotive.Text = "Удалить локомотив"; buttonRemoveLocomotive.UseVisualStyleBackColor = true; @@ -175,14 +190,23 @@ // // buttonAddLocomotive // - buttonAddLocomotive.Location = new Point(10, 293); + buttonAddLocomotive.Location = new Point(12, 346); buttonAddLocomotive.Name = "buttonAddLocomotive"; - buttonAddLocomotive.Size = new Size(184, 29); + buttonAddLocomotive.Size = new Size(176, 29); buttonAddLocomotive.TabIndex = 0; buttonAddLocomotive.Text = "Добавить локомотив"; buttonAddLocomotive.UseVisualStyleBackColor = true; buttonAddLocomotive.Click += buttonAddLocomotive_Click; // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Fill; + pictureBoxCollection.Location = new Point(3, 3); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(673, 493); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // // menuStrip1 // menuStrip1.ImageScalingSize = new Size(20, 20); @@ -203,14 +227,14 @@ // saveToolStripMenuItem // saveToolStripMenuItem.Name = "saveToolStripMenuItem"; - saveToolStripMenuItem.Size = new Size(224, 26); + saveToolStripMenuItem.Size = new Size(177, 26); saveToolStripMenuItem.Text = "Сохранение"; saveToolStripMenuItem.Click += saveToolStripMenuItem_Click; // // loadToolStripMenuItem // loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - loadToolStripMenuItem.Size = new Size(224, 26); + loadToolStripMenuItem.Size = new Size(177, 26); loadToolStripMenuItem.Text = "Загрузка"; loadToolStripMenuItem.Click += loadToolStripMenuItem_Click; // @@ -218,7 +242,7 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(885, 481); + ClientSize = new Size(885, 527); Controls.Add(tableLayoutPanel1); Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; @@ -226,11 +250,11 @@ Name = "FormLocomotiveCollection"; Text = "FormLocomotiveCollection"; tableLayoutPanel1.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); groupBox2.ResumeLayout(false); groupBox2.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); menuStrip1.ResumeLayout(false); menuStrip1.PerformLayout(); ResumeLayout(false); @@ -242,7 +266,6 @@ private TableLayoutPanel tableLayoutPanel1; private GroupBox groupBox1; private Button buttonAddLocomotive; - private PictureBox pictureBoxCollection; private Button buttonRemoveLocomotive; private Button buttonRefreshCollection; private MaskedTextBox maskedTextBoxNumber; @@ -255,5 +278,8 @@ private ToolStripMenuItem fileToolStripMenuItem; private ToolStripMenuItem saveToolStripMenuItem; private ToolStripMenuItem loadToolStripMenuItem; + private Button button2; + private Button buttonSortByType; + private PictureBox pictureBoxCollection; } } \ No newline at end of file diff --git a/ElectricLocomotive/FormLocomotiveCollection.cs b/ElectricLocomotive/FormLocomotiveCollection.cs index 1b8a281..6a7be5c 100644 --- a/ElectricLocomotive/FormLocomotiveCollection.cs +++ b/ElectricLocomotive/FormLocomotiveCollection.cs @@ -50,5 +50,15 @@ namespace ProjectElectricLocomotive { LoadToolStripMenuItem_Click(sender, e); } + + private void buttonSortByType_Click(object sender, EventArgs e) + { + ButtonSortByType_Click(sender, e); + } + + private void button2_Click(object sender, EventArgs e) + { + ButtonSortByColor_Click(sender, e); + } } } diff --git a/ElectricLocomotive/FormLocomotiveCollection.resx b/ElectricLocomotive/FormLocomotiveCollection.resx index a0623c8..a39c409 100644 --- a/ElectricLocomotive/FormLocomotiveCollection.resx +++ b/ElectricLocomotive/FormLocomotiveCollection.resx @@ -120,4 +120,7 @@ 17, 17 + + 25 + \ No newline at end of file diff --git a/ElectricLocomotive/Generics/DrawiningLocomotiveEqutables.cs b/ElectricLocomotive/Generics/DrawiningLocomotiveEqutables.cs new file mode 100644 index 0000000..a403a0d --- /dev/null +++ b/ElectricLocomotive/Generics/DrawiningLocomotiveEqutables.cs @@ -0,0 +1,63 @@ +using ProjectElectricLocomotive.DrawningObjects; +using ProjectElectricLocomotive.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Generics +{ + internal class DrawiningLocomotiveEqutables : IEqualityComparer + { + public bool Equals(DrawningLocomotive? x, DrawningLocomotive? y) + { + if (x == null || x.EntityLocomotive == null) + { + throw new ArgumentException(nameof(x)); + } + if (y == null || y.EntityLocomotive == null) + { + throw new ArgumentException(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 DrawningElectricLocomotive && y is DrawningElectricLocomotive) + { + if (((EntityElectricLocomotive)x.EntityLocomotive).AdditionalColor != ((EntityElectricLocomotive)y.EntityLocomotive).AdditionalColor) + { + return false; + } + if (((EntityElectricLocomotive)x.EntityLocomotive).Battery != ((EntityElectricLocomotive)y.EntityLocomotive).Battery) + { + return false; + } + if (((EntityElectricLocomotive)x.EntityLocomotive).Horns != ((EntityElectricLocomotive)y.EntityLocomotive).Horns) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningLocomotive? obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/ElectricLocomotive/Generics/LocomotiveCompareByColor.cs b/ElectricLocomotive/Generics/LocomotiveCompareByColor.cs new file mode 100644 index 0000000..ac9ad53 --- /dev/null +++ b/ElectricLocomotive/Generics/LocomotiveCompareByColor.cs @@ -0,0 +1,35 @@ +using ProjectElectricLocomotive.DrawningObjects; +using ProjectElectricLocomotive.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Generics +{ + internal class LocomotiveCompareByColor : IComparer + { + public int Compare(DrawningLocomotive? x, DrawningLocomotive? 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 is DrawningElectricLocomotive && y is DrawningElectricLocomotive)) + { + return x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb()); + } + if (x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb()) != 0) + { + return x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb()); + } + return ((EntityElectricLocomotive)x.EntityLocomotive).AdditionalColor.ToArgb().CompareTo( + ((EntityElectricLocomotive)y.EntityLocomotive).AdditionalColor.ToArgb()); + } + } +} diff --git a/ElectricLocomotive/Generics/LocomotiveCompareByType.cs b/ElectricLocomotive/Generics/LocomotiveCompareByType.cs new file mode 100644 index 0000000..4b0dd9e --- /dev/null +++ b/ElectricLocomotive/Generics/LocomotiveCompareByType.cs @@ -0,0 +1,34 @@ +using ProjectElectricLocomotive.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Generics +{ + internal class LocomotiveCompareByType : IComparer + { + public int Compare(DrawningLocomotive? x, DrawningLocomotive? 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 sreedCompare = x.EntityLocomotive.Speed.CompareTo(y.EntityLocomotive.Speed); + if (sreedCompare != 0) + { + return sreedCompare; + } + return x.EntityLocomotive.Weight.CompareTo(y.EntityLocomotive.Weight); + } + } +} diff --git a/ElectricLocomotive/Generics/LocomotivesCollectionInfo.cs b/ElectricLocomotive/Generics/LocomotivesCollectionInfo.cs new file mode 100644 index 0000000..351c7dc --- /dev/null +++ b/ElectricLocomotive/Generics/LocomotivesCollectionInfo.cs @@ -0,0 +1,25 @@ +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 (ReferenceEquals(null, other)) return false; + return Name.Equals(other.Name); + } + public override int GetHashCode() => Name.GetHashCode(); + } +} diff --git a/ElectricLocomotive/Generics/LocomotivesGenericCollection.cs b/ElectricLocomotive/Generics/LocomotivesGenericCollection.cs index 6df5d63..e88495b 100644 --- a/ElectricLocomotive/Generics/LocomotivesGenericCollection.cs +++ b/ElectricLocomotive/Generics/LocomotivesGenericCollection.cs @@ -62,7 +62,7 @@ namespace ProjectElectricLocomotive.Generics { return 0; } - if (collect?._collection.Insert(obj) ?? false) + if (collect?._collection.Insert(obj, new DrawiningLocomotiveEqutables()) ?? false) { return collect._collection.Count; } @@ -150,5 +150,10 @@ namespace ProjectElectricLocomotive.Generics /// Получение объектов коллекции /// public IEnumerable GetLocomotives => _collection.GetLocomotives(); + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); } } \ No newline at end of file diff --git a/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs b/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs index 012d4ce..895a7f4 100644 --- a/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs +++ b/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs @@ -17,14 +17,14 @@ namespace ProjectElectricLocomotive.Generics /// /// Словарь (хранилище) /// - readonly Dictionary > _locomotiveStorage; /// /// Возвращение списка названий наборов /// - public List Keys => _locomotiveStorage.Keys.ToList(); + public List Keys => _locomotiveStorage.Keys.ToList(); /// /// Ширина окна отрисовки @@ -55,7 +55,7 @@ namespace ProjectElectricLocomotive.Generics /// public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) { - _locomotiveStorage = new Dictionary>(); _pictureWidth = pictureWidth; @@ -68,11 +68,12 @@ namespace ProjectElectricLocomotive.Generics /// Название набора public void AddSet(string name) { - if (Keys.Contains(name)) + LocomotivesCollectionInfo locomotivesCollectionInfo = new LocomotivesCollectionInfo(name, ""); + if (Keys.Contains(locomotivesCollectionInfo)) { return; }; - _locomotiveStorage.Add(name, + _locomotiveStorage.Add(locomotivesCollectionInfo, new LocomotivesGenericCollection< DrawningLocomotive, DrawningObjectLocomotive>( _pictureWidth, _pictureHeight)); @@ -83,9 +84,10 @@ namespace ProjectElectricLocomotive.Generics /// Название набора public void DelSet(string name) { - if (Keys.Contains(name)) + LocomotivesCollectionInfo locomotivesCollectionInfo = new LocomotivesCollectionInfo(name, ""); + if (Keys.Contains(locomotivesCollectionInfo)) { - _locomotiveStorage.Remove(name); + _locomotiveStorage.Remove(locomotivesCollectionInfo); } } /// @@ -98,9 +100,10 @@ namespace ProjectElectricLocomotive.Generics { get { - if (Keys.Contains(ind)) + LocomotivesCollectionInfo locomotivesCollectionInfo = new LocomotivesCollectionInfo(ind, ""); + if (Keys.Contains(locomotivesCollectionInfo)) { - return _locomotiveStorage[ind]; + return _locomotiveStorage[locomotivesCollectionInfo]; } return null; } @@ -126,11 +129,11 @@ namespace ProjectElectricLocomotive.Generics throw new InvalidOperationException("Невалиданя операция, нет данных длясохранения"); } foreach ( - KeyValuePair> record in _locomotiveStorage) { - sw.Write(record.Key); + sw.Write(record.Key.Name); sw.Write(_separatorForKeyValue); foreach (DrawningLocomotive? elem in record.Value.GetLocomotives) { @@ -189,7 +192,8 @@ namespace ProjectElectricLocomotive.Generics } } } - _locomotiveStorage.Add(record[0], collection); + LocomotivesCollectionInfo locomotivesCollectionInfo = new LocomotivesCollectionInfo(record[0], ""); + _locomotiveStorage.Add(locomotivesCollectionInfo, collection); } return true; } diff --git a/ElectricLocomotive/Generics/SetGeneric.cs b/ElectricLocomotive/Generics/SetGeneric.cs index fbaf54f..9129456 100644 --- a/ElectricLocomotive/Generics/SetGeneric.cs +++ b/ElectricLocomotive/Generics/SetGeneric.cs @@ -41,13 +41,9 @@ namespace ProjectElectricLocomotive.Generics /// /// Добавляемый локомотив /// - public bool Insert(T locomotive) + public bool Insert(T locomotive, IEqualityComparer? equal = null) { - if (_places.Count >= _maxCount) { - throw new StorageOverflowException(_maxCount); - } - _places.Insert(0, locomotive); - return true; + return Insert(locomotive, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -55,12 +51,17 @@ namespace ProjectElectricLocomotive.Generics /// Добавляемый поезд /// Позиция /// - public bool Insert(T locomotive, int position) + public bool Insert(T locomotive, int position, IEqualityComparer? equal = null) { - if (_places.Count >= _maxCount || position >= 0 || position <= _places.Count) + if (_places.Count >= _maxCount || position < 0 || position > _places.Count) { return false; } + if (equal != null && _places.Contains(locomotive, equal)) + { + throw new ArgumentException("объект уже находится в коллекции"); + } + _places.Insert(position, locomotive); return true; } @@ -118,6 +119,10 @@ namespace ProjectElectricLocomotive.Generics } } } - + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); } } \ No newline at end of file diff --git a/ElectricLocomotive/LogicFormLocomotiveCollection.cs b/ElectricLocomotive/LogicFormLocomotiveCollection.cs index ba3cb51..2d3a800 100644 --- a/ElectricLocomotive/LogicFormLocomotiveCollection.cs +++ b/ElectricLocomotive/LogicFormLocomotiveCollection.cs @@ -43,7 +43,7 @@ namespace ProjectElectricLocomotive listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(_storage.Keys[i]); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) { @@ -147,6 +147,10 @@ namespace ProjectElectricLocomotive MessageBox.Show(ex.Message); _logger.LogWarning(ex.ToString()); } + catch (ArgumentException ex) { + MessageBox.Show(ex.Message); + _logger.LogWarning(ex.ToString()); + } } /// @@ -271,5 +275,37 @@ namespace ProjectElectricLocomotive } } } + /// + /// Сортировка по сравнителю + /// + /// + 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 ButtonSortByType_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLocomotives(new LocomotiveCompareByColor()); + } } -- 2.25.1 From f70ddb35998f7aa7e4a404ae294d161d40c7ef95 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 20 Dec 2023 08:44:24 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BA=20=D1=81=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ElectricLocomotive/Generics/LocomotiveCompareByColor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ElectricLocomotive/Generics/LocomotiveCompareByColor.cs b/ElectricLocomotive/Generics/LocomotiveCompareByColor.cs index ac9ad53..af0aa18 100644 --- a/ElectricLocomotive/Generics/LocomotiveCompareByColor.cs +++ b/ElectricLocomotive/Generics/LocomotiveCompareByColor.cs @@ -24,7 +24,7 @@ namespace ProjectElectricLocomotive.Generics { return x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb()); } - if (x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb()) != 0) + if (x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb()) == 0) { return x.EntityLocomotive.BodyColor.ToArgb().CompareTo(y.EntityLocomotive.BodyColor.ToArgb()); } -- 2.25.1