From e84806f6da951d13b334c669552e5cdd75d800d0 Mon Sep 17 00:00:00 2001 From: aaahsap Date: Mon, 1 Apr 2024 16:12:24 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=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=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionType.cs | 25 +++ .../ListGenericObjects.cs | 70 ++++++ .../StorageCollection.cs | 76 +++++++ Lab1/Lab1/FormTruckCollection.Designer.cs | 201 ++++++++++++++---- Lab1/Lab1/FormTruckCollection.cs | 108 +++++++++- 5 files changed, 437 insertions(+), 43 deletions(-) create mode 100644 Lab1/Lab1/CollectionGenericObjects/CollectionType.cs create mode 100644 Lab1/Lab1/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 Lab1/Lab1/CollectionGenericObjects/StorageCollection.cs diff --git a/Lab1/Lab1/CollectionGenericObjects/CollectionType.cs b/Lab1/Lab1/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..07b2a8d --- /dev/null +++ b/Lab1/Lab1/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lab1.CollectionGenericObjects; + +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 +} diff --git a/Lab1/Lab1/CollectionGenericObjects/ListGenericObjects.cs b/Lab1/Lab1/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..b816b48 --- /dev/null +++ b/Lab1/Lab1/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lab1.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 проверка, что не превышено максимальное количество элементов + // TODO вставка в конец набора + if (Count == _maxCount) return -1; + _collection.Add(obj); + return Count; + } + + public int Insert(T obj, int position) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO проверка позиции + // TODO вставка по позиции + if (Count == _maxCount) return -1; + if (position >= Count || position < 0) return -1; + _collection.Insert(position, obj); + return position; + } + + public T Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из списка + if (position >= Count || position < 0) return null; + T obj = _collection[position]; + _collection.RemoveAt(position); + return obj; + } + } + diff --git a/Lab1/Lab1/CollectionGenericObjects/StorageCollection.cs b/Lab1/Lab1/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..481a19a --- /dev/null +++ b/Lab1/Lab1/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lab1.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 Прописать логику для добавления + if (_storages.ContainsKey(name)) return; + if (collectionType == CollectionType.None) return; + else if (collectionType == CollectionType.Massive) + _storages[name] = new MassiveGenericObjects(); + else if (collectionType == CollectionType.List) + _storages[name] = new ListGenericObjects(); + } + + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + // TODO Прописать логику для удаления коллекции + if (_storages.ContainsKey(name)) + _storages.Remove(name); + } + + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObjects? this[string name] + { + get + { + // TODO Продумать логику получения объекта + if (_storages.ContainsKey(name)) + return _storages[name]; + return null; + } + } +} diff --git a/Lab1/Lab1/FormTruckCollection.Designer.cs b/Lab1/Lab1/FormTruckCollection.Designer.cs index cb7e01f..d065824 100644 --- a/Lab1/Lab1/FormTruckCollection.Designer.cs +++ b/Lab1/Lab1/FormTruckCollection.Designer.cs @@ -29,26 +29,35 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + panelCompanyTools = new Panel(); buttonRefresh = new Button(); buttonGoToCheck = new Button(); buttonRemoveTruck = new Button(); - maskedTextBoxPosition = new MaskedTextBox(); - buttonAddRoadTrain = new Button(); buttonAddTruck = new Button(); + buttonAddRoadTrain = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + 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(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); groupBoxTools.SuspendLayout(); + panelCompanyTools.SuspendLayout(); + panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); SuspendLayout(); // // groupBoxTools // - groupBoxTools.Controls.Add(buttonRefresh); - groupBoxTools.Controls.Add(buttonGoToCheck); - groupBoxTools.Controls.Add(buttonRemoveTruck); - groupBoxTools.Controls.Add(maskedTextBoxPosition); - groupBoxTools.Controls.Add(buttonAddRoadTrain); - groupBoxTools.Controls.Add(buttonAddTruck); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(807, 0); @@ -58,12 +67,25 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(buttonRemoveTruck); + panelCompanyTools.Controls.Add(buttonAddTruck); + panelCompanyTools.Controls.Add(buttonAddRoadTrain); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Location = new Point(0, 338); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(245, 278); + panelCompanyTools.TabIndex = 8; + // // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(16, 423); + buttonRefresh.Location = new Point(16, 216); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(214, 58); + buttonRefresh.Size = new Size(214, 29); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -72,9 +94,9 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(16, 333); + buttonGoToCheck.Location = new Point(16, 179); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(214, 58); + buttonGoToCheck.Size = new Size(214, 31); buttonGoToCheck.TabIndex = 5; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -83,38 +105,18 @@ // buttonRemoveTruck // buttonRemoveTruck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveTruck.Location = new Point(16, 248); + buttonRemoveTruck.Location = new Point(16, 144); buttonRemoveTruck.Name = "buttonRemoveTruck"; - buttonRemoveTruck.Size = new Size(214, 58); + buttonRemoveTruck.Size = new Size(214, 29); buttonRemoveTruck.TabIndex = 4; buttonRemoveTruck.Text = "Удалить грузовик"; buttonRemoveTruck.UseVisualStyleBackColor = true; buttonRemoveTruck.Click += ButtonRemoveCar_Click; // - // maskedTextBoxPosition - // - maskedTextBoxPosition.Location = new Point(16, 215); - maskedTextBoxPosition.Mask = "00"; - maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - maskedTextBoxPosition.Size = new Size(214, 27); - maskedTextBoxPosition.TabIndex = 3; - maskedTextBoxPosition.ValidatingType = typeof(int); - // - // buttonAddRoadTrain - // - buttonAddRoadTrain.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddRoadTrain.Location = new Point(16, 133); - buttonAddRoadTrain.Name = "buttonAddRoadTrain"; - buttonAddRoadTrain.Size = new Size(214, 58); - buttonAddRoadTrain.TabIndex = 2; - buttonAddRoadTrain.Text = "Добавление грузовика с дополнениями"; - buttonAddRoadTrain.UseVisualStyleBackColor = true; - buttonAddRoadTrain.Click += ButtonAddRoadTrain_Click; - // // buttonAddTruck // buttonAddTruck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddTruck.Location = new Point(16, 98); + buttonAddTruck.Location = new Point(16, 12); buttonAddTruck.Name = "buttonAddTruck"; buttonAddTruck.Size = new Size(214, 29); buttonAddTruck.TabIndex = 1; @@ -122,13 +124,125 @@ buttonAddTruck.UseVisualStyleBackColor = true; buttonAddTruck.Click += ButtonAddTruck_Click; // + // buttonAddRoadTrain + // + buttonAddRoadTrain.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddRoadTrain.Location = new Point(16, 47); + buttonAddRoadTrain.Name = "buttonAddRoadTrain"; + buttonAddRoadTrain.Size = new Size(214, 58); + buttonAddRoadTrain.TabIndex = 2; + buttonAddRoadTrain.Text = "Добавление грузовика с дополнениями"; + buttonAddRoadTrain.UseVisualStyleBackColor = true; + buttonAddRoadTrain.Click += ButtonAddRoadTrain_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(16, 111); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(214, 27); + maskedTextBoxPosition.TabIndex = 3; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(16, 303); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(214, 29); + buttonCreateCompany.TabIndex = 7; + 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(236, 229); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(13, 191); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(214, 29); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += buttonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 20; + listBoxCollection.Location = new Point(13, 121); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(214, 64); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(13, 86); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(214, 29); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(147, 56); + 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(13, 56); + 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(13, 23); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(214, 27); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(44, 0); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(158, 20); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // // comboBoxSelectorCompany // comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(16, 26); + comboBoxSelectorCompany.Location = new Point(16, 258); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(214, 28); comboBoxSelectorCompany.TabIndex = 0; @@ -153,7 +267,10 @@ Name = "FormTruckCollection"; Text = "Коллекция грузовиков"; groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); ResumeLayout(false); } @@ -169,5 +286,15 @@ private Button buttonRemoveTruck; private Button buttonGoToCheck; private Button buttonRefresh; + private Panel panelStorage; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private Button buttonCreateCompany; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/Lab1/Lab1/FormTruckCollection.cs b/Lab1/Lab1/FormTruckCollection.cs index ce6d3cd..d0fdb12 100644 --- a/Lab1/Lab1/FormTruckCollection.cs +++ b/Lab1/Lab1/FormTruckCollection.cs @@ -15,6 +15,10 @@ namespace Lab1; public partial class FormTruckCollection : Form { + /// + /// Хранилише коллекций + /// + private readonly StorageCollection _storageCollection; /// /// Компания /// @@ -26,6 +30,7 @@ public partial class FormTruckCollection : Form public FormTruckCollection() { InitializeComponent(); + _storageCollection = new(); } /// /// Выбор компании @@ -34,12 +39,7 @@ public partial class FormTruckCollection : Form /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new TruckPark(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } /// @@ -192,4 +192,100 @@ public partial class FormTruckCollection : Form pictureBox.Image = _company.Show(); } + /// + /// Добавление коллекции + /// + /// + /// + 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 (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + 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); + } + } + + } + /// + /// Создание компании + /// + /// + /// + 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 TruckPark(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } }