From 96e1afff66386ebb17561095effef0e7650eba64 Mon Sep 17 00:00:00 2001 From: Vladislave Date: Fri, 12 Apr 2024 12:58:06 +0300 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=E2=84=964?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionGenericObject/CollectionType.cs | 24 ++ .../ListGenericObjects.cs | 79 ++++++ .../StorageCollection.cs | 77 ++++++ .../FormPlaneCollection.Designer.cs | 229 ++++++++++++++---- .../ProjectBomber/FormPlaneCollection.cs | 89 ++++++- 5 files changed, 438 insertions(+), 60 deletions(-) create mode 100644 ProjectBomber/ProjectBomber/CollectionGenericObject/CollectionType.cs create mode 100644 ProjectBomber/ProjectBomber/CollectionGenericObject/ListGenericObjects.cs create mode 100644 ProjectBomber/ProjectBomber/CollectionGenericObject/StorageCollection.cs diff --git a/ProjectBomber/ProjectBomber/CollectionGenericObject/CollectionType.cs b/ProjectBomber/ProjectBomber/CollectionGenericObject/CollectionType.cs new file mode 100644 index 0000000..36bca76 --- /dev/null +++ b/ProjectBomber/ProjectBomber/CollectionGenericObject/CollectionType.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirBomber.CollectionGenericObject; + +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + /// + /// Массив + /// + Massive = 1, + /// + /// Список + /// + List = 2 + +} diff --git a/ProjectBomber/ProjectBomber/CollectionGenericObject/ListGenericObjects.cs b/ProjectBomber/ProjectBomber/CollectionGenericObject/ListGenericObjects.cs new file mode 100644 index 0000000..8861abe --- /dev/null +++ b/ProjectBomber/ProjectBomber/CollectionGenericObject/ListGenericObjects.cs @@ -0,0 +1,79 @@ +using ProjectAirBomber.CollectionGenericObject; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirBomber.CollectionGenericObject; + +public class ListGenericObjects : ICollectionGenericObject + 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 >= Count || position < 0) return null; + return _collection[position]; + } + + public int Insert(T obj) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO вставка в конец набора + if (Count == _maxCount) return -1; + _collection.Add(obj); + return Count; + } + + public int Insert(T obj, int position) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO проверка позиции + // TODO вставка по позиции + if (Count == _maxCount) return -1; + if (position >= Count || position < 0) return -1; + _collection.Insert(position, obj); + return position; + + } + + public T Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из списка + if (position >= Count || position < 0) return null; + T obj = _collection[position]; + _collection.RemoveAt(position); + return obj; + + + } + + bool? ICollectionGenericObject.Insert(T obj, int position) + { + throw new NotImplementedException(); + } +} diff --git a/ProjectBomber/ProjectBomber/CollectionGenericObject/StorageCollection.cs b/ProjectBomber/ProjectBomber/CollectionGenericObject/StorageCollection.cs new file mode 100644 index 0000000..6832803 --- /dev/null +++ b/ProjectBomber/ProjectBomber/CollectionGenericObject/StorageCollection.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAirBomber.CollectionGenericObject; +/// +/// Класс-хранилище коллекций +/// +/// +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 (_storages.ContainsKey(name)) return; + + if (collectionType == CollectionType.None) return; + else if (collectionType == CollectionType.Massive) + _storages[name] = new MassiveGenericObject(); + else if (collectionType == CollectionType.List) + _storages[name] = new ListGenericObjects(); + } + + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + // TODO Прописать логику для удаления коллекции + if (_storages.ContainsKey(name)) + _storages.Remove(name); + } + + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObject? this[string name] + { + get + { + // TODO Продумать логику получения объекта + if (_storages.ContainsKey(name)) + return _storages[name]; + return null; + } + } +} \ No newline at end of file diff --git a/ProjectBomber/ProjectBomber/FormPlaneCollection.Designer.cs b/ProjectBomber/ProjectBomber/FormPlaneCollection.Designer.cs index 77fbedf..11c27f5 100644 --- a/ProjectBomber/ProjectBomber/FormPlaneCollection.Designer.cs +++ b/ProjectBomber/ProjectBomber/FormPlaneCollection.Designer.cs @@ -29,50 +29,103 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + panelCompanyTools = new Panel(); + maskedTextBox = new MaskedTextBox(); + buttonAddPlane = new Button(); buttonRefresh = new Button(); + buttonAddAirBomber = new Button(); buttonGoToCheck = new Button(); buttonDelPlane = new Button(); - maskedTextBox = new MaskedTextBox(); - buttonAddAirBomber = new Button(); - buttonAddPlane = new Button(); comboBoxSelectionCompany = new ComboBox(); + buttonCreateCompany = new Button(); + panelStorage = new Panel(); + buttonCollectionDel = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + radioButtonMassive = new RadioButton(); + labelCollectionName = new Label(); + textBoxCollectionName = new TextBox(); 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(buttonDelPlane); - groupBoxTools.Controls.Add(maskedTextBox); - groupBoxTools.Controls.Add(buttonAddAirBomber); - groupBoxTools.Controls.Add(buttonAddPlane); + groupBoxTools.Controls.Add(panelCompanyTools); groupBoxTools.Controls.Add(comboBoxSelectionCompany); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(1035, 0); + groupBoxTools.Location = new Point(1152, 0); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(250, 667); + groupBoxTools.Size = new Size(250, 713); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(buttonAddPlane); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(buttonAddAirBomber); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(buttonDelPlane); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 437); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(244, 273); + panelCompanyTools.TabIndex = 9; + // + // maskedTextBox + // + maskedTextBox.Location = new Point(9, 56); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(229, 27); + maskedTextBox.TabIndex = 4; + maskedTextBox.ValidatingType = typeof(int); + // + // buttonAddPlane + // + buttonAddPlane.Location = new Point(6, 3); + buttonAddPlane.Name = "buttonAddPlane"; + buttonAddPlane.Size = new Size(235, 47); + buttonAddPlane.TabIndex = 1; + buttonAddPlane.Text = "Добавление самолетов"; + buttonAddPlane.UseVisualStyleBackColor = true; + buttonAddPlane.Click += ButtonAddPlane_Click; + // // buttonRefresh // - buttonRefresh.Location = new Point(6, 291); + buttonRefresh.Location = new Point(6, 169); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(232, 32); + buttonRefresh.Size = new Size(229, 32); buttonRefresh.TabIndex = 7; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.Click += ButtonRefresh_Click; // + // buttonAddAirBomber + // + buttonAddAirBomber.Location = new Point(3, 207); + buttonAddAirBomber.Name = "buttonAddAirBomber"; + buttonAddAirBomber.Size = new Size(235, 59); + buttonAddAirBomber.TabIndex = 2; + buttonAddAirBomber.Text = "Добавление бомбардировщика"; + buttonAddAirBomber.UseVisualStyleBackColor = true; + buttonAddAirBomber.Click += ButtonAddAirBomber_Click; + // // buttonGoToCheck // - buttonGoToCheck.Location = new Point(6, 253); + buttonGoToCheck.Location = new Point(9, 131); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(232, 32); + buttonGoToCheck.Size = new Size(229, 32); buttonGoToCheck.TabIndex = 6; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -80,7 +133,7 @@ // // buttonDelPlane // - buttonDelPlane.Location = new Point(9, 211); + buttonDelPlane.Location = new Point(6, 89); buttonDelPlane.Name = "buttonDelPlane"; buttonDelPlane.Size = new Size(232, 36); buttonDelPlane.TabIndex = 5; @@ -88,54 +141,115 @@ buttonDelPlane.UseVisualStyleBackColor = true; buttonDelPlane.Click += ButtonDelPlane_Click; // - // maskedTextBox - // - maskedTextBox.Location = new Point(9, 178); - maskedTextBox.Mask = "00"; - maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(229, 27); - maskedTextBox.TabIndex = 4; - maskedTextBox.ValidatingType = typeof(int); - // - // buttonAddAirBomber - // - buttonAddAirBomber.Location = new Point(9, 113); - buttonAddAirBomber.Name = "buttonAddAirBomber"; - buttonAddAirBomber.Size = new Size(232, 59); - buttonAddAirBomber.TabIndex = 2; - buttonAddAirBomber.Text = "Добавление бомбардировщика"; - buttonAddAirBomber.UseVisualStyleBackColor = true; - buttonAddAirBomber.Click += ButtonAddAirBomber_Click; - // - // buttonAddPlane - // - buttonAddPlane.Location = new Point(9, 60); - buttonAddPlane.Name = "buttonAddPlane"; - buttonAddPlane.Size = new Size(232, 47); - buttonAddPlane.TabIndex = 1; - buttonAddPlane.Text = "Добавление самолетов"; - buttonAddPlane.UseVisualStyleBackColor = true; - buttonAddPlane.Click += ButtonAddPlane_Click; - // // comboBoxSelectionCompany // comboBoxSelectionCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; comboBoxSelectionCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectionCompany.FormattingEnabled = true; comboBoxSelectionCompany.Items.AddRange(new object[] { "Ангар" }); - comboBoxSelectionCompany.Location = new Point(6, 26); + comboBoxSelectionCompany.Location = new Point(12, 403); comboBoxSelectionCompany.Name = "comboBoxSelectionCompany"; comboBoxSelectionCompany.Size = new Size(232, 28); comboBoxSelectionCompany.TabIndex = 0; - comboBoxSelectionCompany.SelectedIndexChanged += ComboBoxSelectionCompany_SelectedIndexChanged; - comboBoxSelectionCompany.Click += ComboBoxSelectionCompany_SelectedIndexChanged; + // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(6, 358); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(232, 29); + buttonCreateCompany.TabIndex = 6; + 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(labelCollectionName); + panelStorage.Controls.Add(textBoxCollectionName); + panelStorage.Dock = DockStyle.Top; + panelStorage.Location = new Point(3, 23); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(244, 293); + panelStorage.TabIndex = 8; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(6, 262); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(226, 29); + buttonCollectionDel.TabIndex = 5; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 20; + listBoxCollection.Location = new Point(9, 132); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(229, 124); + listBoxCollection.TabIndex = 4; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(9, 97); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(226, 29); + buttonCollectionAdd.TabIndex = 3; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(155, 67); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(80, 24); + radioButtonList.TabIndex = 2; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(9, 67); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(82, 24); + radioButtonMassive.TabIndex = 1; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(41, 11); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(155, 20); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции"; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(9, 34); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(226, 27); + textBoxCollectionName.TabIndex = 0; // // pictureBox // pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 0); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(1035, 667); + pictureBox.Size = new Size(1152, 713); pictureBox.TabIndex = 3; pictureBox.TabStop = false; // @@ -143,13 +257,16 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1285, 667); + ClientSize = new Size(1402, 713); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Name = "FormPlaneCollection"; Text = "Коллекция Самолетов"; groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); ResumeLayout(false); } @@ -165,5 +282,15 @@ private MaskedTextBox maskedTextBox; private Button buttonRefresh; private Button buttonGoToCheck; + private Panel panelStorage; + private Label labelCollectionName; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private Button buttonCreateCompany; + private Button buttonCollectionDel; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/ProjectBomber/ProjectBomber/FormPlaneCollection.cs b/ProjectBomber/ProjectBomber/FormPlaneCollection.cs index a045197..4125e8f 100644 --- a/ProjectBomber/ProjectBomber/FormPlaneCollection.cs +++ b/ProjectBomber/ProjectBomber/FormPlaneCollection.cs @@ -16,6 +16,8 @@ namespace ProjectAirBomber { public partial class FormPlaneCollection : Form { + private readonly StorageCollection _storageCollection; + /// /// Стратегия перемещения /// @@ -24,18 +26,13 @@ namespace ProjectAirBomber public FormPlaneCollection() { InitializeComponent(); + _storageCollection = new(); } - private void ComboBoxSelectionCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectionCompany.Text) - { - case "Ангар": - _company = new PlaneHangar(pictureBox.Width, - pictureBox.Height, new MassiveGenericObject()); - break; - } + panelCompanyTools.Enabled = false; } + private void CreateObject(string type) { @@ -62,7 +59,7 @@ namespace ProjectAirBomber return; } - if (_company + drawningPlane!= -1) + if (_company + drawningPlane != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); @@ -158,6 +155,80 @@ namespace ProjectAirBomber } pictureBox.Image = _company.Show(); } + + 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 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.SelectedIndex < 0 || listBoxCollection.SelectedItems == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + RefreshListBoxItems(); + } + private void ButtonCreateCompany_Click(object sender, EventArgs e) + { + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + + ICollectionGenericObject? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; + if (collection == null) + { + MessageBox.Show("Коллекция не проинициализирована"); + return; + } + + switch (comboBoxSelectionCompany.Text) + { + case "Ангар": + _company = new PlaneHangar(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RefreshListBoxItems(); + } } }