From a3ddbf0952faca4479e40f5fa442a243e7f19a5e Mon Sep 17 00:00:00 2001 From: "yuliya.mavrina@internet.ru" Date: Tue, 26 Dec 2023 10:32:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=E2=84=968?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormBusCollection.Designer.cs | 58 ++++++++---- .../DoubleDeckerBus/FormBusCollection.cs | 91 ++++++++++++------- .../DoubleDeckerBus/FormBusCollection.resx | 3 - .../Generics/BusCompareByColor.cs | 33 +++++++ .../Generics/BusCompareByType.cs | 35 +++++++ .../Generics/BusesCollectionInfo.cs | 34 +++++++ .../Generics/BusesGenericCollection.cs | 3 +- .../Generics/BusesGenericStorage.cs | 50 +++++----- .../Generics/DrawiningBusEqutables.cs | 63 +++++++++++++ .../DoubleDeckerBus/Generics/SetGeneric.cs | 12 ++- 10 files changed, 294 insertions(+), 88 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByColor.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByType.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/Generics/BusesCollectionInfo.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/Generics/DrawiningBusEqutables.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs index f4e4064..99cf767 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs @@ -34,6 +34,8 @@ maskedTextBoxNumber = new MaskedTextBox(); pictureBoxCollection = new PictureBox(); panel = new Panel(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); textBoxStorageName = new TextBox(); listBoxStorages = new ListBox(); ButtonDelObject = new Button(); @@ -41,10 +43,9 @@ menuStrip = new MenuStrip(); ToolStripMenuItem = new ToolStripMenuItem(); LoadToolStripMenuItem = new ToolStripMenuItem(); - saveToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); - SaveToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); panel.SuspendLayout(); menuStrip.SuspendLayout(); @@ -53,7 +54,7 @@ // buttonAddBus // buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonAddBus.Location = new Point(19, 277); + buttonAddBus.Location = new Point(19, 287); buttonAddBus.Name = "buttonAddBus"; buttonAddBus.Size = new Size(138, 31); buttonAddBus.TabIndex = 0; @@ -64,7 +65,7 @@ // buttonRemoveBus // buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonRemoveBus.Location = new Point(19, 343); + buttonRemoveBus.Location = new Point(19, 353); buttonRemoveBus.Name = "buttonRemoveBus"; buttonRemoveBus.Size = new Size(138, 31); buttonRemoveBus.TabIndex = 1; @@ -86,7 +87,7 @@ // maskedTextBoxNumber // maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right; - maskedTextBoxNumber.Location = new Point(36, 314); + maskedTextBoxNumber.Location = new Point(36, 324); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(100, 23); maskedTextBoxNumber.TabIndex = 3; @@ -103,6 +104,8 @@ // panel // panel.Anchor = AnchorStyles.Top | AnchorStyles.Right; + panel.Controls.Add(ButtonSortByColor); + panel.Controls.Add(ButtonSortByType); panel.Controls.Add(textBoxStorageName); panel.Controls.Add(listBoxStorages); panel.Controls.Add(ButtonDelObject); @@ -118,6 +121,26 @@ panel.TabIndex = 5; panel.Tag = ""; // + // ButtonSortByColor + // + ButtonSortByColor.Location = new Point(13, 247); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(138, 25); + ButtonSortByColor.TabIndex = 12; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; + // + // ButtonSortByType + // + ButtonSortByType.Location = new Point(13, 214); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(138, 27); + ButtonSortByType.TabIndex = 11; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; + // // textBoxStorageName // textBoxStorageName.Location = new Point(13, 36); @@ -129,9 +152,9 @@ // listBoxStorages.FormattingEnabled = true; listBoxStorages.ItemHeight = 15; - listBoxStorages.Location = new Point(13, 100); + listBoxStorages.Location = new Point(3, 100); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(138, 64); + listBoxStorages.Size = new Size(165, 64); listBoxStorages.TabIndex = 8; listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; // @@ -174,16 +197,16 @@ // LoadToolStripMenuItem // LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - LoadToolStripMenuItem.Size = new Size(180, 22); + LoadToolStripMenuItem.Size = new Size(133, 22); LoadToolStripMenuItem.Text = "Загрузить"; LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // - // saveToolStripMenuItem + // SaveToolStripMenuItem // - saveToolStripMenuItem.Name = "saveToolStripMenuItem"; - saveToolStripMenuItem.Size = new Size(180, 22); - saveToolStripMenuItem.Text = "Сохранить"; - saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(133, 22); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // openFileDialog // @@ -194,13 +217,6 @@ // saveFileDialog.Filter = "«txt file | *.txt»"; // - // SaveToolStripMenuItem - // - SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - SaveToolStripMenuItem.Size = new Size(180, 22); - SaveToolStripMenuItem.Text = "Сохранить"; - SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; - // // FormBusCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -237,5 +253,7 @@ private ToolStripMenuItem ToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem SaveToolStripMenuItem; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs index 78b69e5..8bae5ce 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs @@ -16,8 +16,6 @@ using DoubleDeckerBus.Exceptions; using Microsoft.Extensions.Logging; using System.Xml.Linq; - - namespace DoubleDeckerBus { public partial class FormBusCollection : Form @@ -33,13 +31,13 @@ namespace DoubleDeckerBus private void ReloadObjects() { int index = listBoxStorages.SelectedIndex; - listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { listBoxStorages.Items.Add(_storage.Keys[i]); } - if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) + if (listBoxStorages.Items.Count > 0 && (index == -1 || index + >= listBoxStorages.Items.Count)) { listBoxStorages.SelectedIndex = 0; } @@ -60,7 +58,7 @@ namespace DoubleDeckerBus MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _storage.AddSet(textBoxStorageName.Text); + _storage.AddSet(textBoxStorageName.Text.ToString()); ReloadObjects(); _logger.LogInformation($"Добавлен набор:{textBoxStorageName.Text}"); } @@ -85,30 +83,41 @@ namespace DoubleDeckerBus { return; } - var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? - string.Empty]; + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } - FormBusConfig form = new(); - form.Show(); - Action? busDelegate = new((m) => + FormBusConfig formBusConfig = new FormBusConfig(); + formBusConfig.AddEvent(AddBus); + formBusConfig.Show(); + } + + private void AddBus(DrawningBus bus) + { + if (listBoxStorages.SelectedIndex == -1) { - try - { - bool q = obj + m; - MessageBox.Show("Объект добавлен"); - _logger.LogInformation($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); - pictureBoxCollection.Image = obj.ShowBuses(); - } - catch (StorageOverflowException ex) - { - _logger.LogWarning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена"); - MessageBox.Show(ex.Message); - } - }); - form.AddEvent(busDelegate); + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + _logger.LogWarning("Добавление пустого объекта"); + return; + } + try + { + _ = obj + bus; + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowBuses(); + _logger.LogInformation($"bus added in set {listBoxStorages.SelectedItem.ToString()}"); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"bus not added in set {listBoxStorages.SelectedItem.ToString()}"); + } + } private void buttonRemoveBus_Click(object sender, EventArgs e) { @@ -116,7 +125,7 @@ namespace DoubleDeckerBus { return; } - var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; @@ -127,23 +136,23 @@ namespace DoubleDeckerBus } try { - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); if (obj - pos != null) { MessageBox.Show("Объект удален"); pictureBoxCollection.Image = obj.ShowBuses(); - _logger.LogInformation($"лодка удалена из набора {listBoxStorages.SelectedItem.ToString()}"); + _logger.LogInformation($"Автобус удален из набора {listBoxStorages.SelectedItem.ToString()}"); } else { MessageBox.Show("Объект не удален"); - _logger.LogWarning($"лодка не удалена из набора {listBoxStorages.SelectedItem.ToString()}"); + _logger.LogWarning($"Автобус не удален из набора {listBoxStorages.SelectedItem.ToString()}"); } } catch (BusNotFoundException ex) { MessageBox.Show(ex.Message); - _logger.LogWarning($"BoatNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}"); + _logger.LogWarning($"BusNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}"); } catch (Exception ex) { @@ -170,13 +179,13 @@ namespace DoubleDeckerBus try { _storage.SaveData(saveFileDialog.FileName); - MessageBox.Show("Сохранение прошло успешно","Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.LogInformation($"Файл {saveFileDialog.FileName} успешно сохранен"); } catch (Exception ex) { _logger.LogWarning("Не удалось сохранить"); - MessageBox.Show($"Не сохранилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -189,7 +198,7 @@ namespace DoubleDeckerBus try { _storage.LoadData(openFileDialog.FileName); - MessageBox.Show("Загрузка прошла успешно","Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.LogInformation($"Файл {openFileDialog.FileName} успешно загружен"); foreach (var collection in _storage.Keys) { @@ -200,11 +209,27 @@ namespace DoubleDeckerBus catch (Exception ex) { _logger.LogWarning("Не удалось загрузить"); - MessageBox.Show($"Не загрузилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareBuses(new BusCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareBuses(new BusCompareByColor()); + private void CompareBuses(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.ShowBuses(); + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx index 11110dc..7ea2349 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx @@ -120,9 +120,6 @@ 11, 17 - - 11, 17 - 132, 17 diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByColor.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByColor.cs new file mode 100644 index 0000000..b0913ed --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByColor.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DoubleDeckerBus.DrawningObjects; +using DoubleDeckerBus.Entities; + +namespace DoubleDeckerBus.Generics +{ + internal class BusCompareByColor : IComparer + { + public int Compare(DrawningBus? x, DrawningBus? y) + { + if (x == null || x.EntityBus == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityBus == null) + throw new ArgumentNullException(nameof(y)); + + var xCruiser = x.EntityBus; + var yCruiser = y.EntityBus; + + if (xCruiser.BodyColor != yCruiser.BodyColor) + return xCruiser.BodyColor.Name.CompareTo(yCruiser.BodyColor.Name); + + var speedCompare = x.EntityBus.Speed.CompareTo(y.EntityBus.Speed); + if (speedCompare != 0) + return speedCompare; + return x.EntityBus.Weight.CompareTo(y.EntityBus.Weight); + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByType.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByType.cs new file mode 100644 index 0000000..754109a --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusCompareByType.cs @@ -0,0 +1,35 @@ +using DoubleDeckerBus.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.Generics +{ + internal class BusCompareByType : IComparer + { + public int Compare(DrawningBus? x, DrawningBus? y) + { + if (x == null || x.EntityBus == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBus == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityBus.Speed.CompareTo(y.EntityBus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityBus.Weight.CompareTo(y.EntityBus.Weight); + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesCollectionInfo.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesCollectionInfo.cs new file mode 100644 index 0000000..6ae4d60 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesCollectionInfo.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.Generics +{ + internal class BusesCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public BusesCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(BusesCollectionInfo? 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/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs index 4326941..f582db6 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs @@ -12,6 +12,7 @@ namespace DoubleDeckerBus.Generics where T : DrawningBus where U : IMoveableObject { + public void Sort(IComparer comparer) => _collection.SortSet(comparer); public IEnumerable GetTheBuses => _collection.GetTheBuses(); private readonly int _pictureWidth; private readonly int _pictureHeight; @@ -33,7 +34,7 @@ namespace DoubleDeckerBus.Generics return false; } - return collect?._collection.Insert(obj) ?? false; + return collect._collection.Insert(obj, new DrawiningBusEqutables()); } public static T? operator -(BusesGenericCollection collect, int pos) { diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs index 263df3a..f34de82 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs @@ -10,8 +10,8 @@ namespace DoubleDeckerBus.Generics { internal class BusesGenericStorage { - readonly Dictionary> _busStorages; - public List Keys => _busStorages.Keys.ToList(); + readonly Dictionary> _busStorages; + public List Keys => _busStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; private static readonly char _separatorForKeyValue = '|'; @@ -19,32 +19,29 @@ namespace DoubleDeckerBus.Generics private static readonly char _separatorForObject = ':'; public BusesGenericStorage(int pictureWidth, int pictureHeight) { - _busStorages = new Dictionary>(); + _busStorages = new Dictionary> (); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void AddSet(string name) { - _busStorages.Add(name, new BusesGenericCollection(_pictureWidth, _pictureHeight)); + _busStorages.Add(new BusesCollectionInfo(name, string.Empty), + new BusesGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { - if (!_busStorages.ContainsKey(name)) - { + if (!_busStorages.ContainsKey(new BusesCollectionInfo(name, string.Empty))) return; - } - - _busStorages.Remove(name); + _busStorages.Remove(new BusesCollectionInfo(name, string.Empty)); } - public BusesGenericCollection?this[string ind] + + public BusesGenericCollection? this[string ind] { get { - if (_busStorages.ContainsKey(ind)) - { - return _busStorages[ind]; - } - + BusesCollectionInfo indObj = new BusesCollectionInfo(ind, string.Empty); + if (_busStorages.ContainsKey(indObj)) + return _busStorages[indObj]; return null; } } @@ -55,19 +52,18 @@ namespace DoubleDeckerBus.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _busStorages) + foreach (KeyValuePair> record in _busStorages) { StringBuilder records = new(); foreach (DrawningBus? elem in record.Value.GetTheBuses) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { - throw new Exception("Невалидная операция, нет данных для сохранения"); + throw new Exception("Невалиданя операция, нет данных для сохранения"); } using FileStream fs = new(filename, FileMode.Create); byte[] info = new @@ -75,6 +71,7 @@ namespace DoubleDeckerBus.Generics fs.Write(info, 0, info.Length); return; } + public void LoadData(string filename) { if (!File.Exists(filename)) @@ -91,8 +88,7 @@ namespace DoubleDeckerBus.Generics bufferTextFromFile += temp.GetString(b); } } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); if (strs == null || strs.Length == 0) { throw new Exception("Нет данных для загрузки"); @@ -105,8 +101,7 @@ namespace DoubleDeckerBus.Generics _busStorages.Clear(); foreach (string data in strs) { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); + string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 2) { continue; @@ -117,17 +112,16 @@ namespace DoubleDeckerBus.Generics StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { - DrawningBus? car = - elem?.CreateDrawningBus(_separatorForObject, _pictureWidth, _pictureHeight); - if (car != null) + DrawningBus? bus = elem?.CreateDrawningBus(_separatorForObject, _pictureWidth, _pictureHeight); + if (bus != null) { - if (!(collection + car)) + if (!(collection + bus)) { throw new Exception("Ошибка добавления в коллекцию"); } } } - _busStorages.Add(record[0], collection); + _busStorages.Add(new BusesCollectionInfo(record[0], string.Empty), collection); } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/DrawiningBusEqutables.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/DrawiningBusEqutables.cs new file mode 100644 index 0000000..73aa1ad --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/DrawiningBusEqutables.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DoubleDeckerBus.DrawningObjects; +using DoubleDeckerBus.Entities; + + +namespace DoubleDeckerBus.Generics +{ + internal class DrawiningBusEqutables : IEqualityComparer + { + public bool Equals(DrawningBus? x, DrawningBus? y) + { + if (x == null || x.EntityBus == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityBus == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityBus.Speed != y.EntityBus.Speed) + { + return false; + } + if (x.EntityBus.Weight != y.EntityBus.Weight) + { + return false; + } + if (x.EntityBus.BodyColor != y.EntityBus.BodyColor) + { + return false; + } + if (x is DrawningDoubleDeckerBus && y is DrawningDoubleDeckerBus) + { + if ((x.EntityBus as EntityDoubleDeckerBus).AdditionalColor != (y.EntityBus as EntityDoubleDeckerBus).AdditionalColor) + { + return false; + } + if ((x.EntityBus as EntityDoubleDeckerBus).SecondFloor != (y.EntityBus as EntityDoubleDeckerBus).SecondFloor) + { + return false; + } + if ((x.EntityBus as EntityDoubleDeckerBus).Ladder != (y.EntityBus as EntityDoubleDeckerBus).Ladder) + { + return false; + } + } + return true; + } + public int GetHashCode([DisallowNull] DrawningBus obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs index b0df051..b9768c5 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs @@ -14,24 +14,30 @@ namespace DoubleDeckerBus.Generics private readonly List _places; public int Count => _places.Count; private readonly int _maxCount; + public void SortSet(IComparer comparer) => _places.Sort(comparer); public SetGeneric(int count) { _maxCount = count; _places = new List(count); } - public bool Insert(T bus) + public bool Insert(T bus, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - Insert(bus, 0); + Insert(bus, 0, equal); return true; } - public bool Insert(T bus, int position) + public bool Insert(T bus, int position, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!(position >= 0 && position <= Count)) return false; + if (equal != null) + { + if (_places.Contains(bus, equal)) + throw new ArgumentException(("Такой объект уже существует")); + } _places.Insert(position, bus); return true; }