исправленное

This commit is contained in:
Ekaterina23578 2024-05-11 15:57:19 +04:00
parent a8bcb964fe
commit b4c1796f23

View File

@ -96,7 +96,7 @@ public class StorageCollection<T>
} }
/// <summary> /// <summary>
/// Сохранение информации по автомобилям в хранилище в файл /// Сохранение информации по бомбардировщикам в хранилище в файл
/// </summary> /// </summary>
/// <param name="filename">Путь и имя файла</param> /// <param name="filename">Путь и имя файла</param>
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns> /// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
@ -112,24 +112,23 @@ public class StorageCollection<T>
File.Delete(filename); File.Delete(filename);
} }
StringBuilder sb = new(); using StreamWriter writer = new StreamWriter(filename);
writer.Write(_collectionKey);
sb.Append(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages) foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{ {
sb.Append(Environment.NewLine); writer.Write(Environment.NewLine);
// не сохраняем пустые коллекции // не сохраняем пустые коллекции
if (value.Value.Count == 0) if (value.Value.Count == 0)
{ {
continue; continue;
} }
sb.Append(value.Key); writer.Write(value.Key);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
sb.Append(value.Value.GetCollectionType); writer.Write(value.Value.GetCollectionType);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
sb.Append(value.Value.MaxCount); writer.Write(value.Value.MaxCount);
sb.Append(_separatorForKeyValue); writer.Write(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems()) foreach (T? item in value.Value.GetItems())
{ {
@ -139,14 +138,11 @@ public class StorageCollection<T>
continue; continue;
} }
sb.Append(data); writer.Write(data);
sb.Append(_separatorItems); writer.Write(_separatorItems);
} }
} }
using FileStream fs = new(filename, FileMode.Create);
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
fs.Write(info, 0, info.Length);
return true; return true;
} }
@ -162,53 +158,51 @@ public class StorageCollection<T>
return false; return false;
} }
using (StreamReader reader = File.OpenText(filename)) using StreamReader reader = File.OpenText(filename);
string? str = reader.ReadLine();
if (str == null || str.Length == 0)
{ {
string? str = reader.ReadLine(); return false;
if (str == null || str.Length == 0) }
if (!str.StartsWith(_collectionKey))
{
return false;
}
_storages.Clear();
string? strs = "";
while ((strs = reader.ReadLine()) != null)
{
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{
continue;
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{ {
return false; return false;
} }
if (!str.StartsWith(_collectionKey)) collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{ {
return false; if (elem?.CreateDrawningBomber() is T bomber)
}
_storages.Clear();
string? strs = "";
while ((strs = reader.ReadLine()) != null)
{
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4)
{ {
continue; if (collection.Insert(bomber) == -1)
}
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null)
{
return false;
}
collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set)
{
if (elem?.CreateDrawningBomber() is T bomber)
{ {
if (collection.Insert(bomber) == -1) return false;
{
return false;
}
} }
} }
_storages.Add(record[0], collection);
} }
_storages.Add(record[0], collection);
return true;
} }
return true;
} }
/// <summary> /// <summary>