diff --git a/AirplaneWithRadar/AirplaneWithRadar/AbstractMap.cs b/AirplaneWithRadar/AirplaneWithRadar/AbstractMap.cs index 8fbbabd..d5669b8 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AbstractMap.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirplaneWithRadar { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -117,5 +117,32 @@ namespace AirplaneWithRadar protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + public bool Equals(AbstractMap? other) + { + if (other == null || + _map != other._map || + _width != other._width || + _size_x != other._size_x || + _size_y != other._size_y || + _height != other._height || + GetType() != other.GetType() || + _map.GetLength(0) != other._map.GetLength(0) || + _map.GetLength(1) != other._map.GetLength(1)) + { + return false; + } + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] != other._map[i, j]) + { + return false; + } + } + } + return true; + } } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs new file mode 100644 index 0000000..f2f615a --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByColor.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace AirplaneWithRadar +{ + internal class AirplaneCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xAirplane = x as DrawningObjectAirplane; + var yAirplane = y as DrawningObjectAirplane; + if (xAirplane == null && yAirplane == null) + { + return 0; + } + if (xAirplane == null && yAirplane != null) + { + return 1; + } + if (xAirplane != null && yAirplane == null) + { + return -1; + } + + var color = xAirplane.GetAirplane.Airplane.BodyColor.ToArgb().CompareTo(yAirplane.GetAirplane.Airplane.BodyColor.ToArgb()); + if (color != 0 || + xAirplane.GetAirplane.Airplane is not EntityAirplaneWithRadar xEntityAirplaneWithRadar || + yAirplane.GetAirplane.Airplane is not EntityAirplaneWithRadar yEntityAirplaneWithRadar) + { + return color; + } + return xEntityAirplaneWithRadar.DopColor.ToArgb().CompareTo(yEntityAirplaneWithRadar.DopColor.ToArgb()); + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs new file mode 100644 index 0000000..e7c33a7 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar +{ + internal class AirplaneCompareByType : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xAirplane = x as DrawningObjectAirplane; + var yAirplane = y as DrawningObjectAirplane; + if (xAirplane == null && yAirplane == null) + { + return 0; + } + if (xAirplane == null && yAirplane != null) + { + return 1; + } + if (xAirplane != null && yAirplane == null) + { + return -1; + } + if (xAirplane.GetAirplane.GetType().Name != yAirplane.GetAirplane.GetType().Name) + { + if (xAirplane.GetAirplane.GetType().Name == "DrawningAirplane") + { + return -1; + } + return 1; + } + var speedCompare = xAirplane.GetAirplane.Airplane.Speed.CompareTo(yAirplane.GetAirplane.Airplane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirplane.GetAirplane.Airplane.Weight.CompareTo(yAirplane.GetAirplane.Airplane.Weight); + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs index 1675811..51575b5 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs @@ -13,6 +13,9 @@ namespace AirplaneWithRadar { _airplane = airplane; } + + public DrawningAirplane GetAirplane => _airplane; + public float Step => _airplane?.Airplane?.Step ?? 0; public (float Left, float Top, float Right, float Bottom) GetCurrentPosition() @@ -35,5 +38,45 @@ namespace AirplaneWithRadar public string GetInfo() => _airplane?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectAirplane(data.CreateDrawningAirplane()); + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherAirplane = other as DrawningObjectAirplane; + if (otherAirplane == null) + { + return false; + } + var airplane = _airplane.Airplane; + var otherAirplaneAirplane = otherAirplane._airplane.Airplane; + if (airplane.Speed != otherAirplaneAirplane.Speed) + { + return false; + } + if (airplane.Weight != otherAirplaneAirplane.Weight) + { + return false; + } + if (airplane.BodyColor != otherAirplaneAirplane.BodyColor) + { + return false; + } + if (airplane.GetType().Name != otherAirplaneAirplane.GetType().Name) + { + return false; + } + + if (airplane is EntityAirplaneWithRadar entityAirplaneWithRadar && + otherAirplaneAirplane is EntityAirplaneWithRadar otherAirplaneWithRadar && ( + entityAirplaneWithRadar.Radar != otherAirplaneWithRadar.Radar || + entityAirplaneWithRadar.DopColor != otherAirplaneWithRadar.DopColor || + entityAirplaneWithRadar.FuelTanks != otherAirplaneWithRadar.FuelTanks)) + { + return false; + } + return true; + } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.Designer.cs index 41244e4..c2f2044 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.Designer.cs @@ -51,6 +51,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByType); + this.groupBoxTools.Controls.Add(this.buttonSortByColor); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonRight); @@ -72,7 +76,7 @@ this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(801, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 545); + this.groupBoxTools.Size = new System.Drawing.Size(200, 637); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -141,7 +145,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(14, 337); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(14, 420); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(174, 23); @@ -152,7 +156,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(121, 509); + this.buttonRight.Location = new System.Drawing.Point(121, 601); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 8; @@ -164,7 +168,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(49, 509); + this.buttonLeft.Location = new System.Drawing.Point(49, 601); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 7; @@ -176,7 +180,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(85, 509); + this.buttonDown.Location = new System.Drawing.Point(85, 601); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 6; @@ -188,7 +192,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.arrUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(85, 473); + this.buttonUp.Location = new System.Drawing.Point(85, 565); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 5; @@ -197,7 +201,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(14, 440); + this.buttonShowOnMap.Location = new System.Drawing.Point(14, 523); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(174, 34); this.buttonShowOnMap.TabIndex = 4; @@ -207,7 +211,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(14, 402); + this.buttonShowStorage.Location = new System.Drawing.Point(14, 485); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(174, 32); this.buttonShowStorage.TabIndex = 3; @@ -217,7 +221,7 @@ // // buttonRemoveAirplane // - this.buttonRemoveAirplane.Location = new System.Drawing.Point(14, 366); + this.buttonRemoveAirplane.Location = new System.Drawing.Point(14, 449); this.buttonRemoveAirplane.Name = "buttonRemoveAirplane"; this.buttonRemoveAirplane.Size = new System.Drawing.Size(174, 30); this.buttonRemoveAirplane.TabIndex = 2; @@ -227,7 +231,7 @@ // // buttonAddAirplane // - this.buttonAddAirplane.Location = new System.Drawing.Point(14, 298); + this.buttonAddAirplane.Location = new System.Drawing.Point(14, 381); this.buttonAddAirplane.Name = "buttonAddAirplane"; this.buttonAddAirplane.Size = new System.Drawing.Size(175, 33); this.buttonAddAirplane.TabIndex = 1; @@ -240,7 +244,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(801, 545); + this.pictureBox.Size = new System.Drawing.Size(801, 637); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -285,11 +289,31 @@ // this.openFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(13, 287); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(175, 35); + this.buttonSortByType.TabIndex = 16; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(13, 328); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(175, 35); + this.buttonSortByColor.TabIndex = 15; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormMapWithSetAirplanes // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1001, 569); + this.ClientSize = new System.Drawing.Size(1001, 661); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -333,5 +357,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs index f661403..48d42fd 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/FormMapWithSetAirplanes.cs @@ -154,6 +154,11 @@ namespace AirplaneWithRadar _logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch (ArgumentException ex) + { + _logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, airplane); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } /// /// Удаление объекта @@ -296,5 +301,27 @@ namespace AirplaneWithRadar } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? + string.Empty].Sort(new AirplaneCompareByType()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? + string.Empty].Sort(new AirplaneCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs b/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs index 1a63bcd..5e820df 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirplaneWithRadar { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs index 62eee4e..84c4e2d 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/MapWithSetAirplanesGeneric.cs @@ -6,7 +6,7 @@ /// /// internal class MapWithSetAirplanesGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -136,7 +136,14 @@ _setAirplanes.Insert(DrawningObjectAirplane.Create(rec) as T); } } - + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setAirplanes.SortSet(comparer); + } /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs index d1c304b..79418d9 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/SetAirplanesGeneric.cs @@ -5,7 +5,7 @@ /// /// internal class SetAirplanesGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -48,6 +48,8 @@ /// public int Insert(T airplane, int position) { + if (_places.Contains(airplane)) + throw new ArgumentException($"Такой объект: {airplane}, уже есть в наборе"); if (Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!isCorrectPosition(position)) @@ -104,5 +106,17 @@ } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }