From 7719d789d908e1db09778ac5b9d7db7bc1e32b20 Mon Sep 17 00:00:00 2001 From: Anitonchik Date: Tue, 2 Apr 2024 09:21:14 +0400 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=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=964?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 1 + .../CollectionType.cs | 27 +++ .../ListGenericObjects.cs | 96 ++++++++ .../StorageCollection.cs | 91 ++++++++ .../FormTruckCollection.Designer.cs | 210 ++++++++++++++---- .../ProjectDumpTruck/FormTruckCollection.cs | 105 ++++++++- .../ProjectDumpTruck/FormTruckCollection.resx | 3 + 7 files changed, 484 insertions(+), 49 deletions(-) create mode 100644 ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/CollectionType.cs create mode 100644 ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ListGenericObjects.cs create mode 100644 ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/StorageCollection.cs diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/AbstractCompany.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/AbstractCompany.cs index 1035242..6b52aba 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/AbstractCompany.cs @@ -74,6 +74,7 @@ public abstract class AbstractCompany return company._collection.Remove(position); } + /// /// Получение случайного объекта из коллекции /// diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/CollectionType.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..d3c9836 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.CollectionGenericObjects; + +/// +/// Тип коллекции +/// +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + /// + /// Массив + /// + Massive = 1, + /// + /// Список + /// + List = 2 +} + diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ListGenericObjects.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..0dad494 --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.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 && _collection[position] != null) + return _collection[position]; + return null; + } + public int Insert(T obj) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO вставка в конец набора + if (_collection.Count > _maxCount) + { + return -1; + } + _collection.Add(obj); + return _collection.Count; + } + public int Insert(T obj, int position) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO проверка позиции + // TODO вставка по позиции + if (_collection.Count > _maxCount || position < 0 || position > _maxCount) + { + return -1; + } + + if (_collection[position] == null) + { + _collection.Insert(position, obj); + return position; + } + for (int i = position + 1; i < _maxCount; ++i) + { + if (_collection[i] == null) + { + _collection.Insert(position, obj); + return position; + } + } + for (int i = 0; i < position; ++i) + { + if (_collection[i] == null) + { + _collection.Insert(position, obj); + return position; + } + } + return -1; + } + public T Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из списка + if (position < 0 || position > _maxCount) + { + return null; + } + T temp = _collection[position]; + _collection.RemoveAt(position); + return temp; + } +} + diff --git a/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/StorageCollection.cs b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..f3cd33a --- /dev/null +++ b/ProjectDumpTruck/ProjectDumpTruck/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectDumpTruck.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) + { + // TODO проверка, что name не пустой и нет в словаре записи с таким ключом + // TODO Прописать логику для добавления + + if (_storages.ContainsKey(name)) + { + return; + } + switch (collectionType) + { + case CollectionType.None: + return; + case CollectionType.Massive: + _storages.Add(name, new MassiveGenericObjects()); + break; + case CollectionType.List: + _storages.Add(name, new ListGenericObjects()); + break; + } + } + + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + // TODO Прописать логику для удаления коллекции + /*if (name == null) + return;*/ + if ( _storages.ContainsKey(name)) + { + _storages.Remove(name); + } + + } + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObjects? this[string name] + { + get + { + // TODO Продумать логику получения объекта + if (_storages.ContainsKey(name)) + { + return _storages[name]; + } + return null; + } + } +} diff --git a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs index b9648e0..f29301a 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.Designer.cs @@ -29,38 +29,82 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + panelCompanyTools = new Panel(); + buttonAddTruck = new Button(); + buttonAddDumpTruck = new Button(); buttonRefresh = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); buttonGoToChek = new Button(); buttonRemoveTruck = new Button(); - maskedTextBoxPosition = new MaskedTextBox(); - buttonAddDumpTruck = new Button(); - buttonAddTruck = new Button(); + 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(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); groupBoxTools.SuspendLayout(); + panelCompanyTools.SuspendLayout(); + panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); SuspendLayout(); // // groupBoxTools // - groupBoxTools.Controls.Add(buttonRefresh); - groupBoxTools.Controls.Add(buttonGoToChek); - groupBoxTools.Controls.Add(buttonRemoveTruck); - groupBoxTools.Controls.Add(maskedTextBoxPosition); - groupBoxTools.Controls.Add(buttonAddDumpTruck); - groupBoxTools.Controls.Add(buttonAddTruck); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(832, 0); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(250, 753); + groupBoxTools.Size = new Size(250, 827); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddTruck); + panelCompanyTools.Controls.Add(buttonAddDumpTruck); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(maskedTextBoxPosition); + panelCompanyTools.Controls.Add(buttonGoToChek); + panelCompanyTools.Controls.Add(buttonRemoveTruck); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Location = new Point(3, 450); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(244, 374); + panelCompanyTools.TabIndex = 4; + // + // buttonAddTruck + // + buttonAddTruck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddTruck.Location = new Point(15, 13); + buttonAddTruck.Name = "buttonAddTruck"; + buttonAddTruck.Size = new Size(220, 57); + buttonAddTruck.TabIndex = 1; + buttonAddTruck.Text = "Добавление грузовика"; + buttonAddTruck.UseVisualStyleBackColor = true; + buttonAddTruck.Click += ButtonAddTruck_Click; + // + // buttonAddDumpTruck + // + buttonAddDumpTruck.Location = new Point(15, 76); + buttonAddDumpTruck.Name = "buttonAddDumpTruck"; + buttonAddDumpTruck.Size = new Size(220, 57); + buttonAddDumpTruck.TabIndex = 2; + buttonAddDumpTruck.Text = "Добавление самосвала"; + buttonAddDumpTruck.UseVisualStyleBackColor = true; + buttonAddDumpTruck.Click += ButtonAddDumpTruck_Click; + // // buttonRefresh // - buttonRefresh.Location = new Point(18, 507); + buttonRefresh.Location = new Point(15, 300); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(220, 57); buttonRefresh.TabIndex = 7; @@ -68,9 +112,18 @@ buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.Click += ButtonRefresh_Click; // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(15, 139); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(223, 27); + maskedTextBoxPosition.TabIndex = 4; + maskedTextBoxPosition.ValidatingType = typeof(int); + // // buttonGoToChek // - buttonGoToChek.Location = new Point(18, 406); + buttonGoToChek.Location = new Point(15, 237); buttonGoToChek.Name = "buttonGoToChek"; buttonGoToChek.Size = new Size(220, 57); buttonGoToChek.TabIndex = 6; @@ -80,7 +133,7 @@ // // buttonRemoveTruck // - buttonRemoveTruck.Location = new Point(18, 309); + buttonRemoveTruck.Location = new Point(15, 174); buttonRemoveTruck.Name = "buttonRemoveTruck"; buttonRemoveTruck.Size = new Size(220, 57); buttonRemoveTruck.TabIndex = 5; @@ -88,35 +141,97 @@ buttonRemoveTruck.UseVisualStyleBackColor = true; buttonRemoveTruck.Click += ButtonRemoveTruck_Click; // - // maskedTextBoxPosition + // buttonCreateCompany // - maskedTextBoxPosition.Location = new Point(18, 276); - maskedTextBoxPosition.Mask = "00"; - maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - maskedTextBoxPosition.Size = new Size(226, 27); - maskedTextBoxPosition.TabIndex = 4; - maskedTextBoxPosition.ValidatingType = typeof(int); + buttonCreateCompany.Location = new Point(18, 406); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(220, 29); + buttonCreateCompany.TabIndex = 9; + buttonCreateCompany.Text = "Создать компанию"; + buttonCreateCompany.UseVisualStyleBackColor = true; + buttonCreateCompany.Click += ButtonCreateCompany_Click; // - // buttonAddDumpTruck + // panelStorage // - buttonAddDumpTruck.Location = new Point(18, 173); - buttonAddDumpTruck.Name = "buttonAddDumpTruck"; - buttonAddDumpTruck.Size = new Size(220, 57); - buttonAddDumpTruck.TabIndex = 2; - buttonAddDumpTruck.Text = "Добавление самосвала"; - buttonAddDumpTruck.UseVisualStyleBackColor = true; - buttonAddDumpTruck.Click += ButtonAddDumpTruck_Click; + 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(244, 310); + panelStorage.TabIndex = 8; // - // buttonAddTruck + // buttonCollectionDel // - buttonAddTruck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddTruck.Location = new Point(18, 110); - buttonAddTruck.Name = "buttonAddTruck"; - buttonAddTruck.Size = new Size(220, 57); - buttonAddTruck.TabIndex = 1; - buttonAddTruck.Text = "Добавление грузовика"; - buttonAddTruck.UseVisualStyleBackColor = true; - buttonAddTruck.Click += ButtonAddTruck_Click; + buttonCollectionDel.Location = new Point(3, 265); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(238, 29); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 20; + listBoxCollection.Location = new Point(3, 135); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(238, 124); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 100); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(238, 29); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(142, 70); + 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(15, 70); + 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, 37); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(238, 27); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(38, 14); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(155, 20); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции"; // // comboBoxSelectorCompany // @@ -124,7 +239,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(18, 26); + comboBoxSelectorCompany.Location = new Point(18, 353); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(220, 28); comboBoxSelectorCompany.TabIndex = 0; @@ -135,7 +250,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 0); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(832, 753); + pictureBox.Size = new Size(832, 827); pictureBox.TabIndex = 3; pictureBox.TabStop = false; // @@ -143,13 +258,16 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1082, 753); + ClientSize = new Size(1082, 827); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Name = "FormTruckCollection"; 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 +283,15 @@ private PictureBox pictureBox; private Button buttonRefresh; private Button buttonGoToChek; + private Panel panelStorage; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private Button buttonCollectionAdd; + private Button buttonCreateCompany; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs index cbe8b57..954e18f 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs +++ b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.cs @@ -14,6 +14,10 @@ namespace ProjectDumpTruck; public partial class FormTruckCollection : Form { + /// + /// Хранилише коллекций + /// + private readonly StorageCollection _storageCollection; /// /// Компания @@ -23,22 +27,17 @@ public partial class FormTruckCollection : Form public FormTruckCollection() { InitializeComponent(); + _storageCollection = new(); } /// /// Выбор компании - /// + /// /// /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new TruckSharingService(pictureBox.Width, - pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = true; } @@ -181,5 +180,95 @@ public partial class FormTruckCollection : Form } pictureBox.Image = _company.Show(); } + + /// + /// Добавление коллекции + /// + /// + /// Обновление списка в 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) + { + // TODO прописать логику удаления элемента из коллекции + // нужно убедиться, что есть выбранная коллекция + // спросить у пользователя через MessageBox, что он подтверждает, что хочет удалить запись + // удалить и обновить ListBox + if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItems == 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 TruckSharingService(pictureBox.Width, + pictureBox.Height, collection); + break; + } + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } } diff --git a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.resx b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.resx index af32865..139c828 100644 --- a/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.resx +++ b/ProjectDumpTruck/ProjectDumpTruck/FormTruckCollection.resx @@ -117,4 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + \ No newline at end of file -- 2.25.1