From d5e19301a4c782688b8e1460e54b640ec0a12bb4 Mon Sep 17 00:00:00 2001 From: qkrlnt Date: Sat, 18 May 2024 16:26:55 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=968?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 9 ++- .../CollectionInfo.cs | 77 +++++++++++++++++++ .../ICollectionGenericObjects.cs | 13 +++- .../ListGenericObjects.cs | 13 +++- .../MassiveGenericObjects.cs | 12 ++- .../StorageCollection.cs | 53 +++++++------ .../Drawings/DrawingTrainCompareByColor.cs | 29 +++++++ .../Drawings/DrawingTrainCompareByType.cs | 29 +++++++ .../Drawings/DrawingTrainEqutables.cs | 65 ++++++++++++++++ .../Exceptions/ObjectExistsException.cs | 18 +++++ .../FormTrainCollection.Designer.cs | 44 +++++++++-- ProjectMonorail/FormTrainCollection.cs | 25 +++++- ProjectMonorail/FormTrainCollection.resx | 2 +- 13 files changed, 346 insertions(+), 43 deletions(-) create mode 100644 ProjectMonorail/CollectionGenericObjects/CollectionInfo.cs create mode 100644 ProjectMonorail/Drawings/DrawingTrainCompareByColor.cs create mode 100644 ProjectMonorail/Drawings/DrawingTrainCompareByType.cs create mode 100644 ProjectMonorail/Drawings/DrawingTrainEqutables.cs create mode 100644 ProjectMonorail/Exceptions/ObjectExistsException.cs diff --git a/ProjectMonorail/CollectionGenericObjects/AbstractCompany.cs b/ProjectMonorail/CollectionGenericObjects/AbstractCompany.cs index 739a979..0e417a8 100644 --- a/ProjectMonorail/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectMonorail/CollectionGenericObjects/AbstractCompany.cs @@ -70,7 +70,7 @@ public abstract class AbstractCompany { return -1; } - return company._collection.Insert(train); + return company._collection.Insert(train, new DrawingTrainEqutables()); } /// @@ -117,6 +117,13 @@ public abstract class AbstractCompany return bitmap; } + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); + + /// /// Вывод заднего фона /// diff --git a/ProjectMonorail/CollectionGenericObjects/CollectionInfo.cs b/ProjectMonorail/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..653eb7f --- /dev/null +++ b/ProjectMonorail/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectMonorail.CollectionGenericObjects; + +/// +/// Класс, хранящий информацию по коллекции +/// +public class CollectionInfo : IEquatable +{ + /// + /// Название + /// + public string Name { get; private set; } + + /// + /// Тип + /// + public CollectionType CollectionType { get; private set; } + + /// + /// Описание + /// + public string Description { get; private set; } + + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separator = "-"; + + /// + /// Конструктор + /// + /// Название + /// Тип + /// Описание + public CollectionInfo(string name, CollectionType collectionType, string description) + { + Name = name; + CollectionType = collectionType; + Description = description; + } + + public static CollectionInfo? GetCollectionInfo(string data) + { + string[] strs = data.Split(_separator, StringSplitOptions.RemoveEmptyEntries); + if (strs.Length < 1 || strs.Length > 3) + { + return null; + } + + return new CollectionInfo(strs[0], (CollectionType)Enum.Parse(typeof(CollectionType), strs[1]), strs.Length > 2 ? strs[2] : string.Empty); + } + + public override string ToString() + { + return Name + _separator + CollectionType + _separator + Description; + } + + public bool Equals(CollectionInfo? other) + { + return Name == other?.Name; + } + + public override bool Equals(object? obj) + { + return Equals(obj as CollectionInfo); + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } +} \ No newline at end of file diff --git a/ProjectMonorail/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectMonorail/CollectionGenericObjects/ICollectionGenericObjects.cs index dc7943b..ce57a50 100644 --- a/ProjectMonorail/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectMonorail/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,4 +1,5 @@ using ProjectMonorail.CollectionGenericObjects; +using ProjectMonorail.Drawings; using System; using System.Collections.Generic; using System.Linq; @@ -28,16 +29,18 @@ public interface ICollectionGenericObjects /// Добавление объекта в коллекцию /// /// Добавляемый объект + /// Cравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// Добавляемый объект /// Позиция + /// Cравнение двух объектов /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции @@ -63,4 +66,10 @@ public interface ICollectionGenericObjects /// /// Поэлементный вывод элементов коллекции IEnumerable GetItems(); + + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } \ No newline at end of file diff --git a/ProjectMonorail/CollectionGenericObjects/ListGenericObjects.cs b/ProjectMonorail/CollectionGenericObjects/ListGenericObjects.cs index 2f3e2a4..5b56383 100644 --- a/ProjectMonorail/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectMonorail/CollectionGenericObjects/ListGenericObjects.cs @@ -1,5 +1,7 @@ using ProjectMonorail.CollectionGenericObject; +using ProjectMonorail.Drawings; using ProjectMonorail.Exceptions; +using System.Linq; namespace ProjectMonorail.CollectionGenericObjects; @@ -40,17 +42,19 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { if (Count + 1 > _maxCount) { throw new CollectionOverflowException(); } + if (_collection.Contains(obj, comparer)) { throw new ObjectExistsException(); } _collection.Add(obj); return Count + 1; } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { if (Count + 1 > _maxCount) { throw new CollectionOverflowException(); } if (position < 0 || position >= Count) { throw new PositionOutOfCollectionException(); } + if (_collection.Contains(obj, comparer)) { throw new ObjectExistsException(); } _collection.Insert(position, obj); return Count + 1; } @@ -70,4 +74,9 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/ProjectMonorail/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectMonorail/CollectionGenericObjects/MassiveGenericObjects.cs index e5716b6..04deccf 100644 --- a/ProjectMonorail/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectMonorail/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ using ProjectMonorail.CollectionGenericObjects; +using ProjectMonorail.Drawings; using ProjectMonorail.Exceptions; namespace ProjectMonorail.CollectionGenericObject; @@ -56,8 +57,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + if (_collection.Contains(obj, comparer)) { throw new ObjectExistsException(); } for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -69,9 +71,10 @@ public class MassiveGenericObjects : ICollectionGenericObjects throw new CollectionOverflowException(); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { if (position < 0 || position >= Count) { throw new PositionOutOfCollectionException(); } + if (_collection.Contains(obj, comparer)) { throw new ObjectExistsException(); } for (int i = position; i < Count; i++) { if (_collection[i] == null) @@ -107,4 +110,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } \ No newline at end of file diff --git a/ProjectMonorail/CollectionGenericObjects/StorageCollection.cs b/ProjectMonorail/CollectionGenericObjects/StorageCollection.cs index 6442606..cfb63f2 100644 --- a/ProjectMonorail/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectMonorail/CollectionGenericObjects/StorageCollection.cs @@ -1,6 +1,7 @@ using ProjectMonorail.CollectionGenericObject; using ProjectMonorail.Drawings; using ProjectMonorail.Exceptions; +using System.Collections; namespace ProjectMonorail.CollectionGenericObjects; @@ -14,12 +15,12 @@ public class StorageCollection /// /// Словарь (хранилище) коллекций /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Ключевое слово, с которого должен начинаться файл @@ -41,7 +42,7 @@ public class StorageCollection /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// @@ -51,16 +52,17 @@ public class StorageCollection /// Тип коллекции public void AddCollection(String name, CollectionType collectionType) { - if (name == null || _storages.ContainsKey(name)) { return; } - switch (collectionType) + CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty); + if (collectionInfo.Name == null || _storages.ContainsKey(collectionInfo)) { return; } + switch (collectionInfo.CollectionType) { case CollectionType.None: break; case CollectionType.Massive: - _storages.Add(name, new MassiveGenericObjects()); + _storages.Add(collectionInfo, new MassiveGenericObjects()); break; case CollectionType.List: - _storages.Add(name, new ListGenericObjects()); + _storages.Add(collectionInfo, new ListGenericObjects()); break; } } @@ -71,21 +73,23 @@ public class StorageCollection /// Название коллекции public void DelCollection(String name) { - if (name == null || !_storages.ContainsKey(name)) { return; } - _storages.Remove(name); - } + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (collectionInfo.Name == null || !_storages.ContainsKey(collectionInfo)) { return; } + _storages.Remove(collectionInfo); + } /// /// Доступ к коллекции /// /// Название коллекции /// - public ICollectionGenericObjects? this[String name] + public ICollectionGenericObjects? this[String collectionName] { get { - if (_storages.TryGetValue(name, out ICollectionGenericObjects? value)) { return value; } - return null; + CollectionInfo collectionInfo = new CollectionInfo(collectionName, CollectionType.None, ""); + if (collectionInfo == null || !_storages.ContainsKey(collectionInfo)) { return null; } + return _storages[collectionInfo]; } } @@ -108,7 +112,7 @@ public class StorageCollection using FileStream fs = new(filename, FileMode.Create); using StreamWriter streamWriter = new StreamWriter(fs); streamWriter.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { streamWriter.Write(Environment.NewLine); @@ -120,8 +124,6 @@ public class StorageCollection streamWriter.Write(value.Key); streamWriter.Write(_separatorForKeyValue); - streamWriter.Write(value.Value.GetCollectionType); - streamWriter.Write(_separatorForKeyValue); streamWriter.Write(value.Value.MaxCount); streamWriter.Write(_separatorForKeyValue); @@ -166,20 +168,18 @@ public class StorageCollection while (!streamReader.EndOfStream) { string[] record = streamReader.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - if (record.Length != 4) + if (record.Length != 3) { continue; } - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); - if (collection == null) - { - throw new InvalidCastException("Не удалось создать коллекцию"); - } - collection.MaxCount = Convert.ToInt32(record[2]); + CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции:" + record[0]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType) ?? + throw new Exception("Не удалось определить тип коллекции:" + record[1]); + collection.MaxCount = Convert.ToInt32(record[1]); - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { if (elem?.CreateDrawingTrain() is T train) @@ -197,8 +197,7 @@ public class StorageCollection } } } - - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } diff --git a/ProjectMonorail/Drawings/DrawingTrainCompareByColor.cs b/ProjectMonorail/Drawings/DrawingTrainCompareByColor.cs new file mode 100644 index 0000000..889c652 --- /dev/null +++ b/ProjectMonorail/Drawings/DrawingTrainCompareByColor.cs @@ -0,0 +1,29 @@ +namespace ProjectMonorail.Drawings; + +/// +/// Сравнение по цвету, скорости, весу +/// +public class DrawingTrainCompareByColor : IComparer +{ + public int Compare(DrawingTrain? x, DrawingTrain? y) + { + if (x == null || x.EntityTrain == null) + { + return -1; + } + if (y == null || y.EntityTrain == null) + { + return 1; + } + if (x.EntityTrain.MainColor.Name != y.EntityTrain.MainColor.Name) + { + return x.EntityTrain.MainColor.Name.CompareTo(y.EntityTrain.MainColor.Name); + } + var speedCompare = x.EntityTrain.Speed.CompareTo(y.EntityTrain.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityTrain.Weight.CompareTo(y.EntityTrain.Weight); + } +} diff --git a/ProjectMonorail/Drawings/DrawingTrainCompareByType.cs b/ProjectMonorail/Drawings/DrawingTrainCompareByType.cs new file mode 100644 index 0000000..9897741 --- /dev/null +++ b/ProjectMonorail/Drawings/DrawingTrainCompareByType.cs @@ -0,0 +1,29 @@ +namespace ProjectMonorail.Drawings; + +/// +/// Сравнение по типу, скорости, весу +/// +public class DrawingTrainCompareByType : IComparer +{ + public int Compare(DrawingTrain? x, DrawingTrain? y) + { + if (x == null || x.EntityTrain == null) + { + return -1; + } + if (y == null || y.EntityTrain == null) + { + return 1; + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityTrain.Speed.CompareTo(y.EntityTrain.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityTrain.Weight.CompareTo(y.EntityTrain.Weight); + } +} diff --git a/ProjectMonorail/Drawings/DrawingTrainEqutables.cs b/ProjectMonorail/Drawings/DrawingTrainEqutables.cs new file mode 100644 index 0000000..0652c79 --- /dev/null +++ b/ProjectMonorail/Drawings/DrawingTrainEqutables.cs @@ -0,0 +1,65 @@ +using ProjectMonorail.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectMonorail.Drawings; + +/// +/// Реализация сравнения двух объектов класса-прорисовки +/// +public class DrawingTrainEqutables : IEqualityComparer +{ + public bool Equals(DrawingTrain? x, DrawingTrain? y) + { + if (x == null || x.EntityTrain == null) + { + return false; + } + if (y == null || y.EntityTrain == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityTrain.Speed != y.EntityTrain.Speed) + { + return false; + } + if (x.EntityTrain.Weight != y.EntityTrain.Weight) + { + return false; + } + if (x.EntityTrain.MainColor != y.EntityTrain.MainColor) + { + return false; + } + if (x is DrawingMonorail && y is DrawingMonorail) + { + EntityMonorail newX = (EntityMonorail)x.EntityTrain; + EntityMonorail newY = (EntityMonorail)y.EntityTrain; + if (newX.AdditionalColor != newY.AdditionalColor) + { + return false; + } + if (newX.Wheels != newY.Wheels) + { + return false; + } + if (newX.Rail != newY.Rail) + { + return false; + } + if (newX.SecondСarriage != newY.SecondСarriage) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull] DrawingTrain? obj) + { + return obj.GetHashCode(); + } +} diff --git a/ProjectMonorail/Exceptions/ObjectExistsException.cs b/ProjectMonorail/Exceptions/ObjectExistsException.cs new file mode 100644 index 0000000..b4c8dd3 --- /dev/null +++ b/ProjectMonorail/Exceptions/ObjectExistsException.cs @@ -0,0 +1,18 @@ +using System.Runtime.Serialization; + +namespace ProjectMonorail.Exceptions; + +/// +/// Класс, описывающий ошибку добавления уже существующего объекта +/// +[Serializable] +public class ObjectExistsException : ApplicationException +{ + public ObjectExistsException() : base("Данный объект уже существует") { } + + public ObjectExistsException(string message) : base(message) { } + + public ObjectExistsException(string message, Exception exception) : base(message, exception) { } + + protected ObjectExistsException(SerializationInfo info, StreamingContext context) : base(info, context) { } +} diff --git a/ProjectMonorail/FormTrainCollection.Designer.cs b/ProjectMonorail/FormTrainCollection.Designer.cs index 0b75152..cd9e1cb 100644 --- a/ProjectMonorail/FormTrainCollection.Designer.cs +++ b/ProjectMonorail/FormTrainCollection.Designer.cs @@ -52,6 +52,8 @@ loadToolStripMenuItem = new ToolStripMenuItem(); saveFileDialog = new SaveFileDialog(); openFileDialog = new OpenFileDialog(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxTools.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); @@ -70,13 +72,15 @@ groupBoxTools.Margin = new Padding(3, 2, 3, 2); groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Padding = new Padding(3, 2, 3, 2); - groupBoxTools.Size = new Size(200, 592); + groupBoxTools.Size = new Size(200, 642); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Controls.Add(buttonRemoveTrain); @@ -84,9 +88,9 @@ panelCompanyTools.Controls.Add(buttonAddTrain); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 383); + panelCompanyTools.Location = new Point(3, 370); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(194, 207); + panelCompanyTools.Size = new Size(194, 270); panelCompanyTools.TabIndex = 8; // // buttonRefresh @@ -150,7 +154,7 @@ // buttonCreateCompany // buttonCreateCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonCreateCompany.Location = new Point(6, 344); + buttonCreateCompany.Location = new Point(6, 332); buttonCreateCompany.Margin = new Padding(3, 2, 3, 2); buttonCreateCompany.Name = "buttonCreateCompany"; buttonCreateCompany.Size = new Size(185, 34); @@ -247,7 +251,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 317); + comboBoxSelectorCompany.Location = new Point(6, 305); comboBoxSelectorCompany.Margin = new Padding(3, 2, 3, 2); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(185, 23); @@ -260,7 +264,7 @@ pictureBox.Location = new Point(0, 24); pictureBox.Margin = new Padding(3, 2, 3, 2); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(1006, 592); + pictureBox.Size = new Size(1006, 642); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -305,11 +309,35 @@ openFileDialog.FileName = "openFileDialog1"; openFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByColor + // + buttonSortByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByColor.Location = new Point(3, 229); + buttonSortByColor.Margin = new Padding(3, 2, 3, 2); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(185, 34); + buttonSortByColor.TabIndex = 14; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByType.Location = new Point(3, 191); + buttonSortByType.Margin = new Padding(3, 2, 3, 2); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(185, 34); + buttonSortByType.TabIndex = 13; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // FormTrainCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1206, 616); + ClientSize = new Size(1206, 666); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); @@ -355,5 +383,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/ProjectMonorail/FormTrainCollection.cs b/ProjectMonorail/FormTrainCollection.cs index c9c3217..3ec616f 100644 --- a/ProjectMonorail/FormTrainCollection.cs +++ b/ProjectMonorail/FormTrainCollection.cs @@ -81,6 +81,11 @@ public partial class FormTrainCollection : Form MessageBox.Show("Коллекция переполнена"); _logger.LogWarning($"Не удалось добавить объект в коллекцию: {ex.Message}"); } + catch (ObjectExistsException ex) + { + MessageBox.Show("Данный объект уже существует"); + _logger.LogWarning($"Не удалось добавить объект в коллекцию: {ex.Message}"); + } } /// @@ -245,7 +250,7 @@ public partial class FormTrainCollection : Form listBoxCollection.Items.Clear(); for (int i = 0; i < _storageCollection.Keys?.Count; i++) { - string? colName = _storageCollection.Keys?[i]; + string? colName = _storageCollection.Keys?[i].Name; if (!string.IsNullOrEmpty(colName)) { listBoxCollection.Items.Add(colName); @@ -333,4 +338,22 @@ public partial class FormTrainCollection : Form } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + CompareTrains(new DrawingTrainCompareByType()); + } + + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + CompareTrains(new DrawingTrainCompareByColor()); + } + + private void CompareTrains(IComparer comparer) + { + if (_company == null) { return; } + + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } } diff --git a/ProjectMonorail/FormTrainCollection.resx b/ProjectMonorail/FormTrainCollection.resx index dc0485e..a3940ca 100644 --- a/ProjectMonorail/FormTrainCollection.resx +++ b/ProjectMonorail/FormTrainCollection.resx @@ -127,6 +127,6 @@ 261, 17 - 44 + 48 \ No newline at end of file