diff --git a/SailBoat/SailBoat/FormBoatCollection.Designer.cs b/SailBoat/SailBoat/FormBoatCollection.Designer.cs index 7ad46d2..03286c6 100644 --- a/SailBoat/SailBoat/FormBoatCollection.Designer.cs +++ b/SailBoat/SailBoat/FormBoatCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { tools = new GroupBox(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); sets = new GroupBox(); ButtonDelObject = new Button(); listBoxStorages = new ListBox(); @@ -53,6 +55,8 @@ // // tools // + tools.Controls.Add(ButtonSortByColor); + tools.Controls.Add(ButtonSortByType); tools.Controls.Add(sets); tools.Controls.Add(maskedTextBoxNumber); tools.Controls.Add(buttonRefreshCollection); @@ -66,6 +70,26 @@ tools.TabStop = false; tools.Text = "Инструменты"; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(12, 353); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(176, 45); + ButtonSortByColor.TabIndex = 7; + ButtonSortByColor.Text = "Сортировать по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(12, 305); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(176, 42); + ButtonSortByType.TabIndex = 6; + ButtonSortByType.Text = "Сортировать по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // sets // sets.Controls.Add(ButtonDelObject); @@ -74,14 +98,14 @@ sets.Controls.Add(textBoxStorageName); sets.Location = new Point(6, 22); sets.Name = "sets"; - sets.Size = new Size(188, 344); + sets.Size = new Size(188, 257); sets.TabIndex = 5; sets.TabStop = false; sets.Text = "Наборы"; // // ButtonDelObject // - ButtonDelObject.Location = new Point(6, 263); + ButtonDelObject.Location = new Point(6, 208); ButtonDelObject.Name = "ButtonDelObject"; ButtonDelObject.Size = new Size(176, 41); ButtonDelObject.TabIndex = 3; @@ -93,7 +117,7 @@ // listBoxStorages.FormattingEnabled = true; listBoxStorages.ItemHeight = 15; - listBoxStorages.Location = new Point(6, 148); + listBoxStorages.Location = new Point(6, 93); listBoxStorages.Name = "listBoxStorages"; listBoxStorages.Size = new Size(176, 109); listBoxStorages.TabIndex = 2; @@ -101,7 +125,7 @@ // // ButtonAddObject // - ButtonAddObject.Location = new Point(6, 39); + ButtonAddObject.Location = new Point(6, 17); ButtonAddObject.Name = "ButtonAddObject"; ButtonAddObject.Size = new Size(176, 41); ButtonAddObject.TabIndex = 1; @@ -111,14 +135,14 @@ // // textBoxStorageName // - textBoxStorageName.Location = new Point(6, 97); + textBoxStorageName.Location = new Point(6, 64); textBoxStorageName.Name = "textBoxStorageName"; textBoxStorageName.Size = new Size(176, 23); textBoxStorageName.TabIndex = 0; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(45, 454); + maskedTextBoxNumber.Location = new Point(45, 488); maskedTextBoxNumber.Mask = "00000"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(112, 23); @@ -127,7 +151,7 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(6, 545); + buttonRefreshCollection.Location = new Point(6, 562); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(188, 39); buttonRefreshCollection.TabIndex = 3; @@ -137,7 +161,7 @@ // // buttonRemoveBoat // - buttonRemoveBoat.Location = new Point(6, 483); + buttonRemoveBoat.Location = new Point(6, 517); buttonRemoveBoat.Name = "buttonRemoveBoat"; buttonRemoveBoat.Size = new Size(188, 39); buttonRemoveBoat.TabIndex = 1; @@ -147,7 +171,7 @@ // // buttonAddBoat // - buttonAddBoat.Location = new Point(6, 372); + buttonAddBoat.Location = new Point(6, 421); buttonAddBoat.Name = "buttonAddBoat"; buttonAddBoat.Size = new Size(188, 39); buttonAddBoat.TabIndex = 0; @@ -183,14 +207,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(180, 22); + SaveToolStripMenuItem.Size = new Size(141, 22); SaveToolStripMenuItem.Text = "Сохранение"; SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(180, 22); + LoadToolStripMenuItem.Size = new Size(141, 22); LoadToolStripMenuItem.Text = "Загрузка"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -243,5 +267,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 e7f3c3a..de05a5f 100644 --- a/SailBoat/SailBoat/FormBoatCollection.cs +++ b/SailBoat/SailBoat/FormBoatCollection.cs @@ -107,7 +107,13 @@ namespace SailBoat _logger.LogWarning("Не удалось добавить объект"); } } - catch(StorageOverflowException ex) { + catch (StorageOverflowException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"{ex.Message}. Не удалось добавить объект"); + } + catch (Exception ex) + { MessageBox.Show(ex.Message); _logger.LogWarning($"{ex.Message}. Не удалось добавить объект"); } @@ -150,7 +156,8 @@ namespace SailBoat MessageBox.Show("Не удалось удалить объект"); _logger.LogWarning("Не удалось удалить объект"); } - } catch(BoatNotFoundException ex) + } + catch (BoatNotFoundException ex) { MessageBox.Show(ex.Message); _logger.LogWarning($"{ex.Message}. Не удалось удалить объект"); @@ -175,7 +182,7 @@ namespace SailBoat MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.LogInformation("Сохранение"); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning("Ошибка сохранения"); @@ -185,7 +192,7 @@ namespace SailBoat private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - if(openFileDialog.ShowDialog() == DialogResult.OK) + if (openFileDialog.ShowDialog() == DialogResult.OK) { try { @@ -194,12 +201,31 @@ namespace SailBoat MessageBox.Show("Загрузка произошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.LogInformation("Загрузка"); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning("Ошибка загрузки"); } } } + + 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/Generics/BoatCompareByColor.cs b/SailBoat/SailBoat/Generics/BoatCompareByColor.cs new file mode 100644 index 0000000..9d3a27f --- /dev/null +++ b/SailBoat/SailBoat/Generics/BoatCompareByColor.cs @@ -0,0 +1,43 @@ +using SailBoat.DrawningObjects; +using SailBoat.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SailBoat.Generics +{ + internal class BoatCompareByColor : IComparer + { + public int Compare(DrawningBoat? x, DrawningBoat? 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.EntityBoat.BodyColor != y.EntityBoat.BodyColor) + { + return x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name); + } + if (x.EntityBoat is EntitySailBoat _SailBoatX && y.EntityBoat is EntitySailBoat _SailBoatY) + { + var ElementsColorCompare = _SailBoatX.AdditionalColor.Name.CompareTo(_SailBoatY.AdditionalColor.Name); + if (ElementsColorCompare != 0) + { + return ElementsColorCompare; + } + } + 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/Generics/BoatCompareByType.cs b/SailBoat/SailBoat/Generics/BoatCompareByType.cs new file mode 100644 index 0000000..189eb1e --- /dev/null +++ b/SailBoat/SailBoat/Generics/BoatCompareByType.cs @@ -0,0 +1,34 @@ +using SailBoat.DrawningObjects; +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(DrawningBoat? x, DrawningBoat? 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/Generics/BoatsCollectionInfo.cs b/SailBoat/SailBoat/Generics/BoatsCollectionInfo.cs new file mode 100644 index 0000000..03e7496 --- /dev/null +++ b/SailBoat/SailBoat/Generics/BoatsCollectionInfo.cs @@ -0,0 +1,35 @@ +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) + { + return Name == other.Name; + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } + public override string ToString() + { + return Name; + } + } +} diff --git a/SailBoat/SailBoat/Generics/BoatsGenericCollection.cs b/SailBoat/SailBoat/Generics/BoatsGenericCollection.cs index 0d56e6a..20cbc62 100644 --- a/SailBoat/SailBoat/Generics/BoatsGenericCollection.cs +++ b/SailBoat/SailBoat/Generics/BoatsGenericCollection.cs @@ -35,7 +35,7 @@ namespace SailBoat.Generics public static bool operator +(BoatsGenericCollection collect, T? obj) { if (obj == null) return false; - return collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawingBoatEqutables()); } public static T? operator -(BoatsGenericCollection collect, int pos) @@ -90,5 +90,7 @@ namespace SailBoat.Generics } public IEnumerable GetBoats => _collection.GetBoats(); + + public void Sort(IComparer comparer) => _collection.SortSet(comparer); } } diff --git a/SailBoat/SailBoat/Generics/BoatsGenericStorage.cs b/SailBoat/SailBoat/Generics/BoatsGenericStorage.cs index 35f5575..ab77c8b 100644 --- a/SailBoat/SailBoat/Generics/BoatsGenericStorage.cs +++ b/SailBoat/SailBoat/Generics/BoatsGenericStorage.cs @@ -10,9 +10,9 @@ namespace SailBoat.Generics { internal class BoatsGenericStorage { - readonly Dictionary> _boatStorages; + readonly Dictionary> _boatStorages; - public List Keys => _boatStorages.Keys.ToList(); + public List Keys => _boatStorages.Keys.ToList(); private readonly int _pictureWidth; @@ -20,24 +20,25 @@ namespace SailBoat.Generics public BoatsGenericStorage(int pictureWidth, int pictureHeight) { - _boatStorages = new Dictionary>(); + _boatStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string Name) { - if (!_boatStorages.ContainsKey(Name)) + + if (!_boatStorages.ContainsKey(new BoatsCollectionInfo(Name, ""))) { - _boatStorages[Name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); + _boatStorages[new BoatsCollectionInfo(Name, "")] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); } } public void DelSet(string Name) { - if (_boatStorages.ContainsKey(Name)) + if (_boatStorages.ContainsKey(new BoatsCollectionInfo(Name, ""))) { - _boatStorages.Remove(Name); + _boatStorages.Remove(new BoatsCollectionInfo(Name, "")); } } @@ -45,7 +46,8 @@ namespace SailBoat.Generics { get { - if (_boatStorages.ContainsKey(ind)) return _boatStorages[ind]; + var info = new BoatsCollectionInfo(ind, ""); + if (_boatStorages.ContainsKey(info)) return _boatStorages[info]; return null; } } @@ -63,7 +65,7 @@ namespace SailBoat.Generics StringBuilder data = new(); - foreach (KeyValuePair < string, BoatsGenericCollection < DrawningBoat, DrawningObjectBoat>> record in _boatStorages) + foreach (KeyValuePair > record in _boatStorages) { StringBuilder records = new(); foreach (DrawningBoat? elem in record.Value.GetBoats) @@ -127,7 +129,7 @@ namespace SailBoat.Generics } } - _boatStorages.Add(record[0], collection); + _boatStorages.Add(new BoatsCollectionInfo(record[0], ""), collection); } return true; diff --git a/SailBoat/SailBoat/Generics/DrawingBoatEqutables.cs b/SailBoat/SailBoat/Generics/DrawingBoatEqutables.cs new file mode 100644 index 0000000..44b6314 --- /dev/null +++ b/SailBoat/SailBoat/Generics/DrawingBoatEqutables.cs @@ -0,0 +1,60 @@ +using SailBoat.DrawningObjects; +using SailBoat.Entities; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SailBoat.Generics +{ + internal class DrawingBoatEqutables : IEqualityComparer + { + public bool Equals(DrawningBoat? x, DrawningBoat? 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 DrawningSailBoat && y is DrawningSailBoat) + { + if ((x.EntityBoat as EntitySailBoat).Sail != (y.EntityBoat as EntitySailBoat).Sail) + { + return false; + } + if((x.EntityBoat as EntitySailBoat).RainforcedBody != (y.EntityBoat as EntitySailBoat).RainforcedBody) + { + return false; + } + } + return true; + + } + + public int GetHashCode([DisallowNull] DrawningBoat obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/SailBoat/SailBoat/Generics/SetGeneric.cs b/SailBoat/SailBoat/Generics/SetGeneric.cs index 8a3deaf..6c582a7 100644 --- a/SailBoat/SailBoat/Generics/SetGeneric.cs +++ b/SailBoat/SailBoat/Generics/SetGeneric.cs @@ -22,12 +22,12 @@ namespace SailBoat.Generics _places = new List(Count); } - public bool Insert(T boat) + public bool Insert(T boat, IEqualityComparer? equal = null) { - return Insert(boat, 0); + return Insert(boat, 0, equal); } - public bool Insert(T boat, int position) + public bool Insert(T boat, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) { @@ -37,6 +37,18 @@ namespace SailBoat.Generics { throw new StorageOverflowException(_maxCount); } + + if(equal != null) + { + foreach(var i in _places) + { + if (equal.Equals(i, boat)) + { + throw new ApplicationException($"Объект уже существует"); + } + } + } + _places.Insert(position, boat); return true; } @@ -81,5 +93,7 @@ namespace SailBoat.Generics } } } + + public void SortSet(IComparer comparer) => _places.Sort(comparer); } }