From 79db737a1e99544d5b6f016d3673a20d05881675 Mon Sep 17 00:00:00 2001 From: RozhVan Date: Sun, 19 May 2024 15:08:52 +0300 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=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 16 +++- .../CollectionInfo.cs | 77 +++++++++++++++++++ .../ICollectionGenericObjects.cs | 8 +- .../ListGenericObjects.cs | 31 +++++++- .../MassiveGenericObjects.cs | 28 ++++++- .../StorageCollection.cs | 59 +++++++------- .../Drawnings/DrawningShipCompareByColor.cs | 42 ++++++++++ .../Drawnings/DrawningShipCompareByType.cs | 29 +++++++ .../Drawnings/DrawningShipEqutables.cs | 63 +++++++++++++++ .../ObjectAlreadyInCollectionException.cs | 20 +++++ .../FormShipCollection.Designer.cs | 40 ++++++++-- .../ProjectPlane/FormShipCollection.cs | 63 ++++++++++++--- ProjectPlane/ProjectPlane/log20240519.txt | 21 +++++ 13 files changed, 442 insertions(+), 55 deletions(-) create mode 100644 ProjectPlane/ProjectPlane/CollectionGenericObjects/CollectionInfo.cs create mode 100644 ProjectPlane/ProjectPlane/Drawnings/DrawningShipCompareByColor.cs create mode 100644 ProjectPlane/ProjectPlane/Drawnings/DrawningShipCompareByType.cs create mode 100644 ProjectPlane/ProjectPlane/Drawnings/DrawningShipEqutables.cs create mode 100644 ProjectPlane/ProjectPlane/Exceptions/ObjectAlreadyInCollectionException.cs create mode 100644 ProjectPlane/ProjectPlane/log20240519.txt diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/AbstractCompany.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/AbstractCompany.cs index cbe69e2..48d7837 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/AbstractCompany.cs @@ -27,7 +27,18 @@ public abstract class AbstractCompany public static int operator +(AbstractCompany company, DrawningShip ship) { - return company._collection.Insert(ship, 0); + try + { + return company._collection.Insert(ship, 0, new DrawningShipEqutables()); + } + catch (ObjectAlreadyInCollectionException) + { + return -1; + } + catch (CollectionOverflowException) + { + return -1; + } } public static DrawningShip? operator -(AbstractCompany company, int position) @@ -53,6 +64,7 @@ public abstract class AbstractCompany Bitmap bitmap = new(_pictureWidth, _pictureHeight); Graphics graphics = Graphics.FromImage(bitmap); DrawBackgound(graphics); + SetObjectsPosition(); for (int i = 0; i < (_collection?.Count ?? 0); ++i) { @@ -72,4 +84,6 @@ public abstract class AbstractCompany protected abstract void DrawBackgound(Graphics g); protected abstract void SetObjectsPosition(); + + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); } diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/CollectionInfo.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..6a7252c --- /dev/null +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,77 @@ +namespace ProjectPlane.CollectionGenericObjects; + +/// +/// Класс, хранящиий информацию по коллекции +/// +public class CollectionInfo : IEquatable +{ + /// + /// Название + /// + public string Name { get; private set; } + + /// + /// Тип + /// + public CollectionType CollectionType { get; private set; } + + /// + /// Описание + /// + public string Description { get; private set; } + + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separator = "-"; + + /// + /// Конструктор + /// + /// Название + /// Тип + /// Описание + public CollectionInfo(string name, CollectionType collectionType, string description) + { + Name = name; + CollectionType = collectionType; + Description = description; + } + + /// + /// Создание объекта из строки + /// + /// Строка + /// Объект или null + public static CollectionInfo? GetCollectionInfo(string data) + { + string[] strs = data.Split(_separator, StringSplitOptions.RemoveEmptyEntries); + if (strs.Length < 1 || strs.Length > 3) + { + return null; + } + + return new CollectionInfo(strs[0], (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), + strs.Length > 2 ? strs[2] : string.Empty); + } + + public override string ToString() + { + return Name + _separator + CollectionType + _separator + Description; + } + + public bool Equals(CollectionInfo? other) + { + return Name == other?.Name; + } + + public override bool Equals(object? obj) + { + return Equals(obj as CollectionInfo); + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } +} diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs index eae1769..d0605f1 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,4 +1,5 @@ -namespace ProjectPlane.CollectionGenericObjects; +using ProjectPlane.Drawnings; +namespace ProjectPlane.CollectionGenericObjects; public interface ICollectionGenericObjects where T : class @@ -7,9 +8,9 @@ public interface ICollectionGenericObjects int MaxCount { get; set; } - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); T? Remove(int position); @@ -19,5 +20,6 @@ public interface ICollectionGenericObjects IEnumerable GetItems(); + void CollectionSort(IComparer comparer); } diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs index bae9274..620dd4f 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs @@ -1,5 +1,5 @@ using ProjectPlane.Exceptions; -using System.CodeDom.Compiler; +using ProjectPlane.Drawnings; namespace ProjectPlane.CollectionGenericObjects; @@ -32,31 +32,50 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если переполнение + // TODO выброс ошибки, если такой объект есть в коллекции if (Count == _maxCount) { throw new CollectionOverflowException(Count); } + for (int i = 0; i < Count; i++) + { + if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) + { + throw new ObjectAlreadyInCollectionException(i); + } + } + _collection.Add(obj); return _collection.Count; } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если выход за границы списка // TODO выброс ошибки, если переполнение + // TODO выброс ошибки, если такой объект есть в коллекции if (position < 0 || position > Count) { throw new PositionOutOfCollectionException(position); } + if (Count == _maxCount) { throw new CollectionOverflowException(Count); } + for (int i = 0; i < Count; i++) + { + if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) + { + throw new ObjectAlreadyInCollectionException(i); + } + } + _collection.Insert(position, obj); return position; } @@ -81,4 +100,10 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + // TODO + _collection.Sort(comparer); + } } diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs index f4d1b62..bebadca 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ using ProjectPlane.Exceptions; +using ProjectPlane.Drawnings; namespace ProjectPlane.CollectionGenericObjects; @@ -52,9 +53,18 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + // TODO выброс ошибки, если такой объект есть в коллекции // TODO выброс ошибки, если переполнение + for (int i = 0; i < Count; i++) + { + if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) + { + throw new ObjectAlreadyInCollectionException(i); + } + } + for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -67,15 +77,24 @@ 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 (position < 0 || position >= Count) { throw new PositionOutOfCollectionException(position); } + for (int i = 0; i < Count; i++) + { + if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) + { + throw new ObjectAlreadyInCollectionException(i); + } + } + if (_collection[position] == null) { _collection[position] = obj; @@ -129,4 +148,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } + public void CollectionSort(IComparer comparer) + { + // TODO + Array.Sort(_collection, comparer); + } } diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/StorageCollection.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/StorageCollection.cs index 2efd86d..11aefae 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/StorageCollection.cs @@ -1,6 +1,5 @@ using ProjectPlane.Drawnings; using ProjectPlane.Exceptions; -using System.Text; namespace ProjectPlane.CollectionGenericObjects; @@ -14,16 +13,16 @@ public class StorageCollection private readonly string _separatorItems = ";"; - private Dictionary> _storages; + private Dictionary> _storages; - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Конструктор /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// @@ -31,21 +30,21 @@ public class StorageCollection /// /// Название коллекции /// Тип коллекции - public void AddCollection(string name, CollectionType collectionType) + public void AddCollection(CollectionInfo info) { - if (name == null || _storages.ContainsKey(name)) + if (info == null || _storages.ContainsKey(info)) { return; } - if (collectionType == CollectionType.Massive) + if (info.CollectionType == CollectionType.Massive) { - _storages.Add(name, new MassiveGenericObjects()); + _storages.Add(info, new MassiveGenericObjects()); } - if (collectionType == CollectionType.List) + if (info.CollectionType == CollectionType.List) { - _storages.Add(name, new ListGenericObjects()); + _storages.Add(info, new ListGenericObjects()); } } @@ -53,14 +52,14 @@ public class StorageCollection /// Удаление коллекции /// /// Название коллекции - public void DelCollection(string name) + public void DelCollection(CollectionInfo info) { - if (name == null || !_storages.ContainsKey(name)) + if (info == null || !_storages.ContainsKey(info)) { return; } - _storages.Remove(name); + _storages.Remove(info); } public void SaveData(string filename) @@ -79,7 +78,7 @@ public class StorageCollection { sw.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { sw.Write(Environment.NewLine); // не сохраняем пустые коллекции @@ -90,8 +89,6 @@ public class StorageCollection sw.Write(value.Key); sw.Write(_separatorForKeyValue); - sw.Write(value.Value.GetCollectionType); - sw.Write(_separatorForKeyValue); sw.Write(value.Value.MaxCount); sw.Write(_separatorForKeyValue); @@ -134,27 +131,25 @@ public class StorageCollection while ((line = sr.ReadLine()) != null) { string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 4) + if (record.Length != 3) { continue; } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); - if (collection == null) - { - throw new InvalidOperationException("Не удалось создать коллекцию"); - } + CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции: " + record[0]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType) ?? + throw new Exception("Не удалось создать коллекцию"); + collection.MaxCount = Convert.ToInt32(record[1]); - collection.MaxCount = Convert.ToInt32(record[2]); - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { if (elem?.CreateDrawningShip() is T ship) { try { - if (collection.Insert(ship) == -1) + if (collection.Insert(ship, new DrawningShipEqutables()) == -1) { throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]); } @@ -163,20 +158,24 @@ public class StorageCollection { throw new OverflowException("Коллекция переполнена", ex); } + catch (ObjectAlreadyInCollectionException ex) + { + throw new InvalidOperationException("Объект уже присутствует в коллекции", ex); + } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } - public ICollectionGenericObjects? this[string name] + public ICollectionGenericObjects? this[CollectionInfo info] { get { - if (_storages.ContainsKey(name)) + if (_storages.ContainsKey(info)) { - return _storages[name]; + return _storages[info]; } return null; diff --git a/ProjectPlane/ProjectPlane/Drawnings/DrawningShipCompareByColor.cs b/ProjectPlane/ProjectPlane/Drawnings/DrawningShipCompareByColor.cs new file mode 100644 index 0000000..c9278b9 --- /dev/null +++ b/ProjectPlane/ProjectPlane/Drawnings/DrawningShipCompareByColor.cs @@ -0,0 +1,42 @@ +using ProjectPlane.Entities; + +namespace ProjectPlane.Drawnings; + +/// +/// Сравнение по цвету, скорости, весу +/// +public class DrawningShipCompareByColor : IComparer +{ + public int Compare(DrawningShip? x, DrawningShip? y) + { + // TODO прописать логику сравения по цветам, скорости, весу + if (x == null || x.EntityShip == null) + { + return 1; + } + if (y == null || y.EntityShip == null) + { + return -1; + } + var bodyColorCompare = y.EntityShip.ShipColor.Name.CompareTo(x.EntityShip.ShipColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x is DrawCont && y is DrawCont) + { + var additionalColorCompare = (y.EntityShip as EntityContainer).ContainerColor.Name.CompareTo( + (x.EntityShip as EntityContainer).ContainerColor.Name); + if (additionalColorCompare != 0) + { + return additionalColorCompare; + } + } + var speedCompare = y.EntityShip.Speed.CompareTo(x.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return y.EntityShip.Weight.CompareTo(x.EntityShip.Weight); + } +} diff --git a/ProjectPlane/ProjectPlane/Drawnings/DrawningShipCompareByType.cs b/ProjectPlane/ProjectPlane/Drawnings/DrawningShipCompareByType.cs new file mode 100644 index 0000000..e38f6e7 --- /dev/null +++ b/ProjectPlane/ProjectPlane/Drawnings/DrawningShipCompareByType.cs @@ -0,0 +1,29 @@ +namespace ProjectPlane.Drawnings; + +/// +/// Сравнение по типу, скорости, весу +/// +public class DrawningShipCompareByType : IComparer +{ + public int Compare(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + return 1; + } + if (y == null || y.EntityShip == null) + { + return -1; + } + if (x.GetType().Name != y.GetType().Name) + { + return y.GetType().Name.CompareTo(x.GetType().Name); + } + var speedCompare = y.EntityShip.Speed.CompareTo(x.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return y.EntityShip.Weight.CompareTo(x.EntityShip.Weight); + } +} diff --git a/ProjectPlane/ProjectPlane/Drawnings/DrawningShipEqutables.cs b/ProjectPlane/ProjectPlane/Drawnings/DrawningShipEqutables.cs new file mode 100644 index 0000000..8829b18 --- /dev/null +++ b/ProjectPlane/ProjectPlane/Drawnings/DrawningShipEqutables.cs @@ -0,0 +1,63 @@ +using ProjectPlane.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectPlane.Drawnings; + +/// +/// Реализация сравнения двух объектов класса-прорисовки +/// +public class DrawningShipEqutables : IEqualityComparer +{ + public bool Equals(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + return false; + } + if (y == null || y.EntityShip == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityShip.Speed != y.EntityShip.Speed) + { + return false; + } + if (x.EntityShip.Weight != y.EntityShip.Weight) + { + return false; + } + if (x.EntityShip.ShipColor != y.EntityShip.ShipColor) + { + return false; + } + if (x is DrawCont && y is DrawCont) + { + // TODO доделать логику сравнения дополнительных параметров + if ((x.EntityShip as EntityContainer)?.ContainerColor != + (y.EntityShip as EntityContainer)?.ContainerColor) + { + return false; + } + if ((x.EntityShip as EntityContainer)?.Container != + (y.EntityShip as EntityContainer)?.Container) + { + return false; + } + if ((x.EntityShip as EntityContainer)?.Crane != + (y.EntityShip as EntityContainer)?.Crane) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningShip? obj) + { + return obj.GetHashCode(); + } +} diff --git a/ProjectPlane/ProjectPlane/Exceptions/ObjectAlreadyInCollectionException.cs b/ProjectPlane/ProjectPlane/Exceptions/ObjectAlreadyInCollectionException.cs new file mode 100644 index 0000000..2d92cfd --- /dev/null +++ b/ProjectPlane/ProjectPlane/Exceptions/ObjectAlreadyInCollectionException.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace ProjectPlane.Exceptions; + +/// +/// Класс, описывающий ошибку переполнения коллекции +/// +[Serializable] +internal class ObjectAlreadyInCollectionException : ApplicationException +{ + public ObjectAlreadyInCollectionException(int index) : base("Такой объект уже присутствует в коллекции. Позиция " + index) { } + + public ObjectAlreadyInCollectionException() : base() { } + + public ObjectAlreadyInCollectionException(string message) : base(message) { } + + public ObjectAlreadyInCollectionException(string message, Exception exception) : base(message, exception) { } + + protected ObjectAlreadyInCollectionException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} diff --git a/ProjectPlane/ProjectPlane/FormShipCollection.Designer.cs b/ProjectPlane/ProjectPlane/FormShipCollection.Designer.cs index 7834076..cb6fcc3 100644 --- a/ProjectPlane/ProjectPlane/FormShipCollection.Designer.cs +++ b/ProjectPlane/ProjectPlane/FormShipCollection.Designer.cs @@ -30,6 +30,8 @@ { groupBoxTools = new GroupBox(); panelCompanyTools = new Panel(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); ButtonAddShip = new Button(); maskedTextBoxPosition = new MaskedTextBox(); ButtonRefresh = new Button(); @@ -75,6 +77,8 @@ // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(ButtonAddShip); panelCompanyTools.Controls.Add(maskedTextBoxPosition); panelCompanyTools.Controls.Add(ButtonRefresh); @@ -83,13 +87,33 @@ panelCompanyTools.Enabled = false; panelCompanyTools.Location = new Point(3, 400); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(194, 274); + panelCompanyTools.Size = new Size(194, 292); panelCompanyTools.TabIndex = 9; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(6, 252); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(182, 34); + buttonSortByColor.TabIndex = 8; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(6, 213); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(182, 34); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // ButtonAddShip // ButtonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - ButtonAddShip.Location = new Point(6, 20); + ButtonAddShip.Location = new Point(6, 3); ButtonAddShip.Name = "ButtonAddShip"; ButtonAddShip.Size = new Size(182, 40); ButtonAddShip.TabIndex = 1; @@ -99,7 +123,7 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(6, 111); + maskedTextBoxPosition.Location = new Point(6, 49); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(188, 23); @@ -109,7 +133,7 @@ // ButtonRefresh // ButtonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - ButtonRefresh.Location = new Point(6, 230); + ButtonRefresh.Location = new Point(6, 168); ButtonRefresh.Name = "ButtonRefresh"; ButtonRefresh.Size = new Size(182, 39); ButtonRefresh.TabIndex = 6; @@ -120,18 +144,18 @@ // ButtonDel // ButtonDel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - ButtonDel.Location = new Point(6, 140); + ButtonDel.Location = new Point(6, 78); ButtonDel.Name = "ButtonDel"; ButtonDel.Size = new Size(182, 39); ButtonDel.TabIndex = 4; - ButtonDel.Text = "Удплить контейнеровоз"; + ButtonDel.Text = "Удалить контейнеровоз"; ButtonDel.UseVisualStyleBackColor = true; ButtonDel.Click += ButtonDel_Click; // // ButtonGoToCheck // ButtonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - ButtonGoToCheck.Location = new Point(6, 185); + ButtonGoToCheck.Location = new Point(6, 123); ButtonGoToCheck.Name = "ButtonGoToCheck"; ButtonGoToCheck.Size = new Size(182, 39); ButtonGoToCheck.TabIndex = 5; @@ -333,5 +357,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/ProjectPlane/ProjectPlane/FormShipCollection.cs b/ProjectPlane/ProjectPlane/FormShipCollection.cs index 0d92b02..c1a2e97 100644 --- a/ProjectPlane/ProjectPlane/FormShipCollection.cs +++ b/ProjectPlane/ProjectPlane/FormShipCollection.cs @@ -34,27 +34,40 @@ public partial class FormShipCollection : Form private void SetShip(DrawningShip? ship) { + if (_company == null || ship == null) + { + return; + } try { - if (_company == null || ship == null) - { - return; - } - if (_company + ship != -1) { MessageBox.Show("Объект добавлен"); _logger.LogInformation($"Добавлен объект {ship.GetDataForSave()}"); pictureBox.Image = _company.Show(); } + else + { + MessageBox.Show("Не удалось добавить объект"); + } } catch (CollectionOverflowException ex) { MessageBox.Show(ex.Message); _logger.LogWarning($"Ошибка: {ex.Message}"); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Ошибка: {ex.Message}"); + } } + /// + /// Удаление объекта + /// + /// + /// private void ButtonDel_Click(object sender, EventArgs e) { @@ -122,6 +135,11 @@ public partial class FormShipCollection : Form } + /// + /// перерисовка коллекции + /// + /// + /// private void ButtonRefresh_Click(object sender, EventArgs e) { if (_company == null) @@ -137,7 +155,7 @@ public partial class FormShipCollection : 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); @@ -150,6 +168,7 @@ public partial class FormShipCollection : Form if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: Заполнены не все данные для добавления коллекции"); return; } @@ -162,8 +181,10 @@ public partial class FormShipCollection : Form { collectionType = CollectionType.List; } + CollectionInfo collectionInfo = new CollectionInfo(textBoxCollectionName.Text, collectionType, string.Empty); - _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); + _storageCollection.AddCollection(collectionInfo); + _logger.LogInformation($"Добавлена коллекция: {textBoxCollectionName.Text} типа: {collectionType}"); RerfreshListBoxItems(); } @@ -179,7 +200,10 @@ public partial class FormShipCollection : Form { return; } - _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + + CollectionInfo collectionInfo = new CollectionInfo(listBoxCollection.SelectedItem.ToString(), CollectionType.None, string.Empty); + _storageCollection.DelCollection(collectionInfo); + _logger.LogInformation($"Удалена коллекция: {listBoxCollection.SelectedItem.ToString()}"); RerfreshListBoxItems(); } @@ -191,7 +215,8 @@ public partial class FormShipCollection : Form return; } - ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; + CollectionInfo collectionInfo = new CollectionInfo(listBoxCollection.SelectedItem.ToString(), CollectionType.None, string.Empty); + ICollectionGenericObjects? collection = _storageCollection[collectionInfo]; if (collection == null) { MessageBox.Show("Коллекция не проинициализирована"); @@ -244,4 +269,24 @@ public partial class FormShipCollection : Form } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + CompareShips(new DrawningShipCompareByType()); + } + + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + CompareShips(new DrawningShipCompareByColor()); + } + + private void CompareShips(IComparer comparer) + { + if (_company == null) + { + return; + } + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } } diff --git a/ProjectPlane/ProjectPlane/log20240519.txt b/ProjectPlane/ProjectPlane/log20240519.txt new file mode 100644 index 0000000..01d60f2 --- /dev/null +++ b/ProjectPlane/ProjectPlane/log20240519.txt @@ -0,0 +1,21 @@ +2024-05-19 14:40:29.0277 | INFORMATION | ProjectPlane.FormShipCollection | Добавлена коллекция: fgjd типа: List +2024-05-19 14:40:36.9497 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Yellow +2024-05-19 14:41:42.9096 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Yellow:Red:True:True +2024-05-19 14:42:44.9889 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Blue +2024-05-19 14:42:55.1814 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Red +2024-05-19 14:43:35.2025 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Blue:Purple:True:True +2024-05-19 14:44:32.1260 | INFORMATION | ProjectPlane.FormShipCollection | Добавлена коллекция: dhd типа: Massive +2024-05-19 14:44:43.1068 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Red +2024-05-19 14:44:49.7075 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Blue +2024-05-19 14:45:05.2935 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Red:Black:True:True +2024-05-19 14:45:34.8489 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Blue:Black:False:True +2024-05-19 15:03:53.6387 | INFORMATION | ProjectPlane.FormShipCollection | Добавлена коллекция: вапр типа: Massive +2024-05-19 15:04:10.5066 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Blue +2024-05-19 15:04:15.2984 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Red +2024-05-19 15:04:21.7942 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Blue:Black:True:True +2024-05-19 15:04:40.3403 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Purple +2024-05-19 15:04:51.3945 | INFORMATION | ProjectPlane.FormShipCollection | Добавлена коллекция: вао типа: List +2024-05-19 15:05:05.4559 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Yellow +2024-05-19 15:05:17.1129 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Purple:Yellow:True:True +2024-05-19 15:06:00.3431 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Blue:Black:True:True +2024-05-19 15:06:12.9361 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Green -- 2.25.1 From d8637d4a9d830c57563b4e94e081527a3becfca1 Mon Sep 17 00:00:00 2001 From: RozhVan Date: Mon, 20 May 2024 13:29:41 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ICollectionGenericObjects.cs | 4 +- .../ListGenericObjects.cs | 37 ++++++++----------- .../MassiveGenericObjects.cs | 35 ++++++++++-------- ProjectPlane/ProjectPlane/log20240520.txt | 12 ++++++ 4 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 ProjectPlane/ProjectPlane/log20240520.txt diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs index d0605f1..74d1a9b 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -8,9 +8,9 @@ public interface ICollectionGenericObjects int MaxCount { get; set; } - int Insert(T obj, IEqualityComparer? comparer = null); + int Insert(T obj, IEqualityComparer? comparer = null); - int Insert(T obj, int position, IEqualityComparer? comparer = null); + int Insert(T obj, int position, IEqualityComparer? comparer = null); T? Remove(int position); diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs index 620dd4f..9f3ccf5 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/ListGenericObjects.cs @@ -32,50 +32,45 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj, IEqualityComparer? comparer = null) + public int Insert(T obj, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если переполнение - // TODO выброс ошибки, если такой объект есть в коллекции + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectAlreadyInCollectionException(); + } + } if (Count == _maxCount) { throw new CollectionOverflowException(Count); } - for (int i = 0; i < Count; i++) - { - if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) - { - throw new ObjectAlreadyInCollectionException(i); - } - } - _collection.Add(obj); return _collection.Count; } - public int Insert(T obj, int position, IEqualityComparer? comparer = null) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { // TODO выброс ошибки, если выход за границы списка // TODO выброс ошибки, если переполнение - // TODO выброс ошибки, если такой объект есть в коллекции + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectAlreadyInCollectionException(); + } + } if (position < 0 || position > Count) { throw new PositionOutOfCollectionException(position); } - if (Count == _maxCount) { throw new CollectionOverflowException(Count); } - for (int i = 0; i < Count; i++) - { - if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) - { - throw new ObjectAlreadyInCollectionException(i); - } - } - _collection.Insert(position, obj); return position; } diff --git a/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs index bebadca..1254fd9 100644 --- a/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectPlane/ProjectPlane/CollectionGenericObjects/MassiveGenericObjects.cs @@ -53,18 +53,21 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj, IEqualityComparer? comparer = null) + public int Insert(T obj, IEqualityComparer? comparer = null) { - // TODO выброс ошибки, если такой объект есть в коллекции // TODO выброс ошибки, если переполнение - for (int i = 0; i < Count; i++) + if (comparer != null) { - if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) + foreach (T? item in _collection) { - throw new ObjectAlreadyInCollectionException(i); + if ((comparer as IEqualityComparer).Equals(obj as DrawningShip, item as DrawningShip)) + { + throw new ObjectAlreadyInCollectionException(); + } } } + for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -77,24 +80,26 @@ public class MassiveGenericObjects : ICollectionGenericObjects throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position, IEqualityComparer? comparer = null) + 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 DrawningShip, item as DrawningShip)) + { + throw new ObjectAlreadyInCollectionException(); + } + } + } + if (position < 0 || position >= Count) { throw new PositionOutOfCollectionException(position); } - for (int i = 0; i < Count; i++) - { - if (comparer.Equals((_collection[i] as DrawningShip), (obj as DrawningShip))) - { - throw new ObjectAlreadyInCollectionException(i); - } - } - if (_collection[position] == null) { _collection[position] = obj; diff --git a/ProjectPlane/ProjectPlane/log20240520.txt b/ProjectPlane/ProjectPlane/log20240520.txt new file mode 100644 index 0000000..ca355b0 --- /dev/null +++ b/ProjectPlane/ProjectPlane/log20240520.txt @@ -0,0 +1,12 @@ +2024-05-20 13:25:42.8162 | INFORMATION | ProjectPlane.FormShipCollection | Добавлена коллекция: edhh типа: Massive +2024-05-20 13:25:50.1068 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Blue +2024-05-20 13:25:54.2419 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Red +2024-05-20 13:26:02.3952 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Blue:Red:True:True +2024-05-20 13:26:19.6205 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Purple +2024-05-20 13:26:34.6992 | INFORMATION | ProjectPlane.FormShipCollection | Добавлена коллекция: eha blya типа: List +2024-05-20 13:26:47.2484 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Black:Black:True:True +2024-05-20 13:27:00.5461 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Black:White:True:True +2024-05-20 13:28:19.2272 | INFORMATION | ProjectPlane.FormShipCollection | Удален объект по позиции 1 +2024-05-20 13:28:39.0064 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityContainer:100:100:Red:Purple:True:True +2024-05-20 13:28:46.8157 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Red +2024-05-20 13:29:12.9298 | INFORMATION | ProjectPlane.FormShipCollection | Добавлен объект EntityShip:100:100:Black -- 2.25.1