diff --git a/base/Catamaran/Catamaran/CatamaranCompareByColor.cs b/base/Catamaran/Catamaran/CatamaranCompareByColor.cs new file mode 100644 index 0000000..67bcd1f --- /dev/null +++ b/base/Catamaran/Catamaran/CatamaranCompareByColor.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Catamaran.DrawningObjects; +using Catamaran.Entities; + +namespace Catamaran.Generics +{ + 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)); + } + var bodyColorCompare = x.EntityCatamaran.BodyColor.Name.CompareTo(y.EntityCatamaran.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityCatamaran is EntitySailCatamaran xEntitySailCatamaran && y.EntityCatamaran is EntitySailCatamaran yEntitySailCatamaran) + { + var dumpBoxColorCompare = xEntitySailCatamaran.BodyColor.Name.CompareTo(yEntitySailCatamaran.BodyColor.Name); + if (dumpBoxColorCompare != 0) + { + return dumpBoxColorCompare; + } + var tentColorCompare = xEntitySailCatamaran.AdditionalColor.Name.CompareTo(yEntitySailCatamaran.AdditionalColor.Name); + if (tentColorCompare != 0) + { + return tentColorCompare; + } + } + var speedCompare = x.EntityCatamaran.Speed.CompareTo(y.EntityCatamaran.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityCatamaran.Weight.CompareTo(y.EntityCatamaran.Weight); + } + } +} \ No newline at end of file diff --git a/base/Catamaran/Catamaran/CatamaranCompareByType.cs b/base/Catamaran/Catamaran/CatamaranCompareByType.cs new file mode 100644 index 0000000..c7c4da6 --- /dev/null +++ b/base/Catamaran/Catamaran/CatamaranCompareByType.cs @@ -0,0 +1,35 @@ +using Catamaran.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.Generics +{ + 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/base/Catamaran/Catamaran/CatamaransCollectionInfo.cs b/base/Catamaran/Catamaran/CatamaransCollectionInfo.cs new file mode 100644 index 0000000..8bcccec --- /dev/null +++ b/base/Catamaran/Catamaran/CatamaransCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran.Generics +{ + internal class CatamaransCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public CatamaransCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(CatamaransCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/base/Catamaran/Catamaran/CatamaransGenericCollection.cs b/base/Catamaran/Catamaran/CatamaransGenericCollection.cs index 0146196..437fca8 100644 --- a/base/Catamaran/Catamaran/CatamaransGenericCollection.cs +++ b/base/Catamaran/Catamaran/CatamaransGenericCollection.cs @@ -41,6 +41,8 @@ namespace Catamaran.Generics /// Получение объектов коллекции /// public IEnumerable GetCatamarans => _collection.GetCatamarans(); + + public void Sort(IComparer comparer) => _collection.SortSet(comparer); /// /// Конструктор /// @@ -66,7 +68,7 @@ namespace Catamaran.Generics { return false; } - return collect?._collection.Insert(obj) ?? false; + return (bool)collect?._collection.Insert(obj, new DrawingCatamaranEqutables()); } /// /// Перегрузка оператора вычитания diff --git a/base/Catamaran/Catamaran/CatamaransGenericStorage.cs b/base/Catamaran/Catamaran/CatamaransGenericStorage.cs index 2808773..2746f88 100644 --- a/base/Catamaran/Catamaran/CatamaransGenericStorage.cs +++ b/base/Catamaran/Catamaran/CatamaransGenericStorage.cs @@ -12,12 +12,12 @@ namespace Catamaran.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _catamaranStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _catamaranStorages.Keys.ToList(); + public List Keys => _catamaranStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -45,7 +45,7 @@ namespace Catamaran.Generics /// public CatamaransGenericStorage(int pictureWidth, int pictureHeight) { - _catamaranStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -57,8 +57,7 @@ namespace Catamaran.Generics public void AddSet(string name) { // TODO Прописать логику для добавления - if (_catamaranStorages.ContainsKey(name)) return; - _catamaranStorages[name] = new CatamaransGenericCollection(_pictureWidth, _pictureHeight); + _catamaranStorages.Add(new CatamaransCollectionInfo(name, string.Empty), new CatamaransGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -67,9 +66,9 @@ namespace Catamaran.Generics public void DelSet(string name) { // TODO Прописать логику для удаления - if (!_catamaranStorages.ContainsKey(name)) + if (!_catamaranStorages.ContainsKey(new CatamaransCollectionInfo(name, string.Empty))) return; - _catamaranStorages.Remove(name); + _catamaranStorages.Remove(new CatamaransCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -82,8 +81,9 @@ namespace Catamaran.Generics get { // TODO Продумать логику получения набора - if (_catamaranStorages.ContainsKey(ind)) - return _catamaranStorages[ind]; + CatamaransCollectionInfo indObj = new CatamaransCollectionInfo(ind, string.Empty); + if (_catamaranStorages.ContainsKey(indObj)) + return _catamaranStorages[indObj]; return null; } } @@ -99,14 +99,14 @@ namespace Catamaran.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _catamaranStorages) + foreach (KeyValuePair> record in _catamaranStorages) { StringBuilder records = new(); foreach (DrawningCatamaran? elem in record.Value.GetCatamarans) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -173,7 +173,7 @@ namespace Catamaran.Generics } } } - _catamaranStorages.Add(record[0], collection); + _catamaranStorages.Add(new CatamaransCollectionInfo(record[0], string.Empty), collection); } } } diff --git a/base/Catamaran/Catamaran/DrawingCatamaranEqutables.cs b/base/Catamaran/Catamaran/DrawingCatamaranEqutables.cs new file mode 100644 index 0000000..33b1be8 --- /dev/null +++ b/base/Catamaran/Catamaran/DrawingCatamaranEqutables.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 Catamaran.DrawningObjects; +using Catamaran.Entities; + +namespace Catamaran.Generics +{ + internal class DrawingCatamaranEqutables : 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 DrawningSailCatamaran && y is DrawningSailCatamaran) + { + EntitySailCatamaran EntityX = (EntitySailCatamaran)x.EntityCatamaran; + EntitySailCatamaran EntityY = (EntitySailCatamaran)y.EntityCatamaran; + if (EntityX.Sail != EntityY.Sail) + return false; + if (EntityX.FloatDetail != EntityY.FloatDetail) + return false; + if (EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningCatamaran obj) + { + return obj.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs b/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs index a057197..0bb694a 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs @@ -45,6 +45,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.SortByType = new System.Windows.Forms.Button(); + this.SortByColor = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.groupBoxTools.SuspendLayout(); this.groupBoxSets.SuspendLayout(); @@ -61,6 +63,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.SortByColor); + this.groupBoxTools.Controls.Add(this.SortByType); this.groupBoxTools.Controls.Add(this.groupBoxSets); this.groupBoxTools.Controls.Add(this.maskedTextBoxNumber); this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection); @@ -80,7 +84,7 @@ this.groupBoxSets.Controls.Add(this.buttonDelObject); this.groupBoxSets.Controls.Add(this.listBoxStorages); this.groupBoxSets.Controls.Add(this.buttonAddObject); - this.groupBoxSets.Location = new System.Drawing.Point(3, 63); + this.groupBoxSets.Location = new System.Drawing.Point(3, 46); this.groupBoxSets.Name = "groupBoxSets"; this.groupBoxSets.Size = new System.Drawing.Size(138, 203); this.groupBoxSets.TabIndex = 4; @@ -126,7 +130,7 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 321); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(0, 351); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(137, 23); this.maskedTextBoxNumber.TabIndex = 3; @@ -143,7 +147,7 @@ // // ButtonRemoveCatamaran // - this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(0, 363); + this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(-1, 380); this.ButtonRemoveCatamaran.Name = "ButtonRemoveCatamaran"; this.ButtonRemoveCatamaran.Size = new System.Drawing.Size(140, 34); this.ButtonRemoveCatamaran.TabIndex = 1; @@ -153,9 +157,9 @@ // // ButtonAddCatamaran // - this.ButtonAddCatamaran.Location = new System.Drawing.Point(3, 270); + this.ButtonAddCatamaran.Location = new System.Drawing.Point(-1, 311); this.ButtonAddCatamaran.Name = "ButtonAddCatamaran"; - this.ButtonAddCatamaran.Size = new System.Drawing.Size(137, 34); + this.ButtonAddCatamaran.Size = new System.Drawing.Size(140, 34); this.ButtonAddCatamaran.TabIndex = 0; this.ButtonAddCatamaran.Text = "Добавить катамаран"; this.ButtonAddCatamaran.UseVisualStyleBackColor = true; @@ -183,14 +187,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -198,6 +202,26 @@ // this.openFileDialog.FileName = "openFileDialog1"; // + // SortByType + // + this.SortByType.Location = new System.Drawing.Point(-1, 255); + this.SortByType.Name = "SortByType"; + this.SortByType.Size = new System.Drawing.Size(140, 23); + this.SortByType.TabIndex = 6; + this.SortByType.Text = "Сортировка по типу"; + this.SortByType.UseVisualStyleBackColor = true; + this.SortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // + // SortByColor + // + this.SortByColor.Location = new System.Drawing.Point(0, 282); + this.SortByColor.Name = "SortByColor"; + this.SortByColor.Size = new System.Drawing.Size(139, 23); + this.SortByColor.TabIndex = 7; + this.SortByColor.Text = "Сортировка по цвету"; + this.SortByColor.UseVisualStyleBackColor = true; + this.SortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // // FormCatamaranCollection // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -238,5 +262,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button SortByColor; + private Button SortByType; } } \ No newline at end of file diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.cs b/base/Catamaran/Catamaran/FormCatamaranCollection.cs index b0fe8d4..6f31b59 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.cs +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.cs @@ -49,7 +49,7 @@ namespace Catamaran listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(_storage.Keys[i]); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) @@ -106,6 +106,10 @@ namespace Catamaran MessageBox.Show(ex.Message); _logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}"); } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + } } } /// @@ -277,5 +281,22 @@ namespace Catamaran ReloadObjects(); } } + private void buttonSortByType_Click(object sender, EventArgs e) => CompareCatamarans(new CatamaranCompareByType()); + private void CompareCatamarans(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.ShowCatamarans(); + } + private void buttonSortByColor_Click(object sender, EventArgs e) => CompareCatamarans(new CatamaranCompareByColor()); } } \ No newline at end of file diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.resx b/base/Catamaran/Catamaran/FormCatamaranCollection.resx index e9d6757..e22daa9 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.resx +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.resx @@ -67,6 +67,6 @@ 254, 17 - 96 + 25 \ No newline at end of file diff --git a/base/Catamaran/Catamaran/SetGeneric.cs b/base/Catamaran/Catamaran/SetGeneric.cs index b767957..20a46d3 100644 --- a/base/Catamaran/Catamaran/SetGeneric.cs +++ b/base/Catamaran/Catamaran/SetGeneric.cs @@ -27,6 +27,8 @@ namespace Catamaran.Generics /// Максимальное количество объектов в списке /// private readonly int _maxCount; + + public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Конструктор /// @@ -41,23 +43,24 @@ namespace Catamaran.Generics /// /// Добавляемый катамаран /// - public bool Insert(T catamaran) + public bool Insert(T catamaran, IEqualityComparer? equal = null) { - return Insert(catamaran, 0); + return Insert(catamaran, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию /// - /// Добавляемая лодка + /// Добавляемая лодка /// Позиция /// - public bool Insert(T catamaran, int position) - { + public bool Insert(T catamaran, int position, IEqualityComparer? equal = null) + { if (position < 0 || position >= _maxCount) throw new CatamaranNotFoundException(position); - if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + if (equal != null && _places.Contains(catamaran, equal)) + throw new ArgumentException("Данный объект уже есть в коллекции"); _places.Insert(0, catamaran); return true; }