diff --git a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/AbstractCompany.cs b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/AbstractCompany.cs index 5469fca..4c3ea16 100644 --- a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/AbstractCompany.cs @@ -64,7 +64,7 @@ public abstract class AbstractCompany /// public static int operator +(AbstractCompany company, DrawningWarPlane plane) { - if (company._collection.Insert(plane)) + if (company._collection.Insert(plane, new DrawiningWarPlaneEqutables())) { return 1; } @@ -120,7 +120,11 @@ public abstract class AbstractCompany return bitmap; } - + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); /// /// Вывод заднего фона /// diff --git a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/ICollectionGenericObjects.cs index 48a71a3..1a5195c 100644 --- a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -23,7 +23,7 @@ public interface ICollectionGenericObjects /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj); + bool Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию @@ -31,7 +31,7 @@ public interface ICollectionGenericObjects /// Добавляемый объект /// Позиция /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj, int position); + bool Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции @@ -57,4 +57,10 @@ public interface ICollectionGenericObjects /// /// Поэлементый вывод элементов коллекции IEnumerable GetItems(); + + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } \ No newline at end of file diff --git a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/ListGenericObjects.cs b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/ListGenericObjects.cs index 5720473..2fdbafd 100644 --- a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/ListGenericObjects.cs @@ -55,7 +55,7 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public bool Insert(T obj) + public bool Insert(T obj, IEqualityComparer? comparer = null) { if (Count + 1 > MaxCount) { @@ -66,7 +66,7 @@ public class ListGenericObjects : ICollectionGenericObjects return true; } - public bool Insert(T obj, int position) + public bool Insert(T obj, int position, IEqualityComparer? comparer = null) { if (_collection.Count + 1 < _maxCount) { @@ -98,4 +98,9 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } \ No newline at end of file diff --git a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/MassiveGenericObjects.cs index 88ea6af..3f5c13f 100644 --- a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/MassiveGenericObjects.cs @@ -62,7 +62,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public bool Insert(T obj) + public bool Insert(T obj, IEqualityComparer? comparer = null) { int index = Array.IndexOf(_collection, null); @@ -75,7 +75,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects } - public bool Insert(T obj, int position) + public bool Insert(T obj, int position, IEqualityComparer? comparer = null) { if(position < 0 || position > _collection.Length -1) @@ -120,9 +120,19 @@ public class MassiveGenericObjects : ICollectionGenericObjects public IEnumerable GetItems() { - for (int i = 0; i < _collection.Length; ++i) + for (int i = 0; i < _collection.Length; ++i) { yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + if(_collection?.Length > 0) + { + Array.Sort(_collection, comparer); + //_collection = _collection.OrderBy(warPlane => warPlane == null).ToArray(); + } + + } } \ No newline at end of file diff --git a/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawiningWarPlaneEqutables.cs b/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawiningWarPlaneEqutables.cs new file mode 100644 index 0000000..3f87daa --- /dev/null +++ b/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawiningWarPlaneEqutables.cs @@ -0,0 +1,71 @@ +using ProjectStormTrooper.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectStormTrooper.Drawnings; + +/// +/// Реализация сравнения двух объектов класса-прорисовки +/// +public class DrawiningWarPlaneEqutables : IEqualityComparer +{ + public bool Equals(DrawningWarPlane? x, DrawningWarPlane? y) + { + if (x == null || x.EntityWarPlane == null) + { + return false; + } + + if (y == null || y.EntityWarPlane == null) + { + return false; + } + + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + + if (x.EntityWarPlane.Speed != y.EntityWarPlane.Speed) + { + return false; + } + + if (x.EntityWarPlane.Weight != y.EntityWarPlane.Weight) + { + return false; + } + + if (x.EntityWarPlane.BodyColor != y.EntityWarPlane.BodyColor) + { + return false; + } + if(x.EntityWarPlane.ColorOfHead != y.EntityWarPlane.ColorOfHead) + { + return false; + } + if (x is DrawningStormTrooper && y is DrawningStormTrooper) + { + EntityStormTrooper entityX = (EntityStormTrooper)x.EntityWarPlane; + EntityStormTrooper entityY = (EntityStormTrooper)y.EntityWarPlane; + if(entityX.Rockets != entityY.Rockets) + { + return false; + } + if(entityX.Bombs != entityY.Bombs) + { + return false; + } + if(entityX.AdditionalColor != entityY.AdditionalColor) + { + return false; + } + } + + return true; + } + + public int GetHashCode([DisallowNull] DrawningWarPlane obj) + { + return obj.GetHashCode(); + } +} \ No newline at end of file diff --git a/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawningWarPlaneCompareByColor.cs b/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawningWarPlaneCompareByColor.cs new file mode 100644 index 0000000..5951438 --- /dev/null +++ b/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawningWarPlaneCompareByColor.cs @@ -0,0 +1,34 @@ +namespace ProjectStormTrooper.Drawnings; + +/// +/// Сравнение по цвету, скорости, весу +/// +public class DrawningWarPlaneCompareByColor : IComparer +{ + public int Compare(DrawningWarPlane? x, DrawningWarPlane? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null || x.EntityWarPlane == null) + { + return -1; + } + + if (y == null || y.EntityWarPlane == null) + { + return 1; + } + if(x.EntityWarPlane.BodyColor.Name != y.EntityWarPlane.BodyColor.Name) + { + return x.EntityWarPlane.BodyColor.Name.CompareTo(y.EntityWarPlane.BodyColor.Name); + } + var speedCompare = x.EntityWarPlane.Speed.CompareTo(y.EntityWarPlane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityWarPlane.Weight.CompareTo(y.EntityWarPlane.Weight); + } +} \ No newline at end of file diff --git a/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawningWarPlaneCompareByType.cs b/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawningWarPlaneCompareByType.cs new file mode 100644 index 0000000..4a64066 --- /dev/null +++ b/ProjectStormTrooper/ProjectStormTrooper/Drawnings/DrawningWarPlaneCompareByType.cs @@ -0,0 +1,39 @@ + + +namespace ProjectStormTrooper.Drawnings; +/// +/// Сравнение по типу, скорости, весу +/// +public class DrawningWarPlaneCompareByType : IComparer +{ + public int Compare(DrawningWarPlane? x, DrawningWarPlane? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null || x.EntityWarPlane == null) + { + return -1; + } + + if (y == null || y.EntityWarPlane == null) + { + return 1; + } + + if (!x.GetType().Name.Equals(y.GetType().Name)) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + + var speedCompare = x.EntityWarPlane.Speed.CompareTo(y.EntityWarPlane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + + + return x.EntityWarPlane.Weight.CompareTo(y.EntityWarPlane.Weight); + } +} \ No newline at end of file diff --git a/ProjectStormTrooper/ProjectStormTrooper/FormWarPlaneCollection.Designer.cs b/ProjectStormTrooper/ProjectStormTrooper/FormWarPlaneCollection.Designer.cs index 3900342..1e4d2e8 100644 --- a/ProjectStormTrooper/ProjectStormTrooper/FormWarPlaneCollection.Designer.cs +++ b/ProjectStormTrooper/ProjectStormTrooper/FormWarPlaneCollection.Designer.cs @@ -39,6 +39,8 @@ labelCollectionName = new Label(); buttonCreateCompany = new Button(); panelCompanyTools = new Panel(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); buttonGoToCheck = new Button(); buttonRefresh = new Button(); buttonRemoveWarPlane = new Button(); @@ -68,7 +70,7 @@ groupBox1.Dock = DockStyle.Right; groupBox1.Location = new Point(616, 28); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(197, 782); + groupBox1.Size = new Size(197, 737); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; @@ -157,7 +159,7 @@ // // buttonCreateCompany // - buttonCreateCompany.Location = new Point(8, 441); + buttonCreateCompany.Location = new Point(6, 399); buttonCreateCompany.Name = "buttonCreateCompany"; buttonCreateCompany.Size = new Size(172, 29); buttonCreateCompany.TabIndex = 8; @@ -167,6 +169,8 @@ // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Controls.Add(buttonRemoveWarPlane); @@ -174,14 +178,34 @@ panelCompanyTools.Controls.Add(buttonAddWarPlane); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 521); + panelCompanyTools.Location = new Point(3, 449); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(191, 258); + panelCompanyTools.Size = new Size(191, 285); panelCompanyTools.TabIndex = 7; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(5, 249); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(172, 27); + buttonSortByColor.TabIndex = 8; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(5, 218); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(172, 27); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // buttonGoToCheck // - buttonGoToCheck.Location = new Point(5, 185); + buttonGoToCheck.Location = new Point(5, 150); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(172, 27); buttonGoToCheck.TabIndex = 6; @@ -191,7 +215,7 @@ // // buttonRefresh // - buttonRefresh.Location = new Point(5, 218); + buttonRefresh.Location = new Point(5, 183); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(172, 29); buttonRefresh.TabIndex = 5; @@ -201,7 +225,7 @@ // // buttonRemoveWarPlane // - buttonRemoveWarPlane.Location = new Point(5, 128); + buttonRemoveWarPlane.Location = new Point(3, 93); buttonRemoveWarPlane.Name = "buttonRemoveWarPlane"; buttonRemoveWarPlane.Size = new Size(172, 51); buttonRemoveWarPlane.TabIndex = 4; @@ -211,7 +235,7 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(5, 95); + maskedTextBoxPosition.Location = new Point(5, 60); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(172, 27); @@ -232,7 +256,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Ангар" }); - comboBoxSelectorCompany.Location = new Point(8, 396); + comboBoxSelectorCompany.Location = new Point(6, 365); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(172, 28); comboBoxSelectorCompany.TabIndex = 0; @@ -243,7 +267,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 28); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(616, 782); + pictureBox.Size = new Size(616, 737); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -292,7 +316,7 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(813, 810); + ClientSize = new Size(813, 765); Controls.Add(pictureBox); Controls.Add(groupBox1); Controls.Add(menuStrip); @@ -337,5 +361,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/ProjectStormTrooper/ProjectStormTrooper/FormWarPlaneCollection.cs b/ProjectStormTrooper/ProjectStormTrooper/FormWarPlaneCollection.cs index 794b89a..3374702 100644 --- a/ProjectStormTrooper/ProjectStormTrooper/FormWarPlaneCollection.cs +++ b/ProjectStormTrooper/ProjectStormTrooper/FormWarPlaneCollection.cs @@ -58,13 +58,13 @@ namespace ProjectStormTrooper _logger.LogInformation($"Добавлен объект {warPlane.GetDataForSave()}"); pictureBox.Image = _company.Show(); } - catch(CollectionOverflowException ex) + catch (CollectionOverflowException ex) { MessageBox.Show("Не удалось добавить объект"); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } - + } /// @@ -111,12 +111,12 @@ namespace ProjectStormTrooper } - catch(ObjectNotFoundException ex) + catch (ObjectNotFoundException ex) { MessageBox.Show("Объект не найден"); _logger.LogWarning($"Удаление не найденного объекта в позиции {pos} "); } - catch(PositionOutOfCollectionException ex) + catch (PositionOutOfCollectionException ex) { MessageBox.Show("Удаление вне рамках коллекции"); _logger.LogWarning($"Удаление объекта за пределами коллекции {pos} "); @@ -303,7 +303,7 @@ namespace ProjectStormTrooper private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - if(openFileDialog.ShowDialog() == DialogResult.OK) + if (openFileDialog.ShowDialog() == DialogResult.OK) { try { @@ -312,16 +312,50 @@ namespace ProjectStormTrooper _logger.LogInformation("Загрузка из файла: {filename}", saveFileDialog.FileName); RerfreshListBoxItems(); } - catch(Exception ex) - { + catch (Exception ex) + { MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogError("Ошибка: {Message}", ex.Message); } - - + + } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + CompareCars(new DrawningWarPlaneCompareByType()); + } + + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + CompareCars(new DrawningWarPlaneCompareByColor()); + } + + /// + /// Сортировка по сравнителю + /// + /// Сравнитель объектов + private void CompareCars(IComparer comparer) + { + if (_company == null) + { + return; + } + + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } } }