From cd2280c3b9edfb1c2e4a677e5c66f132df8666b3 Mon Sep 17 00:00:00 2001 From: vkobi Date: Mon, 8 Apr 2024 21:08:18 +0400 Subject: [PATCH] =?UTF-8?q?4=20=D0=BB=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=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionType.cs | 22 +++ .../ListGenericObjects.cs | 82 ++++++++ .../StorageCollection.cs | 83 ++++++++ .../FormLocomotiveCollection.Designer.cs | 179 +++++++++++++++--- .../FormLocomotiveCollection.cs | 122 +++++++++--- 5 files changed, 438 insertions(+), 50 deletions(-) create mode 100644 WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/CollectionType.cs create mode 100644 WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/StorageCollection.cs diff --git a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/CollectionType.cs b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..bc83857 --- /dev/null +++ b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,22 @@ +namespace WarmlyLocomotive.CollectionGenericObjects; + +/// +/// Тип коллекции +/// +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 +} diff --git a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ListGenericObjects.cs b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..dc38f21 --- /dev/null +++ b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,82 @@ +namespace WarmlyLocomotive.CollectionGenericObjects; + +/// +/// Параметризованный набор объектов +/// +/// Параметр: ограничение - ссылочный тип +public class ListGenericObjects : ICollectionGenericObjects + where T : class +{ + /// + /// Список объектов, которые храним + /// + private readonly List _collection; + + /// + /// Максимально допустимое число объектов в списке + /// + private int _maxCount; + + public int Count => _collection.Count; + + public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + + /// + /// Конструктор + /// + public ListGenericObjects() + { + _collection = new(); + } + + + public T? Get(int position) + { + // TODO проверка позиции + if (position >= Count || position < 0) + { + return null; + } + return _collection[position]; + } + + public int Insert(T obj) + { + // TODO проверка, что не превышено максимальное количество элементов + if (Count == _maxCount) + { + return -1; + } + // TODO вставка в конец набора + _collection.Add(obj); + return _collection.Count; + } + public int Insert(T obj, int position) + { + // TODO проверка, что не превышено максимальное количество элементов + if (Count == _maxCount) + { + return -1; + } + // TODO проверка позиции + if (position >= Count || position < 0) + { + return -1; + } + // TODO вставка по позиции + _collection.Insert(position, obj); + return position; + } + public T? Remove(int position) + { + // TODO проверка позиции + if (position >= Count || position < 0) + { + return null; + } + // TODO удаление объекта из списка + T? obj = _collection[position]; + _collection.RemoveAt(position); + return obj; + } +} diff --git a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/StorageCollection.cs b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..97811ca --- /dev/null +++ b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,83 @@ +namespace WarmlyLocomotive.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 не пустой и нет в словаре записи с таким ключом + if (name == null || _storages.ContainsKey(name)) + { + return; + } + // TODO Прописать логику для добавления + if (collectionType == CollectionType.List) + { + _storages.Add(name, new ListGenericObjects()); + } + if (collectionType == CollectionType.Massive) + { + _storages.Add(name, new MassiveGenericObjects()); + } + } + + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + if (name == null || !_storages.ContainsKey(name)) + { + return; + } + // TODO Прописать логику для удаления коллекции + _storages.Remove(name); + } + + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObjects? this[string name] + { + get + { + // TODO Продумать логику получения объекта + if (_storages.ContainsKey(name)) + { + return _storages[name]; + } + return null; + } + } + +} diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.Designer.cs b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.Designer.cs index 893eb54..e02f33e 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.Designer.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.Designer.cs @@ -29,6 +29,15 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + buttonCreateCompany = new Button(); + panelStorage = new Panel(); + buttonCollectionDel = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + radioButtonMassive = new RadioButton(); + textBoxCollectionName = new TextBox(); + labelCollectionName = new Label(); buttonRefresh = new Button(); buttonGoToCheck = new Button(); buttonRemoveLocomotive = new Button(); @@ -37,34 +46,125 @@ 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(buttonRemoveLocomotive); - groupBoxTools.Controls.Add(maskedTextBoxPosition); - groupBoxTools.Controls.Add(buttonAddWarmlyLocomotive); - groupBoxTools.Controls.Add(buttonAddLocomotive); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(808, 0); + groupBoxTools.Location = new Point(821, 0); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(193, 527); + groupBoxTools.Size = new Size(193, 618); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; - groupBoxTools.Enter += groupBoxTools_Enter; + // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(6, 309); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(181, 29); + buttonCreateCompany.TabIndex = 8; + buttonCreateCompany.Text = "Создать компанию"; + buttonCreateCompany.UseVisualStyleBackColor = true; + buttonCreateCompany.Click += ButtonCreateCompany_Click; + // + // 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.Controls.Add(labelCollectionName); + panelStorage.Dock = DockStyle.Top; + panelStorage.Location = new Point(3, 23); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(187, 246); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(3, 205); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(181, 29); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 20; + listBoxCollection.Location = new Point(6, 135); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(172, 64); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 95); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(181, 29); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(104, 66); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(80, 24); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(3, 66); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(82, 24); + radioButtonMassive.TabIndex = 2; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 33); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(181, 27); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(20, 10); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(158, 20); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; // // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(6, 440); + buttonRefresh.Location = new Point(6, 231); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(175, 48); + buttonRefresh.Size = new Size(172, 34); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -73,9 +173,9 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(6, 348); + buttonGoToCheck.Location = new Point(6, 195); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(175, 48); + buttonGoToCheck.Size = new Size(172, 33); buttonGoToCheck.TabIndex = 5; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -84,9 +184,9 @@ // buttonRemoveLocomotive // buttonRemoveLocomotive.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveLocomotive.Location = new Point(6, 294); + buttonRemoveLocomotive.Location = new Point(6, 144); buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; - buttonRemoveLocomotive.Size = new Size(175, 48); + buttonRemoveLocomotive.Size = new Size(172, 48); buttonRemoveLocomotive.TabIndex = 4; buttonRemoveLocomotive.Text = "Удалить локомотив"; buttonRemoveLocomotive.UseVisualStyleBackColor = true; @@ -94,20 +194,19 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(6, 234); + maskedTextBoxPosition.Location = new Point(6, 111); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(175, 27); maskedTextBoxPosition.TabIndex = 3; maskedTextBoxPosition.ValidatingType = typeof(int); - maskedTextBoxPosition.MaskInputRejected += maskedTextBoxPosition_MaskInputRejected; // // buttonAddWarmlyLocomotive // buttonAddWarmlyLocomotive.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddWarmlyLocomotive.Location = new Point(6, 142); + buttonAddWarmlyLocomotive.Location = new Point(6, 57); buttonAddWarmlyLocomotive.Name = "buttonAddWarmlyLocomotive"; - buttonAddWarmlyLocomotive.Size = new Size(175, 48); + buttonAddWarmlyLocomotive.Size = new Size(172, 48); buttonAddWarmlyLocomotive.TabIndex = 2; buttonAddWarmlyLocomotive.Text = "Добавление локомотива"; buttonAddWarmlyLocomotive.UseVisualStyleBackColor = true; @@ -116,9 +215,9 @@ // buttonAddLocomotive // buttonAddLocomotive.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddLocomotive.Location = new Point(6, 88); + buttonAddLocomotive.Location = new Point(6, 3); buttonAddLocomotive.Name = "buttonAddLocomotive"; - buttonAddLocomotive.Size = new Size(175, 48); + buttonAddLocomotive.Size = new Size(172, 48); buttonAddLocomotive.TabIndex = 1; buttonAddLocomotive.Text = "Добавление тепловоза"; buttonAddLocomotive.UseVisualStyleBackColor = true; @@ -130,7 +229,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Депо" }); - comboBoxSelectorCompany.Location = new Point(6, 26); + comboBoxSelectorCompany.Location = new Point(6, 275); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(175, 28); comboBoxSelectorCompany.TabIndex = 0; @@ -141,24 +240,40 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 0); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(808, 527); + pictureBox.Size = new Size(821, 618); pictureBox.TabIndex = 1; pictureBox.TabStop = false; - pictureBox.Click += pictureBox_Click; + // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddLocomotive); + panelCompanyTools.Controls.Add(buttonAddWarmlyLocomotive); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(buttonRemoveLocomotive); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 347); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(187, 268); + panelCompanyTools.TabIndex = 9; // // FormLocomotiveCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1001, 527); + ClientSize = new Size(1014, 618); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Name = "FormLocomotiveCollection"; Text = "Коллекция локомотивов"; - Load += FormLocomotiveCollection_Load; groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); ResumeLayout(false); } @@ -173,5 +288,15 @@ private Button buttonGoToCheck; private Button buttonRemoveLocomotive; private MaskedTextBox maskedTextBoxPosition; + private Panel panelStorage; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private Button buttonCollectionAdd; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCreateCompany; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs index c30e711..bac166c 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs @@ -8,6 +8,11 @@ namespace WarmlyLocomotive; /// public partial class FormLocomotiveCollection : Form { + /// + /// Хранилише коллекций + /// + private readonly StorageCollection _storageCollection; + /// /// Компания /// @@ -19,6 +24,7 @@ public partial class FormLocomotiveCollection : Form public FormLocomotiveCollection() { InitializeComponent(); + _storageCollection = new(); } /// @@ -28,12 +34,7 @@ public partial class FormLocomotiveCollection : Form /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Депо": - _company = new LocomotiveSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } /// @@ -89,22 +90,6 @@ public partial class FormLocomotiveCollection : Form return color; } - - private void pictureBox_Click(object sender, EventArgs e) - { - - } - - private void FormLocomotiveCollection_Load(object sender, EventArgs e) - { - - } - - private void groupBoxTools_Enter(object sender, EventArgs e) - { - - } - private void ButtonAddWarmlyLocomotive_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarmlyLocomotive)); private void ButtonAddLocomotive_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLocomotive)); @@ -180,8 +165,99 @@ public partial class FormLocomotiveCollection : Form pictureBox.Image = _company.Show(); } - private void maskedTextBoxPosition_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) + + /// + /// Добавление коллекции + /// + /// + /// + 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(); + } + + /// + /// Удаление коллекции + /// + /// + /// + private void ButtonCollectionDel_Click(object sender, EventArgs e) + { + // TODO прописать логику удаления элемента из коллекции + // нужно убедиться, что есть выбранная коллекция + // спросить у пользователя через MessageBox, что он подтверждает, что хочет удалить запись + // удалить и обновить ListBox + if (!radioButtonList.Checked && !radioButtonMassive.Checked || string.IsNullOrEmpty(textBoxCollectionName.Text)) + { + return; + } + + ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + RerfreshListBoxItems(); + } + + /// + /// Создание компании + /// + /// + /// + 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 LocomotiveSharingService(pictureBox.Width, pictureBox.Height, collection); + break; + } + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } + + /// + /// Обновление списка в 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); + } + } } }