This commit is contained in:
Anna 2023-12-29 21:27:30 +04:00
parent ffa3f822d9
commit 968b6e9588
10 changed files with 292 additions and 56 deletions

View File

@ -31,6 +31,8 @@ namespace Airbus_Base
private void InitializeComponent()
{
this.toolsPanel = new System.Windows.Forms.Panel();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.panelSets = new System.Windows.Forms.Panel();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.listBoxObjects = new System.Windows.Forms.ListBox();
@ -43,10 +45,10 @@ namespace Airbus_Base
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox();
this.LabelTools = new System.Windows.Forms.Label();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.menuStrip = new MenuStrip();
this.FileToolStripMenuItem = new ToolStripMenuItem();
this.SaveToolStripMenuItem = new ToolStripMenuItem();
this.LoadToolStripMenuItem = new ToolStripMenuItem();
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.FileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.toolsPanel.SuspendLayout();
@ -57,6 +59,8 @@ namespace Airbus_Base
//
// toolsPanel
//
this.toolsPanel.Controls.Add(this.ButtonSortByColor);
this.toolsPanel.Controls.Add(this.ButtonSortByType);
this.toolsPanel.Controls.Add(this.panelSets);
this.toolsPanel.Controls.Add(this.ButtonAddAirplane);
this.toolsPanel.Controls.Add(this.ButtonDeleteAirplane);
@ -68,6 +72,26 @@ namespace Airbus_Base
this.toolsPanel.Size = new System.Drawing.Size(220, 451);
this.toolsPanel.TabIndex = 0;
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(18, 272);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(187, 32);
this.ButtonSortByColor.TabIndex = 8;
this.ButtonSortByColor.Text = "Сортировать по цвету";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(18, 234);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(187, 32);
this.ButtonSortByType.TabIndex = 7;
this.ButtonSortByType.Text = "Сортировать по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// panelSets
//
this.panelSets.Controls.Add(this.textBoxStorageName);
@ -128,9 +152,9 @@ namespace Airbus_Base
//
// ButtonAddAirplane
//
this.ButtonAddAirplane.Location = new System.Drawing.Point(61, 234);
this.ButtonAddAirplane.Location = new System.Drawing.Point(41, 310);
this.ButtonAddAirplane.Name = "ButtonAddAirplane";
this.ButtonAddAirplane.Size = new System.Drawing.Size(103, 49);
this.ButtonAddAirplane.Size = new System.Drawing.Size(146, 31);
this.ButtonAddAirplane.TabIndex = 1;
this.ButtonAddAirplane.Text = "Добавить объект";
this.ButtonAddAirplane.UseVisualStyleBackColor = true;
@ -138,9 +162,9 @@ namespace Airbus_Base
//
// ButtonDeleteAirplane
//
this.ButtonDeleteAirplane.Location = new System.Drawing.Point(61, 301);
this.ButtonDeleteAirplane.Location = new System.Drawing.Point(41, 345);
this.ButtonDeleteAirplane.Name = "ButtonDeleteAirplane";
this.ButtonDeleteAirplane.Size = new System.Drawing.Size(103, 55);
this.ButtonDeleteAirplane.Size = new System.Drawing.Size(146, 33);
this.ButtonDeleteAirplane.TabIndex = 2;
this.ButtonDeleteAirplane.Text = "Удалить объект";
this.ButtonDeleteAirplane.UseVisualStyleBackColor = true;
@ -148,9 +172,9 @@ namespace Airbus_Base
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Location = new System.Drawing.Point(61, 395);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(24, 417);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(103, 56);
this.ButtonRefreshCollection.Size = new System.Drawing.Size(184, 31);
this.ButtonRefreshCollection.TabIndex = 4;
this.ButtonRefreshCollection.Text = "Обновить коллекцию";
this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -158,7 +182,7 @@ namespace Airbus_Base
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(48, 362);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(51, 384);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(125, 27);
this.maskedTextBoxNumber.TabIndex = 2;
@ -180,45 +204,46 @@ namespace Airbus_Base
this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false;
//
// menuStrip1
// menuStrip
//
menuStrip.ImageScalingSize = new Size(20, 20);
menuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem });
menuStrip.Location = new Point(0, 0);
menuStrip.Name = "menuStrip";
menuStrip.Size = new Size(882, 28);
menuStrip.TabIndex = 2;
menuStrip.Text = "menuStrip";
this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.FileToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(900, 28);
this.menuStrip.TabIndex = 2;
this.menuStrip.Text = "menuStrip";
//
// FileToolStripMenuItem
//
FileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem });
FileToolStripMenuItem.Name = "FileToolStripMenuItem";
FileToolStripMenuItem.Size = new Size(59, 24);
FileToolStripMenuItem.Text = "Файл";
this.FileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.SaveToolStripMenuItem,
this.LoadToolStripMenuItem});
this.FileToolStripMenuItem.Name = "FileToolStripMenuItem";
this.FileToolStripMenuItem.Size = new System.Drawing.Size(59, 24);
this.FileToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(224, 26);
SaveToolStripMenuItem.Text = "Сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
this.SaveToolStripMenuItem.Text = "Сохранение";
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(224, 26);
LoadToolStripMenuItem.Text = "Загрузка";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
this.LoadToolStripMenuItem.Text = "Загрузка";
//
// openFileDialog
//
openFileDialog.FileName = "openFileDialog";
openFileDialog.Filter = "txt file | *.txt";
this.openFileDialog.FileName = "openFileDialog";
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
saveFileDialog.Filter = "txt file | *.txt";
this.saveFileDialog.Filter = "txt file | *.txt";
//
// FormAirplaneCollection
//
@ -227,8 +252,8 @@ namespace Airbus_Base
this.ClientSize = new System.Drawing.Size(900, 450);
this.Controls.Add(this.pictureBoxCollection);
this.Controls.Add(this.toolsPanel);
this.Controls.Add(menuStrip);
this.MainMenuStrip = menuStrip;
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Name = "FormAirplaneCollection";
this.Text = "FormAirplaneCollection";
this.toolsPanel.ResumeLayout(false);
@ -239,7 +264,7 @@ namespace Airbus_Base
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
PerformLayout();
this.PerformLayout();
}
@ -269,5 +294,7 @@ namespace Airbus_Base
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
}
}

