From 7d44b74eba9b6dbf0b329de678c2ed84f0c4c9d4 Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Thu, 28 Mar 2024 18:07:22 +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 | 20 ++ .../ListGenericObjects.cs | 68 +++++ .../StorageCollection.cs | 77 ++++++ .../FormAirplaneCollection.Designer.cs | 247 +++++++++++++----- .../FormAirplaneCollection.cs | 102 +++++++- 5 files changed, 449 insertions(+), 65 deletions(-) create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/CollectionType.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/CollectionType.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..4dfe50f --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,20 @@ +namespace ProjectAirplaneWithRadar.CollectionGenericObjects +{ + public enum CollectionType + { + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..9e05461 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,68 @@ +namespace ProjectAirplaneWithRadar.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 >= Count || position < 0) + return null; + + return _collection[position]; + } + + public int Insert(T obj) + { + if (Count + 1 > _maxCount) + return -1; + _collection.Add(obj); + return Count; + } + + public int Insert(T obj, int position) + { + if (Count + 1 > _maxCount) + return -1; + if (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? temp = _collection[position]; + _collection.RemoveAt(position); + return temp; + } + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..75b6233 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,77 @@ +namespace ProjectAirplaneWithRadar.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; + + switch (collectionType) + { + case CollectionType.None: + return; + case CollectionType.Massive: + _storages[name] = new MassiveGenericObjects(); + return; + case CollectionType.List: + _storages[name] = new ListGenericObjects(); + return; + } + } + + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + if(_storages.ContainsKey(name)) + _storages.Remove(name); + } + + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObjects? this[string name] + { + get + { + if (name == null || !_storages.ContainsKey(name)) + return null; + + return _storages[name]; + } + } + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs index 8928c33..5dd90ab 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.Designer.cs @@ -29,26 +29,35 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); - buttonRefresh = new Button(); - buttonGoToCheck = new Button(); - buttonRemoveAirplane = new Button(); - maskedTextBoxPosition = new MaskedTextBox(); - buttonAddAirplaneWithRadar = new Button(); + panelCompanyTools = new Panel(); buttonAddAirplane = new Button(); + buttonAddAirplaneWithRadar = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonRefresh = new Button(); + buttonRemoveAirplane = new Button(); + buttonGoToCheck = new Button(); + 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(buttonRemoveAirplane); - groupBoxTools.Controls.Add(maskedTextBoxPosition); - groupBoxTools.Controls.Add(buttonAddAirplaneWithRadar); - groupBoxTools.Controls.Add(buttonAddAirplane); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(883, 0); @@ -58,69 +67,176 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // - // buttonRefresh + // panelCompanyTools // - buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(109, 513); - buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(91, 56); - buttonRefresh.TabIndex = 6; - buttonRefresh.Text = "Обновить"; - buttonRefresh.UseVisualStyleBackColor = true; - buttonRefresh.Click += ButtonRefresh_Click; + panelCompanyTools.Controls.Add(buttonAddAirplane); + panelCompanyTools.Controls.Add(buttonAddAirplaneWithRadar); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(buttonRemoveAirplane); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 417); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(200, 187); + panelCompanyTools.TabIndex = 8; // - // buttonGoToCheck + // buttonAddAirplane // - buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(6, 513); - buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(91, 56); - buttonGoToCheck.TabIndex = 5; - buttonGoToCheck.Text = "Передать на тесты"; - buttonGoToCheck.UseVisualStyleBackColor = true; - buttonGoToCheck.Click += buttonGoToCheck_Click; + buttonAddAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddAirplane.Location = new Point(0, 3); + buttonAddAirplane.Name = "buttonAddAirplane"; + buttonAddAirplane.Size = new Size(97, 56); + buttonAddAirplane.TabIndex = 1; + buttonAddAirplane.Text = "Добавить самолет"; + buttonAddAirplane.UseVisualStyleBackColor = true; + buttonAddAirplane.Click += ButtonAddAirplane_Click; // - // buttonRemoveAirplane + // buttonAddAirplaneWithRadar // - buttonRemoveAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveAirplane.Location = new Point(109, 204); - buttonRemoveAirplane.Name = "buttonRemoveAirplane"; - buttonRemoveAirplane.Size = new Size(91, 43); - buttonRemoveAirplane.TabIndex = 4; - buttonRemoveAirplane.Text = "Удалить самолет"; - buttonRemoveAirplane.UseVisualStyleBackColor = true; - buttonRemoveAirplane.Click += ButtonRemoveAirplane_Click; + buttonAddAirplaneWithRadar.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddAirplaneWithRadar.Location = new Point(103, 3); + buttonAddAirplaneWithRadar.Name = "buttonAddAirplaneWithRadar"; + buttonAddAirplaneWithRadar.Size = new Size(97, 56); + buttonAddAirplaneWithRadar.TabIndex = 2; + buttonAddAirplaneWithRadar.Text = "Добавить самолет с радаром"; + buttonAddAirplaneWithRadar.UseVisualStyleBackColor = true; + buttonAddAirplaneWithRadar.Click += ButtonAddAirplaneWithRadar_Click; // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(6, 215); + maskedTextBoxPosition.Location = new Point(0, 81); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(78, 23); maskedTextBoxPosition.TabIndex = 3; maskedTextBoxPosition.ValidatingType = typeof(int); // - // buttonAddAirplaneWithRadar + // buttonRefresh // - buttonAddAirplaneWithRadar.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddAirplaneWithRadar.Location = new Point(109, 98); - buttonAddAirplaneWithRadar.Name = "buttonAddAirplaneWithRadar"; - buttonAddAirplaneWithRadar.Size = new Size(91, 56); - buttonAddAirplaneWithRadar.TabIndex = 2; - buttonAddAirplaneWithRadar.Text = "Добавить самолет с радаром"; - buttonAddAirplaneWithRadar.UseVisualStyleBackColor = true; - buttonAddAirplaneWithRadar.Click += ButtonAddAirplaneWithRadar_Click; + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(103, 123); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(97, 56); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; // - // buttonAddAirplane + // buttonRemoveAirplane // - buttonAddAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddAirplane.Location = new Point(6, 98); - buttonAddAirplane.Name = "buttonAddAirplane"; - buttonAddAirplane.Size = new Size(91, 56); - buttonAddAirplane.TabIndex = 1; - buttonAddAirplane.Text = "Добавить самолет"; - buttonAddAirplane.UseVisualStyleBackColor = true; - buttonAddAirplane.Click += ButtonAddAirplane_Click; + buttonRemoveAirplane.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveAirplane.Location = new Point(103, 70); + buttonRemoveAirplane.Name = "buttonRemoveAirplane"; + buttonRemoveAirplane.Size = new Size(97, 43); + buttonRemoveAirplane.TabIndex = 4; + buttonRemoveAirplane.Text = "Удалить самолет"; + buttonRemoveAirplane.UseVisualStyleBackColor = true; + buttonRemoveAirplane.Click += ButtonRemoveAirplane_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(0, 123); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(97, 56); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += buttonGoToCheck_Click; + // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(6, 305); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(194, 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(200, 251); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(3, 221); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(194, 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, 121); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(194, 94); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 82); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(194, 23); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(106, 57); + 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(27, 57); + 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, 28); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(194, 23); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(38, 10); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(125, 15); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; // // comboBoxSelectorCompany // @@ -128,7 +244,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 22); + comboBoxSelectorCompany.Location = new Point(6, 276); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(194, 23); comboBoxSelectorCompany.TabIndex = 0; @@ -153,7 +269,10 @@ Name = "FormAirplaneCollection"; 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 +288,15 @@ private Button buttonRefresh; private Button buttonGoToCheck; private Button buttonRemoveAirplane; + 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/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs index 44d3655..1130c95 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs @@ -8,6 +8,11 @@ namespace ProjectAirplaneWithRadar /// public partial class FormAirplaneCollection : Form { + /// + /// Хранилише коллекций + /// + private readonly StorageCollection _storageCollection; + /// /// Компания /// @@ -19,6 +24,7 @@ namespace ProjectAirplaneWithRadar public FormAirplaneCollection() { InitializeComponent(); + _storageCollection = new(); } /// @@ -28,12 +34,7 @@ namespace ProjectAirplaneWithRadar /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new PlaneSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } /// @@ -185,5 +186,94 @@ namespace ProjectAirplaneWithRadar 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); + RefreshListBoxItems(); + } + + /// + /// Удаление коллекции + /// + /// + /// + private void ButtonCollectionDel_Click(object sender, EventArgs e) + { + if(listBoxCollection.SelectedItems == null || listBoxCollection.SelectedIndex < 0) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + RefreshListBoxItems(); + } + + /// + /// Обновление списка в listBoxCollection + /// + private void RefreshListBoxItems() + { + 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 PlaneSharingService(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RefreshListBoxItems(); + } } } \ No newline at end of file -- 2.25.1