From 86aefff19974d44c20a5b3f6fe9557b2af19dff3 Mon Sep 17 00:00:00 2001 From: mar-va Date: Sat, 20 Apr 2024 10:14:45 +0400 Subject: [PATCH] Lab6 --- .../StorageCollection.cs | 78 +++++++++---------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/StorageCollection.cs b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/StorageCollection.cs index f8a3f22..a34d8f3 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/CollectionGenericObjects/StorageCollection.cs @@ -50,8 +50,6 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - // TODO проверка, что name не пустой и нет в словаре записи с таким ключом - // TODO Прописать логику для добавления if (_storages.ContainsKey(name) || name == "") return; if (collectionType == CollectionType.Massive) @@ -69,7 +67,6 @@ public class StorageCollection /// Название коллекции public void DelCollection(string name) { - // TODO Прописать логику для удаления коллекции _storages.Remove(name); } @@ -82,7 +79,6 @@ public class StorageCollection { get { - // TODO Продумать логику получения объекта if (name == "") { return null; @@ -101,32 +97,34 @@ public class StorageCollection { File.Delete(filename); } - using FileStream fs = new(filename, FileMode.Create); - using StreamWriter sw = new StreamWriter(fs); - sw.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + using (StreamWriter writer = new(filename)) { - sw.Write(Environment.NewLine); - // не сохраняем пустые коллекции - if (value.Value.Count == 0) + writer.Write(_collectionKey); + foreach (KeyValuePair> value in _storages) { - continue; - } - sw.Write(value.Key); - 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)) + writer.Write(Environment.NewLine); + // не сохраняем пустые коллекции + if (value.Value.Count == 0) { continue; } - sw.Write(data); - sw.Write(_separatorItems); + 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()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + writer.Write(data); + writer.Write(_separatorItems); + } } } return true; @@ -138,47 +136,43 @@ public class StorageCollection { return false; } - - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader reader = new(filename)) { - using StreamReader sr = new StreamReader(fs); - - string str = sr.ReadLine(); - if (str == null || str.Length == 0) + string line = reader.ReadLine(); + if (line == null || line.Length == 0) { return false; } - - if (!str.Equals(_collectionKey)) + if (!line.Equals(_collectionKey)) { return false; } _storages.Clear(); - - while (!sr.EndOfStream) + while ((line = reader.ReadLine()) != null) { - string[] record = sr.ReadLine().Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + string[] record = line.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); if (record.Length != 4) { continue; } - CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); if (collection == null) { return false; } - collection.MaxCount = Convert.ToInt32(record[2]); - - string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + string[] set = record[3].Split(_separatorItems, + StringSplitOptions.RemoveEmptyEntries); 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; + } } } _storages.Add(record[0], collection);