This commit is contained in:
zum 2024-05-07 16:02:49 +04:00
parent 96cd694efd
commit 21ab932b58
9 changed files with 258 additions and 29 deletions

View File

@ -64,7 +64,7 @@ public abstract class AbstractCompany
/// <returns></returns>
public static int operator +(AbstractCompany company, DrawningWarPlane plane)
{
if (company._collection.Insert(plane))
if (company._collection.Insert(plane, new DrawiningWarPlaneEqutables()))
{
return 1;
}
@ -120,7 +120,11 @@ public abstract class AbstractCompany
return bitmap;
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
public void Sort(IComparer<DrawningWarPlane?> comparer) => _collection?.CollectionSort(comparer);
/// <summary>
/// Вывод заднего фона
/// </summary>

View File

@ -23,7 +23,7 @@ public interface ICollectionGenericObjects<T>
/// </summary>
/// <param name="obj">Добавляемый объект</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
bool Insert(T obj);
bool Insert(T obj, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Добавление объекта в коллекцию на конкретную позицию
@ -31,7 +31,7 @@ public interface ICollectionGenericObjects<T>
/// <param name="obj">Добавляемый объект</param>
/// <param name="position">Позиция</param>
/// <returns>true - вставка прошла удачно, false - вставка не удалась</returns>
bool Insert(T obj, int position);
bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null);
/// <summary>
/// Удаление объекта из коллекции с конкретной позиции
@ -57,4 +57,10 @@ public interface ICollectionGenericObjects<T>
/// </summary>
/// <returns>Поэлементый вывод элементов коллекции</returns>
IEnumerable<T?> GetItems();
/// <summary>
/// Сортировка коллекции
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
void CollectionSort(IComparer<T?> comparer);
}

View File

@ -55,7 +55,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position];
}
public bool Insert(T obj)
public bool Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
if (Count + 1 > MaxCount)
{
@ -66,7 +66,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
return true;
}
public bool Insert(T obj, int position)
public bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
if (_collection.Count + 1 < _maxCount)
{
@ -98,4 +98,9 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
yield return _collection[i];
}
}
public void CollectionSort(IComparer<T?> comparer)
{
_collection.Sort(comparer);
}
}

View File

@ -62,7 +62,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
return _collection[position];
}
public bool Insert(T obj)
public bool Insert(T obj, IEqualityComparer<T?>? comparer = null)
{
int index = Array.IndexOf(_collection, null);
@ -75,7 +75,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
}
public bool Insert(T obj, int position)
public bool Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{
if(position < 0 || position > _collection.Length -1)
@ -120,9 +120,19 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < _collection.Length; ++i)
for (int i = 0; i < _collection.Length; ++i)
{
yield return _collection[i];
}
}
public void CollectionSort(IComparer<T?> comparer)
{
if(_collection?.Length > 0)
{
Array.Sort(_collection, comparer);
//_collection = _collection.OrderBy(warPlane => warPlane == null).ToArray();
}
}
}

View File

