From 63f5950f5c72625437ad34af573476bf73a9a5a4 Mon Sep 17 00:00:00 2001 From: "kagbie3nn@mail.ru" Date: Tue, 19 Dec 2023 01:29:53 +0400 Subject: [PATCH] =?UTF-8?q?8=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawiningShipEqutables.cs | 65 +++++++++++++++++++ .../FormShipCollection.Designer.cs | 34 ++++++++-- .../ProjectWarmlyShip/FormShipCollection.cs | 35 +++++++++- .../ProjectWarmlyShip/Program.cs | 2 +- .../ProjectWarmlyShip/SetGeneric.cs | 11 +++- .../ProjectWarmlyShip/ShipCompareByColor.cs | 51 +++++++++++++++ .../ProjectWarmlyShip/ShipCompareByType.cs | 34 ++++++++++ .../ProjectWarmlyShip/ShipsCollectionInfo.cs | 33 ++++++++++ .../ShipsGenericCollection.cs | 3 +- .../ProjectWarmlyShip/ShipsGenericStorage.cs | 32 ++++++--- 10 files changed, 279 insertions(+), 21 deletions(-) create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/DrawiningShipEqutables.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/DrawiningShipEqutables.cs b/ProjectWarmlyShip/ProjectWarmlyShip/DrawiningShipEqutables.cs new file mode 100644 index 0000000..35c937c --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/DrawiningShipEqutables.cs @@ -0,0 +1,65 @@ +using ProjectWarmlyShip.DrawningObjects; +using ProjectWarmlyShip.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + internal class DrawiningShipEqutables : IEqualityComparer + { + public bool Equals(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + 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.BodyColor != y.EntityShip.BodyColor) + { + return false; + } + if (x is DrawningWarmlyShip && y is DrawningWarmlyShip) + { + EntityWarmlyShip _warshipX = (EntityWarmlyShip)x.EntityShip; + EntityWarmlyShip _warshipY = (EntityWarmlyShip)y.EntityShip; + if (_warshipX.ShipPipes != _warshipY.ShipPipes) + { + return false; + } + if (_warshipX.ShipFuel != _warshipY.ShipFuel) + { + return false; + } + if (_warshipX.AdditionalColor != _warshipY.AdditionalColor) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningShip obj) + { + return obj.GetHashCode(); + } + + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs index 79ba4b0..152694c 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs @@ -30,6 +30,8 @@ { pictureBoxCollection = new PictureBox(); groupBox1 = new GroupBox(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); maskedTextBoxNumber = new MaskedTextBox(); buttonRefreshCollection = new Button(); buttonRemoveShip = new Button(); @@ -63,6 +65,8 @@ // // groupBox1 // + groupBox1.Controls.Add(ButtonSortByColor); + groupBox1.Controls.Add(ButtonSortByType); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(buttonRefreshCollection); groupBox1.Controls.Add(buttonRemoveShip); @@ -74,6 +78,26 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(16, 298); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(136, 23); + ButtonSortByColor.TabIndex = 5; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(16, 269); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(136, 23); + ButtonSortByType.TabIndex = 4; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // maskedTextBoxNumber // maskedTextBoxNumber.Location = new Point(16, 357); @@ -119,14 +143,14 @@ groupBox2.Controls.Add(textBoxStorageName); groupBox2.Location = new Point(625, 22); groupBox2.Name = "groupBox2"; - groupBox2.Size = new Size(175, 300); + groupBox2.Size = new Size(175, 241); groupBox2.TabIndex = 4; groupBox2.TabStop = false; groupBox2.Text = "Наборы"; // // buttonDelObject // - buttonDelObject.Location = new Point(16, 240); + buttonDelObject.Location = new Point(16, 213); buttonDelObject.Name = "buttonDelObject"; buttonDelObject.Size = new Size(136, 23); buttonDelObject.TabIndex = 3; @@ -171,14 +195,14 @@ // сохранениеToolStripMenuItem // сохранениеToolStripMenuItem.Name = "сохранениеToolStripMenuItem"; - сохранениеToolStripMenuItem.Size = new Size(180, 22); + сохранениеToolStripMenuItem.Size = new Size(141, 22); сохранениеToolStripMenuItem.Text = "Сохранение"; сохранениеToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // загрузкаToolStripMenuItem // загрузкаToolStripMenuItem.Name = "загрузкаToolStripMenuItem"; - загрузкаToolStripMenuItem.Size = new Size(180, 22); + загрузкаToolStripMenuItem.Size = new Size(141, 22); загрузкаToolStripMenuItem.Text = "Загрузка"; загрузкаToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -237,5 +261,7 @@ private MenuStrip menuStrip; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs index c0930e3..2559ee7 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -45,9 +45,9 @@ namespace ProjectWarmlyShip { int index = listBoxStorages.SelectedIndex; listBoxStorages.Items.Clear(); - foreach (var key in _storage.Keys) + for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(key); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) @@ -146,6 +146,11 @@ namespace ProjectWarmlyShip MessageBox.Show(ex.Message); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + } }); form.AddEvent(ShipDelegate); form.Show(); @@ -267,5 +272,31 @@ namespace ProjectWarmlyShip } } } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareShips(new ShipCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareShips(new ShipCompareByColor()); + /// + /// Сортировка по сравнителю + /// + /// + private void CompareShips(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.ShowShips(); + } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs index 9f4d7e4..096caf8 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs @@ -41,7 +41,7 @@ namespace ProjectWarmlyShip option.SetMinimumLevel(LogLevel.Information); option.AddSerilog(logger); - }); + }); } } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs index 840a8bb..f4564c1 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs @@ -36,14 +36,15 @@ namespace ProjectWarmlyShip.Generics _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемый корабль /// - public bool Insert(T ship) + public bool Insert(T ship, IEqualityComparer? equal = null) { - return Insert(ship, 0); + return Insert(ship, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -51,13 +52,17 @@ namespace ProjectWarmlyShip.Generics /// Добавляемый корабль /// Позиция /// - public bool Insert(T ship, int position) + public bool Insert(T ship, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new ShipNotFoundException(position); if (Count >= _maxCount) throw new StorageOverflowException(Count); + if (equal != null && _places.Contains(ship, equal)) + { + throw new ArgumentException("Добавляемый объект уже сущесвует в коллекции"); + } _places.Insert(0, ship); return true; diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs new file mode 100644 index 0000000..6c63578 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByColor.cs @@ -0,0 +1,51 @@ +using ProjectWarmlyShip.DrawningObjects; +using ProjectWarmlyShip.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + internal class ShipCompareByColor : IComparer + { + public int Compare(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityShip.BodyColor.Name != y.EntityShip.BodyColor.Name) + { + return x.EntityShip.BodyColor.Name.CompareTo(y.EntityShip.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + if (x.GetType().Name == y.GetType().Name && x is DrawningWarmlyShip) + { + EntityWarmlyShip _warshipX = (EntityWarmlyShip)x.EntityShip; + EntityWarmlyShip _warshipY = (EntityWarmlyShip)y.EntityShip; + + if (_warshipX.AdditionalColor.Name != _warshipY.AdditionalColor.Name) + { + return _warshipX.AdditionalColor.Name.CompareTo(_warshipY.AdditionalColor.Name); + } + } + + var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs new file mode 100644 index 0000000..18a002f --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipCompareByType.cs @@ -0,0 +1,34 @@ +using ProjectWarmlyShip.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + internal class ShipCompareByType : IComparer + { + public int Compare(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs new file mode 100644 index 0000000..79a5679 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsCollectionInfo.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + internal class ShipsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public ShipsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(ShipsCollectionInfo? other) + { + // TODO прописать логику сравнения по свойству Name + if (this.Name == other?.Name) + { + return true; + } + return false; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } + +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs index e281639..ffc0eed 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs @@ -17,6 +17,7 @@ namespace ProjectWarmlyShip.Generics where T : DrawningShip where U : IMoveableObject { + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Ширина окна прорисовки /// @@ -63,7 +64,7 @@ namespace ProjectWarmlyShip.Generics { return false; } - return (bool)collect?._collection.Insert(obj); + return (bool)collect?._collection.Insert(obj, new DrawiningShipEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs index 5f11424..ff3e499 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs @@ -19,11 +19,11 @@ namespace ProjectWarmlyShip /// /// Словарь (хранилище) /// - readonly Dictionary> _shipStorages; + readonly Dictionary> _shipStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _shipStorages.Keys.ToList(); + public List Keys => _shipStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -51,7 +51,7 @@ namespace ProjectWarmlyShip /// /// public ShipsGenericStorage(int pictureWidth, int pictureHeight) { - _shipStorages = new Dictionary>(); + _shipStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -62,8 +62,16 @@ namespace ProjectWarmlyShip public void AddSet(string name) { // TODO Прописать логику для добавления - if (_shipStorages.ContainsKey(name)) return; - _shipStorages[name] = new ShipsGenericCollection(_pictureWidth, _pictureHeight); + if (_shipStorages.ContainsKey(new ShipsCollectionInfo(name, string.Empty))) + { + MessageBox.Show("Словарь уже содержит набор с таким названием", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + + } + else + { + _shipStorages.Add(new ShipsCollectionInfo(name, string.Empty), new ShipsGenericCollection(_pictureWidth, _pictureHeight)); + } } /// /// Удаление набора @@ -72,8 +80,11 @@ namespace ProjectWarmlyShip public void DelSet(string name) { // TODO Прописать логику для удаления - if (!_shipStorages.ContainsKey(name)) return; - _shipStorages.Remove(name); + ShipsGenericCollection ship; + if (_shipStorages.TryGetValue(new ShipsCollectionInfo(name, string.Empty), out ship)) + { + _shipStorages.Remove(new ShipsCollectionInfo(name, string.Empty)); + } } /// /// Доступ к набору @@ -86,7 +97,8 @@ namespace ProjectWarmlyShip get { // TODO Продумать логику получения набора - if (_shipStorages.ContainsKey(ind)) return _shipStorages[ind]; + ShipsCollectionInfo infShip = new ShipsCollectionInfo(ind, string.Empty); + if (_shipStorages.ContainsKey(infShip)) return _shipStorages[infShip]; return null; } } @@ -102,7 +114,7 @@ namespace ProjectWarmlyShip File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _shipStorages) { StringBuilder records = new(); @@ -178,7 +190,7 @@ namespace ProjectWarmlyShip } } } - _shipStorages.Add(name, collection); + _shipStorages.Add(new ShipsCollectionInfo(name, string.Empty), collection); } } } -- 2.25.1