diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs index ecb9eaf..fcbb498 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs @@ -10,6 +10,12 @@ namespace ProjectElectricLocomotive.Generics { internal class LocomotiveGenericCollection where T : DrawingLocomotive where U : IMoveableObject { + /// + /// Получение объектов коллекции + /// + public IEnumerable GetLocomotives => _collection.GetLocomotives(); + + //ширина/высота окна private readonly int _pictureWidth; private readonly int _pictureHeight; diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs index 63704a9..8c87e67 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs @@ -20,9 +20,9 @@ namespace ProjectElectricLocomotive.Generics /// public List Keys => _locomotivesStorage.Keys.ToList(); - + private readonly int _pictureWidth; - + private readonly int _pictureHeight; /// @@ -78,5 +78,112 @@ namespace ProjectElectricLocomotive.Generics return null; } } + + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _locomotivesStorage) + { + StringBuilder records = new(); + foreach (DrawingLocomotive? elem in record.Value.GetLocomotives) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new + UTF8Encoding(true).GetBytes($"LocomotiveStorage{Environment.NewLine}{data}"); + fs.Write(info, 0, info.Length); + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("LocomotiveStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _locomotivesStorage.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + LocomotiveGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingLocomotive? loco = + elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); + if (loco != null) + { + if ((collection + loco) != -1) // or vice versa + { + return false; + } + } + } + _locomotivesStorage.Add(record[0], collection); + } + return true; + } } }