From 3e9dddb1cdac9f7209be174d23f6e289dae6e2d7 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:30:26 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9C=D0=B8=D0=BD=D0=B8-=D0=BD=D0=B0=D1=87?= =?UTF-8?q?=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Generics/DrawningMonorailEqutables.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Monorail/Monorail/Generics/DrawningMonorailEqutables.cs diff --git a/Monorail/Monorail/Generics/DrawningMonorailEqutables.cs b/Monorail/Monorail/Generics/DrawningMonorailEqutables.cs new file mode 100644 index 0000000..a8da33f --- /dev/null +++ b/Monorail/Monorail/Generics/DrawningMonorailEqutables.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Monorail.DrawningObjects; +using Monorail.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace Monorail.Generics +{ + internal class DrawningMonorailEqutables : IEqualityComparer + { + public bool Equals(DrawningMonorail? x, DrawningMonorail? y) + { + if(x==null || x.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityMonorail.Speed != y.EntityMonorail.Speed) + { + return false; + } + if (x.EntityMonorail.Weight != y.EntityMonorail.Weight) + { + return false; + } + if (x.EntityMonorail.BodyColor != y.EntityMonorail.BodyColor) + { + return false; + } + if (x is DrawningSecondMonorail && y is DrawningSecondMonorail) + { + // TODO + } + return true; + } + public int GetHashCode([DisallowNull] DrawningMonorail obj) + { + return obj.GetHashCode(); + } + } +} -- 2.25.1 From edd36266774d8f1c940178304e74cd32240eae85 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Thu, 14 Dec 2023 18:46:20 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=9E=D0=B6=D0=B8=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMonorailCollection.Designer.cs | 109 ++++++++++++------ Monorail/Monorail/FormMonorailCollection.cs | 31 ++++- .../Generics/DrawningMonorailEqutables.cs | 14 +++ .../Generics/MonorailCompareByColor.cs | 52 +++++++++ .../Generics/MonorailCompareByType.cs | 35 ++++++ .../Generics/MonorailsCollectionInfo.cs | 28 +++++ .../Generics/MonorailsGenericCollection.cs | 7 +- .../Generics/MonorailsGenericStorage.cs | 32 ++--- Monorail/Monorail/Generics/SetGeneric.cs | 25 ++-- 9 files changed, 270 insertions(+), 63 deletions(-) create mode 100644 Monorail/Monorail/Generics/MonorailCompareByColor.cs create mode 100644 Monorail/Monorail/Generics/MonorailCompareByType.cs create mode 100644 Monorail/Monorail/Generics/MonorailsCollectionInfo.cs diff --git a/Monorail/Monorail/FormMonorailCollection.Designer.cs b/Monorail/Monorail/FormMonorailCollection.Designer.cs index b5201ce..4360181 100644 --- a/Monorail/Monorail/FormMonorailCollection.Designer.cs +++ b/Monorail/Monorail/FormMonorailCollection.Designer.cs @@ -30,6 +30,8 @@ { pictureBoxCollection = new PictureBox(); panel1 = new Panel(); + ButtonSortByColor = new Button(); + ButtonSortByType = new Button(); panel2 = new Panel(); label2 = new Label(); listBoxStorages = new ListBox(); @@ -55,26 +57,52 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(5, 31); + pictureBoxCollection.Location = new Point(6, 39); + pictureBoxCollection.Margin = new Padding(4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(1017, 485); + pictureBoxCollection.Size = new Size(1271, 606); pictureBoxCollection.TabIndex = 5; pictureBoxCollection.TabStop = false; // // panel1 // panel1.BackColor = SystemColors.Info; + panel1.Controls.Add(ButtonSortByColor); + panel1.Controls.Add(ButtonSortByType); panel1.Controls.Add(panel2); panel1.Controls.Add(maskedTextBoxNumber); panel1.Controls.Add(buttonRefreshCol); panel1.Controls.Add(buttonDelMonorail); panel1.Controls.Add(buttonAddMonorail); panel1.Controls.Add(label1); - panel1.Location = new Point(1025, -1); + panel1.Location = new Point(1281, -1); + panel1.Margin = new Padding(4); panel1.Name = "panel1"; - panel1.Size = new Size(204, 517); + panel1.Size = new Size(255, 646); panel1.TabIndex = 4; // + // ButtonSortByColor + // + ButtonSortByColor.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); + ButtonSortByColor.Location = new Point(15, 366); + ButtonSortByColor.Margin = new Padding(4); + ButtonSortByColor.Name = "ButtonSortByColor"; + ButtonSortByColor.Size = new Size(224, 51); + ButtonSortByColor.TabIndex = 9; + ButtonSortByColor.Text = "Сортировка по цвету"; + ButtonSortByColor.UseVisualStyleBackColor = true; + // + // ButtonSortByType + // + ButtonSortByType.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); + ButtonSortByType.Location = new Point(15, 307); + ButtonSortByType.Margin = new Padding(4); + ButtonSortByType.Name = "ButtonSortByType"; + ButtonSortByType.Size = new Size(224, 51); + ButtonSortByType.TabIndex = 8; + ButtonSortByType.Text = "Сортировка по типу"; + ButtonSortByType.UseVisualStyleBackColor = true; + // // panel2 // panel2.Controls.Add(label2); @@ -82,43 +110,48 @@ panel2.Controls.Add(textBoxStorageName); panel2.Controls.Add(button1); panel2.Controls.Add(ButtonAddObject); - panel2.Location = new Point(12, 20); + panel2.Location = new Point(15, 25); + panel2.Margin = new Padding(4); panel2.Name = "panel2"; - panel2.Size = new Size(179, 263); + panel2.Size = new Size(224, 261); panel2.TabIndex = 7; // // label2 // label2.AutoSize = true; - label2.Location = new Point(3, -3); + label2.Location = new Point(4, -4); + label2.Margin = new Padding(4, 0, 4, 0); label2.Name = "label2"; - label2.Size = new Size(66, 20); + label2.Size = new Size(79, 25); label2.TabIndex = 8; label2.Text = "Наборы"; // // listBoxStorages // listBoxStorages.FormattingEnabled = true; - listBoxStorages.ItemHeight = 20; - listBoxStorages.Location = new Point(17, 109); + listBoxStorages.ItemHeight = 25; + listBoxStorages.Location = new Point(21, 106); + listBoxStorages.Margin = new Padding(4); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(144, 104); + listBoxStorages.Size = new Size(179, 104); listBoxStorages.TabIndex = 9; listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged; // // textBoxStorageName // - textBoxStorageName.Location = new Point(18, 29); + textBoxStorageName.Location = new Point(21, 25); + textBoxStorageName.Margin = new Padding(4); textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(143, 27); + textBoxStorageName.Size = new Size(178, 31); textBoxStorageName.TabIndex = 8; // // button1 // button1.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - button1.Location = new Point(18, 219); + button1.Location = new Point(20, 218); + button1.Margin = new Padding(4); button1.Name = "button1"; - button1.Size = new Size(143, 41); + button1.Size = new Size(179, 33); button1.TabIndex = 5; button1.Text = "Удалить набор"; button1.UseVisualStyleBackColor = true; @@ -127,9 +160,10 @@ // ButtonAddObject // ButtonAddObject.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - ButtonAddObject.Location = new Point(18, 62); + ButtonAddObject.Location = new Point(22, 64); + ButtonAddObject.Margin = new Padding(4); ButtonAddObject.Name = "ButtonAddObject"; - ButtonAddObject.Size = new Size(143, 41); + ButtonAddObject.Size = new Size(179, 34); ButtonAddObject.TabIndex = 6; ButtonAddObject.Text = "Добавить набор"; ButtonAddObject.UseVisualStyleBackColor = true; @@ -137,17 +171,19 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(30, 336); + maskedTextBoxNumber.Location = new Point(37, 484); + maskedTextBoxNumber.Margin = new Padding(4); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(125, 27); + maskedTextBoxNumber.Size = new Size(155, 31); maskedTextBoxNumber.TabIndex = 4; // // buttonRefreshCol // buttonRefreshCol.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonRefreshCol.Location = new Point(12, 466); + buttonRefreshCol.Location = new Point(15, 582); + buttonRefreshCol.Margin = new Padding(4); buttonRefreshCol.Name = "buttonRefreshCol"; - buttonRefreshCol.Size = new Size(179, 41); + buttonRefreshCol.Size = new Size(224, 51); buttonRefreshCol.TabIndex = 3; buttonRefreshCol.Text = "Обновить коллекцию"; buttonRefreshCol.UseVisualStyleBackColor = true; @@ -156,9 +192,10 @@ // buttonDelMonorail // buttonDelMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonDelMonorail.Location = new Point(12, 369); + buttonDelMonorail.Location = new Point(15, 523); + buttonDelMonorail.Margin = new Padding(4); buttonDelMonorail.Name = "buttonDelMonorail"; - buttonDelMonorail.Size = new Size(179, 41); + buttonDelMonorail.Size = new Size(224, 51); buttonDelMonorail.TabIndex = 2; buttonDelMonorail.Text = "Удалить монорельс"; buttonDelMonorail.UseVisualStyleBackColor = true; @@ -167,9 +204,10 @@ // buttonAddMonorail // buttonAddMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonAddMonorail.Location = new Point(12, 289); + buttonAddMonorail.Location = new Point(15, 425); + buttonAddMonorail.Margin = new Padding(4); buttonAddMonorail.Name = "buttonAddMonorail"; - buttonAddMonorail.Size = new Size(179, 41); + buttonAddMonorail.Size = new Size(224, 51); buttonAddMonorail.TabIndex = 1; buttonAddMonorail.Text = "Добавить монорельс"; buttonAddMonorail.UseVisualStyleBackColor = true; @@ -178,9 +216,10 @@ // label1 // label1.AutoSize = true; - label1.Location = new Point(3, -3); + label1.Location = new Point(4, -4); + label1.Margin = new Padding(4, 0, 4, 0); label1.Name = "label1"; - label1.Size = new Size(103, 20); + label1.Size = new Size(122, 25); label1.TabIndex = 0; label1.Text = "Инструменты"; // @@ -190,7 +229,8 @@ menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Size = new Size(1228, 28); + menuStrip1.Padding = new Padding(8, 2, 0, 2); + menuStrip1.Size = new Size(1535, 33); menuStrip1.TabIndex = 6; menuStrip1.Text = "menuStrip1"; // @@ -198,20 +238,20 @@ // файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem }); файлToolStripMenuItem.Name = "файлToolStripMenuItem"; - файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.Size = new Size(69, 29); файлToolStripMenuItem.Text = "Файл"; // // сохранитьToolStripMenuItem // сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem"; - сохранитьToolStripMenuItem.Size = new Size(166, 26); + сохранитьToolStripMenuItem.Size = new Size(200, 34); сохранитьToolStripMenuItem.Text = "Сохранить"; сохранитьToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // загрузитьToolStripMenuItem // загрузитьToolStripMenuItem.Name = "загрузитьToolStripMenuItem"; - загрузитьToolStripMenuItem.Size = new Size(166, 26); + загрузитьToolStripMenuItem.Size = new Size(200, 34); загрузитьToolStripMenuItem.Text = "Загрузить"; загрузитьToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -227,14 +267,15 @@ // // FormMonorailCollection // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; BackColor = SystemColors.ControlDarkDark; - ClientSize = new Size(1228, 518); + ClientSize = new Size(1535, 648); Controls.Add(pictureBoxCollection); Controls.Add(panel1); Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; + Margin = new Padding(4); Name = "FormMonorailCollection"; Text = "Набор Монорельсов"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); @@ -269,5 +310,7 @@ private ToolStripMenuItem загрузитьToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByColor; + private Button ButtonSortByType; } } \ No newline at end of file diff --git a/Monorail/Monorail/FormMonorailCollection.cs b/Monorail/Monorail/FormMonorailCollection.cs index f2bd6ba..4d0ee53 100644 --- a/Monorail/Monorail/FormMonorailCollection.cs +++ b/Monorail/Monorail/FormMonorailCollection.cs @@ -25,6 +25,11 @@ namespace Monorail private readonly ILogger _logger; readonly int countPlace = 22; + private void ButtonSortByType_Click(object sender, EventArgs e) => + CompareMonorails(new MonorailCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => + CompareMonorails(new MonorailCompareByColor()); + public FormMonorailCollection(ILogger logger) { InitializeComponent(); @@ -71,6 +76,22 @@ EventArgs e) pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowMonorails(); } + private void CompareMonorails(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.ShowMonorails(); + } + private void ButtonDelObject_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) @@ -129,7 +150,7 @@ EventArgs e) } catch (StorageOverflowException ex) { - MessageBox.Show(ex.Message, "Ошибка", + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning(ex.Message); } @@ -165,14 +186,16 @@ EventArgs e) MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - try { + try + { var curObj = obj - pos; MessageBox.Show("Объект удален"); pictureBoxCollection.Image = obj.ShowMonorails(); _logger.LogInformation($"Объект удален по позиции: {pos}"); - }catch(MonorailNotFoundException ex) + } + catch (MonorailNotFoundException ex) { - MessageBox.Show(ex.Message, "Ошибка", + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning($"Не удалось удалить объект по позиции {pos}"); } diff --git a/Monorail/Monorail/Generics/DrawningMonorailEqutables.cs b/Monorail/Monorail/Generics/DrawningMonorailEqutables.cs index a8da33f..e73d046 100644 --- a/Monorail/Monorail/Generics/DrawningMonorailEqutables.cs +++ b/Monorail/Monorail/Generics/DrawningMonorailEqutables.cs @@ -40,6 +40,20 @@ namespace Monorail.Generics if (x is DrawningSecondMonorail && y is DrawningSecondMonorail) { // TODO + EntitySecondMonorail entityX = (EntitySecondMonorail)x.EntityMonorail; + EntitySecondMonorail entityY = (EntitySecondMonorail)y.EntityMonorail; + if(entityX.AdditionalColor != entityY.AdditionalColor) + { + return false; + } + if(entityX.Monorails != entityY.Monorails) + { + return false; + } + if(entityX.SecondCabin != entityY.SecondCabin) + { + return false; + } } return true; } diff --git a/Monorail/Monorail/Generics/MonorailCompareByColor.cs b/Monorail/Monorail/Generics/MonorailCompareByColor.cs new file mode 100644 index 0000000..132033f --- /dev/null +++ b/Monorail/Monorail/Generics/MonorailCompareByColor.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Monorail.DrawningObjects; +using Monorail.Entities; + +namespace Monorail.Generics +{ + internal class MonorailCompareByColor : IComparer + { + public int Compare(DrawningMonorail? x, DrawningMonorail? y) + { + if (x == null || x.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(y)); + } + if(x.EntityMonorail.BodyColor.Name != y.EntityMonorail.BodyColor.Name) + { + return x.EntityMonorail.BodyColor.Name.CompareTo(y.EntityMonorail.BodyColor.Name); + } + if (x.GetType().Name != y.GetType().Name) + { + if (x is DrawningMonorail) + return -1; + else + return 1; + } + if (x.GetType().Name == y.GetType().Name && x is DrawningSecondMonorail) + { + EntitySecondMonorail entityX = (EntitySecondMonorail)x.EntityMonorail; + EntitySecondMonorail entityY = (EntitySecondMonorail)y.EntityMonorail; + if (entityX.AdditionalColor.Name != entityY.AdditionalColor.Name) + { + return entityX.AdditionalColor.Name.CompareTo(entityY.AdditionalColor.Name); + } + } + var speedCompare = + x.EntityMonorail.Speed.CompareTo(y.EntityMonorail.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityMonorail.Weight.CompareTo(y.EntityMonorail.Weight); + } + } +} diff --git a/Monorail/Monorail/Generics/MonorailCompareByType.cs b/Monorail/Monorail/Generics/MonorailCompareByType.cs new file mode 100644 index 0000000..496deeb --- /dev/null +++ b/Monorail/Monorail/Generics/MonorailCompareByType.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Monorail.DrawningObjects; + +namespace Monorail.Generics +{ + internal class MonorailCompareByType : IComparer + { + public int Compare(DrawningMonorail? x, DrawningMonorail? y) + { + if(x == null || x.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityMonorail == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityMonorail.Speed.CompareTo(y.EntityMonorail.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityMonorail.Weight.CompareTo(y.EntityMonorail.Weight); + } + } +} diff --git a/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs b/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs new file mode 100644 index 0000000..3057e90 --- /dev/null +++ b/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail.Generics +{ + internal class MonorailsCollectionInfo : IEquatable + { + public string Name { get; protected set; } + public string Description { get; protected set; } + public MonorailsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(MonorailsCollectionInfo? other) + { + // TODO прописать логику сравнения по свойству Name + return Name == other.Name; + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } +} diff --git a/Monorail/Monorail/Generics/MonorailsGenericCollection.cs b/Monorail/Monorail/Generics/MonorailsGenericCollection.cs index 0a05c67..9abdffa 100644 --- a/Monorail/Monorail/Generics/MonorailsGenericCollection.cs +++ b/Monorail/Monorail/Generics/MonorailsGenericCollection.cs @@ -18,6 +18,9 @@ namespace Monorail.Generics private readonly int _placeSizeWidth = 295; private readonly int _placeSizeHeight = 65; private readonly SetGeneric _collection; + + public void Sort(IComparer comparer) => + _collection.SortSet(comparer); public MonorailsGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -26,13 +29,13 @@ namespace Monorail.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static int operator +(MonorailsGenericCollection collect, T? obj) + public static int operator +(MonorailsGenericCollection collect, T obj) { if (obj == null) { return -1; } - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj, new DrawningMonorailEqutables()) ?? -1; } public static T? operator -(MonorailsGenericCollection collect, int pos) { diff --git a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs index 64dc5f7..43e556f 100644 --- a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs +++ b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs @@ -14,14 +14,14 @@ namespace Monorail.Generics private readonly char _separatorRecords = ';'; private static readonly char _separatorForObject = ':'; - readonly Dictionary> _monorailsStorages; - public List Keys => _monorailsStorages.Keys.ToList(); + public List Keys => _monorailsStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; public MonorailsGenericStorage(int pictureWidth, int pictureHeight) { - _monorailsStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; ; @@ -29,27 +29,33 @@ namespace Monorail.Generics public void AddSet(string name) { //TO DO - _monorailsStorages.Add(name, new MonorailsGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { - if (_monorailsStorages[name] == null) + if (_monorailsStorages[new MonorailsCollectionInfo(name, string.Empty)] == null) { return; } - _monorailsStorages.Remove(name); + _monorailsStorages.Remove(new MonorailsCollectionInfo(name, string.Empty)); } - public MonorailsGenericCollection? this[string ind] + public MonorailsGenericCollection this[string ind] { get { - //TO DO - if (_monorailsStorages[ind] == null) + MonorailsCollectionInfo index = new MonorailsCollectionInfo(ind, string.Empty); + if (_monorailsStorages.ContainsKey(index)) { - return null; + return _monorailsStorages[index]; } - return _monorailsStorages[ind]; + return null; } } public void SaveData(string filename) @@ -60,7 +66,7 @@ namespace Monorail.Generics } StringBuilder data = new(); - foreach (KeyValuePair> + foreach (KeyValuePair> record in _monorailsStorages) { StringBuilder records = new(); @@ -122,7 +128,7 @@ namespace Monorail.Generics } } } - _monorailsStorages.Add(record[0], collection); + _monorailsStorages.Add(new MonorailsCollectionInfo(record[0], string.Empty), collection); } } } diff --git a/Monorail/Monorail/Generics/SetGeneric.cs b/Monorail/Monorail/Generics/SetGeneric.cs index b211434..3a512ee 100644 --- a/Monorail/Monorail/Generics/SetGeneric.cs +++ b/Monorail/Monorail/Generics/SetGeneric.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; @@ -18,11 +19,16 @@ namespace Monorail.Generics _maxCount = count; _places = new List(count); } - public int Insert(T monorail) + public void SortSet(IComparer comparer) => + _places.Sort(comparer); + public int Insert(T monorail, IEqualityComparer? equal = null) { - return Insert(monorail, 0); + if (_places.Count == _maxCount) + throw new StorageOverflowException(_maxCount); + Insert(monorail, 0, equal); + return 0; } - public int Insert(T monorail, int position) + public bool Insert(T monorail, int position, IEqualityComparer? equal = null) { if (Count == _maxCount || position >= _maxCount) throw new StorageOverflowException(Count); @@ -30,16 +36,13 @@ namespace Monorail.Generics if (position < 0 || monorail == null) throw new StorageOverflowException("Ошибка. Объект не найден или введённый " + "номер позиции = отрицательное число"); - - if (Count == 0) + if (equal != null) { - _places.Add(monorail); + if (_places.Contains(monorail, equal)) + throw new ArgumentException(nameof(monorail)); } - else - { - _places.Insert(position, monorail); - } - return Count; + _places.Insert(position, monorail); + return true; } public bool Remove(int position) { -- 2.25.1 From 73257bd563f6e768d3e5b904699d4781160f3078 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Sun, 17 Dec 2023 20:37:47 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=81=20=D0=BD=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Monorail/Monorail/FormMonorailCollection.cs | 4 ++-- .../Monorail/Generics/MonorailsCollectionInfo.cs | 13 +++++++++---- .../Monorail/Generics/MonorailsGenericStorage.cs | 1 + 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Monorail/Monorail/FormMonorailCollection.cs b/Monorail/Monorail/FormMonorailCollection.cs index 4d0ee53..1cc0e1c 100644 --- a/Monorail/Monorail/FormMonorailCollection.cs +++ b/Monorail/Monorail/FormMonorailCollection.cs @@ -43,7 +43,7 @@ namespace Monorail 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)) @@ -105,7 +105,7 @@ EventArgs e) "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); + _storage.DelSet(name); ReloadObjects(); _logger.LogInformation($"Удален набор: {name}"); } diff --git a/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs b/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs index 3057e90..5457224 100644 --- a/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs +++ b/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs @@ -8,8 +8,8 @@ namespace Monorail.Generics { internal class MonorailsCollectionInfo : IEquatable { - public string Name { get; protected set; } - public string Description { get; protected set; } + public string Name { get; private set; } + public string Description { get; private set; } public MonorailsCollectionInfo(string name, string description) { Name = name; @@ -18,11 +18,16 @@ namespace Monorail.Generics public bool Equals(MonorailsCollectionInfo? other) { // TODO прописать логику сравнения по свойству Name - return Name == other.Name; + return Name == other?.Name; + throw new NotImplementedException(); } public override int GetHashCode() { - return this.Name.GetHashCode(); + return Name.GetHashCode(); + } + public override string ToString() + { + return "Object: " + Name; } } } diff --git a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs index 43e556f..98a2089 100644 --- a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs +++ b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs @@ -33,6 +33,7 @@ namespace Monorail.Generics { MessageBox.Show("Словарь уже имеет объект с таким названием", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; } _monorailsStorages.Add(new MonorailsCollectionInfo(name, string.Empty), new MonorailsGenericCollection Date: Sun, 17 Dec 2023 21:10:48 +0400 Subject: [PATCH 4/4] final --- .../FormMonorailCollection.Designer.cs | 93 ++++++++----------- Monorail/Monorail/FormMonorailCollection.cs | 6 ++ .../Generics/MonorailsCollectionInfo.cs | 5 - .../Generics/MonorailsGenericStorage.cs | 2 +- 4 files changed, 46 insertions(+), 60 deletions(-) diff --git a/Monorail/Monorail/FormMonorailCollection.Designer.cs b/Monorail/Monorail/FormMonorailCollection.Designer.cs index 4360181..edf4e46 100644 --- a/Monorail/Monorail/FormMonorailCollection.Designer.cs +++ b/Monorail/Monorail/FormMonorailCollection.Designer.cs @@ -57,10 +57,9 @@ // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(6, 39); - pictureBoxCollection.Margin = new Padding(4); + pictureBoxCollection.Location = new Point(5, 31); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(1271, 606); + pictureBoxCollection.Size = new Size(1017, 485); pictureBoxCollection.TabIndex = 5; pictureBoxCollection.TabStop = false; // @@ -75,33 +74,32 @@ panel1.Controls.Add(buttonDelMonorail); panel1.Controls.Add(buttonAddMonorail); panel1.Controls.Add(label1); - panel1.Location = new Point(1281, -1); - panel1.Margin = new Padding(4); + panel1.Location = new Point(1025, -1); panel1.Name = "panel1"; - panel1.Size = new Size(255, 646); + panel1.Size = new Size(204, 517); panel1.TabIndex = 4; // // ButtonSortByColor // ButtonSortByColor.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - ButtonSortByColor.Location = new Point(15, 366); - ButtonSortByColor.Margin = new Padding(4); + ButtonSortByColor.Location = new Point(12, 293); ButtonSortByColor.Name = "ButtonSortByColor"; - ButtonSortByColor.Size = new Size(224, 51); + ButtonSortByColor.Size = new Size(179, 41); ButtonSortByColor.TabIndex = 9; ButtonSortByColor.Text = "Сортировка по цвету"; ButtonSortByColor.UseVisualStyleBackColor = true; + ButtonSortByColor.Click += ButtonSortByColor_Click; // // ButtonSortByType // ButtonSortByType.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - ButtonSortByType.Location = new Point(15, 307); - ButtonSortByType.Margin = new Padding(4); + ButtonSortByType.Location = new Point(12, 246); ButtonSortByType.Name = "ButtonSortByType"; - ButtonSortByType.Size = new Size(224, 51); + ButtonSortByType.Size = new Size(179, 41); ButtonSortByType.TabIndex = 8; ButtonSortByType.Text = "Сортировка по типу"; ButtonSortByType.UseVisualStyleBackColor = true; + ButtonSortByType.Click += ButtonSortByType_Click; // // panel2 // @@ -110,48 +108,43 @@ panel2.Controls.Add(textBoxStorageName); panel2.Controls.Add(button1); panel2.Controls.Add(ButtonAddObject); - panel2.Location = new Point(15, 25); - panel2.Margin = new Padding(4); + panel2.Location = new Point(12, 20); panel2.Name = "panel2"; - panel2.Size = new Size(224, 261); + panel2.Size = new Size(179, 209); panel2.TabIndex = 7; // // label2 // label2.AutoSize = true; - label2.Location = new Point(4, -4); - label2.Margin = new Padding(4, 0, 4, 0); + label2.Location = new Point(3, -3); label2.Name = "label2"; - label2.Size = new Size(79, 25); + label2.Size = new Size(66, 20); label2.TabIndex = 8; label2.Text = "Наборы"; // // listBoxStorages // listBoxStorages.FormattingEnabled = true; - listBoxStorages.ItemHeight = 25; - listBoxStorages.Location = new Point(21, 106); - listBoxStorages.Margin = new Padding(4); + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(17, 85); listBoxStorages.Name = "listBoxStorages"; - listBoxStorages.Size = new Size(179, 104); + listBoxStorages.Size = new Size(144, 84); listBoxStorages.TabIndex = 9; listBoxStorages.Click += ListBoxObjects_SelectedIndexChanged; // // textBoxStorageName // - textBoxStorageName.Location = new Point(21, 25); - textBoxStorageName.Margin = new Padding(4); + textBoxStorageName.Location = new Point(17, 20); textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(178, 31); + textBoxStorageName.Size = new Size(143, 27); textBoxStorageName.TabIndex = 8; // // button1 // button1.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - button1.Location = new Point(20, 218); - button1.Margin = new Padding(4); + button1.Location = new Point(16, 174); button1.Name = "button1"; - button1.Size = new Size(179, 33); + button1.Size = new Size(143, 26); button1.TabIndex = 5; button1.Text = "Удалить набор"; button1.UseVisualStyleBackColor = true; @@ -160,10 +153,9 @@ // ButtonAddObject // ButtonAddObject.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - ButtonAddObject.Location = new Point(22, 64); - ButtonAddObject.Margin = new Padding(4); + ButtonAddObject.Location = new Point(18, 51); ButtonAddObject.Name = "ButtonAddObject"; - ButtonAddObject.Size = new Size(179, 34); + ButtonAddObject.Size = new Size(143, 27); ButtonAddObject.TabIndex = 6; ButtonAddObject.Text = "Добавить набор"; ButtonAddObject.UseVisualStyleBackColor = true; @@ -171,19 +163,17 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(37, 484); - maskedTextBoxNumber.Margin = new Padding(4); + maskedTextBoxNumber.Location = new Point(30, 387); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(155, 31); + maskedTextBoxNumber.Size = new Size(125, 27); maskedTextBoxNumber.TabIndex = 4; // // buttonRefreshCol // buttonRefreshCol.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonRefreshCol.Location = new Point(15, 582); - buttonRefreshCol.Margin = new Padding(4); + buttonRefreshCol.Location = new Point(12, 466); buttonRefreshCol.Name = "buttonRefreshCol"; - buttonRefreshCol.Size = new Size(224, 51); + buttonRefreshCol.Size = new Size(179, 41); buttonRefreshCol.TabIndex = 3; buttonRefreshCol.Text = "Обновить коллекцию"; buttonRefreshCol.UseVisualStyleBackColor = true; @@ -192,10 +182,9 @@ // buttonDelMonorail // buttonDelMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonDelMonorail.Location = new Point(15, 523); - buttonDelMonorail.Margin = new Padding(4); + buttonDelMonorail.Location = new Point(12, 418); buttonDelMonorail.Name = "buttonDelMonorail"; - buttonDelMonorail.Size = new Size(224, 51); + buttonDelMonorail.Size = new Size(179, 41); buttonDelMonorail.TabIndex = 2; buttonDelMonorail.Text = "Удалить монорельс"; buttonDelMonorail.UseVisualStyleBackColor = true; @@ -204,10 +193,9 @@ // buttonAddMonorail // buttonAddMonorail.Font = new Font("Segoe UI", 7.8F, FontStyle.Regular, GraphicsUnit.Point); - buttonAddMonorail.Location = new Point(15, 425); - buttonAddMonorail.Margin = new Padding(4); + buttonAddMonorail.Location = new Point(12, 340); buttonAddMonorail.Name = "buttonAddMonorail"; - buttonAddMonorail.Size = new Size(224, 51); + buttonAddMonorail.Size = new Size(179, 41); buttonAddMonorail.TabIndex = 1; buttonAddMonorail.Text = "Добавить монорельс"; buttonAddMonorail.UseVisualStyleBackColor = true; @@ -216,10 +204,9 @@ // label1 // label1.AutoSize = true; - label1.Location = new Point(4, -4); - label1.Margin = new Padding(4, 0, 4, 0); + label1.Location = new Point(3, -3); label1.Name = "label1"; - label1.Size = new Size(122, 25); + label1.Size = new Size(103, 20); label1.TabIndex = 0; label1.Text = "Инструменты"; // @@ -229,8 +216,7 @@ menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Padding = new Padding(8, 2, 0, 2); - menuStrip1.Size = new Size(1535, 33); + menuStrip1.Size = new Size(1228, 28); menuStrip1.TabIndex = 6; menuStrip1.Text = "menuStrip1"; // @@ -238,20 +224,20 @@ // файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { сохранитьToolStripMenuItem, загрузитьToolStripMenuItem }); файлToolStripMenuItem.Name = "файлToolStripMenuItem"; - файлToolStripMenuItem.Size = new Size(69, 29); + файлToolStripMenuItem.Size = new Size(59, 24); файлToolStripMenuItem.Text = "Файл"; // // сохранитьToolStripMenuItem // сохранитьToolStripMenuItem.Name = "сохранитьToolStripMenuItem"; - сохранитьToolStripMenuItem.Size = new Size(200, 34); + сохранитьToolStripMenuItem.Size = new Size(166, 26); сохранитьToolStripMenuItem.Text = "Сохранить"; сохранитьToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // загрузитьToolStripMenuItem // загрузитьToolStripMenuItem.Name = "загрузитьToolStripMenuItem"; - загрузитьToolStripMenuItem.Size = new Size(200, 34); + загрузитьToolStripMenuItem.Size = new Size(166, 26); загрузитьToolStripMenuItem.Text = "Загрузить"; загрузитьToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // @@ -267,15 +253,14 @@ // // FormMonorailCollection // - AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; BackColor = SystemColors.ControlDarkDark; - ClientSize = new Size(1535, 648); + ClientSize = new Size(1228, 518); Controls.Add(pictureBoxCollection); Controls.Add(panel1); Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; - Margin = new Padding(4); Name = "FormMonorailCollection"; Text = "Набор Монорельсов"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); diff --git a/Monorail/Monorail/FormMonorailCollection.cs b/Monorail/Monorail/FormMonorailCollection.cs index 1cc0e1c..d04844e 100644 --- a/Monorail/Monorail/FormMonorailCollection.cs +++ b/Monorail/Monorail/FormMonorailCollection.cs @@ -154,6 +154,12 @@ EventArgs e) MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogWarning(ex.Message); } + catch (ArgumentException) + { + MessageBox.Show("Такой монорельс уже существует", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning("Такой монорельс уже существует"); + } } private void ButtonRemoveMonorail_Click(object sender, EventArgs e) diff --git a/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs b/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs index 5457224..f67ae2a 100644 --- a/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs +++ b/Monorail/Monorail/Generics/MonorailsCollectionInfo.cs @@ -17,7 +17,6 @@ namespace Monorail.Generics } public bool Equals(MonorailsCollectionInfo? other) { - // TODO прописать логику сравнения по свойству Name return Name == other?.Name; throw new NotImplementedException(); } @@ -25,9 +24,5 @@ namespace Monorail.Generics { return Name.GetHashCode(); } - public override string ToString() - { - return "Object: " + Name; - } } } diff --git a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs index 98a2089..3d2e305 100644 --- a/Monorail/Monorail/Generics/MonorailsGenericStorage.cs +++ b/Monorail/Monorail/Generics/MonorailsGenericStorage.cs @@ -75,7 +75,7 @@ namespace Monorail.Generics { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { -- 2.25.1