From 34b237b368b2c113105414f43a1ed72d0f95faac Mon Sep 17 00:00:00 2001 From: 5_fG Date: Mon, 29 Apr 2024 12:21:40 +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=20=E2=84=96=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionType.cs | 22 +++ .../ListGenericObjects.cs | 87 ++++++++ .../MassiveGenericObjects.cs | 20 +- .../StorageCollection.cs | 82 ++++++++ .../FormExcavatorCollection.Designer.cs | 185 ++++++++++++++++-- .../FormExcavatorCollection.cs | 106 ++++++++++ 6 files changed, 466 insertions(+), 36 deletions(-) create mode 100644 ProjectExcavator/ProjectExcavator/CollectionGenericObjects/CollectionType.cs create mode 100644 ProjectExcavator/ProjectExcavator/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 ProjectExcavator/ProjectExcavator/CollectionGenericObjects/StorageCollection.cs diff --git a/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/CollectionType.cs b/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..04a9763 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,22 @@ +namespace ProjectExcavator.CollectionGenericObjects; + +/// +/// Тип коллекции +/// +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/ListGenericObjects.cs b/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..817e959 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,87 @@ +namespace ProjectExcavator.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) + { + if (position >= 0 && position < Count) + { + return _collection[position]; + } + else + { + return null; + } + } + + public int Insert(T obj) + { + //проверка, что не превышено максимальное количество элементов + //вставка в конец набора + + if (_collection.Count > _maxCount) + { + return -1; + } + _collection.Add(obj); + return 1; + + } + + public int Insert(T obj, int position) + { + // проверка, что не превышено максимальное количество элементов + // проверка позиции + // вставка по позиции + + if ((_collection.Count > _maxCount) || (position < 0 || position > Count)) + { + return -1; + } + _collection.Insert(position, obj); + return 1; + } + + public T? Remove(int position) + { + // проверка позиции + // удаление объекта из списка + + if (position < 0 || position > Count) + { + return null; + } + T? obj = _collection[position]; + _collection.RemoveAt(position); + return obj; + + + } +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/MassiveGenericObjects.cs index c953890..1831f84 100644 --- a/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/MassiveGenericObjects.cs @@ -58,15 +58,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects { //Вставка в свободное место набора - for (int i = 0; i < Count -1; i++) - { - if (_collection[i] == null) - { - _collection[i] = obj; - return i; - } - } - return -1; + return Insert(obj, 0); } public int Insert(T obj, int position) @@ -117,17 +109,13 @@ public class MassiveGenericObjects : ICollectionGenericObjects // Проверка позиции // Удаление объекта из массива, присвоив элементу массива значение null - T? obj = _collection[position]; if (position < 0 || _collection[position] == null || position > Count -1) { return null; } - - else - { - _collection[position] = null; - return obj; - } + T obj = _collection[position]; + _collection[position] = null; + return obj; } } diff --git a/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/StorageCollection.cs b/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..128c57e --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; +using System.Xml.Linq; + +namespace ProjectExcavator.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) + { + if (name == null || _storages.ContainsKey(name)) + { + return; + } + else + { + switch (collectionType) + { + case CollectionType.None: + return; + case CollectionType.List: + _storages.Add(name, new ListGenericObjects { }); + return; + case CollectionType.Massive: + _storages.Add(name, new MassiveGenericObjects { }); + return; + } + } + } + + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + if (name == null || !_storages.ContainsKey(name)) { return; } + _storages.Remove(name); + } + + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + + public ICollectionGenericObjects? this[string name] + { + get + { + if (name == null || !_storages.ContainsKey(name)) { return null; } + return _storages[name]; + } + } +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs index dbbe438..4825a31 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.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(); ButtonRemoveExcavator = new Button(); @@ -37,18 +46,18 @@ buttonAddTrackedVehicle = 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(ButtonRemoveExcavator); - groupBoxTools.Controls.Add(maskedTextBoxPosition); - groupBoxTools.Controls.Add(buttonAddExcavator); - groupBoxTools.Controls.Add(buttonAddTrackedVehicle); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(777, 0); @@ -58,11 +67,115 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(13, 317); + buttonCreateCompany.Margin = new Padding(3, 4, 3, 4); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(200, 30); + buttonCreateCompany.TabIndex = 9; + 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(219, 253); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Anchor = AnchorStyles.Top; + buttonCollectionDel.Location = new Point(3, 222); + buttonCollectionDel.Margin = new Padding(3, 4, 3, 4); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(215, 27); + buttonCollectionDel.TabIndex = 7; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.Anchor = AnchorStyles.Top; + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 20; + listBoxCollection.Location = new Point(3, 135); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(215, 84); + listBoxCollection.TabIndex = 6; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Anchor = AnchorStyles.Top; + buttonCollectionAdd.Location = new Point(3, 100); + buttonCollectionAdd.Margin = new Padding(3, 4, 3, 4); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(215, 27); + buttonCollectionAdd.TabIndex = 5; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(130, 70); + radioButtonList.Margin = new Padding(3, 4, 3, 4); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(80, 24); + radioButtonList.TabIndex = 4; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(10, 70); + radioButtonMassive.Margin = new Padding(3, 4, 3, 4); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(82, 24); + radioButtonMassive.TabIndex = 3; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Anchor = AnchorStyles.Top; + textBoxCollectionName.Location = new Point(3, 34); + textBoxCollectionName.Margin = new Padding(3, 4, 3, 4); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(215, 27); + textBoxCollectionName.TabIndex = 2; + // + // labelCollectionName + // + labelCollectionName.Anchor = AnchorStyles.Top; + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(31, 11); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(158, 20); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // // buttonRefresh // - buttonRefresh.Location = new Point(13, 554); + buttonRefresh.Anchor = AnchorStyles.Top; + buttonRefresh.Location = new Point(3, 240); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(200, 60); + buttonRefresh.Size = new Size(215, 40); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -70,9 +183,10 @@ // // buttonGoToCheck // - buttonGoToCheck.Location = new Point(13, 469); + buttonGoToCheck.Anchor = AnchorStyles.Top; + buttonGoToCheck.Location = new Point(3, 194); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(200, 60); + buttonGoToCheck.Size = new Size(215, 40); buttonGoToCheck.TabIndex = 5; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -80,9 +194,10 @@ // // ButtonRemoveExcavator // - ButtonRemoveExcavator.Location = new Point(10, 386); + ButtonRemoveExcavator.Anchor = AnchorStyles.Top; + ButtonRemoveExcavator.Location = new Point(3, 148); ButtonRemoveExcavator.Name = "ButtonRemoveExcavator"; - ButtonRemoveExcavator.Size = new Size(200, 60); + ButtonRemoveExcavator.Size = new Size(215, 40); ButtonRemoveExcavator.TabIndex = 4; ButtonRemoveExcavator.Text = "Удаление транспорта"; ButtonRemoveExcavator.UseVisualStyleBackColor = true; @@ -90,18 +205,20 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(13, 281); + maskedTextBoxPosition.Anchor = AnchorStyles.Top; + maskedTextBoxPosition.Location = new Point(3, 115); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - maskedTextBoxPosition.Size = new Size(200, 27); + maskedTextBoxPosition.Size = new Size(215, 27); maskedTextBoxPosition.TabIndex = 3; maskedTextBoxPosition.ValidatingType = typeof(int); // // buttonAddExcavator // - buttonAddExcavator.Location = new Point(13, 164); + buttonAddExcavator.Anchor = AnchorStyles.Top; + buttonAddExcavator.Location = new Point(2, 59); buttonAddExcavator.Name = "buttonAddExcavator"; - buttonAddExcavator.Size = new Size(200, 65); + buttonAddExcavator.Size = new Size(215, 50); buttonAddExcavator.TabIndex = 2; buttonAddExcavator.Text = "Добавление экскаватора"; buttonAddExcavator.UseVisualStyleBackColor = true; @@ -109,9 +226,10 @@ // // buttonAddTrackedVehicle // - buttonAddTrackedVehicle.Location = new Point(10, 93); + buttonAddTrackedVehicle.Anchor = AnchorStyles.Top; + buttonAddTrackedVehicle.Location = new Point(3, 3); buttonAddTrackedVehicle.Name = "buttonAddTrackedVehicle"; - buttonAddTrackedVehicle.Size = new Size(200, 65); + buttonAddTrackedVehicle.Size = new Size(215, 50); buttonAddTrackedVehicle.TabIndex = 1; buttonAddTrackedVehicle.Text = "Добавление гусеничной машины"; buttonAddTrackedVehicle.UseVisualStyleBackColor = true; @@ -123,7 +241,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(10, 26); + comboBoxSelectorCompany.Location = new Point(13, 282); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(200, 28); comboBoxSelectorCompany.TabIndex = 0; @@ -138,6 +256,20 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddTrackedVehicle); + panelCompanyTools.Controls.Add(buttonAddExcavator); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(ButtonRemoveExcavator); + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(2, 350); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(220, 289); + panelCompanyTools.TabIndex = 2; + // // FormExcavatorCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -148,8 +280,11 @@ Name = "FormExcavatorCollection"; 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 +299,15 @@ private MaskedTextBox maskedTextBoxPosition; private Button buttonGoToCheck; private Button buttonRefresh; + private Panel panelStorage; + private Label labelCollectionName; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private Button buttonCollectionDel; + private Button buttonCreateCompany; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs index 1fc62c0..6293088 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs @@ -7,6 +7,11 @@ namespace ProjectExcavator; /// public partial class FormExcavatorCollection : Form { + /// + /// Хранилише коллекций + /// + private readonly StorageCollection _storageCollection; + /// /// Компания /// @@ -18,6 +23,7 @@ public partial class FormExcavatorCollection : Form public FormExcavatorCollection() { InitializeComponent(); + _storageCollection = new(); } /// @@ -181,4 +187,104 @@ public partial class FormExcavatorCollection : 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) + { + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItems == 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 GarageService(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } + } -- 2.25.1