diff --git a/AirBomber/AirBomber/DrawningPlanesEquatables.cs b/AirBomber/AirBomber/DrawningPlanesEquatables.cs new file mode 100644 index 0000000..94d9c88 --- /dev/null +++ b/AirBomber/AirBomber/DrawningPlanesEquatables.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber; + +namespace AirBomber +{ + internal class DrawningPlanesEquatables: IEqualityComparer + { + public bool Equals(DrawningAirPlane? x, DrawningAirPlane? y) + { + if (x == null || x.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityAirPlane.Speed != y.EntityAirPlane.Speed) + { + return false; + } + if (x.EntityAirPlane.Weight != y.EntityAirPlane.Weight) + { + return false; + } + if (x.EntityAirPlane.BodyColor != y.EntityAirPlane.BodyColor) + { + return false; + } + if (x is DrawningAirBomber && y is DrawningAirBomber) + { + EntityAirBomber xAirBomber = (EntityAirBomber)x.EntityAirPlane; + EntityAirBomber yAirBomber = (EntityAirBomber)y.EntityAirPlane; + + if (xAirBomber.Bombs != yAirBomber.Bombs) + return false; + + if (xAirBomber.FuelTanks != yAirBomber.FuelTanks) + return false; + + if (xAirBomber.AdditionalColor != yAirBomber.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningAirPlane obj) + { + return obj.GetHashCode(); + } + } +} + diff --git a/AirBomber/AirBomber/FormPlaneCollection.Designer.cs b/AirBomber/AirBomber/FormPlaneCollection.Designer.cs index dfb5dfb..d5ca21c 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.Designer.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.Designer.cs @@ -34,6 +34,8 @@ buttonRefreshCollection = new Button(); maskedTextBoxNumber = new MaskedTextBox(); groupBoxTools = new GroupBox(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxStorages = new GroupBox(); textBoxStorageName = new TextBox(); buttonDelObject = new Button(); @@ -63,9 +65,9 @@ // // buttonAddPlane // - buttonAddPlane.Location = new Point(76, 417); + buttonAddPlane.Location = new Point(49, 525); buttonAddPlane.Name = "buttonAddPlane"; - buttonAddPlane.Size = new Size(160, 64); + buttonAddPlane.Size = new Size(209, 44); buttonAddPlane.TabIndex = 0; buttonAddPlane.Text = "Добавить самолет"; buttonAddPlane.UseVisualStyleBackColor = true; @@ -73,7 +75,7 @@ // // buttonRemovePlane // - buttonRemovePlane.Location = new Point(76, 549); + buttonRemovePlane.Location = new Point(76, 612); buttonRemovePlane.Name = "buttonRemovePlane"; buttonRemovePlane.Size = new Size(160, 60); buttonRemovePlane.TabIndex = 1; @@ -83,9 +85,9 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(76, 648); + buttonRefreshCollection.Location = new Point(49, 678); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(160, 86); + buttonRefreshCollection.Size = new Size(209, 56); buttonRefreshCollection.TabIndex = 2; buttonRefreshCollection.Text = "Обновить коллекцию"; buttonRefreshCollection.UseVisualStyleBackColor = true; @@ -93,7 +95,7 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(76, 501); + maskedTextBoxNumber.Location = new Point(76, 575); maskedTextBoxNumber.Mask = "00"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(160, 31); @@ -102,6 +104,8 @@ // // groupBoxTools // + groupBoxTools.Controls.Add(buttonSortByColor); + groupBoxTools.Controls.Add(buttonSortByType); groupBoxTools.Controls.Add(groupBoxStorages); groupBoxTools.Controls.Add(buttonAddPlane); groupBoxTools.Controls.Add(buttonRefreshCollection); @@ -114,6 +118,26 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(49, 469); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(209, 48); + buttonSortByColor.TabIndex = 6; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(49, 415); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(209, 48); + buttonSortByType.TabIndex = 5; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // // groupBoxStorages // groupBoxStorages.Controls.Add(textBoxStorageName); @@ -184,14 +208,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(270, 34); + SaveToolStripMenuItem.Size = new Size(212, 34); SaveToolStripMenuItem.Text = "Сохранение"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(270, 34); + LoadToolStripMenuItem.Size = new Size(212, 34); LoadToolStripMenuItem.Text = "Загрузка"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -245,5 +269,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormPlaneCollection.cs b/AirBomber/AirBomber/FormPlaneCollection.cs index 15fd2e7..4706d4f 100644 --- a/AirBomber/AirBomber/FormPlaneCollection.cs +++ b/AirBomber/AirBomber/FormPlaneCollection.cs @@ -94,6 +94,7 @@ namespace AirBomber MessageBox.Show(ex.Message); _logger.LogWarning($"Объект не добавлен в набор {listBoxStorages.SelectedItem.ToString()}"); } + } /// /// Удаление объекта из набора @@ -134,7 +135,7 @@ namespace AirBomber { MessageBox.Show(ex.Message); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show("Неверный ввод"); _logger.LogWarning("Неверный ввод"); @@ -228,12 +229,42 @@ namespace AirBomber _logger.LogInformation("Загрузка прошла успешно"); ReloadObjects(); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning($"Не загрузилось: {ex.Message}"); } } } + /// + /// Сортировка по типу + /// + /// + /// + private void buttonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void buttonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor()); + /// + /// Сортировка по сравнителю + /// + /// + private void ComparePlanes(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.ShowPlanes(); + } } } diff --git a/AirBomber/AirBomber/PlaneCollectionInfo.cs b/AirBomber/AirBomber/PlaneCollectionInfo.cs new file mode 100644 index 0000000..7dbb103 --- /dev/null +++ b/AirBomber/AirBomber/PlaneCollectionInfo.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class PlaneCollectionInfo: IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public PlaneCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(PlaneCollectionInfo other) + { + if (Name != other?.Name) + return false; + return true; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + public override string ToString() + { + return Name; + } + } +} diff --git a/AirBomber/AirBomber/PlaneCompareByColor.cs b/AirBomber/AirBomber/PlaneCompareByColor.cs new file mode 100644 index 0000000..bbfc64c --- /dev/null +++ b/AirBomber/AirBomber/PlaneCompareByColor.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber; + +namespace AirBomber +{ + internal class PlaneCompareByColor: IComparer + { + public int Compare(DrawningAirPlane? x, DrawningAirPlane? y) + { + if (x == null || x.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + + if (y == null || y.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + + var bodyColorCompare = x.EntityAirPlane.BodyColor.Name.CompareTo(y.EntityAirPlane.BodyColor.Name); + + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + + if (x.EntityAirPlane is EntityAirBomber xEntitySailCatamaran && y.EntityAirPlane is EntityAirBomber yEntitySailCatamaran) + { + var BodyColorCompare = xEntitySailCatamaran.BodyColor.Name.CompareTo(yEntitySailCatamaran.BodyColor.Name); + if (BodyColorCompare != 0) + { + return BodyColorCompare; + } + var AdditionalColorCompare = xEntitySailCatamaran.AdditionalColor.Name.CompareTo(yEntitySailCatamaran.AdditionalColor.Name); + if (AdditionalColorCompare != 0) + { + return AdditionalColorCompare; + } + } + + var speedCompare = x.EntityAirPlane.Speed.CompareTo(y.EntityAirPlane.Speed); + + if (speedCompare != 0) + { + return speedCompare; + } + + return x.EntityAirPlane.Weight.CompareTo(y.EntityAirPlane.Weight); + } + } +} + diff --git a/AirBomber/AirBomber/PlaneCompareByType.cs b/AirBomber/AirBomber/PlaneCompareByType.cs new file mode 100644 index 0000000..7b64d8e --- /dev/null +++ b/AirBomber/AirBomber/PlaneCompareByType.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBomber; + +namespace AirBomber +{ + internal class PlaneCompareByType: IComparer + { + public int Compare(DrawningAirPlane? x, DrawningAirPlane? y) + { + if (x == null || x.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + + if (y == null || y.EntityAirPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + + var speedCompare = x.EntityAirPlane.Speed.CompareTo(y.EntityAirPlane.Speed); + + if (speedCompare != 0) + { + return speedCompare; + } + + return x.EntityAirPlane.Weight.CompareTo(y.EntityAirPlane.Weight); + } + } +} diff --git a/AirBomber/AirBomber/PlanesGenericCollection.cs b/AirBomber/AirBomber/PlanesGenericCollection.cs index 5c38d3c..74f7279 100644 --- a/AirBomber/AirBomber/PlanesGenericCollection.cs +++ b/AirBomber/AirBomber/PlanesGenericCollection.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using AirBomber; namespace AirBomber { @@ -31,6 +32,11 @@ namespace AirBomber /// private readonly SetGeneric _collection; /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Конструктор /// /// @@ -49,13 +55,13 @@ namespace AirBomber /// /// /// - public static bool operator +(PlanesGenericCollection collect, T obj) + public static int operator +(PlanesGenericCollection collect, T obj) { if (obj == null) { - return false; + return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawningPlanesEquatables()); } /// /// Перегрузка оператора вычитания diff --git a/AirBomber/AirBomber/PlanesGenericStorage.cs b/AirBomber/AirBomber/PlanesGenericStorage.cs index 40f64d7..72e642d 100644 --- a/AirBomber/AirBomber/PlanesGenericStorage.cs +++ b/AirBomber/AirBomber/PlanesGenericStorage.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace AirBomber { @@ -12,11 +13,11 @@ namespace AirBomber /// /// Словарь (хранилище) /// - readonly Dictionary> _planeStorages; + readonly Dictionary> _planeStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _planeStorages.Keys.ToList(); + public List Keys => _planeStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -44,7 +45,7 @@ namespace AirBomber /// public PlanesGenericStorage(int pictureWidth, int pictureHeight) { - _planeStorages = new Dictionary>(); + _planeStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -55,9 +56,10 @@ namespace AirBomber public void AddSet(string name) { // TODO Прописать логику для добавления DONE - if (!_planeStorages.ContainsKey(name)) + if(!_planeStorages.ContainsKey(new PlaneCollectionInfo(name, string.Empty))) { - _planeStorages.Add(name, new PlanesGenericCollection(_pictureWidth, _pictureHeight)); + _planeStorages.Add(new PlaneCollectionInfo(name, string.Empty), + new PlanesGenericCollection(_pictureWidth, _pictureHeight)); } } /// @@ -67,10 +69,11 @@ namespace AirBomber public void DelSet(string name) { // TODO Прописать логику для удаления DONE - if (_planeStorages.ContainsKey(name)) + if (!_planeStorages.ContainsKey(new PlaneCollectionInfo(name, string.Empty))) { - _planeStorages.Remove(name); + return; } + _planeStorages.Remove(new PlaneCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -83,9 +86,9 @@ namespace AirBomber get { // TODO Продумать логику получения набора DONE - if (_planeStorages.ContainsKey(ind)) + if (_planeStorages.ContainsKey(new PlaneCollectionInfo(ind, string.Empty))) { - return _planeStorages[ind]; + return _planeStorages[new PlaneCollectionInfo(ind, string.Empty)]; } return null; } @@ -102,7 +105,7 @@ namespace AirBomber File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair>record in _planeStorages) + foreach (KeyValuePair>record in _planeStorages) { StringBuilder records = new(); foreach (DrawningAirPlane? elem in record.Value.GetPlanes) @@ -169,7 +172,7 @@ namespace AirBomber DrawningAirPlane? plane = elem?.CreateDrawningAirPlane(_separatorForObject, _pictureWidth, _pictureHeight); if (plane != null) { - if (!(collection + plane)) + if ((collection + plane) == -1) { try { @@ -186,7 +189,7 @@ namespace AirBomber } } } - _planeStorages.Add(record[0], collection); + _planeStorages.Add(new PlaneCollectionInfo(record[0], string.Empty), collection); } return true; } diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs index 0b1c8bd..2565729 100644 --- a/AirBomber/AirBomber/SetGeneric.cs +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -31,13 +31,24 @@ namespace AirBomber _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемый самолет /// - public bool Insert(T plane) + public int Insert(T plane, IEqualityComparer? equal = null) { + if (equal != null) + { + foreach (var secondLoco in _places) + { + if (equal.Equals(plane, secondLoco)) + { + throw new Exception("Такой объект уже есть в коллекции"); + } + } + } return Insert(plane, 0); } /// @@ -46,15 +57,23 @@ namespace AirBomber /// Добавляемый самолет /// Позиция /// - public bool Insert(T plane, int position) + public int Insert(T plane, int position, IEqualityComparer? equal = null) { - if (position < 0 || position >= _maxCount) - throw new StorageOverflowException("Невозможно добавить"); - - if (Count >= _maxCount) + if (_places.Count >= _maxCount) throw new StorageOverflowException(_maxCount); - _places.Insert(0, plane); - return true; + + if (position < 0 || position >= _maxCount) + { + return -1; + } + + if (equal != null) + { + if (_places.Contains(plane, equal)) + throw new ArgumentException(("Такой объект уже есть в коллекции")); + } + _places.Insert(position, plane); + return position; } /// /// Удаление объекта из набора с конкретной позиции