2ой шаг (сортировки по типу и цвету)

This commit is contained in:
dex_moth 2023-12-10 14:13:57 +04:00
parent d95866ee89
commit d1b29031d2
7 changed files with 217 additions and 44 deletions

View File

@ -46,6 +46,8 @@
LoadToolStripMenuItem = new ToolStripMenuItem();
openFileDialog = new OpenFileDialog();
saveFileDialog = new SaveFileDialog();
buttonSortByType = new Button();
buttonSortByColor = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
menuStrip.SuspendLayout();
SuspendLayout();
@ -53,10 +55,10 @@
// buttonAddAirbus
//
buttonAddAirbus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonAddAirbus.Location = new Point(654, 443);
buttonAddAirbus.Location = new Point(638, 458);
buttonAddAirbus.Margin = new Padding(3, 4, 3, 4);
buttonAddAirbus.Name = "buttonAddAirbus";
buttonAddAirbus.Size = new Size(157, 37);
buttonAddAirbus.Size = new Size(173, 37);
buttonAddAirbus.TabIndex = 0;
buttonAddAirbus.Text = "Добавить самолёт";
buttonAddAirbus.UseVisualStyleBackColor = true;
@ -67,7 +69,7 @@
pictureBoxCollection.Location = new Point(0, 28);
pictureBoxCollection.Margin = new Padding(3, 4, 3, 4);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(630, 556);
pictureBoxCollection.Size = new Size(620, 545);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
@ -76,7 +78,7 @@
labelInstruments.Anchor = AnchorStyles.Top | AnchorStyles.Right;
labelInstruments.AutoSize = true;
labelInstruments.Font = new Font("Segoe UI", 12F, FontStyle.Regular, GraphicsUnit.Point);
labelInstruments.Location = new Point(654, 23);
labelInstruments.Location = new Point(638, 28);
labelInstruments.Name = "labelInstruments";
labelInstruments.Size = new Size(136, 28);
labelInstruments.TabIndex = 2;
@ -85,10 +87,10 @@
// buttonUpdate
//
buttonUpdate.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonUpdate.Location = new Point(654, 173);
buttonUpdate.Location = new Point(638, 160);
buttonUpdate.Margin = new Padding(3, 4, 3, 4);
buttonUpdate.Name = "buttonUpdate";
buttonUpdate.Size = new Size(150, 33);
buttonUpdate.Size = new Size(166, 33);
buttonUpdate.TabIndex = 3;
buttonUpdate.Text = "Обновить набор";
buttonUpdate.UseVisualStyleBackColor = true;
@ -97,10 +99,10 @@
// buttonDeleteAirbus
//
buttonDeleteAirbus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonDeleteAirbus.Location = new Point(654, 523);
buttonDeleteAirbus.Location = new Point(638, 537);
buttonDeleteAirbus.Margin = new Padding(3, 4, 3, 4);
buttonDeleteAirbus.Name = "buttonDeleteAirbus";
buttonDeleteAirbus.Size = new Size(157, 37);
buttonDeleteAirbus.Size = new Size(173, 37);
buttonDeleteAirbus.TabIndex = 4;
buttonDeleteAirbus.Text = "Удалить самолёт";
buttonDeleteAirbus.UseVisualStyleBackColor = true;
@ -109,11 +111,11 @@
// maskedTextBoxNumber
//
maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right;
maskedTextBoxNumber.Location = new Point(654, 487);
maskedTextBoxNumber.Location = new Point(638, 502);
maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4);
maskedTextBoxNumber.Mask = "00";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(157, 27);
maskedTextBoxNumber.Size = new Size(173, 27);
maskedTextBoxNumber.TabIndex = 5;
maskedTextBoxNumber.ValidatingType = typeof(int);
//
@ -121,7 +123,7 @@
//
label1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
label1.AutoSize = true;
label1.Location = new Point(654, 62);
label1.Location = new Point(638, 65);
label1.Name = "label1";
label1.Size = new Size(66, 20);
label1.TabIndex = 7;
@ -131,17 +133,17 @@
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(654, 213);
listBoxStorages.Location = new Point(638, 196);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(150, 144);
listBoxStorages.Size = new Size(166, 124);
listBoxStorages.TabIndex = 8;
listBoxStorages.SelectedIndexChanged += listBoxObjects_SelectedIndexChanged;
//
// buttonAddStorage
//
buttonAddStorage.Location = new Point(654, 136);
buttonAddStorage.Location = new Point(638, 124);
buttonAddStorage.Name = "buttonAddStorage";
buttonAddStorage.Size = new Size(150, 33);
buttonAddStorage.Size = new Size(166, 33);
buttonAddStorage.TabIndex = 9;
buttonAddStorage.Text = "Добавить набор";
buttonAddStorage.UseVisualStyleBackColor = true;
@ -149,9 +151,9 @@
//
// buttonDeleteStorage
//
buttonDeleteStorage.Location = new Point(654, 364);
buttonDeleteStorage.Location = new Point(638, 324);
buttonDeleteStorage.Name = "buttonDeleteStorage";
buttonDeleteStorage.Size = new Size(150, 33);
buttonDeleteStorage.Size = new Size(166, 33);
buttonDeleteStorage.TabIndex = 10;
buttonDeleteStorage.Text = "Удалить набор";
buttonDeleteStorage.UseVisualStyleBackColor = true;
@ -159,9 +161,9 @@
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(654, 103);
textBoxStorageName.Location = new Point(638, 93);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(150, 27);
textBoxStorageName.Size = new Size(166, 27);
textBoxStorageName.TabIndex = 11;
//
// menuStrip
@ -204,11 +206,33 @@
saveFileDialog.Filter = "txt file | *.txt";
saveFileDialog.OverwritePrompt = false;
//
// buttonSortByType
//
buttonSortByType.Location = new Point(638, 372);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(166, 33);
buttonSortByType.TabIndex = 13;
buttonSortByType.Text = "Сортировка по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += buttonSortByType_Click;
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(638, 408);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(166, 31);
buttonSortByColor.TabIndex = 14;
buttonSortByColor.Text = "Сортировка по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += buttonSortByColor_Click;
//
// FormAirbusCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(827, 591);
ClientSize = new Size(827, 585);
Controls.Add(buttonSortByColor);
Controls.Add(buttonSortByType);
Controls.Add(textBoxStorageName);
Controls.Add(buttonDeleteStorage);
Controls.Add(buttonAddStorage);
@ -252,5 +276,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -29,9 +29,30 @@ namespace ProjectAirbus
_logger = logger;
}
private void buttonSortByType_Click(object sender, EventArgs e) => CompareAirbus(new AirbusCompareByType());
private void buttonSortByColor_Click(object sender, EventArgs e) => CompareAirbus(new AirbusCompareByColor());
// Сортировка по сравнителю
private void CompareAirbus(IComparer<DrawningAirbus?> comparer)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowAirbus();
}
// Обработка нажатия "Сохранение"
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirbus.Drawnings;
using ProjectAirbus.Entities;
namespace ProjectAirbus.Generics
{
internal class AirbusCompareByColor : IComparer<DrawningAirbus>
{
public int Compare(DrawningAirbus? x, DrawningAirbus? y)
{
if (x == null || x.EntityAirbus == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityAirbus == null)
throw new ArgumentNullException(nameof(y));
var xPlane = x.EntityAirbus;
var yPlane = y.EntityAirbus;
if (xPlane.BodyColor != yPlane.BodyColor)
return xPlane.BodyColor.Name.CompareTo(yPlane.BodyColor.Name);
var speedCompare = x.EntityAirbus.Speed.CompareTo(y.EntityAirbus.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityAirbus.Weight.CompareTo(y.EntityAirbus.Weight);
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirbus.Drawnings;
namespace ProjectAirbus.Generics
{
internal class AirbusCompareByType : IComparer<DrawningAirbus>
{
public int Compare(DrawningAirbus? x, DrawningAirbus? y)
{
if (x == null || x.EntityAirbus == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityAirbus == null)
throw new ArgumentNullException(nameof(y));
if (x.GetType().Name != y.GetType().Name)
return x.GetType().Name.CompareTo(y.GetType().Name);
var speedCompare = x.EntityAirbus.Speed.CompareTo(y.EntityAirbus.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityAirbus.Weight.CompareTo(y.EntityAirbus.Weight);
}
}
}

View File

@ -16,8 +16,8 @@ namespace ProjectAirbus.Generics
private readonly int _pictureWidth;
private readonly int _pictureHeight;
// Размер занимаемого места 89х34
private readonly int _placeSizeWidth = 189;
private readonly int _placeSizeHeight = 134;
private readonly int _placeSizeWidth = 89;
private readonly int _placeSizeHeight = 34;
// коллекция
private readonly SetGeneric<T> _collection;
@ -31,6 +31,9 @@ namespace ProjectAirbus.Generics
_collection = new SetGeneric<T>(width * height);
}
// Сортировка
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
public static int operator +(AirbusGenericCollection<T, U> collect, T? obj)
{
if (obj != null)

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics.CodeAnalysis;
using ProjectAirbus.Drawnings;
using ProjectAirbus.Entities;
namespace ProjectAirbus.Generics
{
internal class DrawningAirbusEqutables : IEqualityComparer<DrawningAirbus?>
{
public bool Equals(DrawningAirbus? x, DrawningAirbus? y)
{
if (x == null && x.EntityAirbus == null)
throw new ArgumentNullException(nameof(x));
if (y == null && y.EntityAirbus == null)
throw new ArgumentNullException(nameof(y));
if ((x.GetType().Name != y.GetType().Name))
return false;
if (x.EntityAirbus.Speed != y.EntityAirbus.Speed)
return false;
if (x.EntityAirbus.Weight != y.EntityAirbus.Weight)
return false;
if (x.EntityAirbus.BodyColor != y.EntityAirbus.BodyColor)
return false;
if (x is DrawningPlane && y is DrawningPlane)
{
var xPlane = (EntityPlane)x.EntityAirbus;
var yPlane = (EntityPlane)y.EntityAirbus;
if (xPlane.AdditionalColor != yPlane.AdditionalColor)
return false;
if (xPlane.IsAdditionalEngine != yPlane.IsAdditionalEngine)
return false;
if (xPlane.IsCompartment != yPlane.IsCompartment)
return false;
}
return true;
}
public int GetHashCode([DisallowNull] DrawningAirbus obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -23,14 +23,17 @@ namespace ProjectAirbus.Generics
_places = new List<T?>(count);
}
// Добавление объекта в начало набора
public int Insert(T airbus)
// сортировка набора
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
// Добавление объекта в начало набора
public int Insert(T airbus, IEqualityComparer<T?>? equal = null)
{
return Insert(airbus, 0);
}
// Добавление объекта в набор на конкретную позицию
public int Insert(T airbus, int position)
public int Insert(T airbus, int position, IEqualityComparer<T?>? equal = null)
{
if (Count >= _maxCount)
{
@ -55,27 +58,27 @@ namespace ProjectAirbus.Generics
return true;
}
// Получение объекта из набора по позиции
public T? this[int position]
// Получение объекта из набора по позиции
public T? this[int position]
{
get
{
get
if (position < 0 || position >= Count)
{
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
set
{
if (position < 0 || Count >= _maxCount)
{
return;
}
_places.Insert(position, value);
return null;
}
return _places[position];
}
set
{
if (position < 0 || Count >= _maxCount)
{
return;
}
_places.Insert(position, value);
}
}
// Проход по списку
public IEnumerable<T?> GetAirbus(int? maxAirbus = null)
@ -89,6 +92,5 @@ namespace ProjectAirbus.Generics
}
}
}
}
}