From fa98258a039c97e480746812d73a287c2c1f3314 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 2 Mar 2024 01:53:48 +0400 Subject: [PATCH] 99% --- DumpTruck/DumpTruck/DrawingTruckEqutables.cs | 58 +++++++++++++++++++ .../DumpTruck/FormTruckCollection.Designer.cs | 42 +++++++++++--- DumpTruck/DumpTruck/FormTruckCollection.cs | 37 +++++++++++- DumpTruck/DumpTruck/SetGeneric.cs | 9 ++- DumpTruck/DumpTruck/TruckCompareByColor.cs | 44 ++++++++++++++ DumpTruck/DumpTruck/TruckCompareByType.cs | 39 +++++++++++++ DumpTruck/DumpTruck/TruckGenericStorage.cs | 27 +++++---- DumpTruck/DumpTruck/TrucksCollectionInfo.cs | 29 ++++++++++ .../DumpTruck/TrucksGenericCollection.cs | 20 +++++-- 9 files changed, 273 insertions(+), 32 deletions(-) create mode 100644 DumpTruck/DumpTruck/DrawingTruckEqutables.cs create mode 100644 DumpTruck/DumpTruck/TruckCompareByColor.cs create mode 100644 DumpTruck/DumpTruck/TruckCompareByType.cs create mode 100644 DumpTruck/DumpTruck/TrucksCollectionInfo.cs diff --git a/DumpTruck/DumpTruck/DrawingTruckEqutables.cs b/DumpTruck/DumpTruck/DrawingTruckEqutables.cs new file mode 100644 index 0000000..d611f08 --- /dev/null +++ b/DumpTruck/DumpTruck/DrawingTruckEqutables.cs @@ -0,0 +1,58 @@ +using DumpTruck.DrawningObjects; +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(DrawningTruck? x, DrawningTruck? 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 DrawningDumpTruck && y is DrawningDumpTruck) + { + EntityDumpTruck xDumpTruck = (EntityDumpTruck)x.EntityTruck; + EntityDumpTruck yDumpTruck = (EntityDumpTruck)y.EntityTruck; + + if (xDumpTruck.Trailer != yDumpTruck.Trailer) + return false; + + if (xDumpTruck.AdditionalColor != yDumpTruck.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningTruck obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs index cbf72e2..9bca902 100644 --- a/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs +++ b/DumpTruck/DumpTruck/FormTruckCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { groupBox1 = new GroupBox(); + buttonSortbyColor = new Button(); + buttonSortbyType = new Button(); groupBox2 = new GroupBox(); textBoxStorageName = new TextBox(); listBoxStorages = new ListBox(); @@ -53,12 +55,13 @@ // // groupBox1 // + groupBox1.Controls.Add(buttonSortbyColor); + groupBox1.Controls.Add(buttonSortbyType); groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(buttonRefreshCollection); groupBox1.Controls.Add(buttonRemoveTruck); groupBox1.Controls.Add(buttonAddTruck); - groupBox1.Controls.Add(menuStrip); groupBox1.Location = new Point(689, 16); groupBox1.Margin = new Padding(3, 4, 3, 4); groupBox1.Name = "groupBox1"; @@ -68,6 +71,26 @@ groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // buttonSortbyColor + // + buttonSortbyColor.Location = new Point(4, 339); + buttonSortbyColor.Name = "buttonSortbyColor"; + buttonSortbyColor.Size = new Size(211, 36); + buttonSortbyColor.TabIndex = 7; + buttonSortbyColor.Text = "Сортировка по цвету"; + buttonSortbyColor.UseVisualStyleBackColor = true; + buttonSortbyColor.Click += ButtonSortByColor_Click; + // + // buttonSortbyType + // + buttonSortbyType.Location = new Point(3, 297); + buttonSortbyType.Name = "buttonSortbyType"; + buttonSortbyType.Size = new Size(211, 36); + buttonSortbyType.TabIndex = 6; + buttonSortbyType.Text = "Сортировка по типу"; + buttonSortbyType.UseVisualStyleBackColor = true; + buttonSortbyType.Click += ButtonSortByType_Click; + // // groupBox2 // groupBox2.Controls.Add(textBoxStorageName); @@ -164,10 +187,10 @@ menuStrip.Dock = DockStyle.None; menuStrip.ImageScalingSize = new Size(20, 20); menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem1 }); - menuStrip.Location = new Point(7, 314); + menuStrip.Location = new Point(11, 9); menuStrip.Name = "menuStrip"; menuStrip.RenderMode = ToolStripRenderMode.Professional; - menuStrip.Size = new Size(223, 47); + menuStrip.Size = new Size(223, 29); menuStrip.TabIndex = 6; menuStrip.Tag = ""; menuStrip.Text = "menuStrip"; @@ -176,29 +199,29 @@ // toolStripMenuItem1.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItemLoad, toolStripMenuItemSave }); toolStripMenuItem1.Name = "toolStripMenuItem1"; - toolStripMenuItem1.Size = new Size(44, 43); + toolStripMenuItem1.Size = new Size(44, 25); toolStripMenuItem1.Text = "file"; // // toolStripMenuItemLoad // toolStripMenuItemLoad.Name = "toolStripMenuItemLoad"; - toolStripMenuItemLoad.Size = new Size(224, 26); + toolStripMenuItemLoad.Size = new Size(125, 26); toolStripMenuItemLoad.Text = "Load"; toolStripMenuItemLoad.Click += LoadStripMenuItem_Click; // // toolStripMenuItemSave // toolStripMenuItemSave.Name = "toolStripMenuItemSave"; - toolStripMenuItemSave.Size = new Size(224, 26); + toolStripMenuItemSave.Size = new Size(125, 26); toolStripMenuItemSave.Text = "Save"; toolStripMenuItemSave.Click += SaveStripMenuItem_Click; // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(11, 16); + pictureBoxCollection.Location = new Point(11, 40); pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(672, 568); + pictureBoxCollection.Size = new Size(672, 544); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // @@ -218,6 +241,7 @@ ClientSize = new Size(935, 611); Controls.Add(pictureBoxCollection); Controls.Add(groupBox1); + Controls.Add(menuStrip); MainMenuStrip = menuStrip; Margin = new Padding(3, 4, 3, 4); Name = "FormTruckCollection"; @@ -251,5 +275,7 @@ private SaveFileDialog saveFileDialog; private ToolStripMenuItem toolStripMenuItemLoad; private ToolStripMenuItem toolStripMenuItemSave; + private Button buttonSortbyType; + private Button buttonSortbyColor; } } \ No newline at end of file diff --git a/DumpTruck/DumpTruck/FormTruckCollection.cs b/DumpTruck/DumpTruck/FormTruckCollection.cs index febfe25..17cfdb0 100644 --- a/DumpTruck/DumpTruck/FormTruckCollection.cs +++ b/DumpTruck/DumpTruck/FormTruckCollection.cs @@ -40,7 +40,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)) @@ -159,6 +159,11 @@ namespace DumpTruck System.Windows.Forms.MessageBox.Show(ex.Message); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + System.Windows.Forms.MessageBox.Show(ex.Message); + _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); + } } private void buttonAddTruck_Click(object sender, EventArgs e) { @@ -240,5 +245,35 @@ 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/DumpTruck/SetGeneric.cs b/DumpTruck/DumpTruck/SetGeneric.cs index b21dba9..6d34cc8 100644 --- a/DumpTruck/DumpTruck/SetGeneric.cs +++ b/DumpTruck/DumpTruck/SetGeneric.cs @@ -31,6 +31,7 @@ namespace DumpTruck.Generics /// Конструктор /// /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); public SetGeneric(int count) { _maxCount = count; @@ -41,9 +42,9 @@ namespace DumpTruck.Generics /// /// Добавляемый автомобиль /// - public int Insert(T truck) + public int Insert(T truck, IEqualityComparer? equal = null) { - return Insert(truck, 0); + return Insert(truck, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -51,13 +52,15 @@ 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/DumpTruck/TruckCompareByColor.cs b/DumpTruck/DumpTruck/TruckCompareByColor.cs new file mode 100644 index 0000000..b55d56e --- /dev/null +++ b/DumpTruck/DumpTruck/TruckCompareByColor.cs @@ -0,0 +1,44 @@ +using DumpTruck.DrawningObjects; +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(DrawningTruck? x, DrawningTruck? 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 tentColorCompare = xEntityDumpTruck.AdditionalColor.Name.CompareTo(yEntityDumpTruck.AdditionalColor.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); + } + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/TruckCompareByType.cs b/DumpTruck/DumpTruck/TruckCompareByType.cs new file mode 100644 index 0000000..be738b6 --- /dev/null +++ b/DumpTruck/DumpTruck/TruckCompareByType.cs @@ -0,0 +1,39 @@ +using DumpTruck.DrawningObjects; +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(DrawningTruck? x, DrawningTruck? 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; + } + var weightCompare = x.EntityTruck.Weight.CompareTo(y.EntityTruck.Weight); + if (weightCompare != 0) + { + return weightCompare; + } + } + } + +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/TruckGenericStorage.cs b/DumpTruck/DumpTruck/TruckGenericStorage.cs index f8ba81f..390065b 100644 --- a/DumpTruck/DumpTruck/TruckGenericStorage.cs +++ b/DumpTruck/DumpTruck/TruckGenericStorage.cs @@ -17,11 +17,11 @@ namespace DumpTruck.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _truckStorages; - /// - /// Возвращение списка названий наборов + readonly Dictionary> _truckStorages; + /// + /// /// Возвращение списка названий наборов /// - public List Keys => _truckStorages.Keys.ToList(); + public List Keys => _truckStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -49,7 +49,7 @@ namespace DumpTruck.Generics /// /// public TrucksGenericStorage(int pictureWidth, int pictureHeight) { - _truckStorages = new Dictionary>(); + _truckStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -60,7 +60,7 @@ namespace DumpTruck.Generics public void AddSet(string name) { // TODO Прописать логику для добавления - _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)); } /// /// Удаление набора @@ -69,8 +69,7 @@ namespace DumpTruck.Generics public void DelSet(string name) { // TODO Прописать логику для удаления - if (_truckStorages.ContainsKey(name)) - _truckStorages.Remove(name); + if (_truckStorages.ContainsKey(new TrucksCollectionInfo(name, string.Empty))) _truckStorages.Remove(new TrucksCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -82,8 +81,8 @@ namespace DumpTruck.Generics get { // TODO Продумать логику получения набора - if (_truckStorages.ContainsKey(ind)) - return _truckStorages[ind]; + if (_truckStorages.ContainsKey(new TrucksCollectionInfo(ind, string.Empty))) + return _truckStorages[new TrucksCollectionInfo(ind, string.Empty)]; return null; } } @@ -99,14 +98,14 @@ namespace DumpTruck.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _truckStorages) - { + foreach (KeyValuePair> record in _truckStorages) + { StringBuilder records = new(); foreach (DrawningTruck? 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) { @@ -158,7 +157,7 @@ namespace DumpTruck.Generics } } } - _truckStorages.Add(record[0], collection); + _truckStorages.Add(new TrucksCollectionInfo(record[0], string.Empty), collection); } } } diff --git a/DumpTruck/DumpTruck/TrucksCollectionInfo.cs b/DumpTruck/DumpTruck/TrucksCollectionInfo.cs new file mode 100644 index 0000000..91c07ce --- /dev/null +++ b/DumpTruck/DumpTruck/TrucksCollectionInfo.cs @@ -0,0 +1,29 @@ +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(); + } + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/TrucksGenericCollection.cs b/DumpTruck/DumpTruck/TrucksGenericCollection.cs index 82806e5..f2e95f5 100644 --- a/DumpTruck/DumpTruck/TrucksGenericCollection.cs +++ b/DumpTruck/DumpTruck/TrucksGenericCollection.cs @@ -36,7 +36,15 @@ namespace DumpTruck.Generics /// Конструктор /// /// + /// + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + + public TrucksGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -45,6 +53,11 @@ namespace DumpTruck.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } + + /// + /// Получение объектов коллекции + /// + public IEnumerable GetTrucks => _collection.GetTrucks(); /// /// Перегрузка оператора сложения /// @@ -57,7 +70,7 @@ namespace DumpTruck.Generics { return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj,new DrawiningCarEqutables()); } /// /// Перегрузка оператора вычитания @@ -132,10 +145,5 @@ namespace DumpTruck.Generics truck.DrawTransport(g); } } - - /// - /// Получение объектов коллекции - /// - public IEnumerable GetTrucks => _collection.GetTrucks(); } } \ No newline at end of file