From 6dcd4ac6d97f8769d1c2fb1ab743a6cd05fd221c Mon Sep 17 00:00:00 2001 From: cleverman1337 <74911264+cleverman1337@users.noreply.github.com> Date: Thu, 16 May 2024 09:36:27 +0400 Subject: [PATCH] LabWork04 --- .../CollectionType.cs | 25 +++ .../ICollectionGenericObjects.cs | 2 +- .../ListGenericObjects.cs | 62 +++++++ .../MassiveGenericObjects.cs | 39 ++-- .../StorageCollection.cs | 78 ++++++++ .../FormTankCollection.Designer.cs | 167 ++++++++++++++++-- .../FormTankCollection.cs | 91 ++++++++-- 7 files changed, 416 insertions(+), 48 deletions(-) create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/CollectionType.cs create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/CollectionType.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..fc98700 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.CollectionGenericObjects; + +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ICollectionGenericObjects.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ICollectionGenericObjects.cs index 5e6d93b..07bd474 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -36,7 +36,7 @@ public interface ICollectionGenericObjects /// Добавляемый объект /// Позиция /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj, int position); + int Insert(T obj, int position); /// /// Удаление объекта из коллекции с конкретной позиции diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..ed341fd --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.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 >= Count) return null; + return _collection[position]; + } + + public int Insert(T? obj) + { + if (Count == _maxCount) return -1; + _collection.Add(obj); + return Count - 1; + } + + public int Insert(T? obj, int position) + { + if (Count == _maxCount) return -1; + if (position < 0 || position >= Count) return -1; + _collection.Insert(position, obj); + return position; + } + + public T? Remove(int position) + { + if (position < 0 || position >= Count) return null; + T? obj = _collection[position]; + _collection.RemoveAt(position); + return obj; + } +} \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs index d092beb..b4e61f9 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/MassiveGenericObjects.cs @@ -62,32 +62,37 @@ public class MassiveGenericObjects : ICollectionGenericObjects return -1; } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { - if (position < 0 || position >= _collection.Length) - return false; + if (position < 0 || position >= _collection.Length) { return position; } + if (_collection[position] == null) { _collection[position] = obj; - return true; + return position; } - for (int i = position; i < _collection.Length; i++) + else { - if (_collection[i] == null) + for (int i = position + 1; i < _collection.Length; i++) { - _collection[i] = obj; - return true; + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + + for (int i = position - 1; i >= 0; i--) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } } } - for (int i = 0; i < position; i++) - { - if (_collection[i] == null) - { - _collection[i] = obj; - return true; - } - } - return false; + + return -1; } public T? Remove(int position) diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..033b7d2 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.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)) return; + if (_storages.ContainsKey(name)) return; + if (collectionType == CollectionType.None) return; + 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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormTankCollection.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormTankCollection.Designer.cs index ddefb7d..8fd3f7e 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormTankCollection.Designer.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormTankCollection.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(); buttonGoToCheak = new Button(); buttonRemoveTank = new Button(); @@ -37,18 +46,18 @@ buttonAddArtillery = 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(buttonGoToCheak); - groupBoxTools.Controls.Add(buttonRemoveTank); - groupBoxTools.Controls.Add(maskedTextBoxPosition); - groupBoxTools.Controls.Add(buttonAddTank); - groupBoxTools.Controls.Add(buttonAddArtillery); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(715, 0); @@ -58,12 +67,104 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(5, 267); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(189, 24); + 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, 19); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(194, 213); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(5, 186); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(189, 24); + buttonCollectionDel.TabIndex = 7; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 15; + listBoxCollection.Location = new Point(6, 115); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(185, 64); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(2, 81); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(189, 24); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(78, 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(5, 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, 30); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(188, 23); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(25, 12); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(125, 15); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(12, 423); + buttonRefresh.Location = new Point(12, 174); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(176, 41); + buttonRefresh.Size = new Size(170, 23); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -72,9 +173,9 @@ // buttonGoToCheak // buttonGoToCheak.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheak.Location = new Point(12, 337); + buttonGoToCheak.Location = new Point(12, 143); buttonGoToCheak.Name = "buttonGoToCheak"; - buttonGoToCheak.Size = new Size(176, 41); + buttonGoToCheak.Size = new Size(170, 25); buttonGoToCheak.TabIndex = 5; buttonGoToCheak.Text = "Передать на тесты"; buttonGoToCheak.UseVisualStyleBackColor = true; @@ -83,9 +184,9 @@ // buttonRemoveTank // buttonRemoveTank.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveTank.Location = new Point(12, 215); + buttonRemoveTank.Location = new Point(12, 110); buttonRemoveTank.Name = "buttonRemoveTank"; - buttonRemoveTank.Size = new Size(176, 41); + buttonRemoveTank.Size = new Size(170, 27); buttonRemoveTank.TabIndex = 4; buttonRemoveTank.Text = "Удалить танк"; buttonRemoveTank.UseVisualStyleBackColor = true; @@ -93,7 +194,7 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(12, 186); + maskedTextBoxPosition.Location = new Point(12, 82); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(176, 23); @@ -103,9 +204,9 @@ // buttonAddTank // buttonAddTank.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddTank.Location = new Point(12, 82); + buttonAddTank.Location = new Point(12, 9); buttonAddTank.Name = "buttonAddTank"; - buttonAddTank.Size = new Size(176, 41); + buttonAddTank.Size = new Size(170, 30); buttonAddTank.TabIndex = 2; buttonAddTank.Text = "Добавление танка"; buttonAddTank.UseVisualStyleBackColor = true; @@ -114,9 +215,9 @@ // buttonAddArtillery // buttonAddArtillery.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddArtillery.Location = new Point(12, 129); + buttonAddArtillery.Location = new Point(12, 45); buttonAddArtillery.Name = "buttonAddArtillery"; - buttonAddArtillery.Size = new Size(176, 41); + buttonAddArtillery.Size = new Size(170, 31); buttonAddArtillery.TabIndex = 1; buttonAddArtillery.Text = "Добавление сау"; buttonAddArtillery.UseVisualStyleBackColor = true; @@ -128,7 +229,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(12, 33); + comboBoxSelectorCompany.Location = new Point(6, 238); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(182, 23); comboBoxSelectorCompany.TabIndex = 0; @@ -143,6 +244,21 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddTank); + panelCompanyTools.Controls.Add(buttonAddArtillery); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Controls.Add(buttonGoToCheak); + panelCompanyTools.Controls.Add(buttonRemoveTank); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 300); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(194, 205); + panelCompanyTools.TabIndex = 9; + // // FormTankCollection // AutoScaleDimensions = new SizeF(7F, 15F); @@ -153,8 +269,11 @@ Name = "FormTankCollection"; Text = "Коллекция сау"; groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); ResumeLayout(false); } @@ -169,5 +288,15 @@ private Button buttonRemoveTank; private Button buttonRefresh; private Button buttonGoToCheak; + private Panel panelStorage; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private Button buttonCreateCompany; + private Button buttonCollectionDel; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormTankCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormTankCollection.cs index 6af2c20..fbe4577 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormTankCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormTankCollection.cs @@ -11,6 +11,7 @@ public partial class FormTankCollection : Form /// Компания /// private AbstractCompany? _company = null; + private readonly StorageCollection _storageCollection; /// /// Конструктор @@ -18,6 +19,7 @@ public partial class FormTankCollection : Form public FormTankCollection() { InitializeComponent(); + _storageCollection = new(); } /// /// Выбор компании @@ -28,12 +30,7 @@ public partial class FormTankCollection : Form private void comboBoxSelectorCompany_SelectedIndexChanged_1(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new TankSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } private void CreateObject(string type) @@ -51,17 +48,14 @@ public partial class FormTankCollection : Form drawningTank = new DrawningTank(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); break; case nameof(DrawningArtillery): - // TODO вызов диалогового окна для выбора цвета drawningTank = new DrawningArtillery(random.Next(100, 300), random.Next(1000, 3000), - GetColor(random), - GetColor(random), - 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))); break; default: return; } - if (_company + drawningTank !=-1) + if (_company + drawningTank != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); @@ -163,4 +157,79 @@ public partial class FormTankCollection : 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 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.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + 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 TankSharingService(pictureBox.Width, pictureBox.Height, collection); + break; + } + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } } +