diff --git a/Sailboat/Sailboat/BoatCompareByColor.cs b/Sailboat/Sailboat/BoatCompareByColor.cs new file mode 100644 index 0000000..b3aae56 --- /dev/null +++ b/Sailboat/Sailboat/BoatCompareByColor.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.Entities; + +namespace Sailboat.Generics +{ + internal class BoatCompareByColor : IComparer + { + public int Compare(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bodyColorCompare = x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityBoat is EntitySailboat xEntitySailboat && y.EntityBoat is EntitySailboat yEntitySailboat) + { + var dumpBoxColorCompare = xEntitySailboat.BodyColor.Name.CompareTo(yEntitySailboat.BodyColor.Name); + if (dumpBoxColorCompare != 0) + { + return dumpBoxColorCompare; + } + var tentColorCompare = xEntitySailboat.AdditionalColor.Name.CompareTo(yEntitySailboat.AdditionalColor.Name); + if (tentColorCompare != 0) + { + return tentColorCompare; + } + } + var speedCompare = x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight); + } + } +} diff --git a/Sailboat/Sailboat/BoatCompareByType.cs b/Sailboat/Sailboat/BoatCompareByType.cs new file mode 100644 index 0000000..32419f8 --- /dev/null +++ b/Sailboat/Sailboat/BoatCompareByType.cs @@ -0,0 +1,35 @@ +using Sailboat.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Generics +{ + internal class BoatCompareByType : IComparer + { + public int Compare(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityBoat.Speed.CompareTo(y.EntityBoat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBoat.Weight.CompareTo(y.EntityBoat.Weight); + } + } +} diff --git a/Sailboat/Sailboat/BoatsCollectionInfo.cs b/Sailboat/Sailboat/BoatsCollectionInfo.cs new file mode 100644 index 0000000..9a20a0f --- /dev/null +++ b/Sailboat/Sailboat/BoatsCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Generics +{ + internal class BoatsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BoatsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BoatsCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/Sailboat/Sailboat/BoatsGenericCollection.cs b/Sailboat/Sailboat/BoatsGenericCollection.cs index 9c5176d..77fef71 100644 --- a/Sailboat/Sailboat/BoatsGenericCollection.cs +++ b/Sailboat/Sailboat/BoatsGenericCollection.cs @@ -38,6 +38,7 @@ namespace Sailboat.Generics /// private readonly SetGeneric _collection; public IEnumerable GetBoats => _collection.GetBoats(); + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Конструктор /// @@ -63,7 +64,7 @@ namespace Sailboat.Generics { return false; } - return (bool)collect?._collection.Insert(obj); + return (bool)collect?._collection.Insert(obj, new DrawingBoatEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs index 2e94e5e..f723f66 100644 --- a/Sailboat/Sailboat/BoatsGenericStorage.cs +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -15,11 +15,11 @@ namespace Sailboat.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _boatStorages; + readonly Dictionary> _boatStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _boatStorages.Keys.ToList(); + public List Keys => _boatStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -47,7 +47,7 @@ namespace Sailboat.Generics /// public BoatsGenericStorage(int pictureWidth, int pictureHeight) { - _boatStorages = new Dictionary>(); + _boatStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -57,11 +57,7 @@ namespace Sailboat.Generics /// Название набора public void AddSet(string name) { - if (_boatStorages.ContainsKey(name)) - { - return; - } - _boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); + _boatStorages.Add(new BoatsCollectionInfo(name, string.Empty), new BoatsGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -69,11 +65,9 @@ namespace Sailboat.Generics /// Название набора public void DelSet(string name) { - if (!_boatStorages.ContainsKey(name)) - { + if (!_boatStorages.ContainsKey(new BoatsCollectionInfo(name, string.Empty))) return; - } - _boatStorages.Remove(name); + _boatStorages.Remove(new BoatsCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -84,10 +78,9 @@ namespace Sailboat.Generics { get { - if (_boatStorages.ContainsKey(ind)) - { - return _boatStorages[ind]; - } + BoatsCollectionInfo indObj = new BoatsCollectionInfo(ind, string.Empty); + if (_boatStorages.ContainsKey(indObj)) + return _boatStorages[indObj]; return null; } } @@ -105,7 +98,7 @@ namespace Sailboat.Generics } StringBuilder data = new(); - foreach (KeyValuePair> record in _boatStorages) + foreach (KeyValuePair> record in _boatStorages) { StringBuilder records = new(); foreach (DrawingBoat? elem in record.Value.GetBoats) @@ -180,7 +173,7 @@ namespace Sailboat.Generics } } } - _boatStorages.Add(name, collection); + _boatStorages.Add(new BoatsCollectionInfo(name, string.Empty), collection); } } } diff --git a/Sailboat/Sailboat/DrawingBoatEqutables.cs b/Sailboat/Sailboat/DrawingBoatEqutables.cs new file mode 100644 index 0000000..e66d5c0 --- /dev/null +++ b/Sailboat/Sailboat/DrawingBoatEqutables.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.Entities; + +namespace Sailboat.Generics +{ + internal class DrawingBoatEqutables : IEqualityComparer + { + public bool Equals(DrawingBoat? x, DrawingBoat? y) + { + if (x == null || x.EntityBoat == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBoat == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityBoat.Speed != y.EntityBoat.Speed) + { + return false; + } + if (x.EntityBoat.Weight != y.EntityBoat.Weight) + { + return false; + } + if (x.EntityBoat.BodyColor != y.EntityBoat.BodyColor) + { + return false; + } + if (x is DrawingSailboat && y is DrawingSailboat) + { + EntitySailboat EntityX = (EntitySailboat)x.EntityBoat; + EntitySailboat EntityY = (EntitySailboat)y.EntityBoat; + if (EntityX.Sail != EntityY.Sail) + return false; + if (EntityX.Hull != EntityY.Hull) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawingBoat obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Sailboat/Sailboat/FormBoatCollection.Designer.cs b/Sailboat/Sailboat/FormBoatCollection.Designer.cs index f18e0c1..a3ec354 100644 --- a/Sailboat/Sailboat/FormBoatCollection.Designer.cs +++ b/Sailboat/Sailboat/FormBoatCollection.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.panelCollection = new System.Windows.Forms.Panel(); this.buttonDelObject = new System.Windows.Forms.Button(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -64,6 +66,8 @@ // panelTools // this.panelTools.BackColor = System.Drawing.SystemColors.ScrollBar; + this.panelTools.Controls.Add(this.buttonSortByColor); + this.panelTools.Controls.Add(this.buttonSortByType); this.panelTools.Controls.Add(this.panelCollection); this.panelTools.Controls.Add(this.maskedTextBoxNumber); this.panelTools.Controls.Add(this.buttonRefreshCollection); @@ -75,6 +79,26 @@ this.panelTools.Size = new System.Drawing.Size(209, 722); this.panelTools.TabIndex = 1; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(20, 479); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(180, 34); + this.buttonSortByColor.TabIndex = 7; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(20, 439); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(180, 34); + this.buttonSortByType.TabIndex = 6; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // // panelCollection // this.panelCollection.BackColor = System.Drawing.SystemColors.AppWorkspace; @@ -82,7 +106,7 @@ this.panelCollection.Controls.Add(this.listBoxStorages); this.panelCollection.Controls.Add(this.buttonAddObject); this.panelCollection.Controls.Add(this.textBoxStorageName); - this.panelCollection.Location = new System.Drawing.Point(20, 166); + this.panelCollection.Location = new System.Drawing.Point(20, 124); this.panelCollection.Name = "panelCollection"; this.panelCollection.Size = new System.Drawing.Size(181, 287); this.panelCollection.TabIndex = 4; @@ -126,7 +150,7 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 567); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(49, 600); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(125, 27); this.maskedTextBoxNumber.TabIndex = 3; @@ -143,7 +167,7 @@ // // buttonRemoveBoat // - this.buttonRemoveBoat.Location = new System.Drawing.Point(21, 620); + this.buttonRemoveBoat.Location = new System.Drawing.Point(20, 642); this.buttonRemoveBoat.Name = "buttonRemoveBoat"; this.buttonRemoveBoat.Size = new System.Drawing.Size(180, 34); this.buttonRemoveBoat.TabIndex = 1; @@ -153,7 +177,7 @@ // // buttonAddBoat // - this.buttonAddBoat.Location = new System.Drawing.Point(20, 490); + this.buttonAddBoat.Location = new System.Drawing.Point(20, 544); this.buttonAddBoat.Name = "buttonAddBoat"; this.buttonAddBoat.Size = new System.Drawing.Size(180, 34); this.buttonAddBoat.TabIndex = 0; @@ -189,14 +213,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 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(177, 26); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -251,5 +275,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/Sailboat/Sailboat/FormBoatCollection.cs b/Sailboat/Sailboat/FormBoatCollection.cs index 7f90c09..2fe23d0 100644 --- a/Sailboat/Sailboat/FormBoatCollection.cs +++ b/Sailboat/Sailboat/FormBoatCollection.cs @@ -219,5 +219,25 @@ namespace Sailboat } } } + + private void buttonSortByType_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByType()); + + private void CompareBoats(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.ShowBoats(); + } + + private void buttonSortByColor_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByColor()); } } diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs index 45817e1..5ed07e7 100644 --- a/Sailboat/Sailboat/SetGeneric.cs +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -22,6 +22,7 @@ namespace Sailboat.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Конструктор /// @@ -36,9 +37,9 @@ namespace Sailboat.Generics /// /// Добавляемая лодка /// - public bool Insert(T boat) + public bool Insert(T boat, IEqualityComparer? equal = null) { - return Insert(boat, 0); + return Insert(boat, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию @@ -46,13 +47,16 @@ namespace Sailboat.Generics /// Добавляемая лодка /// Позиция /// - public bool Insert(T boat, int position) + public bool Insert(T boat, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new BoatNotFoundException(position); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + + if (equal != null && _places.Contains(boat, equal)) + return false; _places.Insert(0, boat); return true; }