From 7c4cf25b537c06f9b65725c316ce2ff90afb6ba2 Mon Sep 17 00:00:00 2001 From: Kristina Date: Mon, 25 Dec 2023 16:59:01 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D1=84=D0=B8=D0=BA=D1=81=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormBoatCollection.Designer.cs | 42 ++- .../ProjectBoat_bae/FormBoatCollection.cs | 240 ++++++++++++++---- .../Generics/BoatCompareByColor.cs | 37 +++ .../Generics/BoatCompareByType.cs | 34 +++ .../Generics/BoatsCollectionInfo.cs | 36 +++ .../Generics/BoatsGenericCollection.cs | 6 +- .../Generics/BoatsGenericStorage.cs | 4 + .../Generics/DrawingBoatEqutables.cs | 55 ++++ .../ProjectBoat_bae/Generics/SetGeneric.cs | 81 +++++- 9 files changed, 464 insertions(+), 71 deletions(-) create mode 100644 ProjectBoat_base/ProjectBoat_bae/Generics/BoatCompareByColor.cs create mode 100644 ProjectBoat_base/ProjectBoat_bae/Generics/BoatCompareByType.cs create mode 100644 ProjectBoat_base/ProjectBoat_bae/Generics/BoatsCollectionInfo.cs create mode 100644 ProjectBoat_base/ProjectBoat_bae/Generics/DrawingBoatEqutables.cs diff --git a/ProjectBoat_base/ProjectBoat_bae/FormBoatCollection.Designer.cs b/ProjectBoat_base/ProjectBoat_bae/FormBoatCollection.Designer.cs index ac590a7..e385293 100644 --- a/ProjectBoat_base/ProjectBoat_bae/FormBoatCollection.Designer.cs +++ b/ProjectBoat_base/ProjectBoat_bae/FormBoatCollection.Designer.cs @@ -43,6 +43,8 @@ LoadToolStripMenuItem = new ToolStripMenuItem(); openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); + ButtonSortByType = new Button(); + ButtonSortByColor = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); menuStrip1.SuspendLayout(); SuspendLayout(); @@ -59,16 +61,16 @@ // listBoxStorages.FormattingEnabled = true; listBoxStorages.ItemHeight = 25; - listBoxStorages.Location = new Point(935, 146); + listBoxStorages.Location = new Point(935, 130); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(231, 129); + listBoxStorages.Size = new Size(231, 79); listBoxStorages.TabIndex = 1; listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; // // ButtonAddObject // ButtonAddObject.BackColor = SystemColors.ButtonHighlight; - ButtonAddObject.Location = new Point(935, 102); + ButtonAddObject.Location = new Point(935, 86); ButtonAddObject.Name = "ButtonAddObject"; ButtonAddObject.Size = new Size(231, 38); ButtonAddObject.TabIndex = 2; @@ -78,7 +80,7 @@ // // textBoxStorageName // - textBoxStorageName.Location = new Point(935, 65); + textBoxStorageName.Location = new Point(935, 49); textBoxStorageName.Name = "textBoxStorageName"; textBoxStorageName.Size = new Size(231, 31); textBoxStorageName.TabIndex = 3; @@ -86,7 +88,7 @@ // ButtonDelObject // ButtonDelObject.BackColor = SystemColors.ButtonHighlight; - ButtonDelObject.Location = new Point(935, 281); + ButtonDelObject.Location = new Point(935, 215); ButtonDelObject.Name = "ButtonDelObject"; ButtonDelObject.Size = new Size(231, 34); ButtonDelObject.TabIndex = 4; @@ -154,14 +156,14 @@ // SaveToolStripMenuItem // SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(270, 34); + SaveToolStripMenuItem.Size = new Size(212, 34); SaveToolStripMenuItem.Text = "Сохранение"; SaveToolStripMenuItem.Click += SaveToolStripMenu_Click; // // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(270, 34); + LoadToolStripMenuItem.Size = new Size(212, 34); LoadToolStripMenuItem.Text = "Загрузка"; LoadToolStripMenuItem.Click += LoadToolStripMenu_Click; // @@ -174,11 +176,35 @@ // saveFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByType + // + ButtonSortByType.BackColor = SystemColors.ButtonHighlight; + ButtonSortByType.Location = new Point(935, 284); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(231, 34); + ButtonSortByType.TabIndex = 10; + ButtonSortByType.Text = "Сортировать по типу"; + ButtonSortByType.UseVisualStyleBackColor = false; + ButtonSortByType.Click += ButtonSortByType_Click; + // + // ButtonSortByColor + // + ButtonSortByColor.BackColor = SystemColors.ButtonHighlight; + ButtonSortByColor.Location = new Point(935, 329); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(231, 34); + ButtonSortByColor.TabIndex = 11; + ButtonSortByColor.Text = "Сортировать по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = false; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // // FormBoatCollection // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1178, 644); + Controls.Add(ButtonSortByColor); + Controls.Add(ButtonSortByType); Controls.Add(menuStrip1); Controls.Add(textBoxBoat); Controls.Add(ButtonRefreshCollection); @@ -216,5 +242,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/ProjectBoat_base/ProjectBoat_bae/FormBoatCollection.cs b/ProjectBoat_base/ProjectBoat_bae/FormBoatCollection.cs index a20afa8..6a290e1 100644 --- a/ProjectBoat_base/ProjectBoat_bae/FormBoatCollection.cs +++ b/ProjectBoat_base/ProjectBoat_bae/FormBoatCollection.cs @@ -15,6 +15,7 @@ using ProjectBoat_bae.Generics; using ProjectBoat_bae.Exceptions; using ProjectBoat_bae.MovementStrategy; using NLog; +using System.Numerics; namespace ProjectBoat_bae { @@ -24,7 +25,7 @@ namespace ProjectBoat_bae private readonly BoatsGenericStorage _storage; // Логер - private readonly + private readonly Microsoft.Extensions.Logging.ILogger? _logger; public FormBoatCollection(ILogger logger) @@ -59,13 +60,21 @@ namespace ProjectBoat_bae private void ButtonAddObject_Click_1(object sender, EventArgs e) { + //if (string.IsNullOrEmpty(textBoxStorageName.Text)) + //{ + // MessageBox.Show("Не все данные заполнены", "Ошибка", + // MessageBoxButtons.OK, MessageBoxIcon.Error); + // return; + //} + //_storage.AddSet(textBoxStorageName.Text); + //ReloadObjects(); + //_logger.LogInformation($"Added set: {textBoxStorageName.Text}"); if (string.IsNullOrEmpty(textBoxStorageName.Text)) { - MessageBox.Show("Не все данные заполнены", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Input not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _storage.AddSet(textBoxStorageName.Text); + _storage.AddSet(textBoxStorageName.Text.ToString()); ReloadObjects(); _logger.LogInformation($"Added set: {textBoxStorageName.Text}"); } @@ -73,57 +82,52 @@ namespace ProjectBoat_bae // Удаление набора private void ButtonDelObject_Click(object sender, EventArgs e) { + //if (listBoxStorages.SelectedIndex == -1) + //{ + // return; + //} + + //if (string.IsNullOrEmpty(textBoxStorageName.Text)) + //{ + // MessageBox.Show("Не всё заполнено", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + // _logger.LogWarning("Неудачная попытка. Коллекция не добавлена, не все данные заполнены"); + // return; + //} + //_storage.AddSet(textBoxStorageName.Text); + //ReloadObjects(); + + //_logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}"); if (listBoxStorages.SelectedIndex == -1) { return; } - - if (string.IsNullOrEmpty(textBoxStorageName.Text)) + string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty; + if (MessageBox.Show($"Delete Object {name}?", "Deleting", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - MessageBox.Show("Не всё заполнено", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - _logger.LogWarning("Неудачная попытка. Коллекция не добавлена, не все данные заполнены"); - return; + _storage.DelSet(name); + ReloadObjects(); + _logger.LogInformation($"Deleted set: {name}"); } - _storage.AddSet(textBoxStorageName.Text); - ReloadObjects(); - - _logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}"); } // Добавление объекта в набор private void ButtonAddBoat_Click(object sender, EventArgs e) { + //if (listBoxStorages.SelectedIndex == -1) + //{ + // return; + //} + //var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + //if (obj == null) + //{ + // return; + //} - FormBoatConfig formPlaneConfig = new FormBoatConfig(); - formPlaneConfig.AddEvent(AddBoat); - formPlaneConfig.Show(); - } + //FormBoatConfig formPlaneConfig = new FormBoatConfig(); + //formPlaneConfig.AddEvent(AddBoat); + //formPlaneConfig.Show(); - private void AddBoat(Drawningboat boat) - { - boat._pictureWidth = pictureBoxCollection.Width; - boat._pictureHeight = pictureBoxCollection.Height; - if (listBoxStorages.SelectedIndex == -1) return; - var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; - if (obj == null) return; - - if (obj + boat) - { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = obj.ShowBoats(); - _logger.LogInformation($"Добавлен объект {obj}"); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - } - - } - - //удаление объекта - private void ButtonRemoveBoat_Click(object sender, EventArgs e) - { - if (listBoxStorages.SelectedIndex == -1) { return; @@ -133,7 +137,108 @@ namespace ProjectBoat_bae { return; } - if (MessageBox.Show("Удалить объект?", "Удалить", MessageBoxButtons.YesNo, + FormBoatConfig formPlaneConfig = new FormBoatConfig(); + formPlaneConfig.AddEvent(AddBoat); + formPlaneConfig.Show(); + } + + private void AddBoat(Drawningboat boat) + { + //boat._pictureWidth = pictureBoxCollection.Width; + //boat._pictureHeight = pictureBoxCollection.Height; + //if (listBoxStorages.SelectedIndex == -1) return; + //var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + //if (obj == null) return; + + //if (obj + boat) + //{ + // MessageBox.Show("Объект добавлен"); + // pictureBoxCollection.Image = obj.ShowBoats(); + // _logger.LogInformation($"Добавлен объект {obj}"); + //} + //else + //{ + // MessageBox.Show("Не удалось добавить объект"); + //} + + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + _logger.LogWarning("Добавление пустого объекта"); + return; + } + try + { + _ = obj + boat; + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowBoats(); + _logger.LogInformation($"plane added in set {listBoxStorages.SelectedItem.ToString()}"); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"plane not added in set {listBoxStorages.SelectedItem.ToString()}"); + } + + } + + //удаление объекта + private void ButtonRemoveBoat_Click(object sender, EventArgs e) + { + + //if (listBoxStorages.SelectedIndex == -1) + //{ + // return; + //} + //var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + //if (obj == null) + //{ + // return; + //} + //if (MessageBox.Show("Удалить объект?", "Удалить", MessageBoxButtons.YesNo, + // MessageBoxIcon.Question) == DialogResult.No) + //{ + // return; + //} + //try + //{ + // int pos = Convert.ToInt32(textBoxBoat.Text); + // if (obj - pos != null) + // { + // MessageBox.Show("Объект удален"); + // pictureBoxCollection.Image = obj.ShowBoats(); + // _logger.LogInformation($"лодка удалена из набора {listBoxStorages.SelectedItem.ToString()}"); + // } + // else + // { + // MessageBox.Show("Объект не удален"); + // _logger.LogWarning($"лодка не удалена из набора {listBoxStorages.SelectedItem.ToString()}"); + // } + //} + //catch (BoatNotFoundException ex) + //{ + // MessageBox.Show(ex.Message); + // _logger.LogWarning($"BoatNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}"); + //} + //catch (Exception ex) + //{ + // MessageBox.Show("Объекта нет"); + // _logger.LogWarning("Not input"); + //} + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Delete Object?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; @@ -143,24 +248,24 @@ namespace ProjectBoat_bae int pos = Convert.ToInt32(textBoxBoat.Text); if (obj - pos != null) { - MessageBox.Show("Объект удален"); + MessageBox.Show("Object deleted"); pictureBoxCollection.Image = obj.ShowBoats(); - _logger.LogInformation($"лодка удалена из набора {listBoxStorages.SelectedItem.ToString()}"); + _logger.LogInformation($"plane deleted in set {listBoxStorages.SelectedItem.ToString()}"); } else { - MessageBox.Show("Объект не удален"); - _logger.LogWarning($"лодка не удалена из набора {listBoxStorages.SelectedItem.ToString()}"); + MessageBox.Show("Object not deleted"); + _logger.LogWarning($"plane not deleted in set {listBoxStorages.SelectedItem.ToString()}"); } } catch (BoatNotFoundException ex) { MessageBox.Show(ex.Message); - _logger.LogWarning($"BoatNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}"); + _logger.LogWarning($"PlaneNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}"); } catch (Exception ex) { - MessageBox.Show("Объекта нет"); + MessageBox.Show("Not input"); _logger.LogWarning("Not input"); } } @@ -168,12 +273,23 @@ namespace ProjectBoat_bae // Обновление private void ButtonRefreshCollection_Click(object sender, EventArgs e) { + //if (listBoxStorages.SelectedIndex == -1) + //{ + // return; + //} + //var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + //string.Empty]; + //if (obj == null) + //{ + // return; + //} + //pictureBoxCollection.Image = obj.ShowBoats(); + if (listBoxStorages.SelectedIndex == -1) { return; } - var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? - string.Empty]; + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; @@ -184,6 +300,8 @@ namespace ProjectBoat_bae //Выбор набора private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) { + //pictureBoxCollection.Image = + //_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBoats(); pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBoats(); } @@ -191,7 +309,7 @@ namespace ProjectBoat_bae //сохранение private void SaveToolStripMenu_Click(object sender, EventArgs e) { - + if (saveFileDialog.ShowDialog() == DialogResult.OK) { try @@ -213,7 +331,7 @@ namespace ProjectBoat_bae private void LoadToolStripMenu_Click(object sender, EventArgs args) { - + if (openFileDialog.ShowDialog() == DialogResult.OK) { try @@ -229,5 +347,21 @@ namespace ProjectBoat_bae } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBoats(new BoatCompareByType()); + + public 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/ProjectBoat_base/ProjectBoat_bae/Generics/BoatCompareByColor.cs b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatCompareByColor.cs new file mode 100644 index 0000000..17a7736 --- /dev/null +++ b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatCompareByColor.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using ProjectBoat_bae.DrawningObjects; +using ProjectBoat_bae.Entities; + +namespace ProjectBoat_bae.Generics +{ + internal class BoatCompareByColor : IComparer + { + public int Compare(Drawningboat? x, Drawningboat? y) + { + + if (x == null || x.EntityBoat == null) + throw new NotImplementedException(nameof(x)); + + if (y == null || y.EntityBoat == null) + throw new NotImplementedException(nameof(y)); + + var bodyColor = x.EntityBoat.BodyColor.Name.CompareTo(y.EntityBoat.BodyColor.Name); + + if (bodyColor != 0) return bodyColor; + + if (x.EntityBoat is EntityMotorBoat && + y.EntityBoat is EntityMotorBoat) + { + var addcolor = (x.EntityBoat as EntityMotorBoat).AdditionalColor.Name.CompareTo((y.EntityBoat + as EntityMotorBoat).AdditionalColor.Name); + if (addcolor != 0) return addcolor; + } + return 1; + } + } +} diff --git a/ProjectBoat_base/ProjectBoat_bae/Generics/BoatCompareByType.cs b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatCompareByType.cs new file mode 100644 index 0000000..89762b3 --- /dev/null +++ b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatCompareByType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using ProjectBoat_bae.DrawningObjects; +namespace ProjectBoat_bae.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/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsCollectionInfo.cs b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsCollectionInfo.cs new file mode 100644 index 0000000..b7f5c06 --- /dev/null +++ b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsCollectionInfo.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBoat_bae.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) + throw new NotImplementedException(nameof(Name)); + return true; + } + + public override int GetHashCode() + { + return Name.GetHashCode(); + } + + public override string ToString() + { + return Name; + } + } +} diff --git a/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsGenericCollection.cs b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsGenericCollection.cs index 6b11b6c..7f4a2d9 100644 --- a/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsGenericCollection.cs +++ b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsGenericCollection.cs @@ -45,10 +45,11 @@ namespace ProjectBoat_bae.Generics { return false; } - return (bool)collect._collection.Insert(obj); + //return (bool)collect._collection.Insert(obj); + return collect._collection.Insert(obj, new DrawingBoatEqutables()); } - // Перегрузка оператора вычитания + // Перегрузка оператора вычитанияreturn collect._collection.Insert(obj, new DrawingPlaneEqutables()); public static T? operator -(BoatsGenericCollection collect, int pos) { @@ -111,5 +112,6 @@ namespace ProjectBoat_bae.Generics // Получение объектов коллекции public IEnumerable GetBoats => _collection.GetBoats(); + public void Sort(IComparer comparer) => _collection.SortSet(comparer); } } diff --git a/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsGenericStorage.cs b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsGenericStorage.cs index d5d2a6f..a9362dd 100644 --- a/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsGenericStorage.cs +++ b/ProjectBoat_base/ProjectBoat_bae/Generics/BoatsGenericStorage.cs @@ -14,7 +14,9 @@ namespace ProjectBoat_bae.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; @@ -27,6 +29,8 @@ namespace ProjectBoat_bae.Generics } public void AddSet(string name) { + //if (_boatStorages.ContainsKey(name)) return; + //_boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); if (_boatStorages.ContainsKey(name)) return; _boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); } diff --git a/ProjectBoat_base/ProjectBoat_bae/Generics/DrawingBoatEqutables.cs b/ProjectBoat_base/ProjectBoat_bae/Generics/DrawingBoatEqutables.cs new file mode 100644 index 0000000..a417b8a --- /dev/null +++ b/ProjectBoat_base/ProjectBoat_bae/Generics/DrawingBoatEqutables.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectBoat_bae.DrawningObjects; +using ProjectBoat_bae.Entities; + +namespace ProjectBoat_bae.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; + // to do logic for "сравнения" additional parameters :) + if (x is DrawningMotorBoat && y is DrawningMotorBoat) + { + if ((x.EntityBoat as EntityMotorBoat).AdditionalColor != (y.EntityBoat as EntityMotorBoat).AdditionalColor) + { + return false; + } + if ((x.EntityBoat as EntityMotorBoat).Body != (y.EntityBoat as EntityMotorBoat).Body) + { + return false; + } + if ((x.EntityBoat as EntityMotorBoat).Wing != (y.EntityBoat as EntityMotorBoat).Wing) + { + return false; + } + } + + return true; + } + + public int GetHashCode([DisallowNull] Drawningboat obj) + { + return obj.GetHashCode(); + } + } +} + diff --git a/ProjectBoat_base/ProjectBoat_bae/Generics/SetGeneric.cs b/ProjectBoat_base/ProjectBoat_bae/Generics/SetGeneric.cs index 3981658..78336ff 100644 --- a/ProjectBoat_base/ProjectBoat_bae/Generics/SetGeneric.cs +++ b/ProjectBoat_base/ProjectBoat_bae/Generics/SetGeneric.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectBoat_bae.Exceptions; + namespace ProjectBoat_bae.Generics { internal class SetGeneric where T : class @@ -14,6 +16,8 @@ namespace ProjectBoat_bae.Generics // Максимальное количество объектов в списке private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); + // Конструктор public SetGeneric(int count) { @@ -21,27 +25,86 @@ namespace ProjectBoat_bae.Generics _places = new List(_maxCount); } - public bool Insert(T boat) + //public bool Insert(T boat) + //{ + // return Insert(boat, 0); + //} + //public bool Insert(T boat, int position) + //{ + // if (position < 0 || position >= _maxCount) + // return false; + + // if (Count >= _maxCount) + // return false; + // _places.Insert(0, boat); + // return true; + //} + public bool Insert(T boat, IEqualityComparer? equal = null) { + if (equal != null) + { + foreach (var secondBoat in _places) + { + if (equal.Equals(boat, secondBoat)) + { + throw new Exception("Такой объект уже есть в коллекции"); + } + } + } return Insert(boat, 0); } - public bool Insert(T boat, int position) + public bool Insert(T boat, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) - return false; + throw new StorageOverflowException("Impossible to insert"); if (Count >= _maxCount) - return false; - _places.Insert(0, boat); + throw new StorageOverflowException(_maxCount); + if (equal != null) + { + foreach (var secondBoat in _places) + { + if (equal.Equals(boat, secondBoat)) + { + throw new ApplicationException("Такой объект уже есть в коллекции"); + } + } + } + _places.Insert(position, boat); return true; } + //public bool Remove(int position) + //{ + // if (position < 0 || position > _maxCount) + // return false; + // if (position >= Count) + // return false; + // _places.RemoveAt(position); + // return true; + //} + //public T? this[int position] + //{ + // get + // { + // if (position < 0 || position > _maxCount) + // return null; + // return _places[position]; + // } + // set + // { + // if (position < 0 || position > _maxCount) + // return; + // _places[position] = value; + // } + //} + public bool Remove(int position) { - if (position < 0 || position > _maxCount) - return false; - if (position >= Count) - return false; + if (position >= Count || position < 0) + throw new BoatNotFoundException("Invalid operation"); + if (_places[position] == null) + throw new BoatNotFoundException(position); _places.RemoveAt(position); return true; }