From f26076703dedcdb605467751851e86bc24f666c9 Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 9 Jun 2024 18:11:19 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 8 +- .../CollectionInfo.cs | 83 +++++++++++++++++++ .../ICollectionGenericObjects.cs | 11 ++- .../ListGenericObjects.cs | 23 +++-- .../MassiveGenericObjects.cs | 23 ++++- .../StorageCollection.cs | 58 ++++++------- .../DrawningAirplaneCompareByColor.cs | 41 +++++++++ .../DrawningAirplaneCompareByType.cs | 46 ++++++++++ .../Drawnings/DrawningAirplaneEqutables.cs | 68 +++++++++++++++ .../Exceptions/ObjectExistException.cs | 23 +++++ .../FormAirplaneCollection.Designer.cs | 44 ++++++++-- .../FormAirplaneCollection.cs | 30 ++++++- 12 files changed, 410 insertions(+), 48 deletions(-) create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/CollectionInfo.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneCompareByColor.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneCompareByType.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneEqutables.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/ObjectExistException.cs diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs index 7de004a..6920e45 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/AbstractCompany.cs @@ -60,7 +60,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// public static int operator +(AbstractCompany company, DrawningAirplane airplane) { - return company._collection.Insert(airplane); + return company._collection.Insert(airplane, new DrawningAirplaneEqutables()); } /// @@ -128,5 +128,11 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// Расстановка объектов /// protected abstract void SetObjectsPosition(); + + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/CollectionInfo.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..bcf40a9 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.CollectionGenericObjects +{ + /// + /// Класс, хранящиий информацию по коллекции + /// + public class CollectionInfo : IEquatable + { + /// + /// Название + /// + public string Name { get; private set; } + + /// + /// Тип + /// + public CollectionType CollectionType { get; private set; } + + /// + /// Описание + /// + public string Description { get; private set; } + + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separator = "-"; + + /// + /// Конструктор + /// + /// Название + /// Тип + /// Описание + public CollectionInfo(string name, CollectionType collectionType, string description) + { + Name = name; + CollectionType = collectionType; + Description = description; + } + + /// + /// Создание объекта из строки + /// + /// Строка + /// Объект или null + public static CollectionInfo? GetCollectionInfo(string data) + { + string[] strs = data.Split(_separator, StringSplitOptions.RemoveEmptyEntries); + if (strs.Length < 1 || strs.Length > 3) + { + return null; + } + + return new CollectionInfo(strs[0], (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? strs[2] : string.Empty); + } + + public override string ToString() + { + return Name + _separator + CollectionType + _separator + Description; + } + + public bool Equals(CollectionInfo? other) + { + return Name == other?.Name; + } + + public override bool Equals(object? obj) + { + return Equals(obj as CollectionInfo); + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs index be6574b..30f341e 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -23,16 +23,18 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// Добавление объекта в коллекцию /// /// Добавляемый объект + /// Сравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// Добавляемый объект /// Позиция + /// Сравнение двух объектов /// 1 - вставка прошла удачно, -1 - вставка не удалась - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции @@ -59,5 +61,10 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// Поэлементый вывод элементов коллекции IEnumerable GetItems(); + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs index 1e335d1..cae3505 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs @@ -55,20 +55,28 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects return _collection[position]; } - public int Insert(T obj) - { + public int Insert(T obj, IEqualityComparer? comparer = null) + { if (Count == _maxCount) throw new CollectionOverflowException(Count); + if (_collection.Contains(obj, comparer)) + { + throw new ObjectExistException(); + } _collection.Add(obj); return Count; } - public int Insert(T obj, int position) - { + public int Insert(T obj, int position, IEqualityComparer? comparer = null) + { if (Count == _maxCount) throw new CollectionOverflowException(Count); if (position < 0 || position > Count) - throw new PositionOutOfCollectionException(position); ; + throw new PositionOutOfCollectionException(position); + if (_collection.Contains(obj, comparer)) + { + throw new ObjectExistException(position); + } _collection.Insert(position, obj); return position; } @@ -90,5 +98,10 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } } diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs index 859797c..58d53ba 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@  +using System; using ProjectAirplaneWithRadar.Exceptions; namespace ProjectAirplaneWithRadar.CollectionGenericObjects @@ -59,8 +60,13 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + int index = Array.IndexOf(_collection, null); + if (_collection.Contains(obj, comparer)) + { + throw new ObjectExistException(index); + } for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -72,8 +78,12 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectExistException(position); + } if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); @@ -128,5 +138,14 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + if (_collection?.Length > 0) + { + Array.Sort(_collection, comparer); + Array.Reverse(_collection); + } + } } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs index 86acf32..e718a72 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs @@ -1,4 +1,5 @@ -using System.Data; +using System.Collections; +using System.Data; using System.IO; using System.Text; using ProjectAirplaneWithRadar.Drawnings; @@ -16,12 +17,12 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Ключевое слово, с которого должен начинаться файл @@ -43,7 +44,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// @@ -53,18 +54,19 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (name == null || _storages.ContainsKey(name)) - return; + CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty); + if (collectionInfo.Name == null || _storages.ContainsKey(collectionInfo)) + return; - switch (collectionType) + switch (collectionInfo.CollectionType) { case CollectionType.None: return; case CollectionType.Massive: - _storages[name] = new MassiveGenericObjects(); + _storages.Add(collectionInfo, new MassiveGenericObjects { }); return; case CollectionType.List: - _storages[name] = new ListGenericObjects(); + _storages.Add(collectionInfo, new ListGenericObjects { }); return; } } @@ -75,8 +77,10 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects /// Название коллекции public void DelCollection(string name) { - if(_storages.ContainsKey(name)) - _storages.Remove(name); + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (collectionInfo.Name == null || !_storages.ContainsKey(collectionInfo)) + return; + _storages.Remove(collectionInfo); } /// @@ -88,10 +92,11 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects { get { - if (name == null || !_storages.ContainsKey(name)) - return null; + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, ""); + if (collectionInfo == null || !_storages.ContainsKey(collectionInfo)) + return null; - return _storages[name]; + return _storages[collectionInfo]; } } @@ -112,7 +117,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects using (StreamWriter sw = new(filename)) { sw.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { sw.Write(Environment.NewLine); if (value.Value.Count == 0) @@ -122,8 +127,6 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects sw.Write(value.Key); sw.Write(_separatorForKeyValue); - sw.Write(value.Value.GetCollectionType); - sw.Write(_separatorForKeyValue); sw.Write(value.Value.MaxCount); sw.Write(_separatorForKeyValue); @@ -171,21 +174,20 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects while (!sr.EndOfStream) { string[] record = sr.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 4) + if (record.Length != 3) { continue; } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); - if (collection == null) - { - throw new InvalidOperationException("Не удалось создать коллекцию"); - } + CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции:" + record[0]); + + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType) ?? + throw new Exception("Не удалось определить тип коллекции:" + record[1]); + + collection.MaxCount = Convert.ToInt32(record[1]); - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { if (elem?.CreateDrawningAirplane() is T airplane) @@ -201,7 +203,7 @@ namespace ProjectAirplaneWithRadar.CollectionGenericObjects } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneCompareByColor.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneCompareByColor.cs new file mode 100644 index 0000000..750f42e --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneCompareByColor.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.Drawnings +{ + /// + /// Сравнение по цвету, скорости, весу + /// + public class DrawningAirplaneCompareByColor : IComparer + { + public int Compare(DrawningAirplane? x, DrawningAirplane? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null || x.EntityAirplane == null) + { + return -1; + } + + if (y == null || y.EntityAirplane == null) + { + return 1; + } + 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); + } + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneCompareByType.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneCompareByType.cs new file mode 100644 index 0000000..b32ba4d --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneCompareByType.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.Drawnings +{ + /// + /// Сравнение по типу, скорости, весу + /// + public class DrawningAirplaneCompareByType : IComparer + { + public int Compare(DrawningAirplane? x, DrawningAirplane? y) + { + + if (x == null && y == null) + { + return 0; + } + if (x == null || x.EntityAirplane == null) + { + return -1; + } + + if (y == null || y.EntityAirplane == null) + { + return 1; + } + + if (!x.GetType().Name.Equals(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); + } + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneEqutables.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneEqutables.cs new file mode 100644 index 0000000..3822279 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplaneEqutables.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAirplaneWithRadar.Entities; + +namespace ProjectAirplaneWithRadar.Drawnings +{ + /// + /// Реализация сравнения двух объектов класса-прорисовки + /// + public class DrawningAirplaneEqutables : IEqualityComparer + { + public bool Equals(DrawningAirplane? x, DrawningAirplane? y) + { + if (x == null || x.EntityAirplane == null) + { + return false; + } + if (y == null || y.EntityAirplane == null) + { + return false; + } + 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 DrawingAirplaneWithRadar && y is DrawingAirplaneWithRadar) + { + EntityAirplaneWithRadar entityX = (EntityAirplaneWithRadar)x.EntityAirplane; + EntityAirplaneWithRadar entityY = (EntityAirplaneWithRadar)y.EntityAirplane; + + if (entityX.AdditionalColor != entityY.AdditionalColor) + { + return false; + } + if (entityX.Wheels != entityY.Wheels) + { + return false; + } + if (entityX.Radar != entityY.Radar) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningAirplane obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/ObjectExistException.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/ObjectExistException.cs new file mode 100644 index 0000000..ad1b685 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Exceptions/ObjectExistException.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirplaneWithRadar.Exceptions +{ + [Serializable] + internal class ObjectExistException : ApplicationException + { + public ObjectExistException(int count) : base("Вставка уже существующего объекта") { } + + public ObjectExistException() : base() { } + + public ObjectExistException(string message) : base(message) { } + + public ObjectExistException(string message, Exception exception) : base(message, exception) { } + + protected ObjectExistException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs index 0f8f332..ef07c13 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs @@ -30,6 +30,8 @@ { groupBoxTools = new GroupBox(); panelCompanyTools = new Panel(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); buttonAddAirplane = new Button(); maskedTextBoxPosition = new MaskedTextBox(); buttonRefresh = new Button(); @@ -66,15 +68,17 @@ groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(445, 24); + groupBoxTools.Location = new Point(653, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(206, 523); + groupBoxTools.Size = new Size(206, 584); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(buttonAddAirplane); panelCompanyTools.Controls.Add(maskedTextBoxPosition); panelCompanyTools.Controls.Add(buttonRefresh); @@ -82,17 +86,39 @@ panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 333); + panelCompanyTools.Location = new Point(3, 335); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(200, 187); + panelCompanyTools.Size = new Size(200, 246); panelCompanyTools.TabIndex = 8; // + // buttonSortByColor + // + buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByColor.Location = new Point(103, 185); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(97, 56); + buttonSortByColor.TabIndex = 8; + buttonSortByColor.Text = "Сорт. по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByType.Location = new Point(0, 185); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(97, 56); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сорт. по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // buttonAddAirplane // buttonAddAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonAddAirplane.Location = new Point(0, 3); buttonAddAirplane.Name = "buttonAddAirplane"; - buttonAddAirplane.Size = new Size(97, 56); + buttonAddAirplane.Size = new Size(197, 56); buttonAddAirplane.TabIndex = 1; buttonAddAirplane.Text = "Добавить самолет"; buttonAddAirplane.UseVisualStyleBackColor = true; @@ -249,7 +275,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 24); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(445, 523); + pictureBox.Size = new Size(653, 584); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -258,7 +284,7 @@ menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; - menuStrip.Size = new Size(651, 24); + menuStrip.Size = new Size(859, 24); menuStrip.TabIndex = 2; menuStrip.Text = "menuStrip"; // @@ -297,7 +323,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(651, 547); + ClientSize = new Size(859, 608); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); @@ -342,5 +368,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs index f91062e..54f42b4 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs @@ -82,7 +82,12 @@ namespace ProjectAirplaneWithRadar { MessageBox.Show(ex.Message); _logger.LogError("Ошибка: {Message}", ex.Message); - } + } + catch (ObjectExistException ex) + { + MessageBox.Show("Такой объект есть в коллекции"); + _logger.LogWarning($"Добавление существующего объекта: {ex.Message}"); + } } /// @@ -237,7 +242,7 @@ namespace ProjectAirplaneWithRadar listBoxCollection.Items.Clear(); for (int i = 0; i < _storageCollection.Keys?.Count; ++i) { - string? colName = _storageCollection.Keys?[i]; + string? colName = _storageCollection.Keys?[i].Name; if (!string.IsNullOrEmpty(colName)) listBoxCollection.Items.Add(colName); } @@ -322,5 +327,26 @@ namespace ProjectAirplaneWithRadar } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + CompareAirplanes(new DrawningAirplaneCompareByType()); + } + + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + CompareAirplanes(new DrawningAirplaneCompareByColor()); + } + + private void CompareAirplanes(IComparer comparer) + { + if (_company == null) + { + return; + } + + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } } } \ No newline at end of file