From dbe1bfdaa349c82baa3cedcb2efaa496b8795f4a Mon Sep 17 00:00:00 2001 From: user <732603@gmail.com> Date: Sun, 7 Apr 2024 20:04:54 +0400 Subject: [PATCH] =?UTF-8?q?=20=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=E2=84=964?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionType.cs | 19 ++ .../ListGenericObjects.cs | 81 +++++++++ .../MassiveGenericObjects.cs | 9 +- .../StorageCollection.cs | 61 +++++++ .../FormBoatCollection.Designer.cs | 167 ++++++++++++++++-- .../ProjectMotorBoat/FormBoatCollection.cs | 113 +++++++++++- 6 files changed, 420 insertions(+), 30 deletions(-) create mode 100644 ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/CollectionType.cs create mode 100644 ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs diff --git a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/CollectionType.cs b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..294ada6 --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,19 @@ +namespace ProjectMotorBoat.CollectionGenericObjects; + +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 +} diff --git a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/ListGenericObjects.cs b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..0f53bbd --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,81 @@ +namespace ProjectMotorBoat.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 (_collection[position] != null && position < Count - 1) + { + return _collection[position]; + } + return null; + } + + public int Insert(T obj) + { + for (int i = 0; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + return -1; + } + + public int Insert(T obj, int position) + { + if (_collection[position] != null) + { + for (int i = position; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[position] = obj; + return position; + } + } + + for (int i = position; i > 0; i--) + { + if (_collection[i] == null) + { + _collection[position] = obj; + return position; + } + } + } + + return -1; + } + + public T? Remove(int position) + { + if (_collection[position] != null) + { + T? obj = _collection[position]; + _collection[position] = null; + return obj; + } + + return null; + } +} diff --git a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/MassiveGenericObjects.cs index 1c056ac..d4fdc41 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/MassiveGenericObjects.cs @@ -20,7 +20,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects { if (value > 0) { - if (_collection.Length > 0) + if (Count > 0) { Array.Resize(ref _collection, value); } @@ -53,7 +53,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Insert(T obj) { // TODO вставка в свободное место набора ✔ - for (int i = 0; i < _collection.Length; i++) + for (int i = 0; i < Count; i++) { if (_collection[i] == null) { @@ -73,7 +73,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects // TODO вставка ✔ if (_collection[position] != null) { - for (int i = position; i < _collection.Length; i++) + for (int i = position; i < Count; i++) { if (_collection[i] == null) { @@ -101,8 +101,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects // TODO удаление объекта из массива, присвоив элементу массива значение null ✔ if (_collection[position] != null) { + T? obj = _collection[position]; _collection[position] = null; - return _collection[position]; + return obj; } return null; diff --git a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..1422a9d --- /dev/null +++ b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,61 @@ +namespace ProjectMotorBoat.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/ProjectMotorBoat/ProjectMotorBoat/FormBoatCollection.Designer.cs b/ProjectMotorBoat/ProjectMotorBoat/FormBoatCollection.Designer.cs index 3101d35..7a6af43 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/FormBoatCollection.Designer.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/FormBoatCollection.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(); buttonRemoveBoat = new Button(); @@ -37,18 +46,18 @@ buttonAddBoat = 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(buttonRemoveBoat); - groupBoxTools.Controls.Add(maskedTextBoxPosition); - groupBoxTools.Controls.Add(buttonAddMotorBoat); - groupBoxTools.Controls.Add(buttonAddBoat); + 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,12 +67,104 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(6, 300); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(189, 23); + 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, 19); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(195, 246); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(3, 216); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(189, 23); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 15; + listBoxCollection.Location = new Point(3, 101); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(189, 109); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 72); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(189, 23); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(98, 47); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(66, 19); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(25, 47); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(67, 19); + radioButtonMassive.TabIndex = 2; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 18); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(189, 23); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(3, 0); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(125, 15); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(12, 389); + buttonRefresh.Location = new Point(3, 184); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(189, 32); + buttonRefresh.Size = new Size(184, 32); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -72,9 +173,9 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(6, 289); + buttonGoToCheck.Location = new Point(3, 146); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(189, 32); + buttonGoToCheck.Size = new Size(184, 32); buttonGoToCheck.TabIndex = 5; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -83,9 +184,9 @@ // buttonRemoveBoat // buttonRemoveBoat.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveBoat.Location = new Point(6, 201); + buttonRemoveBoat.Location = new Point(3, 108); buttonRemoveBoat.Name = "buttonRemoveBoat"; - buttonRemoveBoat.Size = new Size(189, 32); + buttonRemoveBoat.Size = new Size(184, 32); buttonRemoveBoat.TabIndex = 4; buttonRemoveBoat.Text = "Удалить лодку"; buttonRemoveBoat.UseVisualStyleBackColor = true; @@ -93,7 +194,7 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(6, 172); + maskedTextBoxPosition.Location = new Point(3, 79); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(189, 23); @@ -103,9 +204,9 @@ // buttonAddMotorBoat // buttonAddMotorBoat.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddMotorBoat.Location = new Point(6, 107); + buttonAddMotorBoat.Location = new Point(3, 41); buttonAddMotorBoat.Name = "buttonAddMotorBoat"; - buttonAddMotorBoat.Size = new Size(189, 32); + buttonAddMotorBoat.Size = new Size(184, 32); buttonAddMotorBoat.TabIndex = 2; buttonAddMotorBoat.Text = "Добавление моторной лодки"; buttonAddMotorBoat.UseVisualStyleBackColor = true; @@ -114,9 +215,9 @@ // buttonAddBoat // buttonAddBoat.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddBoat.Location = new Point(6, 69); + buttonAddBoat.Location = new Point(3, 3); buttonAddBoat.Name = "buttonAddBoat"; - buttonAddBoat.Size = new Size(189, 32); + buttonAddBoat.Size = new Size(184, 32); buttonAddBoat.TabIndex = 1; buttonAddBoat.Text = "Добавление лодки"; buttonAddBoat.UseVisualStyleBackColor = true; @@ -128,7 +229,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 22); + comboBoxSelectorCompany.Location = new Point(6, 271); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(189, 23); comboBoxSelectorCompany.TabIndex = 0; @@ -143,6 +244,21 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddBoat); + panelCompanyTools.Controls.Add(buttonAddMotorBoat); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(buttonRemoveBoat); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 329); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(195, 253); + panelCompanyTools.TabIndex = 8; + // // FormBoatCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -153,8 +269,11 @@ Name = "FormBoatCollection"; Text = " Коллекция лодок"; groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); ResumeLayout(false); } @@ -169,5 +288,15 @@ private MaskedTextBox maskedTextBoxPosition; private PictureBox pictureBox; private Button buttonRefresh; + private Panel panelStorage; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private Button buttonCreateCompany; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/ProjectMotorBoat/ProjectMotorBoat/FormBoatCollection.cs b/ProjectMotorBoat/ProjectMotorBoat/FormBoatCollection.cs index 4c35f84..df11543 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/FormBoatCollection.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/FormBoatCollection.cs @@ -8,6 +8,11 @@ namespace ProjectMotorBoat; /// public partial class FormBoatCollection : Form { + /// + /// Хранилише коллекций + /// + private readonly StorageCollection _storageCollection; + /// /// Компания /// @@ -19,6 +24,7 @@ public partial class FormBoatCollection : Form public FormBoatCollection() { InitializeComponent(); + _storageCollection = new(); } /// @@ -28,12 +34,7 @@ public partial class FormBoatCollection : Form /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new BoatHarborService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } /// @@ -136,6 +137,11 @@ public partial class FormBoatCollection : Form } } + /// + /// Передача объекта в другую форму + /// + /// + /// private void ButtonGoToCheck_Click(object sender, EventArgs e) { if (_company == null) @@ -151,7 +157,7 @@ public partial class FormBoatCollection : Form counter--; if (counter <= 0) { - break; + break; } } @@ -167,6 +173,11 @@ public partial class FormBoatCollection : Form form.ShowDialog(); } + /// + /// Перерисовка коллекции + /// + /// + /// private void ButtonRefresh_Click(object sender, EventArgs e) { if (_company == null) @@ -176,4 +187,92 @@ public partial class FormBoatCollection : 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 + } + + /// + /// Обновление списка в 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 BoatHarborService(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } }