diff --git a/Boats/Boats/BoatCompareByColor.cs b/Boats/Boats/BoatCompareByColor.cs new file mode 100644 index 0000000..a623f73 --- /dev/null +++ b/Boats/Boats/BoatCompareByColor.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Boats +{ + internal class BoatCompareByColor : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xBoat = x as DrawingObjectBoat; + var yBoat = y as DrawingObjectBoat; + if (xBoat == null && yBoat == null) + { + return 0; + } + if (xBoat == null && yBoat != null) + { + return 1; + } + if (xBoat != null && yBoat == null) + { + return -1; + } + string xBoatColor = xBoat.GetBoat.Boat.BodyColor.Name; + string yBoatColor = yBoat.GetBoat.Boat.BodyColor.Name; + if (xBoatColor != yBoatColor) + { + return xBoatColor.CompareTo(yBoatColor); + } + if (xBoat.GetBoat.GetType().Name != yBoat.GetBoat.GetType().Name) + { + if (xBoat.GetBoat.GetType().Name == "DrawingBoat") + { + return -1; + } + return 1; + } + if (xBoat.GetBoat.Boat is EntityCatamaran xCatamaran && + yBoat.GetBoat.Boat is EntityCatamaran yCatamaran) + { + string xBoatDopColor = xCatamaran.DopColor.Name; + string yBoatDopColor = yCatamaran.DopColor.Name; + var dopColorCompare = xBoatDopColor.CompareTo(yBoatDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xBoat.GetBoat.Boat.Speed.CompareTo(yBoat.GetBoat.Boat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xBoat.GetBoat.Boat.Weight.CompareTo(yBoat.GetBoat.Boat.Weight); + } + } +} diff --git a/Boats/Boats/BoatCompareByType.cs b/Boats/Boats/BoatCompareByType.cs new file mode 100644 index 0000000..54bbebc --- /dev/null +++ b/Boats/Boats/BoatCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Boats +{ + internal class BoatCompareByType : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xBoat = x as DrawingObjectBoat; + var yBoat = y as DrawingObjectBoat; + if (xBoat == null && yBoat == null) + { + return 0; + } + if (xBoat == null && yBoat != null) + { + return 1; + } + if (xBoat != null && yBoat == null) + { + return -1; + } + if (xBoat.GetBoat.GetType().Name != yBoat.GetBoat.GetType().Name) + { + if (xBoat.GetBoat.GetType().Name == "DrawingBoat") + { + return -1; + } + return 1; + } + var speedCompare = xBoat.GetBoat.Boat.Speed.CompareTo(yBoat.GetBoat.Boat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xBoat.GetBoat.Boat.Weight.CompareTo(yBoat.GetBoat.Boat.Weight); + } + } +} diff --git a/Boats/Boats/DrawingObjectBoat.cs b/Boats/Boats/DrawingObjectBoat.cs index add9704..34babdf 100644 --- a/Boats/Boats/DrawingObjectBoat.cs +++ b/Boats/Boats/DrawingObjectBoat.cs @@ -9,6 +9,7 @@ namespace Boats internal class DrawingObjectBoat : IDrawingObject { private DrawingBoat _boat = null; + public DrawingBoat GetBoat => _boat; public DrawingObjectBoat(DrawingBoat boat) { _boat = boat; diff --git a/Boats/Boats/FormMapWithSetBoats.Designer.cs b/Boats/Boats/FormMapWithSetBoats.Designer.cs index 6097658..311cd70 100644 --- a/Boats/Boats/FormMapWithSetBoats.Designer.cs +++ b/Boats/Boats/FormMapWithSetBoats.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.groupBoxInstruments = new System.Windows.Forms.GroupBox(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.ButtonDown = new System.Windows.Forms.Button(); this.ButtonRight = new System.Windows.Forms.Button(); this.ButtonLeft = new System.Windows.Forms.Button(); @@ -59,6 +61,8 @@ // // groupBoxInstruments // + this.groupBoxInstruments.Controls.Add(this.buttonSortByColor); + this.groupBoxInstruments.Controls.Add(this.buttonSortByType); this.groupBoxInstruments.Controls.Add(this.ButtonDown); this.groupBoxInstruments.Controls.Add(this.ButtonRight); this.groupBoxInstruments.Controls.Add(this.ButtonLeft); @@ -76,6 +80,26 @@ this.groupBoxInstruments.TabStop = false; this.groupBoxInstruments.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(9, 354); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(232, 29); + this.buttonSortByColor.TabIndex = 12; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(9, 321); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(232, 29); + this.buttonSortByType.TabIndex = 11; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // // ButtonDown // this.ButtonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); @@ -270,14 +294,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(166, 26); this.SaveToolStripMenuItem.Text = "Сохранить"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(166, 26); this.LoadToolStripMenuItem.Text = "Загрузить"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -339,5 +363,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/Boats/Boats/FormMapWithSetBoats.cs b/Boats/Boats/FormMapWithSetBoats.cs index 213d5f8..55a7887 100644 --- a/Boats/Boats/FormMapWithSetBoats.cs +++ b/Boats/Boats/FormMapWithSetBoats.cs @@ -28,10 +28,6 @@ namespace Boats private readonly MapsCollection _mapsCollection; private readonly ILogger _logger; /// - /// Объект от класса карты с набором объектов - /// - private MapWithSetBoatsGeneric _mapBoatsCollectionGeneric; - /// /// Конструктор /// public FormMapWithSetBoats(ILogger logger) @@ -72,28 +68,8 @@ namespace Boats /// private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { - AbstractMap map = null; - switch (ComboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Линии карта": - map = new LineMap(); - break; - case "Океан карта": - map = new OceanMap(); - break; - } - if (map != null) - { - _mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric( - pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapBoatsCollectionGeneric = null; - } + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty]?.ShowSet(); + _logger.LogInformation($"Переход на карту: {listBoxMaps.SelectedItem?.ToString() ?? string.Empty}"); } /// /// Добавление объекта @@ -291,6 +267,12 @@ namespace Boats MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch (ArgumentException ex) + { + _logger.LogWarning($"Ошибка добавления объекта: {ex.Message}"); + MessageBox.Show($"Ошибка добавления объекта: {ex.Message}", + "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } /// /// Обработка нажатия "Сохранить" @@ -338,5 +320,25 @@ namespace Boats } } } + + private void buttonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BoatCompareByType()); + 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 BoatCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/Boats/Boats/MapWithSetBoatsGeneric.cs b/Boats/Boats/MapWithSetBoatsGeneric.cs index 92f94b4..2165096 100644 --- a/Boats/Boats/MapWithSetBoatsGeneric.cs +++ b/Boats/Boats/MapWithSetBoatsGeneric.cs @@ -264,5 +264,9 @@ namespace Boats _setBoats.Insert(DrawingObjectBoat.Create(rec) as T); } } + public void Sort(IComparer comparer) + { + _setBoats.SortSet(comparer); + } } } diff --git a/Boats/Boats/SetBoatsGeneric.cs b/Boats/Boats/SetBoatsGeneric.cs index 53f70a2..0388020 100644 --- a/Boats/Boats/SetBoatsGeneric.cs +++ b/Boats/Boats/SetBoatsGeneric.cs @@ -119,5 +119,13 @@ namespace Boats } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }