From 2fa4300a97e977ab431f75859220ff3c6da818b4 Mon Sep 17 00:00:00 2001 From: Evgehil Date: Sun, 17 Mar 2024 19:24:38 +0400 Subject: [PATCH] =?UTF-8?q?4=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=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 | 22 +++ .../ListGenericObjects.cs | 61 +++++++ .../MassiveGenericObjects.cs | 16 +- .../StorageCollection.cs | 68 +++++++ .../FormAirplanCollection.Designer.cs | 166 ++++++++++++++++-- .../Project_airbus/FormAirplanCollection.cs | 108 +++++++++++- 6 files changed, 402 insertions(+), 39 deletions(-) create mode 100644 Project_airbus/Project_airbus/CollectionGenericObjects/CollectionType.cs create mode 100644 Project_airbus/Project_airbus/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 Project_airbus/Project_airbus/CollectionGenericObjects/StorageCollection.cs diff --git a/Project_airbus/Project_airbus/CollectionGenericObjects/CollectionType.cs b/Project_airbus/Project_airbus/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..43531c3 --- /dev/null +++ b/Project_airbus/Project_airbus/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,22 @@ +namespace Project_airbus.CollectionGenericObjects; + + /// + /// Тип коллекции + /// +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 +} \ No newline at end of file diff --git a/Project_airbus/Project_airbus/CollectionGenericObjects/ListGenericObjects.cs b/Project_airbus/Project_airbus/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..06f3670 --- /dev/null +++ b/Project_airbus/Project_airbus/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,61 @@ +namespace Project_airbus.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 == _maxCount) return -1; + _collection.Add(obj); + return Count; + } + + public int Insert(T obj, int position) + { + if (Count == _maxCount) return -1; + if (position >= Count || position < 0) return -1; + _collection.Insert(position, obj); + return position; + } + + public T Remove(int position) + { + if (position >= _collection.Count || position < 0) return null; + T obj = _collection[position]; + _collection.RemoveAt(position); + return obj; + } + +} diff --git a/Project_airbus/Project_airbus/CollectionGenericObjects/MassiveGenericObjects.cs b/Project_airbus/Project_airbus/CollectionGenericObjects/MassiveGenericObjects.cs index 0d04590..7cef140 100644 --- a/Project_airbus/Project_airbus/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/Project_airbus/Project_airbus/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Project_airbus.CollectionGenericObjects; +namespace Project_airbus.CollectionGenericObjects; /// /// Параметризованный набор объектов @@ -48,14 +42,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Get(int position) { - // TODO проверка позиции if (position >= _collection.Length || position < 0) return null; return _collection[position]; } public int Insert(T obj) { - // TODO вставка в свободное место набора int index = 0; while (index < _collection.Length) { @@ -72,12 +64,8 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Insert(T obj, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // ищется свободное место после этой позиции и идет вставка туда - // если нет после, ищем до - // TODO вставка if (position >= _collection.Length || position < 0) return -1; + if (_collection[position] == null) { _collection[position] = obj; diff --git a/Project_airbus/Project_airbus/CollectionGenericObjects/StorageCollection.cs b/Project_airbus/Project_airbus/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..fc492e3 --- /dev/null +++ b/Project_airbus/Project_airbus/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,68 @@ +namespace Project_airbus.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 (_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) + { + if (_storages.ContainsKey(name)) + _storages.Remove(name); + } + + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObjects? this[string name] + { + get + { + if (_storages.ContainsKey(name)) + return _storages[name]; + return null; + } + } +} diff --git a/Project_airbus/Project_airbus/FormAirplanCollection.Designer.cs b/Project_airbus/Project_airbus/FormAirplanCollection.Designer.cs index bf27fec..9b79560 100644 --- a/Project_airbus/Project_airbus/FormAirplanCollection.Designer.cs +++ b/Project_airbus/Project_airbus/FormAirplanCollection.Designer.cs @@ -29,6 +29,15 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + buttonCreateToCompany = 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(); ButtonDelAirplan = new Button(); @@ -37,30 +46,122 @@ buttonAddAirplan = 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(ButtonDelAirplan); - groupBoxTools.Controls.Add(maskedTextBox); - groupBoxTools.Controls.Add(buttonAddAirbus); - groupBoxTools.Controls.Add(buttonAddAirplan); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateToCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(860, 0); + groupBoxTools.Location = new Point(925, 0); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(210, 617); + groupBoxTools.Size = new Size(210, 707); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonCreateToCompany + // + buttonCreateToCompany.Location = new Point(6, 368); + buttonCreateToCompany.Name = "buttonCreateToCompany"; + buttonCreateToCompany.Size = new Size(192, 26); + buttonCreateToCompany.TabIndex = 8; + buttonCreateToCompany.Text = "Создать компанию"; + buttonCreateToCompany.UseVisualStyleBackColor = true; + buttonCreateToCompany.Click += buttonCreateToCompany_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(204, 267); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(0, 211); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(207, 26); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += buttonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 20; + listBoxCollection.Location = new Point(3, 141); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(201, 64); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(-3, 109); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(207, 26); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(115, 79); + 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(18, 79); + 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, 36); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(198, 27); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(28, 13); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(155, 20); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции"; + // // buttonRefresh // - buttonRefresh.Location = new Point(6, 495); + buttonRefresh.Location = new Point(3, 253); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(195, 46); buttonRefresh.TabIndex = 6; @@ -70,7 +171,7 @@ // // buttonGoToCheck // - buttonGoToCheck.Location = new Point(6, 355); + buttonGoToCheck.Location = new Point(3, 201); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(195, 46); buttonGoToCheck.TabIndex = 5; @@ -80,7 +181,7 @@ // // ButtonDelAirplan // - ButtonDelAirplan.Location = new Point(6, 267); + ButtonDelAirplan.Location = new Point(3, 149); ButtonDelAirplan.Name = "ButtonDelAirplan"; ButtonDelAirplan.Size = new Size(195, 46); ButtonDelAirplan.TabIndex = 4; @@ -90,7 +191,7 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(6, 234); + maskedTextBox.Location = new Point(3, 116); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Size = new Size(192, 27); @@ -99,7 +200,7 @@ // // buttonAddAirbus // - buttonAddAirbus.Location = new Point(6, 142); + buttonAddAirbus.Location = new Point(3, 63); buttonAddAirbus.Name = "buttonAddAirbus"; buttonAddAirbus.Size = new Size(192, 47); buttonAddAirbus.TabIndex = 2; @@ -109,7 +210,7 @@ // // buttonAddAirplan // - buttonAddAirplan.Location = new Point(6, 85); + buttonAddAirplan.Location = new Point(3, 6); buttonAddAirplan.Name = "buttonAddAirplan"; buttonAddAirplan.Size = new Size(192, 51); buttonAddAirplan.TabIndex = 1; @@ -123,9 +224,9 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(6, 26); + comboBoxSelectorCompany.Location = new Point(6, 334); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; - comboBoxSelectorCompany.Size = new Size(192, 28); + comboBoxSelectorCompany.Size = new Size(140, 28); comboBoxSelectorCompany.TabIndex = 0; comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; // @@ -134,22 +235,39 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 0); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(860, 617); + pictureBox.Size = new Size(925, 707); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddAirplan); + panelCompanyTools.Controls.Add(buttonAddAirbus); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(ButtonDelAirplan); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 400); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(207, 307); + panelCompanyTools.TabIndex = 9; + // // FormAirplanCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1070, 617); + ClientSize = new Size(1135, 707); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Name = "FormAirplanCollection"; 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 +282,15 @@ private PictureBox pictureBox; private Button buttonRefresh; private Button buttonGoToCheck; + private Panel panelStorage; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private RadioButton radioButtonMassive; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private RadioButton radioButtonList; + private Button buttonCreateToCompany; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/Project_airbus/Project_airbus/FormAirplanCollection.cs b/Project_airbus/Project_airbus/FormAirplanCollection.cs index f8f1650..9f03aec 100644 --- a/Project_airbus/Project_airbus/FormAirplanCollection.cs +++ b/Project_airbus/Project_airbus/FormAirplanCollection.cs @@ -8,6 +8,11 @@ namespace Project_airbus; /// public partial class FormAirplanCollection : Form { + /// + /// Хранилише коллекций + /// + private readonly StorageCollection _storageCollection; + /// /// Компания /// @@ -19,6 +24,7 @@ public partial class FormAirplanCollection : Form public FormAirplanCollection() { InitializeComponent(); + _storageCollection = new(); } /// @@ -28,12 +34,7 @@ public partial class FormAirplanCollection : Form /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new AirplanSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } /// @@ -183,4 +184,99 @@ public partial class FormAirplanCollection : 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(); + + } + + /// + /// Обновление списка в 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 buttonCollectionDel_Click(object sender, EventArgs e) + { + 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 buttonCreateToCompany_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 AirplanSharingService(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } } \ No newline at end of file