@ -0,0 +1,71 @@
using ProjectStormTrooper.Entities;
using System.Diagnostics.CodeAnalysis;
namespace ProjectStormTrooper.Drawnings;
/// <summary>
/// Реализация сравнения двух объектов класса-прорисовки
/// </summary>
public class DrawiningWarPlaneEqutables : IEqualityComparer<DrawningWarPlane?>
{
public bool Equals(DrawningWarPlane? x, DrawningWarPlane? y)
{
if (x == null || x.EntityWarPlane == null)
{
return false;
}
if (y == null || y.EntityWarPlane == null)
{
return false;
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityWarPlane.Speed != y.EntityWarPlane.Speed)
{
return false;
}
if (x.EntityWarPlane.Weight != y.EntityWarPlane.Weight)
{
return false;
}
if (x.EntityWarPlane.BodyColor != y.EntityWarPlane.BodyColor)
{
return false;
}
if(x.EntityWarPlane.ColorOfHead != y.EntityWarPlane.ColorOfHead)
{
return false;
}
if (x is DrawningStormTrooper && y is DrawningStormTrooper)
{
EntityStormTrooper entityX = (EntityStormTrooper)x.EntityWarPlane;
EntityStormTrooper entityY = (EntityStormTrooper)y.EntityWarPlane;
if(entityX.Rockets != entityY.Rockets)
{
return false;
}
if(entityX.Bombs != entityY.Bombs)
{
return false;
}
if(entityX.AdditionalColor != entityY.AdditionalColor)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningWarPlane obj)
{
return obj.GetHashCode();
}
}

View File

@ -0,0 +1,34 @@
namespace ProjectStormTrooper.Drawnings;
/// <summary>
/// Сравнение по цвету, скорости, весу
/// </summary>
public class DrawningWarPlaneCompareByColor : IComparer<DrawningWarPlane?>
{
public int Compare(DrawningWarPlane? x, DrawningWarPlane? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null || x.EntityWarPlane == null)
{
return -1;
}
if (y == null || y.EntityWarPlane == null)
{
return 1;
}
if(x.EntityWarPlane.BodyColor.Name != y.EntityWarPlane.BodyColor.Name)
{
return x.EntityWarPlane.BodyColor.Name.CompareTo(y.EntityWarPlane.BodyColor.Name);
}
var speedCompare = x.EntityWarPlane.Speed.CompareTo(y.EntityWarPlane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityWarPlane.Weight.CompareTo(y.EntityWarPlane.Weight);
}
}

View File

@ -0,0 +1,39 @@

namespace ProjectStormTrooper.Drawnings;
/// <summary>
/// Сравнение по типу, скорости, весу
/// </summary>
public class DrawningWarPlaneCompareByType : IComparer<DrawningWarPlane?>
{
public int Compare(DrawningWarPlane? x, DrawningWarPlane? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null || x.EntityWarPlane == null)
{
return -1;
}
if (y == null || y.EntityWarPlane == null)
{
return 1;
}
if (!x.GetType().Name.Equals(y.GetType().Name))
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare = x.EntityWarPlane.Speed.CompareTo(y.EntityWarPlane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityWarPlane.Weight.CompareTo(y.EntityWarPlane.Weight);
}
}

View File

@ -39,6 +39,8 @@
labelCollectionName = new Label();
buttonCreateCompany = new Button();
panelCompanyTools = new Panel();
buttonSortByColor = new Button();
buttonSortByType = new Button();
buttonGoToCheck = new Button();
buttonRefresh = new Button();
buttonRemoveWarPlane = new Button();
@ -68,7 +70,7 @@
groupBox1.Dock = DockStyle.Right;
groupBox1.Location = new Point(616, 28);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(197, 782);
groupBox1.Size = new Size(197, 737);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
@ -157,7 +159,7 @@
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(8, 441);
buttonCreateCompany.Location = new Point(6, 399);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(172, 29);
buttonCreateCompany.TabIndex = 8;
@ -167,6 +169,8 @@
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonSortByColor);
panelCompanyTools.Controls.Add(buttonSortByType);
panelCompanyTools.Controls.Add(buttonGoToCheck);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(buttonRemoveWarPlane);
@ -174,14 +178,34 @@
panelCompanyTools.Controls.Add(buttonAddWarPlane);
panelCompanyTools.Dock = DockStyle.Bottom;
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(3, 521);
panelCompanyTools.Location = new Point(3, 449);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(191, 258);
panelCompanyTools.Size = new Size(191, 285);
panelCompanyTools.TabIndex = 7;
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(5, 249);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(172, 27);
buttonSortByColor.TabIndex = 8;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += ButtonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Location = new Point(5, 218);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(172, 27);
buttonSortByType.TabIndex = 7;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += ButtonSortByType_Click;
//
// buttonGoToCheck
//
buttonGoToCheck.Location = new Point(5, 185);
buttonGoToCheck.Location = new Point(5, 150);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(172, 27);
buttonGoToCheck.TabIndex = 6;
@ -191,7 +215,7 @@
//
// buttonRefresh
//
buttonRefresh.Location = new Point(5, 218);
buttonRefresh.Location = new Point(5, 183);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(172, 29);
buttonRefresh.TabIndex = 5;
@ -201,7 +225,7 @@
//
// buttonRemoveWarPlane
//
buttonRemoveWarPlane.Location = new Point(5, 128);
buttonRemoveWarPlane.Location = new Point(3, 93);
buttonRemoveWarPlane.Name = "buttonRemoveWarPlane";
buttonRemoveWarPlane.Size = new Size(172, 51);
buttonRemoveWarPlane.TabIndex = 4;
@ -211,7 +235,7 @@
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(5, 95);
maskedTextBoxPosition.Location = new Point(5, 60);
maskedTextBoxPosition.Mask = "00";
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(172, 27);
@ -232,7 +256,7 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Ангар" });
comboBoxSelectorCompany.Location = new Point(8, 396);
comboBoxSelectorCompany.Location = new Point(6, 365);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(172, 28);
comboBoxSelectorCompany.TabIndex = 0;
@ -243,7 +267,7 @@
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 28);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(616, 782);
pictureBox.Size = new Size(616, 737);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
@ -292,7 +316,7 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(813, 810);
ClientSize = new Size(813, 765);
Controls.Add(pictureBox);
Controls.Add(groupBox1);
Controls.Add(menuStrip);
@ -337,5 +361,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -58,7 +58,7 @@ namespace ProjectStormTrooper
_logger.LogInformation($"Добавлен объект {warPlane.GetDataForSave()}");
pictureBox.Image = _company.Show();
}
catch(CollectionOverflowException ex)
catch (CollectionOverflowException ex)
{
MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning($"Не удалось добавить объект: {ex.Message}");
@ -111,12 +111,12 @@ namespace ProjectStormTrooper
}
catch(ObjectNotFoundException ex)
catch (ObjectNotFoundException ex)
{
MessageBox.Show("Объект не найден");
_logger.LogWarning($"Удаление не найденного объекта в позиции {pos} ");
}
catch(PositionOutOfCollectionException ex)
catch (PositionOutOfCollectionException ex)
{
MessageBox.Show("Удаление вне рамках коллекции");
_logger.LogWarning($"Удаление объекта за пределами коллекции {pos} ");
@ -303,7 +303,7 @@ namespace ProjectStormTrooper
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if(openFileDialog.ShowDialog() == DialogResult.OK)
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
@ -312,7 +312,7 @@ namespace ProjectStormTrooper
_logger.LogInformation("Загрузка из файла: {filename}", saveFileDialog.FileName);
RerfreshListBoxItems();
}
catch(Exception ex)
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError("Ошибка: {Message}", ex.Message);
@ -323,5 +323,39 @@ namespace ProjectStormTrooper
}
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e)
{
CompareCars(new DrawningWarPlaneCompareByType());
}
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
CompareCars(new DrawningWarPlaneCompareByColor());
}
/// <summary>
/// Сортировка по сравнителю
/// </summary>
/// <param name="comparer">Сравнитель объектов</param>
private void CompareCars(IComparer<DrawningWarPlane?> comparer)
{
if (_company == null)
{
return;
}
_company.Sort(comparer);
pictureBox.Image = _company.Show();
}
}
}