From 477cf6921adcdd70fe4d3c4e6cc76c45c39b0cff Mon Sep 17 00:00:00 2001 From: YourDax Date: Mon, 11 Dec 2023 22:27:32 +0400 Subject: [PATCH] Lab8 done --- .../FormAntiAirCraftGunCollection.Designer.cs | 28 ++++++- .../FormAntiAirCraftGunCollection.cs | 27 ++++++- .../Generics/AntiAirCraftGunCollectionInfo.cs | 28 +++++++ .../Generics/AntiAirCraftGunCompareByColor.cs | 73 +++++++++++++++++++ .../Generics/AntiAirCraftGunCompareByType.cs | 32 ++++++++ .../AntiAirCraftGunGenericCollection.cs | 3 +- .../Generics/AntiAirCraftGunGenericStorage.cs | 27 +++---- .../AntiAirCraftGun/Generics/SetGeneric.cs | 14 +++- 8 files changed, 212 insertions(+), 20 deletions(-) create mode 100644 AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCollectionInfo.cs create mode 100644 AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCompareByColor.cs create mode 100644 AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCompareByType.cs diff --git a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.Designer.cs b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.Designer.cs index e3684cc..d21b817 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.Designer.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.Designer.cs @@ -30,6 +30,8 @@ { this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.panelTools = new System.Windows.Forms.Panel(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); + this.ButtonSortByType = new System.Windows.Forms.Button(); this.panel1 = new System.Windows.Forms.Panel(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -61,6 +63,8 @@ // // panelTools // + this.panelTools.Controls.Add(this.ButtonSortByColor); + this.panelTools.Controls.Add(this.ButtonSortByType); this.panelTools.Controls.Add(this.panel1); this.panelTools.Controls.Add(this.maskedTextBoxNumber); this.panelTools.Controls.Add(this.buttonUpdateColletion); @@ -72,13 +76,33 @@ this.panelTools.TabIndex = 1; this.panelTools.Tag = ""; // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(25, 344); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(193, 35); + this.ButtonSortByColor.TabIndex = 10; + this.ButtonSortByColor.Text = "Сортировка по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(27, 303); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(193, 35); + this.ButtonSortByType.TabIndex = 9; + this.ButtonSortByType.Text = "Сортировка по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // panel1 // this.panel1.Controls.Add(this.textBoxStorageName); this.panel1.Controls.Add(this.listBoxStorages); this.panel1.Controls.Add(this.buttonAddCollection); this.panel1.Controls.Add(this.buttonDelCollection); - this.panel1.Location = new System.Drawing.Point(15, 52); + this.panel1.Location = new System.Drawing.Point(15, 3); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(215, 294); this.panel1.TabIndex = 8; @@ -242,5 +266,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/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.cs b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.cs index 697dead..ccf0434 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/FormAntiAirCraftGunCollection.cs @@ -37,7 +37,7 @@ namespace AntiAircraftGun listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(_storage.Keys[i]); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) @@ -129,6 +129,11 @@ namespace AntiAircraftGun Serilog.Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена"); MessageBox.Show(ex.Message); } + catch (ArgumentException ex) + { + Serilog.Log.Warning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + } }); form.AddEvent(zenitDelegate); } @@ -226,6 +231,7 @@ namespace AntiAircraftGun { listBoxStorages.Items.Add(collection); } + ReloadObjects(); } catch (Exception ex) { @@ -235,6 +241,25 @@ namespace AntiAircraftGun } } } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareAntiAirCraftGun(new AntiAirCraftGunCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareAntiAirCraftGun(new AntiAirCraftGunCompareByColor()); + + 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.ShowZenits(); + } } } diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCollectionInfo.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCollectionInfo.cs new file mode 100644 index 0000000..2a43e8d --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCollectionInfo.cs @@ -0,0 +1,28 @@ +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) + { + return Name == other.Name; + } + + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCompareByColor.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCompareByColor.cs new file mode 100644 index 0000000..caba4ac --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCompareByColor.cs @@ -0,0 +1,73 @@ +using AntiAircraftGun.DrawingObjects; +using AntiAircraftGun.Enitites; +using AntiAircraftGun.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.Generics +{ + internal class AntiAirCraftGunCompareByColor : IComparer + { + public int Compare(BaseDrawingAntiAirCraftGun? x, BaseDrawingAntiAirCraftGun? y) + { + if (x == null && y == null) + { + // Оба объекта равны + return 0; + } + if (x == null && y != null) + { + // Объект x меньше + return 1; + } + if (x != null && y == null) + { + // Объект x больше + return -1; + } + var xAntiAircraftGun = x as BaseDrawingAntiAirCraftGun; + var yAntiAircraftGun = y as BaseDrawingAntiAirCraftGun; + + if (xAntiAircraftGun == null && yAntiAircraftGun == null) + { + return 0; + } + if (xAntiAircraftGun == null && yAntiAircraftGun != null) + { + return 1; + } + if (xAntiAircraftGun != null && yAntiAircraftGun == null) + { + return -1; + } + if (xAntiAircraftGun.AntiAirСraftGun.BodyColor == yAntiAircraftGun.AntiAirСraftGun.BodyColor) + { + // Объекты равны по цвету + return 0; + } + if (xAntiAircraftGun.AntiAirСraftGun.BodyColor.R.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.R) == 0) + { + if (xAntiAircraftGun.AntiAirСraftGun.BodyColor.G.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.G) == 0) + { + // Сравнение по компоненте B, если R и G совпадают + return xAntiAircraftGun.AntiAirСraftGun.BodyColor.B.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.B); + } + else + { + // Сравнение по компоненте G, если R совпадает, но G не совпадает + return xAntiAircraftGun.AntiAirСraftGun.BodyColor.G.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.G); + } + } + else + { + // Сравнение по компоненте R, если R не совпадает + return xAntiAircraftGun.AntiAirСraftGun.BodyColor.R.CompareTo(yAntiAircraftGun.AntiAirСraftGun.BodyColor.R); + } + + } + + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCompareByType.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCompareByType.cs new file mode 100644 index 0000000..3247b06 --- /dev/null +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunCompareByType.cs @@ -0,0 +1,32 @@ +using AntiAircraftGun.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntiAircraftGun.Generics +{ + internal class AntiAirCraftGunCompareByType : IComparer + { + public int Compare(BaseDrawingAntiAirCraftGun? x, BaseDrawingAntiAirCraftGun? y) + { + if (x == null || x.AntiAirСraftGun == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.AntiAirСraftGun == null) + throw new ArgumentNullException(nameof(y)); + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.AntiAirСraftGun.Speed.CompareTo(y.AntiAirСraftGun.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.AntiAirСraftGun.Weight.CompareTo(y.AntiAirСraftGun.Weight); + } + } +} diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs index 3a8b97b..a10ced0 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericCollection.cs @@ -36,6 +36,7 @@ namespace AntiAircraftGun.Generics /// Набор объектов /// private readonly SetGeneric _collection; + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Конструктор /// @@ -59,7 +60,7 @@ namespace AntiAircraftGun.Generics { if (obj == null) return false; - return collect?._collection.Insert(obj) ?? false; + return collect?._collection.Insert(obj, new DrawingAntiAirCraftGunEqutables()) ?? false; } /// /// Перегрузка оператора вычитания diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs index 5859b2b..84fcffa 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/AntiAirCraftGunGenericStorage.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace AntiAircraftGun.Generics { @@ -28,11 +29,11 @@ namespace AntiAircraftGun.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _zenitStorages; + readonly Dictionary> _zenitStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _zenitStorages.Keys.ToList(); + public List Keys => _zenitStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -48,7 +49,7 @@ namespace AntiAircraftGun.Generics /// public AntiAirCraftGunGenericStorage(int pictureWidth, int pictureHeight) { - _zenitStorages = new Dictionary>(); + _zenitStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -59,13 +60,13 @@ namespace AntiAircraftGun.Generics public void AddSet(string name) { // TODO: Прописать логику для добавления набора - if (_zenitStorages.ContainsKey(name)) + if (_zenitStorages.ContainsKey(new AntiAirCraftGunCollectionInfo(name, string.Empty))) { MessageBox.Show("Набор с таким именем уже существует"); return; } - _zenitStorages.Add(name, new AntiAirCraftGunGenericCollection(_pictureWidth, _pictureHeight)); + _zenitStorages.Add(new AntiAirCraftGunCollectionInfo(name,string.Empty), new AntiAirCraftGunGenericCollection(_pictureWidth, _pictureHeight)); } /// @@ -75,12 +76,12 @@ namespace AntiAircraftGun.Generics public void DelSet(string name) { // TODO: Прописать логику для удаления набора - if (!_zenitStorages.ContainsKey(name)) + if (!_zenitStorages.ContainsKey(new AntiAirCraftGunCollectionInfo(name, string.Empty))) { return; } - _zenitStorages.Remove(name); + _zenitStorages.Remove(new AntiAirCraftGunCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -92,10 +93,11 @@ namespace AntiAircraftGun.Generics { get { + AntiAirCraftGunCollectionInfo indobj = new AntiAirCraftGunCollectionInfo(ind, string.Empty); // TODO: Продумать логику получения набора - if (_zenitStorages.ContainsKey(ind)) + if (_zenitStorages.ContainsKey(indobj)) { - return _zenitStorages[ind]; + return _zenitStorages[indobj]; } else { @@ -115,15 +117,14 @@ namespace AntiAircraftGun.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _zenitStorages) + foreach (KeyValuePair> record in _zenitStorages) { StringBuilder records = new(); foreach (BaseDrawingAntiAirCraftGun? elem in record.Value.GetCars) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -186,7 +187,7 @@ namespace AntiAircraftGun.Generics } } } - _zenitStorages.Add(record[0], collection); + _zenitStorages.Add(new AntiAirCraftGunCollectionInfo(record[0],string.Empty), collection); str = sr.ReadLine(); } while (str != null); diff --git a/AntiAirCraftGun/AntiAirCraftGun/Generics/SetGeneric.cs b/AntiAirCraftGun/AntiAirCraftGun/Generics/SetGeneric.cs index 3e42168..3856d58 100644 --- a/AntiAirCraftGun/AntiAirCraftGun/Generics/SetGeneric.cs +++ b/AntiAirCraftGun/AntiAirCraftGun/Generics/SetGeneric.cs @@ -31,26 +31,32 @@ namespace AntiAircraftGun.Generics _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемый автомобиль /// - public bool Insert(T car) + public bool Insert(T car, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - Insert(car, 0); + Insert(car, 0, equal); return true; } - public bool Insert(T car, int position) + public bool Insert(T car, int position, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!(position >= 0 && position <= Count)) return false; - _places.Insert(position, car); + if (equal != null) + { + if (_places.Contains(car, equal)) + throw new ArgumentException(nameof(car)); + } + _places.Insert(position, car); return true; }