diff --git a/CourseWork.zip b/CourseWork.zip new file mode 100644 index 0000000..1773705 Binary files /dev/null and b/CourseWork.zip differ diff --git a/ProjectFighterJet/CollectionGenericObjects/AbstractCompany.cs b/ProjectFighterJet/CollectionGenericObjects/AbstractCompany.cs index 9710808..69462c3 100644 --- a/ProjectFighterJet/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectFighterJet/CollectionGenericObjects/AbstractCompany.cs @@ -60,7 +60,7 @@ public abstract class AbstractCompany /// public static int operator +(AbstractCompany company, DrawningJet jet) { - return company._collection.Insert(jet); + return company._collection.Insert(jet, new DrawiningJetEqutables()); } /// @@ -112,4 +112,9 @@ public abstract class AbstractCompany /// Расстановка объектов /// protected abstract void SetObjectsPosition(); + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); } diff --git a/ProjectFighterJet/CollectionGenericObjects/CollectionInfo.cs b/ProjectFighterJet/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..2b860cb --- /dev/null +++ b/ProjectFighterJet/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectFighterJet.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; + } + 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/ProjectFighterJet/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectFighterJet/CollectionGenericObjects/ICollectionGenericObjects.cs index 159589d..703cb4b 100644 --- a/ProjectFighterJet/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectFighterJet/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectFighterJet.CollectionGenericObjects; +namespace ProjectFighterJet.CollectionGenericObjects; public interface ICollectionGenericObjects where T : class @@ -23,16 +17,18 @@ where T : class /// Добавление объекта в коллекцию /// /// Добавляемый объект + /// Cравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// Добавляемый объект /// Позиция + /// Cравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции @@ -56,4 +52,9 @@ where T : class /// /// Поэлементый вывод элементов коллекции IEnumerable GetItems(); + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } diff --git a/ProjectFighterJet/CollectionGenericObjects/ListGenericObjects.cs b/ProjectFighterJet/CollectionGenericObjects/ListGenericObjects.cs index 4e0b97c..bcbf371 100644 --- a/ProjectFighterJet/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectFighterJet/CollectionGenericObjects/ListGenericObjects.cs @@ -56,17 +56,30 @@ where T : class } return null; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - // TODO выброс ошибки если переполнение + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectIsEqualException(); + } + } + if (Count == _maxCount) throw new CollectionOverflowException(Count); _collection.Add(obj); return Count; } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - // TODO выброс ошибки если переполнение - // TODO выброс ошибки если за границу + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectIsEqualException(); + } + } + if (Count == _maxCount) throw new CollectionOverflowException(Count); if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); _collection.Insert(position, obj); @@ -87,4 +100,8 @@ where T : class yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/ProjectFighterJet/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectFighterJet/CollectionGenericObjects/MassiveGenericObjects.cs index 7f5129d..ec2d582 100644 --- a/ProjectFighterJet/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectFighterJet/CollectionGenericObjects/MassiveGenericObjects.cs @@ -53,9 +53,17 @@ where T : class return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - // TODO вставка в свободное место набора + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningJet, item as DrawningJet)) + throw new ObjectIsEqualException(); + } + } + int index = 0; while (index < _collection.Length) { @@ -69,10 +77,17 @@ where T : class throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position) - { - // TODO выброс ошибки если переполнение - // TODO выброс ошибки если выход за границу + public int Insert(T obj, int position, IEqualityComparer? comparer = null) + { + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningJet, item as DrawningJet)) + throw new ObjectIsEqualException(); + } + } + if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) { @@ -104,8 +119,6 @@ where T : class public T? Remove(int position) { - // TODO выброс ошибки если выход за границу - // TODO выброс ошибки если объект пустой if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) throw new ObjectNotFoundException(position); T obj = _collection[position]; @@ -120,4 +133,8 @@ where T : class yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } diff --git a/ProjectFighterJet/CollectionGenericObjects/StorageCollection.cs b/ProjectFighterJet/CollectionGenericObjects/StorageCollection.cs index 8d2b42c..1d217e0 100644 --- a/ProjectFighterJet/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectFighterJet/CollectionGenericObjects/StorageCollection.cs @@ -20,17 +20,17 @@ where T : DrawningJet /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Конструктор /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// /// Добавление коллекции в хранилище @@ -39,17 +39,13 @@ where T : DrawningJet /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (!(collectionType == CollectionType.None) && !_storages.ContainsKey(name)) - { - if (collectionType == CollectionType.List) - { - _storages.Add(name, new ListGenericObjects()); - } - else if (collectionType == CollectionType.Massive) - { - _storages.Add(name, new MassiveGenericObjects()); - } - } + CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty); + if (_storages.ContainsKey(collectionInfo)) return; + if (collectionType == CollectionType.None) return; + else if (collectionType == CollectionType.Massive) + _storages[collectionInfo] = new MassiveGenericObjects(); + else if (collectionType == CollectionType.List) + _storages[collectionInfo] = new ListGenericObjects(); } /// /// Удаление коллекции @@ -57,7 +53,9 @@ where T : DrawningJet /// Название коллекции public void DelCollection(string name) { - if (_storages.ContainsKey(name)) { _storages.Remove(name); } + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(collectionInfo)) + _storages.Remove(collectionInfo); } /// /// Доступ к коллекции @@ -68,10 +66,9 @@ where T : DrawningJet { get { - if (_storages.ContainsKey(name)) - { - return _storages[name]; - } + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(collectionInfo)) + return _storages[collectionInfo]; return null; } } @@ -105,7 +102,7 @@ where T : DrawningJet using (StreamWriter writer = new StreamWriter(filename)) { writer.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { writer.Write(Environment.NewLine); // не сохраняем пустые коллекции @@ -115,8 +112,6 @@ where T : DrawningJet } writer.Write(value.Key); writer.Write(_separatorForKeyValue); - writer.Write(value.Value.GetCollectionType); - writer.Write(_separatorForKeyValue); writer.Write(value.Value.MaxCount); writer.Write(_separatorForKeyValue); foreach (T? item in value.Value.GetItems()) @@ -161,18 +156,16 @@ where T : DrawningJet { string[] record = strs.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) - { + CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции: " + record[0]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType) ?? throw new Exception("Не удалось создать коллекцию"); - } - collection.MaxCount = Convert.ToInt32(record[2]); - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + collection.MaxCount = Convert.ToInt32(record[1]); + string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { if (elem?.CreateDrawningJet() is T jet) @@ -190,7 +183,7 @@ where T : DrawningJet } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } diff --git a/ProjectFighterJet/Drawnings/DrawiningJetEqutables.cs b/ProjectFighterJet/Drawnings/DrawiningJetEqutables.cs new file mode 100644 index 0000000..7ac26be --- /dev/null +++ b/ProjectFighterJet/Drawnings/DrawiningJetEqutables.cs @@ -0,0 +1,66 @@ +using ProjectFighterJet.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectFighterJet.Drawnings; + +public class DrawiningJetEqutables : IEqualityComparer +{ + public bool Equals(DrawningJet? x, DrawningJet? y) + { + if (x == null || x.EntityJet == null) + { + return false; + } + if (y == null || y.EntityJet == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityJet.Speed != y.EntityJet.Speed) + { + return false; + } + if (x.EntityJet.Weight != y.EntityJet.Weight) + { + return false; + } + if (x.EntityJet.BodyColor != y.EntityJet.BodyColor) + { + return false; + } + if (x is DrawningFighterJet && y is DrawningFighterJet) + { + EntityFighterJet _x = (EntityFighterJet)x.EntityJet; + EntityFighterJet _y = (EntityFighterJet)x.EntityJet; + if (_x.AdditionalColor != _y.AdditionalColor) + { + return false; + } + if (_x.Engines != _y.Engines) + { + return false; + } + if (_x.Fuel != _y.Fuel) + { + return false; + } + if (_x.Rockets != _y.Rockets) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningJet obj) + { + return obj.GetHashCode(); + } +} diff --git a/ProjectFighterJet/Drawnings/DrawningJetCompareByColor.cs b/ProjectFighterJet/Drawnings/DrawningJetCompareByColor.cs new file mode 100644 index 0000000..a890ae2 --- /dev/null +++ b/ProjectFighterJet/Drawnings/DrawningJetCompareByColor.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectFighterJet.Drawnings; + +internal class DrawningJetCompareByColor : IComparer +{ + public int Compare(DrawningJet? x, DrawningJet? y) + { + if (x == null || x.EntityJet == null) + { + return 1; + } + + if (y == null || y.EntityJet == null) + { + return -1; + } + var bodycolorCompare = x.EntityJet.BodyColor.Name.CompareTo(y.EntityJet.BodyColor.Name); + if (bodycolorCompare != 0) + { + return bodycolorCompare; + } + var speedCompare = x.EntityJet.Speed.CompareTo(y.EntityJet.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityJet.Weight.CompareTo(y.EntityJet.Weight); + } +} diff --git a/ProjectFighterJet/Drawnings/DrawningJetCompareByType.cs b/ProjectFighterJet/Drawnings/DrawningJetCompareByType.cs new file mode 100644 index 0000000..c005f74 --- /dev/null +++ b/ProjectFighterJet/Drawnings/DrawningJetCompareByType.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectFighterJet.Drawnings; + +internal class DrawningJetCompareByType : IComparer +{ + public int Compare(DrawningJet? x, DrawningJet? y) + { + if (x == null || x.EntityJet == null) + { + return 1; + } + + if (y == null || y.EntityJet == null) + { + return -1; + } + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + + var speedCompare = x.EntityJet.Speed.CompareTo(y.EntityJet.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityJet.Weight.CompareTo(y.EntityJet.Weight); + } +} diff --git a/ProjectFighterJet/Exceptions/ObjectIsEqualException.cs b/ProjectFighterJet/Exceptions/ObjectIsEqualException.cs new file mode 100644 index 0000000..1aae019 --- /dev/null +++ b/ProjectFighterJet/Exceptions/ObjectIsEqualException.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectFighterJet.Exceptions; + +/// +/// Класс, описывающий ошибку переполнения коллекции +/// +[Serializable] +public class ObjectIsEqualException : ApplicationException +{ + public ObjectIsEqualException(int count) : base("В коллекции содержится равный элемент: " + count) { } + public ObjectIsEqualException() : base() { } + public ObjectIsEqualException(string message) : base(message) { } + public ObjectIsEqualException(string message, Exception exception) : base(message, exception) { } + protected ObjectIsEqualException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} \ No newline at end of file diff --git a/ProjectFighterJet/FormJetCollection.Designer.cs b/ProjectFighterJet/FormJetCollection.Designer.cs index be8c1a2..311724d 100644 --- a/ProjectFighterJet/FormJetCollection.Designer.cs +++ b/ProjectFighterJet/FormJetCollection.Designer.cs @@ -30,6 +30,8 @@ { groupBoxTools = new GroupBox(); panelCompanyTools = new Panel(); + buttonSortByType = new Button(); + buttonSortByColor = new Button(); buttonAddJet = new Button(); maskedTextBoxPosition = new MaskedTextBox(); buttonGoToCheck = new Button(); @@ -66,7 +68,7 @@ groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(911, 28); + groupBoxTools.Location = new Point(881, 28); groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Size = new Size(208, 637); groupBoxTools.TabIndex = 0; @@ -75,23 +77,47 @@ // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByType); + panelCompanyTools.Controls.Add(buttonSortByColor); panelCompanyTools.Controls.Add(buttonAddJet); panelCompanyTools.Controls.Add(maskedTextBoxPosition); panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Controls.Add(buttonRemoveJet); panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(9, 378); + panelCompanyTools.Location = new Point(9, 360); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(199, 287); + panelCompanyTools.Size = new Size(199, 305); panelCompanyTools.TabIndex = 8; // + // buttonSortByType + // + buttonSortByType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByType.Location = new Point(3, 193); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(190, 33); + buttonSortByType.TabIndex = 8; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // + // buttonSortByColor + // + buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByColor.Location = new Point(3, 232); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(190, 33); + buttonSortByColor.TabIndex = 7; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // // buttonAddJet // buttonAddJet.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; buttonAddJet.Location = new Point(3, 3); buttonAddJet.Name = "buttonAddJet"; - buttonAddJet.Size = new Size(190, 48); + buttonAddJet.Size = new Size(190, 35); buttonAddJet.TabIndex = 1; buttonAddJet.Text = "Добавление самолёта"; buttonAddJet.UseVisualStyleBackColor = true; @@ -99,17 +125,17 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(3, 111); + maskedTextBoxPosition.Location = new Point(3, 44); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - maskedTextBoxPosition.Size = new Size(202, 27); + maskedTextBoxPosition.Size = new Size(190, 27); maskedTextBoxPosition.TabIndex = 3; maskedTextBoxPosition.ValidatingType = typeof(int); // // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(3, 182); + buttonGoToCheck.Location = new Point(3, 115); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(190, 33); buttonGoToCheck.TabIndex = 6; @@ -120,7 +146,7 @@ // buttonRemoveJet // buttonRemoveJet.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveJet.Location = new Point(3, 144); + buttonRemoveJet.Location = new Point(3, 77); buttonRemoveJet.Name = "buttonRemoveJet"; buttonRemoveJet.Size = new Size(190, 32); buttonRemoveJet.TabIndex = 4; @@ -131,7 +157,7 @@ // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(3, 221); + buttonRefresh.Location = new Point(3, 154); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(190, 33); buttonRefresh.TabIndex = 5; @@ -141,7 +167,7 @@ // // buttonCreateCompany // - buttonCreateCompany.Location = new Point(6, 343); + buttonCreateCompany.Location = new Point(6, 325); buttonCreateCompany.Name = "buttonCreateCompany"; buttonCreateCompany.Size = new Size(196, 29); buttonCreateCompany.TabIndex = 7; @@ -162,12 +188,12 @@ panelStorage.Dock = DockStyle.Top; panelStorage.Location = new Point(3, 23); panelStorage.Name = "panelStorage"; - panelStorage.Size = new Size(202, 280); + panelStorage.Size = new Size(202, 262); panelStorage.TabIndex = 7; // // buttonCollectionDel // - buttonCollectionDel.Location = new Point(3, 248); + buttonCollectionDel.Location = new Point(0, 228); buttonCollectionDel.Name = "buttonCollectionDel"; buttonCollectionDel.Size = new Size(196, 29); buttonCollectionDel.TabIndex = 6; @@ -181,7 +207,7 @@ listBoxCollection.ItemHeight = 20; listBoxCollection.Location = new Point(3, 138); listBoxCollection.Name = "listBoxCollection"; - listBoxCollection.Size = new Size(196, 104); + listBoxCollection.Size = new Size(196, 84); listBoxCollection.TabIndex = 5; // // buttonCollectionAdd @@ -239,7 +265,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 309); + comboBoxSelectorCompany.Location = new Point(6, 291); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(196, 28); comboBoxSelectorCompany.TabIndex = 0; @@ -250,7 +276,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 28); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(911, 637); + pictureBox.Size = new Size(881, 637); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -260,7 +286,7 @@ menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; - menuStrip.Size = new Size(1119, 28); + menuStrip.Size = new Size(1089, 28); menuStrip.TabIndex = 2; menuStrip.Text = "menuStrip1"; // @@ -299,7 +325,7 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1119, 665); + ClientSize = new Size(1089, 665); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); @@ -344,5 +370,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/ProjectFighterJet/FormJetCollection.cs b/ProjectFighterJet/FormJetCollection.cs index c7814c7..152844c 100644 --- a/ProjectFighterJet/FormJetCollection.cs +++ b/ProjectFighterJet/FormJetCollection.cs @@ -74,6 +74,11 @@ public partial class FormJetCollection : Form MessageBox.Show("Не удалось добавить объект"); _logger.LogError("Ошибка: {Message}", ex.Message); } + catch (ObjectIsEqualException ex) + { + MessageBox.Show("Такой объект уже присутствует в коллекции"); + _logger.LogError("Ошибка: {Message}", ex.Message); + } } /// /// Получение цвета @@ -212,27 +217,33 @@ public partial class FormJetCollection : Form private void buttonCollectionDel_Click(object sender, EventArgs e) { - // нужно убедиться, что есть выбранная коллекция - // спросить у пользователя через MessageBox, что он подтверждает, что хочет удалить запись - // удалить и обновить ListBox + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) { MessageBox.Show("Коллекция не выбрана"); return; } - if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + try { - return; + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + RerfreshListBoxItems(); + _logger.LogInformation("Коллекция: " + listBoxCollection.SelectedItem.ToString() + " удалена"); + } + catch (Exception ex) + { + _logger.LogError("Ошибка: {Message}", ex.Message); } - _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); - RerfreshListBoxItems(); } private void RerfreshListBoxItems() { 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); @@ -293,7 +304,7 @@ public partial class FormJetCollection : Form private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - + if (openFileDialog.ShowDialog() == DialogResult.OK) { try @@ -310,4 +321,24 @@ public partial class FormJetCollection : Form } } } + + private void buttonSortByType_Click(object sender, EventArgs e) + { + CompareJet(new DrawningJetCompareByType()); + } + + private void buttonSortByColor_Click(object sender, EventArgs e) + { + CompareJet(new DrawningJetCompareByColor()); + } + private void CompareJet(IComparer comparer) + { + if (_company == null) + { + return; + } + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } + }