From be7d510f0318d3d35bb65d7058cf0a1648ca46e8 Mon Sep 17 00:00:00 2001 From: bocchanskyy Date: Mon, 25 Dec 2023 04:14:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=208=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D1=83=20i=20guess...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...lldozer.cs => DrawingBulldozerUpgraded.cs} | 0 ...ldozer.cs => ExtensionDrawingBulldozer.cs} | 2 +- .../FormBulldozerCollection.Designer.cs | 40 ++++++++-- .../Bulldozer/FormBulldozerCollection.cs | 33 +++++++++ .../Generics/BulldozerCompareByColor.cs | 73 +++++++++++++++++++ .../Generics/BulldozerCompareByType.cs | 32 ++++++++ .../Generics/BulldozersGenericCollection.cs | 6 ++ .../Generics/DrawingBulldozerEqutables.cs | 59 +++++++++++++++ Bulldozer/Bulldozer/Generics/SetGeneric.cs | 17 ++++- 9 files changed, 251 insertions(+), 11 deletions(-) rename Bulldozer/Bulldozer/DrawingObjects/{DrawingUpgradedBulldozer.cs => DrawingBulldozerUpgraded.cs} (100%) rename Bulldozer/Bulldozer/DrawingObjects/{ExtentionDrawingBulldozer.cs => ExtensionDrawingBulldozer.cs} (97%) create mode 100644 Bulldozer/Bulldozer/Generics/BulldozerCompareByColor.cs create mode 100644 Bulldozer/Bulldozer/Generics/BulldozerCompareByType.cs create mode 100644 Bulldozer/Bulldozer/Generics/DrawingBulldozerEqutables.cs diff --git a/Bulldozer/Bulldozer/DrawingObjects/DrawingUpgradedBulldozer.cs b/Bulldozer/Bulldozer/DrawingObjects/DrawingBulldozerUpgraded.cs similarity index 100% rename from Bulldozer/Bulldozer/DrawingObjects/DrawingUpgradedBulldozer.cs rename to Bulldozer/Bulldozer/DrawingObjects/DrawingBulldozerUpgraded.cs diff --git a/Bulldozer/Bulldozer/DrawingObjects/ExtentionDrawingBulldozer.cs b/Bulldozer/Bulldozer/DrawingObjects/ExtensionDrawingBulldozer.cs similarity index 97% rename from Bulldozer/Bulldozer/DrawingObjects/ExtentionDrawingBulldozer.cs rename to Bulldozer/Bulldozer/DrawingObjects/ExtensionDrawingBulldozer.cs index 6e30586..8129aa3 100644 --- a/Bulldozer/Bulldozer/DrawingObjects/ExtentionDrawingBulldozer.cs +++ b/Bulldozer/Bulldozer/DrawingObjects/ExtensionDrawingBulldozer.cs @@ -2,7 +2,7 @@ namespace Bulldozer.DrawingObjects { - public static class ExtentionDrawingBulldozer + public static class ExtensionDrawingBulldozer { public static DrawingBulldozer? CreateDrawingBulldozer(this string info, char separatorForObject, int width, int height) { diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs index 9ecba64..c45cb4b 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs @@ -45,6 +45,8 @@ this.toolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); + this.ButtonSortByType = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.panelTools.SuspendLayout(); this.groupListBulldozersBox.SuspendLayout(); @@ -65,6 +67,8 @@ // panelTools // this.panelTools.Anchor = System.Windows.Forms.AnchorStyles.Right; + this.panelTools.Controls.Add(this.ButtonSortByType); + this.panelTools.Controls.Add(this.ButtonSortByColor); this.panelTools.Controls.Add(this.groupListBulldozersBox); this.panelTools.Controls.Add(this.textBoxDeletingBulldozer); this.panelTools.Controls.Add(this.buttonUpdateColletion); @@ -132,7 +136,7 @@ // // textBoxDeletingBulldozer // - this.textBoxDeletingBulldozer.Location = new System.Drawing.Point(25, 525); + this.textBoxDeletingBulldozer.Location = new System.Drawing.Point(22, 525); this.textBoxDeletingBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.textBoxDeletingBulldozer.Name = "textBoxDeletingBulldozer"; this.textBoxDeletingBulldozer.Size = new System.Drawing.Size(220, 27); @@ -140,7 +144,7 @@ // // buttonUpdateColletion // - this.buttonUpdateColletion.Location = new System.Drawing.Point(25, 692); + this.buttonUpdateColletion.Location = new System.Drawing.Point(22, 693); this.buttonUpdateColletion.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonUpdateColletion.Name = "buttonUpdateColletion"; this.buttonUpdateColletion.Size = new System.Drawing.Size(221, 53); @@ -150,7 +154,7 @@ // // buttonDeleteBulldozer // - this.buttonDeleteBulldozer.Location = new System.Drawing.Point(25, 563); + this.buttonDeleteBulldozer.Location = new System.Drawing.Point(22, 560); this.buttonDeleteBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonDeleteBulldozer.Name = "buttonDeleteBulldozer"; this.buttonDeleteBulldozer.Size = new System.Drawing.Size(221, 53); @@ -161,7 +165,7 @@ // // buttonAddBulldozer // - this.buttonAddBulldozer.Location = new System.Drawing.Point(24, 464); + this.buttonAddBulldozer.Location = new System.Drawing.Point(22, 464); this.buttonAddBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonAddBulldozer.Name = "buttonAddBulldozer"; this.buttonAddBulldozer.Size = new System.Drawing.Size(221, 53); @@ -202,17 +206,39 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(166, 26); this.SaveToolStripMenuItem.Text = "Сохранить"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(166, 26); this.LoadToolStripMenuItem.Text = "Загрузить"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(22, 322); + this.ButtonSortByColor.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(221, 53); + this.ButtonSortByColor.TabIndex = 9; + this.ButtonSortByColor.Text = "Сортировка по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(22, 383); + this.ButtonSortByType.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(221, 53); + this.ButtonSortByType.TabIndex = 10; + this.ButtonSortByType.Text = "Сортировка по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // FormBulldozerCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -255,5 +281,7 @@ private ToolStripMenuItem toolStripMenuItem; private ToolStripMenuItem SaveToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.cs index 97e804a..2ae42d4 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.cs @@ -243,5 +243,38 @@ namespace Bulldozer } } } + + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBulldozers(new BulldozerCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBulldozers(new BulldozerCompareByColor()); + + /// + /// Сортировка по сравнителю + /// + /// + + private void CompareBulldozers(IComparer comparer) + { + if (listBoxBulldozerStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowBulldozers(); + } } } diff --git a/Bulldozer/Bulldozer/Generics/BulldozerCompareByColor.cs b/Bulldozer/Bulldozer/Generics/BulldozerCompareByColor.cs new file mode 100644 index 0000000..94f9cf8 --- /dev/null +++ b/Bulldozer/Bulldozer/Generics/BulldozerCompareByColor.cs @@ -0,0 +1,73 @@ +using Bulldozer.DrawingObjects; +using Bulldozer.Entities; +using Bulldozer.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class BulldozerCompareByColor : IComparer + { + public int Compare(DrawingBulldozer? x, DrawingBulldozer? 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 xBulldozer = x as DrawingBulldozer; + var yBulldozer = y as DrawingBulldozer; + + if (xBulldozer == null && yBulldozer == null) + { + return 0; + } + if (xBulldozer == null && yBulldozer != null) + { + return 1; + } + if (xBulldozer != null && yBulldozer == null) + { + return -1; + } + if (xBulldozer.EntityBulldozer.BodyColor == yBulldozer.EntityBulldozer.BodyColor) + { + // Объекты равны по цвету + return 0; + } + if (xBulldozer.EntityBulldozer.BodyColor.R.CompareTo(yBulldozer.EntityBulldozer.BodyColor.R) == 0) + { + if (xBulldozer.EntityBulldozer.BodyColor.G.CompareTo(yBulldozer.EntityBulldozer.BodyColor.G) == 0) + { + // Сравнение по компоненте B, если R и G совпадают + return xBulldozer.EntityBulldozer.BodyColor.B.CompareTo(yBulldozer.EntityBulldozer.BodyColor.B); + } + else + { + // Сравнение по компоненте G, если R совпадает, но G не совпадает + return xBulldozer.EntityBulldozer.BodyColor.G.CompareTo(yBulldozer.EntityBulldozer.BodyColor.G); + } + } + else + { + // Сравнение по компоненте R, если R не совпадает + return xBulldozer.EntityBulldozer.BodyColor.R.CompareTo(yBulldozer.EntityBulldozer.BodyColor.R); + } + + } + + } +} \ No newline at end of file diff --git a/Bulldozer/Bulldozer/Generics/BulldozerCompareByType.cs b/Bulldozer/Bulldozer/Generics/BulldozerCompareByType.cs new file mode 100644 index 0000000..ffbf5f3 --- /dev/null +++ b/Bulldozer/Bulldozer/Generics/BulldozerCompareByType.cs @@ -0,0 +1,32 @@ +using Bulldozer.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class BulldozerCompareByType : IComparer + { + public int Compare(DrawingBulldozer? x, DrawingBulldozer? y) + { + if (x == null || x.EntityBulldozer == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityBulldozer == null) + throw new ArgumentNullException(nameof(y)); + + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityBulldozer.Speed.CompareTo(y.EntityBulldozer.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityBulldozer.Weight.CompareTo(y.EntityBulldozer.Weight); + } + } +} \ No newline at end of file diff --git a/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs b/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs index ca6d47e..ceeb7bb 100644 --- a/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs +++ b/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs @@ -7,6 +7,12 @@ namespace Bulldozer.Generics where T : DrawingBulldozer where U : IMoveableObject { + + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Получение объектов коллекции /// diff --git a/Bulldozer/Bulldozer/Generics/DrawingBulldozerEqutables.cs b/Bulldozer/Bulldozer/Generics/DrawingBulldozerEqutables.cs new file mode 100644 index 0000000..74b2e00 --- /dev/null +++ b/Bulldozer/Bulldozer/Generics/DrawingBulldozerEqutables.cs @@ -0,0 +1,59 @@ +using Bulldozer.DrawingObjects; +using Bulldozer.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class DrawingBulldozerEqutables : IEqualityComparer + { + public bool Equals(DrawingBulldozer? x, DrawingBulldozer? y) + { + if (x == null || x.EntityBulldozer == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBulldozer == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityBulldozer.Speed != y.EntityBulldozer.Speed) + { + return false; + } + if (x.EntityBulldozer.Weight != y.EntityBulldozer.Weight) + { + return false; + } + if (x.EntityBulldozer.BodyColor != y.EntityBulldozer.BodyColor) + { + return false; + } + if (x is DrawingBulldozerUpgraded && y is DrawingBulldozerUpgraded) + { + EntityBulldozerUpgraded EntityX = (EntityBulldozerUpgraded)x.EntityBulldozer; + EntityBulldozerUpgraded EntityY = (EntityBulldozerUpgraded)y.EntityBulldozer; + if (EntityX.Blade != EntityY.Blade) + return false; + if (EntityX.Ripper != EntityY.Ripper) + return false; + if (EntityX.DopColor != EntityY.DopColor) + return false; + } + return true; + } + + public int GetHashCode([DisallowNull] DrawingBulldozer obj) + { + return obj.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/Bulldozer/Bulldozer/Generics/SetGeneric.cs b/Bulldozer/Bulldozer/Generics/SetGeneric.cs index 165e25f..b6fc48a 100644 --- a/Bulldozer/Bulldozer/Generics/SetGeneric.cs +++ b/Bulldozer/Bulldozer/Generics/SetGeneric.cs @@ -23,7 +23,11 @@ namespace Bulldozer.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; - + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Конструктор /// @@ -39,9 +43,9 @@ namespace Bulldozer.Generics /// /// Добавляемый бульдозер /// - public bool Insert(T bulldozer) + public bool Insert(T bulldozer, IEqualityComparer? equal = null) { - return Insert(bulldozer, 0); + return Insert(bulldozer, 0, equal); } /// @@ -49,12 +53,17 @@ namespace Bulldozer.Generics /// /// Добавляемый бульдозер /// - public bool Insert(T bulldozer , int position) + public bool Insert(T bulldozer , int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) { throw new StorageOverflowException("Вставка невозможна."); } if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + if (equal != null) + { + if (_places.Contains(bulldozer, equal)) + throw new ArgumentException(nameof(bulldozer)); + } _places.Insert(position, bulldozer); return true; }