View File

@ -48,7 +48,7 @@ namespace Airbus_Base
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxObjects.Items.Add(_storage.Keys[i]);
listBoxObjects.Items.Add(_storage.Keys[i].Name);
}
if (listBoxObjects.Items.Count > 0 && (index == -1 || index >= listBoxObjects.Items.Count))
@ -138,10 +138,10 @@ namespace Airbus_Base
pictureBoxCollection.Image = obj.ShowTheAirplanes();
Log.Information($"Добавлен объект в коллекцию {listBoxObjects.SelectedItem.ToString() ?? string.Empty}");
}
catch (StorageOverflowException ex)
catch (ArgumentException ex)
{
Log.Warning($"Коллекция {listBoxObjects.SelectedItem.ToString() ?? string.Empty} переполнена");
MessageBox.Show(ex.Message);
Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxObjects.SelectedItem.ToString() ?? string.Empty}");
MessageBox.Show("Добавляемый объект уже сущесвует в коллекции");
}
});
form.AddEvent(airplaneDelegate);
@ -260,5 +260,21 @@ namespace Airbus_Base
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByColor());
private void CompareAirplanes(IComparer<DrawningAirplane?> comparer)
{
if (listBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxObjects.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
obj.Sort(comparer);
pictureBoxCollection.Image = obj.ShowTheAirplanes();
}
}
}

View File

