52 lines
1.1 KiB
C#
52 lines
1.1 KiB
C#
|
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;
|
|||
|
}
|
|||
|
}
|