CourseWorkKnapsack/forms/MainForm.cs

165 lines
5.3 KiB
C#
Raw Normal View History

2024-05-20 02:52:44 +04:00
using ProjectKnapsack.classes;
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;
2024-05-29 02:26:53 +04:00
using static ProjectKnapsack.classes.KnapsackParameters;
2024-05-20 02:52:44 +04:00
namespace ProjectKnapsack.forms;
public partial class MainForm : Form
{
private KnapsackVisualizer visualization;
private KnapsackManager manager;
private Storage storage;
private int currentStep;
2024-05-29 02:26:53 +04:00
private List<Item> items; // Добавление списка предметов
2024-05-20 02:52:44 +04:00
public MainForm()
{
InitializeComponent();
visualization = new KnapsackVisualizer();
2024-05-29 02:26:53 +04:00
items = new List<Item>(); // Инициализация списка предметов
2024-05-20 02:52:44 +04:00
}
private void startButton_Click(object sender, EventArgs e)
{
var parametersForm = new ParametersForm();
if (parametersForm.ShowDialog() == DialogResult.OK)
{
var parameters = parametersForm.Parameters;
if (parameters != null) // Добавьте проверку на null перед использованием параметров
{
manager = new KnapsackManager(parameters);
manager.Execute();
storage = manager.Storage;
currentStep = 0;
2024-05-29 02:26:53 +04:00
items = parameters.Items.ToList(); // Инициализация списка предметов из параметров
2024-05-20 02:52:44 +04:00
DisplayCurrentState();
}
else
{
MessageBox.Show("Parameters are null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void DisplayCurrentState()
{
2024-05-29 02:26:53 +04:00
//if (storage != null && visualization != null)
//{
// var state = storage.GetState(currentStep);
// if (state != null)
// {
// visualization.Visualize(state.Items, state.Capacity, this);
// }
// else
// {
// MessageBox.Show("State is null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// }
//}
2024-05-20 02:52:44 +04:00
if (storage != null && visualization != null)
{
var state = storage.GetState(currentStep);
if (state != null)
{
2024-05-29 02:26:53 +04:00
visualization.Visualize(state.Items, state.Capacity, state.CurrentWeight, state.CurrentValue, this);
2024-05-20 02:52:44 +04:00
}
else
{
MessageBox.Show("State is null.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void saveMenuItem_Click(object sender, EventArgs e)
{
if (storage != null)
{
using (var saveFileDialog = new SaveFileDialog())
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
storage.SaveToFile(saveFileDialog.FileName);
}
}
}
else
{
MessageBox.Show("No data to save.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void loadMenuItem_Click(object sender, EventArgs e)
{
using (var openFileDialog = new OpenFileDialog())
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
storage = new Storage();
storage.LoadFromFile(openFileDialog.FileName);
currentStep = 0;
DisplayCurrentState();
}
}
}
private void aboutMenuItem_Click(object sender, EventArgs e)
{
InformationForm informationForm = new InformationForm();
informationForm.Show();
}
2024-05-29 02:26:53 +04:00
private void nextStepButton_Click(object sender, EventArgs e)
{
//if (currentStep < storage.StateCount - 1)
//{
// currentStep++;
// DisplayCurrentState();
//}
if (currentStep < storage.StateCount - 1)
{
currentStep++;
var state = storage.GetState(currentStep);
if (state != null)
{
if (state.Items.Count > currentStep)
{
var addedItem = state.Items[currentStep];
items.Remove(addedItem); // Удаление добавленного предмета из списка предметов
DisplayCurrentState();
}
else
{
MessageBox.Show("Следующего шага нет");
return;
}
}
}
}
private void prevStepButton_Click(object sender, EventArgs e)
{
//if (currentStep > 0)
//{
// currentStep--;
// DisplayCurrentState();
//}
if (currentStep > 0)
{
currentStep--;
var state = storage.GetState(currentStep);
if (state != null)
{
var addedItem = state.Items[currentStep];
items.Add(addedItem); // Добавление предмета обратно в список предметов
DisplayCurrentState();
}
}
}
2024-05-20 02:52:44 +04:00
}