From b17f2d743dacd244eec281f064591e39f8731689 Mon Sep 17 00:00:00 2001 From: Dasha Date: Sat, 30 Dec 2023 13:14:20 +0400 Subject: [PATCH] done --- WarmlyShip/FormShipCollection.Designer.cs | 40 +++++++++-- WarmlyShip/FormShipCollection.cs | 25 +++++-- WarmlyShip/Generics/DrawiningShipEqutables.cs | 66 +++++++++++++++++++ WarmlyShip/Generics/SetGeneric.cs | 21 ++++-- WarmlyShip/Generics/ShipCompareByColor.cs | 39 +++++++++++ WarmlyShip/Generics/ShipCompareByType.cs | 36 ++++++++++ WarmlyShip/Generics/ShipsCollectionInfo.cs | 30 +++++++++ WarmlyShip/Generics/ShipsGenericCollection.cs | 8 ++- WarmlyShip/Generics/ShipsGenericStorage.cs | 23 +++---- 9 files changed, 260 insertions(+), 28 deletions(-) create mode 100644 WarmlyShip/Generics/DrawiningShipEqutables.cs create mode 100644 WarmlyShip/Generics/ShipCompareByColor.cs create mode 100644 WarmlyShip/Generics/ShipCompareByType.cs create mode 100644 WarmlyShip/Generics/ShipsCollectionInfo.cs diff --git a/WarmlyShip/FormShipCollection.Designer.cs b/WarmlyShip/FormShipCollection.Designer.cs index 1237368..ff3889e 100644 --- a/WarmlyShip/FormShipCollection.Designer.cs +++ b/WarmlyShip/FormShipCollection.Designer.cs @@ -45,8 +45,10 @@ FileToolStripMenuItem = new ToolStripMenuItem(); SaveToolStripMenuItem = new ToolStripMenuItem(); LoadToolStripMenuItem = new ToolStripMenuItem(); - openFileDialog = new System.Windows.Forms.OpenFileDialog(); - saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + openFileDialog = new OpenFileDialog(); + saveFileDialog = new SaveFileDialog(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); toolsPanel.SuspendLayout(); panelSets.SuspendLayout(); @@ -63,6 +65,8 @@ // // toolsPanel // + toolsPanel.Controls.Add(ButtonSortByType); + toolsPanel.Controls.Add(ButtonSortByColor); toolsPanel.Controls.Add(LabelSets); toolsPanel.Controls.Add(panelSets); toolsPanel.Controls.Add(LabelTools); @@ -71,7 +75,7 @@ toolsPanel.Controls.Add(maskedTextBoxNumber); toolsPanel.Controls.Add(ButtonAddShip); toolsPanel.Location = new Point(660, 12); - toolsPanel.Name = "toolsLabel"; + toolsPanel.Name = "toolsPanel"; toolsPanel.Size = new Size(230, 495); toolsPanel.TabIndex = 0; // @@ -143,7 +147,7 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(45, 432); + ButtonRefreshCollection.Location = new Point(45, 453); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(142, 37); ButtonRefreshCollection.TabIndex = 2; @@ -153,7 +157,7 @@ // // ButtonDeleteShip // - ButtonDeleteShip.Location = new Point(45, 369); + ButtonDeleteShip.Location = new Point(45, 410); ButtonDeleteShip.Name = "ButtonDeleteShip"; ButtonDeleteShip.Size = new Size(142, 37); ButtonDeleteShip.TabIndex = 2; @@ -163,14 +167,14 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(68, 340); + maskedTextBoxNumber.Location = new Point(68, 381); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(100, 23); maskedTextBoxNumber.TabIndex = 2; // // ButtonAddShip // - ButtonAddShip.Location = new Point(45, 274); + ButtonAddShip.Location = new Point(45, 339); ButtonAddShip.Name = "ButtonAddShip"; ButtonAddShip.Size = new Size(142, 36); ButtonAddShip.TabIndex = 2; @@ -218,6 +222,26 @@ // saveFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(45, 306); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(142, 27); + ButtonSortByColor.TabIndex = 4; + ButtonSortByColor.Text = "Сортировать по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(45, 273); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(142, 27); + ButtonSortByType.TabIndex = 5; + ButtonSortByType.Text = "Сортировать по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // FormShipCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -266,5 +290,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/WarmlyShip/FormShipCollection.cs b/WarmlyShip/FormShipCollection.cs index 32f4758..ec356cb 100644 --- a/WarmlyShip/FormShipCollection.cs +++ b/WarmlyShip/FormShipCollection.cs @@ -46,7 +46,7 @@ namespace WarmlyShip ListBoxObjects.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - ListBoxObjects.Items.Add(_storage.Keys[i]); + ListBoxObjects.Items.Add(_storage.Keys[i].Name); } if (ListBoxObjects.Items.Count > 0 && (index == -1 || index >= ListBoxObjects.Items.Count)) @@ -137,10 +137,10 @@ namespace WarmlyShip pictureBoxCollection.Image = obj.ShowShips(); Log.Information($"Добавлен объект в коллекцию {ListBoxObjects.SelectedItem.ToString() ?? string.Empty}"); } - catch (StorageOverflowException ex) + catch (ArgumentException ex) { - Log.Warning($"Коллекция {ListBoxObjects.SelectedItem.ToString() ?? string.Empty} переполнена"); - MessageBox.Show(ex.Message); + Log.Warning($"Добавляемый объект уже существует в коллекции {ListBoxObjects.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); } }); form.AddEvent(shipDelegate); @@ -258,5 +258,22 @@ namespace WarmlyShip } } } + + 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 (ListBoxObjects.SelectedIndex == -1) + { + return; + } + var obj = _storage[ListBoxObjects.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowShips(); + } } } diff --git a/WarmlyShip/Generics/DrawiningShipEqutables.cs b/WarmlyShip/Generics/DrawiningShipEqutables.cs new file mode 100644 index 0000000..9ee6ff9 --- /dev/null +++ b/WarmlyShip/Generics/DrawiningShipEqutables.cs @@ -0,0 +1,66 @@ +using WarmlyShip.DrawningObjects; +using WarmlyShip.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip.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 EntityX = (EntityWarmlyShip)x.EntityShip; + EntityWarmlyShip EntityY = (EntityWarmlyShip)y.EntityShip; + if (EntityX.Pipe != EntityY.Pipe) + { + return false; + } + if (EntityX.FuelCompartment != EntityY.FuelCompartment) + { + return false; + } + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningShip? obj) + { + return obj.GetHashCode(); + } + } +} + diff --git a/WarmlyShip/Generics/SetGeneric.cs b/WarmlyShip/Generics/SetGeneric.cs index 8cab7bb..a0988e3 100644 --- a/WarmlyShip/Generics/SetGeneric.cs +++ b/WarmlyShip/Generics/SetGeneric.cs @@ -39,19 +39,24 @@ namespace WarmlyShip.Generics _places = new List(count); } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); + /// /// Добавление объекта в набор /// /// Добавляемый корабль /// - public void Insert(T ship) + public void Insert(T ship, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) { throw new StorageOverflowException(_maxCount); } - - Insert(ship, 0); + Insert(ship, 0, equal); } /// @@ -60,7 +65,7 @@ namespace WarmlyShip.Generics /// Добавляемый корабль /// Позиция /// - public void Insert(T ship, int position) + public void Insert(T ship, int position, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) { @@ -70,7 +75,13 @@ namespace WarmlyShip.Generics { throw new Exception("Неверная позиция для вставки"); } - + if (equal != null) + { + if (_places.Contains(ship, equal)) + { + throw new ArgumentException(nameof(ship)); + } + } _places.Insert(position, ship); } diff --git a/WarmlyShip/Generics/ShipCompareByColor.cs b/WarmlyShip/Generics/ShipCompareByColor.cs new file mode 100644 index 0000000..39529d5 --- /dev/null +++ b/WarmlyShip/Generics/ShipCompareByColor.cs @@ -0,0 +1,39 @@ +using WarmlyShip.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip.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); + } + + 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/WarmlyShip/Generics/ShipCompareByType.cs b/WarmlyShip/Generics/ShipCompareByType.cs new file mode 100644 index 0000000..6cac4c3 --- /dev/null +++ b/WarmlyShip/Generics/ShipCompareByType.cs @@ -0,0 +1,36 @@ +using WarmlyShip.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip.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/WarmlyShip/Generics/ShipsCollectionInfo.cs b/WarmlyShip/Generics/ShipsCollectionInfo.cs new file mode 100644 index 0000000..4d1359a --- /dev/null +++ b/WarmlyShip/Generics/ShipsCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip.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) + { + if (Name == other?.Name) + return true; + return false; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} + diff --git a/WarmlyShip/Generics/ShipsGenericCollection.cs b/WarmlyShip/Generics/ShipsGenericCollection.cs index cf2e2e2..06ed67f 100644 --- a/WarmlyShip/Generics/ShipsGenericCollection.cs +++ b/WarmlyShip/Generics/ShipsGenericCollection.cs @@ -23,6 +23,12 @@ namespace WarmlyShip.Generics /// public IEnumerable GetShips => _collection.GetShips(); + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Ширина окна прорисовки /// @@ -74,7 +80,7 @@ namespace WarmlyShip.Generics { return false; } - collect?._collection.Insert(obj); + collect?._collection.Insert(obj, new DrawiningShipEqutables()); return true; } diff --git a/WarmlyShip/Generics/ShipsGenericStorage.cs b/WarmlyShip/Generics/ShipsGenericStorage.cs index 07ce10d..850e1d9 100644 --- a/WarmlyShip/Generics/ShipsGenericStorage.cs +++ b/WarmlyShip/Generics/ShipsGenericStorage.cs @@ -16,13 +16,13 @@ namespace WarmlyShip.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _shipStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _shipStorages.Keys.ToList(); + public List Keys => _shipStorages.Keys.ToList(); /// /// Ширина окна отрисовки @@ -54,7 +54,7 @@ namespace WarmlyShip.Generics /// public ShipsGenericStorage(int pictureWidth, int pictureHeight) { - _shipStorages = new Dictionary>(); + _shipStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -71,14 +71,14 @@ namespace WarmlyShip.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _shipStorages) + foreach (KeyValuePair> record in _shipStorages) { StringBuilder records = new(); foreach (DrawningShip? elem in record.Value.GetShips) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) @@ -143,7 +143,7 @@ namespace WarmlyShip.Generics } } } - _shipStorages.Add(record[0], collection); + _shipStorages.Add(new ShipsCollectionInfo(record[0], string.Empty), collection); str = sr.ReadLine(); } while (str != null); @@ -156,7 +156,7 @@ namespace WarmlyShip.Generics /// Название набора public void AddSet(string name) { - _shipStorages.Add(name, new ShipsGenericCollection(_pictureWidth, _pictureHeight)); + _shipStorages.Add(new ShipsCollectionInfo(name, string.Empty), new ShipsGenericCollection(_pictureWidth, _pictureHeight)); } /// @@ -165,12 +165,12 @@ namespace WarmlyShip.Generics /// Название набора public void DelSet(string name) { - if (!_shipStorages.ContainsKey(name)) + if (!_shipStorages.ContainsKey(new ShipsCollectionInfo(name, string.Empty))) { return; } - _shipStorages.Remove(name); + _shipStorages.Remove(new ShipsCollectionInfo(name, string.Empty)); } /// @@ -182,9 +182,10 @@ namespace WarmlyShip.Generics { get { - if (_shipStorages.ContainsKey(ind)) + ShipsCollectionInfo indObj = new ShipsCollectionInfo(ind, string.Empty); + if (_shipStorages.ContainsKey(indObj)) { - return _shipStorages[ind]; + return _shipStorages[indObj]; } return null; }