From 6f1fb9ec3b0070c39b1c08c0aa52812a98d2a17f Mon Sep 17 00:00:00 2001 From: egorvasin Date: Fri, 24 May 2024 19:31:21 +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 +++ .../ICollectionGenericObjects.cs | 2 +- .../ListGenericObjects.cs | 61 ++++++++ .../StorageCollection.cs | 71 +++++++++ .../FormBuldozerCollection.Designer.cs | 137 ++++++++++++++++-- .../ProjectBuldozer/FormBuldozerCollection.cs | 80 +++++++++- 6 files changed, 358 insertions(+), 13 deletions(-) create mode 100644 ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/CollectionType.cs create mode 100644 ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/StorageCollection.cs diff --git a/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/CollectionType.cs b/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..a62ca31 --- /dev/null +++ b/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,20 @@ +namespace ProjectBuldozer.CollectionGenericObjects; + +/// +/// Тип коллекции +/// +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + /// + /// Массив + /// + Massive = 1, + /// + /// Список + /// + List = 2 +} diff --git a/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/ICollectionGenericObjects.cs index 175a2e2..a6109cc 100644 --- a/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -37,7 +37,7 @@ public interface ICollectionGenericObjects /// /// Позиция /// true - удаление прошло удачно, false - удаление не удалось - T Remove(int position); + T? Remove(int position); /// /// Получение объекта по позиции diff --git a/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/ListGenericObjects.cs b/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..20fe6b7 --- /dev/null +++ b/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,61 @@ +namespace ProjectBuldozer.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) + { + _collection.Add(obj); + return Count; + } + return -1; + } + + public int Insert(T obj, int position) + { + if (position < 0 || position >= Count) return -1; + if (Count == _maxCount) return -1; + _collection.Add(obj); + return position; + } + + public T? Remove(int position) + { + if (position < 0 || position >= Count) return null; + T removeObj = _collection[position]; + _collection.RemoveAt(position); + return removeObj; + } +} diff --git a/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/StorageCollection.cs b/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..d4ba1a7 --- /dev/null +++ b/ProjectBuldozer/ProjectBuldozer/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,71 @@ +namespace ProjectBuldozer.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/ProjectBuldozer/ProjectBuldozer/FormBuldozerCollection.Designer.cs b/ProjectBuldozer/ProjectBuldozer/FormBuldozerCollection.Designer.cs index 92e15ab..a8c9069 100644 --- a/ProjectBuldozer/ProjectBuldozer/FormBuldozerCollection.Designer.cs +++ b/ProjectBuldozer/ProjectBuldozer/FormBuldozerCollection.Designer.cs @@ -37,12 +37,24 @@ buttonAddUsualBuldozer = new Button(); comboBoxSelectCompany = new ComboBox(); pictureBox = new PictureBox(); + panelStorage = new Panel(); + labelCollectionName = new Label(); + textBoxCollectionName = new TextBox(); + radioButtonMassive = new RadioButton(); + radioButtonList = new RadioButton(); + buttonCollectionAdd = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionDel = new Button(); + buttonCreateCompany = new Button(); groupBoxTools.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + panelStorage.SuspendLayout(); SuspendLayout(); // // groupBoxTools // + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(buttonRefresh); groupBoxTools.Controls.Add(buttonGoToCheck); groupBoxTools.Controls.Add(buttonRemoveBuldozer); @@ -53,7 +65,7 @@ groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(705, 0); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(220, 450); + groupBoxTools.Size = new Size(220, 611); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -61,7 +73,7 @@ // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(6, 371); + buttonRefresh.Location = new Point(6, 570); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(208, 39); buttonRefresh.TabIndex = 6; @@ -72,7 +84,7 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(6, 257); + buttonGoToCheck.Location = new Point(6, 528); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(208, 39); buttonGoToCheck.TabIndex = 5; @@ -83,7 +95,7 @@ // buttonRemoveBuldozer // buttonRemoveBuldozer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveBuldozer.Location = new Point(6, 212); + buttonRemoveBuldozer.Location = new Point(6, 483); buttonRemoveBuldozer.Name = "buttonRemoveBuldozer"; buttonRemoveBuldozer.Size = new Size(208, 39); buttonRemoveBuldozer.TabIndex = 4; @@ -93,7 +105,7 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(6, 183); + maskedTextBox.Location = new Point(6, 454); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; maskedTextBox.Size = new Size(208, 23); @@ -103,7 +115,7 @@ // buttonAddBuldozer // buttonAddBuldozer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddBuldozer.Location = new Point(6, 128); + buttonAddBuldozer.Location = new Point(6, 399); buttonAddBuldozer.Name = "buttonAddBuldozer"; buttonAddBuldozer.Size = new Size(208, 39); buttonAddBuldozer.TabIndex = 2; @@ -114,7 +126,7 @@ // buttonAddUsualBuldozer // buttonAddUsualBuldozer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddUsualBuldozer.Location = new Point(6, 83); + buttonAddUsualBuldozer.Location = new Point(6, 354); buttonAddUsualBuldozer.Name = "buttonAddUsualBuldozer"; buttonAddUsualBuldozer.Size = new Size(208, 39); buttonAddUsualBuldozer.TabIndex = 1; @@ -128,7 +140,7 @@ comboBoxSelectCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectCompany.FormattingEnabled = true; comboBoxSelectCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectCompany.Location = new Point(6, 22); + comboBoxSelectCompany.Location = new Point(6, 281); comboBoxSelectCompany.Name = "comboBoxSelectCompany"; comboBoxSelectCompany.Size = new Size(208, 23); comboBoxSelectCompany.TabIndex = 0; @@ -139,16 +151,108 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 0); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(705, 450); + pictureBox.Size = new Size(705, 611); pictureBox.TabIndex = 1; pictureBox.TabStop = false; - pictureBox.Click += pictureBox_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(214, 256); + panelStorage.TabIndex = 7; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(52, 12); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(122, 15); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции"; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 39); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(208, 23); + textBoxCollectionName.TabIndex = 1; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(12, 68); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(67, 19); + radioButtonMassive.TabIndex = 2; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(111, 68); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(66, 19); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + radioButtonList.CheckedChanged += radioButton_CheckedChanged; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 98); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(208, 23); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавление коллекции"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 15; + listBoxCollection.Location = new Point(3, 127); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(208, 94); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(3, 227); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(208, 23); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += buttonCollectionDel_Click; + // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(6, 310); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(208, 23); + buttonCreateCompany.TabIndex = 7; + buttonCreateCompany.Text = "Создать компанию"; + buttonCreateCompany.UseVisualStyleBackColor = true; + buttonCreateCompany.Click += buttonCreateCompany_Click; // // FormBuldozerCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(925, 450); + ClientSize = new Size(925, 611); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Name = "FormBuldozerCollection"; @@ -156,6 +260,8 @@ groupBoxTools.ResumeLayout(false); groupBoxTools.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ResumeLayout(false); } @@ -170,5 +276,14 @@ private PictureBox pictureBox; private Button buttonRefresh; private Button buttonGoToCheck; + private Panel panelStorage; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private Button buttonCreateCompany; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; } } \ No newline at end of file diff --git a/ProjectBuldozer/ProjectBuldozer/FormBuldozerCollection.cs b/ProjectBuldozer/ProjectBuldozer/FormBuldozerCollection.cs index 10e4166..8f8abe6 100644 --- a/ProjectBuldozer/ProjectBuldozer/FormBuldozerCollection.cs +++ b/ProjectBuldozer/ProjectBuldozer/FormBuldozerCollection.cs @@ -5,6 +5,8 @@ namespace ProjectBuldozer; public partial class FormBuldozerCollection : Form { + + private readonly StorageCollection _storageCollection; /// /// компания /// @@ -15,6 +17,7 @@ public partial class FormBuldozerCollection : Form public FormBuldozerCollection() { InitializeComponent(); + _storageCollection = new(); } /// /// Выбор компании @@ -84,6 +87,19 @@ public partial class FormBuldozerCollection : Form } } + 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); + } + } + } + /// /// Получение цвета /// @@ -173,11 +189,73 @@ public partial class FormBuldozerCollection : Form pictureBox.Image = _company.Show(); } - private void pictureBox_Click(object sender, EventArgs e) + private void radioButton_CheckedChanged(object sender, EventArgs e) { } + 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 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 (comboBoxSelectCompany.Text) + { + case "Хранилище": + _company = new BuldozerSharingServise(pictureBox.Width, pictureBox.Height, collection); + break; + } + panelStorage.Enabled = true; + 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(); + } + //private void buttonAddUsualBuldozer_Click_1(object sender, EventArgs e) //{