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

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