From 5ca46b05360f2e8afbab7c46586a24508d4081aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D0=B1=D0=B5=D0=B5=D0=B2=20=D0=90=D0=BB=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Mon, 25 Dec 2023 16:58:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=208=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormAircraftGunCollection.Designer.cs | 40 +++++++++-- .../FormAircraftGunCollection.cs | 26 ++++++- .../Generics/AircraftGunCompareByColor.cs | 43 ++++++++++++ .../Generics/AircraftGunCompareByType.cs | 34 ++++++++++ .../Generics/AntiAircraftGunCollectionInfo.cs | 41 ++++++++++++ .../AntiAircraftGunsGenericCollection.cs | 4 +- .../AntiAircraftGunsGenericStorage.cs | 23 ++++--- .../Generics/DrawningAircraftGunEqutables.cs | 67 +++++++++++++++++++ .../AntiAircraftGun/Generics/SetGeneric.cs | 18 ++++- 9 files changed, 273 insertions(+), 23 deletions(-) create mode 100644 AntiAircraftGun/AntiAircraftGun/Generics/AircraftGunCompareByColor.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/Generics/AircraftGunCompareByType.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunCollectionInfo.cs create mode 100644 AntiAircraftGun/AntiAircraftGun/Generics/DrawningAircraftGunEqutables.cs diff --git a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs index c5e271f..5cb5059 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.Designer.cs @@ -30,6 +30,8 @@ { this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.ButtonSortByType = new System.Windows.Forms.Button(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.listBoxStorages = new System.Windows.Forms.ListBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); @@ -61,18 +63,40 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.ButtonSortByType); + this.groupBox1.Controls.Add(this.ButtonSortByColor); this.groupBox1.Controls.Add(this.groupBox2); this.groupBox1.Controls.Add(this.maskedTextBoxNumber); this.groupBox1.Controls.Add(this.ButtonAddAircraftGun); this.groupBox1.Controls.Add(this.ButtonRemoveAircraftGun); this.groupBox1.Controls.Add(this.ButtonRefreshCollection); - this.groupBox1.Location = new System.Drawing.Point(815, 37); + this.groupBox1.Location = new System.Drawing.Point(815, 27); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(236, 437); + this.groupBox1.Size = new System.Drawing.Size(236, 470); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(49, 236); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(156, 27); + this.ButtonSortByType.TabIndex = 8; + this.ButtonSortByType.Text = "Сортировка по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(49, 269); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(156, 27); + this.ButtonSortByColor.TabIndex = 7; + this.ButtonSortByColor.Text = "Сортировка по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // groupBox2 // this.groupBox2.Controls.Add(this.listBoxStorages); @@ -81,7 +105,7 @@ this.groupBox2.Controls.Add(this.ButtonDelObject); this.groupBox2.Location = new System.Drawing.Point(21, 22); this.groupBox2.Name = "groupBox2"; - this.groupBox2.Size = new System.Drawing.Size(209, 222); + this.groupBox2.Size = new System.Drawing.Size(209, 205); this.groupBox2.TabIndex = 6; this.groupBox2.TabStop = false; this.groupBox2.Text = "Наборы"; @@ -125,14 +149,14 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 318); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 335); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(156, 23); this.maskedTextBoxNumber.TabIndex = 5; // // ButtonAddAircraftGun // - this.ButtonAddAircraftGun.Location = new System.Drawing.Point(49, 285); + this.ButtonAddAircraftGun.Location = new System.Drawing.Point(49, 302); this.ButtonAddAircraftGun.Name = "ButtonAddAircraftGun"; this.ButtonAddAircraftGun.Size = new System.Drawing.Size(156, 27); this.ButtonAddAircraftGun.TabIndex = 2; @@ -142,7 +166,7 @@ // // ButtonRemoveAircraftGun // - this.ButtonRemoveAircraftGun.Location = new System.Drawing.Point(49, 347); + this.ButtonRemoveAircraftGun.Location = new System.Drawing.Point(49, 364); this.ButtonRemoveAircraftGun.Name = "ButtonRemoveAircraftGun"; this.ButtonRemoveAircraftGun.Size = new System.Drawing.Size(156, 26); this.ButtonRemoveAircraftGun.TabIndex = 2; @@ -152,7 +176,7 @@ // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(49, 379); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(49, 396); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Size = new System.Drawing.Size(156, 27); this.ButtonRefreshCollection.TabIndex = 2; @@ -244,5 +268,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs index 38fcb7d..778da00 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormAircraftGunCollection.cs @@ -136,6 +136,11 @@ namespace AntiAircraftGun MessageBox.Show(ex.Message); _logger.LogWarning($"{ex.Message}. Не удалось добавить объект"); } + catch(Exception ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"{ex.Message}. Не удалось добавить объект"); + } } // Добавление объекта в набор private void ButtonAddAircraftGun_Click(object sender, EventArgs e) @@ -226,6 +231,25 @@ namespace AntiAircraftGun _logger.LogWarning("Ошибка загрузки"); } } - } + } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAntiAircraftGun(new AircraftGunCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAntiAircraftGun(new AircraftGunCompareByColor()); + + private void CompareAntiAircraftGun(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.ShowAntiAircraftGuns(); + } } } diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AircraftGunCompareByColor.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AircraftGunCompareByColor.cs new file mode 100644 index 0000000..f6cfdfb --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AircraftGunCompareByColor.cs @@ -0,0 +1,43 @@ +using AntiAircraftGun.DrawningObjects; +using AntiAircraftGun.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.Generics +{ + internal class AircraftGunCompareByColor : IComparer + { + public int Compare(DrawningAircraftGun? x, DrawningAircraftGun? y) + { + if (x == null || x.EntityAircraftGun == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAircraftGun == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.EntityAircraftGun.BodyColor != y.EntityAircraftGun.BodyColor) + { + return x.EntityAircraftGun.BodyColor.Name.CompareTo(y.EntityAircraftGun.BodyColor.Name); + } + if (x.EntityAircraftGun is EntityAntiAircraftGun AntiAircraftGunX && y.EntityAircraftGun is EntityAntiAircraftGun AntiAircraftGunY) + { + var colorCompare = AntiAircraftGunX.AdditionalColor.Name.CompareTo(AntiAircraftGunY.AdditionalColor.Name); + if (colorCompare != 0) + { + return colorCompare; + } + } + var speedCompare = x.EntityAircraftGun.Speed.CompareTo(y.EntityAircraftGun.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityAircraftGun.Weight.CompareTo(y.EntityAircraftGun.Weight); + } + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AircraftGunCompareByType.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AircraftGunCompareByType.cs new file mode 100644 index 0000000..3117bca --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AircraftGunCompareByType.cs @@ -0,0 +1,34 @@ +using AntiAircraftGun.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.Generics +{ + internal class AircraftGunCompareByType : IComparer + { + public int Compare(DrawningAircraftGun? x, DrawningAircraftGun? y) + { + if (x == null || x.EntityAircraftGun == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAircraftGun == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityAircraftGun.Speed.CompareTo(y.EntityAircraftGun.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityAircraftGun.Weight.CompareTo(y.EntityAircraftGun.Weight); + } + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunCollectionInfo.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunCollectionInfo.cs new file mode 100644 index 0000000..4c56a85 --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunCollectionInfo.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.Generics +{ + internal class AntiAircraftGunCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + + public AntiAircraftGunCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + + public bool Equals(AntiAircraftGunCollectionInfo? other) + { + if (other == null || Name == null || other.Name == null) + { + return false; + } + if (Name == other.Name) + { + return true; + } + throw new NotImplementedException(); + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + public override string ToString() + { + return Name; + } + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs index 1996b83..f24b7d9 100644 --- a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericCollection.cs @@ -40,7 +40,7 @@ namespace AntiAircraftGun.Generics { return false; } - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawningAircraftGunEqutables()); } // Перегрузка оператора вычитания @@ -100,5 +100,7 @@ namespace AntiAircraftGun.Generics // Получение объектов коллекции public IEnumerable GetAntiAircraftGuns => _collection.GetAntiAircraftGuns(); + + public void Sort(IComparer comparer) => _collection.SortSet(comparer); } } diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs index 33c27c1..c29ce12 100644 --- a/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs +++ b/AntiAircraftGun/AntiAircraftGun/Generics/AntiAircraftGunsGenericStorage.cs @@ -11,10 +11,10 @@ namespace AntiAircraftGun.Generics internal class AntiAircraftGunsGenericStorage { // Словарь - readonly Dictionary> _aircraftgunStorages; + readonly Dictionary> _aircraftgunStorages; // Возвращение списка названий наборов - public List Keys => _aircraftgunStorages.Keys.ToList(); + public List Keys => _aircraftgunStorages.Keys.ToList(); // Ширина окна отрисовки private readonly int _pictureWidth; // Высота окна отрисовки @@ -29,7 +29,7 @@ namespace AntiAircraftGun.Generics // Конструктор public AntiAircraftGunsGenericStorage(int pictureWidth, int pictureHeight) { - _aircraftgunStorages = new Dictionary>(); + _aircraftgunStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -37,21 +37,21 @@ namespace AntiAircraftGun.Generics // Добавление набора public void AddSet(string name) { - if(_aircraftgunStorages.ContainsKey(name)) + if(_aircraftgunStorages.ContainsKey(new AntiAircraftGunCollectionInfo(name, ""))) { return; } - _aircraftgunStorages[name] = new AntiAircraftGunsGenericCollection(_pictureWidth, _pictureHeight); + _aircraftgunStorages[new AntiAircraftGunCollectionInfo(name, "")] = new AntiAircraftGunsGenericCollection(_pictureWidth, _pictureHeight); } // Удаление набора public void DelSet(string name) { - if (!_aircraftgunStorages.ContainsKey(name)) + if (!_aircraftgunStorages.ContainsKey(new AntiAircraftGunCollectionInfo(name, ""))) { return; } - _aircraftgunStorages.Remove(name); + _aircraftgunStorages.Remove(new AntiAircraftGunCollectionInfo(name, "")); } // Доступ к набору @@ -59,9 +59,10 @@ namespace AntiAircraftGun.Generics { get { - if(_aircraftgunStorages.ContainsKey(ind)) + var info = new AntiAircraftGunCollectionInfo(ind, ""); + if(_aircraftgunStorages.ContainsKey(info)) { - return _aircraftgunStorages[ind]; + return _aircraftgunStorages[info]; } return null; } @@ -75,7 +76,7 @@ namespace AntiAircraftGun.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _aircraftgunStorages) + foreach (KeyValuePair> record in _aircraftgunStorages) { StringBuilder records = new(); foreach (DrawningAircraftGun? elem in record.Value.GetAntiAircraftGuns) @@ -136,7 +137,7 @@ namespace AntiAircraftGun.Generics } } } - _aircraftgunStorages.Add(record[0], collection); + _aircraftgunStorages.Add(new AntiAircraftGunCollectionInfo(record[0], ""), collection); } return true; } diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/DrawningAircraftGunEqutables.cs b/AntiAircraftGun/AntiAircraftGun/Generics/DrawningAircraftGunEqutables.cs new file mode 100644 index 0000000..db0d9ee --- /dev/null +++ b/AntiAircraftGun/AntiAircraftGun/Generics/DrawningAircraftGunEqutables.cs @@ -0,0 +1,67 @@ +using AntiAircraftGun.DrawningObjects; +using AntiAircraftGun.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.Generics +{ + internal class DrawningAircraftGunEqutables : IEqualityComparer + { + public bool Equals(DrawningAircraftGun? x, DrawningAircraftGun? y) + { + if (x == null || x.EntityAircraftGun == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityAircraftGun == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityAircraftGun.Speed != y.EntityAircraftGun.Speed) + { + return false; + } + if (x.EntityAircraftGun.Weight != y.EntityAircraftGun.Weight) + { + return false; + } + if (x.EntityAircraftGun.BodyColor != y.EntityAircraftGun.BodyColor) + { + return false; + } + if (x is DrawingAntiAircraftGun && y is DrawingAntiAircraftGun) + { + if ((x.EntityAircraftGun as EntityAntiAircraftGun).Radar != (y.EntityAircraftGun as EntityAntiAircraftGun).Radar) + { + return false; + } + if ((x.EntityAircraftGun as EntityAntiAircraftGun).ZenitkaGun != (y.EntityAircraftGun as EntityAntiAircraftGun).ZenitkaGun) + { + return false; + } + if ((x.EntityAircraftGun as EntityAntiAircraftGun).Luke != (y.EntityAircraftGun as EntityAntiAircraftGun).Luke) + { + return false; + } + if ((x.EntityAircraftGun as EntityAntiAircraftGun).AdditionalColor != (y.EntityAircraftGun as EntityAntiAircraftGun).AdditionalColor) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningAircraftGun obj) + { + return obj.GetHashCode(); + } + + } +} diff --git a/AntiAircraftGun/AntiAircraftGun/Generics/SetGeneric.cs b/AntiAircraftGun/AntiAircraftGun/Generics/SetGeneric.cs index 6f72744..608700e 100644 --- a/AntiAircraftGun/AntiAircraftGun/Generics/SetGeneric.cs +++ b/AntiAircraftGun/AntiAircraftGun/Generics/SetGeneric.cs @@ -30,13 +30,13 @@ namespace AntiAircraftGun.Generics } // Добавление объекта в набор - public bool Insert(T antiaircraftgun) + public bool Insert(T antiaircraftgun, IEqualityComparer? equal = null) { - return Insert(antiaircraftgun, 0); + return Insert(antiaircraftgun, 0, equal); } // Добавление объекта на конкретную позицию - public bool Insert(T antiaircraftgun, int position) + public bool Insert(T antiaircraftgun, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) { @@ -46,6 +46,16 @@ namespace AntiAircraftGun.Generics { throw new StorageOverflowException(_maxCount); } + if(equal != null) + { + foreach(var i in _places) + { + if (equal.Equals(i, antiaircraftgun)) + { + throw new ApplicationException("Объект уже существует"); + } + } + } _places.Insert(position, antiaircraftgun); return true; } @@ -98,5 +108,7 @@ namespace AntiAircraftGun.Generics } } } + + public void SortSet(IComparer comparer) => _places.Sort(comparer); } } -- 2.25.1