diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/CollectionType.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..21cadf0 --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TrolleybusProject.CollectionGenericObjects; + +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + /// + /// Массив + /// + Massive = 1, + /// + /// Список + /// + List = 2 + +} diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/ListGenericObjects.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..7db938b --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TrolleybusProject.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 int MaxCount => _maxCount; + + /// + /// Конструктор + /// + public ListGenericObjects() + { + _collection = new(); + } + public T? Get(int position) + { + if (position >= Count || position < 0) + { + return null; + } + return _collection[position]; + } + + public int Insert(T obj) + { + if (Count == _maxCount) + { + return -1; + } + + _collection.Add(obj); + return _collection.Count; + } + public int Insert(T obj, int position) + { + if (Count == _maxCount || position < 0 || position > Count) + { + return -1; + } + + _collection.Insert(position, obj); + return position; + } + public T? Remove(int position) + { + if (_collection == null || position < 0 || position >= _collection.Count) { + + return null; + + } + T? obj = _collection[position]; + _collection[position] = null; + return obj; + } + +} + diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs index 44783e9..4b7ac38 100644 --- a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs @@ -16,7 +16,23 @@ internal class MassiveGenericObjects : ICollectionGenericObjects 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/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..dc393ec --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TrolleybusProject.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) && name == "") + { + return; + } + + if (collectionType == CollectionType.Massive) + { + _storages[name] = new MassiveGenericObjects(); + } + else + { + _storages[name] = new ListGenericObjects(); + } + } + + /// + /// Удаление коллекции + /// + /// Название коллекции + public void DelCollection(string name) + { + _storages.Remove(name); +} + /// + /// Доступ к коллекции + /// + /// Название коллекции + /// + public ICollectionGenericObjects? this[string name] + { + get + { + if (name == "") + { + return null; + } + return _storages[name]; + } + } +} + + + diff --git a/TrolleybusProject/TrolleybusProject/DirectionType.cs b/TrolleybusProject/TrolleybusProject/Drawnings/DirectionType.cs similarity index 100% rename from TrolleybusProject/TrolleybusProject/DirectionType.cs rename to TrolleybusProject/TrolleybusProject/Drawnings/DirectionType.cs diff --git a/TrolleybusProject/TrolleybusProject/FormBusCollection.Designer.cs b/TrolleybusProject/TrolleybusProject/FormBusCollection.Designer.cs index 1b19def..5648226 100644 --- a/TrolleybusProject/TrolleybusProject/FormBusCollection.Designer.cs +++ b/TrolleybusProject/TrolleybusProject/FormBusCollection.Designer.cs @@ -29,26 +29,35 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + panelCompanyTools = new Panel(); + buttonCreateCompany = new Button(); buttonRefresh = new Button(); - buttonGoToCheck = new Button(); - buttonDelTrolleybus = new Button(); - maskedTextBox = new MaskedTextBox(); - buttonAddTrolleybus = new Button(); - buttonAddBus = new Button(); comboBoxSelectionCompany = new ComboBox(); + buttonGoToCheck = new Button(); + buttonAddBus = new Button(); + buttonDelTrolleybus = new Button(); + buttonAddTrolleybus = new Button(); + maskedTextBox = new MaskedTextBox(); + panelStorage = new Panel(); + buttonCollectionDel = new Button(); + listBoxCollection = new ListBox(); + buttonCollectionAdd = new Button(); + radioButtonList = new RadioButton(); + radioButtonMassive = new RadioButton(); + textBoxCollectionName = new TextBox(); + labelCollectionName = new Label(); pictureBox = new PictureBox(); groupBoxTools.SuspendLayout(); + panelCompanyTools.SuspendLayout(); + panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); SuspendLayout(); // // groupBoxTools // - groupBoxTools.Controls.Add(buttonRefresh); - groupBoxTools.Controls.Add(buttonGoToCheck); - groupBoxTools.Controls.Add(buttonDelTrolleybus); - groupBoxTools.Controls.Add(maskedTextBox); - groupBoxTools.Controls.Add(buttonAddTrolleybus); - groupBoxTools.Controls.Add(buttonAddBus); + groupBoxTools.Controls.Add(buttonCreateCompany); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectionCompany); groupBoxTools.Dock = DockStyle.Right; groupBoxTools.Location = new Point(904, 0); @@ -58,82 +67,187 @@ groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(buttonAddBus); + panelCompanyTools.Controls.Add(buttonDelTrolleybus); + panelCompanyTools.Controls.Add(buttonAddTrolleybus); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(0, 397); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(294, 259); + panelCompanyTools.TabIndex = 9; + // + // buttonCreateCompany + // + buttonCreateCompany.Location = new Point(63, 345); + buttonCreateCompany.Name = "buttonCreateCompany"; + buttonCreateCompany.Size = new Size(187, 34); + buttonCreateCompany.TabIndex = 8; + buttonCreateCompany.Text = "Создать компанию"; + buttonCreateCompany.UseVisualStyleBackColor = true; + buttonCreateCompany.Click += buttonCreateCompany_Click; + // // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(20, 485); + buttonRefresh.Location = new Point(24, 215); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(249, 34); + buttonRefresh.Size = new Size(246, 34); buttonRefresh.TabIndex = 6; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.Click += buttonRefresh_Click; // - // buttonGoToCheck - // - buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(20, 395); - buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(249, 34); - buttonGoToCheck.TabIndex = 5; - buttonGoToCheck.Text = "Передать на тесты"; - buttonGoToCheck.UseVisualStyleBackColor = true; - buttonGoToCheck.Click += buttonGoToCheck_Click; - // - // buttonDelTrolleybus - // - buttonDelTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonDelTrolleybus.Location = new Point(40, 278); - buttonDelTrolleybus.Name = "buttonDelTrolleybus"; - buttonDelTrolleybus.Size = new Size(217, 34); - buttonDelTrolleybus.TabIndex = 4; - buttonDelTrolleybus.Text = "Удалить автобус"; - buttonDelTrolleybus.UseVisualStyleBackColor = true; - buttonDelTrolleybus.Click += buttonDelTrolleybus_Click; - // - // maskedTextBox - // - maskedTextBox.Location = new Point(73, 227); - maskedTextBox.Mask = "00"; - maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(150, 31); - maskedTextBox.TabIndex = 3; - maskedTextBox.ValidatingType = typeof(int); - // - // buttonAddTrolleybus - // - buttonAddTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddTrolleybus.Location = new Point(19, 165); - buttonAddTrolleybus.Name = "buttonAddTrolleybus"; - buttonAddTrolleybus.Size = new Size(249, 34); - buttonAddTrolleybus.TabIndex = 2; - buttonAddTrolleybus.Text = "Добавление троллейбуса"; - buttonAddTrolleybus.UseVisualStyleBackColor = true; - buttonAddTrolleybus.Click += buttonAddTrolleybus_Click; - // - // buttonAddBus - // - buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddBus.Location = new Point(20, 97); - buttonAddBus.Name = "buttonAddBus"; - buttonAddBus.Size = new Size(249, 34); - buttonAddBus.TabIndex = 1; - buttonAddBus.Text = "Добавление автобуса"; - buttonAddBus.UseVisualStyleBackColor = true; - buttonAddBus.Click += buttonAddBus_Click; - // // comboBoxSelectionCompany // comboBoxSelectionCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; comboBoxSelectionCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectionCompany.FormattingEnabled = true; comboBoxSelectionCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectionCompany.Location = new Point(19, 30); + comboBoxSelectionCompany.Location = new Point(26, 306); comboBoxSelectionCompany.Name = "comboBoxSelectionCompany"; - comboBoxSelectionCompany.Size = new Size(250, 33); + comboBoxSelectionCompany.Size = new Size(244, 33); comboBoxSelectionCompany.TabIndex = 0; comboBoxSelectionCompany.SelectedIndexChanged += comboBoxSelectionCompany_SelectedIndexChanged; // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(24, 175); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(246, 34); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += buttonGoToCheck_Click; + // + // buttonAddBus + // + buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddBus.Location = new Point(26, 18); + buttonAddBus.Name = "buttonAddBus"; + buttonAddBus.Size = new Size(244, 34); + buttonAddBus.TabIndex = 1; + buttonAddBus.Text = "Добавление автобуса"; + buttonAddBus.UseVisualStyleBackColor = true; + buttonAddBus.Click += buttonAddBus_Click; + // + // buttonDelTrolleybus + // + buttonDelTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonDelTrolleybus.Location = new Point(22, 135); + buttonDelTrolleybus.Name = "buttonDelTrolleybus"; + buttonDelTrolleybus.Size = new Size(244, 34); + buttonDelTrolleybus.TabIndex = 4; + buttonDelTrolleybus.Text = "Удалить автобус"; + buttonDelTrolleybus.UseVisualStyleBackColor = true; + buttonDelTrolleybus.Click += buttonDelTrolleybus_Click; + // + // buttonAddTrolleybus + // + buttonAddTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddTrolleybus.Location = new Point(24, 58); + buttonAddTrolleybus.Name = "buttonAddTrolleybus"; + buttonAddTrolleybus.Size = new Size(246, 34); + buttonAddTrolleybus.TabIndex = 2; + buttonAddTrolleybus.Text = "Добавление троллейбуса"; + buttonAddTrolleybus.UseVisualStyleBackColor = true; + buttonAddTrolleybus.Click += buttonAddTrolleybus_Click; + // + // maskedTextBox + // + maskedTextBox.Location = new Point(68, 98); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(150, 31); + maskedTextBox.TabIndex = 3; + maskedTextBox.ValidatingType = typeof(int); + // + // 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, 27); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(294, 273); + panelStorage.TabIndex = 7; + // + // buttonCollectionDel + // + buttonCollectionDel.Location = new Point(8, 225); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(277, 34); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += buttonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.ItemHeight = 25; + listBoxCollection.Location = new Point(11, 140); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(274, 79); + listBoxCollection.TabIndex = 7; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(8, 100); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(277, 34); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(171, 65); + radioButtonList.Name = "radioButtonList"; + radioButtonList.Size = new Size(96, 29); + radioButtonList.TabIndex = 3; + radioButtonList.TabStop = true; + radioButtonList.Text = "Список"; + radioButtonList.UseVisualStyleBackColor = true; + // + // radioButtonMassive + // + radioButtonMassive.AutoSize = true; + radioButtonMassive.Location = new Point(11, 65); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(98, 29); + radioButtonMassive.TabIndex = 2; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(3, 28); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(288, 31); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(65, 0); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(182, 25); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции"; + // // pictureBox // pictureBox.Dock = DockStyle.Fill; @@ -153,7 +267,10 @@ Name = "FormBusCollection"; Text = "Коллекция автобусов"; groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); ResumeLayout(false); } @@ -169,5 +286,15 @@ private PictureBox pictureBox; private Button buttonRefresh; private Button buttonGoToCheck; + private Panel panelStorage; + private Label labelCollectionName; + private RadioButton radioButtonMassive; + private TextBox textBoxCollectionName; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private RadioButton radioButtonList; + private Button buttonCreateCompany; + private Button buttonCollectionDel; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/TrolleybusProject/TrolleybusProject/FormBusCollection.cs b/TrolleybusProject/TrolleybusProject/FormBusCollection.cs index abcd519..33ae42e 100644 --- a/TrolleybusProject/TrolleybusProject/FormBusCollection.cs +++ b/TrolleybusProject/TrolleybusProject/FormBusCollection.cs @@ -14,14 +14,25 @@ namespace TrolleybusProject; public partial class FormBusCollection : Form -{ +{ /// + /// Хранилише коллекций + /// + private readonly StorageCollection _storageCollection; + /// + /// Компания + /// + private AbstractCompany? _company = null; public FormBusCollection() { InitializeComponent(); + _storageCollection = new(); } - + private ListBox GetListBoxCollection() + { + return listBoxCollection; + } private void CreateObject(string type) { if (_company == null) @@ -59,13 +70,7 @@ public partial class FormBusCollection : Form private void comboBoxSelectionCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectionCompany.Text) - { - case "Хранилище": - _company = new BusStation(pictureBox.Width, - pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } @@ -141,5 +146,89 @@ public partial class FormBusCollection : Form } + + 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) + { + if (listBoxCollection.SelectedItem == null) return; + if (listBoxCollection.SelectedIndex < 0 ) + { + 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 (comboBoxSelectionCompany.Text) + { + case "Хранилище": + _company = new BusStation(pictureBox.Width, + pictureBox.Height, collection); + break; + } + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + } } + +