From ea87423301ccca175e782fdafc9c2c89f77c1052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Wed, 27 Dec 2023 21:57:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatCompareByColor.cs | 50 ++++++++++++++++ Sailboat/Sailboat/BoatCompareByType.cs | 35 +++++++++++ Sailboat/Sailboat/BoatsCollectionInfo.cs | 30 ++++++++++ Sailboat/Sailboat/BoatsGenericCollection.cs | 5 +- Sailboat/Sailboat/BoatsGenericStorage.cs | 32 ++++------ Sailboat/Sailboat/DrawingBoatEqutables.cs | 59 +++++++++++++++++++ Sailboat/Sailboat/DrawningSailboat.cs | 3 +- .../Sailboat/FormBoatCollection.Designer.cs | 38 ++++++++++-- Sailboat/Sailboat/FormBoatCollection.cs | 26 +++++++- Sailboat/Sailboat/SetGeneric.cs | 35 +++++------ 10 files changed, 264 insertions(+), 49 deletions(-) create mode 100644 Sailboat/Sailboat/BoatCompareByColor.cs create mode 100644 Sailboat/Sailboat/BoatCompareByType.cs create mode 100644 Sailboat/Sailboat/BoatsCollectionInfo.cs create mode 100644 Sailboat/Sailboat/DrawingBoatEqutables.cs diff --git a/Sailboat/Sailboat/BoatCompareByColor.cs b/Sailboat/Sailboat/BoatCompareByColor.cs new file mode 100644 index 0000000..b3aae56 --- /dev/null +++ b/Sailboat/Sailboat/BoatCompareByColor.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.Entities; + +namespace Sailboat.Generics +{ + internal class BoatCompareByColor : IComparer + { + public int Compare(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bodyColorCompare = x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityBoat is EntitySailboat xEntitySailboat && y.EntityBoat is EntitySailboat yEntitySailboat) + { + var dumpBoxColorCompare = xEntitySailboat.BodyColor.Name.CompareTo(yEntitySailboat.BodyColor.Name); + if (dumpBoxColorCompare != 0) + { + return dumpBoxColorCompare; + } + var tentColorCompare = xEntitySailboat.AdditionalColor.Name.CompareTo(yEntitySailboat.AdditionalColor.Name); + if (tentColorCompare != 0) + { + return tentColorCompare; + } + } + var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight); + } + } +} diff --git a/Sailboat/Sailboat/BoatCompareByType.cs b/Sailboat/Sailboat/BoatCompareByType.cs new file mode 100644 index 0000000..0ea9bfd --- /dev/null +++ b/Sailboat/Sailboat/BoatCompareByType.cs @@ -0,0 +1,35 @@ +using Sailboat.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Generics +{ + internal class BoatCompareByType : IComparer + { + public int Compare(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight); + } + } +} \ No newline at end of file diff --git a/Sailboat/Sailboat/BoatsCollectionInfo.cs b/Sailboat/Sailboat/BoatsCollectionInfo.cs new file mode 100644 index 0000000..0c5a834 --- /dev/null +++ b/Sailboat/Sailboat/BoatsCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Generics +{ + internal class BoatsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BoatsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BoatsCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Sailboat/Sailboat/BoatsGenericCollection.cs b/Sailboat/Sailboat/BoatsGenericCollection.cs index 27b85bf..c44773e 100644 --- a/Sailboat/Sailboat/BoatsGenericCollection.cs +++ b/Sailboat/Sailboat/BoatsGenericCollection.cs @@ -38,6 +38,7 @@ namespace Sailboat.Generics /// private readonly SetGeneric _collection; public IEnumerable GetBoats => _collection.GetBoats(); + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Конструктор /// @@ -63,7 +64,7 @@ namespace Sailboat.Generics { return false; } - return (bool)collect?._collection.Insert(obj); + return (bool)collect?._collection.Insert(obj, new DrawingBoatEqutables()); } /// /// Перегрузка оператора вычитания @@ -140,4 +141,4 @@ namespace Sailboat.Generics } } } -} +} \ No newline at end of file diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index c9390b5..4030d5e 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -15,11 +15,11 @@ namespace Sailboat.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _boatStorages; + readonly Dictionary> _boatStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _boatStorages.Keys.ToList(); + public List Keys => _boatStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -47,7 +47,7 @@ namespace Sailboat.Generics /// public BoatsGenericStorage(int pictureWidth, int pictureHeight) { - _boatStorages = new Dictionary>(); + _boatStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -57,11 +57,7 @@ namespace Sailboat.Generics /// Название набора public void AddSet(string name) { - if (_boatStorages.ContainsKey(name)) - { - return; - } - _boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); + _boatStorages.Add(new BoatsCollectionInfo(name, string.Empty), new BoatsGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -69,11 +65,9 @@ namespace Sailboat.Generics /// Название набора public void DelSet(string name) { - if (!_boatStorages.ContainsKey(name)) - { + if (!_boatStorages.ContainsKey(new BoatsCollectionInfo(name, string.Empty))) return; - } - _boatStorages.Remove(name); + _boatStorages.Remove(new BoatsCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -84,10 +78,9 @@ namespace Sailboat.Generics { get { - if (_boatStorages.ContainsKey(ind)) - { - return _boatStorages[ind]; - } + BoatsCollectionInfo indObj = new BoatsCollectionInfo(ind, string.Empty); + if (_boatStorages.ContainsKey(indObj)) + return _boatStorages[indObj]; return null; } } @@ -105,15 +98,14 @@ namespace Sailboat.Generics } StringBuilder data = new(); - foreach (KeyValuePair> record in _boatStorages) + foreach (KeyValuePair> record in _boatStorages) { StringBuilder records = new(); foreach (DrawingBoat? elem in record.Value.GetBoats) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); - + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -180,7 +172,7 @@ namespace Sailboat.Generics } } } - _boatStorages.Add(name, collection); + _boatStorages.Add(new BoatsCollectionInfo(name, string.Empty), collection); } } } diff --git a/Sailboat/Sailboat/DrawingBoatEqutables.cs b/Sailboat/Sailboat/DrawingBoatEqutables.cs new file mode 100644 index 0000000..42af3f6 --- /dev/null +++ b/Sailboat/Sailboat/DrawingBoatEqutables.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.Entities; + +namespace Sailboat.Generics +{ + internal class DrawingBoatEqutables : IEqualityComparer + { + public bool Equals(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityBoat.Speed != y.EntityBoat.Speed) + { + return false; + } + if (x.EntityBoat.Weight != y.EntityBoat.Weight) + { + return false; + } + if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor) + { + return false; + } + if (x is DrawingSailboat && y is DrawingSailboat) + { + EntitySailboat EntityX = (EntitySailboat)x.EntityBoat; + EntitySailboat EntityY = (EntitySailboat)y.EntityBoat; + if (EntityX.Sail != EntityY.Sail) + return false; + if (EntityX.Hull != EntityY.Hull) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingBoat obj) + { + return obj.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Sailboat/Sailboat/DrawningSailboat.cs b/Sailboat/Sailboat/DrawningSailboat.cs index c53bda1..c9dda29 100644 --- a/Sailboat/Sailboat/DrawningSailboat.cs +++ b/Sailboat/Sailboat/DrawningSailboat.cs @@ -25,7 +25,8 @@ namespace Sailboat.DrawingObjects /// Признак наличия паруса /// Ширина картинки /// Высота картинки - public DrawingSailboat(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail, int width, int height) : base(speed, weight, bodyColor, width, height, 200, 160) + public DrawingSailboat(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail, int width, int height) : + base(speed, weight, bodyColor, width, height, 200, 160) { if (EntityBoat != null) { diff --git a/Sailboat/Sailboat/FormBoatCollection.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index 7968f58..6d33438 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.Designer.cs @@ -34,6 +34,8 @@ buttonRefreshCollection = new Button(); maskedTextBoxNumber = new MaskedTextBox(); groupBoxTools = new GroupBox(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxCollection = new GroupBox(); textBoxStorageName = new TextBox(); listBoxStorages = new ListBox(); @@ -99,6 +101,8 @@ // // groupBoxTools // + groupBoxTools.Controls.Add(buttonSortByColor); + groupBoxTools.Controls.Add(buttonSortByType); groupBoxTools.Controls.Add(groupBoxCollection); groupBoxTools.Controls.Add(buttonAddBoat); groupBoxTools.Controls.Add(buttonRefreshCollection); @@ -112,15 +116,35 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(12, 367); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(184, 29); + buttonSortByColor.TabIndex = 8; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(12, 335); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(184, 29); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // groupBoxCollection // groupBoxCollection.Controls.Add(textBoxStorageName); groupBoxCollection.Controls.Add(listBoxStorages); groupBoxCollection.Controls.Add(buttonDelObject); groupBoxCollection.Controls.Add(buttonAddObject); - groupBoxCollection.Location = new Point(6, 75); + groupBoxCollection.Location = new Point(6, 54); groupBoxCollection.Name = "groupBoxCollection"; - groupBoxCollection.Size = new Size(196, 299); + groupBoxCollection.Size = new Size(196, 275); groupBoxCollection.TabIndex = 5; groupBoxCollection.TabStop = false; groupBoxCollection.Text = "Наборы"; @@ -144,9 +168,9 @@ // // buttonDelObject // - buttonDelObject.Location = new Point(5, 256); + buttonDelObject.Location = new Point(6, 231); buttonDelObject.Name = "buttonDelObject"; - buttonDelObject.Size = new Size(191, 37); + buttonDelObject.Size = new Size(184, 37); buttonDelObject.TabIndex = 1; buttonDelObject.Text = "Удалить набор"; buttonDelObject.UseVisualStyleBackColor = true; @@ -182,14 +206,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(224, 26); + SaveToolStripMenuItem.Size = new Size(177, 26); SaveToolStripMenuItem.Text = "Сохранение"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(224, 26); + LoadToolStripMenuItem.Size = new Size(177, 26); LoadToolStripMenuItem.Text = "Загрузка"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -241,5 +265,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index 7ab1f8e..316a7ff 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -33,7 +33,7 @@ namespace Sailboat 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)) @@ -90,6 +90,10 @@ namespace Sailboat MessageBox.Show(ex.Message); _logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}"); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + } } private void buttonRemoveBoat_Click(object sender, EventArgs e) @@ -219,5 +223,25 @@ namespace Sailboat } } } + + private void buttonSortByType_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByType()); + + private void CompareBoats(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.ShowBoats(); + } + + private void buttonSortByColor_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByColor()); } } \ No newline at end of file diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs index b3d3f93..2b4f419 100644 --- a/Sailboat/Sailboat/SetGeneric.cs +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -1,10 +1,10 @@ using System; -using Sailboat.Exceptions; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using System.Windows.Forms; + +using Sailboat.Exceptions; namespace Sailboat.Generics { @@ -22,6 +22,7 @@ namespace Sailboat.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Конструктор /// @@ -36,14 +37,9 @@ namespace Sailboat.Generics /// /// Добавляемая лодка /// - public bool Insert(T boat) + public bool Insert(T boat, IEqualityComparer? equal = null) { - if (_places.Count == _maxCount) - { - throw new StorageOverflowException(_maxCount); - } - Insert(boat, 0); - return true; + return Insert(boat, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -51,15 +47,17 @@ namespace Sailboat.Generics /// Добавляемая лодка /// Позиция /// - public bool Insert(T boat, int position) + public bool Insert(T boat, int position, IEqualityComparer? equal = null) { - if (_places.Count == _maxCount) + if (position < 0 || position >= _maxCount) + throw new BoatNotFoundException(position); + + if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - if (!(position >= 0 && position <= Count)) - { - return false; - } - _places.Insert(position, boat); + + if (equal != null && _places.Contains(boat, equal)) + throw new ArgumentException("Данный объект уже есть в коллекции"); + _places.Insert(0, boat); return true; } /// @@ -69,10 +67,9 @@ namespace Sailboat.Generics /// public bool Remove(int position) { - if (position < 0 || position >= Count) - { + if (position < 0 || position > _maxCount || position >= Count) throw new BoatNotFoundException(position); - } + _places.RemoveAt(position); return true; } -- 2.25.1