From d1b29031d217676feb18e8993de029fc037b2754 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Sun, 10 Dec 2023 14:13:57 +0400 Subject: [PATCH] =?UTF-8?q?2=D0=BE=D0=B9=20=D1=88=D0=B0=D0=B3=20(=D1=81?= =?UTF-8?q?=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=82=D0=B8=D0=BF=D1=83=20=D0=B8=20=D1=86=D0=B2=D0=B5?= =?UTF-8?q?=D1=82=D1=83)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Airbus/FormAirbusCollection.Designer.cs | 66 +++++++++++++++------- Airbus/FormAirbusCollection.cs | 23 +++++++- Airbus/Generics/AirbusCompareByColor.cs | 34 +++++++++++ Airbus/Generics/AirbusCompareByType.cs | 30 ++++++++++ Airbus/Generics/AirbusGenericCollection.cs | 7 ++- Airbus/Generics/DrawningAirbusEqutables.cs | 57 +++++++++++++++++++ Airbus/Generics/SetGeneric.cs | 44 ++++++++------- 7 files changed, 217 insertions(+), 44 deletions(-) create mode 100644 Airbus/Generics/AirbusCompareByColor.cs create mode 100644 Airbus/Generics/AirbusCompareByType.cs create mode 100644 Airbus/Generics/DrawningAirbusEqutables.cs diff --git a/Airbus/FormAirbusCollection.Designer.cs b/Airbus/FormAirbusCollection.Designer.cs index fa03c11..0c151da 100644 --- a/Airbus/FormAirbusCollection.Designer.cs +++ b/Airbus/FormAirbusCollection.Designer.cs @@ -46,6 +46,8 @@ LoadToolStripMenuItem = new ToolStripMenuItem(); openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); + buttonSortByType = new Button(); + buttonSortByColor = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); menuStrip.SuspendLayout(); SuspendLayout(); @@ -53,10 +55,10 @@ // buttonAddAirbus // buttonAddAirbus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonAddAirbus.Location = new Point(654, 443); + buttonAddAirbus.Location = new Point(638, 458); buttonAddAirbus.Margin = new Padding(3, 4, 3, 4); buttonAddAirbus.Name = "buttonAddAirbus"; - buttonAddAirbus.Size = new Size(157, 37); + buttonAddAirbus.Size = new Size(173, 37); buttonAddAirbus.TabIndex = 0; buttonAddAirbus.Text = "Добавить самолёт"; buttonAddAirbus.UseVisualStyleBackColor = true; @@ -67,7 +69,7 @@ pictureBoxCollection.Location = new Point(0, 28); pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(630, 556); + pictureBoxCollection.Size = new Size(620, 545); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // @@ -76,7 +78,7 @@ labelInstruments.Anchor = AnchorStyles.Top | AnchorStyles.Right; labelInstruments.AutoSize = true; labelInstruments.Font = new Font("Segoe UI", 12F, FontStyle.Regular, GraphicsUnit.Point); - labelInstruments.Location = new Point(654, 23); + labelInstruments.Location = new Point(638, 28); labelInstruments.Name = "labelInstruments"; labelInstruments.Size = new Size(136, 28); labelInstruments.TabIndex = 2; @@ -85,10 +87,10 @@ // buttonUpdate // buttonUpdate.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonUpdate.Location = new Point(654, 173); + buttonUpdate.Location = new Point(638, 160); buttonUpdate.Margin = new Padding(3, 4, 3, 4); buttonUpdate.Name = "buttonUpdate"; - buttonUpdate.Size = new Size(150, 33); + buttonUpdate.Size = new Size(166, 33); buttonUpdate.TabIndex = 3; buttonUpdate.Text = "Обновить набор"; buttonUpdate.UseVisualStyleBackColor = true; @@ -97,10 +99,10 @@ // buttonDeleteAirbus // buttonDeleteAirbus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonDeleteAirbus.Location = new Point(654, 523); + buttonDeleteAirbus.Location = new Point(638, 537); buttonDeleteAirbus.Margin = new Padding(3, 4, 3, 4); buttonDeleteAirbus.Name = "buttonDeleteAirbus"; - buttonDeleteAirbus.Size = new Size(157, 37); + buttonDeleteAirbus.Size = new Size(173, 37); buttonDeleteAirbus.TabIndex = 4; buttonDeleteAirbus.Text = "Удалить самолёт"; buttonDeleteAirbus.UseVisualStyleBackColor = true; @@ -109,11 +111,11 @@ // maskedTextBoxNumber // maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right; - maskedTextBoxNumber.Location = new Point(654, 487); + maskedTextBoxNumber.Location = new Point(638, 502); maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(157, 27); + maskedTextBoxNumber.Size = new Size(173, 27); maskedTextBoxNumber.TabIndex = 5; maskedTextBoxNumber.ValidatingType = typeof(int); // @@ -121,7 +123,7 @@ // label1.Anchor = AnchorStyles.Top | AnchorStyles.Right; label1.AutoSize = true; - label1.Location = new Point(654, 62); + label1.Location = new Point(638, 65); label1.Name = "label1"; label1.Size = new Size(66, 20); label1.TabIndex = 7; @@ -131,17 +133,17 @@ // listBoxStorages.FormattingEnabled = true; listBoxStorages.ItemHeight = 20; - listBoxStorages.Location = new Point(654, 213); + listBoxStorages.Location = new Point(638, 196); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(150, 144); + listBoxStorages.Size = new Size(166, 124); listBoxStorages.TabIndex = 8; listBoxStorages.SelectedIndexChanged += listBoxObjects_SelectedIndexChanged; // // buttonAddStorage // - buttonAddStorage.Location = new Point(654, 136); + buttonAddStorage.Location = new Point(638, 124); buttonAddStorage.Name = "buttonAddStorage"; - buttonAddStorage.Size = new Size(150, 33); + buttonAddStorage.Size = new Size(166, 33); buttonAddStorage.TabIndex = 9; buttonAddStorage.Text = "Добавить набор"; buttonAddStorage.UseVisualStyleBackColor = true; @@ -149,9 +151,9 @@ // // buttonDeleteStorage // - buttonDeleteStorage.Location = new Point(654, 364); + buttonDeleteStorage.Location = new Point(638, 324); buttonDeleteStorage.Name = "buttonDeleteStorage"; - buttonDeleteStorage.Size = new Size(150, 33); + buttonDeleteStorage.Size = new Size(166, 33); buttonDeleteStorage.TabIndex = 10; buttonDeleteStorage.Text = "Удалить набор"; buttonDeleteStorage.UseVisualStyleBackColor = true; @@ -159,9 +161,9 @@ // // textBoxStorageName // - textBoxStorageName.Location = new Point(654, 103); + textBoxStorageName.Location = new Point(638, 93); textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(150, 27); + textBoxStorageName.Size = new Size(166, 27); textBoxStorageName.TabIndex = 11; // // menuStrip @@ -204,11 +206,33 @@ saveFileDialog.Filter = "txt file | *.txt"; saveFileDialog.OverwritePrompt = false; // + // buttonSortByType + // + buttonSortByType.Location = new Point(638, 372); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(166, 33); + buttonSortByType.TabIndex = 13; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(638, 408); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(166, 31); + buttonSortByColor.TabIndex = 14; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // // FormAirbusCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(827, 591); + ClientSize = new Size(827, 585); + Controls.Add(buttonSortByColor); + Controls.Add(buttonSortByType); Controls.Add(textBoxStorageName); Controls.Add(buttonDeleteStorage); Controls.Add(buttonAddStorage); @@ -252,5 +276,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/Airbus/FormAirbusCollection.cs b/Airbus/FormAirbusCollection.cs index 1cd01d6..e7a0938 100644 --- a/Airbus/FormAirbusCollection.cs +++ b/Airbus/FormAirbusCollection.cs @@ -29,9 +29,30 @@ namespace ProjectAirbus _logger = logger; } + private void buttonSortByType_Click(object sender, EventArgs e) => CompareAirbus(new AirbusCompareByType()); + + private void buttonSortByColor_Click(object sender, EventArgs e) => CompareAirbus(new AirbusCompareByColor()); + + // Сортировка по сравнителю + private void CompareAirbus(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.ShowAirbus(); + } + // Обработка нажатия "Сохранение" private void SaveToolStripMenuItem_Click(object sender, EventArgs e) - { + { if (saveFileDialog.ShowDialog() == DialogResult.OK) { try diff --git a/Airbus/Generics/AirbusCompareByColor.cs b/Airbus/Generics/AirbusCompareByColor.cs new file mode 100644 index 0000000..f85ce4d --- /dev/null +++ b/Airbus/Generics/AirbusCompareByColor.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAirbus.Drawnings; +using ProjectAirbus.Entities; + +namespace ProjectAirbus.Generics +{ + internal class AirbusCompareByColor : IComparer + { + public int Compare(DrawningAirbus? x, DrawningAirbus? y) + { + if (x == null || x.EntityAirbus == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityAirbus == null) + throw new ArgumentNullException(nameof(y)); + + var xPlane = x.EntityAirbus; + var yPlane = y.EntityAirbus; + + if (xPlane.BodyColor != yPlane.BodyColor) + return xPlane.BodyColor.Name.CompareTo(yPlane.BodyColor.Name); + + var speedCompare = x.EntityAirbus.Speed.CompareTo(y.EntityAirbus.Speed); + if (speedCompare != 0) + return speedCompare; + + return x.EntityAirbus.Weight.CompareTo(y.EntityAirbus.Weight); + } + } +} diff --git a/Airbus/Generics/AirbusCompareByType.cs b/Airbus/Generics/AirbusCompareByType.cs new file mode 100644 index 0000000..8e19526 --- /dev/null +++ b/Airbus/Generics/AirbusCompareByType.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAirbus.Drawnings; + +namespace ProjectAirbus.Generics +{ + internal class AirbusCompareByType : IComparer + { + public int Compare(DrawningAirbus? x, DrawningAirbus? y) + { + if (x == null || x.EntityAirbus == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityAirbus == null) + throw new ArgumentNullException(nameof(y)); + + if (x.GetType().Name != y.GetType().Name) + return x.GetType().Name.CompareTo(y.GetType().Name); + + var speedCompare = x.EntityAirbus.Speed.CompareTo(y.EntityAirbus.Speed); + if (speedCompare != 0) + return speedCompare; + + return x.EntityAirbus.Weight.CompareTo(y.EntityAirbus.Weight); + } + } +} \ No newline at end of file diff --git a/Airbus/Generics/AirbusGenericCollection.cs b/Airbus/Generics/AirbusGenericCollection.cs index 346b222..9b11744 100644 --- a/Airbus/Generics/AirbusGenericCollection.cs +++ b/Airbus/Generics/AirbusGenericCollection.cs @@ -16,8 +16,8 @@ namespace ProjectAirbus.Generics private readonly int _pictureWidth; private readonly int _pictureHeight; // Размер занимаемого места 89х34 - private readonly int _placeSizeWidth = 189; - private readonly int _placeSizeHeight = 134; + private readonly int _placeSizeWidth = 89; + private readonly int _placeSizeHeight = 34; // коллекция private readonly SetGeneric _collection; @@ -31,6 +31,9 @@ namespace ProjectAirbus.Generics _collection = new SetGeneric(width * height); } + // Сортировка + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + public static int operator +(AirbusGenericCollection collect, T? obj) { if (obj != null) diff --git a/Airbus/Generics/DrawningAirbusEqutables.cs b/Airbus/Generics/DrawningAirbusEqutables.cs new file mode 100644 index 0000000..a59d898 --- /dev/null +++ b/Airbus/Generics/DrawningAirbusEqutables.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics.CodeAnalysis; +using ProjectAirbus.Drawnings; +using ProjectAirbus.Entities; + +namespace ProjectAirbus.Generics +{ + internal class DrawningAirbusEqutables : IEqualityComparer + { + public bool Equals(DrawningAirbus? x, DrawningAirbus? y) + { + if (x == null && x.EntityAirbus == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null && y.EntityAirbus == null) + throw new ArgumentNullException(nameof(y)); + + if ((x.GetType().Name != y.GetType().Name)) + return false; + + if (x.EntityAirbus.Speed != y.EntityAirbus.Speed) + return false; + + if (x.EntityAirbus.Weight != y.EntityAirbus.Weight) + return false; + + if (x.EntityAirbus.BodyColor != y.EntityAirbus.BodyColor) + return false; + + if (x is DrawningPlane && y is DrawningPlane) + { + var xPlane = (EntityPlane)x.EntityAirbus; + var yPlane = (EntityPlane)y.EntityAirbus; + + if (xPlane.AdditionalColor != yPlane.AdditionalColor) + return false; + + if (xPlane.IsAdditionalEngine != yPlane.IsAdditionalEngine) + return false; + + if (xPlane.IsCompartment != yPlane.IsCompartment) + return false; + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningAirbus obj) + { + return obj.GetHashCode(); + } + + } +} diff --git a/Airbus/Generics/SetGeneric.cs b/Airbus/Generics/SetGeneric.cs index deeacf5..3c5faba 100644 --- a/Airbus/Generics/SetGeneric.cs +++ b/Airbus/Generics/SetGeneric.cs @@ -23,14 +23,17 @@ namespace ProjectAirbus.Generics _places = new List(count); } - // Добавление объекта в начало набора - public int Insert(T airbus) + // сортировка набора + public void SortSet(IComparer comparer) => _places.Sort(comparer); + + // Добавление объекта в начало набора + public int Insert(T airbus, IEqualityComparer? equal = null) { return Insert(airbus, 0); } // Добавление объекта в набор на конкретную позицию - public int Insert(T airbus, int position) + public int Insert(T airbus, int position, IEqualityComparer? equal = null) { if (Count >= _maxCount) { @@ -55,27 +58,27 @@ namespace ProjectAirbus.Generics return true; } - // Получение объекта из набора по позиции - public T? this[int position] + // Получение объекта из набора по позиции + public T? this[int position] + { + get { - get + if (position < 0 || position >= Count) { - if (position < 0 || position >= Count) - { - return null; - } - return _places[position]; - } - set - { - if (position < 0 || Count >= _maxCount) - { - return; - } - - _places.Insert(position, value); + return null; } + return _places[position]; } + set + { + if (position < 0 || Count >= _maxCount) + { + return; + } + + _places.Insert(position, value); + } + } // Проход по списку public IEnumerable GetAirbus(int? maxAirbus = null) @@ -89,6 +92,5 @@ namespace ProjectAirbus.Generics } } } - } }