From c34ef0c561349920547e3dbe2d1497a9ae40bda5 Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 16 May 2024 21:57:38 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 7 +- .../CollectionInfo.cs | 74 +++++++++++++++++++ .../ICollectionGenericObjects.cs | 14 +++- .../ListGenericObjects.cs | 25 ++++++- .../MassiveGenericObjects.cs | 27 ++++++- .../StorageCollection.cs | 50 ++++++------- .../DrawinigStormtrooperBaseEqutables.cs | 64 ++++++++++++++++ .../DrawningStormtrooperBaseByColor.cs | 35 +++++++++ .../DrawningStormtrooperBaseByType.cs | 34 +++++++++ ...s => ExtentionDrawningStormtrooperBase.cs} | 2 +- .../Exceptions/ObjectIsEqualException.cs | 21 ++++++ .../FormStormtrooperCollection.Designer.cs | 48 +++++++++--- .../FormStormtrooperCollection.cs | 40 ++++++++-- 13 files changed, 387 insertions(+), 54 deletions(-) create mode 100644 ProjectStormtrooper/CollectionGenericObjects/CollectionInfo.cs create mode 100644 ProjectStormtrooper/Drawnings/DrawinigStormtrooperBaseEqutables.cs create mode 100644 ProjectStormtrooper/Drawnings/DrawningStormtrooperBaseByColor.cs create mode 100644 ProjectStormtrooper/Drawnings/DrawningStormtrooperBaseByType.cs rename ProjectStormtrooper/Drawnings/{ExtentionDrawningStormtrooper.cs => ExtentionDrawningStormtrooperBase.cs} (97%) create mode 100644 ProjectStormtrooper/Exceptions/ObjectIsEqualException.cs diff --git a/ProjectStormtrooper/CollectionGenericObjects/AbstractCompany.cs b/ProjectStormtrooper/CollectionGenericObjects/AbstractCompany.cs index 71e19ad..65cfc7a 100644 --- a/ProjectStormtrooper/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectStormtrooper/CollectionGenericObjects/AbstractCompany.cs @@ -56,7 +56,7 @@ public abstract class AbstractCompany /// public static int operator +(AbstractCompany company, DrawningStormtrooperBase stormtrooper) { - return company._collection.Insert(stormtrooper); + return company._collection.Insert(stormtrooper, new DrawinigStormtrooperBaseEqutables()); } /// /// Перегрузка оператора удаления для класса @@ -108,4 +108,9 @@ public abstract class AbstractCompany /// Расстановка объектов /// protected abstract void SetObjectsPosition(); + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); } diff --git a/ProjectStormtrooper/CollectionGenericObjects/CollectionInfo.cs b/ProjectStormtrooper/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..278c209 --- /dev/null +++ b/ProjectStormtrooper/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectStormtrooper.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/ProjectStormtrooper/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectStormtrooper/CollectionGenericObjects/ICollectionGenericObjects.cs index a55e162..29049e0 100644 --- a/ProjectStormtrooper/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectStormtrooper/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,4 +1,5 @@ -using System; +using ProjectStormtrooper.Drawnings; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -24,15 +25,17 @@ public interface ICollectionGenericObjects /// Добавление объекта в коллекцию /// /// Добавление объекта + /// 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); /// /// Удаление объекта из коллекции с конкретной позиции /// @@ -55,5 +58,10 @@ public interface ICollectionGenericObjects /// /// Поэлементый вывод элементов коллекции IEnumerable GetItems(); + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } diff --git a/ProjectStormtrooper/CollectionGenericObjects/ListGenericObjects.cs b/ProjectStormtrooper/CollectionGenericObjects/ListGenericObjects.cs index 73ce612..5718a31 100644 --- a/ProjectStormtrooper/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectStormtrooper/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,5 @@ -using ProjectStormtrooper.Exceptions; +using ProjectStormtrooper.Drawnings; +using ProjectStormtrooper.Exceptions; using System; using System.Collections.Generic; using System.Linq; @@ -53,21 +54,35 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { // TODO проверка, что не превышено максимальное количество элементов // 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 проверка позиции // 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); @@ -91,4 +106,8 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/ProjectStormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectStormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs index 3738366..14b1959 100644 --- a/ProjectStormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectStormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ -using ProjectStormtrooper.Exceptions; +using ProjectStormtrooper.Drawnings; +using ProjectStormtrooper.Exceptions; using System; using System.Collections.Generic; using System.Linq; @@ -58,8 +59,16 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + if (comparer != null) + { + foreach(T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningStormtrooperBase, item as DrawningStormtrooperBase)) + throw new ObjectIsEqualException(); + } + } // TODO вставка в свободное место набора int index = 0; while ( index < _collection.Length) @@ -74,13 +83,21 @@ public class MassiveGenericObjects : ICollectionGenericObjects throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, если нет, то // ищется свободное место после этой позиции и идет вставка туда // если нет после, ищем до // TODO вставка + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningStormtrooperBase, item as DrawningStormtrooperBase)) + throw new ObjectIsEqualException(); + } + } if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) { @@ -128,4 +145,8 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } diff --git a/ProjectStormtrooper/CollectionGenericObjects/StorageCollection.cs b/ProjectStormtrooper/CollectionGenericObjects/StorageCollection.cs index 80d8390..d2ba8c0 100644 --- a/ProjectStormtrooper/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectStormtrooper/CollectionGenericObjects/StorageCollection.cs @@ -17,17 +17,17 @@ public class StorageCollection /// /// Словарь(хранилище) с коллекциями /// - 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>(); } /// /// Добавление коллекции в хранилище @@ -38,17 +38,13 @@ public class StorageCollection { // TODO проверка, что name не пустой и нет в словаре записи с таким ключом // TODO Прописать логику для добавления - 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,9 +53,10 @@ public class StorageCollection public void DelCollection(string name) { // TODO Прописать логику для удаления коллекции - if (_storages.ContainsKey(name)) + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(collectionInfo)) { - _storages.Remove(name); + _storages.Remove(collectionInfo); } } @@ -73,8 +70,9 @@ public class StorageCollection get { // TODO Продумать логику получения объекта - 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; } @@ -110,7 +108,7 @@ public class StorageCollection using (StreamWriter writer = new StreamWriter(filename)) { writer.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { writer.Write(Environment.NewLine); if (value.Value.Count == 0) @@ -164,18 +162,20 @@ public class StorageCollection while ((strs = fs.ReadLine()) != null) { 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); + CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции: " + record[0]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType) ?? + throw new Exception("Не удалось создать коллекцию"); if (collection == null) { 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?.CreateDrawningStormtrooper() is T stormtrooper) @@ -195,7 +195,7 @@ public class StorageCollection } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } diff --git a/ProjectStormtrooper/Drawnings/DrawinigStormtrooperBaseEqutables.cs b/ProjectStormtrooper/Drawnings/DrawinigStormtrooperBaseEqutables.cs new file mode 100644 index 0000000..2284dec --- /dev/null +++ b/ProjectStormtrooper/Drawnings/DrawinigStormtrooperBaseEqutables.cs @@ -0,0 +1,64 @@ +using ProjectStormtrooper.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectStormtrooper.Drawnings; + +public class DrawinigStormtrooperBaseEqutables: IEqualityComparer +{ + public bool Equals(DrawningStormtrooperBase? x, DrawningStormtrooperBase? y) + { + if (x == null || x.EntityStormtrooperBase == null) + { + return false; + } + if(y==null || y.EntityStormtrooperBase == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityStormtrooperBase.Speed!= y.EntityStormtrooperBase.Speed) + { + return false; + } + if (x.EntityStormtrooperBase.Weight != y.EntityStormtrooperBase.Weight) + { + return false; + } + if (x.EntityStormtrooperBase.BodyColor != y.EntityStormtrooperBase.BodyColor) + { + return false; + } + if (x is DrawingStormtrooper && y is DrawingStormtrooper) + { + EntityStormtrooper _x = (EntityStormtrooper)x.EntityStormtrooperBase; + EntityStormtrooper _y = (EntityStormtrooper)x.EntityStormtrooperBase; + if (_x.AdditionalColor != _y.AdditionalColor) + { + return false; + } + if (_x.Bombs != _y.Bombs) + { + return false; + } + if (_x.Rockets != _y.Rockets) + { + return false; + } + } + return true; + + } + + public int GetHashCode([DisallowNull] DrawningStormtrooperBase obj) + { + return obj.GetHashCode(); + } +} diff --git a/ProjectStormtrooper/Drawnings/DrawningStormtrooperBaseByColor.cs b/ProjectStormtrooper/Drawnings/DrawningStormtrooperBaseByColor.cs new file mode 100644 index 0000000..c603886 --- /dev/null +++ b/ProjectStormtrooper/Drawnings/DrawningStormtrooperBaseByColor.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectStormtrooper.Drawnings; +/// +/// Сравнению по цвету, скорости, весу +/// +public class DrawningStormtrooperBaseByColor:IComparer +{ + public int Compare(DrawningStormtrooperBase? x, DrawningStormtrooperBase? y) + { + if (x == null || x.EntityStormtrooperBase == null) + { + return 1; + } + if (y == null || y.EntityStormtrooperBase == null) + { + return -1; + } + var bodycolorCompare = x.EntityStormtrooperBase.BodyColor.Name.CompareTo(y.EntityStormtrooperBase.BodyColor.Name); + if (bodycolorCompare != 0) + { + return bodycolorCompare; + } + var speedCompare = x.EntityStormtrooperBase.Speed.CompareTo(y.EntityStormtrooperBase.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityStormtrooperBase.Weight.CompareTo(y.EntityStormtrooperBase.Weight); + } +} diff --git a/ProjectStormtrooper/Drawnings/DrawningStormtrooperBaseByType.cs b/ProjectStormtrooper/Drawnings/DrawningStormtrooperBaseByType.cs new file mode 100644 index 0000000..76d04ec --- /dev/null +++ b/ProjectStormtrooper/Drawnings/DrawningStormtrooperBaseByType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectStormtrooper.Drawnings; +/// +/// Сравнению по типу, скорости, весу +/// +public class DrawningStormtrooperBaseByType: IComparer +{ + public int Compare(DrawningStormtrooperBase? x, DrawningStormtrooperBase? y) + { + if (x == null || x.EntityStormtrooperBase == null) + { + return 1; + } + if (y == null || y.EntityStormtrooperBase == null) + { + return -1; + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityStormtrooperBase.Speed.CompareTo(y.EntityStormtrooperBase.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityStormtrooperBase.Weight.CompareTo(y.EntityStormtrooperBase.Weight); + } +} diff --git a/ProjectStormtrooper/Drawnings/ExtentionDrawningStormtrooper.cs b/ProjectStormtrooper/Drawnings/ExtentionDrawningStormtrooperBase.cs similarity index 97% rename from ProjectStormtrooper/Drawnings/ExtentionDrawningStormtrooper.cs rename to ProjectStormtrooper/Drawnings/ExtentionDrawningStormtrooperBase.cs index 24f4101..4791e77 100644 --- a/ProjectStormtrooper/Drawnings/ExtentionDrawningStormtrooper.cs +++ b/ProjectStormtrooper/Drawnings/ExtentionDrawningStormtrooperBase.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ProjectStormtrooper.Drawnings { - public static class ExtentionDrawningStormtrooper + public static class ExtentionDrawningStormtrooperBase { /// /// Разделитель для записи информации по объекту в файл diff --git a/ProjectStormtrooper/Exceptions/ObjectIsEqualException.cs b/ProjectStormtrooper/Exceptions/ObjectIsEqualException.cs new file mode 100644 index 0000000..185f05d --- /dev/null +++ b/ProjectStormtrooper/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 ProjectStormtrooper.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) { } +} diff --git a/ProjectStormtrooper/FormStormtrooperCollection.Designer.cs b/ProjectStormtrooper/FormStormtrooperCollection.Designer.cs index 006cca3..b6b90eb 100644 --- a/ProjectStormtrooper/FormStormtrooperCollection.Designer.cs +++ b/ProjectStormtrooper/FormStormtrooperCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); panelCompanyTools = new Panel(); buttonAddStormtrooperBase = new Button(); maskedTextBox = new MaskedTextBox(); @@ -68,14 +70,38 @@ groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(911, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(173, 646); + groupBoxTools.Size = new Size(173, 687); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByColor.Location = new Point(5, 238); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(153, 32); + buttonSortByColor.TabIndex = 10; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByType.Location = new Point(3, 196); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(155, 36); + buttonSortByType.TabIndex = 9; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); panelCompanyTools.Controls.Add(buttonAddStormtrooperBase); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(maskedTextBox); panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Controls.Add(buttonRemoveStormtrooper); @@ -83,7 +109,7 @@ panelCompanyTools.Enabled = false; panelCompanyTools.Location = new Point(6, 361); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(161, 261); + panelCompanyTools.Size = new Size(161, 276); panelCompanyTools.TabIndex = 7; // // buttonAddStormtrooperBase @@ -99,7 +125,7 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(8, 99); + maskedTextBox.Location = new Point(7, 48); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Size = new Size(153, 23); @@ -109,7 +135,7 @@ // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(7, 209); + buttonRefresh.Location = new Point(5, 158); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(155, 32); buttonRefresh.TabIndex = 6; @@ -120,9 +146,9 @@ // buttonRemoveStormtrooper // buttonRemoveStormtrooper.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveStormtrooper.Location = new Point(6, 128); + buttonRemoveStormtrooper.Location = new Point(5, 77); buttonRemoveStormtrooper.Name = "buttonRemoveStormtrooper"; - buttonRemoveStormtrooper.Size = new Size(166, 33); + buttonRemoveStormtrooper.Size = new Size(153, 33); buttonRemoveStormtrooper.TabIndex = 4; buttonRemoveStormtrooper.Text = "Удалить Штурмовик"; buttonRemoveStormtrooper.UseVisualStyleBackColor = true; @@ -131,9 +157,9 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(7, 167); + buttonGoToCheck.Location = new Point(3, 116); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(166, 36); + buttonGoToCheck.Size = new Size(158, 36); buttonGoToCheck.TabIndex = 5; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -248,7 +274,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 24); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(911, 646); + pictureBox.Size = new Size(911, 687); pictureBox.TabIndex = 3; pictureBox.TabStop = false; // @@ -296,7 +322,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1084, 670); + ClientSize = new Size(1084, 711); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); @@ -341,5 +367,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/ProjectStormtrooper/FormStormtrooperCollection.cs b/ProjectStormtrooper/FormStormtrooperCollection.cs index 77b5c1c..c7a2016 100644 --- a/ProjectStormtrooper/FormStormtrooperCollection.cs +++ b/ProjectStormtrooper/FormStormtrooperCollection.cs @@ -247,7 +247,7 @@ public partial class FormStormtrooperCollection : Form 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); @@ -334,11 +334,35 @@ public partial class FormStormtrooperCollection : Form } } - - - - - - - + /// + /// сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + CompareStorntrooper(new DrawningStormtrooperBaseByType()); + } + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + CompareStorntrooper(new DrawningStormtrooperBaseByColor()); + } + /// + /// Сортировка по сравнителю + /// + /// + private void CompareStorntrooper(IComparer comparer) + { + if (_company == null) + { + return; + } + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } } -- 2.25.1 From f705ead5c2cda695d37292ef2fbeeac4dc724484 Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Fri, 17 May 2024 18:49:09 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=97=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/FormStormtrooperCollection.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ProjectStormtrooper/FormStormtrooperCollection.cs b/ProjectStormtrooper/FormStormtrooperCollection.cs index c7a2016..1aa7cf7 100644 --- a/ProjectStormtrooper/FormStormtrooperCollection.cs +++ b/ProjectStormtrooper/FormStormtrooperCollection.cs @@ -91,6 +91,11 @@ public partial class FormStormtrooperCollection : Form MessageBox.Show("Не удалось добавить объект"); _logger.LogError("Ошибка: {Message}", ex.Message); } + catch(ObjectIsEqualException ex) + { + MessageBox.Show("Такой объект уже существует "); + _logger.LogError("Ошибка: {Message}", ex.Message); + } } /// -- 2.25.1