CourseWorkKnapsack/classes/Storage.cs

52 lines
1.1 KiB
C#
Raw Normal View History

2024-05-20 02:52:44 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.IO;
namespace ProjectKnapsack.classes;
public class Storage
{
private List<KnapsackState> states;
private int currentPosition;
public Storage()
{
states = new List<KnapsackState>();
currentPosition = 0;
}
public void AddState(KnapsackState state)
{
states.Add(state);
}
public KnapsackState GetState(int index)
{
return states[index];
}
public int StateCount => states.Count;
public void SaveToFile(string filename)
{
var options = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(states, options);
File.WriteAllText(filename, json);
}
public void LoadFromFile(string filename)
{
var json = File.ReadAllText(filename);
states = JsonSerializer.Deserialize<List<KnapsackState>>(json);
}
public void Reset()
{
currentPosition = 0;
}
}