From 2655e17c9b4afe6f29ee4bf380b3784fd5467d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Wed, 27 Dec 2023 22:05:44 +0400 Subject: [PATCH] done --- .../FormBusCollection.Designer.cs | 42 ++++++++--- .../FormBusCollection.cs | 25 +++++-- .../Generics/BusCompareByColor.cs | 38 ++++++++++ .../Generics/BusCompareByType.cs | 34 +++++++++ .../Generics/BusesCollectionInfo.cs | 29 ++++++++ .../Generics/DrawiningBusEqutables.cs | 69 +++++++++++++++++++ .../Generics/TheBusesGenericCollection.cs | 8 ++- .../Generics/TheBusesGenericStorage.cs | 25 +++---- 8 files changed, 245 insertions(+), 25 deletions(-) create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusCompareByColor.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusCompareByType.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusesCollectionInfo.cs create mode 100644 PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/DrawiningBusEqutables.cs diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs index ae59e16..9c24793 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.Designer.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.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(); + ButtonSortByType = new Button(); + ButtonSortByColor = new Button(); toolsPanel.SuspendLayout(); panelSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); @@ -55,6 +57,8 @@ // // toolsPanel // + toolsPanel.Controls.Add(ButtonSortByColor); + toolsPanel.Controls.Add(ButtonSortByType); toolsPanel.Controls.Add(panelSets); toolsPanel.Controls.Add(ButtonRefreshCollection); toolsPanel.Controls.Add(ButtonDeleteBus); @@ -136,7 +140,7 @@ // // ButtonDeleteBus // - ButtonDeleteBus.Location = new Point(8, 379); + ButtonDeleteBus.Location = new Point(13, 406); ButtonDeleteBus.Name = "ButtonDeleteBus"; ButtonDeleteBus.Size = new Size(197, 41); ButtonDeleteBus.TabIndex = 3; @@ -146,14 +150,14 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(48, 346); + maskedTextBoxNumber.Location = new Point(53, 373); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(125, 27); maskedTextBoxNumber.TabIndex = 2; // // ButtonAddBus // - ButtonAddBus.Location = new Point(8, 284); + ButtonAddBus.Location = new Point(13, 311); ButtonAddBus.Name = "ButtonAddBus"; ButtonAddBus.Size = new Size(197, 41); ButtonAddBus.TabIndex = 1; @@ -178,7 +182,7 @@ pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // - // menuStrip1 + // menuStrip // menuStrip.ImageScalingSize = new Size(20, 20); menuStrip.Items.AddRange(new ToolStripItem[] { FileToolStripMenuItem }); @@ -198,14 +202,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; // @@ -218,6 +222,26 @@ // saveFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(18, 234); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(187, 32); + ButtonSortByType.TabIndex = 7; + ButtonSortByType.Text = "Сортировать по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(18, 272); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(187, 32); + ButtonSortByColor.TabIndex = 8; + ButtonSortByColor.Text = "Сортировать по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // // FormBusCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -266,5 +290,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/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs index d3fc657..be1ba81 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/FormBusCollection.cs @@ -46,7 +46,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base 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 PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base pictureBoxCollection.Image = obj.ShowTheBuses(); 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(busDelegate); @@ -258,5 +258,22 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBuses(new BusCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBuses(new BusCompareByColor()); + private void CompareBuses(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.ShowTheBuses(); + } } } diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusCompareByColor.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusCompareByColor.cs new file mode 100644 index 0000000..70c375a --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusCompareByColor.cs @@ -0,0 +1,38 @@ +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics +{ + internal class BusCompareByColor : IComparer + { + public int Compare(DrawningBus? x, DrawningBus? y) + { + if (x == null || x.EntityBus == null) + { + throw new ArgumentNullException(nameof(x)); + } + + if (y == null || y.EntityBus == null) + { + throw new ArgumentNullException(nameof(y)); + } + + if (x.EntityBus.BodyColor.Name != y.EntityBus.BodyColor.Name) + { + return x.EntityBus.BodyColor.Name.CompareTo(y.EntityBus.BodyColor.Name); + } + + var speedCompare = x.EntityBus.Speed.CompareTo(y.EntityBus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + + return x.EntityBus.Weight.CompareTo(y.EntityBus.Weight); + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusCompareByType.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusCompareByType.cs new file mode 100644 index 0000000..ee5601e --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusCompareByType.cs @@ -0,0 +1,34 @@ +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics +{ + internal class BusCompareByType : IComparer + { + public int Compare(DrawningBus? x, DrawningBus? y) + { + if (x == null || x.EntityBus == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBus == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityBus.Speed.CompareTo(y.EntityBus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBus.Weight.CompareTo(y.EntityBus.Weight); + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusesCollectionInfo.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusesCollectionInfo.cs new file mode 100644 index 0000000..c5dc3e7 --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/BusesCollectionInfo.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics +{ + internal class BusesCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BusesCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BusesCollectionInfo? other) + { + if (Name == other?.Name) + return true; + return false; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/DrawiningBusEqutables.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/DrawiningBusEqutables.cs new file mode 100644 index 0000000..7de149f --- /dev/null +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/DrawiningBusEqutables.cs @@ -0,0 +1,69 @@ +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.DrawningObjects; +using PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics +{ + internal class DrawiningBusEqutables : IEqualityComparer + { + public bool Equals(DrawningBus? x, DrawningBus? y) + { + if (x == null || x.EntityBus == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBus == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityBus.Speed != y.EntityBus.Speed) + { + return false; + } + if (x.EntityBus.Weight != y.EntityBus.Weight) + { + return false; + } + if (x.EntityBus.BodyColor != y.EntityBus.BodyColor) + { + return false; + } + if (x is DrawningDoubleDeckerBus && y is DrawningDoubleDeckerBus) + { + EntityDoubleDeckerBus EntityX = (EntityDoubleDeckerBus)x.EntityBus; + EntityDoubleDeckerBus EntityY = (EntityDoubleDeckerBus)y.EntityBus; + if (EntityX.Ladder != EntityY.Ladder) + { + return false; + } + if (EntityX.LineBetweenFloor != EntityY.LineBetweenFloor) + { + return false; + } + if (EntityX.SecondFloor != EntityY.SecondFloor) + { + return false; + } + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningBus? obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs index 1588bf8..bf0891d 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericCollection.cs @@ -23,6 +23,12 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// public IEnumerable GetTheBuses => _collection.GetTheBuses(); + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Ширина окна прорисовки /// @@ -75,7 +81,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics return false; } - collect?._collection.Insert(obj); + collect?._collection.Insert(obj, new DrawiningBusEqutables()); return true; } diff --git a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs index 5784951..22426ee 100644 --- a/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs +++ b/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/PIbd-23_Ivanov_V.N._DoubleDeckerBus._Base/Generics/TheBusesGenericStorage.cs @@ -16,13 +16,13 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _busStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _busStorages.Keys.ToList(); + public List Keys => _busStorages.Keys.ToList(); /// /// Ширина окна отрисовки @@ -54,7 +54,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// public TheBusesGenericStorage(int pictureWidth, int pictureHeight) { - _busStorages = new Dictionary>(); + _busStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -71,14 +71,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _busStorages) + foreach (KeyValuePair> record in _busStorages) { StringBuilder records = new(); foreach (DrawningBus? elem in record.Value.GetTheBuses) { 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 PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics } } } - _busStorages.Add(record[0], collection); + _busStorages.Add(new BusesCollectionInfo(record[0], string.Empty), collection); str = sr.ReadLine(); } while (str != null); @@ -156,7 +156,7 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// Название набора public void AddSet(string name) { - _busStorages.Add(name, new TheBusesGenericCollection(_pictureWidth, _pictureHeight)); + _busStorages.Add(new BusesCollectionInfo(name, string.Empty), new TheBusesGenericCollection(_pictureWidth, _pictureHeight)); } /// @@ -165,12 +165,12 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics /// Название набора public void DelSet(string name) { - if (!_busStorages.ContainsKey(name)) + if (!_busStorages.ContainsKey(new BusesCollectionInfo(name, string.Empty))) { return; } - _busStorages.Remove(name); + _busStorages.Remove(new BusesCollectionInfo(name, string.Empty)); } /// @@ -182,13 +182,14 @@ namespace PIbd_23_Ivanov_V.N._DoubleDeckerBus._Base.Generics { get { - if (_busStorages.ContainsKey(ind)) + BusesCollectionInfo indObj = new BusesCollectionInfo(ind, string.Empty); + if (_busStorages.ContainsKey(indObj)) { - return _busStorages[ind]; + return _busStorages[indObj]; } return null; } } } -} +} \ No newline at end of file