This commit is contained in:
mar-va 2024-04-20 10:14:45 +04:00
parent 2365620cf6
commit 86aefff199

View File

@ -50,8 +50,6 @@ public class StorageCollection<T>
/// <param name="collectionType">тип коллекции</param> /// <param name="collectionType">тип коллекции</param>
public void AddCollection(string name, CollectionType collectionType) public void AddCollection(string name, CollectionType collectionType)
{ {
// TODO проверка, что name не пустой и нет в словаре записи с таким ключом
// TODO Прописать логику для добавления
if (_storages.ContainsKey(name) || name == "") return; if (_storages.ContainsKey(name) || name == "") return;
if (collectionType == CollectionType.Massive) if (collectionType == CollectionType.Massive)
@ -69,7 +67,6 @@ public class StorageCollection<T>
/// <param name="name">Название коллекции</param> /// <param name="name">Название коллекции</param>
public void DelCollection(string name) public void DelCollection(string name)
{ {
// TODO Прописать логику для удаления коллекции
_storages.Remove(name); _storages.Remove(name);
} }
@ -82,7 +79,6 @@ public class StorageCollection<T>
{ {
get get
{ {
// TODO Продумать логику получения объекта
if (name == "") if (name == "")
{ {
return null; return null;
@ -101,32 +97,34 @@ public class StorageCollection<T>
{ {
File.Delete(filename); File.Delete(filename);
} }
using FileStream fs = new(filename, FileMode.Create); using (StreamWriter writer = new(filename))
using StreamWriter sw = new StreamWriter(fs);
sw.Write(_collectionKey);
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
{ {
sw.Write(Environment.NewLine); writer.Write(_collectionKey);
// не сохраняем пустые коллекции foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
if (value.Value.Count == 0)
{ {
continue; writer.Write(Environment.NewLine);
} // не сохраняем пустые коллекции
sw.Write(value.Key); if (value.Value.Count == 0)
sw.Write(_separatorForKeyValue);
sw.Write(value.Value.GetCollectionType);
sw.Write(_separatorForKeyValue);
sw.Write(value.Value.MaxCount);
sw.Write(_separatorForKeyValue);
foreach (T? item in value.Value.GetItems())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{ {
continue; continue;
} }
sw.Write(data); writer.Write(value.Key);
sw.Write(_separatorItems); 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())
{
string data = item?.GetDataForSave() ?? string.Empty;
if (string.IsNullOrEmpty(data))
{
continue;
}
writer.Write(data);
writer.Write(_separatorItems);
}
} }
} }
return true; return true;
@ -138,47 +136,43 @@ public class StorageCollection<T>
{ {
return false; return false;
} }
using (StreamReader reader = new(filename))
using (FileStream fs = new(filename, FileMode.Open))
{ {
using StreamReader sr = new StreamReader(fs); string line = reader.ReadLine();
if (line == null || line.Length == 0)
string str = sr.ReadLine();
if (str == null || str.Length == 0)
{ {
return false; return false;
} }
if (!line.Equals(_collectionKey))
if (!str.Equals(_collectionKey))
{ {
return false; return false;
} }
_storages.Clear(); _storages.Clear();
while ((line = reader.ReadLine()) != null)
while (!sr.EndOfStream)
{ {
string[] record = sr.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); string[] record = line.Split(_separatorForKeyValue,
StringSplitOptions.RemoveEmptyEntries);
if (record.Length != 4) if (record.Length != 4)
{ {
continue; continue;
} }
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType); ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
if (collection == null) if (collection == null)
{ {
return false; return false;
} }
collection.MaxCount = Convert.ToInt32(record[2]); collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems,
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries);
foreach (string elem in set) foreach (string elem in set)
{ {
if (elem?.CreateDrawningBus() is T bus) if (elem?.CreateDrawningBus() is T truck)
{ {
if (collection.Insert(bus) == -1) if (collection.Insert(truck) == -1)
{
return false; return false;
}
} }
} }
_storages.Add(record[0], collection); _storages.Add(record[0], collection);