PIbd22_NikiforovaMV_Contain.../ContainerShip/ShipsGenericStorage.cs

148 lines
5.4 KiB
C#
Raw Permalink Normal View History

2023-12-16 11:14:14 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContainerShip.MovementStrategy;
using ContainerShip.DrawningObjects;
2023-12-29 22:18:15 +04:00
using ContainerShip.Entities;
2023-12-16 11:14:14 +04:00
namespace ContainerShip.Generic
{
internal class ShipsGenericStorage
{
2023-12-16 11:18:38 +04:00
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
2023-12-16 11:14:14 +04:00
readonly Dictionary<string, ShipsGenericCollection<DrawningShip, DrawingObjectShip>> _shipStorages;
public List<string> Keys => _shipStorages.Keys.ToList();
private readonly int _pictureWidth;
2023-12-16 11:18:38 +04:00
2023-12-16 11:14:14 +04:00
private readonly int _pictureHeight;
public ShipsGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new Dictionary<string,
ShipsGenericCollection<DrawningShip, DrawingObjectShip>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
foreach (string nameStorage in Keys)
{
if (nameStorage == name)
{
MessageBox.Show("Набор с заданным именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
_shipStorages.Add(name, new ShipsGenericCollection<DrawningShip, DrawingObjectShip>(_pictureWidth, _pictureHeight));
}
2023-12-16 11:18:38 +04:00
2023-12-16 11:14:14 +04:00
public void DelSet(string name)
{
if (_shipStorages.ContainsKey(name))
{
_shipStorages.Remove(name);
}
}
2023-12-16 11:18:38 +04:00
public bool SaveData(string filename)
{
2023-12-29 22:18:15 +04:00
if (_shipStorages.Count == 0)
{
throw new InvalidOperationException("Невалидная операция, нет данных для сохранения");
}
2023-12-16 11:18:38 +04:00
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter sw = File.CreateText(filename))
{
sw.WriteLine($"ShipStorage");
2023-12-29 22:18:15 +04:00
2023-12-16 11:18:38 +04:00
foreach (var record in _shipStorages)
{
StringBuilder records = new();
2023-12-29 22:18:15 +04:00
if (record.Value.count <= 0)
{
throw new InvalidOperationException("Невалидная операция, нет данных для сохранения");
}
2023-12-16 11:18:38 +04:00
foreach (DrawningShip? elem in record.Value.GetShip)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
sw.WriteLine($"{record.Key}{_separatorForKeyValue}{records}");
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
2023-12-29 22:18:15 +04:00
throw new FileNotFoundException($"Файл {filename} не найден");
2023-12-16 11:18:38 +04:00
}
using (StreamReader sr = File.OpenText(filename))
{
string? curLine = sr.ReadLine();
2023-12-29 22:18:15 +04:00
if (curLine == null || curLine.Length == 0 || !curLine.StartsWith("ShipStorage"))
2023-12-16 11:18:38 +04:00
{
2023-12-29 22:18:15 +04:00
throw new ArgumentException("Неверный формат данных");
2023-12-16 11:18:38 +04:00
}
_shipStorages.Clear();
curLine = sr.ReadLine();
2023-12-29 22:18:15 +04:00
if (curLine == null || curLine.Length == 0)
{
throw new ArgumentException("Нет данных");
}
2023-12-16 11:18:38 +04:00
while (curLine != null)
{
2023-12-29 22:18:15 +04:00
if (!curLine.Contains(_separatorRecords))
{
throw new ArgumentException("Коллекция пуста");
}
2023-12-16 11:18:38 +04:00
string[] record = curLine.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
ShipsGenericCollection<DrawningShip, DrawingObjectShip> collection = new(_pictureWidth, _pictureHeight);
2023-12-29 22:18:15 +04:00
2023-12-16 11:18:38 +04:00
string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawningShip? ship = elem?.CreateDrawningShip(_separatorForObject, _pictureWidth, _pictureHeight);
if (ship != null)
{
if (collection + ship == -1)
{
2023-12-29 22:18:15 +04:00
throw new InvalidOperationException("Невалидная операция, ошибка добавления в коллекцию");
2023-12-16 11:18:38 +04:00
}
}
}
_shipStorages.Add(record[0], collection);
curLine = sr.ReadLine();
}
}
return true;
}
2023-12-16 11:14:14 +04:00
public ShipsGenericCollection<DrawningShip, DrawingObjectShip>? this[string ind]
{
get
{
if (_shipStorages.ContainsKey(ind))
{
return _shipStorages[ind];
}
return null;
}
}
}
}