PIBD-14_Lavrova_K.I._Simple/solution/lab1/FormTrackedVehicleCollection.cs

291 lines
8.7 KiB
C#
Raw Normal View History

2024-04-24 22:45:48 +04:00
using lab1.CollectionGenericObjects;
using lab1.Drawnings;
2024-05-23 11:15:50 +04:00
using System.Windows.Forms;
2024-04-24 22:45:48 +04:00
namespace lab1;
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
/// <summary>
2024-05-23 11:15:50 +04:00
/// Форма работы с компанией и ее коллекцией
2024-04-24 22:45:48 +04:00
/// </summary>
public partial class FormTrackedVehicleCollection : Form
{
/// <summary>
2024-05-23 11:15:50 +04:00
/// Хранилише коллекций
/// </summary>
private readonly StorageCollection<DrawningTrackedVehicle> _storageCollection;
2024-04-24 22:45:48 +04:00
/// <summary>
/// Компания
/// </summary>
private AbstractCompany? _company = null;
/// <summary>
/// Конструктор
/// </summary>
public FormTrackedVehicleCollection()
{
InitializeComponent();
_storageCollection = new();
2024-04-24 22:45:48 +04:00
}
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
/// <summary>
/// Выбор компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-05-23 11:15:50 +04:00
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
2024-04-24 22:45:48 +04:00
{
2024-05-23 11:15:50 +04:00
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new TrackedVehicleSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningTrackedVehicle>());
break;
}
2024-04-24 22:45:48 +04:00
}
/// <summary>
2024-05-23 11:15:50 +04:00
/// Добавление обычного автомобиля
2024-04-24 22:45:48 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddTrackedVehicle_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTrackedVehicle));
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
/// <summary>
2024-05-23 11:15:50 +04:00
/// Добавление спортивного автомобиля
2024-04-24 22:45:48 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-05-20 14:42:55 +04:00
private void ButtonAddFighter_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningEntityFighter));
2024-04-24 22:45:48 +04:00
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
2024-05-23 11:15:50 +04:00
/// <param name="type">Тип создаваемого объекта</param>
2024-04-24 22:45:48 +04:00
private void CreateObject(string type)
{
if (_company == null)
{
return;
}
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
Random random = new();
2024-05-23 11:15:50 +04:00
DrawningTrackedVehicle drawingTrans;
2024-04-24 22:45:48 +04:00
switch (type)
{
case nameof(DrawningTrackedVehicle):
2024-05-23 11:15:50 +04:00
drawingTrans = new DrawningTrackedVehicle(random.Next(100, 300), random.Next(1000, 3000), GetColor(random));
2024-04-24 22:45:48 +04:00
break;
case nameof(DrawningEntityFighter):
2024-05-23 11:15:50 +04:00
// вызов диалогового окна для выбора цвета
drawingTrans = new DrawningEntityFighter(random.Next(100, 300), random.Next(1000, 3000),
GetColor(random),
GetColor(random),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
2024-04-24 22:45:48 +04:00
break;
default:
return;
}
2024-05-23 11:15:50 +04:00
if (_company + drawingTrans != -1)
2024-04-24 22:45:48 +04:00
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
}
else
{
2024-05-23 11:15:50 +04:00
_ = MessageBox.Show(drawingTrans.ToString());
2024-04-24 22:45:48 +04:00
}
}
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
/// <summary>
2024-05-23 11:15:50 +04:00
/// Получение цвета
/// </summary>
/// <param name="random">Генератор случайных чисел</param>
/// <returns></returns>
2024-04-24 22:45:48 +04:00
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;
}
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
return color;
}
2024-04-24 22:45:48 +04:00
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-05-23 11:15:50 +04:00
private void buttonCollectionDel_Click(object sender, EventArgs e)
2024-04-24 22:45:48 +04:00
{
if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null)
{
return;
}
2024-05-23 11:15:50 +04:00
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
2024-04-24 22:45:48 +04:00
{
return;
}
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
int pos = Convert.ToInt32(maskedTextBox.Text);
if (_company - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _company.Show();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
/// <summary>
/// Передача объекта в другую форму
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
2024-05-23 11:15:50 +04:00
DrawningTrackedVehicle? car = null;
2024-04-24 22:45:48 +04:00
int counter = 100;
2024-05-23 11:15:50 +04:00
while (car == null)
2024-04-24 22:45:48 +04:00
{
2024-05-23 11:15:50 +04:00
car = _company.GetRandomObject();
2024-04-24 22:45:48 +04:00
counter--;
if (counter <= 0)
{
break;
}
}
2024-05-23 11:15:50 +04:00
if (car == null)
2024-04-24 22:45:48 +04:00
{
return;
}
2024-05-23 11:15:50 +04:00
2024-04-24 22:45:48 +04:00
FormFighter form = new()
{
2024-05-23 11:15:50 +04:00
SetTrackedVehicle = car
2024-04-24 22:45:48 +04:00
};
form.ShowDialog();
}
/// <summary>
/// Перерисовка коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefresh_Click_1(object sender, EventArgs e)
{
if (_company == null)
{
return;
}
2024-05-23 11:15:50 +04:00
pictureBox.Image = _company.Show();
2024-04-24 22:45:48 +04:00
}
/// <summary>
2024-05-23 11:15:50 +04:00
/// Добавление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCollectionAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2024-05-23 11:15:50 +04:00
CollectionType collectionType = CollectionType.None;
if (radioButtonMassive.Checked)
{
collectionType = CollectionType.Massive;
}
else if (radioButtonList.Checked)
{
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
RerfreshListBoxItems();
}
2024-05-23 11:15:50 +04:00
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-05-23 11:15:50 +04:00
private void ButtonRemoveTrackedVehicle_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
2024-05-23 11:15:50 +04:00
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
return;
}
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RerfreshListBoxItems();
}
2024-05-23 11:15:50 +04:00
/// <summary>
/// Обновление списка в listBoxCollection
/// </summary>
private void RerfreshListBoxItems()
{
listBoxCollection.Items.Clear();
2024-05-23 11:15:50 +04:00
foreach (var key in _storageCollection.Keys ?? Enumerable.Empty<string>())
{
2024-05-23 11:15:50 +04:00
if (!string.IsNullOrEmpty(key))
{
2024-05-23 11:15:50 +04:00
listBoxCollection.Items.Add(key);
}
}
2024-05-23 11:15:50 +04:00
}
2024-05-23 11:15:50 +04:00
/// <summary>
/// Создание компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1CreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
2024-05-23 11:15:50 +04:00
ICollectionGenericObjects<DrawningTrackedVehicle>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
2024-05-23 11:15:50 +04:00
switch (comboBoxSelectorCompany.Text)
{
case "Хранилище":
_company = new TrackedVehicleSharingService(pictureBox.Width, pictureBox.Height, collection);
break;
}
2024-05-23 11:15:50 +04:00
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
2024-04-24 22:45:48 +04:00
2024-05-23 11:15:50 +04:00
}