diff --git a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs index 8555feb..18ecd0e 100644 --- a/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectMotorBoat/ProjectMotorBoat/CollectionGenericObjects/StorageCollection.cs @@ -54,7 +54,7 @@ public class StorageCollection { // проверка, что name не пустой и нет в словаре записи с таким ключом ✔ // Прописать логику для добавления ✔ - if (name != null && !_storages.ContainsKey(name)) + if (name != null && !_storages.ContainsKey(name)) { switch (collectionType) { @@ -103,7 +103,7 @@ public class StorageCollection /// Сохранение информации по автомобилям в хранилище в файл /// /// Путь и имя файла - public void SaveData(string filename) + public bool SaveData(string filename) { if (_storages.Count == 0) { @@ -156,11 +156,11 @@ public class StorageCollection /// Загрузка информации по автомобилям в хранилище из файла /// /// Путь и имя файла - public void LoadData(string filename) + public bool LoadData(string filename) { if (!File.Exists(filename)) { - throw new Exception("Файл не существует"); + return false; } using (StreamReader sr = new StreamReader(filename)) @@ -171,16 +171,10 @@ public class StorageCollection return false; } - string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - throw new Exception("В файле нет данных"); - } - - if (!strs[0].Equals(_collectionKey)) - { - throw new Exception("В файле неверные данные"); - } + if (!str[0].Equals(_collectionKey)) + { + return false; + } _storages.Clear(); string data = ""; @@ -192,30 +186,23 @@ public class StorageCollection continue; } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType) ?? - throw new Exception("Не удалось определить тип коллекции:" + record[1]); - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) - { - if (elem?.CreateDrawingBoat() is T boat) + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + collection.MaxCount = Convert.ToInt32(record[2]); + string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) { - try + if (elem?.CreateDrawingBoat() is T boat) { if (collection.Insert(boat) == -1) { - throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]); + return false; } } - catch (CollectionOverflowException ex) - { - throw new Exception("Коллекция переполнена", ex); - } } + _storages.Add(record[0], collection); } - _storages.Add(record[0], collection); + return true; } } @@ -233,4 +220,4 @@ public class StorageCollection _ => null, }; } -} +} \ No newline at end of file