From 62e8ffea11d5792551cd71a297e509e753e5e63f Mon Sep 17 00:00:00 2001 From: "leonteva.v" Date: Sat, 18 May 2024 16:34:15 +0400 Subject: [PATCH] 4 lab --- .../CollectionType.cs | 22 +++ .../ListGenericObjects.cs | 60 +++++++ .../StorageCollection.cs | 71 ++++++++ .../FormShipCollection.Designer.cs | 170 +++++++++++++++--- .../ProjectBattleship/FormShipCollection.cs | 101 ++++++++++- 5 files changed, 395 insertions(+), 29 deletions(-) create mode 100644 ProjectBattleship/ProjectBattleship/CollectionGenericObjects/CollectionType.cs create mode 100644 ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 ProjectBattleship/ProjectBattleship/CollectionGenericObjects/StorageCollection.cs diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/CollectionType.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..fba5da9 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,22 @@ +namespace ProjectBattleship.CollectionGenericObjects; + +/// +/// Тип коллекции +/// +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ListGenericObjects.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..1de2e66 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,60 @@ +using System; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectBattleship.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 < 0 || position >= _collection.Count) + return null; + return _collection[position]; + } + public int Insert(T obj) + { + if (_collection.Count + 1 <= _maxCount) + { + _collection.Add(obj); + return _collection.Count - 1; + } + return -1; + } + public bool Insert(T obj, int position) + { + if (_collection.Count + 1 > _maxCount || position < 0 || position >= _collection.Count) + return false; + _collection.Insert(position, obj); + return true; + } + public T? Remove(int position) + { + if (position < 0 || position >= _collection.Count) + return null; + T? temp = _collection[position]; + _collection.RemoveAt(position); + return temp; + } +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/StorageCollection.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..b8377e5 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,71 @@ +using ProjectBattleship.CollectionGenericObjects; + +namespace ProjectSportCar.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 (string.IsNullOrEmpty(name) || _storages.ContainsKey(name)) + return; + switch (collectionType) + { + case CollectionType.List: + _storages.Add(name, new ListGenericObjects()); + break; + case CollectionType.Massive: + _storages.Add(name, new MassiveGenericObjects()); + break; + default: + break; + } + } + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + if (_storages.ContainsKey(name)) + _storages.Remove(name); + } + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObjects? this[string name] + { + get + { + if (_storages.TryGetValue(name, out ICollectionGenericObjects? value)) + return value; + return null; + } + } +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/FormShipCollection.Designer.cs b/ProjectBattleship/ProjectBattleship/FormShipCollection.Designer.cs index e5bacaa..61fff26 100644 --- a/ProjectBattleship/ProjectBattleship/FormShipCollection.Designer.cs +++ b/ProjectBattleship/ProjectBattleship/FormShipCollection.Designer.cs @@ -29,6 +29,15 @@ private void InitializeComponent() { Tools = 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(); buttonGoToTest = new Button(); buttonDelShip = new Button(); @@ -37,18 +46,18 @@ buttonAddShip = new Button(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + panelCompanyTools = new Panel(); Tools.SuspendLayout(); + panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + panelCompanyTools.SuspendLayout(); SuspendLayout(); // // Tools // - Tools.Controls.Add(buttonRefresh); - Tools.Controls.Add(buttonGoToTest); - Tools.Controls.Add(buttonDelShip); - Tools.Controls.Add(maskedTextBox); - Tools.Controls.Add(buttonAddBettleship); - Tools.Controls.Add(buttonAddShip); + Tools.Controls.Add(panelCompanyTools); + Tools.Controls.Add(buttonCreateCompany); + Tools.Controls.Add(panelStorage); Tools.Controls.Add(comboBoxSelectorCompany); Tools.Dock = DockStyle.Right; Tools.Location = new Point(1605, 0); @@ -58,12 +67,104 @@ Tools.TabStop = false; Tools.Text = "Инструменты"; // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(9, 612); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(473, 68); + 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, 35); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(482, 512); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(6, 443); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(473, 46); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 32; + listBoxCollection.Location = new Point(9, 209); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(473, 228); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(6, 157); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(473, 46); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(306, 102); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(125, 36); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(43, 102); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(128, 36); + radioButtonMassive.TabIndex = 2; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(6, 48); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(476, 39); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(128, 13); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(251, 32); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(14, 1134); + buttonRefresh.Location = new Point(16, 432); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(468, 90); + buttonRefresh.Size = new Size(466, 90); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -72,9 +173,9 @@ // buttonGoToTest // buttonGoToTest.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToTest.Location = new Point(6, 768); + buttonGoToTest.Location = new Point(16, 336); buttonGoToTest.Name = "buttonGoToTest"; - buttonGoToTest.Size = new Size(476, 90); + buttonGoToTest.Size = new Size(466, 90); buttonGoToTest.TabIndex = 5; buttonGoToTest.Text = "Передать на тест"; buttonGoToTest.UseVisualStyleBackColor = true; @@ -83,9 +184,9 @@ // buttonDelShip // buttonDelShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonDelShip.Location = new Point(6, 573); + buttonDelShip.Location = new Point(12, 240); buttonDelShip.Name = "buttonDelShip"; - buttonDelShip.Size = new Size(476, 90); + buttonDelShip.Size = new Size(473, 90); buttonDelShip.TabIndex = 4; buttonDelShip.Text = "Удалить корабль"; buttonDelShip.UseVisualStyleBackColor = true; @@ -94,19 +195,19 @@ // maskedTextBox // maskedTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - maskedTextBox.Location = new Point(6, 520); + maskedTextBox.Location = new Point(12, 195); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(476, 39); + maskedTextBox.Size = new Size(470, 39); maskedTextBox.TabIndex = 3; maskedTextBox.ValidatingType = typeof(int); // // buttonAddBettleship // buttonAddBettleship.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddBettleship.Location = new Point(6, 283); + buttonAddBettleship.Location = new Point(9, 99); buttonAddBettleship.Name = "buttonAddBettleship"; - buttonAddBettleship.Size = new Size(476, 90); + buttonAddBettleship.Size = new Size(473, 90); buttonAddBettleship.TabIndex = 2; buttonAddBettleship.Text = "Добавить боевой корабль"; buttonAddBettleship.UseVisualStyleBackColor = true; @@ -115,13 +216,13 @@ // buttonAddShip // buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddShip.Location = new Point(6, 172); + buttonAddShip.Location = new Point(9, 3); buttonAddShip.Name = "buttonAddShip"; - buttonAddShip.Size = new Size(476, 90); + buttonAddShip.Size = new Size(473, 90); buttonAddShip.TabIndex = 1; buttonAddShip.Text = "Добавить корабль"; buttonAddShip.UseVisualStyleBackColor = true; - buttonAddShip.Click += buttonAddShip_Click_1; + buttonAddShip.Click += ButtonAddShip_Click_1; // // comboBoxSelectorCompany // @@ -129,7 +230,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Доки" }); - comboBoxSelectorCompany.Location = new Point(6, 38); + comboBoxSelectorCompany.Location = new Point(9, 566); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(476, 40); comboBoxSelectorCompany.TabIndex = 0; @@ -144,6 +245,20 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddShip); + panelCompanyTools.Controls.Add(buttonAddBettleship); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(buttonGoToTest); + panelCompanyTools.Controls.Add(buttonDelShip); + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(0, 686); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(488, 569); + panelCompanyTools.TabIndex = 8; + // // FormShipCollection // AutoScaleDimensions = new SizeF(13F, 32F); @@ -154,8 +269,11 @@ Name = "FormShipCollection"; Text = "Коллекция кораблей"; Tools.ResumeLayout(false); - Tools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); ResumeLayout(false); } @@ -170,5 +288,15 @@ private PictureBox pictureBox; private Button buttonRefresh; private Button buttonGoToTest; + private Panel panelStorage; + private Label labelCollectionName; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private Button buttonCreateCompany; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/FormShipCollection.cs b/ProjectBattleship/ProjectBattleship/FormShipCollection.cs index 4a68470..55481c3 100644 --- a/ProjectBattleship/ProjectBattleship/FormShipCollection.cs +++ b/ProjectBattleship/ProjectBattleship/FormShipCollection.cs @@ -1,6 +1,7 @@ using ProjectBattleship; using ProjectBattleship.CollectionGenericObjects; using ProjectBattleship.Drawnings; +using ProjectSportCar.CollectionGenericObjects; using System; using System.Collections.Generic; using System.ComponentModel; @@ -15,6 +16,7 @@ namespace Battleship; public partial class FormShipCollection : Form { + private readonly StorageCollection _storageCollection; /// /// Компания /// @@ -26,6 +28,7 @@ public partial class FormShipCollection : Form public FormShipCollection() { InitializeComponent(); + _storageCollection = new(); } /// /// Выбор компании @@ -34,13 +37,7 @@ public partial class FormShipCollection : Form /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Доки": - _company = new ShipDocks(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } - + panelCompanyTools.Enabled = false; } /// /// Создание объекта класса-перемещения @@ -163,5 +160,93 @@ public partial class FormShipCollection : Form } private void ButtonAddBattleship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingBattleship)); - private void buttonAddShip_Click_1(object sender, EventArgs e) => CreateObject(nameof(DrawingShip)); + private void ButtonAddShip_Click_1(object sender, EventArgs e) => CreateObject(nameof(DrawingShip)); + /// + /// Добавление коллекции + /// + /// + /// + 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) + { + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + return; + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + 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 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 ShipDocks(pictureBox.Width, pictureBox.Height, collection); + break; + } + + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } } -- 2.25.1