From ff8cbd3ea60ae2bffa6085155ac108265deedb24 Mon Sep 17 00:00:00 2001 From: Whoisthatjulia Date: Fri, 29 Dec 2023 20:42:01 +0400 Subject: [PATCH] lab_8 --- .../AirFighter/AirFighterGenericStorage.cs | 88 ++++++++++++------- .../AirFighter/FormAirFighterCollection.cs | 2 +- AirFighter/AirFighter/appsettings.json | 2 +- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/AirFighter/AirFighter/AirFighterGenericStorage.cs b/AirFighter/AirFighter/AirFighterGenericStorage.cs index 843e080..3922efc 100644 --- a/AirFighter/AirFighter/AirFighterGenericStorage.cs +++ b/AirFighter/AirFighter/AirFighterGenericStorage.cs @@ -3,68 +3,82 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using AirFighter.DrawningObjects; -using AirFighter.MovementStrategy; -using AirFighter.Generics; -using System.IO; -using AirFighter.Exceptions; +using AirFighter.Drawnings; +using AirFighter.MovementStrategy; +using AirFighter.DrawningObjects; +using AirFighter.Exceptions; +using AirFighter.Exceptions; namespace AirFighter.Generics + { internal class AirFighterGenericStorage { - readonly Dictionary> _fighterStorages; - - public List Keys => _fighterStorages.Keys.ToList(); - + readonly Dictionary> _lincornStorages; + public List Keys => _lincornStorages.Keys.ToList(); private readonly int _pictureWidth; private readonly int _pictureHeight; + private static readonly char _separatorForKeyValue = '|'; + private readonly char _separatorRecords = ';'; + private static readonly char _separatorForObject = ':'; public AirFighterGenericStorage(int pictureWidth, int pictureHeight) { - _fighterStorages = new Dictionary>(); + _lincornStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } + /// + /// Добавление набора + /// + /// Название набора public void AddSet(string name) { // TODO Прописать логику для добавления - if (!_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) + if (!_lincornStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) { - var fighterCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); - _fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), fighterCollection); + var lincornCollection = new AirFighterGenericCollection(_pictureWidth, _pictureHeight); + _lincornStorages.Add(new AirFighterCollectionInfo(name, string.Empty), lincornCollection); } } - + /// + /// Удаление набора + /// + /// Название набора public void DelSet(string name) { // TODO Прописать логику для удаления - if (_fighterStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) + if (_lincornStorages.ContainsKey(new AirFighterCollectionInfo(name, string.Empty))) { - _fighterStorages.Remove(new AirFighterCollectionInfo(name, string.Empty)); + _lincornStorages.Remove(new AirFighterCollectionInfo(name, string.Empty)); } } - + /// + /// Доступ к набору + /// + /// + /// public AirFighterGenericCollection? this[string ind] { get { AirFighterCollectionInfo indObj = new AirFighterCollectionInfo(ind, string.Empty); // TODO Продумать логику получения набора - if (_fighterStorages.ContainsKey(indObj)) + if (_lincornStorages.ContainsKey(indObj)) { - return _fighterStorages[indObj]; + return _lincornStorages[indObj]; } return null; } } - - private static readonly char _separatorForKeyValue = '|'; - - private readonly char _separatorRecords = ';'; - - private static readonly char _separatorForObject = ':'; + /// + /// Сохранение информации по установкам в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных public void SaveData(string filename) { if (File.Exists(filename)) @@ -72,7 +86,7 @@ namespace AirFighter.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _fighterStorages) + foreach (KeyValuePair> record in _lincornStorages) { StringBuilder records = new(); foreach (DrawningAirFighter? elem in record.Value.GetAirFighter) @@ -88,15 +102,22 @@ namespace AirFighter.Generics using (StreamWriter writer = new StreamWriter(filename)) { - writer.Write($"fighterStorage{Environment.NewLine}{data}"); + writer.Write($"lincornStorage{Environment.NewLine}{data}"); } } + + // + /// Загрузка информации по установкам в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных public void LoadData(string filename) { if (!File.Exists(filename)) { throw new Exception("Файл не найден"); } + using (StreamReader reader = new StreamReader(filename)) { string cheker = reader.ReadLine(); @@ -104,11 +125,11 @@ namespace AirFighter.Generics { throw new Exception("Нет данных для загрузки"); } - if (!cheker.StartsWith("fighterStorage")) + if (!cheker.StartsWith("lincornStorage")) { throw new Exception("Неверный формат ввода"); } - _fighterStorages.Clear(); + _lincornStorages.Clear(); string strs; bool firstinit = true; while ((strs = reader.ReadLine()) != null) @@ -126,11 +147,11 @@ namespace AirFighter.Generics AirFighterGenericCollection collection = new(_pictureWidth, _pictureHeight); foreach (string data in strs.Split(_separatorForKeyValue)[1].Split(_separatorRecords)) { - DrawningAirFighter? fighter = + DrawningAirFighter? lincorn = data?.CreateDrawningAirFighter(_separatorForObject, _pictureWidth, _pictureHeight); - if (fighter != null) + if (lincorn != null) { - try { _ = collection + fighter; } + try { _ = collection + lincorn; } catch (AirFighterNotFoundException e) { throw e; @@ -141,9 +162,10 @@ namespace AirFighter.Generics } } } - _fighterStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection); + _lincornStorages.Add(new AirFighterCollectionInfo(name, string.Empty), collection); } } } } } + diff --git a/AirFighter/AirFighter/FormAirFighterCollection.cs b/AirFighter/AirFighter/FormAirFighterCollection.cs index dbee92e..16f0049 100644 --- a/AirFighter/AirFighter/FormAirFighterCollection.cs +++ b/AirFighter/AirFighter/FormAirFighterCollection.cs @@ -38,7 +38,7 @@ namespace AirFighter listBoxStorage.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorage.Items.Add(_storage.Keys[i]); + listBoxStorage.Items.Add(_storage.Keys[i].Name); } if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count)) diff --git a/AirFighter/AirFighter/appsettings.json b/AirFighter/AirFighter/appsettings.json index c9ceae7..66e218d 100644 --- a/AirFighter/AirFighter/appsettings.json +++ b/AirFighter/AirFighter/appsettings.json @@ -14,7 +14,7 @@ ], "Enrich": [ "FromLogContext", "WithShipName", "WithThreadId" ], "Properties": { - "Application": "ContainerShip" + "Application": "AirFighter" } } } \ No newline at end of file