Сравнение.

This commit is contained in:
Мельников Игорь 2022-12-06 22:11:56 +04:00
parent 86f03f05c5
commit f866ee1024
7 changed files with 161 additions and 5 deletions

View File

@ -9,6 +9,9 @@
/// Объект от класса отрисовки локомотива /// Объект от класса отрисовки локомотива
/// </summary> /// </summary>
private DrawningLocomotive _locomotive = null; private DrawningLocomotive _locomotive = null;
public DrawningLocomotive GetLocomotive => _locomotive;
public DrawningObjectLocomotive(DrawningLocomotive locomotive) public DrawningObjectLocomotive(DrawningLocomotive locomotive)
{ {
_locomotive = locomotive; _locomotive = locomotive;

View File

@ -29,6 +29,8 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBoxTools = new System.Windows.Forms.GroupBox(); this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.buttonDeleteMap = new System.Windows.Forms.Button(); this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox(); this.listBoxMaps = new System.Windows.Forms.ListBox();
@ -59,6 +61,8 @@
// //
// groupBoxTools // groupBoxTools
// //
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Controls.Add(this.buttonUp);
this.groupBoxTools.Controls.Add(this.buttonDown); this.groupBoxTools.Controls.Add(this.buttonDown);
@ -77,6 +81,26 @@
this.groupBoxTools.TabStop = false; this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты"; this.groupBoxTools.Text = "Инструменты";
// //
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(31, 341);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(164, 23);
this.buttonSortByColor.TabIndex = 3;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(31, 312);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(164, 23);
this.buttonSortByType.TabIndex = 3;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBoxMaps // groupBoxMaps
// //
this.groupBoxMaps.Anchor = System.Windows.Forms.AnchorStyles.Right; this.groupBoxMaps.Anchor = System.Windows.Forms.AnchorStyles.Right;
@ -192,7 +216,7 @@
// //
// buttonShowOnMap // buttonShowOnMap
// //
this.buttonShowOnMap.Location = new System.Drawing.Point(31, 426); this.buttonShowOnMap.Location = new System.Drawing.Point(31, 526);
this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(164, 26); this.buttonShowOnMap.Size = new System.Drawing.Size(164, 26);
this.buttonShowOnMap.TabIndex = 5; this.buttonShowOnMap.TabIndex = 5;
@ -202,7 +226,7 @@
// //
// buttonShowStorage // buttonShowStorage
// //
this.buttonShowStorage.Location = new System.Drawing.Point(31, 394); this.buttonShowStorage.Location = new System.Drawing.Point(31, 494);
this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(164, 26); this.buttonShowStorage.Size = new System.Drawing.Size(164, 26);
this.buttonShowStorage.TabIndex = 4; this.buttonShowStorage.TabIndex = 4;
@ -212,7 +236,7 @@
// //
// buttonRemoveLocomotive // buttonRemoveLocomotive
// //
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(31, 362); this.buttonRemoveLocomotive.Location = new System.Drawing.Point(31, 462);
this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
this.buttonRemoveLocomotive.Size = new System.Drawing.Size(164, 26); this.buttonRemoveLocomotive.Size = new System.Drawing.Size(164, 26);
this.buttonRemoveLocomotive.TabIndex = 3; this.buttonRemoveLocomotive.TabIndex = 3;
@ -222,7 +246,7 @@
// //
// maskedTextBoxPosition // maskedTextBoxPosition
// //
this.maskedTextBoxPosition.Location = new System.Drawing.Point(31, 333); this.maskedTextBoxPosition.Location = new System.Drawing.Point(31, 433);
this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(164, 23); this.maskedTextBoxPosition.Size = new System.Drawing.Size(164, 23);
@ -230,7 +254,7 @@
// //
// buttonAddCar // buttonAddCar
// //
this.buttonAddCar.Location = new System.Drawing.Point(31, 291); this.buttonAddCar.Location = new System.Drawing.Point(31, 391);
this.buttonAddCar.Name = "buttonAddCar"; this.buttonAddCar.Name = "buttonAddCar";
this.buttonAddCar.Size = new System.Drawing.Size(164, 26); this.buttonAddCar.Size = new System.Drawing.Size(164, 26);
this.buttonAddCar.TabIndex = 1; this.buttonAddCar.TabIndex = 1;
@ -336,5 +360,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

@ -301,5 +301,33 @@ namespace Locomotives
} }
} }
} }
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new LocomotiveCompareByType());
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new LocomotiveCompareByColor());
pictureBoxLocomotives.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
} }
} }

View File

@ -0,0 +1,38 @@
namespace Locomotives
{
internal class LocomotiveCompareByColor : IComparer<IDrawningObject>
{
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 xLocomotive = x as DrawningObjectLocomotive;
var yLocomotive = y as DrawningObjectLocomotive;
if (xLocomotive == null && yLocomotive == null)
{
return 0;
}
if (xLocomotive == null && yLocomotive != null)
{
return 1;
}
if (xLocomotive != null && yLocomotive == null)
{
return -1;
}
var xColorName = xLocomotive.GetLocomotive.Locomotive.BodyColor.Name;
var yColorName = yLocomotive.GetLocomotive.Locomotive.BodyColor.Name;
return xColorName.CompareTo(yColorName);
}
}
}

View File

@ -0,0 +1,49 @@
namespace Locomotives
{
internal class LocomotiveCompareByType : IComparer<IDrawningObject>
{
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 xLocomotive = x as DrawningObjectLocomotive;
var yLocomotive = y as DrawningObjectLocomotive;
if (xLocomotive == null && yLocomotive == null)
{
return 0;
}
if (xLocomotive == null && yLocomotive != null)
{
return 1;
}
if (xLocomotive != null && yLocomotive == null)
{
return -1;
}
if (xLocomotive?.GetLocomotive.GetType().Name != yLocomotive?.GetLocomotive.GetType().Name)
{
if (xLocomotive.GetLocomotive.GetType().Name == "DrawningLocomotive")
{
return -1;
}
return 1;
}
var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight);
}
}
}

View File

@ -201,5 +201,9 @@
_setLocomotives.Insert(DrawningObjectLocomotive.Create(record) as T); _setLocomotives.Insert(DrawningObjectLocomotive.Create(record) as T);
} }
} }
public void Sort(IComparer<T> comparer)
{
_setLocomotives.SortSet(comparer);
}
} }
} }

View File

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