From 8e06837ec5c1f6f6e998aa559fe08d50c5853c72 Mon Sep 17 00:00:00 2001 From: russell Date: Fri, 15 Dec 2023 22:21:46 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=208=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DumpTruck/FormTruckCollection.Designer.cs | 119 +++++++++++------- DumpTruck/FormTruckCollection.cs | 41 +++++- DumpTruck/Generics/DrawingTruckEqutables.cs | 65 ++++++++++ DumpTruck/Generics/SetGeneric.cs | 24 ++-- DumpTruck/Generics/TruckCompareByColor.cs | 49 ++++++++ DumpTruck/Generics/TruckCompareByType.cs | 35 ++++++ DumpTruck/Generics/TrucksCollectionInfo.cs | 31 +++++ DumpTruck/Generics/TrucksGenericCollection.cs | 8 +- DumpTruck/Generics/TrucksGenericStorage.cs | 34 ++--- 9 files changed, 334 insertions(+), 72 deletions(-) create mode 100644 DumpTruck/Generics/DrawingTruckEqutables.cs create mode 100644 DumpTruck/Generics/TruckCompareByColor.cs create mode 100644 DumpTruck/Generics/TruckCompareByType.cs create mode 100644 DumpTruck/Generics/TrucksCollectionInfo.cs diff --git a/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/FormTruckCollection.Designer.cs index 557082d..3d3112a 100644 --- a/DumpTruck/FormTruckCollection.Designer.cs +++ b/DumpTruck/FormTruckCollection.Designer.cs @@ -32,10 +32,6 @@ this.panelStorages = new System.Windows.Forms.Panel(); this.labelStorages = new System.Windows.Forms.Label(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); - this.menuStripFile = new System.Windows.Forms.MenuStrip(); - this.toolStripMenuItemFile = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem(); this.buttonDelObject = new System.Windows.Forms.Button(); this.buttonAddObject = new System.Windows.Forms.Button(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -44,9 +40,15 @@ this.labelTools = new System.Windows.Forms.Label(); this.buttonRemoveTruck = new System.Windows.Forms.Button(); this.buttonAddTruck = new System.Windows.Forms.Button(); + this.menuStripFile = new System.Windows.Forms.MenuStrip(); + this.toolStripMenuItemFile = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); this.panelTools.SuspendLayout(); this.panelStorages.SuspendLayout(); this.menuStripFile.SuspendLayout(); @@ -57,6 +59,8 @@ // this.panelTools.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.panelTools.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.panelTools.Controls.Add(this.buttonSortByColor); + this.panelTools.Controls.Add(this.buttonSortByType); this.panelTools.Controls.Add(this.panelStorages); this.panelTools.Controls.Add(this.maskedTextBoxNumber); this.panelTools.Controls.Add(this.buttonRefreshCollection); @@ -72,13 +76,12 @@ // this.panelStorages.Controls.Add(this.labelStorages); this.panelStorages.Controls.Add(this.textBoxStorageName); - this.panelStorages.Controls.Add(this.menuStripFile); this.panelStorages.Controls.Add(this.buttonDelObject); this.panelStorages.Controls.Add(this.buttonAddObject); this.panelStorages.Controls.Add(this.listBoxStorages); this.panelStorages.Location = new System.Drawing.Point(3, 18); this.panelStorages.Name = "panelStorages"; - this.panelStorages.Size = new System.Drawing.Size(200, 246); + this.panelStorages.Size = new System.Drawing.Size(200, 215); this.panelStorages.TabIndex = 5; // // labelStorages @@ -97,40 +100,6 @@ this.textBoxStorageName.Size = new System.Drawing.Size(150, 23); this.textBoxStorageName.TabIndex = 3; // - // menuStripFile - // - this.menuStripFile.Dock = System.Windows.Forms.DockStyle.None; - this.menuStripFile.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripMenuItemFile}); - this.menuStripFile.Location = new System.Drawing.Point(25, 222); - this.menuStripFile.Name = "menuStripFile"; - this.menuStripFile.Size = new System.Drawing.Size(176, 24); - this.menuStripFile.TabIndex = 2; - this.menuStripFile.Text = "menuStripFile"; - // - // toolStripMenuItemFile - // - this.toolStripMenuItemFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripMenuItemSave, - this.toolStripMenuItemLoad}); - this.toolStripMenuItemFile.Name = "toolStripMenuItemFile"; - this.toolStripMenuItemFile.Size = new System.Drawing.Size(48, 20); - this.toolStripMenuItemFile.Text = "Файл"; - // - // toolStripMenuItemSave - // - this.toolStripMenuItemSave.Name = "toolStripMenuItemSave"; - this.toolStripMenuItemSave.Size = new System.Drawing.Size(180, 22); - this.toolStripMenuItemSave.Text = "Сохранить"; - this.toolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); - // - // toolStripMenuItemLoad - // - this.toolStripMenuItemLoad.Name = "toolStripMenuItemLoad"; - this.toolStripMenuItemLoad.Size = new System.Drawing.Size(180, 22); - this.toolStripMenuItemLoad.Text = "Загрузить"; - this.toolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); - // // buttonDelObject // this.buttonDelObject.Location = new System.Drawing.Point(25, 185); @@ -163,14 +132,14 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 306); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 339); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 23); this.maskedTextBoxNumber.TabIndex = 4; // // buttonRefreshCollection // - this.buttonRefreshCollection.Location = new System.Drawing.Point(28, 395); + this.buttonRefreshCollection.Location = new System.Drawing.Point(28, 404); this.buttonRefreshCollection.Name = "buttonRefreshCollection"; this.buttonRefreshCollection.Size = new System.Drawing.Size(150, 30); this.buttonRefreshCollection.TabIndex = 3; @@ -190,7 +159,7 @@ // // buttonRemoveTruck // - this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 335); + this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 368); this.buttonRemoveTruck.Name = "buttonRemoveTruck"; this.buttonRemoveTruck.Size = new System.Drawing.Size(150, 30); this.buttonRemoveTruck.TabIndex = 2; @@ -200,7 +169,7 @@ // // buttonAddTruck // - this.buttonAddTruck.Location = new System.Drawing.Point(28, 270); + this.buttonAddTruck.Location = new System.Drawing.Point(28, 303); this.buttonAddTruck.Name = "buttonAddTruck"; this.buttonAddTruck.Size = new System.Drawing.Size(150, 30); this.buttonAddTruck.TabIndex = 1; @@ -208,11 +177,45 @@ this.buttonAddTruck.UseVisualStyleBackColor = true; this.buttonAddTruck.Click += new System.EventHandler(this.buttonAddTruck_Click); // + // menuStripFile + // + this.menuStripFile.Dock = System.Windows.Forms.DockStyle.None; + this.menuStripFile.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItemFile}); + this.menuStripFile.Location = new System.Drawing.Point(9, 4); + this.menuStripFile.Name = "menuStripFile"; + this.menuStripFile.Size = new System.Drawing.Size(56, 24); + this.menuStripFile.TabIndex = 2; + this.menuStripFile.Text = "menuStripFile"; + // + // toolStripMenuItemFile + // + this.toolStripMenuItemFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItemSave, + this.toolStripMenuItemLoad}); + this.toolStripMenuItemFile.Name = "toolStripMenuItemFile"; + this.toolStripMenuItemFile.Size = new System.Drawing.Size(48, 20); + this.toolStripMenuItemFile.Text = "Файл"; + // + // toolStripMenuItemSave + // + this.toolStripMenuItemSave.Name = "toolStripMenuItemSave"; + this.toolStripMenuItemSave.Size = new System.Drawing.Size(133, 22); + this.toolStripMenuItemSave.Text = "Сохранить"; + this.toolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // toolStripMenuItemLoad + // + this.toolStripMenuItemLoad.Name = "toolStripMenuItemLoad"; + this.toolStripMenuItemLoad.Size = new System.Drawing.Size(133, 22); + this.toolStripMenuItemLoad.Text = "Загрузить"; + this.toolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // // pictureBoxCollection // - this.pictureBoxCollection.Location = new System.Drawing.Point(2, 3); + this.pictureBoxCollection.Location = new System.Drawing.Point(2, 31); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(588, 445); + this.pictureBoxCollection.Size = new System.Drawing.Size(588, 417); this.pictureBoxCollection.TabIndex = 1; this.pictureBoxCollection.TabStop = false; // @@ -225,6 +228,26 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(28, 238); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(150, 25); + this.buttonSortByType.TabIndex = 6; + this.buttonSortByType.Text = "Сортировка по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(28, 269); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(150, 25); + this.buttonSortByColor.TabIndex = 7; + this.buttonSortByColor.Text = "Сортировка по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormTruckCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -232,6 +255,7 @@ this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.panelTools); + this.Controls.Add(this.menuStripFile); this.MainMenuStrip = this.menuStripFile; this.Name = "FormTruckCollection"; this.Text = "FormTruckCollection"; @@ -243,6 +267,7 @@ this.menuStripFile.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -267,5 +292,7 @@ private ToolStripMenuItem toolStripMenuItemLoad; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/DumpTruck/FormTruckCollection.cs b/DumpTruck/FormTruckCollection.cs index 88158a7..65f5f37 100644 --- a/DumpTruck/FormTruckCollection.cs +++ b/DumpTruck/FormTruckCollection.cs @@ -46,7 +46,7 @@ namespace DumpTruck 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)) @@ -138,6 +138,11 @@ namespace DumpTruck MessageBox.Show(ex.Message); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); + } } private void buttonAddTruck_Click(object sender, EventArgs e) @@ -252,5 +257,39 @@ namespace DumpTruck } } } + + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) => + CompareTrucks(new TruckCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => + CompareTrucks(new TruckCompareByColor()); + /// + /// Сортировка по сравнителю + /// + /// + private void CompareTrucks(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.ShowTrucks(); + } } } diff --git a/DumpTruck/Generics/DrawingTruckEqutables.cs b/DumpTruck/Generics/DrawingTruckEqutables.cs new file mode 100644 index 0000000..ebecec7 --- /dev/null +++ b/DumpTruck/Generics/DrawingTruckEqutables.cs @@ -0,0 +1,65 @@ +using DumpTruck.DrawingObjects; +using DumpTruck.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class DrawiningCarEqutables : IEqualityComparer + { + public bool Equals(DrawingTruck? x, DrawingTruck? y) + { + if (x == null || x.EntityTruck == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTruck == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityTruck.Speed != y.EntityTruck.Speed) + { + return false; + } + if (x.EntityTruck.Weight != y.EntityTruck.Weight) + { + return false; + } + if (x.EntityTruck.BodyColor != y.EntityTruck.BodyColor) + { + return false; + } + if (x is DrawingDumpTruck && y is DrawingDumpTruck) + { + EntityDumpTruck xDumpTruck = (EntityDumpTruck)x.EntityTruck; + EntityDumpTruck yDumpTruck = (EntityDumpTruck)y.EntityTruck; + + if (xDumpTruck.DumpBoxColor != yDumpTruck.DumpBoxColor) + return false; + + if (xDumpTruck.TentColor != yDumpTruck.TentColor) + return false; + + if (xDumpTruck.Tent != yDumpTruck.Tent) + return false; + + if (xDumpTruck.DumpBox != yDumpTruck.DumpBox) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingTruck obj) + { + return obj.GetHashCode(); + } + } + +} diff --git a/DumpTruck/Generics/SetGeneric.cs b/DumpTruck/Generics/SetGeneric.cs index 9e828c5..ba5430a 100644 --- a/DumpTruck/Generics/SetGeneric.cs +++ b/DumpTruck/Generics/SetGeneric.cs @@ -26,11 +26,17 @@ namespace DumpTruck.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; - /// - /// Конструктор - /// - /// - public SetGeneric(int count) + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); + + /// + /// Конструктор + /// + /// + public SetGeneric(int count) { _maxCount = count; _places = new List(count); @@ -40,9 +46,9 @@ namespace DumpTruck.Generics /// /// Добавляемый грузовик /// - public int Insert(T truck) + public int Insert(T truck, IEqualityComparer? equal = null) { - return Insert(truck, 0); + return Insert(truck, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -50,12 +56,14 @@ namespace DumpTruck.Generics /// Добавляемый грузовик /// Позиция /// - public int Insert(T truck, int position) + public int Insert(T truck, int position, IEqualityComparer? equal = null) { if (position < 0 || position > Count) throw new TruckNotFoundException(position); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + if (equal != null && _places.Contains(truck, equal)) + throw new ArgumentException("Данный объект уже есть в коллекции"); _places.Insert(position, truck); return position; } diff --git a/DumpTruck/Generics/TruckCompareByColor.cs b/DumpTruck/Generics/TruckCompareByColor.cs new file mode 100644 index 0000000..5c01fc9 --- /dev/null +++ b/DumpTruck/Generics/TruckCompareByColor.cs @@ -0,0 +1,49 @@ +using DumpTruck.DrawingObjects; +using DumpTruck.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class TruckCompareByColor : IComparer + { + public int Compare(DrawingTruck? x, DrawingTruck? y) + { + if (x == null || x.EntityTruck == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTruck == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bodyColorCompare = x.EntityTruck.BodyColor.Name.CompareTo(y.EntityTruck.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityTruck is EntityDumpTruck xEntityDumpTruck && y.EntityTruck is EntityDumpTruck yEntityDumpTruck) + { + var dumpBoxColorCompare = xEntityDumpTruck.DumpBoxColor.Name.CompareTo(yEntityDumpTruck.DumpBoxColor.Name); + if (dumpBoxColorCompare != 0) + { + return dumpBoxColorCompare; + } + var tentColorCompare = xEntityDumpTruck.TentColor.Name.CompareTo(yEntityDumpTruck.TentColor.Name); + if (tentColorCompare != 0) + { + return tentColorCompare; + } + } + var speedCompare = x.EntityTruck.Speed.CompareTo(y.EntityTruck.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityTruck.Weight.CompareTo(y.EntityTruck.Weight); + } + } +} diff --git a/DumpTruck/Generics/TruckCompareByType.cs b/DumpTruck/Generics/TruckCompareByType.cs new file mode 100644 index 0000000..3b30035 --- /dev/null +++ b/DumpTruck/Generics/TruckCompareByType.cs @@ -0,0 +1,35 @@ +using DumpTruck.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class TruckCompareByType : IComparer + { + public int Compare(DrawingTruck? x, DrawingTruck? y) + { + if (x == null || x.EntityTruck == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTruck == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityTruck.Speed.CompareTo(y.EntityTruck.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityTruck.Weight.CompareTo(y.EntityTruck.Weight); + } + } + +} diff --git a/DumpTruck/Generics/TrucksCollectionInfo.cs b/DumpTruck/Generics/TrucksCollectionInfo.cs new file mode 100644 index 0000000..f5434a1 --- /dev/null +++ b/DumpTruck/Generics/TrucksCollectionInfo.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class TrucksCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public TrucksCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(TrucksCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } + +} diff --git a/DumpTruck/Generics/TrucksGenericCollection.cs b/DumpTruck/Generics/TrucksGenericCollection.cs index dd49384..122b2d4 100644 --- a/DumpTruck/Generics/TrucksGenericCollection.cs +++ b/DumpTruck/Generics/TrucksGenericCollection.cs @@ -41,6 +41,12 @@ namespace DumpTruck.Generics /// Получение объектов коллекции /// public IEnumerable GetTrucks => _collection.GetTrucks(); + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Конструктор /// @@ -66,7 +72,7 @@ namespace DumpTruck.Generics { return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawiningCarEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/DumpTruck/Generics/TrucksGenericStorage.cs b/DumpTruck/Generics/TrucksGenericStorage.cs index 385402d..18bbde3 100644 --- a/DumpTruck/Generics/TrucksGenericStorage.cs +++ b/DumpTruck/Generics/TrucksGenericStorage.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; using DumpTruck.DrawingObjects; using DumpTruck.MovementStrategy; @@ -28,11 +29,11 @@ namespace DumpTruck.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _truckStorages; + readonly Dictionary> _truckStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _truckStorages.Keys.ToList(); + public List Keys => _truckStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -48,7 +49,7 @@ namespace DumpTruck.Generics /// public TrucksGenericStorage(int pictureWidth, int pictureHeight) { - _truckStorages = new Dictionary>(); + _truckStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -58,7 +59,7 @@ namespace DumpTruck.Generics /// Название набора public void AddSet(string name) { - if (!_truckStorages.ContainsKey(name)) _truckStorages.Add(name, new TrucksGenericCollection(_pictureWidth, _pictureHeight)); + if (!_truckStorages.ContainsKey(new TrucksCollectionInfo(name, string.Empty))) _truckStorages.Add(new TrucksCollectionInfo(name, string.Empty), new TrucksGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -66,7 +67,7 @@ namespace DumpTruck.Generics /// Название набора public void DelSet(string name) { - if (_truckStorages.ContainsKey(name)) _truckStorages.Remove(name); + if (_truckStorages.ContainsKey(new TrucksCollectionInfo(name, string.Empty))) _truckStorages.Remove(new TrucksCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -75,10 +76,11 @@ namespace DumpTruck.Generics /// public TrucksGenericCollection? this[string ind] - { - get + { + get { - if (_truckStorages.ContainsKey(ind)) return _truckStorages[ind]; + if (_truckStorages.ContainsKey(new TrucksCollectionInfo(ind, string.Empty))) + return _truckStorages[new TrucksCollectionInfo(ind, string.Empty)]; return null; } } @@ -94,18 +96,18 @@ namespace DumpTruck.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _truckStorages) + foreach (KeyValuePair> record in _truckStorages) { StringBuilder records = new(); foreach (DrawingTruck? elem in record.Value.GetTrucks) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { - throw new InvalidOperationException("Невалиданя операция, нет данных для сохранения"); + throw new Exception("Невалиданя операция, нет данных для сохранения"); } using StreamWriter sw = new(filename); sw.Write($"TruckStorage{Environment.NewLine}{data}"); @@ -119,18 +121,18 @@ namespace DumpTruck.Generics { if (!File.Exists(filename)) { - throw new FileNotFoundException("Файл не найден"); + throw new Exception("Файл не найден"); } using (StreamReader sr = new(filename)) { string str = sr.ReadLine(); if (str == null || str.Length == 0) { - throw new NullReferenceException("Нет данных для загрузки"); + throw new Exception("Нет данных для загрузки"); } if (!str.StartsWith("TruckStorage")) { - throw new InvalidDataException("Неверный формат данных"); + throw new Exception("Неверный формат данных"); } _truckStorages.Clear(); while ((str = sr.ReadLine()) != null) @@ -149,11 +151,11 @@ namespace DumpTruck.Generics { if (collection + truck == -1) { - throw new ApplicationException("Ошибка добавления в коллекцию"); + throw new Exception("Ошибка добавления в коллекцию"); } } } - _truckStorages.Add(record[0], collection); + _truckStorages.Add(new TrucksCollectionInfo(record[0], string.Empty), collection); } } } -- 2.25.1 From fbf00076a085ce144fa210ed5eb1edaae952a55c Mon Sep 17 00:00:00 2001 From: RuslanAnisin Date: Fri, 15 Dec 2023 22:27:39 +0400 Subject: [PATCH 2/3] revert 8e06837ec5c1f6f6e998aa559fe08d50c5853c72 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit revert Готовая 8 лабораторная --- DumpTruck/FormTruckCollection.Designer.cs | 119 +++++++----------- DumpTruck/FormTruckCollection.cs | 41 +----- DumpTruck/Generics/DrawingTruckEqutables.cs | 65 ---------- DumpTruck/Generics/SetGeneric.cs | 24 ++-- DumpTruck/Generics/TruckCompareByColor.cs | 49 -------- DumpTruck/Generics/TruckCompareByType.cs | 35 ------ DumpTruck/Generics/TrucksCollectionInfo.cs | 31 ----- DumpTruck/Generics/TrucksGenericCollection.cs | 8 +- DumpTruck/Generics/TrucksGenericStorage.cs | 34 +++-- 9 files changed, 72 insertions(+), 334 deletions(-) delete mode 100644 DumpTruck/Generics/DrawingTruckEqutables.cs delete mode 100644 DumpTruck/Generics/TruckCompareByColor.cs delete mode 100644 DumpTruck/Generics/TruckCompareByType.cs delete mode 100644 DumpTruck/Generics/TrucksCollectionInfo.cs diff --git a/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/FormTruckCollection.Designer.cs index 3d3112a..021a8ca 100644 --- a/DumpTruck/FormTruckCollection.Designer.cs +++ b/DumpTruck/FormTruckCollection.Designer.cs @@ -32,6 +32,10 @@ this.panelStorages = new System.Windows.Forms.Panel(); this.labelStorages = new System.Windows.Forms.Label(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.menuStripFile = new System.Windows.Forms.MenuStrip(); + this.toolStripMenuItemFile = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem(); this.buttonDelObject = new System.Windows.Forms.Button(); this.buttonAddObject = new System.Windows.Forms.Button(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -40,15 +44,9 @@ this.labelTools = new System.Windows.Forms.Label(); this.buttonRemoveTruck = new System.Windows.Forms.Button(); this.buttonAddTruck = new System.Windows.Forms.Button(); - this.menuStripFile = new System.Windows.Forms.MenuStrip(); - this.toolStripMenuItemFile = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); - this.buttonSortByType = new System.Windows.Forms.Button(); - this.buttonSortByColor = new System.Windows.Forms.Button(); this.panelTools.SuspendLayout(); this.panelStorages.SuspendLayout(); this.menuStripFile.SuspendLayout(); @@ -59,8 +57,6 @@ // this.panelTools.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.panelTools.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.panelTools.Controls.Add(this.buttonSortByColor); - this.panelTools.Controls.Add(this.buttonSortByType); this.panelTools.Controls.Add(this.panelStorages); this.panelTools.Controls.Add(this.maskedTextBoxNumber); this.panelTools.Controls.Add(this.buttonRefreshCollection); @@ -76,12 +72,13 @@ // this.panelStorages.Controls.Add(this.labelStorages); this.panelStorages.Controls.Add(this.textBoxStorageName); + this.panelStorages.Controls.Add(this.menuStripFile); this.panelStorages.Controls.Add(this.buttonDelObject); this.panelStorages.Controls.Add(this.buttonAddObject); this.panelStorages.Controls.Add(this.listBoxStorages); this.panelStorages.Location = new System.Drawing.Point(3, 18); this.panelStorages.Name = "panelStorages"; - this.panelStorages.Size = new System.Drawing.Size(200, 215); + this.panelStorages.Size = new System.Drawing.Size(200, 246); this.panelStorages.TabIndex = 5; // // labelStorages @@ -100,6 +97,40 @@ this.textBoxStorageName.Size = new System.Drawing.Size(150, 23); this.textBoxStorageName.TabIndex = 3; // + // menuStripFile + // + this.menuStripFile.Dock = System.Windows.Forms.DockStyle.None; + this.menuStripFile.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItemFile}); + this.menuStripFile.Location = new System.Drawing.Point(25, 222); + this.menuStripFile.Name = "menuStripFile"; + this.menuStripFile.Size = new System.Drawing.Size(176, 24); + this.menuStripFile.TabIndex = 2; + this.menuStripFile.Text = "menuStripFile"; + // + // toolStripMenuItemFile + // + this.toolStripMenuItemFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItemSave, + this.toolStripMenuItemLoad}); + this.toolStripMenuItemFile.Name = "toolStripMenuItemFile"; + this.toolStripMenuItemFile.Size = new System.Drawing.Size(48, 20); + this.toolStripMenuItemFile.Text = "Файл"; + // + // toolStripMenuItemSave + // + this.toolStripMenuItemSave.Name = "toolStripMenuItemSave"; + this.toolStripMenuItemSave.Size = new System.Drawing.Size(180, 22); + this.toolStripMenuItemSave.Text = "Сохранить"; + this.toolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // toolStripMenuItemLoad + // + this.toolStripMenuItemLoad.Name = "toolStripMenuItemLoad"; + this.toolStripMenuItemLoad.Size = new System.Drawing.Size(180, 22); + this.toolStripMenuItemLoad.Text = "Загрузить"; + this.toolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // // buttonDelObject // this.buttonDelObject.Location = new System.Drawing.Point(25, 185); @@ -132,14 +163,14 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 339); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 306); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 23); this.maskedTextBoxNumber.TabIndex = 4; // // buttonRefreshCollection // - this.buttonRefreshCollection.Location = new System.Drawing.Point(28, 404); + this.buttonRefreshCollection.Location = new System.Drawing.Point(28, 395); this.buttonRefreshCollection.Name = "buttonRefreshCollection"; this.buttonRefreshCollection.Size = new System.Drawing.Size(150, 30); this.buttonRefreshCollection.TabIndex = 3; @@ -159,7 +190,7 @@ // // buttonRemoveTruck // - this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 368); + this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 335); this.buttonRemoveTruck.Name = "buttonRemoveTruck"; this.buttonRemoveTruck.Size = new System.Drawing.Size(150, 30); this.buttonRemoveTruck.TabIndex = 2; @@ -169,7 +200,7 @@ // // buttonAddTruck // - this.buttonAddTruck.Location = new System.Drawing.Point(28, 303); + this.buttonAddTruck.Location = new System.Drawing.Point(28, 270); this.buttonAddTruck.Name = "buttonAddTruck"; this.buttonAddTruck.Size = new System.Drawing.Size(150, 30); this.buttonAddTruck.TabIndex = 1; @@ -177,45 +208,11 @@ this.buttonAddTruck.UseVisualStyleBackColor = true; this.buttonAddTruck.Click += new System.EventHandler(this.buttonAddTruck_Click); // - // menuStripFile - // - this.menuStripFile.Dock = System.Windows.Forms.DockStyle.None; - this.menuStripFile.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripMenuItemFile}); - this.menuStripFile.Location = new System.Drawing.Point(9, 4); - this.menuStripFile.Name = "menuStripFile"; - this.menuStripFile.Size = new System.Drawing.Size(56, 24); - this.menuStripFile.TabIndex = 2; - this.menuStripFile.Text = "menuStripFile"; - // - // toolStripMenuItemFile - // - this.toolStripMenuItemFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripMenuItemSave, - this.toolStripMenuItemLoad}); - this.toolStripMenuItemFile.Name = "toolStripMenuItemFile"; - this.toolStripMenuItemFile.Size = new System.Drawing.Size(48, 20); - this.toolStripMenuItemFile.Text = "Файл"; - // - // toolStripMenuItemSave - // - this.toolStripMenuItemSave.Name = "toolStripMenuItemSave"; - this.toolStripMenuItemSave.Size = new System.Drawing.Size(133, 22); - this.toolStripMenuItemSave.Text = "Сохранить"; - this.toolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); - // - // toolStripMenuItemLoad - // - this.toolStripMenuItemLoad.Name = "toolStripMenuItemLoad"; - this.toolStripMenuItemLoad.Size = new System.Drawing.Size(133, 22); - this.toolStripMenuItemLoad.Text = "Загрузить"; - this.toolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); - // // pictureBoxCollection // - this.pictureBoxCollection.Location = new System.Drawing.Point(2, 31); + this.pictureBoxCollection.Location = new System.Drawing.Point(2, 3); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(588, 417); + this.pictureBoxCollection.Size = new System.Drawing.Size(588, 445); this.pictureBoxCollection.TabIndex = 1; this.pictureBoxCollection.TabStop = false; // @@ -228,26 +225,6 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // - // buttonSortByType - // - this.buttonSortByType.Location = new System.Drawing.Point(28, 238); - this.buttonSortByType.Name = "buttonSortByType"; - this.buttonSortByType.Size = new System.Drawing.Size(150, 25); - this.buttonSortByType.TabIndex = 6; - this.buttonSortByType.Text = "Сортировка по типу"; - this.buttonSortByType.UseVisualStyleBackColor = true; - this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); - // - // buttonSortByColor - // - this.buttonSortByColor.Location = new System.Drawing.Point(28, 269); - this.buttonSortByColor.Name = "buttonSortByColor"; - this.buttonSortByColor.Size = new System.Drawing.Size(150, 25); - this.buttonSortByColor.TabIndex = 7; - this.buttonSortByColor.Text = "Сортировка по цвету"; - this.buttonSortByColor.UseVisualStyleBackColor = true; - this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); - // // FormTruckCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -255,7 +232,6 @@ this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.panelTools); - this.Controls.Add(this.menuStripFile); this.MainMenuStrip = this.menuStripFile; this.Name = "FormTruckCollection"; this.Text = "FormTruckCollection"; @@ -267,7 +243,6 @@ this.menuStripFile.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); - this.PerformLayout(); } @@ -292,7 +267,5 @@ private ToolStripMenuItem toolStripMenuItemLoad; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; - private Button buttonSortByColor; - private Button buttonSortByType; } } \ No newline at end of file diff --git a/DumpTruck/FormTruckCollection.cs b/DumpTruck/FormTruckCollection.cs index 65f5f37..88158a7 100644 --- a/DumpTruck/FormTruckCollection.cs +++ b/DumpTruck/FormTruckCollection.cs @@ -46,7 +46,7 @@ namespace DumpTruck listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(_storage.Keys[i].Name); + listBoxStorages.Items.Add(_storage.Keys[i]); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) @@ -138,11 +138,6 @@ namespace DumpTruck MessageBox.Show(ex.Message); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } - catch (ArgumentException ex) - { - MessageBox.Show(ex.Message); - _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); - } } private void buttonAddTruck_Click(object sender, EventArgs e) @@ -257,39 +252,5 @@ namespace DumpTruck } } } - - /// - /// Сортировка по типу - /// - /// - /// - private void ButtonSortByType_Click(object sender, EventArgs e) => - CompareTrucks(new TruckCompareByType()); - /// - /// Сортировка по цвету - /// - /// - /// - private void ButtonSortByColor_Click(object sender, EventArgs e) => - CompareTrucks(new TruckCompareByColor()); - /// - /// Сортировка по сравнителю - /// - /// - private void CompareTrucks(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.ShowTrucks(); - } } } diff --git a/DumpTruck/Generics/DrawingTruckEqutables.cs b/DumpTruck/Generics/DrawingTruckEqutables.cs deleted file mode 100644 index ebecec7..0000000 --- a/DumpTruck/Generics/DrawingTruckEqutables.cs +++ /dev/null @@ -1,65 +0,0 @@ -using DumpTruck.DrawingObjects; -using DumpTruck.Entities; -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DumpTruck.Generics -{ - internal class DrawiningCarEqutables : IEqualityComparer - { - public bool Equals(DrawingTruck? x, DrawingTruck? y) - { - if (x == null || x.EntityTruck == null) - { - throw new ArgumentNullException(nameof(x)); - } - if (y == null || y.EntityTruck == null) - { - throw new ArgumentNullException(nameof(y)); - } - if (x.GetType().Name != y.GetType().Name) - { - return false; - } - if (x.EntityTruck.Speed != y.EntityTruck.Speed) - { - return false; - } - if (x.EntityTruck.Weight != y.EntityTruck.Weight) - { - return false; - } - if (x.EntityTruck.BodyColor != y.EntityTruck.BodyColor) - { - return false; - } - if (x is DrawingDumpTruck && y is DrawingDumpTruck) - { - EntityDumpTruck xDumpTruck = (EntityDumpTruck)x.EntityTruck; - EntityDumpTruck yDumpTruck = (EntityDumpTruck)y.EntityTruck; - - if (xDumpTruck.DumpBoxColor != yDumpTruck.DumpBoxColor) - return false; - - if (xDumpTruck.TentColor != yDumpTruck.TentColor) - return false; - - if (xDumpTruck.Tent != yDumpTruck.Tent) - return false; - - if (xDumpTruck.DumpBox != yDumpTruck.DumpBox) - return false; - } - return true; - } - public int GetHashCode([DisallowNull] DrawingTruck obj) - { - return obj.GetHashCode(); - } - } - -} diff --git a/DumpTruck/Generics/SetGeneric.cs b/DumpTruck/Generics/SetGeneric.cs index ba5430a..9e828c5 100644 --- a/DumpTruck/Generics/SetGeneric.cs +++ b/DumpTruck/Generics/SetGeneric.cs @@ -26,17 +26,11 @@ namespace DumpTruck.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; - /// - /// Сортировка набора объектов - /// - /// - public void SortSet(IComparer comparer) => _places.Sort(comparer); - - /// - /// Конструктор - /// - /// - public SetGeneric(int count) + /// + /// Конструктор + /// + /// + public SetGeneric(int count) { _maxCount = count; _places = new List(count); @@ -46,9 +40,9 @@ namespace DumpTruck.Generics /// /// Добавляемый грузовик /// - public int Insert(T truck, IEqualityComparer? equal = null) + public int Insert(T truck) { - return Insert(truck, 0, equal); + return Insert(truck, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -56,14 +50,12 @@ namespace DumpTruck.Generics /// Добавляемый грузовик /// Позиция /// - public int Insert(T truck, int position, IEqualityComparer? equal = null) + public int Insert(T truck, int position) { if (position < 0 || position > Count) throw new TruckNotFoundException(position); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - if (equal != null && _places.Contains(truck, equal)) - throw new ArgumentException("Данный объект уже есть в коллекции"); _places.Insert(position, truck); return position; } diff --git a/DumpTruck/Generics/TruckCompareByColor.cs b/DumpTruck/Generics/TruckCompareByColor.cs deleted file mode 100644 index 5c01fc9..0000000 --- a/DumpTruck/Generics/TruckCompareByColor.cs +++ /dev/null @@ -1,49 +0,0 @@ -using DumpTruck.DrawingObjects; -using DumpTruck.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DumpTruck.Generics -{ - internal class TruckCompareByColor : IComparer - { - public int Compare(DrawingTruck? x, DrawingTruck? y) - { - if (x == null || x.EntityTruck == null) - { - throw new ArgumentNullException(nameof(x)); - } - if (y == null || y.EntityTruck == null) - { - throw new ArgumentNullException(nameof(y)); - } - var bodyColorCompare = x.EntityTruck.BodyColor.Name.CompareTo(y.EntityTruck.BodyColor.Name); - if (bodyColorCompare != 0) - { - return bodyColorCompare; - } - if (x.EntityTruck is EntityDumpTruck xEntityDumpTruck && y.EntityTruck is EntityDumpTruck yEntityDumpTruck) - { - var dumpBoxColorCompare = xEntityDumpTruck.DumpBoxColor.Name.CompareTo(yEntityDumpTruck.DumpBoxColor.Name); - if (dumpBoxColorCompare != 0) - { - return dumpBoxColorCompare; - } - var tentColorCompare = xEntityDumpTruck.TentColor.Name.CompareTo(yEntityDumpTruck.TentColor.Name); - if (tentColorCompare != 0) - { - return tentColorCompare; - } - } - var speedCompare = x.EntityTruck.Speed.CompareTo(y.EntityTruck.Speed); - if (speedCompare != 0) - { - return speedCompare; - } - return x.EntityTruck.Weight.CompareTo(y.EntityTruck.Weight); - } - } -} diff --git a/DumpTruck/Generics/TruckCompareByType.cs b/DumpTruck/Generics/TruckCompareByType.cs deleted file mode 100644 index 3b30035..0000000 --- a/DumpTruck/Generics/TruckCompareByType.cs +++ /dev/null @@ -1,35 +0,0 @@ -using DumpTruck.DrawingObjects; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DumpTruck.Generics -{ - internal class TruckCompareByType : IComparer - { - public int Compare(DrawingTruck? x, DrawingTruck? y) - { - if (x == null || x.EntityTruck == null) - { - throw new ArgumentNullException(nameof(x)); - } - if (y == null || y.EntityTruck == null) - { - throw new ArgumentNullException(nameof(y)); - } - if (x.GetType().Name != y.GetType().Name) - { - return x.GetType().Name.CompareTo(y.GetType().Name); - } - var speedCompare = x.EntityTruck.Speed.CompareTo(y.EntityTruck.Speed); - if (speedCompare != 0) - { - return speedCompare; - } - return x.EntityTruck.Weight.CompareTo(y.EntityTruck.Weight); - } - } - -} diff --git a/DumpTruck/Generics/TrucksCollectionInfo.cs b/DumpTruck/Generics/TrucksCollectionInfo.cs deleted file mode 100644 index f5434a1..0000000 --- a/DumpTruck/Generics/TrucksCollectionInfo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DumpTruck.Generics -{ - internal class TrucksCollectionInfo : IEquatable - { - public string Name { get; private set; } - public string Description { get; private set; } - public TrucksCollectionInfo(string name, string description) - { - Name = name; - Description = description; - } - public bool Equals(TrucksCollectionInfo? other) - { - if (Name == other?.Name) - return true; - - return false; - } - public override int GetHashCode() - { - return Name.GetHashCode(); - } - } - -} diff --git a/DumpTruck/Generics/TrucksGenericCollection.cs b/DumpTruck/Generics/TrucksGenericCollection.cs index 122b2d4..dd49384 100644 --- a/DumpTruck/Generics/TrucksGenericCollection.cs +++ b/DumpTruck/Generics/TrucksGenericCollection.cs @@ -41,12 +41,6 @@ namespace DumpTruck.Generics /// Получение объектов коллекции /// public IEnumerable GetTrucks => _collection.GetTrucks(); - /// - /// Сортировка - /// - /// - public void Sort(IComparer comparer) => _collection.SortSet(comparer); - /// /// Конструктор /// @@ -72,7 +66,7 @@ namespace DumpTruck.Generics { return -1; } - return collect._collection.Insert(obj, new DrawiningCarEqutables()); + return collect._collection.Insert(obj); } /// /// Перегрузка оператора вычитания diff --git a/DumpTruck/Generics/TrucksGenericStorage.cs b/DumpTruck/Generics/TrucksGenericStorage.cs index 18bbde3..385402d 100644 --- a/DumpTruck/Generics/TrucksGenericStorage.cs +++ b/DumpTruck/Generics/TrucksGenericStorage.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using System.Xml.Linq; using DumpTruck.DrawingObjects; using DumpTruck.MovementStrategy; @@ -29,11 +28,11 @@ namespace DumpTruck.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _truckStorages; + readonly Dictionary> _truckStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _truckStorages.Keys.ToList(); + public List Keys => _truckStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -49,7 +48,7 @@ namespace DumpTruck.Generics /// public TrucksGenericStorage(int pictureWidth, int pictureHeight) { - _truckStorages = new Dictionary>(); + _truckStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -59,7 +58,7 @@ namespace DumpTruck.Generics /// Название набора public void AddSet(string name) { - if (!_truckStorages.ContainsKey(new TrucksCollectionInfo(name, string.Empty))) _truckStorages.Add(new TrucksCollectionInfo(name, string.Empty), new TrucksGenericCollection(_pictureWidth, _pictureHeight)); + if (!_truckStorages.ContainsKey(name)) _truckStorages.Add(name, new TrucksGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -67,7 +66,7 @@ namespace DumpTruck.Generics /// Название набора public void DelSet(string name) { - if (_truckStorages.ContainsKey(new TrucksCollectionInfo(name, string.Empty))) _truckStorages.Remove(new TrucksCollectionInfo(name, string.Empty)); + if (_truckStorages.ContainsKey(name)) _truckStorages.Remove(name); } /// /// Доступ к набору @@ -76,11 +75,10 @@ namespace DumpTruck.Generics /// public TrucksGenericCollection? this[string ind] - { - get + { + get { - if (_truckStorages.ContainsKey(new TrucksCollectionInfo(ind, string.Empty))) - return _truckStorages[new TrucksCollectionInfo(ind, string.Empty)]; + if (_truckStorages.ContainsKey(ind)) return _truckStorages[ind]; return null; } } @@ -96,18 +94,18 @@ namespace DumpTruck.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _truckStorages) + foreach (KeyValuePair> record in _truckStorages) { StringBuilder records = new(); foreach (DrawingTruck? elem in record.Value.GetTrucks) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { - throw new Exception("Невалиданя операция, нет данных для сохранения"); + throw new InvalidOperationException("Невалиданя операция, нет данных для сохранения"); } using StreamWriter sw = new(filename); sw.Write($"TruckStorage{Environment.NewLine}{data}"); @@ -121,18 +119,18 @@ namespace DumpTruck.Generics { if (!File.Exists(filename)) { - throw new Exception("Файл не найден"); + throw new FileNotFoundException("Файл не найден"); } using (StreamReader sr = new(filename)) { string str = sr.ReadLine(); if (str == null || str.Length == 0) { - throw new Exception("Нет данных для загрузки"); + throw new NullReferenceException("Нет данных для загрузки"); } if (!str.StartsWith("TruckStorage")) { - throw new Exception("Неверный формат данных"); + throw new InvalidDataException("Неверный формат данных"); } _truckStorages.Clear(); while ((str = sr.ReadLine()) != null) @@ -151,11 +149,11 @@ namespace DumpTruck.Generics { if (collection + truck == -1) { - throw new Exception("Ошибка добавления в коллекцию"); + throw new ApplicationException("Ошибка добавления в коллекцию"); } } } - _truckStorages.Add(new TrucksCollectionInfo(record[0], string.Empty), collection); + _truckStorages.Add(record[0], collection); } } } -- 2.25.1 From 4db8b7f4d3e2f9e098b7fe8949e0d5cdafab3664 Mon Sep 17 00:00:00 2001 From: russell Date: Fri, 15 Dec 2023 22:40:13 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=208=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DumpTruck/FormTruckCollection.Designer.cs | 119 +++++++++++------- DumpTruck/FormTruckCollection.cs | 38 +++++- DumpTruck/Generics/DrawingTruckEqutables.cs | 65 ++++++++++ DumpTruck/Generics/SetGeneric.cs | 24 ++-- DumpTruck/Generics/TruckCompareByColor.cs | 49 ++++++++ DumpTruck/Generics/TruckCompareByType.cs | 35 ++++++ DumpTruck/Generics/TrucksCollectionInfo.cs | 31 +++++ DumpTruck/Generics/TrucksGenericCollection.cs | 8 +- DumpTruck/Generics/TrucksGenericStorage.cs | 23 ++-- 9 files changed, 325 insertions(+), 67 deletions(-) create mode 100644 DumpTruck/Generics/DrawingTruckEqutables.cs create mode 100644 DumpTruck/Generics/TruckCompareByColor.cs create mode 100644 DumpTruck/Generics/TruckCompareByType.cs create mode 100644 DumpTruck/Generics/TrucksCollectionInfo.cs diff --git a/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/FormTruckCollection.Designer.cs index 021a8ca..3d3112a 100644 --- a/DumpTruck/FormTruckCollection.Designer.cs +++ b/DumpTruck/FormTruckCollection.Designer.cs @@ -32,10 +32,6 @@ this.panelStorages = new System.Windows.Forms.Panel(); this.labelStorages = new System.Windows.Forms.Label(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); - this.menuStripFile = new System.Windows.Forms.MenuStrip(); - this.toolStripMenuItemFile = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem(); this.buttonDelObject = new System.Windows.Forms.Button(); this.buttonAddObject = new System.Windows.Forms.Button(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -44,9 +40,15 @@ this.labelTools = new System.Windows.Forms.Label(); this.buttonRemoveTruck = new System.Windows.Forms.Button(); this.buttonAddTruck = new System.Windows.Forms.Button(); + this.menuStripFile = new System.Windows.Forms.MenuStrip(); + this.toolStripMenuItemFile = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemSave = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItemLoad = new System.Windows.Forms.ToolStripMenuItem(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); this.panelTools.SuspendLayout(); this.panelStorages.SuspendLayout(); this.menuStripFile.SuspendLayout(); @@ -57,6 +59,8 @@ // this.panelTools.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.panelTools.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.panelTools.Controls.Add(this.buttonSortByColor); + this.panelTools.Controls.Add(this.buttonSortByType); this.panelTools.Controls.Add(this.panelStorages); this.panelTools.Controls.Add(this.maskedTextBoxNumber); this.panelTools.Controls.Add(this.buttonRefreshCollection); @@ -72,13 +76,12 @@ // this.panelStorages.Controls.Add(this.labelStorages); this.panelStorages.Controls.Add(this.textBoxStorageName); - this.panelStorages.Controls.Add(this.menuStripFile); this.panelStorages.Controls.Add(this.buttonDelObject); this.panelStorages.Controls.Add(this.buttonAddObject); this.panelStorages.Controls.Add(this.listBoxStorages); this.panelStorages.Location = new System.Drawing.Point(3, 18); this.panelStorages.Name = "panelStorages"; - this.panelStorages.Size = new System.Drawing.Size(200, 246); + this.panelStorages.Size = new System.Drawing.Size(200, 215); this.panelStorages.TabIndex = 5; // // labelStorages @@ -97,40 +100,6 @@ this.textBoxStorageName.Size = new System.Drawing.Size(150, 23); this.textBoxStorageName.TabIndex = 3; // - // menuStripFile - // - this.menuStripFile.Dock = System.Windows.Forms.DockStyle.None; - this.menuStripFile.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripMenuItemFile}); - this.menuStripFile.Location = new System.Drawing.Point(25, 222); - this.menuStripFile.Name = "menuStripFile"; - this.menuStripFile.Size = new System.Drawing.Size(176, 24); - this.menuStripFile.TabIndex = 2; - this.menuStripFile.Text = "menuStripFile"; - // - // toolStripMenuItemFile - // - this.toolStripMenuItemFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripMenuItemSave, - this.toolStripMenuItemLoad}); - this.toolStripMenuItemFile.Name = "toolStripMenuItemFile"; - this.toolStripMenuItemFile.Size = new System.Drawing.Size(48, 20); - this.toolStripMenuItemFile.Text = "Файл"; - // - // toolStripMenuItemSave - // - this.toolStripMenuItemSave.Name = "toolStripMenuItemSave"; - this.toolStripMenuItemSave.Size = new System.Drawing.Size(180, 22); - this.toolStripMenuItemSave.Text = "Сохранить"; - this.toolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); - // - // toolStripMenuItemLoad - // - this.toolStripMenuItemLoad.Name = "toolStripMenuItemLoad"; - this.toolStripMenuItemLoad.Size = new System.Drawing.Size(180, 22); - this.toolStripMenuItemLoad.Text = "Загрузить"; - this.toolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); - // // buttonDelObject // this.buttonDelObject.Location = new System.Drawing.Point(25, 185); @@ -163,14 +132,14 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 306); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(28, 339); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 23); this.maskedTextBoxNumber.TabIndex = 4; // // buttonRefreshCollection // - this.buttonRefreshCollection.Location = new System.Drawing.Point(28, 395); + this.buttonRefreshCollection.Location = new System.Drawing.Point(28, 404); this.buttonRefreshCollection.Name = "buttonRefreshCollection"; this.buttonRefreshCollection.Size = new System.Drawing.Size(150, 30); this.buttonRefreshCollection.TabIndex = 3; @@ -190,7 +159,7 @@ // // buttonRemoveTruck // - this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 335); + this.buttonRemoveTruck.Location = new System.Drawing.Point(28, 368); this.buttonRemoveTruck.Name = "buttonRemoveTruck"; this.buttonRemoveTruck.Size = new System.Drawing.Size(150, 30); this.buttonRemoveTruck.TabIndex = 2; @@ -200,7 +169,7 @@ // // buttonAddTruck // - this.buttonAddTruck.Location = new System.Drawing.Point(28, 270); + this.buttonAddTruck.Location = new System.Drawing.Point(28, 303); this.buttonAddTruck.Name = "buttonAddTruck"; this.buttonAddTruck.Size = new System.Drawing.Size(150, 30); this.buttonAddTruck.TabIndex = 1; @@ -208,11 +177,45 @@ this.buttonAddTruck.UseVisualStyleBackColor = true; this.buttonAddTruck.Click += new System.EventHandler(this.buttonAddTruck_Click); // + // menuStripFile + // + this.menuStripFile.Dock = System.Windows.Forms.DockStyle.None; + this.menuStripFile.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItemFile}); + this.menuStripFile.Location = new System.Drawing.Point(9, 4); + this.menuStripFile.Name = "menuStripFile"; + this.menuStripFile.Size = new System.Drawing.Size(56, 24); + this.menuStripFile.TabIndex = 2; + this.menuStripFile.Text = "menuStripFile"; + // + // toolStripMenuItemFile + // + this.toolStripMenuItemFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripMenuItemSave, + this.toolStripMenuItemLoad}); + this.toolStripMenuItemFile.Name = "toolStripMenuItemFile"; + this.toolStripMenuItemFile.Size = new System.Drawing.Size(48, 20); + this.toolStripMenuItemFile.Text = "Файл"; + // + // toolStripMenuItemSave + // + this.toolStripMenuItemSave.Name = "toolStripMenuItemSave"; + this.toolStripMenuItemSave.Size = new System.Drawing.Size(133, 22); + this.toolStripMenuItemSave.Text = "Сохранить"; + this.toolStripMenuItemSave.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + // + // toolStripMenuItemLoad + // + this.toolStripMenuItemLoad.Name = "toolStripMenuItemLoad"; + this.toolStripMenuItemLoad.Size = new System.Drawing.Size(133, 22); + this.toolStripMenuItemLoad.Text = "Загрузить"; + this.toolStripMenuItemLoad.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + // // pictureBoxCollection // - this.pictureBoxCollection.Location = new System.Drawing.Point(2, 3); + this.pictureBoxCollection.Location = new System.Drawing.Point(2, 31); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(588, 445); + this.pictureBoxCollection.Size = new System.Drawing.Size(588, 417); this.pictureBoxCollection.TabIndex = 1; this.pictureBoxCollection.TabStop = false; // @@ -225,6 +228,26 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(28, 238); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(150, 25); + this.buttonSortByType.TabIndex = 6; + this.buttonSortByType.Text = "Сортировка по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(28, 269); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(150, 25); + this.buttonSortByColor.TabIndex = 7; + this.buttonSortByColor.Text = "Сортировка по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormTruckCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -232,6 +255,7 @@ this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.pictureBoxCollection); this.Controls.Add(this.panelTools); + this.Controls.Add(this.menuStripFile); this.MainMenuStrip = this.menuStripFile; this.Name = "FormTruckCollection"; this.Text = "FormTruckCollection"; @@ -243,6 +267,7 @@ this.menuStripFile.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -267,5 +292,7 @@ private ToolStripMenuItem toolStripMenuItemLoad; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/DumpTruck/FormTruckCollection.cs b/DumpTruck/FormTruckCollection.cs index 88158a7..9777e44 100644 --- a/DumpTruck/FormTruckCollection.cs +++ b/DumpTruck/FormTruckCollection.cs @@ -46,7 +46,7 @@ namespace DumpTruck 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)) @@ -138,6 +138,11 @@ namespace DumpTruck MessageBox.Show(ex.Message); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); + } } private void buttonAddTruck_Click(object sender, EventArgs e) @@ -252,5 +257,36 @@ namespace DumpTruck } } } + + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareTrucks(new TruckCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareTrucks(new TruckCompareByColor()); + /// + /// Сортировка по сравнителю + /// + /// + private void CompareTrucks(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.ShowTrucks(); + } } } diff --git a/DumpTruck/Generics/DrawingTruckEqutables.cs b/DumpTruck/Generics/DrawingTruckEqutables.cs new file mode 100644 index 0000000..ebecec7 --- /dev/null +++ b/DumpTruck/Generics/DrawingTruckEqutables.cs @@ -0,0 +1,65 @@ +using DumpTruck.DrawingObjects; +using DumpTruck.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class DrawiningCarEqutables : IEqualityComparer + { + public bool Equals(DrawingTruck? x, DrawingTruck? y) + { + if (x == null || x.EntityTruck == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTruck == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityTruck.Speed != y.EntityTruck.Speed) + { + return false; + } + if (x.EntityTruck.Weight != y.EntityTruck.Weight) + { + return false; + } + if (x.EntityTruck.BodyColor != y.EntityTruck.BodyColor) + { + return false; + } + if (x is DrawingDumpTruck && y is DrawingDumpTruck) + { + EntityDumpTruck xDumpTruck = (EntityDumpTruck)x.EntityTruck; + EntityDumpTruck yDumpTruck = (EntityDumpTruck)y.EntityTruck; + + if (xDumpTruck.DumpBoxColor != yDumpTruck.DumpBoxColor) + return false; + + if (xDumpTruck.TentColor != yDumpTruck.TentColor) + return false; + + if (xDumpTruck.Tent != yDumpTruck.Tent) + return false; + + if (xDumpTruck.DumpBox != yDumpTruck.DumpBox) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingTruck obj) + { + return obj.GetHashCode(); + } + } + +} diff --git a/DumpTruck/Generics/SetGeneric.cs b/DumpTruck/Generics/SetGeneric.cs index 9e828c5..ba5430a 100644 --- a/DumpTruck/Generics/SetGeneric.cs +++ b/DumpTruck/Generics/SetGeneric.cs @@ -26,11 +26,17 @@ namespace DumpTruck.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; - /// - /// Конструктор - /// - /// - public SetGeneric(int count) + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); + + /// + /// Конструктор + /// + /// + public SetGeneric(int count) { _maxCount = count; _places = new List(count); @@ -40,9 +46,9 @@ namespace DumpTruck.Generics /// /// Добавляемый грузовик /// - public int Insert(T truck) + public int Insert(T truck, IEqualityComparer? equal = null) { - return Insert(truck, 0); + return Insert(truck, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -50,12 +56,14 @@ namespace DumpTruck.Generics /// Добавляемый грузовик /// Позиция /// - public int Insert(T truck, int position) + public int Insert(T truck, int position, IEqualityComparer? equal = null) { if (position < 0 || position > Count) throw new TruckNotFoundException(position); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + if (equal != null && _places.Contains(truck, equal)) + throw new ArgumentException("Данный объект уже есть в коллекции"); _places.Insert(position, truck); return position; } diff --git a/DumpTruck/Generics/TruckCompareByColor.cs b/DumpTruck/Generics/TruckCompareByColor.cs new file mode 100644 index 0000000..5c01fc9 --- /dev/null +++ b/DumpTruck/Generics/TruckCompareByColor.cs @@ -0,0 +1,49 @@ +using DumpTruck.DrawingObjects; +using DumpTruck.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class TruckCompareByColor : IComparer + { + public int Compare(DrawingTruck? x, DrawingTruck? y) + { + if (x == null || x.EntityTruck == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTruck == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bodyColorCompare = x.EntityTruck.BodyColor.Name.CompareTo(y.EntityTruck.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityTruck is EntityDumpTruck xEntityDumpTruck && y.EntityTruck is EntityDumpTruck yEntityDumpTruck) + { + var dumpBoxColorCompare = xEntityDumpTruck.DumpBoxColor.Name.CompareTo(yEntityDumpTruck.DumpBoxColor.Name); + if (dumpBoxColorCompare != 0) + { + return dumpBoxColorCompare; + } + var tentColorCompare = xEntityDumpTruck.TentColor.Name.CompareTo(yEntityDumpTruck.TentColor.Name); + if (tentColorCompare != 0) + { + return tentColorCompare; + } + } + var speedCompare = x.EntityTruck.Speed.CompareTo(y.EntityTruck.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityTruck.Weight.CompareTo(y.EntityTruck.Weight); + } + } +} diff --git a/DumpTruck/Generics/TruckCompareByType.cs b/DumpTruck/Generics/TruckCompareByType.cs new file mode 100644 index 0000000..3b30035 --- /dev/null +++ b/DumpTruck/Generics/TruckCompareByType.cs @@ -0,0 +1,35 @@ +using DumpTruck.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class TruckCompareByType : IComparer + { + public int Compare(DrawingTruck? x, DrawingTruck? y) + { + if (x == null || x.EntityTruck == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityTruck == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityTruck.Speed.CompareTo(y.EntityTruck.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityTruck.Weight.CompareTo(y.EntityTruck.Weight); + } + } + +} diff --git a/DumpTruck/Generics/TrucksCollectionInfo.cs b/DumpTruck/Generics/TrucksCollectionInfo.cs new file mode 100644 index 0000000..f5434a1 --- /dev/null +++ b/DumpTruck/Generics/TrucksCollectionInfo.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Generics +{ + internal class TrucksCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public TrucksCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(TrucksCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } + +} diff --git a/DumpTruck/Generics/TrucksGenericCollection.cs b/DumpTruck/Generics/TrucksGenericCollection.cs index dd49384..122b2d4 100644 --- a/DumpTruck/Generics/TrucksGenericCollection.cs +++ b/DumpTruck/Generics/TrucksGenericCollection.cs @@ -41,6 +41,12 @@ namespace DumpTruck.Generics /// Получение объектов коллекции /// public IEnumerable GetTrucks => _collection.GetTrucks(); + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Конструктор /// @@ -66,7 +72,7 @@ namespace DumpTruck.Generics { return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawiningCarEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/DumpTruck/Generics/TrucksGenericStorage.cs b/DumpTruck/Generics/TrucksGenericStorage.cs index 385402d..fcdfbb9 100644 --- a/DumpTruck/Generics/TrucksGenericStorage.cs +++ b/DumpTruck/Generics/TrucksGenericStorage.cs @@ -28,11 +28,11 @@ namespace DumpTruck.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _truckStorages; + readonly Dictionary> _truckStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _truckStorages.Keys.ToList(); + public List Keys => _truckStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -48,7 +48,7 @@ namespace DumpTruck.Generics /// public TrucksGenericStorage(int pictureWidth, int pictureHeight) { - _truckStorages = new Dictionary>(); + _truckStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -58,7 +58,7 @@ namespace DumpTruck.Generics /// Название набора public void AddSet(string name) { - if (!_truckStorages.ContainsKey(name)) _truckStorages.Add(name, new TrucksGenericCollection(_pictureWidth, _pictureHeight)); + if (!_truckStorages.ContainsKey(new TrucksCollectionInfo(name, string.Empty))) _truckStorages.Add(new TrucksCollectionInfo(name, string.Empty), new TrucksGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -66,7 +66,7 @@ namespace DumpTruck.Generics /// Название набора public void DelSet(string name) { - if (_truckStorages.ContainsKey(name)) _truckStorages.Remove(name); + if (_truckStorages.ContainsKey(new TrucksCollectionInfo(name, string.Empty))) _truckStorages.Remove(new TrucksCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -75,10 +75,11 @@ namespace DumpTruck.Generics /// public TrucksGenericCollection? this[string ind] - { - get + { + get { - if (_truckStorages.ContainsKey(ind)) return _truckStorages[ind]; + if (_truckStorages.ContainsKey(new TrucksCollectionInfo(ind, string.Empty))) + return _truckStorages[new TrucksCollectionInfo(ind, string.Empty)]; return null; } } @@ -94,14 +95,14 @@ namespace DumpTruck.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _truckStorages) + foreach (KeyValuePair> record in _truckStorages) { StringBuilder records = new(); foreach (DrawingTruck? elem in record.Value.GetTrucks) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -153,7 +154,7 @@ namespace DumpTruck.Generics } } } - _truckStorages.Add(record[0], collection); + _truckStorages.Add(new TrucksCollectionInfo(record[0], string.Empty), collection); } } } -- 2.25.1