From 3957e5ba4fd6cbdbcbde25e3203c270ddb218daf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=9F=D1=83=D1=82?= =?UTF-8?q?=D0=B8=D0=BD=D1=86=D0=B5=D0=B2?= Date: Tue, 19 Dec 2023 13:34:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=208?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RoadTrain/DrawiningTrainEqutables.cs | 59 +++++++++++++++++++++++ RoadTrain/FormTrainCollection.Designer.cs | 46 ++++++++++++++---- RoadTrain/FormTrainCollection.cs | 34 ++++++++++++- RoadTrain/RoadTrainGenericCollection.cs | 8 ++- RoadTrain/RoadTrainGenericStorage.cs | 20 ++++---- RoadTrain/SetGeneric.cs | 15 ++++-- RoadTrain/TrainCompareByColor.cs | 45 +++++++++++++++++ RoadTrain/TrainCompareByType.cs | 35 ++++++++++++++ RoadTrain/TrainsCollectionInfo.cs | 29 +++++++++++ 9 files changed, 267 insertions(+), 24 deletions(-) create mode 100644 RoadTrain/DrawiningTrainEqutables.cs create mode 100644 RoadTrain/TrainCompareByColor.cs create mode 100644 RoadTrain/TrainCompareByType.cs create mode 100644 RoadTrain/TrainsCollectionInfo.cs diff --git a/RoadTrain/DrawiningTrainEqutables.cs b/RoadTrain/DrawiningTrainEqutables.cs new file mode 100644 index 0000000..5ba13d7 --- /dev/null +++ b/RoadTrain/DrawiningTrainEqutables.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RoadTrain.DrawningObjects; +using RoadTrain.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace RoadTrain.Generics +{ + internal class DrawiningTrainEqutables : IEqualityComparer + { + public bool Equals(DrawningRoadTrain? x, DrawningRoadTrain? y) + { + if (x == null || x.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityRoadTrain.Speed != y.EntityRoadTrain.Speed) + { + return false; + } + if (x.EntityRoadTrain.Weight != y.EntityRoadTrain.Weight) + { + return false; + } + if (x.EntityRoadTrain.BodyColor != y.EntityRoadTrain.BodyColor) + { + return false; + } + if (x is DrawningTrain && y is DrawningTrain) + { + EntityTrain EntityX = x.EntityRoadTrain as EntityTrain; + EntityTrain EntityY = y.EntityRoadTrain as EntityTrain; + if (EntityX.WaterContainer != EntityY.WaterContainer) + return false; + if (EntityX.SweepingBrush != EntityY.SweepingBrush) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningRoadTrain obj) + { + return obj.GetHashCode(); + } + + } +} diff --git a/RoadTrain/FormTrainCollection.Designer.cs b/RoadTrain/FormTrainCollection.Designer.cs index 138f604..1c19281 100644 --- a/RoadTrain/FormTrainCollection.Designer.cs +++ b/RoadTrain/FormTrainCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { panel1 = new Panel(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); label2 = new Label(); panel2 = new Panel(); textBoxStorageName = new TextBox(); @@ -56,6 +58,8 @@ // // panel1 // + panel1.Controls.Add(ButtonSortByColor); + panel1.Controls.Add(ButtonSortByType); panel1.Controls.Add(label2); panel1.Controls.Add(panel2); panel1.Controls.Add(InputTextBox); @@ -69,6 +73,26 @@ panel1.Size = new Size(236, 572); panel1.TabIndex = 0; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(32, 305); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(169, 32); + ButtonSortByColor.TabIndex = 7; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(35, 257); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(163, 31); + ButtonSortByType.TabIndex = 6; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // label2 // label2.AutoSize = true; @@ -86,19 +110,19 @@ panel2.Controls.Add(listBoxStorages); panel2.Location = new Point(24, 29); panel2.Name = "panel2"; - panel2.Size = new Size(190, 278); + panel2.Size = new Size(190, 212); panel2.TabIndex = 5; // // textBoxStorageName // - textBoxStorageName.Location = new Point(21, 30); + textBoxStorageName.Location = new Point(21, 21); textBoxStorageName.Name = "textBoxStorageName"; textBoxStorageName.Size = new Size(150, 27); textBoxStorageName.TabIndex = 3; // // ButtonDelObject // - ButtonDelObject.Location = new Point(21, 179); + ButtonDelObject.Location = new Point(21, 170); ButtonDelObject.Name = "ButtonDelObject"; ButtonDelObject.Size = new Size(150, 29); ButtonDelObject.TabIndex = 2; @@ -108,7 +132,7 @@ // // ButtonAddObject // - ButtonAddObject.Location = new Point(21, 77); + ButtonAddObject.Location = new Point(21, 66); ButtonAddObject.Name = "ButtonAddObject"; ButtonAddObject.Size = new Size(150, 29); ButtonAddObject.TabIndex = 1; @@ -120,7 +144,7 @@ // listBoxStorages.FormattingEnabled = true; listBoxStorages.ItemHeight = 20; - listBoxStorages.Location = new Point(21, 121); + listBoxStorages.Location = new Point(21, 110); listBoxStorages.Name = "listBoxStorages"; listBoxStorages.Size = new Size(150, 44); listBoxStorages.TabIndex = 0; @@ -128,7 +152,7 @@ // // InputTextBox // - InputTextBox.Location = new Point(59, 384); + InputTextBox.Location = new Point(58, 407); InputTextBox.Margin = new Padding(3, 4, 3, 4); InputTextBox.Name = "InputTextBox"; InputTextBox.Size = new Size(114, 27); @@ -136,7 +160,7 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(45, 485); + ButtonRefreshCollection.Location = new Point(43, 499); ButtonRefreshCollection.Margin = new Padding(3, 4, 3, 4); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(144, 59); @@ -147,7 +171,7 @@ // // ButtonRemoveTrain // - ButtonRemoveTrain.Location = new Point(45, 428); + ButtonRemoveTrain.Location = new Point(43, 442); ButtonRemoveTrain.Margin = new Padding(3, 4, 3, 4); ButtonRemoveTrain.Name = "ButtonRemoveTrain"; ButtonRemoveTrain.Size = new Size(144, 40); @@ -158,10 +182,10 @@ // // ButtonAddTrain // - ButtonAddTrain.Location = new Point(45, 322); + ButtonAddTrain.Location = new Point(44, 355); ButtonAddTrain.Margin = new Padding(3, 4, 3, 4); ButtonAddTrain.Name = "ButtonAddTrain"; - ButtonAddTrain.Size = new Size(144, 44); + ButtonAddTrain.Size = new Size(142, 44); ButtonAddTrain.TabIndex = 1; ButtonAddTrain.Text = "Добавить поезд"; ButtonAddTrain.UseVisualStyleBackColor = true; @@ -279,5 +303,7 @@ private ToolStripMenuItem SaveToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/RoadTrain/FormTrainCollection.cs b/RoadTrain/FormTrainCollection.cs index f933bc3..30e36af 100644 --- a/RoadTrain/FormTrainCollection.cs +++ b/RoadTrain/FormTrainCollection.cs @@ -35,7 +35,7 @@ pictureBoxCollection.Height); 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)) @@ -73,6 +73,11 @@ pictureBoxCollection.Height); MessageBox.Show(ex.Message); _logger.LogWarning($"Обьект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}"); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Обьект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}"); + } } private void ButtonAddTrain_Click(object sender, EventArgs e) { @@ -210,5 +215,32 @@ _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains() } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => + CompareTrains(new TrainCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => + CompareTrains(new TrainCompareByColor()); + + /// + /// Сортировка по сравнителю + /// + /// + private void CompareTrains(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.ShowTrains(); + } + } } diff --git a/RoadTrain/RoadTrainGenericCollection.cs b/RoadTrain/RoadTrainGenericCollection.cs index 17a8626..e0eb904 100644 --- a/RoadTrain/RoadTrainGenericCollection.cs +++ b/RoadTrain/RoadTrainGenericCollection.cs @@ -47,6 +47,12 @@ namespace RoadTrain.Generics _collection = new SetGeneric(width * height); } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => + _collection.SortSet(comparer); + /// /// Получение объектов коллекции /// public IEnumerable GetTrains => _collection.GetTrains(); @@ -63,7 +69,7 @@ namespace RoadTrain.Generics { return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawiningTrainEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/RoadTrain/RoadTrainGenericStorage.cs b/RoadTrain/RoadTrainGenericStorage.cs index 628b4ed..5da3e4a 100644 --- a/RoadTrain/RoadTrainGenericStorage.cs +++ b/RoadTrain/RoadTrainGenericStorage.cs @@ -18,12 +18,12 @@ namespace RoadTrain.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _trainStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _trainStorages.Keys.ToList(); + public List Keys => _trainStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -52,7 +52,7 @@ namespace RoadTrain.Generics /// public RoadTrainGenericStorage(int pictureWidth, int pictureHeight) { - _trainStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -63,8 +63,8 @@ namespace RoadTrain.Generics /// Название набора public void AddSet(string name) { - if (_trainStorages.ContainsKey(name)) return; - _trainStorages[name] = new RoadTrainGenericCollection(_pictureWidth, _pictureHeight); + if (_trainStorages.ContainsKey(new TrainsCollectionInfo(name, string.Empty))) return; + _trainStorages.Add(new TrainsCollectionInfo(name, string.Empty), new RoadTrainGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -72,7 +72,7 @@ namespace RoadTrain.Generics /// Название набора public void DelSet(string name) { - if (_trainStorages.ContainsKey(name)) _trainStorages.Remove(name); + if (_trainStorages.ContainsKey(new TrainsCollectionInfo(name, string.Empty))) _trainStorages.Remove(new TrainsCollectionInfo(name, string.Empty)); return; } /// @@ -85,7 +85,9 @@ namespace RoadTrain.Generics { get { - if (_trainStorages.ContainsKey(ind)) return _trainStorages[ind]; + TrainsCollectionInfo indObj = new TrainsCollectionInfo(ind, string.Empty); + if (_trainStorages.ContainsKey(indObj)) + return _trainStorages[indObj]; return null; } } @@ -101,7 +103,7 @@ namespace RoadTrain.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _trainStorages) { StringBuilder records = new(); @@ -182,7 +184,7 @@ namespace RoadTrain.Generics } } - _trainStorages.Add(name, collection); + _trainStorages.Add(new TrainsCollectionInfo(name, string.Empty), collection); } } } diff --git a/RoadTrain/SetGeneric.cs b/RoadTrain/SetGeneric.cs index 41747fb..2b74869 100644 --- a/RoadTrain/SetGeneric.cs +++ b/RoadTrain/SetGeneric.cs @@ -28,13 +28,19 @@ namespace RoadTrain.Generics _maxCount = count; _places = new List (count); } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => +_places.Sort(comparer); /// /// Добавление объекта в набор /// /// - public int Insert(T train) + public int Insert(T train, IEqualityComparer? equal = null) { - return Insert(train, 0); + return Insert(train, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -42,13 +48,16 @@ namespace RoadTrain.Generics /// Добавляемый автомобиль /// Позиция /// - public int Insert(T train, int position) + public int Insert(T train, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new StorageOverflowException("Невалидная операция"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + + if (equal != null && _places.Contains(train, equal)) + throw new ArgumentException("Обьект уже есть в коллекции"); _places.Insert(position, train); return position; } diff --git a/RoadTrain/TrainCompareByColor.cs b/RoadTrain/TrainCompareByColor.cs new file mode 100644 index 0000000..9385556 --- /dev/null +++ b/RoadTrain/TrainCompareByColor.cs @@ -0,0 +1,45 @@ +using RoadTrain.DrawningObjects; +using RoadTrain.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RoadTrain +{ + internal class TrainCompareByColor : IComparer + { + public int Compare(DrawningRoadTrain? x, DrawningRoadTrain? y) + { + if (x == null || x.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityRoadTrain.BodyColor.Name != y.EntityRoadTrain.BodyColor.Name) + { + return x.EntityRoadTrain.BodyColor.Name.CompareTo(y.EntityRoadTrain.BodyColor.Name); + } + if (x.GetType().Name == y.GetType().Name && x is DrawningTrain) + { + EntityTrain EntityX = x.EntityRoadTrain as EntityTrain; + EntityTrain EntityY = y.EntityRoadTrain as EntityTrain; + if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name) + { + return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name); + } + } + var speedCompare = + x.EntityRoadTrain.Speed.CompareTo(y.EntityRoadTrain.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityRoadTrain.Weight.CompareTo(y.EntityRoadTrain.Weight); + } + } +} diff --git a/RoadTrain/TrainCompareByType.cs b/RoadTrain/TrainCompareByType.cs new file mode 100644 index 0000000..1e27637 --- /dev/null +++ b/RoadTrain/TrainCompareByType.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RoadTrain.DrawningObjects; + +namespace RoadTrain.Generics +{ + internal class TrainCompareByType : IComparer + { + public int Compare(DrawningRoadTrain? x, DrawningRoadTrain? y) + { + if (x == null || x.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityRoadTrain == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityRoadTrain.Speed.CompareTo(y.EntityRoadTrain.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityRoadTrain.Weight.CompareTo(y.EntityRoadTrain.Weight); + } + } +} diff --git a/RoadTrain/TrainsCollectionInfo.cs b/RoadTrain/TrainsCollectionInfo.cs new file mode 100644 index 0000000..314a3a7 --- /dev/null +++ b/RoadTrain/TrainsCollectionInfo.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RoadTrain +{ + internal class TrainsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public TrainsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(TrainsCollectionInfo? other) + { + if (other == null) + return false; + return other.Name == Name; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } +} -- 2.25.1