From ef09f1cc1e2403757ba7a0fec8bb41694ebff702 Mon Sep 17 00:00:00 2001 From: rakhaliullov Date: Sat, 30 Mar 2024 16:23:24 +0300 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 | 20 +++ .../ListGenericObjects.cs | 79 +++++++++ .../MassiveGenericObjects.cs | 16 +- .../StorageCollection.cs | 80 +++++++++ .../FormAircraftCollection.Designer.cs | 156 ++++++++++++++++-- .../Stormtrooper/FormAircraftCollection.cs | 119 ++++++++++++- 6 files changed, 446 insertions(+), 24 deletions(-) create mode 100644 Stormtrooper/Stormtrooper/CollectionGenericObjects/CollectionType.cs create mode 100644 Stormtrooper/Stormtrooper/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 Stormtrooper/Stormtrooper/CollectionGenericObjects/StorageCollection.cs diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/CollectionType.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..10050bb --- /dev/null +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,20 @@ +namespace Stormtrooper.CollectionGenericObjects; + +/// +/// Тип коллекции +/// +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + /// + /// Массив + /// + Massive = 1, + /// + /// Список + /// + List = 2 +} \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/ListGenericObjects.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..607ad12 --- /dev/null +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper.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>= 0 && position < Count) + { + return _collection[position]; + } + return null; + } + public int Insert(T obj) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO вставка в конец набора + if (Count <= _maxCount) + { + _collection.Add(obj); + return Count; + } + return -1; + } + public int Insert(T obj, int position) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO проверка позиции + // TODO вставка по позиции + if (Count < _maxCount && position>=0 && position < _maxCount) + { + _collection.Insert(position, obj); + return position; + } + return -1; + } + public T Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из списка + T temp = _collection[position]; + if(position>=0 && position < _maxCount) + { + _collection.RemoveAt(position); + return temp; + } + return null; + } + +} + diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs index 62e4526..17be693 100644 --- a/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs @@ -14,7 +14,21 @@ where T : class /// private T?[] _collection; public int Count => _collection.Length; - public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } } + public int SetMaxCount { + set + { + if (value > 0) { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } /// /// Конструктор diff --git a/Stormtrooper/Stormtrooper/CollectionGenericObjects/StorageCollection.cs b/Stormtrooper/Stormtrooper/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..ee0023b --- /dev/null +++ b/Stormtrooper/Stormtrooper/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,80 @@ +using Stormtrooper.CollectionGenericObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper.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 (!(collectionType == CollectionType.None) && !_storages.ContainsKey(name)) + { + if (collectionType == CollectionType.List) + { + _storages.Add(name, new ListGenericObjects()); + } + else if (collectionType == CollectionType.Massive) + { + _storages.Add(name, new MassiveGenericObjects()); + } + } + } + /// + /// Удаление коллекции + /// + /// Название коллекции + 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/Stormtrooper/Stormtrooper/FormAircraftCollection.Designer.cs b/Stormtrooper/Stormtrooper/FormAircraftCollection.Designer.cs index f11846b..13b8629 100644 --- a/Stormtrooper/Stormtrooper/FormAircraftCollection.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormAircraftCollection.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(); buttonRemoveAircraft = new Button(); @@ -37,18 +46,18 @@ buttonAddAiracft = 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(buttonRemoveAircraft); - groupBoxTools.Controls.Add(maskedTextBox); - groupBoxTools.Controls.Add(buttonAddStormtrooper); - groupBoxTools.Controls.Add(buttonAddAiracft); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(980, 0); @@ -59,10 +68,102 @@ groupBoxTools.Tag = ""; groupBoxTools.Text = "Инструменты"; // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(6, 356); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(252, 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(258, 284); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(3, 249); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(252, 29); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 20; + listBoxCollection.Location = new Point(3, 139); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(252, 104); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 104); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(252, 29); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(126, 74); + 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(38, 74); + 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, 37); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(252, 27); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(50, 14); + 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, 601); + buttonRefresh.Location = new Point(3, 224); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(252, 41); buttonRefresh.TabIndex = 6; @@ -73,7 +174,7 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(6, 493); + buttonGoToCheck.Location = new Point(3, 177); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(252, 41); buttonGoToCheck.TabIndex = 5; @@ -84,7 +185,7 @@ // buttonRemoveAircraft // buttonRemoveAircraft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveAircraft.Location = new Point(6, 323); + buttonRemoveAircraft.Location = new Point(3, 130); buttonRemoveAircraft.Name = "buttonRemoveAircraft"; buttonRemoveAircraft.Size = new Size(252, 41); buttonRemoveAircraft.TabIndex = 4; @@ -95,7 +196,7 @@ // maskedTextBox // maskedTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - maskedTextBox.Location = new Point(6, 274); + maskedTextBox.Location = new Point(3, 97); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Size = new Size(252, 27); @@ -105,7 +206,7 @@ // buttonAddStormtrooper // buttonAddStormtrooper.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddStormtrooper.Location = new Point(6, 172); + buttonAddStormtrooper.Location = new Point(3, 50); buttonAddStormtrooper.Name = "buttonAddStormtrooper"; buttonAddStormtrooper.Size = new Size(252, 41); buttonAddStormtrooper.TabIndex = 2; @@ -116,7 +217,7 @@ // buttonAddAiracft // buttonAddAiracft.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddAiracft.Location = new Point(6, 116); + buttonAddAiracft.Location = new Point(3, 3); buttonAddAiracft.Name = "buttonAddAiracft"; buttonAddAiracft.Size = new Size(252, 41); buttonAddAiracft.TabIndex = 1; @@ -130,7 +231,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 26); + comboBoxSelectorCompany.Location = new Point(6, 322); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(252, 28); comboBoxSelectorCompany.TabIndex = 0; @@ -145,6 +246,20 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddStormtrooper); + panelCompanyTools.Controls.Add(buttonAddAiracft); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(buttonRemoveAircraft); + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 391); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(258, 286); + panelCompanyTools.TabIndex = 9; + // // FormAircraftCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -155,8 +270,11 @@ Name = "FormAircraftCollection"; Text = "Коллекция самолетов"; groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); ResumeLayout(false); } @@ -171,5 +289,15 @@ private PictureBox pictureBox; private Button buttonRefresh; private Button buttonGoToCheck; + private Panel panelStorage; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private Button buttonCreateCompany; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormAircraftCollection.cs b/Stormtrooper/Stormtrooper/FormAircraftCollection.cs index 796c0d6..fe42575 100644 --- a/Stormtrooper/Stormtrooper/FormAircraftCollection.cs +++ b/Stormtrooper/Stormtrooper/FormAircraftCollection.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; +using System.Diagnostics.Contracts; using System.Drawing; using System.Linq; using System.Text; @@ -14,27 +15,28 @@ namespace Stormtrooper; public partial class FormAircraftCollection : Form { + /// + /// Хранилище коллекций + /// + private readonly StorageCollection _storageCollection; + /// /// Компания /// - private AbstractCompany? _company = null; + private AbstractCompany? _company; + /// /// Конструктор /// public FormAircraftCollection() { InitializeComponent(); + _storageCollection = new(); } private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new AircraftHangarService(pictureBox.Width, - pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } /// @@ -56,7 +58,7 @@ public partial class FormAircraftCollection : Form break; case nameof(DrawningStormtrooper): drawningAircraft = new DrawningStormtrooper(random.Next(100, 300), random.Next(1000, 3000), - GetColor(random),GetColor(random),Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + GetColor(random), GetColor(random), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); break; default: return; @@ -161,4 +163,103 @@ public partial class FormAircraftCollection : 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(); + + } + + /// + /// Создание компании + /// + /// + /// + 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 AircraftHangarService(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); + } + } + } } -- 2.25.1