@ -57,4 +57,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>144, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>303, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,38 @@
using Airbus_Base.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus_Base.Generics
{
internal class AirplaneCompareByColor : IComparer<DrawningAirplane?>
{
public int Compare(DrawningAirplane? x, DrawningAirplane? y)
{
if (x == null || x.EntityAirplane == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAirplane == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.EntityAirplane.BodyColor.Name != y.EntityAirplane.BodyColor.Name)
{
return x.EntityAirplane.BodyColor.Name.CompareTo(y.EntityAirplane.BodyColor.Name);
}
var speedCompare = x.EntityAirplane.Speed.CompareTo(y.EntityAirplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityAirplane.Weight.CompareTo(y.EntityAirplane.Weight);
}
}
}

View File

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

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus_Base.Generics
{
internal class AirplanesCollectionInfo : IEquatable<AirplanesCollectionInfo>
{
public string Name { get; private set; }
public string Description { get; private set; }
public AirplanesCollectionInfo(string name, string description)
{
Name = name;
Description = description;
}
public bool Equals(AirplanesCollectionInfo? other)
{
if (Name == other?.Name)
return true;
return false;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
}

View File

@ -0,0 +1,65 @@
using Airbus_Base.DrawningObjects;
using Airbus_Base.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus_Base.Generics
{
internal class DrawiningAirplaneEqutables : IEqualityComparer<DrawningAirplane?>
{
public bool Equals(DrawningAirplane? x, DrawningAirplane? y)
{
if (x == null || x.EntityAirplane == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityAirplane == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityAirplane.Speed != y.EntityAirplane.Speed)
{
return false;
}
if (x.EntityAirplane.Weight != y.EntityAirplane.Weight)
{
return false;
}
if (x.EntityAirplane.BodyColor != y.EntityAirplane.BodyColor)
{
return false;
}
if (x is DrawningAirbus && y is DrawningAirbus)
{
EntityAirbus EntityX = (EntityAirbus)x.EntityAirplane;
EntityAirbus EntityY = (EntityAirbus)y.EntityAirplane;
if (EntityX.AdditionalPassengerCompartment != EntityY.AdditionalPassengerCompartment)
{
return false;
}
if (EntityX.AdditionalEngine != EntityY.AdditionalEngine)
{
return false;
}
if (EntityX.AdditionalColor != EntityY.AdditionalColor)
{
return false;
}
}
return true;
}
public int GetHashCode([DisallowNull] DrawningAirplane? obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -39,18 +39,23 @@ namespace Airbus_Base.Generics
_places = new List<T?>(count);
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="airplane">Добавляемый самолёт</param>
/// <returns></returns>
public void Insert(T airplane)
public void Insert(T airplane, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
Insert(airplane, 0);
Insert(airplane, 0, equal);
}
/// <summary>
@ -59,7 +64,7 @@ namespace Airbus_Base.Generics
/// <param name="airplane">Добавляемый самолёт</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public void Insert(T airplane, int position)
public void Insert(T airplane, int position, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
{
@ -69,6 +74,13 @@ namespace Airbus_Base.Generics
{
throw new Exception("Неверная позиция для вставки");
}
if (equal != null)
{
if (_places.Contains(airplane, equal))
{
throw new ArgumentException(nameof(airplane));
}
}
_places.Insert(position, airplane);
}

View File

@ -16,12 +16,12 @@ namespace Airbus_Base.Generics
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
readonly Dictionary<AirplanesCollectionInfo, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _airplaneStorages.Keys.ToList();
public List<AirplanesCollectionInfo> Keys => _airplaneStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
@ -53,7 +53,7 @@ namespace Airbus_Base.Generics
/// <param name="pictureHeight"></param>
public TheAirplaneGenericStorage(int pictureWidth, int pictureHeight)
{
_airplaneStorages = new Dictionary<string, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_airplaneStorages = new Dictionary<AirplanesCollectionInfo, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -69,14 +69,14 @@ namespace Airbus_Base.Generics
File.Delete(filename);
}
StringBuilder data = new();
foreach (KeyValuePair<string, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplaneStorages)
foreach (KeyValuePair<AirplanesCollectionInfo, TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record in _airplaneStorages)
{
StringBuilder records = new();
foreach (DrawningAirplane? elem in record.Value.GetTheAirplanes)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
@ -140,7 +140,7 @@ namespace Airbus_Base.Generics
}
}
}
_airplaneStorages.Add(record[0], collection);
_airplaneStorages.Add(new AirplanesCollectionInfo(record[0], string.Empty), collection);
str = sr.ReadLine();
} while (str != null);
@ -153,7 +153,7 @@ namespace Airbus_Base.Generics
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
_airplaneStorages.Add(name, new TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
_airplaneStorages.Add(new AirplanesCollectionInfo(name, string.Empty), new TheAirplanesGenericCollection<DrawningAirplane, DrawningObjectAirplane>(_pictureWidth, _pictureHeight));
}
/// <summary>
@ -162,12 +162,12 @@ namespace Airbus_Base.Generics
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_airplaneStorages.ContainsKey(name))
if (!_airplaneStorages.ContainsKey(new AirplanesCollectionInfo(name, string.Empty)))
{
return;
}
_airplaneStorages.Remove(name);
_airplaneStorages.Remove(new AirplanesCollectionInfo(name, string.Empty));
}
/// <summary>
@ -179,9 +179,10 @@ namespace Airbus_Base.Generics
{
get
{
if (_airplaneStorages.ContainsKey(ind))
AirplanesCollectionInfo indObj = new AirplanesCollectionInfo(ind, string.Empty);
if (_airplaneStorages.ContainsKey(indObj))
{
return _airplaneStorages[ind];
return _airplaneStorages[indObj];
}
return null;

View File

@ -22,6 +22,11 @@ namespace Airbus_Base.Generics
/// </summary>
public IEnumerable<T?> GetTheAirplanes => _collection.GetTheAirplanes();
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) => _collection.SortSet(comparer);
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth;
@ -72,7 +77,7 @@ namespace Airbus_Base.Generics
{
return false;
}
collect?._collection.Insert(obj);
collect?._collection.Insert(obj, new DrawiningAirplaneEqutables());
return true;
}