From 3ecffd4d5416b9b88e91ccc91da9edacdb508cf2 Mon Sep 17 00:00:00 2001 From: Anitonchik Date: Mon, 13 May 2024 22:43:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=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 | 12 +++- .../CollectionInfo.cs | 71 +++++++++++++++++++ .../ICollectionGenericObjects.cs | 14 ++-- .../ListGenericObjects.cs | 28 ++++++-- .../MassiveGenericObjects.cs | 39 ++++++++-- .../StorageCollection.cs | 46 ++++++------ .../Drawnings/DrawiningTruckEqutables.cs | 63 ++++++++++++++++ .../Drawnings/DrawningTruckCompareByColor.cs | 35 +++++++++ .../Drawnings/DrawningTruckCompareByType.cs | 32 +++++++++ .../Exceptions/ObjectsEqualException.cs | 17 +++++ .../FormTruckCollection.Designer.cs | 36 ++++++++-- .../ProjectDumpTruck/FormTruckCollection.cs | 55 ++++++++++---- 12 files changed, 394 insertions(+), 54 deletions(-) create mode 100644 ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/CollectionInfo.cs create mode 100644 ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawiningTruckEqutables.cs create mode 100644 ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawningTruckCompareByColor.cs create mode 100644 ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawningTruckCompareByType.cs create mode 100644 ProjectDumpTruck/ProjectDumpTruck/Exceptions/ObjectsEqualException.cs diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/AbstractCompany.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/AbstractCompany.cs index 98999ef..2438c80 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/AbstractCompany.cs @@ -38,6 +38,7 @@ public abstract class AbstractCompany /// private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + /// /// Конструктор /// @@ -60,7 +61,7 @@ public abstract class AbstractCompany /// public static int operator +(AbstractCompany company, DrawningTruck truck) { - return company._collection.Insert(truck); + return company._collection.Insert(truck, new DrawiningTruckEqutables()); } /// @@ -105,6 +106,13 @@ public abstract class AbstractCompany } return bitmap; } + + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); + /// /// Вывод заднего фона /// @@ -114,4 +122,6 @@ public abstract class AbstractCompany /// Расстановка объектов /// protected abstract void SetObjectsPosition(); + + } diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/CollectionInfo.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..660a42d --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.CollectionGenericObjects; + +public class CollectionInfo +{ + /// + /// Название + /// + 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; + } + /// + /// Создание объекта из строки + /// + /// Строка + /// Объект или null + 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(); + } +} diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ICollectionGenericObjects.cs index fc4ea8e..9774b49 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,4 +1,6 @@ -using System; +using ProjectDumpTruck.Drawnings; +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -27,14 +29,14 @@ where T : class /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj); + int Insert(T obj, IEqualityComparer? comparer = null); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// /// Добавляемый объект /// Позиция /// true - вставка прошла удачно, false - вставка не удалась - int Insert(T obj, int position); + int Insert(T obj, int position, IEqualityComparer? comparer = null); /// /// Удаление объекта из коллекции с конкретной позиции /// @@ -57,6 +59,10 @@ where T : class /// /// Поэлементый вывод элементов коллекции IEnumerable GetItems(); - + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); } diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ListGenericObjects.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ListGenericObjects.cs index e2c9d4f..72b616f 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ListGenericObjects.cs @@ -1,4 +1,5 @@ -using ProjectDumpTruck.Exceptions; +using ProjectDumpTruck.Drawnings; +using ProjectDumpTruck.Exceptions; using System; using System.Collections.Generic; using System.Linq; @@ -27,7 +28,7 @@ public class ListGenericObjects : ICollectionGenericObjects { get { - return MaxCount; + return _maxCount; } set { @@ -56,8 +57,15 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectsEqualException(); + } + } if (_collection.Count >= _maxCount) { throw new CollectionOverflowException(MaxCount); @@ -66,8 +74,15 @@ public class ListGenericObjects : ICollectionGenericObjects return _collection.Count; } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectsEqualException(position); + } + } if (position < 0 || position > _maxCount) { throw new PositionOutOfCollectionException(position); @@ -99,5 +114,10 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/MassiveGenericObjects.cs index 27de0ac..788194f 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/MassiveGenericObjects.cs @@ -63,25 +63,45 @@ public class MassiveGenericObjects : ICollectionGenericObjects } return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - - for (int i = 0; i < MaxCount; ++i) + if (comparer != null) { - if (_collection[i] == null) + for (int i = 0; i < MaxCount; ++i) { - _collection[i] = obj; - return i; + if ((comparer as IEqualityComparer).Equals(obj as DrawningTruck, _collection[i] as DrawningTruck)) + { + throw new ObjectsEqualException(i); + } + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } } } throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position) + + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { if (position < 0 || position >= MaxCount) { throw new PositionOutOfCollectionException(Count); } + if (comparer != null) + { + for (int i = 0; i < position; i++) + { + if (_collection[i] != null) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningTruck, _collection[i] as DrawningTruck)) + { + throw new ObjectsEqualException(i); + } + } + } + } if (_collection[position] == null) { _collection[position] = obj; @@ -127,5 +147,10 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/StorageCollection.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/StorageCollection.cs index 0d24ee5..cc2ff2b 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/StorageCollection.cs @@ -18,11 +18,11 @@ public class StorageCollection /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Ключевое слово, с которого должен начинаться файл @@ -42,7 +42,7 @@ public class StorageCollection /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// @@ -52,7 +52,8 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (_storages.ContainsKey(name)) + CollectionInfo info = new CollectionInfo(name, collectionType, string.Empty); + if (_storages.ContainsKey(info)) { return; } @@ -61,13 +62,13 @@ public class StorageCollection case CollectionType.None: return; case CollectionType.Massive: - _storages.Add(name, new MassiveGenericObjects()); + _storages.Add(info, new MassiveGenericObjects()); break; case CollectionType.List: - _storages.Add(name, new ListGenericObjects()); + _storages.Add(info, new ListGenericObjects()); break; } - } + } /// /// Удаление коллекции @@ -75,9 +76,10 @@ public class StorageCollection /// Название коллекции public void DelCollection(string name) { - if (_storages.ContainsKey(name)) + CollectionInfo info = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(info)) { - _storages.Remove(name); + _storages.Remove(info); } } @@ -91,9 +93,10 @@ public class StorageCollection { get { - if (_storages.ContainsKey(name)) + CollectionInfo info = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(info)) { - return _storages[name]; + return _storages[info]; } return null; } @@ -121,16 +124,14 @@ public class StorageCollection wr.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { wr.Write(Environment.NewLine); if (value.Value.Count == 0) { continue; } - wr.Write(value.Key); - wr.Write(_separatorForKeyValue); - wr.Write(value.Value.GetCollectionType); + wr.Write(value.Key.ToString()); wr.Write(_separatorForKeyValue); wr.Write(value.Value.MaxCount); wr.Write(_separatorForKeyValue); @@ -177,18 +178,21 @@ public class StorageCollection while ((strs = rd.ReadLine()) != null) { string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 4) + if (record.Length != 3) { continue; } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + CollectionInfo? col_info = CollectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции: " + record[0]); ; + + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(col_info.CollectionType) ?? + throw new Exception("Не удалось создать коллекцию"); if (collection == null) { throw new Exception("Не удалось создать коллекции"); } - collection.MaxCount = Convert.ToInt32(record[2]); - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + collection.MaxCount = Convert.ToInt32(record[1]); + string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { if (elem?.CreateDrawningTruck() is T truck) @@ -207,7 +211,7 @@ public class StorageCollection } } - _storages.Add(record[0], collection); + _storages.Add(col_info, collection); } } diff --git a/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawiningTruckEqutables.cs b/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawiningTruckEqutables.cs new file mode 100644 index 0000000..60f95bf --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawiningTruckEqutables.cs @@ -0,0 +1,63 @@ +using ProjectDumpTruck.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.Drawnings; + +public class DrawiningTruckEqutables : IEqualityComparer +{ + public bool Equals(DrawningTruck? x, DrawningTruck? y) + { + if (x == null || x.EntityTruck == null) + { + return false; + } + if (y == null || y.EntityTruck == null) + { + return false; + } + 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 dump_truck_x = (EntityDumpTruck)x.EntityTruck; + EntityDumpTruck dump_truck_y = (EntityDumpTruck)y.EntityTruck; + if (dump_truck_x.Body != dump_truck_y.Body) + { + return false; + } + if (dump_truck_x.Tent != dump_truck_y.Tent) + { + return false; + } + if (dump_truck_x.AdditionalColor != dump_truck_y.AdditionalColor) + { + return false; + } + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningTruck? obj) + { + throw new NotImplementedException(); + } +} diff --git a/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawningTruckCompareByColor.cs b/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawningTruckCompareByColor.cs new file mode 100644 index 0000000..dbe4331 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawningTruckCompareByColor.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.Drawnings; + +public class DrawningTruckCompareByColor : IComparer +{ + public int Compare(DrawningTruck? x, DrawningTruck? y) + { + if (x == null || x.EntityTruck == null) + { + return 1; + } + + if (y == null || y.EntityTruck == null) + { + return -1; + } + var bodycolorCompare = x.EntityTruck.BodyColor.Name.CompareTo(y.EntityTruck.BodyColor.Name); + if (bodycolorCompare != 0) + { + return bodycolorCompare; + } + 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/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawningTruckCompareByType.cs b/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawningTruckCompareByType.cs new file mode 100644 index 0000000..ca2c8cc --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/Drawnings/DrawningTruckCompareByType.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.Drawnings; + +public class DrawningTruckCompareByType : IComparer +{ + public int Compare(DrawningTruck? x, DrawningTruck? y) + { + if (x == null || x.EntityTruck == null) + { + return -1; + } + if (y == null || y.EntityTruck == null) + { + return 1; + } + 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/ProjectDumpTruck/ProjectDumpTruck/Exceptions/ObjectsEqualException.cs b/ProjectDumpTruck/ProjectDumpTruck/Exceptions/ObjectsEqualException.cs new file mode 100644 index 0000000..141dfd9 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/Exceptions/ObjectsEqualException.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.Exceptions; + +public class ObjectsEqualException : ApplicationException +{ + public ObjectsEqualException(int i) : base("В коллекции находится такой же элемент на позиции " + i) { } + public ObjectsEqualException() : base( "В коллекции находится такой же элемент") { } + public ObjectsEqualException(string message) : base(message) { } + public ObjectsEqualException(string message, Exception exception) : base(message, exception) { } + protected ObjectsEqualException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} diff --git a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs index 2ae6507..b7a5304 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.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(); @@ -68,22 +70,24 @@ groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(832, 28); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(250, 799); + groupBoxTools.Size = new Size(250, 925); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(buttonAddTruck); panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Controls.Add(maskedTextBoxPosition); panelCompanyTools.Controls.Add(buttonGoToChek); panelCompanyTools.Controls.Add(buttonRemoveTruck); panelCompanyTools.Dock = DockStyle.Bottom; - panelCompanyTools.Location = new Point(3, 464); + panelCompanyTools.Location = new Point(3, 479); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(244, 332); + panelCompanyTools.Size = new Size(244, 443); panelCompanyTools.TabIndex = 4; // // buttonAddTruck @@ -245,7 +249,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 28); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(832, 799); + pictureBox.Size = new Size(832, 925); pictureBox.TabIndex = 3; pictureBox.TabStop = false; // @@ -291,11 +295,31 @@ openFileDialog.FileName = "openFileDialog1"; openFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(15, 371); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(220, 57); + buttonSortByColor.TabIndex = 8; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(15, 308); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(220, 57); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // FormTruckCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1082, 827); + ClientSize = new Size(1082, 953); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); @@ -340,5 +364,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/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs index 67003ff..01bfb91 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs @@ -79,17 +79,22 @@ public partial class FormTruckCollection : Form { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); - _logger.LogInformation("Объект добавлен"); + _logger.LogInformation("Объект добавлен"); } } - catch (Exception ex) + catch (ObjectNotFoundException) { } + catch (CollectionOverflowException ex) { - - MessageBox.Show("Не удалось добавить объект"); - _logger.LogWarning($"Ошибка: {ex.Message}"); + MessageBox.Show("Коллекция переполнена"); + _logger.LogError("Ошибка: {Message}", ex.Message); } - - + catch (ObjectsEqualException ex) + { + MessageBox.Show("Такой объект уже существует в коллекции"); + _logger.LogError("Ошибка: {Message}", ex.Message); + } + + } /// @@ -118,7 +123,8 @@ public partial class FormTruckCollection : Form _logger.LogInformation("Объект удален"); } } - catch (Exception ex) { + catch (Exception ex) + { MessageBox.Show("Не удалось удалить объект"); _logger.LogError("Ошибка: {Message}", ex.Message); } @@ -205,7 +211,7 @@ public partial class FormTruckCollection : 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); @@ -286,18 +292,43 @@ public partial class FormTruckCollection : Form { if (openFileDialog.ShowDialog() == DialogResult.OK) { - try + try { _storageCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); RerfreshListBoxItems(); } - catch (Exception ex) - { + catch (Exception ex) + { MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogError("Ошибка: {Message}", ex.Message); } } } + + private void buttonSortByType_Click(object sender, EventArgs e) + { + CompareTrucks(new DrawningTruckCompareByType()); + } + + private void buttonSortByColor_Click(object sender, EventArgs e) + { + CompareTrucks(new DrawningTruckCompareByColor()); + } + + /// + /// Сортировка по сравнителю + /// + /// Сравнитель объектов + private void CompareTrucks(IComparer comparer) + { + if (_company == null) + { + return; + } + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } + } -- 2.25.1