diff --git a/solution/lab1/CollectionGenericObjects/CollectionType.cs b/solution/lab1/CollectionGenericObjects/CollectionType.cs new file mode 100644 index 0000000..d040d21 --- /dev/null +++ b/solution/lab1/CollectionGenericObjects/CollectionType.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.CollectionGenericObjects; + +public enum CollectionType +{ + /// + /// Неопределено + /// + None = 0, + /// + /// Массив + /// + Massive = 1, + /// + /// Список + /// + List = 2 +} diff --git a/solution/lab1/CollectionGenericObjects/ListGenericObjects.cs b/solution/lab1/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..ebaf977 --- /dev/null +++ b/solution/lab1/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.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 < _collection.Count) + { + return _collection[position]; + } + return null; + } + + public int Insert(T obj) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO вставка в конец набора + if (_collection.Count <= _maxCount) + { + _collection.Add(obj); + return _collection.Count; + } + return -1; + } + + public int Insert(T obj, int position) + { + // TODO проверка, что не превышено максимальное количество элементов + // TODO проверка позиции + // TODO вставка по позиции + if (position >= 0 && position < _maxCount && _collection.Count <= _maxCount) + { + _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/solution/lab1/CollectionGenericObjects/StorageCollection.cs b/solution/lab1/CollectionGenericObjects/StorageCollection.cs new file mode 100644 index 0000000..3139ef4 --- /dev/null +++ b/solution/lab1/CollectionGenericObjects/StorageCollection.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.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/solution/lab1/FormTrackedVehicleCollection.Designer.cs b/solution/lab1/FormTrackedVehicleCollection.Designer.cs index a1ab031..048f362 100644 --- a/solution/lab1/FormTrackedVehicleCollection.Designer.cs +++ b/solution/lab1/FormTrackedVehicleCollection.Designer.cs @@ -29,52 +29,107 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); + panelCompanyTools = new Panel(); + buttonAddFighter = new Button(); + buttonAddTrackedVehicle = new Button(); buttonRefresh = new Button(); + maskedTextBox = new MaskedTextBox(); buttonGoToCheck = new Button(); buttonRemoveTrackedVehicle = new Button(); - maskedTextBox = new MaskedTextBox(); - buttonAddTrackedVehicle = new Button(); - buttonAddFighter = new Button(); + button1CreateCompany = 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(buttonGoToCheck); - groupBoxTools.Controls.Add(buttonRemoveTrackedVehicle); - groupBoxTools.Controls.Add(maskedTextBox); - groupBoxTools.Controls.Add(buttonAddTrackedVehicle); - groupBoxTools.Controls.Add(buttonAddFighter); + groupBoxTools.Controls.Add(panelCompanyTools); + groupBoxTools.Controls.Add(button1CreateCompany); + groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(886, 0); + groupBoxTools.Location = new Point(670, 0); groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Size = new Size(297, 617); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // + // panelCompanyTools + // + panelCompanyTools.Controls.Add(buttonAddFighter); + panelCompanyTools.Controls.Add(buttonAddTrackedVehicle); + panelCompanyTools.Controls.Add(buttonRefresh); + panelCompanyTools.Controls.Add(maskedTextBox); + panelCompanyTools.Controls.Add(buttonGoToCheck); + panelCompanyTools.Controls.Add(buttonRemoveTrackedVehicle); + panelCompanyTools.Enabled = false; + panelCompanyTools.Location = new Point(3, 338); + panelCompanyTools.Name = "panelCompanyTools"; + panelCompanyTools.Size = new Size(282, 273); + panelCompanyTools.TabIndex = 10; + // + // buttonAddFighter + // + buttonAddFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddFighter.Location = new Point(6, 0); + buttonAddFighter.Name = "buttonAddFighter"; + buttonAddFighter.Size = new Size(273, 52); + buttonAddFighter.TabIndex = 2; + buttonAddFighter.Text = "Добавление истребителя"; + buttonAddFighter.UseVisualStyleBackColor = true; + buttonAddFighter.Click += ButtonAddTrackedVehicle_Click; + // + // buttonAddTrackedVehicle + // + buttonAddTrackedVehicle.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddTrackedVehicle.Location = new Point(6, 58); + buttonAddTrackedVehicle.Name = "buttonAddTrackedVehicle"; + buttonAddTrackedVehicle.Size = new Size(273, 60); + buttonAddTrackedVehicle.TabIndex = 3; + buttonAddTrackedVehicle.Text = "Добавление гусеничной машины"; + buttonAddTrackedVehicle.UseVisualStyleBackColor = true; + buttonAddTrackedVehicle.Click += ButtonAddTrackedVehicle_Click; + // // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(7, 516); + buttonRefresh.Location = new Point(3, 244); buttonRefresh.Name = "buttonRefresh"; - buttonRefresh.Size = new Size(284, 53); + buttonRefresh.Size = new Size(272, 31); buttonRefresh.TabIndex = 7; buttonRefresh.Text = "Обновить"; buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.Click += ButtonRefresh_Click_1; // + // maskedTextBox + // + maskedTextBox.Location = new Point(3, 124); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(284, 31); + maskedTextBox.TabIndex = 4; + maskedTextBox.ValidatingType = typeof(int); + maskedTextBox.MaskInputRejected += maskedTextBox1_MaskInputRejected; + // // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(5, 457); + buttonGoToCheck.Location = new Point(7, 205); buttonGoToCheck.Name = "buttonGoToCheck"; - buttonGoToCheck.Size = new Size(286, 53); + buttonGoToCheck.Size = new Size(274, 33); buttonGoToCheck.TabIndex = 6; buttonGoToCheck.Text = "Передать на тесты"; buttonGoToCheck.UseVisualStyleBackColor = true; @@ -83,45 +138,106 @@ // buttonRemoveTrackedVehicle // buttonRemoveTrackedVehicle.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveTrackedVehicle.Location = new Point(5, 342); + buttonRemoveTrackedVehicle.Location = new Point(7, 161); buttonRemoveTrackedVehicle.Name = "buttonRemoveTrackedVehicle"; - buttonRemoveTrackedVehicle.Size = new Size(286, 82); + buttonRemoveTrackedVehicle.Size = new Size(274, 38); buttonRemoveTrackedVehicle.TabIndex = 5; buttonRemoveTrackedVehicle.Text = "Удаление гусеничной машины"; buttonRemoveTrackedVehicle.UseVisualStyleBackColor = true; buttonRemoveTrackedVehicle.Click += ButtonRemoveTrackedVehicle_Click; // - // maskedTextBox + // button1CreateCompany // - maskedTextBox.Location = new Point(7, 305); - maskedTextBox.Mask = "00"; - maskedTextBox.Name = "maskedTextBox"; - maskedTextBox.Size = new Size(284, 31); - maskedTextBox.TabIndex = 4; - maskedTextBox.ValidatingType = typeof(int); - maskedTextBox.MaskInputRejected += maskedTextBox1_MaskInputRejected; + button1CreateCompany.Location = new Point(6, 298); + button1CreateCompany.Name = "button1CreateCompany"; + button1CreateCompany.Size = new Size(276, 34); + button1CreateCompany.TabIndex = 9; + button1CreateCompany.Text = "Создать компанию"; + button1CreateCompany.UseVisualStyleBackColor = true; + button1CreateCompany.Click += button1CreateCompany_Click; // - // buttonAddTrackedVehicle + // panelStorage // - buttonAddTrackedVehicle.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddTrackedVehicle.Location = new Point(12, 181); - buttonAddTrackedVehicle.Name = "buttonAddTrackedVehicle"; - buttonAddTrackedVehicle.Size = new Size(279, 82); - buttonAddTrackedVehicle.TabIndex = 3; - buttonAddTrackedVehicle.Text = "Добавление гусеничной машины"; - buttonAddTrackedVehicle.UseVisualStyleBackColor = true; - buttonAddTrackedVehicle.Click += ButtonAddTrackedVehicle_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, 27); + panelStorage.Name = "panelStorage"; + panelStorage.Size = new Size(291, 226); + panelStorage.TabIndex = 8; // - // buttonAddFighter + // buttonCollectionDel // - buttonAddFighter.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddFighter.Location = new Point(0, 98); - buttonAddFighter.Name = "buttonAddFighter"; - buttonAddFighter.Size = new Size(291, 77); - buttonAddFighter.TabIndex = 2; - buttonAddFighter.Text = "Добавление истребителя"; - buttonAddFighter.UseVisualStyleBackColor = true; - buttonAddFighter.Click += ButtonAddTrackedVehicle_Click; + buttonCollectionDel.Location = new Point(3, 189); + buttonCollectionDel.Name = "buttonCollectionDel"; + buttonCollectionDel.Size = new Size(276, 34); + buttonCollectionDel.TabIndex = 6; + buttonCollectionDel.Text = "Удалить коллекцию"; + buttonCollectionDel.UseVisualStyleBackColor = true; + buttonCollectionDel.Click += buttonCollectionDel_Click; + // + // listBoxCollection + // + listBoxCollection.FormattingEnabled = true; + listBoxCollection.ItemHeight = 25; + listBoxCollection.Location = new Point(6, 154); + listBoxCollection.Name = "listBoxCollection"; + listBoxCollection.Size = new Size(276, 29); + listBoxCollection.TabIndex = 5; + // + // buttonCollectionAdd + // + buttonCollectionAdd.Location = new Point(6, 114); + buttonCollectionAdd.Name = "buttonCollectionAdd"; + buttonCollectionAdd.Size = new Size(276, 34); + buttonCollectionAdd.TabIndex = 4; + buttonCollectionAdd.Text = "Добавить коллекцию"; + buttonCollectionAdd.UseVisualStyleBackColor = true; + buttonCollectionAdd.Click += buttonCollectionAdd_Click; + // + // radioButtonList + // + radioButtonList.AutoSize = true; + radioButtonList.Location = new Point(153, 79); + 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(41, 79); + radioButtonMassive.Name = "radioButtonMassive"; + radioButtonMassive.Size = new Size(98, 29); + radioButtonMassive.TabIndex = 2; + radioButtonMassive.TabStop = true; + radioButtonMassive.Text = "Массив"; + radioButtonMassive.UseVisualStyleBackColor = true; + radioButtonMassive.CheckedChanged += radioButtonMassive_CheckedChanged; + // + // textBoxCollectionName + // + textBoxCollectionName.Location = new Point(4, 42); + textBoxCollectionName.Name = "textBoxCollectionName"; + textBoxCollectionName.Size = new Size(278, 31); + textBoxCollectionName.TabIndex = 1; + // + // labelCollectionName + // + labelCollectionName.AutoSize = true; + labelCollectionName.Location = new Point(41, 14); + labelCollectionName.Name = "labelCollectionName"; + labelCollectionName.Size = new Size(186, 25); + labelCollectionName.TabIndex = 0; + labelCollectionName.Text = "Название коллекции:"; // // comboBoxSelectorCompany // @@ -129,7 +245,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(7, 30); + comboBoxSelectorCompany.Location = new Point(3, 259); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(284, 33); comboBoxSelectorCompany.TabIndex = 1; @@ -140,7 +256,7 @@ pictureBox.Dock = DockStyle.Fill; pictureBox.Location = new Point(0, 0); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(886, 617); + pictureBox.Size = new Size(670, 617); pictureBox.TabIndex = 1; pictureBox.TabStop = false; pictureBox.Click += pictureBox1_Click; @@ -149,14 +265,17 @@ // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1183, 617); + ClientSize = new Size(967, 617); Controls.Add(pictureBox); Controls.Add(groupBoxTools); Name = "FormTrackedVehicleCollection"; Text = "Коллекция гусеничных машин"; Load += FormTrackedVehicleCollection_Load; groupBoxTools.ResumeLayout(false); - groupBoxTools.PerformLayout(); + panelCompanyTools.ResumeLayout(false); + panelCompanyTools.PerformLayout(); + panelStorage.ResumeLayout(false); + panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); ResumeLayout(false); } @@ -172,5 +291,15 @@ private MaskedTextBox maskedTextBox; private Button buttonGoToCheck; private Button buttonRefresh; + private Panel panelStorage; + private TextBox textBoxCollectionName; + private Label labelCollectionName; + private RadioButton radioButtonList; + private RadioButton radioButtonMassive; + private ListBox listBoxCollection; + private Button buttonCollectionAdd; + private Button button1CreateCompany; + private Button buttonCollectionDel; + private Panel panelCompanyTools; } } \ No newline at end of file diff --git a/solution/lab1/FormTrackedVehicleCollection.cs b/solution/lab1/FormTrackedVehicleCollection.cs index cfa1c2a..294137b 100644 --- a/solution/lab1/FormTrackedVehicleCollection.cs +++ b/solution/lab1/FormTrackedVehicleCollection.cs @@ -7,6 +7,11 @@ namespace lab1; /// public partial class FormTrackedVehicleCollection : Form { + + /// + /// Хранилище коллекций + /// + private readonly StorageCollection _storageCollection; /// /// Компания /// @@ -18,6 +23,7 @@ public partial class FormTrackedVehicleCollection : Form public FormTrackedVehicleCollection() { InitializeComponent(); + _storageCollection = new(); } /// /// Выбор компании @@ -27,12 +33,7 @@ public partial class FormTrackedVehicleCollection : Form private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { - switch (comboBoxSelectorCompany.Text) - { - case "Хранилище": - _company = new TrackedVehicleSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); - break; - } + panelCompanyTools.Enabled = false; } /// @@ -66,6 +67,7 @@ public partial class FormTrackedVehicleCollection : Form drawningTrackedVehicle = new DrawningTrackedVehicle(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); break; case nameof(DrawningEntityFighter): + //TODO Выбор цветов drawningTrackedVehicle = new DrawningEntityFighter(random.Next(100, 300), random.Next(1000, 3000), GetColor(random), GetColor(random), @@ -103,7 +105,7 @@ public partial class FormTrackedVehicleCollection : Form } return color; } - + /// /// Удаление объекта /// @@ -185,7 +187,7 @@ public partial class FormTrackedVehicleCollection : Form } private void pictureBox1_Click(object sender, EventArgs e) { - + } private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) @@ -193,12 +195,108 @@ public partial class FormTrackedVehicleCollection : Form } - + private void FormTrackedVehicleCollection_Load(object sender, EventArgs e) { } + private void radioButtonMassive_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 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.Yes) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + 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 button1CreateCompany_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 TrackedVehicleSharingService(pictureBox.Width, pictureBox.Height, collection); + break; + } + panelCompanyTools.Enabled = true; + RerfreshListBoxItems(); + + } }