Лістинг Saver.CS using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Win32;
namespace OOP_LAB2
{
public class Saver
{
private Saver()
{
}
private static Saver instance;
string path = @"C:\Users\38098\Desktop\патерни\lab02\Програмний продукт\PATTERN_LAB02\matrix.txt";
private FileStream sw;
public void OpenFile()
{
if (sw!=null)
sw.Close();
sw = new FileStream(path, FileMode.OpenOrCreate);
}
public List ReadAndDeserialize()
{
OpenFile();
List listdis;
if (sw.Length > 0)
{
BinaryFormatter bf = new BinaryFormatter();
listdis = (List)bf.Deserialize(sw);
return listdis;
}
return new List();
}
public void SerializeAndSave( List data)
{
OpenFile();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(sw, data);
}
public void WriteMatrix(Matrix a)
{
List list = ReadAndDeserialize();
list.Add(a);
SerializeAndSave(list);
}
public Matrix SearchById(int id)
{
List list = ReadAndDeserialize();
if (id >= 0 && id < list.Count)
return list[id];
return null;
}
public void Replace(int id, Matrix matrix)
{
List list = ReadAndDeserialize();
if (list[id].X < matrix.X || list[id].Y < matrix.Y)
{
list.Add(matrix);
}
else
{
list[id] = matrix;
}
SerializeAndSave(list);
}
public void Clear()
{
sw.SetLength(0);
}
public void DeleteMatrix(int id)
{
List list = ReadAndDeserialize();
if (id >= 0 && id < list.Count)
list.RemoveAt(id);
SerializeAndSave(list);
}
public static Saver getInstance()
{
if (instance == null)
instance = new Saver();
return instance;
}
}
}