From e3b59236acaa6b48a6743d86eabbb9d7290f2933 Mon Sep 17 00:00:00 2001 From: vladimir_zinovev Date: Thu, 14 Dec 2023 22:01:29 +0400 Subject: [PATCH] lab 8 is done --- ...ArmoredTransportCollectionForm.Designer.cs | 48 +++++++++++---- .../ArmoredTransportCollectionForm.cs | 58 +++++++++++++------ .../ArmoredTransportCompareByColor.cs | 50 ++++++++++++++++ .../ArmoredTransportCompareByType.cs | 36 ++++++++++++ .../ArmoredTransportsCollectionInfo.cs | 28 +++++++++ .../ArmoredTransportsGenericCollection.cs | 7 ++- .../ArmoredTransportsGenericStorage.cs | 35 ++++++----- .../DrawiningArmoredTransportEqutables.cs | 57 ++++++++++++++++++ ProjectTank/ProjectTank/SetGeneric.cs | 16 ++++- 9 files changed, 289 insertions(+), 46 deletions(-) create mode 100644 ProjectTank/ProjectTank/ArmoredTransportCompareByColor.cs create mode 100644 ProjectTank/ProjectTank/ArmoredTransportCompareByType.cs create mode 100644 ProjectTank/ProjectTank/ArmoredTransportsCollectionInfo.cs create mode 100644 ProjectTank/ProjectTank/DrawiningArmoredTransportEqutables.cs diff --git a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs index 1bff6e9..e684ea7 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.Designer.cs @@ -30,6 +30,8 @@ { this.components = new System.ComponentModel.Container(); this.ArmoredTransportCollectionFormToolsGroup = new System.Windows.Forms.GroupBox(); + this.ButtonSortByType = new System.Windows.Forms.Button(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -57,6 +59,8 @@ // this.ArmoredTransportCollectionFormToolsGroup.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); + this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonSortByType); + this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonSortByColor); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.groupBox1); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRefreshCollection); this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRemoveArmoredTransport); @@ -69,6 +73,28 @@ this.ArmoredTransportCollectionFormToolsGroup.TabStop = false; this.ArmoredTransportCollectionFormToolsGroup.Text = "Инструменты"; // + // ButtonSortByType + // + this.ButtonSortByType.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonSortByType.Location = new System.Drawing.Point(41, 516); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(353, 46); + this.ButtonSortByType.TabIndex = 5; + this.ButtonSortByType.Text = "Сортировка по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // ButtonSortByColor + // + this.ButtonSortByColor.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonSortByColor.Location = new System.Drawing.Point(41, 462); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(353, 46); + this.ButtonSortByColor.TabIndex = 4; + this.ButtonSortByColor.Text = "Сортировка по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // groupBox1 // this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); @@ -78,7 +104,7 @@ this.groupBox1.Controls.Add(this.buttonAddObject); this.groupBox1.Location = new System.Drawing.Point(22, 38); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(391, 434); + this.groupBox1.Size = new System.Drawing.Size(391, 412); this.groupBox1.TabIndex = 4; this.groupBox1.TabStop = false; this.groupBox1.Text = "Наборы"; @@ -96,7 +122,7 @@ this.listBoxStorages.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.listBoxStorages.FormattingEnabled = true; this.listBoxStorages.ItemHeight = 32; - this.listBoxStorages.Location = new System.Drawing.Point(19, 165); + this.listBoxStorages.Location = new System.Drawing.Point(19, 148); this.listBoxStorages.Name = "listBoxStorages"; this.listBoxStorages.Size = new System.Drawing.Size(353, 196); this.listBoxStorages.TabIndex = 2; @@ -105,7 +131,7 @@ // buttonDelObject // this.buttonDelObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDelObject.Location = new System.Drawing.Point(19, 374); + this.buttonDelObject.Location = new System.Drawing.Point(19, 352); this.buttonDelObject.Name = "buttonDelObject"; this.buttonDelObject.Size = new System.Drawing.Size(353, 46); this.buttonDelObject.TabIndex = 1; @@ -116,7 +142,7 @@ // buttonAddObject // this.buttonAddObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddObject.Location = new System.Drawing.Point(19, 98); + this.buttonAddObject.Location = new System.Drawing.Point(19, 82); this.buttonAddObject.Name = "buttonAddObject"; this.buttonAddObject.Size = new System.Drawing.Size(353, 46); this.buttonAddObject.TabIndex = 0; @@ -127,7 +153,7 @@ // ButtonRefreshCollection // this.ButtonRefreshCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRefreshCollection.Location = new System.Drawing.Point(22, 773); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(22, 810); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Size = new System.Drawing.Size(391, 47); this.ButtonRefreshCollection.TabIndex = 3; @@ -138,7 +164,7 @@ // ButtonRemoveArmoredTransport // this.ButtonRemoveArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRemoveArmoredTransport.Location = new System.Drawing.Point(22, 641); + this.ButtonRemoveArmoredTransport.Location = new System.Drawing.Point(22, 717); this.ButtonRemoveArmoredTransport.Name = "ButtonRemoveArmoredTransport"; this.ButtonRemoveArmoredTransport.Size = new System.Drawing.Size(391, 82); this.ButtonRemoveArmoredTransport.TabIndex = 2; @@ -149,7 +175,7 @@ // maskedTextBoxNumber // this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxNumber.Location = new System.Drawing.Point(71, 589); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(71, 668); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(275, 39); this.maskedTextBoxNumber.TabIndex = 1; @@ -159,7 +185,7 @@ // ButtonAddArmoredTransport // this.ButtonAddArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonAddArmoredTransport.Location = new System.Drawing.Point(22, 492); + this.ButtonAddArmoredTransport.Location = new System.Drawing.Point(22, 573); this.ButtonAddArmoredTransport.Name = "ButtonAddArmoredTransport"; this.ButtonAddArmoredTransport.Size = new System.Drawing.Size(391, 82); this.ButtonAddArmoredTransport.TabIndex = 0; @@ -206,14 +232,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(359, 44); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(263, 44); this.SaveToolStripMenuItem.Text = "Сохранить"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(359, 44); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(263, 44); this.LoadToolStripMenuItem.Text = "Загрузить"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -269,5 +295,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/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs index 3f2c1ae..04c5dba 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportCollectionForm.cs @@ -41,15 +41,13 @@ namespace ProjectTank 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)) + if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) { listBoxStorages.SelectedIndex = 0; } - else if (listBoxStorages.Items.Count > 0 && index > -1 && - index < listBoxStorages.Items.Count) + else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count) { listBoxStorages.SelectedIndex = index; } @@ -94,11 +92,11 @@ namespace ProjectTank } string name = listBoxStorages.SelectedItem.ToString() ?? string.Empty; if (MessageBox.Show($"Удалить объект {name}?", "Удаление", MessageBoxButtons.YesNo, - MessageBoxIcon.Question) == DialogResult.Yes) + MessageBoxIcon.Question) == DialogResult.Yes) { + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; _storage.DelSet(name); ReloadObjects(); - var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; @@ -139,6 +137,11 @@ namespace ProjectTank MessageBox.Show("Не удалось добавить объект"); _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); } + catch (ArgumentException ex) + { + _logger.LogWarning($"Добавляемый объект уже существует в коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty}"); + MessageBox.Show("Добавляемый объект уже сущесвует в коллекции"); + } }); form.AddEvent(armoredTransportDelegate); form.Show(); @@ -158,22 +161,15 @@ namespace ProjectTank var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { - _logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorages.SelectedItem.ToString()}"); return; } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); try { - if (string.IsNullOrEmpty(maskedTextBoxNumber.Text) || maskedTextBoxNumber.Text == "_") - { - MessageBox.Show("Введите корректное значение для номера"); - _logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorages.SelectedItem.ToString()}"); - return; - } - int pos = Convert.ToInt32(maskedTextBoxNumber.Text); if (obj - pos != null) { MessageBox.Show("Объект удален"); @@ -191,8 +187,6 @@ namespace ProjectTank MessageBox.Show(ex.Message); _logger.LogWarning($"{ex.Message} из набора {listBoxStorages.SelectedItem.ToString()}"); } - - } /// /// Обновление рисунка по набору @@ -263,5 +257,35 @@ namespace ProjectTank } } } + /// + /// Сортировка по сравнителю + /// + /// + private void CompareArmoredTransports(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.ShowArmoredTransports(); + } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) => CompareArmoredTransports(new ArmoredTransportCompareByType()); + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) => CompareArmoredTransports(new ArmoredTransportCompareByColor()); } } diff --git a/ProjectTank/ProjectTank/ArmoredTransportCompareByColor.cs b/ProjectTank/ProjectTank/ArmoredTransportCompareByColor.cs new file mode 100644 index 0000000..e91abe7 --- /dev/null +++ b/ProjectTank/ProjectTank/ArmoredTransportCompareByColor.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTank.DrawningObjects; +using ProjectTank.Drawnings; +using ProjectTank.Entities; + +namespace ProjectTank.Generics +{ + internal class ArmoredTransportCompareByColor : IComparer + { + public int Compare(DrawningArmoredTransport? x, DrawningArmoredTransport? y) + { + if (x == null || x.EntityArmoredTransport == null) + throw new ArgumentNullException(nameof(x)); + + if (y == null || y.EntityArmoredTransport == null) + throw new ArgumentNullException(nameof(y)); + + if (x.EntityArmoredTransport.BodyColor.Name != y.EntityArmoredTransport.BodyColor.Name) + { + return x.EntityArmoredTransport.BodyColor.GetBrightness().CompareTo(y.EntityArmoredTransport.BodyColor.GetBrightness()); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawningArmoredTransport) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawningArmoredTransport) + { + EntityTank EntityX = (EntityTank)x.EntityArmoredTransport; + EntityTank EntityY = (EntityTank)y.EntityArmoredTransport; + if (EntityX.AdditionalColor.Name != EntityY.AdditionalColor.Name) + { + return EntityX.AdditionalColor.Name.CompareTo(EntityY.AdditionalColor.Name); + } + } + var speedCompare = x.EntityArmoredTransport.Speed.CompareTo(y.EntityArmoredTransport.Speed); + + if (speedCompare != 0) + return speedCompare; + + return x.EntityArmoredTransport.Weight.CompareTo(y.EntityArmoredTransport.Weight); + } + } +} diff --git a/ProjectTank/ProjectTank/ArmoredTransportCompareByType.cs b/ProjectTank/ProjectTank/ArmoredTransportCompareByType.cs new file mode 100644 index 0000000..e548738 --- /dev/null +++ b/ProjectTank/ProjectTank/ArmoredTransportCompareByType.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTank.DrawningObjects; +using ProjectTank.Drawnings; + +namespace ProjectTank.Generics +{ + internal class ArmoredTransportCompareByType : IComparer + { + public int Compare(DrawningArmoredTransport? x, DrawningArmoredTransport? y) + { + if (x == null || x.EntityArmoredTransport == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityArmoredTransport == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityArmoredTransport.Speed.CompareTo(y.EntityArmoredTransport.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityArmoredTransport.Weight.CompareTo(y.EntityArmoredTransport.Weight); + } + } +} diff --git a/ProjectTank/ProjectTank/ArmoredTransportsCollectionInfo.cs b/ProjectTank/ProjectTank/ArmoredTransportsCollectionInfo.cs new file mode 100644 index 0000000..94a6f99 --- /dev/null +++ b/ProjectTank/ProjectTank/ArmoredTransportsCollectionInfo.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTank +{ + internal class ArmoredTransportsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public ArmoredTransportsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(ArmoredTransportsCollectionInfo? other) + { + return this.Name == other.Name; + } + + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs b/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs index aad37ab..b646012 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportsGenericCollection.cs @@ -51,6 +51,11 @@ namespace ProjectTank.Generics _collection = new SetGeneric(width * height); } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) => _collection.SortSet(comparer); + /// /// Получение объектов коллекции /// public IEnumerable GetArmoredTransports => _collection.GetArmoredTransports(); @@ -66,7 +71,7 @@ namespace ProjectTank.Generics { return false; } - return collect?._collection.Insert(obj)??false; + return collect?._collection.Insert(obj, new DrawiningArmoredTransportEqutables()) ?? false; } /// /// Перегрузка оператора вычитания diff --git a/ProjectTank/ProjectTank/ArmoredTransportsGenericStorage.cs b/ProjectTank/ProjectTank/ArmoredTransportsGenericStorage.cs index e01ec59..2ae991e 100644 --- a/ProjectTank/ProjectTank/ArmoredTransportsGenericStorage.cs +++ b/ProjectTank/ProjectTank/ArmoredTransportsGenericStorage.cs @@ -15,11 +15,11 @@ namespace ProjectTank.Generics /// /// Словарь (хранилище) /// - readonly Dictionary> _armoredTransportStorages; + readonly Dictionary> _armoredTransportStorages; /// /// Возвращение списка названий наборов /// - public List Keys => _armoredTransportStorages.Keys.ToList(); + public List Keys => _armoredTransportStorages.Keys.ToList(); /// /// Ширина окна отрисовки /// @@ -47,7 +47,7 @@ namespace ProjectTank.Generics /// public ArmoredTransportsGenericStorage(int pictureWidth, int pictureHeight) { - _armoredTransportStorages = new Dictionary>(); + _armoredTransportStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -57,19 +57,25 @@ namespace ProjectTank.Generics /// Название набора public void AddSet(string name) { - _armoredTransportStorages.Add(name, new ArmoredTransportsGenericCollection(_pictureWidth, _pictureHeight)); + if (_armoredTransportStorages.ContainsKey(new ArmoredTransportsCollectionInfo(name, string.Empty))) + { + MessageBox.Show("Словарь уже имеет объект с таким названием", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + _armoredTransportStorages.Add(new ArmoredTransportsCollectionInfo(name, string.Empty), + new ArmoredTransportsGenericCollection(_pictureWidth, _pictureHeight)); } + /// /// Удаление набора /// /// Название набора public void DelSet(string name) { - if (!_armoredTransportStorages.ContainsKey(name)) - { + if (_armoredTransportStorages[new ArmoredTransportsCollectionInfo(name, string.Empty)] == null) return; - } - _armoredTransportStorages.Remove(name); + _armoredTransportStorages.Remove(new ArmoredTransportsCollectionInfo(name, string.Empty)); } /// /// Доступ к набору @@ -80,10 +86,9 @@ namespace ProjectTank.Generics { get { - if (_armoredTransportStorages.ContainsKey(ind)) - { - return _armoredTransportStorages[ind]; - } + ArmoredTransportsCollectionInfo indObj = new ArmoredTransportsCollectionInfo(ind, string.Empty); + if (_armoredTransportStorages.ContainsKey(indObj)) + return _armoredTransportStorages[indObj]; return null; } } @@ -99,14 +104,14 @@ namespace ProjectTank.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _armoredTransportStorages) + foreach (KeyValuePair> record in _armoredTransportStorages) { //создается тип пара ключ значение, который представляет строку и на каждой итерации берется элемент record коллекции _armoredTransportStorages StringBuilder records = new(); foreach (DrawningArmoredTransport? elem in record.Value.GetArmoredTransports) // проход по списку коллекции с получением значения объекта { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); //добавление в стрингбилдер свойств объекта с разделителем : и в конце ; } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); // добавление значения словаря ключа в начало и разделение разделитем + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); // добавление значения словаря ключа в начало и разделение разделитем } if (data.Length == 0) { @@ -170,7 +175,7 @@ namespace ProjectTank.Generics } } } - _armoredTransportStorages.Add(record[0], collection); + _armoredTransportStorages.Add(new ArmoredTransportsCollectionInfo(record[0],string.Empty), collection); str = sr.ReadLine(); } while (str != null); } diff --git a/ProjectTank/ProjectTank/DrawiningArmoredTransportEqutables.cs b/ProjectTank/ProjectTank/DrawiningArmoredTransportEqutables.cs new file mode 100644 index 0000000..68f30d9 --- /dev/null +++ b/ProjectTank/ProjectTank/DrawiningArmoredTransportEqutables.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectTank.Drawnings; +using ProjectTank.Entities; +using System.Diagnostics.CodeAnalysis; +using ProjectTank.DrawningObjects; + +namespace ProjectTank.Generics +{ + internal class DrawiningArmoredTransportEqutables : IEqualityComparer + { + public bool Equals(DrawningArmoredTransport? x, DrawningArmoredTransport? y) + { + if (x == null || x.EntityArmoredTransport == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityArmoredTransport == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityArmoredTransport.Speed != y.EntityArmoredTransport.Speed) + { + return false; + } + if (x.EntityArmoredTransport.Weight != y.EntityArmoredTransport.Weight) + { + return false; + } + if (x.EntityArmoredTransport.BodyColor != y.EntityArmoredTransport.BodyColor) + { + return false; + } + if (x is DrawningTank && y is DrawningTank) + { + EntityTank EntityX = (EntityTank)x.EntityArmoredTransport; + EntityTank EntityY = (EntityTank)y.EntityArmoredTransport; + if (EntityX.MachineGun != EntityY.MachineGun) + return false; + if (EntityX.Gun != EntityY.Gun) + return false; + } + return true; + } + public int GetHashCode([DisallowNull] DrawningArmoredTransport obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/ProjectTank/ProjectTank/SetGeneric.cs b/ProjectTank/ProjectTank/SetGeneric.cs index 44e11c9..94c8d3f 100644 --- a/ProjectTank/ProjectTank/SetGeneric.cs +++ b/ProjectTank/ProjectTank/SetGeneric.cs @@ -37,15 +37,20 @@ namespace ProjectTank.Generics _places = new List(count); } /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) => _places.Sort(comparer); + /// /// Добавление объекта в набор /// /// Добавляемый броне-транспорт /// - public bool Insert(T armoredTransport) + public bool Insert(T armoredTransport, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); - Insert(armoredTransport, 0); + Insert(armoredTransport, 0, equal); return true; } /// @@ -54,12 +59,17 @@ namespace ProjectTank.Generics /// Добавляемый броне-транспорт /// Позиция /// - public bool Insert(T armoredTransport, int position) + public bool Insert(T armoredTransport, 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(armoredTransport, equal)) + throw new ArgumentException(nameof(armoredTransport)); + } _places.Insert(position, armoredTransport); return true; }