PIbd-23_Vrazhkin_S_A_Electr.../lab1/Generics/LocosGenericStorage.cs

138 lines
5.0 KiB
C#
Raw Normal View History

2023-11-28 12:26:51 +04:00
using System.Text;
2023-10-31 13:25:13 +04:00
2023-11-28 12:26:51 +04:00
namespace ElectricLocomotive;
public class LocosGenericStorage
2023-10-31 13:25:13 +04:00
{
2023-12-12 13:58:32 +04:00
readonly Dictionary<LocomotivCollectionInfo, LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>> _electricLocoStorages;
public List<LocomotivCollectionInfo> Keys => _electricLocoStorages.Keys.ToList();
2023-10-31 13:25:13 +04:00
private readonly int _pictureWidth;
private readonly int _pictureHeight;
2023-11-28 12:26:51 +04:00
private static readonly char _separatorForKeyValue = '|';
private readonly char _separatorRecords = ';';
private static readonly char _separatorForObject = ':';
2023-10-31 13:25:13 +04:00
public LocosGenericStorage(int pictureWidth, int pictureHeight)
{
2023-12-12 13:58:32 +04:00
_electricLocoStorages = new Dictionary<LocomotivCollectionInfo,
2023-10-31 13:25:13 +04:00
LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
2023-12-12 13:58:32 +04:00
_electricLocoStorages.Add(new LocomotivCollectionInfo(name, string.Empty), new LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv> (_pictureWidth, _pictureHeight));
2023-10-31 13:25:13 +04:00
}
public void DelSet(string name)
{
2023-12-12 13:58:32 +04:00
if (!_electricLocoStorages.ContainsKey(new LocomotivCollectionInfo(name, string.Empty)))
2023-10-31 13:25:13 +04:00
return;
2023-12-12 13:58:32 +04:00
_electricLocoStorages.Remove(new LocomotivCollectionInfo(name, string.Empty));
2023-10-31 13:25:13 +04:00
}
public LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>? this[string ind]
{
get
{
2023-12-12 13:58:32 +04:00
LocomotivCollectionInfo indObj = new LocomotivCollectionInfo(ind, string.Empty);
if (_electricLocoStorages.ContainsKey(indObj))
return _electricLocoStorages[indObj];
2023-10-31 13:25:13 +04:00
return null;
}
}
2023-11-28 12:26:51 +04:00
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
StringBuilder data = new();
2023-12-12 13:58:32 +04:00
foreach (KeyValuePair<LocomotivCollectionInfo,
2023-11-28 12:26:51 +04:00
LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>> record in _electricLocoStorages)
{
StringBuilder records = new();
foreach (DrawingLocomotiv? elem in record.Value.GetLocos)
{
records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}");
}
data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}");
}
if (data.Length == 0)
{
2023-11-28 13:43:14 +04:00
throw new Exception("Невалиданя операция, нет данных длясохранения");
2023-11-28 12:26:51 +04:00
}
string toWrite = $"LocoStorage{Environment.NewLine}{data}";
var strs = toWrite.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
using (StreamWriter sw = new(filename))
{
foreach (var str in strs)
{
sw.WriteLine(str);
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
2023-11-28 13:43:14 +04:00
throw new Exception("Файл не найден");
2023-11-28 12:26:51 +04:00
}
using (StreamReader sr = new(filename))
{
string str = sr.ReadLine();
var strs = str.Split(new char[] { '\n', '\r' },
StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0)
{
2023-11-28 13:43:14 +04:00
throw new Exception("Нет данных для загрузки");
2023-11-28 12:26:51 +04:00
}
if (!strs[0].StartsWith("LocoStorage"))
{
2023-11-28 13:43:14 +04:00
throw new Exception("Неверный формат данных");
2023-11-28 12:26:51 +04:00
}
_electricLocoStorages.Clear();
do
{
string[] record = str.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 2)
{
str = sr.ReadLine();
continue;
}
LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>
collection = new(_pictureWidth, _pictureHeight);
string[] set = record[1].Split(_separatorRecords,
StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
DrawingLocomotiv? monorail =
elem?.CreateDrawingLoco(_separatorForObject, _pictureWidth, _pictureHeight);
if (monorail != null)
{
if (!(collection + monorail))
{
2023-11-28 13:43:14 +04:00
throw new Exception("Ошибка добавления в коллекцию");
2023-11-28 12:26:51 +04:00
}
}
}
2023-12-12 13:58:32 +04:00
_electricLocoStorages.Add(new LocomotivCollectionInfo(record[0], string.Empty), collection);
2023-11-28 12:26:51 +04:00
str = sr.ReadLine();
} while (str != null);
}
return true;
}
2023-10-31 13:25:13 +04:00
}