From eeb94aad766a075f2738b72f3b3725b8888e85f1 Mon Sep 17 00:00:00 2001 From: nikos77781 Date: Mon, 10 Jun 2024 01:29:45 +0400 Subject: [PATCH] =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbsractCompany.cs | 10 +++- .../CollectionInfo.cs | 12 ++++ .../ICollectionGenericObjects.cs | 10 +++- .../ListGenericObjects.cs | 23 +++++++- .../MassiveGenericObjects.cs | 23 +++++++- .../StorageCollection.cs | 57 ++++++++++--------- .../DrawningExcavatorCompareByColor.cs | 12 ++++ .../DrawningExcavatorCompareByType.cs | 12 ++++ .../Drawnings/DrawningExcavatorEqutables.cs | 12 ++++ .../Exceptions/ObjectNotUniqueException.cs | 12 ++++ .../Excavator/FormExcavatorCollection.cs | 9 ++- 11 files changed, 156 insertions(+), 36 deletions(-) create mode 100644 Excavator/Excavator/CollectionGenericObjects/CollectionInfo.cs create mode 100644 Excavator/Excavator/Drawnings/DrawningExcavatorCompareByColor.cs create mode 100644 Excavator/Excavator/Drawnings/DrawningExcavatorCompareByType.cs create mode 100644 Excavator/Excavator/Drawnings/DrawningExcavatorEqutables.cs create mode 100644 Excavator/Excavator/Exceptions/ObjectNotUniqueException.cs diff --git a/Excavator/Excavator/CollectionGenericObjects/AbsractCompany.cs b/Excavator/Excavator/CollectionGenericObjects/AbsractCompany.cs index e0f2baa..1e98832 100644 --- a/Excavator/Excavator/CollectionGenericObjects/AbsractCompany.cs +++ b/Excavator/Excavator/CollectionGenericObjects/AbsractCompany.cs @@ -57,9 +57,9 @@ public abstract class AbstractCompany /// Компания /// Добавляемый объект /// - public static int operator +(AbstractCompany company, DrawningSimpleExcavator car) + public static int operator +(AbstractCompany company, DrawningSimpleExcavator excavator) { - return company._collection.Insert(car); + return company._collection.Insert(excavator, new DrawningExcavatorEqutables()); } /// @@ -103,6 +103,12 @@ public abstract class AbstractCompany return bitmap; } + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); + /// /// Вывод заднего фона /// diff --git a/Excavator/Excavator/CollectionGenericObjects/CollectionInfo.cs b/Excavator/Excavator/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..73f0948 --- /dev/null +++ b/Excavator/Excavator/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Excavator.CollectionGenericObjects +{ + internal class CollectionInfo + { + } +} diff --git a/Excavator/Excavator/CollectionGenericObjects/ICollectionGenericObjects.cs b/Excavator/Excavator/CollectionGenericObjects/ICollectionGenericObjects.cs index a157a28..f97505f 100644 --- a/Excavator/Excavator/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/Excavator/Excavator/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -22,7 +22,7 @@ public interface ICollectionGenericObjects /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию @@ -30,7 +30,7 @@ public interface ICollectionGenericObjects /// Добавляемый объект /// Позиция /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции @@ -57,4 +57,10 @@ public interface ICollectionGenericObjects /// /// Поэлементный вывод элементов коллекции IEnumerable GetItems(); + + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } diff --git a/Excavator/Excavator/CollectionGenericObjects/ListGenericObjects.cs b/Excavator/Excavator/CollectionGenericObjects/ListGenericObjects.cs index e8ed05e..fb772aa 100644 --- a/Excavator/Excavator/CollectionGenericObjects/ListGenericObjects.cs +++ b/Excavator/Excavator/CollectionGenericObjects/ListGenericObjects.cs @@ -45,14 +45,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 ObjectNotUniqueException(); + } + } 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 ObjectNotUniqueException(); + } + } + if (Count == _maxCount) throw new CollectionOverflowException(Count); ; if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); _collection.Insert(position, obj); @@ -74,5 +89,9 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/Excavator/Excavator/CollectionGenericObjects/MassiveGenericObjects.cs b/Excavator/Excavator/CollectionGenericObjects/MassiveGenericObjects.cs index 6a040d4..3f87330 100644 --- a/Excavator/Excavator/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/Excavator/Excavator/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,6 @@ using Excavator.Exceptions; +using Excavator.Drawnings; +using System.Collections.Immutable; namespace Excavator.CollectionGenericObjects; @@ -59,9 +61,19 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - for(int i = 0; i < Count; i++) + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningSimpleExcavator, item as DrawningSimpleExcavator)) + { + throw new ObjectNotUniqueException(); + } + } + } + for (int i = 0; i < Count; i++) { if (_collection[i] == null) { @@ -73,7 +85,7 @@ 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 (position < 0 || position >= Count) @@ -130,4 +142,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } \ No newline at end of file diff --git a/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs b/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs index addfd0e..7934e15 100644 --- a/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs +++ b/Excavator/Excavator/CollectionGenericObjects/StorageCollection.cs @@ -9,12 +9,12 @@ public class StorageCollection where T : DrawningSimpleExcavator /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Ключевое слово, с которого должен начинаться файл @@ -36,7 +36,7 @@ public class StorageCollection where T : DrawningSimpleExcavator /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// /// Добавление коллекции в хранилище @@ -45,19 +45,17 @@ public class StorageCollection where T : DrawningSimpleExcavator /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (name == null || _storages.ContainsKey(name)) - return; - switch (collectionType) + CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty); + + if (_storages.ContainsKey(collectionInfo) || collectionInfo.CollectionType == CollectionType.None) return; + switch (collectionInfo.CollectionType) { - case CollectionType.None: - return; case CollectionType.Massive: - _storages[name] = new MassiveGenericObjects(); - return; + _storages[collectionInfo] = new MassiveGenericObjects(); + break; case CollectionType.List: - _storages[name] = new ListGenericObjects(); - return; - default: break; + _storages[collectionInfo] = new ListGenericObjects(); + break; } } @@ -67,8 +65,13 @@ public class StorageCollection where T : DrawningSimpleExcavator /// Название коллекции 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) && collectionInfo != null) + { + _storages.Remove(collectionInfo); + } + } } /// @@ -80,8 +83,11 @@ public class StorageCollection where T : DrawningSimpleExcavator { 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; } } @@ -106,7 +112,7 @@ public class StorageCollection where T : DrawningSimpleExcavator using (StreamWriter writer = new StreamWriter(filename)) { writer.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { StringBuilder sb = new(); @@ -119,8 +125,6 @@ public class StorageCollection where T : DrawningSimpleExcavator sb.Append(value.Key); sb.Append(_separatorForKeyValue); - sb.Append(value.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); sb.Append(value.Value.MaxCount); sb.Append(_separatorForKeyValue); @@ -172,18 +176,19 @@ public class StorageCollection where T : DrawningSimpleExcavator 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("Не удалось определить тип коллекции: " + record[1]); - collection.MaxCount = Convert.ToInt32(record[2]); + collection.MaxCount = Convert.ToInt32(record[1]); - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { if (elem?.CreateDrawningSimpleExcavator() is T excavator) @@ -201,7 +206,7 @@ public class StorageCollection where T : DrawningSimpleExcavator } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } diff --git a/Excavator/Excavator/Drawnings/DrawningExcavatorCompareByColor.cs b/Excavator/Excavator/Drawnings/DrawningExcavatorCompareByColor.cs new file mode 100644 index 0000000..4bc7948 --- /dev/null +++ b/Excavator/Excavator/Drawnings/DrawningExcavatorCompareByColor.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Excavator.Drawnings +{ + internal class DrawningAirbusCompareByColor + { + } +} diff --git a/Excavator/Excavator/Drawnings/DrawningExcavatorCompareByType.cs b/Excavator/Excavator/Drawnings/DrawningExcavatorCompareByType.cs new file mode 100644 index 0000000..a5b920f --- /dev/null +++ b/Excavator/Excavator/Drawnings/DrawningExcavatorCompareByType.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Excavator.Drawnings +{ + internal class DrawningAirbusCompareByType + { + } +} diff --git a/Excavator/Excavator/Drawnings/DrawningExcavatorEqutables.cs b/Excavator/Excavator/Drawnings/DrawningExcavatorEqutables.cs new file mode 100644 index 0000000..9a938d0 --- /dev/null +++ b/Excavator/Excavator/Drawnings/DrawningExcavatorEqutables.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Excavator.Drawnings +{ + internal class DrawningAirbusEqutables + { + } +} diff --git a/Excavator/Excavator/Exceptions/ObjectNotUniqueException.cs b/Excavator/Excavator/Exceptions/ObjectNotUniqueException.cs new file mode 100644 index 0000000..ababe7e --- /dev/null +++ b/Excavator/Excavator/Exceptions/ObjectNotUniqueException.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Excavator.Exceptions +{ + internal class ObjectNotUniqueException + { + } +} diff --git a/Excavator/Excavator/FormExcavatorCollection.cs b/Excavator/Excavator/FormExcavatorCollection.cs index ede6845..cbf36b7 100644 --- a/Excavator/Excavator/FormExcavatorCollection.cs +++ b/Excavator/Excavator/FormExcavatorCollection.cs @@ -73,6 +73,11 @@ public partial class FormExcavatorCollection : Form MessageBox.Show(ex.Message); _logger.LogError("Ошибка: {Message}", ex.Message); } + catch (ObjectNotUniqueException ex) + { + MessageBox.Show("Такой объект уже присутствует в коллекции"); + _logger.LogError("Ошибка: {Message}", ex.Message); + } } private void ButtonRemoveExcavator_Click(object sender, EventArgs e) @@ -178,7 +183,7 @@ public partial class FormExcavatorCollection : 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); @@ -305,6 +310,8 @@ public partial class FormExcavatorCollection : Form _logger.LogError("Ошибка: {Message}", ex.Message); } } + } + } \ No newline at end of file