diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs index a95b4aa..9f142a8 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs @@ -49,7 +49,7 @@ public abstract class AbstractCompany /// public static int operator +(AbstractCompany company, DrawningShip ship) { - return company._collection.Insert(ship); + return company._collection.Insert(ship, new DrawiningShipEqutables()); } /// /// Перегрузка оператора удаления для класса @@ -100,4 +100,10 @@ public abstract class AbstractCompany /// Расстановка объектов /// protected abstract void SetObjectsPosition(); + /// + /// Сортировка + /// + /// Сравнитель объектов + public void Sort(IComparer comparer) => _collection?.CollectionSort(comparer); + } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/CollectionInfo.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/CollectionInfo.cs new file mode 100644 index 0000000..5089571 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/CollectionInfo.cs @@ -0,0 +1,43 @@ +namespace ProjectWarmlyShip.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(); + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs index 5ffe4cc..41f74ef 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -15,15 +15,17 @@ 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); /// /// Удаление объекта из коллекции с конкретной позиции /// @@ -45,4 +47,10 @@ public interface ICollectionGenericObjects /// /// Поэлементый вывод элементов коллекции IEnumerable GetItems(); + /// + /// Сортировка коллекции + /// + /// Сравнитель объектов + void CollectionSort(IComparer comparer); + } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ListGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ListGenericObjects.cs index c6b05a2..d62ddb9 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ListGenericObjects.cs @@ -40,14 +40,28 @@ public class ListGenericObjects : ICollectionGenericObjects if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); 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 ObjectIsEqualException(); + } + } if (Count == _maxCount) throw new CollectionOverflowException(Count); _collection.Add(obj); return 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 ObjectIsEqualException(); + } + } if (Count == _maxCount) throw new CollectionOverflowException(Count); if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); _collection.Insert(position, obj); @@ -67,4 +81,8 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs index d5d61b2..f3797c9 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,5 @@ -using ProjectWarmlyShip.Exceptions; +using ProjectWarmlyShip.Drawnings; +using ProjectWarmlyShip.Exceptions; namespace ProjectWarmlyShip.CollectionGenericObjects; @@ -45,8 +46,16 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[position] == null) throw new ObjectNotFoundException(position); return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + if (comparer != null) + { + foreach (T? item in _collection ) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningShip, item as DrawningShip)) + throw new ObjectIsEqualException(); + } + } int index = 0; while (index < _collection.Length) { @@ -59,8 +68,16 @@ public class MassiveGenericObjects : ICollectionGenericObjects } throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { + if (comparer != null) + { + foreach (T? item in _collection) + { + if ((comparer as IEqualityComparer).Equals(obj as DrawningShip, item as DrawningShip)) + throw new ObjectIsEqualException(); + } + } if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) { _collection[position] = obj; @@ -103,4 +120,8 @@ public class MassiveGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + void ICollectionGenericObjects.CollectionSort(IComparer comparer) + { + Array.Sort(_collection, comparer); + } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/StorageCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/StorageCollection.cs index cefc3c0..740bc2a 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/StorageCollection.cs @@ -9,17 +9,17 @@ public class StorageCollection /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Конструктор /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// /// Добавление коллекции в хранилище @@ -28,12 +28,13 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - if (_storages.ContainsKey(name)) return; + CollectionInfo collectionInfo = new CollectionInfo(name, collectionType, string.Empty); + if (_storages.ContainsKey(collectionInfo)) return; if (collectionType == CollectionType.None) return; else if (collectionType == CollectionType.Massive) - _storages[name] = new MassiveGenericObjects(); + _storages[collectionInfo] = new MassiveGenericObjects(); else if (collectionType == CollectionType.List) - _storages[name] = new ListGenericObjects(); + _storages[collectionInfo] = new ListGenericObjects(); } /// /// Удаление коллекции @@ -41,8 +42,9 @@ public class StorageCollection /// Название коллекции public void DelCollection(string name) { - if (_storages.ContainsKey(name)) - _storages.Remove(name); + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(collectionInfo)) + _storages.Remove(collectionInfo); } /// /// Доступ к коллекции @@ -53,8 +55,9 @@ public class StorageCollection { get { - if (_storages.ContainsKey(name)) - return _storages[name]; + CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); + if (_storages.ContainsKey(collectionInfo)) + return _storages[collectionInfo]; return null; } } @@ -87,7 +90,7 @@ public class StorageCollection using (StreamWriter writer = new StreamWriter(filename)) { writer.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair< CollectionInfo, ICollectionGenericObjects > value in _storages) { writer.Write(Environment.NewLine); if (value.Value.Count == 0) @@ -96,8 +99,6 @@ public class StorageCollection } writer.Write(value.Key); writer.Write(_separatorForKeyValue); - writer.Write(value.Value.GetCollectionType); - writer.Write(_separatorForKeyValue); writer.Write(value.Value.MaxCount); writer.Write(_separatorForKeyValue); foreach (T? item in value.Value.GetItems()) @@ -139,18 +140,20 @@ public class StorageCollection while ((strs = fs.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? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? + throw new Exception("Не удалось определить информацию коллекции: " + record[0]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.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?.CreateDrawningShip() is T ship) @@ -168,7 +171,7 @@ public class StorageCollection } } } - _storages.Add(record[0], collection); + _storages.Add(collectionInfo, collection); } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawiningShipEqutables.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawiningShipEqutables.cs new file mode 100644 index 0000000..34677d3 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawiningShipEqutables.cs @@ -0,0 +1,57 @@ +using ProjectWarmlyShip.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace ProjectWarmlyShip.Drawnings; + +public class DrawiningShipEqutables : IEqualityComparer +{ + public bool Equals(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + return false; + } + if (y == null || y.EntityShip == null) + { + return false; + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityShip.Speed != y.EntityShip.Speed) + { + return false; + } + if (x.EntityShip.Weight != y.EntityShip.Weight) + { + return false; + } + if (x.EntityShip.BodyColor != y.EntityShip.BodyColor) + { + return false; + } + if (x is DrawningWarmlyShip && y is DrawningWarmlyShip) + { + EntityWarmlyShip _x = (EntityWarmlyShip)x.EntityShip; + EntityWarmlyShip _y = (EntityWarmlyShip)x.EntityShip; + if (_x.AdditionalColor != _y.AdditionalColor) + { + return false; + } + if (_x.ShipPipes != _y.ShipPipes) + { + return false; + } + if (_x.FuelTank != _y.FuelTank) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningShip obj) + { + return obj.GetHashCode(); + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShipCompareByColor.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShipCompareByColor.cs new file mode 100644 index 0000000..e552e0e --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShipCompareByColor.cs @@ -0,0 +1,28 @@ +namespace ProjectWarmlyShip.Drawnings; + +public class DrawningShipCompareByColor : IComparer +{ + public int Compare(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + return 1; + } + + if (y == null || y.EntityShip == null) + { + return -1; + } + var bodycolorCompare = x.EntityShip.BodyColor.Name.CompareTo(y.EntityShip.BodyColor.Name); + if (bodycolorCompare != 0) + { + return bodycolorCompare; + } + var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShipCompareByType.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShipCompareByType.cs new file mode 100644 index 0000000..9b3a42e --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Drawnings/DrawningShipCompareByType.cs @@ -0,0 +1,29 @@ +namespace ProjectWarmlyShip.Drawnings; + +public class DrawningShipCompareByType : IComparer +{ + public int Compare(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + return 1; + } + + if (y == null || y.EntityShip == null) + { + return -1; + } + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + + var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Exceptions/ObjectIsEqualException.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Exceptions/ObjectIsEqualException.cs new file mode 100644 index 0000000..02c6b4f --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Exceptions/ObjectIsEqualException.cs @@ -0,0 +1,16 @@ +using System.Runtime.Serialization; + +namespace ProjectWarmlyShip.Exceptions; + +/// +/// Класс, описывающий ошибку переполнения коллекции +/// +[Serializable] +public class ObjectIsEqualException : ApplicationException +{ + public ObjectIsEqualException(int count) : base("В коллекции содержится равный элемент: " + count) { } + public ObjectIsEqualException() : base() { } + public ObjectIsEqualException(string message) : base(message) { } + public ObjectIsEqualException(string message, Exception exception) : base(message, exception) { } + protected ObjectIsEqualException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs index 9ecf501..f36849d 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs @@ -30,6 +30,8 @@ { groupBoxTools = new GroupBox(); panelCompanyTools = new Panel(); + buttonByColor = new Button(); + buttonSortByType = new Button(); buttonAddShip = new Button(); buttonRefresh = new Button(); maskedTextBox = new MaskedTextBox(); @@ -67,13 +69,15 @@ groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(887, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(146, 597); + groupBoxTools.Size = new Size(146, 600); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(buttonAddShip); panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Controls.Add(maskedTextBox); @@ -82,9 +86,33 @@ panelCompanyTools.Enabled = false; panelCompanyTools.Location = new Point(6, 333); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(200, 280); + panelCompanyTools.Size = new Size(200, 249); panelCompanyTools.TabIndex = 2; // + // buttonByColor + // + buttonByColor.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonByColor.Font = new Font("Segoe UI", 8.25F, FontStyle.Regular, GraphicsUnit.Point); + buttonByColor.Location = new Point(4, 211); + buttonByColor.Name = "buttonByColor"; + buttonByColor.Size = new Size(133, 29); + buttonByColor.TabIndex = 8; + buttonByColor.Text = "Сортировка по цвету"; + buttonByColor.TextAlign = ContentAlignment.MiddleLeft; + buttonByColor.UseVisualStyleBackColor = true; + buttonByColor.Click += buttonByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonSortByType.Location = new Point(4, 176); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(133, 29); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // buttonAddShip // buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; @@ -98,10 +126,9 @@ // // buttonRefresh // - buttonRefresh.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(3, 214); + buttonRefresh.Location = new Point(4, 144); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(128, 40); + buttonRefresh.Size = new Size(130, 26); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -109,7 +136,7 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(3, 93); + maskedTextBox.Location = new Point(3, 47); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Size = new Size(128, 23); @@ -119,9 +146,9 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(3, 168); + buttonGoToCheck.Location = new Point(4, 109); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(128, 40); + buttonGoToCheck.Size = new Size(128, 29); buttonGoToCheck.TabIndex = 5; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -130,9 +157,9 @@ // buttonRemoveShip // buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveShip.Location = new Point(3, 122); + buttonRemoveShip.Location = new Point(3, 76); buttonRemoveShip.Name = "buttonRemoveShip"; - buttonRemoveShip.Size = new Size(128, 40); + buttonRemoveShip.Size = new Size(128, 27); buttonRemoveShip.TabIndex = 4; buttonRemoveShip.Text = "Удаление"; buttonRemoveShip.UseVisualStyleBackColor = true; @@ -248,7 +275,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 24); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(887, 597); + pictureBox.Size = new Size(887, 600); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -296,7 +323,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1033, 621); + ClientSize = new Size(1033, 624); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); @@ -341,5 +368,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs index c0feba3..3bc9311 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -48,6 +48,11 @@ public partial class FormShipCollection : Form MessageBox.Show("Не удалось добавить объект"); _logger.LogError("Ошибка: {Message}", ex.Message); } + catch (ObjectIsEqualException ex) + { + MessageBox.Show("Не удалось добавить объект"); + _logger.LogError("Ошибка: {Message}", ex.Message); + } } private void buttonRemoveShip_Click(object sender, EventArgs e) { @@ -146,7 +151,7 @@ public partial class FormShipCollection : 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); @@ -170,8 +175,9 @@ public partial class FormShipCollection : Form RerfreshListBoxItems(); _logger.LogInformation("Коллекция: " + listBoxCollection.SelectedItem.ToString() + " удалена"); } - catch (Exception ex) { - _logger.LogError("Ошибка: {Message}", ex.Message); + catch (Exception ex) + { + _logger.LogError("Ошибка: {Message}", ex.Message); } } private void buttonCreateCompany_Click(object sender, EventArgs e) @@ -218,19 +224,37 @@ public partial class FormShipCollection : Form { if (openFileDialog.ShowDialog() == DialogResult.OK) { - try + try { _storageCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); RerfreshListBoxItems(); _logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogError("Ошибка: {Message}", ex.Message); } } } + private void buttonSortByType_Click(object sender, EventArgs e) + { + CompareShip(new DrawningShipCompareByType()); + } + private void buttonByColor_Click(object sender, EventArgs e) + { + CompareShip(new DrawningShipCompareByColor()); + } + private void CompareShip(IComparer comparer) + { + if (_company == null) + { + return; + } + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } + } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx index 8b1dfa1..565d49a 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx @@ -126,4 +126,7 @@ 261, 17 + + 68 + \ No newline at end of file