diff --git a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/CollectionType.cs b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..8d9ec0a --- /dev/null +++ b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,22 @@ +namespace ProjectContainerShip.CollectionGenericObjects; + +/// +/// Тип коллекции +/// +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + + /// + /// Массив + /// + Massive = 1, + + /// + /// Список + /// + List = 2 +} diff --git a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ListGenericObjects.cs b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..6f92313 --- /dev/null +++ b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,56 @@ +using ProjectContainerShip.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) + { + // 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; + } +} diff --git a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/MassiveGenericObjects.cs index 477b828..7d42884 100644 --- a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectContainerShip.CollectionGenericObjects +namespace ProjectContainerShip.CollectionGenericObjects { public class MassiveGenericObjects : ICollectionGenericObjects where T : class @@ -14,7 +8,24 @@ namespace ProjectContainerShip.CollectionGenericObjects /// private T?[] _collection; public int Count => _collection.Length; - public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } } + + public int SetMaxCount + { + set + { + if (value > 0) + { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } /// /// Конструктор diff --git a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ShipSharingService.cs b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ShipPortService.cs similarity index 100% rename from ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ShipSharingService.cs rename to ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/ShipPortService.cs diff --git a/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/StorageCollection.cs b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..1c97ef8 --- /dev/null +++ b/ProjectContainerShip/ProjectContainerShip/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,78 @@ +namespace ProjectContainerShip.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 (!(collectionType == CollectionType.None) && !_storages.ContainsKey(name)) + { + if (collectionType == CollectionType.List) + { + _storages.Add(name, new ListGenericObjects()); + } + else if (collectionType == CollectionType.Massive) + { + _storages.Add(name, new MassiveGenericObjects()); + } + } + } + + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + // TODO Прописать логику для удаления коллекции + 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/ProjectContainerShip/ProjectContainerShip/FormShipCollection.Designer.cs b/ProjectContainerShip/ProjectContainerShip/FormShipCollection.Designer.cs index 8f0ae53..f71e799 100644 --- a/ProjectContainerShip/ProjectContainerShip/FormShipCollection.Designer.cs +++ b/ProjectContainerShip/ProjectContainerShip/FormShipCollection.Designer.cs @@ -29,6 +29,15 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + buttonCreateCompany = new Button(); + panelStorage = new Panel(); + radioButtonMassive = new RadioButton(); + buttonCollectionDel = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + textBoxCollectionName = new TextBox(); + labelCollectionName = new Label(); buttonAddContainerShip = new Button(); buttonRefresh = new Button(); buttonGoToCheck = new Button(); @@ -37,18 +46,18 @@ buttonAddShip = 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(buttonAddContainerShip); - groupBoxTools.Controls.Add(buttonRefresh); - groupBoxTools.Controls.Add(buttonGoToCheck); - groupBoxTools.Controls.Add(buttonRemoveShip); - groupBoxTools.Controls.Add(maskedTextBox); - groupBoxTools.Controls.Add(buttonAddShip); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; groupBoxTools.ForeColor = Color.Black; @@ -59,12 +68,103 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(6, 546); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(370, 46); + buttonCreateCompany.TabIndex = 9; + buttonCreateCompany.Text = "Создать компанию"; + buttonCreateCompany.UseVisualStyleBackColor = true; + buttonCreateCompany.Click += ButtonCreateCompany_Click; + // + // panelStorage + // + panelStorage.Controls.Add(radioButtonMassive); + panelStorage.Controls.Add(buttonCollectionDel); + panelStorage.Controls.Add(listBoxCollection); + panelStorage.Controls.Add(buttonCollectionAdd); + panelStorage.Controls.Add(radioButtonList); + 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(382, 459); + panelStorage.TabIndex = 8; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(39, 91); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(128, 36); + radioButtonMassive.TabIndex = 7; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(3, 387); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(370, 46); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += ButtonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.Location = new Point(3, 185); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(370, 196); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(3, 133); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(370, 46); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += ButtonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(215, 91); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(125, 36); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 46); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(370, 39); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(69, 11); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(251, 32); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; + // // buttonAddContainerShip // buttonAddContainerShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddContainerShip.Location = new Point(15, 247); + buttonAddContainerShip.Location = new Point(3, 118); buttonAddContainerShip.Name = "buttonAddContainerShip"; - buttonAddContainerShip.Size = new Size(367, 77); + buttonAddContainerShip.Size = new Size(370, 77); buttonAddContainerShip.TabIndex = 7; buttonAddContainerShip.Text = "Добавление контейнеровоза"; buttonAddContainerShip.UseVisualStyleBackColor = true; @@ -73,9 +173,9 @@ // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(15, 887); + buttonRefresh.Location = new Point(3, 412); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(367, 77); + buttonRefresh.Size = new Size(370, 77); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; @@ -84,9 +184,9 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(15, 641); + buttonGoToCheck.Location = new Point(3, 329); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(367, 77); + buttonGoToCheck.Size = new Size(370, 77); buttonGoToCheck.TabIndex = 5; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -95,9 +195,9 @@ // buttonRemoveShip // buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveShip.Location = new Point(15, 473); + buttonRemoveShip.Location = new Point(3, 246); buttonRemoveShip.Name = "buttonRemoveShip"; - buttonRemoveShip.Size = new Size(367, 77); + buttonRemoveShip.Size = new Size(370, 77); buttonRemoveShip.TabIndex = 4; buttonRemoveShip.Text = "Удалить корабль"; buttonRemoveShip.UseVisualStyleBackColor = true; @@ -105,19 +205,19 @@ // // maskedTextBox // - maskedTextBox.Location = new Point(15, 416); + maskedTextBox.Location = new Point(3, 201); maskedTextBox.Mask = "00"; maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(367, 39); + maskedTextBox.Size = new Size(370, 39); maskedTextBox.TabIndex = 3; maskedTextBox.ValidatingType = typeof(int); // // buttonAddShip // buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddShip.Location = new Point(15, 164); + buttonAddShip.Location = new Point(3, 35); buttonAddShip.Name = "buttonAddShip"; - buttonAddShip.Size = new Size(367, 77); + buttonAddShip.Size = new Size(370, 77); buttonAddShip.TabIndex = 1; buttonAddShip.Text = "Добавление корабля"; buttonAddShip.UseVisualStyleBackColor = true; @@ -129,9 +229,9 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(15, 47); + comboBoxSelectorCompany.Location = new Point(6, 500); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; - comboBoxSelectorCompany.Size = new Size(367, 40); + comboBoxSelectorCompany.Size = new Size(370, 40); comboBoxSelectorCompany.TabIndex = 0; comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; // @@ -144,6 +244,21 @@ pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddShip); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(buttonRemoveShip); + panelCompanyTools.Controls.Add(buttonAddContainerShip); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Dock = DockStyle.Bottom; + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 598); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(382, 511); + panelCompanyTools.TabIndex = 10; + // // FormShipCollection // AutoScaleDimensions = new SizeF(13F, 32F); @@ -154,8 +269,11 @@ Name = "FormShipCollection"; Text = "Коллекция кораблей"; groupBoxTools.ResumeLayout(false); - groupBoxTools.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 buttonAddContainerShip; + private Panel panelStorage; + private Label labelCollectionName; + private RadioButton radioButtonList; + private TextBox textBoxCollectionName; + private Button buttonCollectionAdd; + private Button buttonCollectionDel; + private ListBox listBoxCollection; + private Button buttonCreateCompany; + private RadioButton radioButtonMassive; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/ProjectContainerShip/ProjectContainerShip/FormShipCollection.cs b/ProjectContainerShip/ProjectContainerShip/FormShipCollection.cs index 3e51d56..aeeda7b 100644 --- a/ProjectContainerShip/ProjectContainerShip/FormShipCollection.cs +++ b/ProjectContainerShip/ProjectContainerShip/FormShipCollection.cs @@ -7,6 +7,11 @@ namespace ProjectContainerShip; /// public partial class FormShipCollection : Form { + /// + /// Хранилище коллекций + /// + private readonly StorageCollection _storageCollection; + /// /// Компания /// @@ -18,6 +23,7 @@ public partial class FormShipCollection : Form public FormShipCollection() { InitializeComponent(); + _storageCollection = new(); } /// @@ -27,12 +33,7 @@ public partial class FormShipCollection : Form /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new ShipPortService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } /// @@ -135,7 +136,7 @@ public partial class FormShipCollection : Form MessageBox.Show("Не удалось удалить объект"); } } - + /// /// Передача на тесты /// @@ -156,7 +157,7 @@ public partial class FormShipCollection : Form counter--; if (counter <= 0) { - break; + break; } } @@ -165,7 +166,7 @@ public partial class FormShipCollection : Form return; } - FormContainerShip form = new () + FormContainerShip form = new() { SetShip = ship }; @@ -186,4 +187,99 @@ public partial class FormShipCollection : 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(); + } + + /// + /// Обновление списка в 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.SelectedItem == null) + { + MessageBox.Show("Коллекция не выбрана"); + return; + } + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + 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 ShipPortService(pictureBox.Width, pictureBox.Height, collection); + break; + } + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } }