using ProjectMonorail.CollectionGenericObject; using ProjectMonorail.CollectionGenericObjects; using ProjectMonorail.Drawings; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ProjectMonorail; /// /// Форма работы с компанией и её коллекцией /// public partial class FormTrainCollection : Form { /// /// Хранилище коллекций /// private readonly StorageCollection _storageCollection; /// /// Компания /// private AbstractCompany? _company = null; /// /// Конструктор /// public FormTrainCollection() { InitializeComponent(); _storageCollection = new(); } /// /// Выбор компании /// /// /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { panelCompanyTools.Enabled = false; } /// /// Добавление поезда /// /// /// private void ButtonAddTrain_Click(object sender, EventArgs e) { FormTrainConfig form = new(); form.AddEvent(SetTrain); form.Show(); } /// /// Добавление поезда в коллекцию /// /// private void SetTrain(DrawingTrain train) { if (_company == null || train == null) { return; } if (_company + train != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); } else { MessageBox.Show("Не удалось добавить объект"); } } /// /// Удаление объекта /// /// /// private void ButtonRemoveTrain_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) { return; } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); if (_company - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _company.Show(); } else { MessageBox.Show("Не удалось удалить объект"); } } /// /// Передача объекта в другую форму /// /// /// private void ButtonGoToCheck_Click(object sender, EventArgs e) { if (_company == null) { return; } DrawingTrain? train = null; int counter = 100; while (train == null) { train = _company.GetRandomObject(); counter--; if (counter <= 0) { break; } } if (train == null) { return; } FormMonorail form = new() { SetTrain = train }; form.ShowDialog(); } /// /// Перерисовка коллекции /// /// /// private void ButtonRefresh_Click(object sender, EventArgs e) { if (_company == null) { return; } 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; } if (radioButtonList.Checked) { collectionType = CollectionType.List; } _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); RefreshListBoxItems(); } /// /// Удаление коллекции /// /// /// 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.No) { return; } _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); RefreshListBoxItems(); } /// /// Обновление списка в listBoxCollection /// private void RefreshListBoxItems() { 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 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 TrainSharingService(pictureBox.Width, pictureBox.Height, collection); break; } panelCompanyTools.Enabled = true; RefreshListBoxItems(); } }