Сортировка

This commit is contained in:
Nikita Potapov 2022-12-27 10:27:47 +04:00
parent 9387eb7e2f
commit 30e5121c94
7 changed files with 196 additions and 28 deletions

View File

@ -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<IDrawingObject>
{
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);
}
}
}

View File

@ -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<IDrawingObject>
{
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);
}
}
}

View File

@ -9,6 +9,7 @@ namespace Boats
internal class DrawingObjectBoat : IDrawingObject internal class DrawingObjectBoat : IDrawingObject
{ {
private DrawingBoat _boat = null; private DrawingBoat _boat = null;
public DrawingBoat GetBoat => _boat;
public DrawingObjectBoat(DrawingBoat boat) public DrawingObjectBoat(DrawingBoat boat)
{ {
_boat = boat; _boat = boat;

View File

@ -29,6 +29,8 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBoxInstruments = new System.Windows.Forms.GroupBox(); 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.ButtonDown = new System.Windows.Forms.Button();
this.ButtonRight = new System.Windows.Forms.Button(); this.ButtonRight = new System.Windows.Forms.Button();
this.ButtonLeft = new System.Windows.Forms.Button(); this.ButtonLeft = new System.Windows.Forms.Button();
@ -59,6 +61,8 @@
// //
// groupBoxInstruments // groupBoxInstruments
// //
this.groupBoxInstruments.Controls.Add(this.buttonSortByColor);
this.groupBoxInstruments.Controls.Add(this.buttonSortByType);
this.groupBoxInstruments.Controls.Add(this.ButtonDown); this.groupBoxInstruments.Controls.Add(this.ButtonDown);
this.groupBoxInstruments.Controls.Add(this.ButtonRight); this.groupBoxInstruments.Controls.Add(this.ButtonRight);
this.groupBoxInstruments.Controls.Add(this.ButtonLeft); this.groupBoxInstruments.Controls.Add(this.ButtonLeft);
@ -76,6 +80,26 @@
this.groupBoxInstruments.TabStop = false; this.groupBoxInstruments.TabStop = false;
this.groupBoxInstruments.Text = "Инструменты"; 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 // ButtonDown
// //
this.ButtonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.ButtonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
@ -270,14 +294,14 @@
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
this.SaveToolStripMenuItem.Name = "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.Text = "Сохранить";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
this.LoadToolStripMenuItem.Name = "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.Text = "Загрузить";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
// //
@ -339,5 +363,7 @@
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
} }
} }

View File

@ -28,10 +28,6 @@ namespace Boats
private readonly MapsCollection _mapsCollection; private readonly MapsCollection _mapsCollection;
private readonly ILogger _logger; private readonly ILogger _logger;
/// <summary> /// <summary>
/// Объект от класса карты с набором объектов
/// </summary>
private MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
/// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormMapWithSetBoats(ILogger<FormMapWithSetBoats> logger) public FormMapWithSetBoats(ILogger<FormMapWithSetBoats> logger)
@ -72,28 +68,8 @@ namespace Boats
/// <param name="e"></param> /// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{ {
AbstractMap map = null; pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty]?.ShowSet();
switch (ComboBoxSelectorMap.Text) _logger.LogInformation($"Переход на карту: {listBoxMaps.SelectedItem?.ToString() ?? string.Empty}");
{
case "Простая карта":
map = new SimpleMap();
break;
case "Линии карта":
map = new LineMap();
break;
case "Океан карта":
map = new OceanMap();
break;
}
if (map != null)
{
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBoatsCollectionGeneric = null;
}
} }
/// <summary> /// <summary>
/// Добавление объекта /// Добавление объекта
@ -291,6 +267,12 @@ namespace Boats
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
catch (ArgumentException ex)
{
_logger.LogWarning($"Ошибка добавления объекта: {ex.Message}");
MessageBox.Show($"Ошибка добавления объекта: {ex.Message}",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
/// <summary> /// <summary>
/// Обработка нажатия "Сохранить" /// Обработка нажатия "Сохранить"
@ -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();
}
} }
} }

View File

@ -264,5 +264,9 @@ namespace Boats
_setBoats.Insert(DrawingObjectBoat.Create(rec) as T); _setBoats.Insert(DrawingObjectBoat.Create(rec) as T);
} }
} }
public void Sort(IComparer<T> comparer)
{
_setBoats.SortSet(comparer);
}
} }
} }

View File

@ -119,5 +119,13 @@ namespace Boats
} }
} }
} }
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
} }
} }