diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs index 119eb89..e745f3d 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs @@ -63,7 +63,7 @@ public class ListGenericObjects : ICollectionGenericObjects public int Insert(T obj, IEqualityComparer? comparer = null) { - + if (_collection.Contains(obj, comparer)) throw new AlreadyExistingObject(obj.GetDataForSave()); if (Count == _maxCount) throw new CollectionOwerflowException(Count); _collection.Add(obj); return Count; @@ -110,4 +110,10 @@ public class ListGenericObjects : ICollectionGenericObjects yield return _collection[i]; } } + + public void CollectionSort(IComparer comparer) + { + _collection.Sort(comparer); + } + } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs index d1c7dc6..5548cab 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs @@ -70,6 +70,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Insert(T obj, IEqualityComparer? comparer = null) { // вставка в свободное место набора + if (_collection.Contains(obj, comparer)) throw new AlreadyExistingObject(obj.GetDataForSave()); for (int i = 0; i < Count; ++i) { if (_collection[i] == null) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/StorageCollection.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/StorageCollection.cs index 50b0d19..a0429c3 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/CollectionGenericObjects/StorageCollection.cs @@ -181,7 +181,7 @@ public class StorageCollection { try { - collection.Insert(locomotive); + collection.Insert(locomotive, new DrawningLocomotiveEqutables()); } catch(Exception ex) { diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollection.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollection.Designer.cs index 85e6d7b..50d9d0f 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollection.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollection.Designer.cs @@ -53,6 +53,8 @@ loadToolStripMenuItem = new ToolStripMenuItem(); saveFileDialog = new SaveFileDialog(); openFileDialog = new OpenFileDialog(); + buttonSortByColor = new Button(); + buttonSortByType = new Button(); groupBoxTools.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); @@ -69,7 +71,7 @@ groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(640, 24); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(200, 569); + groupBoxTools.Size = new Size(200, 678); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -98,6 +100,8 @@ // // panelCompanyTools // + panelCompanyTools.Controls.Add(buttonSortByColor); + panelCompanyTools.Controls.Add(buttonSortByType); panelCompanyTools.Controls.Add(buttonAddLocomotive); panelCompanyTools.Controls.Add(buttonRefresh); panelCompanyTools.Controls.Add(maskedTextBoxPosition); @@ -105,9 +109,9 @@ panelCompanyTools.Controls.Add(buttonRemoveLocomotive); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 340); + panelCompanyTools.Location = new Point(3, 364); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(194, 226); + panelCompanyTools.Size = new Size(194, 311); panelCompanyTools.TabIndex = 8; // // buttonAddLocomotive @@ -246,7 +250,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 24); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(640, 569); + pictureBox.Size = new Size(640, 678); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // @@ -290,11 +294,31 @@ // openFileDialog.Filter = "txt file|*.txt"; // + // buttonSortByColor + // + buttonSortByColor.Location = new Point(3, 260); + buttonSortByColor.Name = "buttonSortByColor"; + buttonSortByColor.Size = new Size(188, 31); + buttonSortByColor.TabIndex = 8; + buttonSortByColor.Text = "Сортировка по цвету"; + buttonSortByColor.UseVisualStyleBackColor = true; + buttonSortByColor.Click += ButtonSortByColor_Click; + // + // buttonSortByType + // + buttonSortByType.Location = new Point(3, 223); + buttonSortByType.Name = "buttonSortByType"; + buttonSortByType.Size = new Size(188, 31); + buttonSortByType.TabIndex = 7; + buttonSortByType.Text = "Сортировка по типу"; + buttonSortByType.UseVisualStyleBackColor = true; + buttonSortByType.Click += ButtonSortByType_Click; + // // FormLocomotiveCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(840, 593); + ClientSize = new Size(840, 702); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Controls.Add(menuStrip); @@ -340,5 +364,7 @@ private ToolStripMenuItem loadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollection.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollection.cs index 5fc3458..392d5d6 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollection.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollection.cs @@ -78,19 +78,19 @@ public partial class FormLocomotiveCollection : Form try { - if((_company + locomotive) != -1) + if ((_company + locomotive) != -1) { MessageBox.Show("Объект добавлен"); _logger.LogInformation("Добавлен объект: {entity}", locomotive.GetDataForSave()); pictureBox.Image = _company.Show(); } } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show("Объект не был добавлен"); _logger.LogError("Ошибка: {Message}", ex.Message); } - + } @@ -228,11 +228,11 @@ public partial class FormLocomotiveCollection : Form private void RefreshListBoxItems() { listBoxCollection.Items.Clear(); - foreach(string colName in _storageCollection.Keys) + foreach (var colName in _storageCollection.Keys) { - if (!string.IsNullOrEmpty(colName)) + if (!string.IsNullOrEmpty(colName.Name)) { - listBoxCollection.Items.Add(colName); + listBoxCollection.Items.Add(colName.Name); } } } @@ -284,7 +284,7 @@ public partial class FormLocomotiveCollection : Form MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.LogInformation("Сохранение в файл {filename}", saveFileDialog.FileName); } - catch (Exception ex) + catch (Exception ex) { MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogError("Ошибка {Message}", ex.Message); @@ -307,7 +307,7 @@ public partial class FormLocomotiveCollection : Form MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); _logger.LogInformation("Загрузка прошла успешно из файла, {filename}", openFileDialog.FileName); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); _logger.LogError("Ошибка {Message}", ex.Message); @@ -315,4 +315,36 @@ public partial class FormLocomotiveCollection : Form RefreshListBoxItems(); } } + + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + CompareCars(new DrawningLocomotiveCompareByType()); + } + + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + CompareCars(new DrawningLocomotiveCompareByColor()); + } + + /// + /// Сортировка по сравнителю + /// + /// Сравнитель объектов + private void CompareCars(IComparer comparer) + { + if (_company == null) return; + + _company.Sort(comparer); + pictureBox.Image = _company.Show(); + } }