This commit is contained in:
Pyro 2024-06-17 00:34:43 +04:00
parent 8a3e041126
commit 847ecf21d8

View File

@ -54,7 +54,7 @@ public class StorageCollection<T>
{
// проверка, что name не пустой и нет в словаре записи с таким ключом ✔
// Прописать логику для добавления ✔
if (name != null && !_storages.ContainsKey(name))
if (name != null && !_storages.ContainsKey(name))
{
switch (collectionType)
{
@ -103,7 +103,7 @@ public class StorageCollection<T>
/// Сохранение информации по автомобилям в хранилище в файл
/// </summary>
/// <param name="filename">Путь и имя файла</param>
public void SaveData(string filename)
public bool SaveData(string filename)
{
if (_storages.Count == 0)
{
@ -156,11 +156,11 @@ public class StorageCollection<T>
/// Загрузка информации по автомобилям в хранилище из файла
/// </summary>
/// <param name="filename">Путь и имя файла</param>
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<T>
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<T>
continue;
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.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<T>? collection = StorageCollection<T>.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<T>
_ => null,
};
}
}
}