From 3e1a8890b544bc5378bd08dff1da2764e6815919 Mon Sep 17 00:00:00 2001 From: Egor_Shtyrkin Date: Sat, 8 Jun 2024 02:01:21 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=968=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 8 ++- .../CollectionInfo.cs | 49 ++++++++++++++ .../ICollectionGenericObjects.cs | 12 +++- .../ListGenericObjects.cs | 23 ++++++- .../MassiveGenericObjects.cs | 29 ++++++++- .../StorageCollection.cs | 41 ++++++------ .../DrawningWarshipCompareByColor.cs | 34 ++++++++++ .../Drawnings/DrawningWarshipCompareByType.cs | 35 ++++++++++ .../Drawnings/DrawningWarshipEqutables.cs | 65 +++++++++++++++++++ .../Exceptions/ObjectIsEqualException.cs | 11 ++++ 10 files changed, 280 insertions(+), 27 deletions(-) create mode 100644 ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/CollectionInfo.cs create mode 100644 ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs create mode 100644 ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs create mode 100644 ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs create mode 100644 ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs index 92867a5..1f0f818 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs @@ -64,7 +64,7 @@ public abstract class AbstractCompany /// public static int operator +(AbstractCompany company, DrawningWarship warship) { - return company._collection.Insert(warship); + return company._collection.Insert(warship, new DrawningWarshipEqutables()); } /// @@ -122,4 +122,10 @@ public abstract class AbstractCompany /// Расстановка объектов /// protected abstract void SetObjectPosition(); + + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/CollectionInfo.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..a823f54 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.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(); + } +} \ No newline at end of file diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs index 2452d03..a54c4e9 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -27,16 +27,18 @@ public interface ICollectionGenericObjects /// Добавление объекта в коллекцию /// /// Добавляемый объект + /// Сравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// Добавляемый объект /// Позиция + /// Сравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции @@ -62,4 +64,10 @@ public interface ICollectionGenericObjects /// /// Поэлементный вывод элементов коллекции IEnumerable GetItems(); + + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ListGenericObjects.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ListGenericObjects.cs index d71a199..e0f67b1 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ListGenericObjects.cs @@ -48,15 +48,29 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + 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) { + 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); @@ -77,4 +91,9 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs index 24dfa0c..eef8c4a 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,5 +1,7 @@ -using ProjectAircraftCarrier.Exceptions; +using ProjectAircraftCarrier.Drawnings; +using ProjectAircraftCarrier.Exceptions; using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -60,8 +62,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 DrawningWarship, item as DrawningWarship)) + throw new ObjectIsEqualException(); + } + } int index = 0; while (index < _collection.Length) { @@ -75,8 +85,16 @@ 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) { + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningWarship, item as DrawningWarship)) + throw new ObjectIsEqualException(); + } + } if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) { @@ -122,4 +140,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs index a7b5def..e5f53be 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs @@ -10,19 +10,19 @@ 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>(); } /// @@ -32,12 +32,13 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (_storages.ContainsKey(name)) return; + CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty); + if (_storages.ContainsKey(collectionInfo)) return; if (collectionType == CollectionType.None) return; else if (collectionType == CollectionType.Massive) - _storages[name] = new MassiveGenericObjects(); + _storages[collectionInfo] = new MassiveGenericObjects(); else if (collectionType == CollectionType.List) - _storages[name] = new ListGenericObjects(); + _storages[collectionInfo] = new ListGenericObjects(); } /// @@ -46,8 +47,9 @@ public class StorageCollection /// Название коллекции 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); } /// @@ -59,8 +61,9 @@ public class StorageCollection { 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; } } @@ -100,7 +103,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) @@ -110,8 +113,6 @@ public class StorageCollection writer.Write(value.Key); writer.Write(_separatorForKeyValue); - writer.Write(value.Value.GetCollectionType); - writer.Write(_separatorForKeyValue); writer.Write(value.Value.MaxCount); writer.Write(_separatorForKeyValue); @@ -160,20 +161,22 @@ 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?.CreateDrawningWarship() is T warship) @@ -191,7 +194,7 @@ public class StorageCollection } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs new file mode 100644 index 0000000..ff57024 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.Drawnings; + +public class DrawningShipCompareByColor : IComparer +{ + public int Compare(DrawningWarship? x, DrawningWarship? y) + { + if (x == null || x.EntityWarship == null) + { + return 1; + } + + if (y == null || y.EntityWarship == null) + { + return -1; + } + var bodycolorCompare = x.EntityWarship.BodyColor.Name.CompareTo(y.EntityWarship.BodyColor.Name); + if (bodycolorCompare != 0) + { + return bodycolorCompare; + } + var speedCompare = x.EntityWarship.Speed.CompareTo(y.EntityWarship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityWarship.Weight.CompareTo(y.EntityWarship.Weight); + } +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs new file mode 100644 index 0000000..0c77084 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.Drawnings; + +public class DrawningShipCompareByType : IComparer +{ + public int Compare(DrawningWarship? x, DrawningWarship? y) + { + if (x == null || x.EntityWarship == null) + { + return 1; + } + + if (y == null || y.EntityWarship == null) + { + return -1; + } + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + + var speedCompare = x.EntityWarship.Speed.CompareTo(y.EntityWarship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityWarship.Weight.CompareTo(y.EntityWarship.Weight); + } +} \ No newline at end of file diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs new file mode 100644 index 0000000..1e62665 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAircraftCarrier.Entities; + +namespace ProjectAircraftCarrier.Drawnings; + +/// +/// Реализация сравнения двух объектов класса-прорисовки +/// +public class DrawningWarshipEqutables : IEqualityComparer +{ + public bool Equals(DrawningWarship? x, DrawningWarship? y) + { + if (x == null || x.EntityWarship == null) + { + return false; + } + if (y == null || y.EntityWarship == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityWarship.Speed != y.EntityWarship.Speed) + { + return false; + } + if (x.EntityWarship.Weight != y.EntityWarship.Weight) + { + return false; + } + if (x.EntityWarship.BodyColor != y.EntityWarship.BodyColor) + { + return false; + } + if (x is DrawningAircraftCarrier && y is DrawningAircraftCarrier) + { + DrawningAircraftCarrier _x = (DrawningAircraftCarrier)x.EntityWarship; + DrawningAircraftCarrier _y = (DrawningAircraftCarrier)x.EntityWarship; + if (_x.AdditionalColor != _y.AdditionalColor) + { + return false; + } + if (_x.AircraftDeck != _y.AircraftDeck) + { + return false; + } + if (_x.ControlRoom != _y.ControlRoom) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningWarship obj) + { + return obj.GetHashCode(); + } +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs new file mode 100644 index 0000000..976d5e0 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.Exceptions; + +internal class ObjectIsEqualException +{ +} -- 2.25.1 From f4110d81aca24e3762ad98af54a1f1a5baa368ad Mon Sep 17 00:00:00 2001 From: Shtyrkin_Egor Date: Sat, 8 Jun 2024 08:48:04 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=968=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.cs | 2 +- .../DrawningWarshipCompareByColor.cs | 2 +- .../Drawnings/DrawningWarshipCompareByType.cs | 2 +- .../Drawnings/DrawningWarshipEqutables.cs | 4 +- .../Exceptions/ObjectIsEqualException.cs | 12 +- .../FormWarshipCollection.Designer.cs | 136 ++++++++++++------ .../FormWarshipCollection.cs | 27 +++- 7 files changed, 135 insertions(+), 50 deletions(-) diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs index e5f53be..e9e2508 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/StorageCollection.cs @@ -166,7 +166,7 @@ public class StorageCollection continue; } - CollectionInfo? collectionInfo = collectionInfo.GetCollectionInfo(record[0]) ?? + CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? throw new Exception("Не удалось определить информацию коллекции: " + record[0]); ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType) ?? throw new Exception("Не удалось создать коллекцию"); diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs index ff57024..0cf4dc4 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByColor.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectAircraftCarrier.Drawnings; -public class DrawningShipCompareByColor : IComparer +public class DrawningWarshipCompareByColor : IComparer { public int Compare(DrawningWarship? x, DrawningWarship? y) { diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs index 0c77084..c08d140 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipCompareByType.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectAircraftCarrier.Drawnings; -public class DrawningShipCompareByType : IComparer +public class DrawningWarshipCompareByType : IComparer { public int Compare(DrawningWarship? x, DrawningWarship? y) { diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs index 1e62665..af40945 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarshipEqutables.cs @@ -41,8 +41,8 @@ public class DrawningWarshipEqutables : IEqualityComparer } if (x is DrawningAircraftCarrier && y is DrawningAircraftCarrier) { - DrawningAircraftCarrier _x = (DrawningAircraftCarrier)x.EntityWarship; - DrawningAircraftCarrier _y = (DrawningAircraftCarrier)x.EntityWarship; + EntityAircraftCarrier _x = (EntityAircraftCarrier)x.EntityWarship; + EntityAircraftCarrier _y = (EntityAircraftCarrier)x.EntityWarship; if (_x.AdditionalColor != _y.AdditionalColor) { return false; diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs index 976d5e0..a6149be 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Exceptions/ObjectIsEqualException.cs @@ -1,11 +1,21 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace ProjectAircraftCarrier.Exceptions; -internal class ObjectIsEqualException +/// +/// Класс, описывающий ошибку переполнения коллекции +/// +[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/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.Designer.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.Designer.cs index 70d0c73..549801c 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.Designer.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.Designer.cs @@ -52,6 +52,8 @@ loadToolStripMenuItem = new ToolStripMenuItem(); saveFileDialog = new SaveFileDialog(); openFileDialog = new OpenFileDialog(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxTools.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); @@ -66,15 +68,19 @@ groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(916, 28); + groupBoxTools.Location = new Point(802, 24); + groupBoxTools.Margin = new Padding(3, 2, 3, 2); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(205, 731); + groupBoxTools.Padding = new Padding(3, 2, 3, 2); + groupBoxTools.Size = new Size(179, 640); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(buttonAddWarship); panelCompanyTools.Controls.Add(maskedTextBox); panelCompanyTools.Controls.Add(buttonRefresh); @@ -82,17 +88,19 @@ panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 400); + panelCompanyTools.Location = new Point(3, 345); + panelCompanyTools.Margin = new Padding(3, 2, 3, 2); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(199, 328); + panelCompanyTools.Size = new Size(173, 293); panelCompanyTools.TabIndex = 9; // // buttonAddWarship // buttonAddWarship.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddWarship.Location = new Point(3, 21); + buttonAddWarship.Location = new Point(3, 2); + buttonAddWarship.Margin = new Padding(3, 2, 3, 2); buttonAddWarship.Name = "buttonAddWarship"; - buttonAddWarship.Size = new Size(193, 52); + buttonAddWarship.Size = new Size(168, 39); buttonAddWarship.TabIndex = 1; buttonAddWarship.Text = "Добавление\r\nвоенного корабля"; buttonAddWarship.UseVisualStyleBackColor = true; @@ -100,19 +108,21 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(3, 119); + maskedTextBox.Location = new Point(3, 44); + maskedTextBox.Margin = new Padding(3, 2, 3, 2); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(193, 27); + maskedTextBox.Size = new Size(169, 23); maskedTextBox.TabIndex = 3; maskedTextBox.ValidatingType = typeof(int); // // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(3, 268); + buttonRefresh.Location = new Point(3, 158); + buttonRefresh.Margin = new Padding(3, 2, 3, 2); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(193, 52); + buttonRefresh.Size = new Size(168, 39); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -121,9 +131,10 @@ // buttonRemoveWarship // buttonRemoveWarship.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveWarship.Location = new Point(3, 152); + buttonRemoveWarship.Location = new Point(3, 71); + buttonRemoveWarship.Margin = new Padding(3, 2, 3, 2); buttonRemoveWarship.Name = "buttonRemoveWarship"; - buttonRemoveWarship.Size = new Size(193, 52); + buttonRemoveWarship.Size = new Size(168, 39); buttonRemoveWarship.TabIndex = 4; buttonRemoveWarship.Text = "Удалить \r\nвоенный корабль"; buttonRemoveWarship.UseVisualStyleBackColor = true; @@ -132,9 +143,10 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(3, 210); + buttonGoToCheck.Location = new Point(3, 115); + buttonGoToCheck.Margin = new Padding(3, 2, 3, 2); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(193, 52); + buttonGoToCheck.Size = new Size(168, 39); buttonGoToCheck.TabIndex = 5; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -142,9 +154,10 @@ // // buttonCreateCompany // - buttonCreateCompany.Location = new Point(6, 369); + buttonCreateCompany.Location = new Point(5, 277); + buttonCreateCompany.Margin = new Padding(3, 2, 3, 2); buttonCreateCompany.Name = "buttonCreateCompany"; - buttonCreateCompany.Size = new Size(193, 29); + buttonCreateCompany.Size = new Size(169, 22); buttonCreateCompany.TabIndex = 8; buttonCreateCompany.Text = "Создать компанию"; buttonCreateCompany.UseVisualStyleBackColor = true; @@ -160,16 +173,18 @@ panelStorage.Controls.Add(textBoxCollectionName); panelStorage.Controls.Add(labelCollectionName); panelStorage.Dock = DockStyle.Top; - panelStorage.Location = new Point(3, 23); + panelStorage.Location = new Point(3, 18); + panelStorage.Margin = new Padding(3, 2, 3, 2); panelStorage.Name = "panelStorage"; - panelStorage.Size = new Size(199, 290); + panelStorage.Size = new Size(173, 218); panelStorage.TabIndex = 7; // // buttonCollectionDel // - buttonCollectionDel.Location = new Point(3, 241); + buttonCollectionDel.Location = new Point(3, 181); + buttonCollectionDel.Margin = new Padding(3, 2, 3, 2); buttonCollectionDel.Name = "buttonCollectionDel"; - buttonCollectionDel.Size = new Size(193, 29); + buttonCollectionDel.Size = new Size(169, 22); buttonCollectionDel.TabIndex = 6; buttonCollectionDel.Text = "Удалить коллекцию"; buttonCollectionDel.UseVisualStyleBackColor = true; @@ -178,17 +193,19 @@ // listBoxCollection // listBoxCollection.FormattingEnabled = true; - listBoxCollection.ItemHeight = 20; - listBoxCollection.Location = new Point(6, 131); + listBoxCollection.ItemHeight = 15; + listBoxCollection.Location = new Point(5, 98); + listBoxCollection.Margin = new Padding(3, 2, 3, 2); listBoxCollection.Name = "listBoxCollection"; - listBoxCollection.Size = new Size(193, 104); + listBoxCollection.Size = new Size(169, 79); listBoxCollection.TabIndex = 5; // // buttonCollectionAdd // - buttonCollectionAdd.Location = new Point(3, 96); + buttonCollectionAdd.Location = new Point(3, 72); + buttonCollectionAdd.Margin = new Padding(3, 2, 3, 2); buttonCollectionAdd.Name = "buttonCollectionAdd"; - buttonCollectionAdd.Size = new Size(193, 29); + buttonCollectionAdd.Size = new Size(169, 22); buttonCollectionAdd.TabIndex = 4; buttonCollectionAdd.Text = "Добавить коллекцию"; buttonCollectionAdd.UseVisualStyleBackColor = true; @@ -197,9 +214,10 @@ // radioButtonList // radioButtonList.AutoSize = true; - radioButtonList.Location = new Point(101, 66); + radioButtonList.Location = new Point(88, 50); + radioButtonList.Margin = new Padding(3, 2, 3, 2); radioButtonList.Name = "radioButtonList"; - radioButtonList.Size = new Size(80, 24); + radioButtonList.Size = new Size(66, 19); radioButtonList.TabIndex = 3; radioButtonList.TabStop = true; radioButtonList.Text = "Список"; @@ -208,9 +226,10 @@ // radioButtonMassive // radioButtonMassive.AutoSize = true; - radioButtonMassive.Location = new Point(13, 66); + radioButtonMassive.Location = new Point(11, 50); + radioButtonMassive.Margin = new Padding(3, 2, 3, 2); radioButtonMassive.Name = "radioButtonMassive"; - radioButtonMassive.Size = new Size(82, 24); + radioButtonMassive.Size = new Size(67, 19); radioButtonMassive.TabIndex = 2; radioButtonMassive.TabStop = true; radioButtonMassive.Text = "Массив"; @@ -218,17 +237,18 @@ // // textBoxCollectionName // - textBoxCollectionName.Location = new Point(3, 33); + textBoxCollectionName.Location = new Point(3, 25); + textBoxCollectionName.Margin = new Padding(3, 2, 3, 2); textBoxCollectionName.Name = "textBoxCollectionName"; - textBoxCollectionName.Size = new Size(193, 27); + textBoxCollectionName.Size = new Size(169, 23); textBoxCollectionName.TabIndex = 1; // // labelCollectionName // labelCollectionName.AutoSize = true; - labelCollectionName.Location = new Point(20, 10); + labelCollectionName.Location = new Point(18, 8); labelCollectionName.Name = "labelCollectionName"; - labelCollectionName.Size = new Size(158, 20); + labelCollectionName.Size = new Size(125, 15); labelCollectionName.TabIndex = 0; labelCollectionName.Text = "Название коллекции:"; // @@ -238,18 +258,20 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 335); + comboBoxSelectorCompany.Location = new Point(5, 251); + comboBoxSelectorCompany.Margin = new Padding(3, 2, 3, 2); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; - comboBoxSelectorCompany.Size = new Size(193, 28); + comboBoxSelectorCompany.Size = new Size(169, 23); comboBoxSelectorCompany.TabIndex = 0; comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; // // pictureBox // pictureBox.Dock = DockStyle.Fill; - pictureBox.Location = new Point(0, 28); + pictureBox.Location = new Point(0, 24); + pictureBox.Margin = new Padding(3, 2, 3, 2); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(916, 731); + pictureBox.Size = new Size(802, 640); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -259,7 +281,8 @@ menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; - menuStrip.Size = new Size(1121, 28); + menuStrip.Padding = new Padding(5, 2, 0, 2); + menuStrip.Size = new Size(981, 24); menuStrip.TabIndex = 2; menuStrip.Text = "menuStrip"; // @@ -267,14 +290,14 @@ // файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); файлToolStripMenuItem.Name = "файлToolStripMenuItem"; - файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.Size = new Size(48, 20); файлToolStripMenuItem.Text = "Файл"; // // saveToolStripMenuItem // saveToolStripMenuItem.Name = "saveToolStripMenuItem"; saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; - saveToolStripMenuItem.Size = new Size(227, 26); + saveToolStripMenuItem.Size = new Size(181, 22); saveToolStripMenuItem.Text = "Сохранение"; saveToolStripMenuItem.Click += saveToolStripMenuItem_Click; // @@ -282,7 +305,7 @@ // loadToolStripMenuItem.Name = "loadToolStripMenuItem"; loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; - loadToolStripMenuItem.Size = new Size(227, 26); + loadToolStripMenuItem.Size = new Size(181, 22); loadToolStripMenuItem.Text = "Загрузка"; loadToolStripMenuItem.Click += loadToolStripMenuItem_Click; // @@ -294,15 +317,40 @@ // openFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByColor + // + buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByColor.Location = new Point(2, 244); + buttonSortByColor.Margin = new Padding(3, 2, 3, 2); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(168, 39); + 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(2, 201); + buttonSortByType.Margin = new Padding(3, 2, 3, 2); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(168, 39); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // FormWarshipCollection // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1121, 759); + ClientSize = new Size(981, 664); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); MainMenuStrip = menuStrip; + Margin = new Padding(3, 2, 3, 2); Name = "FormWarshipCollection"; Text = "Коллекция военных кораблей"; groupBoxTools.ResumeLayout(false); @@ -343,5 +391,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/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.cs index 044243a..bc5ef6a 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.cs @@ -78,6 +78,11 @@ public partial class FormWarshipCollection : Form MessageBox.Show("Не удалось добавить объект"); _logger.LogError("Ошибка: {Message}", ex.Message); } + catch (ObjectIsEqualException ex) + { + MessageBox.Show("Такой объект уже есть в коллекции"); + _logger.LogError("Ошибка: {Message}", ex.Message); + } } private void ButtonRemoveWarship_Click(object sender, EventArgs e) @@ -153,7 +158,7 @@ public partial class FormWarshipCollection : 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); @@ -273,4 +278,24 @@ public partial class FormWarshipCollection : Form } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + CompareWarship(new DrawningWarshipCompareByType()); + } + + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + CompareWarship(new DrawningWarshipCompareByColor()); + } + + private void CompareWarship(IComparer comparer) + { + if (_company == null) + { + return; + } + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } } \ No newline at end of file -- 2.25.1