diff --git a/AirFighter/AirFighter/AirplaneCompareByColor.cs b/AirFighter/AirFighter/AirplaneCompareByColor.cs new file mode 100644 index 0000000..9602b7b --- /dev/null +++ b/AirFighter/AirFighter/AirplaneCompareByColor.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirFighter.DrawningObjects; +using AirFighter.Drawnings; + + +namespace AirFighter.Generics +{ + internal class AirplaneCompareByColor : 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.EntityAirplane.BodyColor.Name != y.EntityAirplane.BodyColor.Name) + { + return x.EntityAirplane.BodyColor.Name.CompareTo(y.EntityAirplane.BodyColor.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/AirFighter/AirFighter/AirplaneCompareByType.cs b/AirFighter/AirFighter/AirplaneCompareByType.cs new file mode 100644 index 0000000..37da086 --- /dev/null +++ b/AirFighter/AirFighter/AirplaneCompareByType.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirFighter.DrawningObjects; +using AirFighter.Drawnings; + + +namespace AirFighter.Generics +{ + internal class AirplaneCompareByType : 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/AirFighter/AirFighter/AirplanesCollectionInfo.cs b/AirFighter/AirFighter/AirplanesCollectionInfo.cs new file mode 100644 index 0000000..3ad6eea --- /dev/null +++ b/AirFighter/AirFighter/AirplanesCollectionInfo.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter.Generics +{ + internal class AirplanesCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public AirplanesCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(AirplanesCollectionInfo? other) + { + return Name == other.Name; + } + + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/AirFighter/AirFighter/AirplanesGenericCollection.cs b/AirFighter/AirFighter/AirplanesGenericCollection.cs index 1e24b77..f8593e1 100644 --- a/AirFighter/AirFighter/AirplanesGenericCollection.cs +++ b/AirFighter/AirFighter/AirplanesGenericCollection.cs @@ -29,6 +29,8 @@ namespace AirFighter.Generics // Набор объектов private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + // Получение объектов коллекции public IEnumerable GetAirplanes => _collection.GetAirplanes(); @@ -48,7 +50,7 @@ namespace AirFighter.Generics { return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawiningAirplaneEqutables()); } // Перегрузка оператора вычитания diff --git a/AirFighter/AirFighter/AirplanesGenericStorage.cs b/AirFighter/AirFighter/AirplanesGenericStorage.cs index c763a44..31db2ff 100644 --- a/AirFighter/AirFighter/AirplanesGenericStorage.cs +++ b/AirFighter/AirFighter/AirplanesGenericStorage.cs @@ -15,11 +15,11 @@ namespace AirFighter.Generics internal class AirplanesGenericStorage { // Словарь (хранилище) - readonly Dictionary> _AirplaneStorages; // Возвращение списка названий наборов - public List Keys => _AirplaneStorages.Keys.ToList(); + public List Keys => _AirplaneStorages.Keys.ToList(); // Ширина окна отрисовки private readonly int _pictureWidth; @@ -39,7 +39,7 @@ namespace AirFighter.Generics // Конструктор public AirplanesGenericStorage(int pictureWidth, int pictureHeight) { - _AirplaneStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -48,16 +48,17 @@ namespace AirFighter.Generics // Добавление набора public void AddSet(string name) { - if (!_AirplaneStorages.ContainsKey(name)) - _AirplaneStorages.Add(name, new AirplanesGenericCollection(_pictureWidth, _pictureHeight)); } // Удаление набора public void DelSet(string name) { - if (_AirplaneStorages.ContainsKey(name)) - _AirplaneStorages.Remove(name); + if (_AirplaneStorages.ContainsKey(new AirplanesCollectionInfo(name, string.Empty))) + _AirplaneStorages.Remove(new AirplanesCollectionInfo(name, string.Empty)); } // Доступ к набору @@ -66,8 +67,10 @@ namespace AirFighter.Generics { get { - if (_AirplaneStorages.ContainsKey(ind)) - return _AirplaneStorages[ind]; + AirplanesCollectionInfo indObj = new AirplanesCollectionInfo(ind, string.Empty); + if (_AirplaneStorages.ContainsKey(indObj)) + return _AirplaneStorages[indObj]; + return null; } } @@ -80,7 +83,7 @@ namespace AirFighter.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _AirplaneStorages) { @@ -89,7 +92,7 @@ namespace AirFighter.Generics { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -138,7 +141,7 @@ namespace AirFighter.Generics } } } - _AirplaneStorages.Add(record[0], collection); + _AirplaneStorages.Add(new AirplanesCollectionInfo(record[0], string.Empty), collection); } } } diff --git a/AirFighter/AirFighter/DrawiningAirplaneEqutables .cs b/AirFighter/AirFighter/DrawiningAirplaneEqutables .cs new file mode 100644 index 0000000..393895c --- /dev/null +++ b/AirFighter/AirFighter/DrawiningAirplaneEqutables .cs @@ -0,0 +1,62 @@ +using AirFighter.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirFighter.Drawnings; +using AirFighter.Entities; +using System.Diagnostics.CodeAnalysis; +using AirFighter.DrawningObject; + +namespace AirFighter.Generics + +{ + internal class DrawiningAirplaneEqutables : 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 DrawningAirFighter && y is DrawningAirFighter) + { + EntityAirFighter EntityX = (EntityAirFighter)x.EntityAirplane; + EntityAirFighter EntityY = (EntityAirFighter)y.EntityAirplane; + if (EntityX.Rockets != EntityY.Rockets) + return false; + if (EntityX.Wings != EntityY.Wings) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningAirplane obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/AirFighter/AirFighter/FormAirPlaneCollection.Designer.cs b/AirFighter/AirFighter/FormAirPlaneCollection.Designer.cs index 35200fb..32cf240 100644 --- a/AirFighter/AirFighter/FormAirPlaneCollection.Designer.cs +++ b/AirFighter/AirFighter/FormAirPlaneCollection.Designer.cs @@ -47,6 +47,8 @@ loadToolStripMenuItem = new ToolStripMenuItem(); openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); + ButtonSortByType = new Button(); + ButtonSortByColor = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); panelTools.SuspendLayout(); panelSets.SuspendLayout(); @@ -64,6 +66,8 @@ // panelTools // panelTools.BorderStyle = BorderStyle.FixedSingle; + panelTools.Controls.Add(ButtonSortByColor); + panelTools.Controls.Add(ButtonSortByType); panelTools.Controls.Add(panelSets); panelTools.Controls.Add(ButtonAddAirplane); panelTools.Controls.Add(ButtonRefreshCollection); @@ -134,7 +138,7 @@ // // ButtonAddAirplane // - ButtonAddAirplane.Location = new Point(12, 381); + ButtonAddAirplane.Location = new Point(13, 433); ButtonAddAirplane.Name = "ButtonAddAirplane"; ButtonAddAirplane.Size = new Size(174, 31); ButtonAddAirplane.TabIndex = 7; @@ -144,7 +148,7 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(12, 532); + ButtonRefreshCollection.Location = new Point(12, 567); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(174, 33); ButtonRefreshCollection.TabIndex = 6; @@ -154,7 +158,7 @@ // // buttonRemoveAirplane // - buttonRemoveAirplane.Location = new Point(12, 480); + buttonRemoveAirplane.Location = new Point(13, 522); buttonRemoveAirplane.Name = "buttonRemoveAirplane"; buttonRemoveAirplane.Size = new Size(174, 30); buttonRemoveAirplane.TabIndex = 5; @@ -164,7 +168,7 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(12, 432); + maskedTextBoxNumber.Location = new Point(13, 480); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(174, 27); maskedTextBoxNumber.TabIndex = 4; @@ -199,14 +203,14 @@ // saveToolStripMenuItem // saveToolStripMenuItem.Name = "saveToolStripMenuItem"; - saveToolStripMenuItem.Size = new Size(224, 26); + saveToolStripMenuItem.Size = new Size(177, 26); saveToolStripMenuItem.Text = "Сохранение"; saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // loadToolStripMenuItem // loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - loadToolStripMenuItem.Size = new Size(224, 26); + loadToolStripMenuItem.Size = new Size(177, 26); loadToolStripMenuItem.Text = "Загрузка"; loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -219,6 +223,26 @@ // saveFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(12, 343); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(171, 29); + ButtonSortByType.TabIndex = 9; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(12, 388); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(174, 29); + ButtonSortByColor.TabIndex = 10; + ButtonSortByColor.Text = "сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // // FormAirPlaneCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -263,5 +287,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/AirFighter/AirFighter/FormAirPlaneCollection.cs b/AirFighter/AirFighter/FormAirPlaneCollection.cs index 8655304..4e3f23d 100644 --- a/AirFighter/AirFighter/FormAirPlaneCollection.cs +++ b/AirFighter/AirFighter/FormAirPlaneCollection.cs @@ -39,7 +39,7 @@ namespace AirFighter listBoxStorage.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorage.Items.Add(_storage.Keys[i]); + listBoxStorage.Items.Add(_storage.Keys[i].Name); } if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count)) @@ -86,7 +86,7 @@ namespace AirFighter } string name = listBoxStorage.SelectedItem.ToString() ?? string.Empty; - if (MessageBox.Show($"удалить набор {name}?", "удаление", + if (MessageBox.Show($"удалить набор {name}?", "удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _storage.DelSet(name); @@ -124,11 +124,17 @@ namespace AirFighter } } - catch (ApplicationException ex) + catch (StorageOverflowException ex) { MessageBox.Show(ex.Message); - _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); + _logger.LogWarning($"Неудачная попытка добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {ex.Message}"); + } + } // Добавление объекта в набор private void ButtonAddAirplane_Click(object sender, EventArgs e) @@ -164,7 +170,7 @@ namespace AirFighter } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - + try { if (obj - pos) @@ -206,7 +212,7 @@ namespace AirFighter if (saveFileDialog.ShowDialog() == DialogResult.OK) { try - { + { _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "результат", MessageBoxButtons.OK, MessageBoxIcon.Information); @@ -241,5 +247,25 @@ namespace AirFighter } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAirplanes(new AirplaneCompareByColor()); + + private void CompareAirplanes(IComparer comparer) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowAirplanes(); + } } } diff --git a/AirFighter/AirFighter/FormAirPlaneCollection.resx b/AirFighter/AirFighter/FormAirPlaneCollection.resx index 4132eb3..b6eeb14 100644 --- a/AirFighter/AirFighter/FormAirPlaneCollection.resx +++ b/AirFighter/AirFighter/FormAirPlaneCollection.resx @@ -126,4 +126,7 @@ 315, 17 + + 33 + \ No newline at end of file diff --git a/AirFighter/AirFighter/SetGeneric.cs b/AirFighter/AirFighter/SetGeneric.cs index cf0b7b8..2632fba 100644 --- a/AirFighter/AirFighter/SetGeneric.cs +++ b/AirFighter/AirFighter/SetGeneric.cs @@ -26,20 +26,29 @@ namespace AirFighter.Generics _places = new List(count); } + // сортировка набора + public void SortSet(IComparer comparer) => _places.Sort(comparer); + // Добавление объекта в набор - public int Insert(T airplane) + public int Insert(T airplane, IEqualityComparer? equal = null) { - return Insert(airplane, 0); + if (_places.Count == _maxCount) + throw new StorageOverflowException(_maxCount); + return Insert(airplane, 0, equal); } // Добавление объекта в набор на конкретную позицию - public int Insert(T airplane, int position) + public int Insert(T airplane, int position, IEqualityComparer? equal= null) { if (position < 0 || position > Count) throw new AirplaneNotFoundException("Вставка невозможна"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - + if (equal != null) + { + if (_places.Contains(airplane, equal)) + throw new ArgumentException(nameof(airplane)); + } _places.Insert(position, airplane); return position; }