diff --git a/Airbus/Airbus/AbstractMap.cs b/Airbus/Airbus/AbstractMap.cs index da78b80..4330bc9 100644 --- a/Airbus/Airbus/AbstractMap.cs +++ b/Airbus/Airbus/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Airbus { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; @@ -118,5 +118,32 @@ namespace Airbus 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; + } } } diff --git a/Airbus/Airbus/AirplaneCompareByColor.cs b/Airbus/Airbus/AirplaneCompareByColor.cs new file mode 100644 index 0000000..83d2f4d --- /dev/null +++ b/Airbus/Airbus/AirplaneCompareByColor.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Airbus +{ + 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; + } + string xAirplaneColor = xAirplane.GetAirplane.airplane.BodyColor.Name; + string yAirplaneColor = yAirplane.GetAirplane.airplane.BodyColor.Name; + if (xAirplaneColor != yAirplaneColor) + { + return xAirplaneColor.CompareTo(yAirplaneColor); + } + if (xAirplane.GetAirplane.airplane is EntityAirbus xAirbus && yAirplane.GetAirplane.airplane is EntityAirbus yAirbus) + { + string xAirplaneDopColor = xAirbus.DopColor.Name; + string yAirplaneDopColor = yAirbus.DopColor.Name; + var dopColorCompare = xAirplaneDopColor.CompareTo(yAirplaneDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + 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); + } + } +} \ No newline at end of file diff --git a/Airbus/Airbus/AirplaneCompareByType.cs b/Airbus/Airbus/AirplaneCompareByType.cs new file mode 100644 index 0000000..443f53a --- /dev/null +++ b/Airbus/Airbus/AirplaneCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Airbus +{ + 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/Airbus/Airbus/DrawningObjectAirplane.cs b/Airbus/Airbus/DrawningObjectAirplane.cs index 5ec45c4..7f5ff50 100644 --- a/Airbus/Airbus/DrawningObjectAirplane.cs +++ b/Airbus/Airbus/DrawningObjectAirplane.cs @@ -14,6 +14,7 @@ namespace Airbus { _airplane = airplane; } + public DrawningAirplane GetAirplane => _airplane; public float Step => _airplane?.airplane?.Step ?? 0; public void DrawningObject(Graphics g) { @@ -27,7 +28,7 @@ namespace Airbus { _airplane?.MoveTransport(direction); } - public void SetObject(int x, int y, int width, int height) + public void SetObject(int x, int y, int width, int height) { _airplane?.SetPosition(x, y, width, height); } @@ -35,5 +36,52 @@ namespace Airbus 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.GetType() != otherAirplaneAirplane.GetType()) + { + return false; + } + if (airplane.Speed != otherAirplaneAirplane.Speed) + { + return false; + } + if (airplane.Weight != otherAirplaneAirplane.Weight) + { + return false; + } + if (airplane.BodyColor != otherAirplaneAirplane.BodyColor) + { + return false; + } + + if (airplane is EntityAirbus airbus && otherAirplaneAirplane is EntityAirbus otherAirbus) + { + if (airbus.DopColor != otherAirbus.DopColor) + { + return false; + } + if (airbus.Engine != otherAirbus.Engine) + { + return false; + } + if (airbus.Compartment != otherAirbus.Compartment) + { + return false; + } + } + return true; + } } } diff --git a/Airbus/Airbus/FormMapWithSetAirplane.Designer.cs b/Airbus/Airbus/FormMapWithSetAirplane.Designer.cs index 4d1a28d..7eb3e71 100644 --- a/Airbus/Airbus/FormMapWithSetAirplane.Designer.cs +++ b/Airbus/Airbus/FormMapWithSetAirplane.Designer.cs @@ -51,6 +51,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + 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.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonDown); this.groupBoxTools.Controls.Add(this.buttonRight); @@ -267,14 +271,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -286,6 +290,26 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(12, 326); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(83, 38); + this.buttonSortByType.TabIndex = 11; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(101, 326); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(93, 38); + this.buttonSortByColor.TabIndex = 12; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // // FormMapWithSetAirplane // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -334,5 +358,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/Airbus/Airbus/FormMapWithSetAirplane.cs b/Airbus/Airbus/FormMapWithSetAirplane.cs index 3eb424c..f66c9b6 100644 --- a/Airbus/Airbus/FormMapWithSetAirplane.cs +++ b/Airbus/Airbus/FormMapWithSetAirplane.cs @@ -296,5 +296,27 @@ namespace Airbus pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } + + 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/Airbus/Airbus/IDrawningObject.cs b/Airbus/Airbus/IDrawningObject.cs index 1195eac..d71f508 100644 --- a/Airbus/Airbus/IDrawningObject.cs +++ b/Airbus/Airbus/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Airbus { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/Airbus/Airbus/MapWithSetAirplaneGeneric.cs b/Airbus/Airbus/MapWithSetAirplaneGeneric.cs index 4209b36..0c2dae2 100644 --- a/Airbus/Airbus/MapWithSetAirplaneGeneric.cs +++ b/Airbus/Airbus/MapWithSetAirplaneGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Airbus { internal class MapWithSetAirplaneGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -201,5 +201,9 @@ namespace Airbus _setAirplane.Insert(DrawningObjectAirplane.Create(rec) as T); } } + public void Sort(IComparer comparer) + { + _setAirplane.SortSet(comparer); + } } } diff --git a/Airbus/Airbus/SetAirplaneGeneric.cs b/Airbus/Airbus/SetAirplaneGeneric.cs index 8280990..da02b91 100644 --- a/Airbus/Airbus/SetAirplaneGeneric.cs +++ b/Airbus/Airbus/SetAirplaneGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Airbus { internal class SetAirplaneGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -24,6 +24,10 @@ namespace Airbus public int Insert(T airplane, int position) { + if (_places.Contains(airplane)) + { + return -1; + } if (Count == _maxCount) throw new StorageOverflowException(_maxCount); if (position < 0 || position > _places.Count) return -1; @@ -68,11 +72,11 @@ namespace Airbus } public IEnumerable GetAirplanes() { - foreach (var car in _places) + foreach (var airplane in _places) { - if (car != null) + if (airplane != null) { - yield return car; + yield return airplane; } else { @@ -80,6 +84,14 @@ namespace Airbus } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }