From 5af581e096a66dbc4bfc07f205cb1ac431cc806d Mon Sep 17 00:00:00 2001 From: Marselchi Date: Mon, 18 Dec 2023 16:11:55 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A4=D1=83=D0=BB=D0=BB=208=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=20=D0=BD=D0=B0=D0=B4=D0=B5=D1=8E=D1=81=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Liner/FormLinerCollection.Designer.cs | 36 +++++++++++-- Liner/FormLinerCollection.cs | 37 +++++++++++++- Liner/Generics/DrawingLinerEqutables.cs | 64 ++++++++++++++++++++++++ Liner/Generics/LinerCollectionInfo.cs | 30 +++++++++++ Liner/Generics/LinerCompareByColor.cs | 44 ++++++++++++++++ Liner/Generics/LinerCompareByType.cs | 34 +++++++++++++ Liner/Generics/LinerGenericCollection.cs | 8 ++- Liner/Generics/LinersGenericStorage.cs | 22 ++++---- Liner/Generics/SetGeneric.cs | 11 ++-- 9 files changed, 265 insertions(+), 21 deletions(-) create mode 100644 Liner/Generics/DrawingLinerEqutables.cs create mode 100644 Liner/Generics/LinerCollectionInfo.cs create mode 100644 Liner/Generics/LinerCompareByColor.cs create mode 100644 Liner/Generics/LinerCompareByType.cs diff --git a/Liner/FormLinerCollection.Designer.cs b/Liner/FormLinerCollection.Designer.cs index b203180..c4bfcdb 100644 --- a/Liner/FormLinerCollection.Designer.cs +++ b/Liner/FormLinerCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + buttonSortByType = new Button(); + buttonSortByColor = new Button(); Storages = new GroupBox(); listBoxStorages = new ListBox(); buttonDeleteSet = new Button(); @@ -53,6 +55,8 @@ // // groupBoxTools // + groupBoxTools.Controls.Add(buttonSortByType); + groupBoxTools.Controls.Add(buttonSortByColor); groupBoxTools.Controls.Add(Storages); groupBoxTools.Controls.Add(buttonRefreshCollection); groupBoxTools.Controls.Add(buttonDeleteLiner); @@ -67,6 +71,28 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Tools"; // + // buttonSortByType + // + buttonSortByType.Location = new Point(28, 413); + buttonSortByType.Margin = new Padding(3, 4, 3, 4); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(190, 41); + buttonSortByType.TabIndex = 8; + buttonSortByType.Text = "Sort By Type"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(28, 462); + buttonSortByColor.Margin = new Padding(3, 4, 3, 4); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(190, 41); + buttonSortByColor.TabIndex = 7; + buttonSortByColor.Text = "Sort By Color"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // // Storages // Storages.Controls.Add(listBoxStorages); @@ -125,7 +151,7 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(13, 668); + buttonRefreshCollection.Location = new Point(13, 691); buttonRefreshCollection.Margin = new Padding(3, 4, 3, 4); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(222, 53); @@ -136,7 +162,7 @@ // // buttonDeleteLiner // - buttonDeleteLiner.Location = new Point(13, 544); + buttonDeleteLiner.Location = new Point(13, 607); buttonDeleteLiner.Margin = new Padding(3, 4, 3, 4); buttonDeleteLiner.Name = "buttonDeleteLiner"; buttonDeleteLiner.Size = new Size(222, 53); @@ -147,7 +173,7 @@ // // textBoxNumber // - textBoxNumber.Location = new Point(47, 491); + textBoxNumber.Location = new Point(47, 572); textBoxNumber.Margin = new Padding(3, 4, 3, 4); textBoxNumber.Name = "textBoxNumber"; textBoxNumber.Size = new Size(158, 27); @@ -155,7 +181,7 @@ // // buttonAddLiner // - buttonAddLiner.Location = new Point(13, 417); + buttonAddLiner.Location = new Point(13, 511); buttonAddLiner.Margin = new Padding(3, 4, 3, 4); buttonAddLiner.Name = "buttonAddLiner"; buttonAddLiner.Size = new Size(222, 53); @@ -256,5 +282,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/Liner/FormLinerCollection.cs b/Liner/FormLinerCollection.cs index 3bd16de..409ff63 100644 --- a/Liner/FormLinerCollection.cs +++ b/Liner/FormLinerCollection.cs @@ -85,6 +85,11 @@ namespace Liner _logger.LogWarning($"Добавление лайнера в {listBoxStorages.SelectedItem.ToString()} не удалось (переполнение коллекции)"); MessageBox.Show(ex.Message); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); + } } /// /// Удаление объекта из набора @@ -224,7 +229,7 @@ namespace Liner listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(_storage.Keys[i]); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) { @@ -272,5 +277,35 @@ namespace Liner } } } + /// + /// Сортировка по сравнителю + /// + /// + private void CompareLiners(IComparer comparer) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowLiners(); + } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareLiners(new LinerCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareLiners(new LinerCompareByColor()); } } diff --git a/Liner/Generics/DrawingLinerEqutables.cs b/Liner/Generics/DrawingLinerEqutables.cs new file mode 100644 index 0000000..0bf082b --- /dev/null +++ b/Liner/Generics/DrawingLinerEqutables.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Liner.Drawing; +using Liner.Entities; +namespace Liner.Generics +{ + internal class DrawingLinerEqutables : IEqualityComparer + { + public bool Equals(DrawingLiner? x, DrawingLiner? y) + { + if (x == null || x.EntityLiner == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityLiner == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityLiner.Speed != y.EntityLiner.Speed) + { + return false; + } + if (x.EntityLiner.Weight != y.EntityLiner.Weight) + { + return false; + } + if (x.EntityLiner.BottomColor != y.EntityLiner.BottomColor) + { + return false; + } + if (x is DrawingBigLiner && y is DrawingBigLiner) + { + EntityBigLiner _LinerX = (EntityBigLiner)x.EntityLiner; + EntityBigLiner _LinerY = (EntityBigLiner)y.EntityLiner; + if (_LinerX.SwimmingPool != _LinerY.SwimmingPool) + { + return false; + } + if (_LinerX.Deck != _LinerY.Deck) + { + return false; + } + if (_LinerX.BodyColor != _LinerY.BodyColor) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawingLiner obj) + { + return obj.GetHashCode(); + } + } + +} diff --git a/Liner/Generics/LinerCollectionInfo.cs b/Liner/Generics/LinerCollectionInfo.cs new file mode 100644 index 0000000..3f7f86b --- /dev/null +++ b/Liner/Generics/LinerCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Liner.Generics +{ + public class LinerCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public LinerCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(LinerCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } +} diff --git a/Liner/Generics/LinerCompareByColor.cs b/Liner/Generics/LinerCompareByColor.cs new file mode 100644 index 0000000..9f90dc8 --- /dev/null +++ b/Liner/Generics/LinerCompareByColor.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Liner.Entities; +using Liner.Drawing; + +namespace Liner.Generics +{ + internal class LinerCompareByColor : IComparer + { + public int Compare(DrawingLiner? x, DrawingLiner? y) + { + if (x == null || x.EntityLiner == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityLiner == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bottomColorCompare = x.EntityLiner.BottomColor.Name.CompareTo(y.EntityLiner.BottomColor.Name); + if (bottomColorCompare != 0) + { + return bottomColorCompare; + } + if (x.EntityLiner is EntityBigLiner _entityBigLinerX && y.EntityLiner is EntityBigLiner _entityBigLinerY) + { + var bodyColorCompare = _entityBigLinerX.BodyColor.Name.CompareTo(_entityBigLinerY.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + } + var speedCompare = x.EntityLiner.Speed.CompareTo(y.EntityLiner.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityLiner.Weight.CompareTo(y.EntityLiner.Weight); + } + } +} diff --git a/Liner/Generics/LinerCompareByType.cs b/Liner/Generics/LinerCompareByType.cs new file mode 100644 index 0000000..87a3bc2 --- /dev/null +++ b/Liner/Generics/LinerCompareByType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Liner.Drawing; +using Liner.Entities; +namespace Liner.Generics +{ + internal class LinerCompareByType : IComparer + { + public int Compare(DrawingLiner? x, DrawingLiner? y) + { + if (x == null || x.EntityLiner == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityLiner == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityLiner.Speed.CompareTo(y.EntityLiner.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityLiner.Weight.CompareTo(y.EntityLiner.Weight); + } + } +} diff --git a/Liner/Generics/LinerGenericCollection.cs b/Liner/Generics/LinerGenericCollection.cs index b33de5e..8918f22 100644 --- a/Liner/Generics/LinerGenericCollection.cs +++ b/Liner/Generics/LinerGenericCollection.cs @@ -54,7 +54,11 @@ namespace Liner.Generics /// Получение объектов коллекции /// public IEnumerable GetLiners => _collection.GetLiners(); - + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// Перегрузка оператора сложения /// /// @@ -66,7 +70,7 @@ namespace Liner.Generics { return -1; } - return collect?._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawingLinerEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/Liner/Generics/LinersGenericStorage.cs b/Liner/Generics/LinersGenericStorage.cs index 119a21d..b33b949 100644 --- a/Liner/Generics/LinersGenericStorage.cs +++ b/Liner/Generics/LinersGenericStorage.cs @@ -16,11 +16,11 @@ namespace Liner.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _linerStorages; + readonly Dictionary> _linerStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _linerStorages.Keys.ToList(); + public List Keys => _linerStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -48,7 +48,7 @@ namespace Liner.Generics /// public LinersGenericStorage(int pictureWidth, int pictureHeight) { - _linerStorages = new Dictionary>(); + _linerStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -58,11 +58,11 @@ namespace Liner.Generics /// Название набора public void AddSet(string name) { - if(_linerStorages.ContainsKey(name)) + if(_linerStorages.ContainsKey(new LinerCollectionInfo(name, string.Empty))) { return; } - _linerStorages.Add(name, new LinerGenericCollection(_pictureWidth,_pictureHeight)); + _linerStorages.Add(new LinerCollectionInfo(name, string.Empty), new LinerGenericCollection(_pictureWidth,_pictureHeight)); } /// /// Удаление набора @@ -70,9 +70,9 @@ namespace Liner.Generics /// Название набора public void DelSet(string name) { - if (_linerStorages.ContainsKey(name)) + if (_linerStorages.ContainsKey(new LinerCollectionInfo(name, string.Empty))) { - _linerStorages.Remove(name); + _linerStorages.Remove(new LinerCollectionInfo(name, string.Empty)); } } /// @@ -84,7 +84,7 @@ namespace Liner.Generics { get { - if (_linerStorages.ContainsKey(ind)) { return _linerStorages[ind]; } + if (_linerStorages.ContainsKey(new LinerCollectionInfo(ind, string.Empty))) { return _linerStorages[new LinerCollectionInfo(ind, string.Empty)]; } return null; } } @@ -99,7 +99,7 @@ namespace Liner.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _linerStorages) { StringBuilder records = new(); @@ -107,7 +107,7 @@ namespace Liner.Generics { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -167,7 +167,7 @@ namespace Liner.Generics } } } - _linerStorages.Add(name, collection); + _linerStorages.Add(new LinerCollectionInfo(name, string.Empty), collection); } } } diff --git a/Liner/Generics/SetGeneric.cs b/Liner/Generics/SetGeneric.cs index 9afda34..0167086 100644 --- a/Liner/Generics/SetGeneric.cs +++ b/Liner/Generics/SetGeneric.cs @@ -40,13 +40,13 @@ namespace Liner.Generics /// /// Добавляемый лайнер /// - public int Insert(T liner) + public int Insert(T liner, IEqualityComparer? equal = null) { if (_places.Count >= _maxCount) { throw new StorageOverflowException(_places.Count); } - _places.Insert(0, liner); + return Insert(liner,0, equal); return 0; } @@ -56,7 +56,7 @@ namespace Liner.Generics /// Добавляемый лайнер /// Позиция /// - public int Insert(T liner, int position) + public int Insert(T liner, int position, IEqualityComparer? equal = null) { if(_places.Count >= _maxCount) { @@ -66,6 +66,10 @@ namespace Liner.Generics { throw new LinerNotFoundException(position); } + if (equal != null && _places.Contains(liner, equal)) + { + throw new ArgumentException("Этот объект уже существует в коллекции"); + } _places.Insert(position, liner); return position; } @@ -83,6 +87,7 @@ namespace Liner.Generics } throw new LinerNotFoundException(position); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Получение объекта из набора по позиции ///