From 737406f41272ab1d5de624062683f4f115a60c69 Mon Sep 17 00:00:00 2001 From: Arkadiy Radaev Date: Wed, 13 Dec 2023 11:58:02 +0400 Subject: [PATCH] =?UTF-8?q?"=D0=A4=D0=B8=D0=BB=D0=BE=D1=81=D0=BE=D1=84?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B5=D1=81=D1=82=D1=8C=20=D0=BF=D0=BE=D0=B7?= =?UTF-8?q?=D0=BD=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=82=D0=BE=D0=B3=D0=BE,=20?= =?UTF-8?q?=D1=87=D1=82=D0=BE=20=D0=B5=D1=81=D1=82=D1=8C"=20-=20=D0=93?= =?UTF-8?q?=D0=B5=D0=B3=D0=B5=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Catamaran/CatamaranCollectionInfo.cs | 27 +++++++++ Catamaran/CatamaranCompareByColor.cs | 48 +++++++++++++++ Catamaran/CatamaranCompareByType.cs | 34 +++++++++++ Catamaran/CatamaransGenericCollection.cs | 7 ++- Catamaran/CatamaransGenericStorage.cs | 28 ++++----- Catamaran/DrawningCatamaranEqutables.cs | 59 +++++++++++++++++++ Catamaran/FormCatamaranCollection.Designer.cs | 42 ++++++++++--- Catamaran/FormCatamaranCollection.cs | 43 +++++++++++--- Catamaran/SetGeneric.cs | 12 +++- 9 files changed, 264 insertions(+), 36 deletions(-) create mode 100644 Catamaran/CatamaranCollectionInfo.cs create mode 100644 Catamaran/CatamaranCompareByColor.cs create mode 100644 Catamaran/CatamaranCompareByType.cs create mode 100644 Catamaran/DrawningCatamaranEqutables.cs diff --git a/Catamaran/CatamaranCollectionInfo.cs b/Catamaran/CatamaranCollectionInfo.cs new file mode 100644 index 0000000..9129083 --- /dev/null +++ b/Catamaran/CatamaranCollectionInfo.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class CatamaranCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public CatamaranCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(CatamaranCollectionInfo? other) + { + return Name == other.Name; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/Catamaran/CatamaranCompareByColor.cs b/Catamaran/CatamaranCompareByColor.cs new file mode 100644 index 0000000..dc18514 --- /dev/null +++ b/Catamaran/CatamaranCompareByColor.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class CatamaranCompareByColor: IComparer + { + public int Compare(DrawningCatamaran? x, DrawningCatamaran? y) + { + if (x == null || x.EntityCatamaran == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityCatamaran == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityCatamaran.BodyColor.Name != y.EntityCatamaran.BodyColor.Name) + { + return x.EntityCatamaran.BodyColor.Name.CompareTo(y.EntityCatamaran.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawningCatamaran) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawningCatamaranPro) + { + EntityCatamaranPro EntityX = (EntityCatamaranPro)x.EntityCatamaran; + EntityCatamaranPro EntityY = (EntityCatamaranPro)y.EntityCatamaran; + if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name) + { + return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name); + } + } + var speedCompare = x.EntityCatamaran.Speed.CompareTo(y.EntityCatamaran.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityCatamaran.Weight.CompareTo(y.EntityCatamaran.Weight); + } + + } +} diff --git a/Catamaran/CatamaranCompareByType.cs b/Catamaran/CatamaranCompareByType.cs new file mode 100644 index 0000000..2a5278d --- /dev/null +++ b/Catamaran/CatamaranCompareByType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class CatamaranCompareByType: IComparer + { + public int Compare(DrawningCatamaran? x, DrawningCatamaran? y) + { + if (x == null || x.EntityCatamaran == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCatamaran == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = x.EntityCatamaran.Speed.CompareTo(y.EntityCatamaran.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityCatamaran.Weight.CompareTo(y.EntityCatamaran.Weight); + } + + } +} diff --git a/Catamaran/CatamaransGenericCollection.cs b/Catamaran/CatamaransGenericCollection.cs index 5bf691f..a334a59 100644 --- a/Catamaran/CatamaransGenericCollection.cs +++ b/Catamaran/CatamaransGenericCollection.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.VisualBasic.Logging; namespace Catamaran { @@ -21,7 +22,9 @@ namespace Catamaran private readonly int _placeSizeHeight = 90; private readonly SetGeneric _collection; - + + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + public CatamaransGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -35,7 +38,7 @@ namespace Catamaran { if (obj == null) return false; - return collect?._collection.Insert(obj) ?? false; + return collect?._collection.Insert(obj, new DrawningCatamaranEqutables()) ?? false; } public static T? operator -(CatamaransGenericCollection collect, int pos) diff --git a/Catamaran/CatamaransGenericStorage.cs b/Catamaran/CatamaransGenericStorage.cs index bd638db..c775f12 100644 --- a/Catamaran/CatamaransGenericStorage.cs +++ b/Catamaran/CatamaransGenericStorage.cs @@ -8,10 +8,9 @@ namespace Catamaran { internal class CatamaransGenericStorage { - readonly Dictionary> _catStorages; - - public List Keys => _catStorages.Keys.ToList(); + + readonly Dictionary> _catStorages; + public List Keys => _catStorages.Keys.ToList(); private readonly int _pictureWidth; @@ -25,8 +24,8 @@ namespace Catamaran public CatamaransGenericStorage(int pictureWidth, int pictureHeight) { - _catStorages = new Dictionary>(); + _catStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -38,7 +37,7 @@ namespace Catamaran File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _catStorages) { StringBuilder records = new(); @@ -46,7 +45,7 @@ namespace Catamaran { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) @@ -108,7 +107,7 @@ namespace Catamaran } } } - _catStorages.Add(record[0], collection); + _catStorages.Add(new CatamaranCollectionInfo(record[0], string.Empty), collection); str = sr.ReadLine(); } while (str != null); @@ -118,22 +117,23 @@ namespace Catamaran public void AddSet(string name) { - _catStorages.Add(name, new CatamaransGenericCollection(_pictureWidth, _pictureHeight)); + _catStorages.Add(new CatamaranCollectionInfo(name, string.Empty), new CatamaransGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { - if (!_catStorages.ContainsKey(name)) + if (!_catStorages.ContainsKey(new CatamaranCollectionInfo(name, string.Empty))) return; - _catStorages.Remove(name); + _catStorages.Remove(new CatamaranCollectionInfo(name, string.Empty)); } public CatamaransGenericCollection? this[string ind] { get { - if (_catStorages.ContainsKey(ind)) - return _catStorages[ind]; + CatamaranCollectionInfo indObj = new CatamaranCollectionInfo(ind, string.Empty); + if (_catStorages.ContainsKey(indObj)) + return _catStorages[indObj]; return null; } } diff --git a/Catamaran/DrawningCatamaranEqutables.cs b/Catamaran/DrawningCatamaranEqutables.cs new file mode 100644 index 0000000..33c8a9d --- /dev/null +++ b/Catamaran/DrawningCatamaranEqutables.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; + +namespace Catamaran +{ + internal class DrawningCatamaranEqutables: IEqualityComparer + { + public bool Equals(DrawningCatamaran? x, DrawningCatamaran? y) + { + if (x == null || x.EntityCatamaran == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityCatamaran == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityCatamaran.Speed != y.EntityCatamaran.Speed) + { + return false; + } + if (x.EntityCatamaran.Weight != y.EntityCatamaran.Weight) + { + return false; + } + if (x.EntityCatamaran.BodyColor != y.EntityCatamaran.BodyColor) + { + return false; + } + if (x is DrawningCatamaranPro && y is DrawningCatamaranPro) + { + EntityCatamaranPro EntityX = (EntityCatamaranPro)x.EntityCatamaran; + EntityCatamaranPro EntityY = (EntityCatamaranPro)y.EntityCatamaran; + if (EntityX.Motor != EntityY.Motor) + return false; + if (EntityX.BodyKit != EntityY.BodyKit) + return false; + if (EntityX.Sail != EntityY.Sail) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningCatamaran obj) + { + return obj.GetHashCode(); + } + + } +} diff --git a/Catamaran/FormCatamaranCollection.Designer.cs b/Catamaran/FormCatamaranCollection.Designer.cs index 28c6ec8..a0153be 100644 --- a/Catamaran/FormCatamaranCollection.Designer.cs +++ b/Catamaran/FormCatamaranCollection.Designer.cs @@ -21,6 +21,8 @@ { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormCatamaranCollection)); groupBoxCollection=new GroupBox(); + buttonSortByColor=new Button(); + buttonSortByType=new Button(); setsBox=new GroupBox(); DeleteSetButton=new Button(); SetslistBox=new ListBox(); @@ -95,6 +97,8 @@ // // groupBoxCollection // + groupBoxCollection.Controls.Add(buttonSortByColor); + groupBoxCollection.Controls.Add(buttonSortByType); groupBoxCollection.Controls.Add(setsBox); groupBoxCollection.Controls.Add(maskedTextBox); groupBoxCollection.Controls.Add(buttonUpdateCollection); @@ -107,13 +111,33 @@ groupBoxCollection.TabStop=false; groupBoxCollection.Text="Инструменты"; // + // buttonSortByColor + // + buttonSortByColor.Location=new Point(21, 410); + buttonSortByColor.Name="buttonSortByColor"; + buttonSortByColor.Size=new Size(204, 34); + buttonSortByColor.TabIndex=5; + buttonSortByColor.Text="Сортировать по цвету"; + buttonSortByColor.UseVisualStyleBackColor=true; + buttonSortByColor.Click+=ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location=new Point(21, 361); + buttonSortByType.Name="buttonSortByType"; + buttonSortByType.Size=new Size(204, 34); + buttonSortByType.TabIndex=4; + buttonSortByType.Text="Сортировать по типу"; + buttonSortByType.UseVisualStyleBackColor=true; + buttonSortByType.Click+=ButtonSortByType_Click; + // // setsBox // setsBox.Controls.Add(DeleteSetButton); setsBox.Controls.Add(SetslistBox); setsBox.Controls.Add(setAddBox); setsBox.Controls.Add(AddSetButton); - setsBox.Location=new Point(6, 41); + setsBox.Location=new Point(6, 30); setsBox.Name="setsBox"; setsBox.RightToLeft=RightToLeft.No; setsBox.Size=new Size(219, 305); @@ -160,16 +184,16 @@ // // maskedTextBox // - maskedTextBox.Location=new Point(21, 457); + maskedTextBox.Location=new Point(21, 525); maskedTextBox.Name="maskedTextBox"; maskedTextBox.Size=new Size(204, 31); maskedTextBox.TabIndex=2; // // buttonUpdateCollection // - buttonUpdateCollection.Location=new Point(21, 577); + buttonUpdateCollection.Location=new Point(21, 606); buttonUpdateCollection.Name="buttonUpdateCollection"; - buttonUpdateCollection.Size=new Size(204, 54); + buttonUpdateCollection.Size=new Size(204, 39); buttonUpdateCollection.TabIndex=2; buttonUpdateCollection.Text="Обновить коллекцию"; buttonUpdateCollection.UseVisualStyleBackColor=true; @@ -177,9 +201,9 @@ // // buttonDeleteCat // - buttonDeleteCat.Location=new Point(21, 504); + buttonDeleteCat.Location=new Point(21, 562); buttonDeleteCat.Name="buttonDeleteCat"; - buttonDeleteCat.Size=new Size(204, 54); + buttonDeleteCat.Size=new Size(204, 38); buttonDeleteCat.TabIndex=1; buttonDeleteCat.Text="Удалить катамаран"; buttonDeleteCat.UseVisualStyleBackColor=true; @@ -187,9 +211,9 @@ // // buttonAddCat // - buttonAddCat.Location=new Point(21, 385); + buttonAddCat.Location=new Point(21, 481); buttonAddCat.Name="buttonAddCat"; - buttonAddCat.Size=new Size(204, 54); + buttonAddCat.Size=new Size(204, 38); buttonAddCat.TabIndex=0; buttonAddCat.Text="Добавить катамаран"; buttonAddCat.UseVisualStyleBackColor=true; @@ -661,5 +685,7 @@ private ToolStripMenuItem файлToolStripMenuItem; private ToolStripMenuItem сохранитьToolStripMenuItem2; private ToolStripMenuItem загрузитьToolStripMenuItem; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/Catamaran/FormCatamaranCollection.cs b/Catamaran/FormCatamaranCollection.cs index 5e30fac..515c29c 100644 --- a/Catamaran/FormCatamaranCollection.cs +++ b/Catamaran/FormCatamaranCollection.cs @@ -31,7 +31,7 @@ namespace Catamaran SetslistBox.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - SetslistBox.Items.Add(_storage.Keys[i]); + SetslistBox.Items.Add(_storage.Keys[i].Name); } if (SetslistBox.Items.Count > 0 && (index == -1 || index >= SetslistBox.Items.Count)) @@ -43,11 +43,11 @@ namespace Catamaran { SetslistBox.SelectedIndex = index; } + } private void ButtonAddStorage_Click(object sender, EventArgs e) { - string storname = setAddBox.Text; - if (string.IsNullOrEmpty(setAddBox.Text) || _storage.Keys.Contains(storname)) + if (string.IsNullOrEmpty(setAddBox.Text)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -55,6 +55,7 @@ namespace Catamaran } _storage.AddSet(setAddBox.Text); ReloadObjects(); + Log.Information($"Добавлен набор: {setAddBox.Text}"); } private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) @@ -71,9 +72,11 @@ namespace Catamaran } if (MessageBox.Show($"Удалить набор {SetslistBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(SetslistBox.SelectedItem.ToString() - ?? string.Empty); + string name = SetslistBox.SelectedItem.ToString() + ?? string.Empty; + _storage.DelSet(name); ReloadObjects(); + Log.Information($"Удален набор: {name}"); } } @@ -106,9 +109,13 @@ namespace Catamaran Log.Warning($"Коллекция {SetslistBox.SelectedItem.ToString() ?? string.Empty} переполнена"); MessageBox.Show(ex.Message); } + catch (ArgumentException) + { + Log.Warning($"Добавляемый объект уже существует в коллекции {SetslistBox.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + } }); form.AddEvent(catamaranDelegate); - } private void ButtonRemoveCatamaran_Click(object sender, EventArgs e) @@ -141,7 +148,7 @@ namespace Catamaran Log.Warning($"Не получилось удалить объект из коллекции {SetslistBox.SelectedItem.ToString() ?? string.Empty}"); MessageBox.Show(ex.Message); } - catch (FormatException ex) + catch (FormatException) { Log.Warning($"Было введено не число"); MessageBox.Show("Введите число"); @@ -177,7 +184,6 @@ namespace Catamaran { Log.Warning("Не удалось сохранить"); MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); - } } } @@ -200,10 +206,29 @@ namespace Catamaran catch (Exception ex) { Log.Warning("Не удалось загрузить"); - MessageBox.Show($"Не загрузилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareCatamarans(new CatamaranCompareByType()); + + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareCatamarans(new CatamaranCompareByColor()); + + private void CompareCatamarans(IComparer comparer) + { + if (SetslistBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[SetslistBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowCats(); + } } } diff --git a/Catamaran/SetGeneric.cs b/Catamaran/SetGeneric.cs index 10ebb7b..727ce73 100644 --- a/Catamaran/SetGeneric.cs +++ b/Catamaran/SetGeneric.cs @@ -18,21 +18,27 @@ namespace Catamaran _maxCount = count; _places = new List(count); } + public void SortSet(IComparer comparer) => _places.Sort(comparer); - public bool Insert(T catamaran) + public bool Insert(T catamaran, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - Insert(catamaran, 0); + Insert(catamaran, 0, equal); return true; } - public bool Insert(T catamaran, int position) + public bool Insert(T catamaran, int position, IEqualityComparer? equal = null) { if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) throw new StorageOverflowException(_maxCount); if (!(position >= 0 && position <= Count)) return false; + if (equal != null) + { + if (_places.Contains(catamaran, equal)) + throw new ArgumentException(nameof(catamaran)); + } _places.Insert(position, catamaran); return true; } -- 2.25.1