From f4307c9c879451ed9465aa28135c1945d6a1136e Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Thu, 28 Mar 2024 08:57:08 +0300 Subject: [PATCH] Todo left --- .../CollectionType.cs | 24 +++ .../ListGenericObjects.cs | 59 +++++++ .../StorageCollection.cs | 62 +++++++ .../FormLocomotiveCollection.Designer.cs | 164 ++++++++++++++++-- .../FormLocomotiveCollection.cs | 109 ++++++++++-- 5 files changed, 390 insertions(+), 28 deletions(-) create mode 100644 ProjectElectricLocomotive/CollectionGenericObjects/CollectionType.cs create mode 100644 ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 ProjectElectricLocomotive/CollectionGenericObjects/StorageCollection.cs diff --git a/ProjectElectricLocomotive/CollectionGenericObjects/CollectionType.cs b/ProjectElectricLocomotive/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..821593a --- /dev/null +++ b/ProjectElectricLocomotive/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.CollectionGenericObjects; + +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + /// + /// Массив + /// + Massive = 1, + /// + /// Список + /// + List = 2 +} + diff --git a/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs b/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..264988c --- /dev/null +++ b/ProjectElectricLocomotive/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.CollectionGenericObjects +{ + public class ListGenericObjects : ICollectionGenericObjects + where T: class + { + /// + /// Список объектов, которые храним + /// + private readonly List _collection; + + /// + /// Максимально допустимое число объектов в списке + /// + private int _maxCount; + + public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + + public int Count => _collection.Count; + + /// + /// Конструктор + /// + public ListGenericObjects() + { + _collection = new(); + } + public T? Get(int position) + { + // TODO проверка позиции + return _collection[position]; + } + public int Insert(T obj) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO вставка в конец набора + return 1; + } + public int Insert(T obj, int position) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO проверка позиции + // TODO вставка по позиции + return 1; + } + public T Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из списка + return (T)1; + } + + } +} diff --git a/ProjectElectricLocomotive/CollectionGenericObjects/StorageCollection.cs b/ProjectElectricLocomotive/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..62df72d --- /dev/null +++ b/ProjectElectricLocomotive/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.CollectionGenericObjects; + +/// +/// Класс-хранилище коллекций +/// +/// +public class StorageCollection +where T : class +{ + /// + /// Словарь (хранилище) с коллекциями + /// + readonly Dictionary> _storages; + /// + /// Возвращение списка названий коллекций + /// + public List Keys => _storages.Keys.ToList(); + /// + /// Конструктор + /// + public StorageCollection() + { + _storages = new Dictionary>(); + } + /// + /// Добавление коллекции в хранилище + /// + /// Название коллекции + /// тип коллекции + public void AddCollection(string name, CollectionType collectionType) + { + // TODO проверка, что name не пустой и нет в словаре записи с таким ключом + // TODO Прописать логику для добавления + } + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + // TODO Прописать логику для удаления коллекции + } + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObjects? this[string name] + { + get + { + // TODO Продумать логику получения объекта + return null; + } + } +} diff --git a/ProjectElectricLocomotive/FormLocomotiveCollection.Designer.cs b/ProjectElectricLocomotive/FormLocomotiveCollection.Designer.cs index 290bdfd..08d691f 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollection.Designer.cs +++ b/ProjectElectricLocomotive/FormLocomotiveCollection.Designer.cs @@ -29,6 +29,15 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + buttonCreateCompany = new Button(); + labelCollectionName = new Label(); + panelStorage = new Panel(); + buttonCollectionDel = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + radioButtonMassive = new RadioButton(); + textBoxCollectionName = new TextBox(); buttonRefresh = new Button(); buttonGoToCheck = new Button(); buttonDelLocomotive = new Button(); @@ -37,30 +46,121 @@ buttonAddLocomotive = new Button(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + panelCompanyTools = new Panel(); groupBoxTools.SuspendLayout(); + panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + panelCompanyTools.SuspendLayout(); SuspendLayout(); // // groupBoxTools // - groupBoxTools.Controls.Add(buttonRefresh); - groupBoxTools.Controls.Add(buttonGoToCheck); - groupBoxTools.Controls.Add(buttonDelLocomotive); - groupBoxTools.Controls.Add(maskedTextBox); - groupBoxTools.Controls.Add(buttonAddElectricLocomotive); - groupBoxTools.Controls.Add(buttonAddLocomotive); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(labelCollectionName); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(574, 0); + groupBoxTools.Location = new Point(589, 0); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(226, 450); + groupBoxTools.Size = new Size(503, 450); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(223, 375); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(246, 34); + buttonCreateCompany.TabIndex = 10; + buttonCreateCompany.Text = "Создать компанию"; + buttonCreateCompany.UseVisualStyleBackColor = true; + buttonCreateCompany.Click += buttonCreateCompany_Click; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(242, 9); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(182, 25); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции"; + // + // panelStorage + // + panelStorage.Controls.Add(buttonCollectionDel); + panelStorage.Controls.Add(listBoxCollection); + panelStorage.Controls.Add(buttonCollectionAdd); + panelStorage.Controls.Add(radioButtonList); + panelStorage.Controls.Add(radioButtonMassive); + panelStorage.Controls.Add(textBoxCollectionName); + panelStorage.Location = new Point(220, 30); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(254, 300); + panelStorage.TabIndex = 8; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(3, 254); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(246, 34); + buttonCollectionDel.TabIndex = 13; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += buttonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 25; + listBoxCollection.Location = new Point(3, 119); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(246, 129); + listBoxCollection.TabIndex = 12; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 79); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(246, 34); + buttonCollectionAdd.TabIndex = 9; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(120, 44); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(96, 29); + radioButtonList.TabIndex = 11; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(3, 44); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(98, 29); + radioButtonMassive.TabIndex = 10; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 7); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(246, 31); + textBoxCollectionName.TabIndex = 9; + // // buttonRefresh // - buttonRefresh.Location = new Point(21, 358); + buttonRefresh.Location = new Point(12, 297); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(193, 39); buttonRefresh.TabIndex = 7; @@ -70,7 +170,7 @@ // // buttonGoToCheck // - buttonGoToCheck.Location = new Point(21, 313); + buttonGoToCheck.Location = new Point(12, 252); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(193, 39); buttonGoToCheck.TabIndex = 6; @@ -80,7 +180,7 @@ // // buttonDelLocomotive // - buttonDelLocomotive.Location = new Point(21, 244); + buttonDelLocomotive.Location = new Point(12, 183); buttonDelLocomotive.Name = "buttonDelLocomotive"; buttonDelLocomotive.Size = new Size(193, 63); buttonDelLocomotive.TabIndex = 5; @@ -90,7 +190,7 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(21, 207); + maskedTextBox.Location = new Point(12, 146); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Size = new Size(193, 31); @@ -99,7 +199,7 @@ // // buttonAddElectricLocomotive // - buttonAddElectricLocomotive.Location = new Point(21, 138); + buttonAddElectricLocomotive.Location = new Point(12, 77); buttonAddElectricLocomotive.Name = "buttonAddElectricLocomotive"; buttonAddElectricLocomotive.Size = new Size(193, 63); buttonAddElectricLocomotive.TabIndex = 2; @@ -109,7 +209,7 @@ // // buttonAddLocomotive // - buttonAddLocomotive.Location = new Point(21, 69); + buttonAddLocomotive.Location = new Point(12, 8); buttonAddLocomotive.Name = "buttonAddLocomotive"; buttonAddLocomotive.Size = new Size(193, 63); buttonAddLocomotive.TabIndex = 1; @@ -123,9 +223,9 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Депо" }); - comboBoxSelectorCompany.Location = new Point(21, 30); + comboBoxSelectorCompany.Location = new Point(223, 336); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; - comboBoxSelectorCompany.Size = new Size(193, 33); + comboBoxSelectorCompany.Size = new Size(246, 33); comboBoxSelectorCompany.TabIndex = 0; comboBoxSelectorCompany.SelectedIndexChanged += comboBoxSelectorCompany_SelectedIndexChanged; // @@ -134,22 +234,40 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 0); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(574, 450); + pictureBox.Size = new Size(589, 450); pictureBox.TabIndex = 3; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddLocomotive); + panelCompanyTools.Controls.Add(buttonAddElectricLocomotive); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(buttonDelLocomotive); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(6, 30); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(211, 379); + panelCompanyTools.TabIndex = 4; + // // FormLocomotiveCollection // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(1092, 450); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Name = "FormLocomotiveCollection"; Text = "Коллекция Локомотивов"; groupBoxTools.ResumeLayout(false); groupBoxTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); ResumeLayout(false); } @@ -164,5 +282,15 @@ private Button buttonDelLocomotive; private Button buttonRefresh; private Button buttonGoToCheck; + private Panel panelStorage; + private Label labelCollectionName; + private Button buttonCollectionAdd; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCreateCompany; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/FormLocomotiveCollection.cs b/ProjectElectricLocomotive/FormLocomotiveCollection.cs index 5164df8..afea35a 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollection.cs +++ b/ProjectElectricLocomotive/FormLocomotiveCollection.cs @@ -18,6 +18,12 @@ namespace ProjectElectricLocomotive; /// public partial class FormLocomotiveCollection : Form { + + /// + /// Хранилище коллекций + /// + private readonly StorageCollection _storageCollection; + /// /// Компания /// @@ -28,6 +34,7 @@ public partial class FormLocomotiveCollection : Form public FormLocomotiveCollection() { InitializeComponent(); + _storageCollection = new(); } @@ -38,13 +45,7 @@ public partial class FormLocomotiveCollection : Form /// private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Депо": - _company = new LocomotiveDepo(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } - + panelCompanyTools.Enabled = false; } /// /// Создание объекта класса-перемещения @@ -59,13 +60,13 @@ public partial class FormLocomotiveCollection : Form case nameof(DrawningLocomotive): drawningLocomotive = new DrawningLocomotive(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); - + break; case nameof(DrawningElectricLocomotive): drawningLocomotive = new DrawningElectricLocomotive(random.Next(100, 300), random.Next(1000, 3000), GetColor(random), GetColor(random), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - + break; default: return; @@ -201,5 +202,93 @@ public partial class FormLocomotiveCollection : Form pictureBox.Image = _company.Show(); } -} + /// + /// Обновление списка в listBoxCollection + /// + private void RerfreshListBoxItems() + { + listBoxCollection.Items.Clear(); + for (int i = 0; i < _storageCollection.Keys?.Count; ++i) + { + string? colName = _storageCollection.Keys?[i]; + if (!string.IsNullOrEmpty(colName)) + { + listBoxCollection.Items.Add(colName); + } + } + } + + /// + /// Создание компании + /// + /// + /// + private void buttonCreateCompany_Click(object sender, EventArgs e) + { + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + ICollectionGenericObjects? collection = + _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; + if (collection == null) + { + MessageBox.Show("Коллекция не проинициализирована"); + return; + } + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new LocomotiveDepo(pictureBox.Width, + pictureBox.Height, collection); + break; + } + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } + + + + /// + /// Удаление коллекции + /// + /// + /// + private void buttonCollectionDel_Click(object sender, EventArgs e) + { + + // TODO прописать логику удаления элемента из коллекции + // нужно убедиться, что есть выбранная коллекция + // спросить у пользователя через MessageBox, что он подтверждает, что хочет удалить запись + // удалить и обновить ListBox + } + + /// + /// Добавление коллекции + /// + /// + /// + private void buttonCollectionAdd_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + CollectionType collectionType = CollectionType.None; + if (radioButtonMassive.Checked) + { + collectionType = CollectionType.Massive; + } + else if (radioButtonList.Checked) + { + collectionType = CollectionType.List; + } + _storageCollection.AddCollection(textBoxCollectionName.Text, + collectionType); + RerfreshListBoxItems(); + } +}