PIbd-14_Rachek_A.S._Base/lab_0/FormSimpleBusCollection.cs

381 lines
11 KiB
C#
Raw Permalink Normal View History

2024-04-25 10:17:26 +04:00
using ProjectBus.MovementStrategy;
using ProjectBus.Drawnings;
using ProjectBus.CollectionGenericObject;
using System.Windows.Forms;
2024-06-17 00:13:00 +04:00
using Microsoft.Extensions.Logging;
2024-04-25 10:17:26 +04:00
namespace ProjectBus;
public partial class FormSimpleBusCollection : Form
{
2024-05-20 14:22:37 +04:00
/// <summary>
/// Хранилише коллекций
/// </summary>
private readonly StorageCollection<DrawningSimpleBus> _storageCollection;
/// <summary>
/// Компания
/// </summary>
2024-04-25 10:17:26 +04:00
private AbstractCompany? _company = null;
2024-05-20 14:22:37 +04:00
2024-06-17 00:13:00 +04:00
private readonly ILogger _logger;
2024-05-20 14:22:37 +04:00
/// <summary>
/// Конструктор
/// </summary>
2024-06-17 00:13:00 +04:00
public FormSimpleBusCollection(ILogger<FormSimpleBusCollection> logger)
2024-04-25 10:17:26 +04:00
{
InitializeComponent();
2024-05-20 14:22:37 +04:00
_storageCollection = new();
2024-06-17 00:13:00 +04:00
_logger = logger;
2024-04-25 10:17:26 +04:00
}
2024-05-20 14:22:37 +04:00
/// <summary>
/// Выбор компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-04-25 10:17:26 +04:00
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "хранилище":
2024-06-17 00:13:00 +04:00
_company = new BusStation(pictureBox.Width, pictureBox.Height,
new MassiveGenericObjects<DrawningSimpleBus>());
2024-04-25 10:17:26 +04:00
break;
}
}
2024-05-20 14:22:37 +04:00
/// <summary>
2024-05-22 22:22:59 +04:00
/// Добавление автобуса
2024-05-20 14:22:37 +04:00
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2024-05-22 22:22:59 +04:00
private void ButtonAddSimpleBus_Click(object sender, EventArgs e)
{
FormSimpleBusConfig form = new();
form.Show();
form.AddEvent(SetSimpleBus);
}
private void SetSimpleBus(DrawningSimpleBus? drawningSimpleBus)
{
2024-06-17 00:13:00 +04:00
if (_company == null || drawningSimpleBus == null)
{
return;
}
try
2024-05-22 22:22:59 +04:00
{
2024-06-17 00:13:00 +04:00
var res = _company + drawningSimpleBus;
2024-05-22 22:22:59 +04:00
MessageBox.Show("Объект добавлен");
2024-06-17 00:13:00 +04:00
_logger.LogInformation($"Объект добавлен под индексом {res}");
2024-05-22 22:22:59 +04:00
pictureBox.Image = _company.Show();
}
2024-06-17 00:13:00 +04:00
catch (Exception ex)
2024-05-22 22:22:59 +04:00
{
2024-06-17 00:13:00 +04:00
MessageBox.Show($"Объект не добавлен: {ex.Message}", "Результат", MessageBoxButtons.OK,
MessageBoxIcon.Error);
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
2024-05-22 22:22:59 +04:00
}
2024-06-17 00:13:00 +04:00
2024-05-22 22:22:59 +04:00
}
2024-04-25 10:17:26 +04:00
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
2024-05-20 14:22:37 +04:00
2024-05-22 22:22:59 +04:00
private Color GetColor(Random random)
2024-04-25 10:17:26 +04:00
{
2024-05-22 22:22:59 +04:00
throw new NotImplementedException();
2024-04-25 10:17:26 +04:00
}
2024-05-22 22:22:59 +04:00
2024-05-20 14:22:37 +04:00
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDelBus_Click(object sender, EventArgs e)
2024-04-25 10:17:26 +04:00
{
if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null)
{
return;
}
2024-06-17 00:13:00 +04:00
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) !=
DialogResult.Yes)
2024-04-25 10:17:26 +04:00
{
return;
}
int pos = Convert.ToInt32(maskedTextBox.Text);
2024-06-17 00:13:00 +04:00
try
2024-04-25 10:17:26 +04:00
{
2024-06-17 00:13:00 +04:00
var res = _company - pos;
2024-04-25 10:17:26 +04:00
MessageBox.Show("Объект удален");
2024-06-17 00:13:00 +04:00
_logger.LogInformation($"Объект удален под индексом {pos}");
2024-04-25 10:17:26 +04:00
pictureBox.Image = _company.Show();
}
2024-06-17 00:13:00 +04:00
catch (Exception ex)
2024-04-25 10:17:26 +04:00
{
2024-06-17 00:13:00 +04:00
MessageBox.Show(ex.Message, "Не удалось удалить объект",
MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
2024-04-25 10:17:26 +04:00
}
}
2024-05-20 14:22:37 +04:00
/// <summary>
/// Перерисовка коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefresh_Click(object sender, EventArgs e)
2024-04-25 10:17:26 +04:00
{
if (_company == null)
2024-04-25 10:17:26 +04:00
{
return;
2024-04-25 10:17:26 +04:00
}
pictureBox.Image = _company.Show();
2024-04-25 10:17:26 +04:00
}
2024-05-20 14:22:37 +04:00
/// <summary>
/// Передача объекта в другую форму
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonGoToCheck_Click(object sender, EventArgs e)
2024-04-25 10:17:26 +04:00
{
if (_company == null)
{
return;
}
DrawningSimpleBus? simplebus = null;
int counter = 100;
while (simplebus == null)
{
simplebus = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
if (simplebus == null)
{
return;
}
FormBus form = new()
{
SetSimpleBus = simplebus
};
form.ShowDialog();
}
2024-05-20 14:22:37 +04:00
/// <summary>
/// Добавление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCollectionAdd_Click(object sender, EventArgs e)
{
2024-06-17 00:13:00 +04:00
if (string.IsNullOrEmpty(textBoxCollectionName.Text) ||
(!radioButtonList.Checked && !radioButtonMassive.Checked))
2024-05-20 14:22:37 +04:00
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
CollectionType collectionType = CollectionType.None;
if (radioButtonMassive.Checked)
{
collectionType = CollectionType.Massive;
}
else if (radioButtonList.Checked)
{
collectionType = CollectionType.List;
}
2024-06-17 00:13:00 +04:00
try
{
2024-06-17 14:21:52 +04:00
_storageCollection.AddCollection(new CollectionInfo(textBoxCollectionName.Text, collectionType, "ХЗ"));
2024-06-17 00:13:00 +04:00
_logger.LogInformation("Добавление коллекции");
RerfreshListBoxItems();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
}
2024-05-20 14:22:37 +04:00
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCollectionDel_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
2024-06-17 00:13:00 +04:00
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) !=
DialogResult.Yes)
2024-05-20 14:22:37 +04:00
{
return;
}
2024-06-17 00:13:00 +04:00
2024-06-17 14:21:52 +04:00
CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(listBoxCollection.SelectedItem.ToString()!);
_storageCollection.DelCollection(collectionInfo!);
2024-06-17 00:13:00 +04:00
_logger.LogInformation("Коллекция удалена");
2024-05-20 14:22:37 +04:00
RerfreshListBoxItems();
}
2024-06-17 00:13:00 +04:00
2024-05-20 14:22:37 +04:00
/// <summary>
/// Обновление списка в listBoxCollection
/// </summary>
private void RerfreshListBoxItems()
{
listBoxCollection.Items.Clear();
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
{
2024-06-17 14:21:52 +04:00
CollectionInfo? col = _storageCollection.Keys?[i];
if (!col!.IsEmpty())
2024-05-20 14:22:37 +04:00
{
2024-06-17 14:21:52 +04:00
listBoxCollection.Items.Add(col);
2024-05-20 14:22:37 +04:00
}
}
}
/// <summary>
/// Создание компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
2024-06-17 14:21:52 +04:00
ICollectionGenericObjects<DrawningSimpleBus>? collection =
_storageCollection[
CollectionInfo.GetCollectionInfo(listBoxCollection.SelectedItem.ToString()!) ??
new CollectionInfo("", CollectionType.None, "")];
2024-05-20 14:22:37 +04:00
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "хранилище":
2024-05-20 14:22:37 +04:00
_company = new BusStation(pictureBox.Width, pictureBox.Height, collection);
2024-06-17 00:13:00 +04:00
_logger.LogInformation("Компания создана");
2024-05-20 14:22:37 +04:00
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
2024-06-17 00:13:00 +04:00
/// <summary>
/// Обработка нажатия "Сохранение"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
2024-06-17 00:13:00 +04:00
try
{
2024-06-17 00:13:00 +04:00
_storageCollection.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Сохранение в файл: {filename}",
saveFileDialog.FileName);
}
2024-06-17 00:13:00 +04:00
catch (Exception ex)
{
2024-06-17 00:13:00 +04:00
MessageBox.Show(ex.Message, "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError($"Ошибка: {ex.Message}", ex.Message);
}
}
}
/// <summary>
/// Обработка нажатия "Загрузка"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
2024-06-17 00:13:00 +04:00
try
{
2024-06-17 00:13:00 +04:00
_storageCollection.LoadData(openFileDialog.FileName);
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation($"Загрузка прошла успешно в {openFileDialog.FileName}");
RerfreshListBoxItems();
}
2024-06-17 00:13:00 +04:00
catch (Exception ex)
{
2024-06-17 00:13:00 +04:00
MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK,
MessageBoxIcon.Error);
_logger.LogError("Ошибка: {Message}", ex.Message);
}
}
}
2024-06-17 14:21:52 +04:00
private void buttonSortByType_Click(object sender, EventArgs e)
{
CompareBus(new DrawningBusCompareByType());
}
private void buttonSortByColor_Click(object sender, EventArgs e)
{
CompareBus(new DrawningBusCompareByColor());
}
private void CompareBus(IComparer<DrawningSimpleBus?> comparer)
{
if (_company == null)
{
return;
}
_company.Sort(comparer);
pictureBox.Image = _company.Show();
}
2024-04-25 10:17:26 +04:00
}
2024-05-20 14:22:37 +04:00
2024-06-17 14:21:52 +04:00