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..5107a74 100644 --- a/Bulldozer/Bulldozer/FormBulldozerCollection.cs +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.cs @@ -127,10 +127,17 @@ namespace Bulldozer MessageBox.Show("Объект добавлен"); _logger.LogInformation("Объект добавлен"); pictureBoxCollection.Image = obj.ShowBulldozers(); - } catch(Exception ex) { + } + catch (StorageOverflowException ex) + { MessageBox.Show(ex.Message); _logger.LogWarning($"Объект не добавлен в набор {listBoxBulldozerStorages.SelectedItem.ToString()}"); } + catch (ArgumentException ex) + { + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxBulldozerStorages.SelectedItem.ToString()}"); + } } @@ -243,5 +250,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/FormBulldozerConfig.Designer.cs b/Bulldozer/Bulldozer/FormBulldozerConfig.Designer.cs index 7e07d50..9e4b643 100644 --- a/Bulldozer/Bulldozer/FormBulldozerConfig.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozerConfig.Designer.cs @@ -125,6 +125,7 @@ this.labelModifiedObject.TabIndex = 8; this.labelModifiedObject.Text = "Продвинутый"; this.labelModifiedObject.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + this.labelModifiedObject.MouseDown += labelObject_MouseDown; // // labelSimpleObject // @@ -137,6 +138,7 @@ this.labelSimpleObject.TabIndex = 7; this.labelSimpleObject.Text = "Простой"; this.labelSimpleObject.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + this.labelSimpleObject.MouseDown += labelObject_MouseDown; // // groupBoxColors // @@ -156,6 +158,8 @@ this.groupBoxColors.TabIndex = 6; this.groupBoxColors.TabStop = false; this.groupBoxColors.Text = "Цвета"; + this.groupBoxColors.DragDrop += PanelObject_DragDrop; + this.groupBoxColors.DragEnter += PanelObject_DragEnter; // // panelPurple // @@ -165,6 +169,7 @@ this.panelPurple.Name = "panelPurple"; this.panelPurple.Size = new System.Drawing.Size(43, 43); this.panelPurple.TabIndex = 1; + this.panelPurple.MouseDown += panelColor_MouseDown; // // panelBlack // @@ -174,6 +179,7 @@ this.panelBlack.Name = "panelBlack"; this.panelBlack.Size = new System.Drawing.Size(43, 43); this.panelBlack.TabIndex = 1; + this.panelBlack.MouseDown += panelColor_MouseDown; // // panelGray // @@ -183,6 +189,7 @@ this.panelGray.Name = "panelGray"; this.panelGray.Size = new System.Drawing.Size(43, 43); this.panelGray.TabIndex = 1; + this.panelGray.MouseDown += panelColor_MouseDown; // // panelWhite // @@ -192,6 +199,7 @@ this.panelWhite.Name = "panelWhite"; this.panelWhite.Size = new System.Drawing.Size(43, 43); this.panelWhite.TabIndex = 1; + this.panelWhite.MouseDown += panelColor_MouseDown; // // panelYellow // @@ -201,6 +209,7 @@ this.panelYellow.Name = "panelYellow"; this.panelYellow.Size = new System.Drawing.Size(43, 43); this.panelYellow.TabIndex = 1; + this.panelYellow.MouseDown += panelColor_MouseDown; // // panelBlue // @@ -210,6 +219,7 @@ this.panelBlue.Name = "panelBlue"; this.panelBlue.Size = new System.Drawing.Size(43, 43); this.panelBlue.TabIndex = 1; + this.panelBlue.MouseDown += panelColor_MouseDown; // // panelGreen // @@ -219,6 +229,7 @@ this.panelGreen.Name = "panelGreen"; this.panelGreen.Size = new System.Drawing.Size(43, 43); this.panelGreen.TabIndex = 1; + this.panelGreen.MouseDown += panelColor_MouseDown; // // panelRed // @@ -228,6 +239,7 @@ this.panelRed.Name = "panelRed"; this.panelRed.Size = new System.Drawing.Size(43, 43); this.panelRed.TabIndex = 0; + this.panelRed.MouseDown += panelColor_MouseDown; // // numericUpDownWeight // @@ -315,6 +327,8 @@ this.panelObject.Name = "panelObject"; this.panelObject.Size = new System.Drawing.Size(334, 202); this.panelObject.TabIndex = 2; + this.panelObject.DragDrop += PanelObject_DragDrop; + this.panelObject.DragEnter += PanelObject_DragEnter; // // labelAddColor // @@ -327,6 +341,8 @@ this.labelAddColor.TabIndex = 3; this.labelAddColor.Text = "Дополн. цвет"; this.labelAddColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + this.labelAddColor.DragDrop += labelColor_dragDrop; + this.labelAddColor.DragEnter += labelColor_dragEnter; // // labelColor // @@ -339,6 +355,8 @@ this.labelColor.TabIndex = 2; this.labelColor.Text = "Основной цвет"; this.labelColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + this.labelColor.DragDrop += labelColor_dragDrop; + this.labelColor.DragEnter += labelColor_dragEnter; // // buttonAdd // @@ -349,6 +367,7 @@ this.buttonAdd.TabIndex = 3; this.buttonAdd.Text = "Добавить"; this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += buttonAdd_Click; // // buttonCancel // diff --git a/Bulldozer/Bulldozer/Generics/BulldozerCollectionInfo.cs b/Bulldozer/Bulldozer/Generics/BulldozerCollectionInfo.cs new file mode 100644 index 0000000..c26bddd --- /dev/null +++ b/Bulldozer/Bulldozer/Generics/BulldozerCollectionInfo.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Generics +{ + internal class BulldozerCollectionInfo: IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BulldozerCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BulldozerCollectionInfo other) + { + if (Name != other?.Name) + return false; + return true; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + public override string ToString() + { + return Name; + } + } +} 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..0d2e80d 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); /// /// Получение объектов коллекции /// @@ -54,7 +60,7 @@ namespace Bulldozer.Generics { if (obj == null) return false; - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj, new DrawingBulldozerEqutables()) ?? false; } /// diff --git a/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs b/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs index 36f2155..d056c26 100644 --- a/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs +++ b/Bulldozer/Bulldozer/Generics/BulldozersGenericStorage.cs @@ -3,6 +3,7 @@ using Bulldozer.MovementStrategy; using System.Text; using Bulldozer.Exceptions; using System.Numerics; +using Microsoft.AspNetCore.Http; namespace Bulldozer.Generics { @@ -14,11 +15,11 @@ namespace Bulldozer.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _bulldozerStorages; + readonly Dictionary> _bulldozerStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _bulldozerStorages.Keys.ToList(); + public List Keys => _bulldozerStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -34,7 +35,7 @@ namespace Bulldozer.Generics /// public BulldozersGenericStorage(int pictureWidth, int pictureHeight) { - _bulldozerStorages = new Dictionary>(); + _bulldozerStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -44,9 +45,9 @@ namespace Bulldozer.Generics /// Название набора public void AddSet(string name) { - if (!_bulldozerStorages.ContainsKey(name)) + if (!_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(name, string.Empty))) { - _bulldozerStorages.Add(name, new BulldozersGenericCollection(_pictureWidth, _pictureHeight)); + _bulldozerStorages.Add(new BulldozerCollectionInfo(name, string.Empty), new BulldozersGenericCollection(_pictureWidth, _pictureHeight)); } } @@ -58,9 +59,9 @@ namespace Bulldozer.Generics public void DelSet(string name) { // TODO: Прописать логику для удаления набора - if (_bulldozerStorages.ContainsKey(name)) + if (_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(name, string.Empty))) { - _bulldozerStorages.Remove(name); + _bulldozerStorages.Remove(new BulldozerCollectionInfo(name, string.Empty)); } } @@ -73,14 +74,11 @@ namespace Bulldozer.Generics { get { - if (_bulldozerStorages.ContainsKey(ind)) + if (_bulldozerStorages.ContainsKey(new BulldozerCollectionInfo(ind, string.Empty))) { - return _bulldozerStorages[ind]; - } - else - { - return null; + return _bulldozerStorages[new BulldozerCollectionInfo(ind, string.Empty)]; } + return null; } } /// Разделитель для записи ключа и значения элемента словаря @@ -107,7 +105,7 @@ namespace Bulldozer.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _bulldozerStorages) + foreach (KeyValuePair> record in _bulldozerStorages) { StringBuilder records = new(); foreach (DrawingBulldozer? elem in record.Value.GetBulldozers) @@ -189,7 +187,7 @@ namespace Bulldozer.Generics } } } - _bulldozerStorages.Add(record[0], collection); + _bulldozerStorages.Add(new BulldozerCollectionInfo(record[0], string.Empty), collection); str = sr.ReadLine(); } while (str != null); 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..c0ea1ba 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,12 @@ namespace Bulldozer.Generics /// /// Добавляемый бульдозер /// - public bool Insert(T bulldozer) + public bool Insert(T bulldozer, IEqualityComparer? equal = null) { - return Insert(bulldozer, 0); + if (_places.Count == _maxCount) + throw new StorageOverflowException(_maxCount); + Insert(bulldozer, 0, equal); + return true; } /// @@ -49,12 +56,21 @@ 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 (_places.Count >= _maxCount) + throw new StorageOverflowException(_maxCount); + + if (position < 0 || position >= _maxCount) + { + return false; + } + + if (equal != null) + { + if (_places.Contains(bulldozer, equal)) + throw new ArgumentException(nameof(bulldozer)); } - if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(position, bulldozer); return true; }