diff --git a/Hydroplane/DrawingPlaneEqutables.cs b/Hydroplane/DrawingPlaneEqutables.cs new file mode 100644 index 0000000..fd27bf3 --- /dev/null +++ b/Hydroplane/DrawingPlaneEqutables.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics.CodeAnalysis; + +using Hydroplane.DrawningObjects; +using Hydroplane.Entities; + +namespace Hydroplane +{ + internal class DrawingPlaneEqutables : IEqualityComparer + { + public bool Equals(DrawningPlane? x, DrawningPlane? y) + { + if (x == null || x.EntityPlane == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityPlane == null) + throw new ArgumentNullException(nameof(y)); + if (x.GetType().Name != y.GetType().Name) + return false; + if (x.EntityPlane.Speed != y.EntityPlane.Speed) + return false; + if (x.EntityPlane.Weight != y.EntityPlane.Weight) + return false; + if (x.EntityPlane.BodyColor != y.EntityPlane.BodyColor) + return false; + // to do logic for "сравнения" additional parameters :) + if (x is DrawningHydroplane && y is DrawningHydroplane) + { + if ((x.EntityPlane as EntityHydroplane).AdditionalColor != (y.EntityPlane as EntityHydroplane).AdditionalColor) + { + return false; + } + if ((x.EntityPlane as EntityHydroplane).Boat != (y.EntityPlane as EntityHydroplane).Boat) + { + return false; + } + if ((x.EntityPlane as EntityHydroplane).Bobber != (y.EntityPlane as EntityHydroplane).Bobber) + { + return false; + } + } + + return true; + } + + public int GetHashCode([DisallowNull] DrawningPlane obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Hydroplane/FormHydroplaneCollection.Designer.cs b/Hydroplane/FormHydroplaneCollection.Designer.cs index 6b1ea78..c7abe6f 100644 --- a/Hydroplane/FormHydroplaneCollection.Designer.cs +++ b/Hydroplane/FormHydroplaneCollection.Designer.cs @@ -47,6 +47,8 @@ loadToolStripMenuItem = new ToolStripMenuItem(); SaveFileDialog = new SaveFileDialog(); OpenFileDialog = new OpenFileDialog(); + buttonSortByType = new Button(); + buttonSortByColor = new Button(); panel1.SuspendLayout(); panel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)DrawPlane).BeginInit(); @@ -55,6 +57,8 @@ // // panel1 // + panel1.Controls.Add(buttonSortByColor); + panel1.Controls.Add(buttonSortByType); panel1.Controls.Add(panel2); panel1.Controls.Add(UpdateButton); panel1.Controls.Add(DeleteButton); @@ -65,7 +69,7 @@ panel1.Location = new Point(646, 24); panel1.Margin = new Padding(3, 2, 3, 2); panel1.Name = "panel1"; - panel1.Size = new Size(219, 362); + panel1.Size = new Size(219, 432); panel1.TabIndex = 0; // // panel2 @@ -133,10 +137,10 @@ // // UpdateButton // - UpdateButton.Location = new Point(9, 301); + UpdateButton.Location = new Point(14, 389); UpdateButton.Margin = new Padding(3, 2, 3, 2); UpdateButton.Name = "UpdateButton"; - UpdateButton.Size = new Size(200, 28); + UpdateButton.Size = new Size(187, 28); UpdateButton.TabIndex = 4; UpdateButton.Text = "Обновить коллекцию"; UpdateButton.UseVisualStyleBackColor = true; @@ -144,10 +148,10 @@ // // DeleteButton // - DeleteButton.Location = new Point(9, 268); + DeleteButton.Location = new Point(14, 356); DeleteButton.Margin = new Padding(3, 2, 3, 2); DeleteButton.Name = "DeleteButton"; - DeleteButton.Size = new Size(200, 28); + DeleteButton.Size = new Size(187, 28); DeleteButton.TabIndex = 3; DeleteButton.Text = "Удалить самолёт"; DeleteButton.UseVisualStyleBackColor = true; @@ -155,10 +159,10 @@ // // AddButton // - AddButton.Location = new Point(9, 212); + AddButton.Location = new Point(14, 300); AddButton.Margin = new Padding(3, 2, 3, 2); AddButton.Name = "AddButton"; - AddButton.Size = new Size(200, 28); + AddButton.Size = new Size(187, 28); AddButton.TabIndex = 2; AddButton.Text = "Добавить самолёт"; AddButton.UseVisualStyleBackColor = true; @@ -166,10 +170,10 @@ // // PlaneTextBox // - PlaneTextBox.Location = new Point(9, 244); + PlaneTextBox.Location = new Point(14, 332); PlaneTextBox.Margin = new Padding(3, 2, 3, 2); PlaneTextBox.Name = "PlaneTextBox"; - PlaneTextBox.Size = new Size(200, 23); + PlaneTextBox.Size = new Size(187, 23); PlaneTextBox.TabIndex = 1; // // label1 @@ -187,7 +191,7 @@ DrawPlane.Location = new Point(0, 24); DrawPlane.Margin = new Padding(3, 2, 3, 2); DrawPlane.Name = "DrawPlane"; - DrawPlane.Size = new Size(646, 362); + DrawPlane.Size = new Size(646, 432); DrawPlane.TabIndex = 1; DrawPlane.TabStop = false; // @@ -230,11 +234,31 @@ OpenFileDialog.FileName = "openFileDialog1"; OpenFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + buttonSortByType.Location = new Point(14, 220); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(187, 27); + buttonSortByType.TabIndex = 6; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += buttonSortByType_Click; + // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(14, 253); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(187, 27); + buttonSortByColor.TabIndex = 7; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += buttonSortByColor_Click; + // // FormHydroplaneCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(865, 386); + ClientSize = new Size(865, 456); Controls.Add(DrawPlane); Controls.Add(panel1); Controls.Add(StripMenu); @@ -274,5 +298,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private SaveFileDialog SaveFileDialog; private OpenFileDialog OpenFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/Hydroplane/FormHydroplaneCollection.cs b/Hydroplane/FormHydroplaneCollection.cs index 9a0185b..4c399e1 100644 --- a/Hydroplane/FormHydroplaneCollection.cs +++ b/Hydroplane/FormHydroplaneCollection.cs @@ -66,7 +66,7 @@ namespace Hydroplane MessageBox.Show("Input not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _storage.AddSet(SetTextBox.Text); + _storage.AddSet(SetTextBox.Text.ToString()); ReloadObjects(); _logger.LogInformation($"Added set: {SetTextBox.Text}"); } @@ -75,8 +75,7 @@ namespace Hydroplane /// /// /// - private void ListBoxObjects_SelectedIndexChanged(object sender, - EventArgs e) + private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { DrawPlane.Image = _storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes(); @@ -228,6 +227,7 @@ namespace Hydroplane } } } + private void LoadToolStripMenu_Click(object sender, EventArgs args) { if (OpenFileDialog.ShowDialog() == DialogResult.OK) @@ -246,5 +246,20 @@ namespace Hydroplane } } } + + private void buttonSortByType_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByType()); + + private void buttonSortByColor_Click(object sender, EventArgs e) => ComparePlanes(new PlaneCompareByColor()); + + public void ComparePlanes(IComparer comparer) + { + if (CollectionListBox.SelectedIndex == -1) return; + + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) return; + + obj.Sort(comparer); + DrawPlane.Image = obj.ShowPlanes(); + } } } diff --git a/Hydroplane/FormHydroplaneCollection.resx b/Hydroplane/FormHydroplaneCollection.resx index bd2300d..d4bf422 100644 --- a/Hydroplane/FormHydroplaneCollection.resx +++ b/Hydroplane/FormHydroplaneCollection.resx @@ -127,6 +127,6 @@ 147, 17 - 128 + 52 \ No newline at end of file diff --git a/Hydroplane/PlaneCompareByColor.cs b/Hydroplane/PlaneCompareByColor.cs new file mode 100644 index 0000000..d39b657 --- /dev/null +++ b/Hydroplane/PlaneCompareByColor.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Hydroplane.DrawningObjects; +using Hydroplane.Entities; + +namespace Hydroplane +{ + internal class PlaneCompareByColor : IComparer + { + public int Compare(DrawningPlane? x, DrawningPlane? y) + { + + if (x == null || x.EntityPlane == null) + throw new NotImplementedException(nameof(x)); + + if (y == null || y.EntityPlane == null) + throw new NotImplementedException(nameof(y)); + + var bodyColor = x.EntityPlane.BodyColor.Name.CompareTo(y.EntityPlane.BodyColor.Name); + + if (bodyColor != 0) return bodyColor; + + if (x.EntityPlane is EntityHydroplane && + y.EntityPlane is EntityHydroplane) + { + var addcolor = (x.EntityPlane as EntityHydroplane).AdditionalColor.Name.CompareTo((y.EntityPlane + as EntityHydroplane).AdditionalColor.Name); + if (addcolor != 0) return addcolor; + } + return 1; + } + } +} diff --git a/Hydroplane/PlaneCompareByType.cs b/Hydroplane/PlaneCompareByType.cs new file mode 100644 index 0000000..cccf4ea --- /dev/null +++ b/Hydroplane/PlaneCompareByType.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Hydroplane.DrawningObjects; + +namespace Hydroplane +{ + internal class PlaneCompareByType : IComparer + { + public int Compare(DrawningPlane? x, DrawningPlane? y) + { + if (x == null || x.EntityPlane == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityPlane == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityPlane.Speed.CompareTo(y.EntityPlane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityPlane.Weight.CompareTo(y.EntityPlane.Weight); + } + } +} diff --git a/Hydroplane/PlanesCollectionInfo.cs b/Hydroplane/PlanesCollectionInfo.cs new file mode 100644 index 0000000..07b990b --- /dev/null +++ b/Hydroplane/PlanesCollectionInfo.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hydroplane +{ + internal class PlanesCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public PlanesCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + + public bool Equals(PlanesCollectionInfo other) + { + if (Name != other?.Name) + throw new NotImplementedException(nameof(Name)); + //return false; + return true; + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } + + public override string ToString() + { + return Name; + } + } +} diff --git a/Hydroplane/PlanesGenericCollection.cs b/Hydroplane/PlanesGenericCollection.cs index 55f2712..693fe98 100644 --- a/Hydroplane/PlanesGenericCollection.cs +++ b/Hydroplane/PlanesGenericCollection.cs @@ -33,7 +33,7 @@ namespace Hydroplane.Generics { return false; } - return (bool)collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawingPlaneEqutables()); } public static T? operator -(PlanesGenericCollection collect, int @@ -90,5 +90,7 @@ namespace Hydroplane.Generics } public IEnumerable GetPlanes => _collection.GetPlanes(); + + public void Sort(IComparer comparer) => _collection.SortSet(comparer); } } diff --git a/Hydroplane/SetGeneric.cs b/Hydroplane/SetGeneric.cs index 3476633..e168573 100644 --- a/Hydroplane/SetGeneric.cs +++ b/Hydroplane/SetGeneric.cs @@ -14,24 +14,46 @@ namespace Hydroplane.Generics private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); + public SetGeneric(int count) { _maxCount = count; _places = new List(_maxCount); } - public bool Insert(T plane) + public bool Insert(T plane, IEqualityComparer? equal = null) { + if (equal != null) + { + foreach (var secondPlane in _places) + { + if (equal.Equals(plane, secondPlane)) + { + throw new Exception("Такой объект уже есть в коллекции"); + } + } + } return Insert(plane, 0); } - public bool Insert(T plane, int position) + public bool Insert(T plane, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new StorageOverflowException("Impossible to insert"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); - _places.Insert(0, plane); + if (equal != null) + { + foreach (var secondPlane in _places) + { + if (equal.Equals(plane, secondPlane)) + { + throw new ApplicationException("Такой объект уже есть в коллекции"); + } + } + } + _places.Insert(position, plane); return true; }