diff --git a/AircraftCarrier/AircraftCarrier/AircraftCompareByColor.cs b/AircraftCarrier/AircraftCarrier/AircraftCompareByColor.cs new file mode 100644 index 0000000..b6a5ce5 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/AircraftCompareByColor.cs @@ -0,0 +1,34 @@ +using AircraftCarrier.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftCarrier.Drawnings; + +namespace AircraftCarrier.Generics +{ + internal class AircraftCompareByColor : IComparer + { + public int Compare(DrawningAircraft? x, DrawningAircraft? y) + { + if (x == null || x.EntityAircraft == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityAircraft == null) + throw new ArgumentNullException(nameof(y)); + if (x.EntityAircraft.BodyColor.Name != y.EntityAircraft.BodyColor.Name) + { + return x.EntityAircraft.BodyColor.Name.CompareTo(y.EntityAircraft.BodyColor.Name); + } + + var speedCompare = x.EntityAircraft.Speed.CompareTo(y.EntityAircraft.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityAircraft.Weight.CompareTo(y.EntityAircraft.Weight); + } + + } +} diff --git a/AircraftCarrier/AircraftCarrier/AircraftCompareByType .cs b/AircraftCarrier/AircraftCarrier/AircraftCompareByType .cs new file mode 100644 index 0000000..7f89689 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/AircraftCompareByType .cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftCarrier.DrawningObjects; +using AircraftCarrier.Drawnings; + +namespace AircraftCarrier.Generics +{ + internal class AircraftCompareByType : IComparer + { + public int Compare(DrawningAircraft? x, DrawningAircraft? y) + { + if (x == null || x.EntityAircraft == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAircraft == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityAircraft.Speed.CompareTo(y.EntityAircraft.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityAircraft.Weight.CompareTo(y.EntityAircraft.Weight); + } + + } +} diff --git a/AircraftCarrier/AircraftCarrier/AircraftsCollectionInfo.cs b/AircraftCarrier/AircraftCarrier/AircraftsCollectionInfo.cs new file mode 100644 index 0000000..d984684 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/AircraftsCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier.Generics +{ + internal class AircraftsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public AircraftsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(AircraftsCollectionInfo? other) + { + if (ReferenceEquals(other, null)) + return false; + + return Name.Equals(other.Name); + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs b/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs index a6dea35..23a4e2e 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs +++ b/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs @@ -12,6 +12,8 @@ namespace AircraftCarrier.Generics where T : DrawningAircraft where U : IMoveableObject { + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// Получение объектов коллекции public IEnumerable GetCars => _collection.GetAircrafts(); /// Ширина окна прорисовки @@ -41,7 +43,7 @@ namespace AircraftCarrier.Generics { return -1; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawiningAircraftEqutables()); } /// Перегрузка оператора вычитания public static bool operator -(AircraftsGenericCollection collect, int pos) diff --git a/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs b/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs index 1512970..a385583 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs +++ b/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs @@ -12,10 +12,10 @@ namespace AircraftCarrier.Generics internal class AircraftsGenericStorage { /// Словарь (хранилище) - readonly Dictionary> _AircraftStorages; /// Возвращение списка названий наборов - public List Keys => _AircraftStorages.Keys.ToList(); + public List Keys => _AircraftStorages.Keys.ToList(); /// Ширина окна отрисовки private readonly int _pictureWidth; /// Высота окна отрисовки @@ -29,7 +29,7 @@ namespace AircraftCarrier.Generics /// Конструктор public AircraftsGenericStorage(int pictureWidth, int pictureHeight) { - _AircraftStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -37,17 +37,17 @@ namespace AircraftCarrier.Generics /// Добавление набора public void AddSet(string name) { - if (!_AircraftStorages.ContainsKey(name)) + if (!_AircraftStorages.ContainsKey(new AircraftsCollectionInfo(name, string.Empty))) { - _AircraftStorages.Add(name, new AircraftsGenericCollection(_pictureWidth, _pictureHeight)); + _AircraftStorages.Add(new AircraftsCollectionInfo(name, string.Empty), new AircraftsGenericCollection(_pictureWidth, _pictureHeight)); } } /// Удаление набора public void DelSet(string name) { - if (_AircraftStorages.ContainsKey(name)) + if (_AircraftStorages.ContainsKey(new AircraftsCollectionInfo(name, string.Empty))) { - _AircraftStorages.Remove(name); + _AircraftStorages.Remove(new AircraftsCollectionInfo(name, string.Empty)); } } /// Доступ к набору @@ -56,9 +56,10 @@ namespace AircraftCarrier.Generics { get { - if (_AircraftStorages.ContainsKey(ind)) + AircraftsCollectionInfo indObj = new AircraftsCollectionInfo(ind, string.Empty); + if (_AircraftStorages.ContainsKey(indObj)) { - return _AircraftStorages[ind]; + return _AircraftStorages[indObj]; } return null; } @@ -71,7 +72,7 @@ namespace AircraftCarrier.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _AircraftStorages) { StringBuilder records = new(); @@ -79,7 +80,7 @@ namespace AircraftCarrier.Generics { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -131,7 +132,7 @@ namespace AircraftCarrier.Generics } } } - _AircraftStorages.Add(record[0], collection); + _AircraftStorages.Add(new AircraftsCollectionInfo(record[0], string.Empty), collection); } } } diff --git a/AircraftCarrier/AircraftCarrier/DrawiningAircraftEqutables .cs b/AircraftCarrier/AircraftCarrier/DrawiningAircraftEqutables .cs new file mode 100644 index 0000000..911a32f --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/DrawiningAircraftEqutables .cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftCarrier.Drawnings; +using AircraftCarrier.Entities; +using System.Diagnostics.CodeAnalysis; +using AircraftCarrier.DrawningObjects; + +namespace AircraftCarrier.Generics +{ + internal class DrawiningAircraftEqutables : IEqualityComparer + { + public bool Equals(DrawningAircraft? x, DrawningAircraft? y) + { + if (x == null || x.EntityAircraft == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAircraft == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityAircraft.Speed != y.EntityAircraft.Speed) + { + return false; + } + if (x.EntityAircraft.Weight != y.EntityAircraft.Weight) + { + return false; + } + if (x.EntityAircraft.BodyColor != y.EntityAircraft.BodyColor) + { + return false; + } + if (x is DrawningAircraftCarrier && y is DrawningAircraftCarrier) + { + EntityAircraftCarrier EntityX = (EntityAircraftCarrier)x.EntityAircraft; + EntityAircraftCarrier EntityY = (EntityAircraftCarrier)y.EntityAircraft; + if (EntityX.Runway != EntityY.Runway) + return false; + if (EntityX.Cabin != EntityY.Cabin) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningAircraft obj) + { + return obj.GetHashCode(); + } + + } +} diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs index fd23b43..8b7acd0 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs @@ -47,6 +47,8 @@ loadToolStripMenuItem = new ToolStripMenuItem(); saveFileDialog = new SaveFileDialog(); openFileDialog = new OpenFileDialog(); + 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(labelTools); PanelTools.Controls.Add(PanelSets); PanelTools.Controls.Add(ButtonRefreshCollection); @@ -144,7 +148,7 @@ // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(36, 572); + ButtonRefreshCollection.Location = new Point(36, 633); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(168, 41); ButtonRefreshCollection.TabIndex = 3; @@ -154,7 +158,7 @@ // // ButtonRemoveAircraft // - ButtonRemoveAircraft.Location = new Point(36, 497); + ButtonRemoveAircraft.Location = new Point(36, 586); ButtonRemoveAircraft.Name = "ButtonRemoveAircraft"; ButtonRemoveAircraft.Size = new Size(168, 41); ButtonRemoveAircraft.TabIndex = 2; @@ -164,7 +168,7 @@ // // MaskedTextBoxNumber // - MaskedTextBoxNumber.Location = new Point(57, 438); + MaskedTextBoxNumber.Location = new Point(57, 553); MaskedTextBoxNumber.Name = "MaskedTextBoxNumber"; MaskedTextBoxNumber.Size = new Size(125, 27); MaskedTextBoxNumber.TabIndex = 1; @@ -172,7 +176,7 @@ // // ButtonAddAircraft // - ButtonAddAircraft.Location = new Point(36, 391); + ButtonAddAircraft.Location = new Point(36, 506); ButtonAddAircraft.Name = "ButtonAddAircraft"; ButtonAddAircraft.Size = new Size(168, 41); ButtonAddAircraft.TabIndex = 0; @@ -200,14 +204,14 @@ // saveToolStripMenuItem // saveToolStripMenuItem.Name = "saveToolStripMenuItem"; - saveToolStripMenuItem.Size = new Size(224, 26); + saveToolStripMenuItem.Size = new Size(166, 26); saveToolStripMenuItem.Text = "Сохранить"; saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // loadToolStripMenuItem // loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - loadToolStripMenuItem.Size = new Size(224, 26); + loadToolStripMenuItem.Size = new Size(166, 26); loadToolStripMenuItem.Text = "Загрузка"; loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -220,6 +224,26 @@ openFileDialog.FileName = "openFileDialog1"; openFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(36, 393); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(168, 41); + ButtonSortByType.TabIndex = 6; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(36, 450); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(168, 41); + ButtonSortByColor.TabIndex = 7; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // // FormAircraftCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -262,5 +286,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/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs index 7793eca..20ea46b 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs @@ -33,7 +33,7 @@ namespace AircraftCarrier 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)) { @@ -114,11 +114,16 @@ namespace AircraftCarrier MessageBox.Show("Неудачная попытка добавить объект"); } } - catch(ApplicationException ex) + catch (StorageOverflowException ex) { MessageBox.Show(ex.Message); _logger.LogWarning($"Неудачная попытка добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {ex.Message}"); + } } private void ButtonRemoveAircraft_Click(object sender, EventArgs e) { @@ -149,7 +154,7 @@ namespace AircraftCarrier } else { - MessageBox.Show("Неудачная попытка удалить объект"); + MessageBox.Show("Неудачная попытка удалить объект"); } } catch (AircraftNotFoundException ex) @@ -211,5 +216,24 @@ namespace AircraftCarrier } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAircrafts(new AircraftCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAircrafts(new AircraftCompareByColor()); + private void CompareAircrafts(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.ShowAircrafts(); + } } } diff --git a/AircraftCarrier/AircraftCarrier/SetGeneric.cs b/AircraftCarrier/AircraftCarrier/SetGeneric.cs index 35f5291..65e8e5f 100644 --- a/AircraftCarrier/AircraftCarrier/SetGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetGeneric.cs @@ -21,20 +21,27 @@ namespace AircraftCarrier.Generics _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// Добавление объекта в набор - public int Insert(T Aircraft) + public int Insert(T aircraft, IEqualityComparer? equal = null) { - return Insert(Aircraft, 0); + if (_places.Count == _maxCount) + throw new StorageOverflowException(_maxCount); + return Insert(aircraft, 0, equal); } /// Добавление объекта в набор на конкретную позицию - public int Insert(T Aircraft, int position) + public int Insert(T aircraft, int position, IEqualityComparer? equal= null) { if (position < 0 || position > Count) throw new AircraftNotFoundException("Impossible to insert"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - - _places.Insert(position, Aircraft); + if (equal != null) + { + if (_places.Contains(aircraft, equal)) + throw new ArgumentException(nameof(aircraft)); + } + _places.Insert(position, aircraft); return position; } /// Удаление объекта из набора с конкретной позиции