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 AbstractCompany? _company = null; /// /// Конструктор /// public FormTrainCollection() { InitializeComponent(); } /// /// Выбор компании /// /// /// private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBoxSelectorCompany.Text) { case "Хранилище": _company = new TrainSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); break; } } /// /// Добавление поезда /// /// /// private void ButtonAddTrain_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingTrain)); /// /// Добавление монорельса /// /// /// private void ButtonAddMonorail_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingMonorail)); /// /// Создание объекта класа - перемещение /// /// private void CreateObject(string type) { if (_company == null) { return; } Random random = new(); DrawingTrain drawingTrain; switch (type) { case nameof(DrawingTrain): drawingTrain = new DrawingTrain(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); break; case nameof(DrawingMonorail): drawingTrain = new DrawingMonorail(random.Next(100, 300), random.Next(1000, 3000), random.Next(2, 5), GetColor(random), GetColor(random), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); break; default: return; } if (_company + drawingTrain != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); } else { MessageBox.Show("Не удалось добавить объект"); } } /// /// Получение цвета /// /// Генератор случайных чисел /// private static Color GetColor(Random random) { Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); ColorDialog dialog = new(); if (dialog.ShowDialog() == DialogResult.OK) { color = dialog.Color; } return color; } /// /// Удаление объекта /// /// /// 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(); } }