diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs index 5fbb369..25bb826 100644 --- a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs @@ -1,5 +1,4 @@ -using ProjectTrolleybus.Exceptions; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -16,12 +15,12 @@ public class StorageCollection /// /// Словарь (хранилище) с коллекциями /// - readonly Dictionary> _storages; + readonly Dictionary> _storages; /// /// Возвращение списка названий коллекций /// - public List Keys => _storages.Keys.ToList(); + public List Keys => _storages.Keys.ToList(); /// /// Ключевое слово, с которого должен начинаться файл @@ -43,7 +42,7 @@ public class StorageCollection /// public StorageCollection() { - _storages = new Dictionary>(); + _storages = new Dictionary>(); } /// /// Добавление коллекции в хранилище @@ -52,36 +51,29 @@ public class StorageCollection /// тип коллекции public void AddCollection(string name, CollectionType collectionType) { - CollectionInfo collectionInfo = new(name, collectionType, string.Empty); - if (name == null || _storages.ContainsKey(collectionInfo)) - return; - switch (collectionType) + if (_storages.ContainsKey(name) && name == "") { - case CollectionType.None: - return; - case CollectionType.Massive: - _storages[collectionInfo] = new MassiveGenericObjects(); - return; - case CollectionType.List: - _storages[collectionInfo] = new ListGenericObjects(); - return; - default: - break; + return; } + if (collectionType == CollectionType.Massive) + { + _storages[name] = new MassiveGenericObjects(); + } + else + { + _storages[name] = new ListGenericObjects(); + } } + /// /// Удаление коллекции /// /// Название коллекции public void DelCollection(string name) { - CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); - if (_storages.ContainsKey(collectionInfo)) - { - _storages.Remove(collectionInfo); - } - } + _storages.Remove(name); +} /// /// Доступ к коллекции /// @@ -91,12 +83,11 @@ public class StorageCollection { get { - CollectionInfo collectionInfo = new CollectionInfo(name, CollectionType.None, string.Empty); - if (_storages.ContainsKey(collectionInfo)) + if (name == "") { - return _storages[collectionInfo]; + return null; } - return null; + return _storages[name]; } } @@ -111,13 +102,10 @@ public class StorageCollection { File.Delete(filename); } - using (StreamWriter writer = new(filename)) - - { writer.Write(_collectionKey); - foreach (KeyValuePair> value in _storages) + foreach (KeyValuePair> value in _storages) { writer.Write(Environment.NewLine); // не сохраняем пустые коллекции @@ -153,38 +141,38 @@ public class StorageCollection { throw new FileNotFoundException("Файл не существует!"); } - - using (StreamReader sr = new(filename)) + using (StreamReader reader = new(filename)) { - string? line = sr.ReadLine(); - + string line = reader.ReadLine(); if (line == null || line.Length == 0) { throw new ArgumentException("В файле нет данных"); } - - if (line != _collectionKey) + if (!line.Equals(_collectionKey)) { - throw new InvalidDataException("В файле неверные данные"); + throw new InvalidDataException("В файле неверные данные"); } - _storages.Clear(); - - while ((line = sr.ReadLine()) != null) + while ((line = reader.ReadLine()) != null) { - string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); - - if (record.Length != 3) + string[] record = line.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) { continue; } - CollectionInfo? collectionInfo = CollectionInfo.GetCollectionInfo(record[0]) ?? throw new ArgumentNullException("Не удалось определить тип коллекции: " + record[0]); - ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionInfo.CollectionType) ?? throw new ArgumentNullException("Не удалось создать коллекцию"); - collection.MaxCount = Convert.ToInt32(record[1]); - string[] set = record[2].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + if (collection == null) + { + throw new InvalidCastException("Не удалось определить тип коллекции: " + record[1]); + } + collection.MaxCount = Convert.ToInt32(record[2]); + string[] set = record[3].Split(_separatorItems, + StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { - if (elem?.CreateDrawningBus() is T bus) + if (elem?.CreateDrawningBus() is T bus ) { try { @@ -201,9 +189,10 @@ public class StorageCollection } } - _storages.Add(collectionInfo, collection); + _storages.Add(record[0], collection); } } + } /// @@ -211,7 +200,8 @@ public class StorageCollection /// /// /// - private static ICollectionGenericObjects?CreateCollection(CollectionType collectionType) + private static ICollectionGenericObjects? + CreateCollection(CollectionType collectionType) { return collectionType switch {