From 57e628eca5e96069f7907ddb2e8ad4e54a46b2d7 Mon Sep 17 00:00:00 2001 From: Extrimal Date: Tue, 12 Dec 2023 18:38:26 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D1=83=D0=BF=D0=B5=D1=80=20=D0=BF=D1=83?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=20=D0=B7=D0=B0=D0=B2=D0=B5=D1=80=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0=207?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftsGenericCollection.cs | 6 +- .../AircraftsGenericStorage.cs | 65 ++++++++----------- .../AircraftCarrier/FormAircraftCollection.cs | 40 ++++++------ 3 files changed, 48 insertions(+), 63 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs b/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs index 6c104eb..a6dea35 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs +++ b/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs @@ -47,11 +47,7 @@ namespace AircraftCarrier.Generics public static bool operator -(AircraftsGenericCollection collect, int pos) { T? obj = collect._collection[pos]; - if (obj != null) - { - return collect._collection.Remove(pos); - } - return false; + return collect._collection.Remove(pos); } /// Получение объекта IMoveableObject public U? GetU(int pos) diff --git a/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs b/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs index 14b75d3..1512970 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs +++ b/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs @@ -87,7 +87,7 @@ namespace AircraftCarrier.Generics } using FileStream fs = new(filename, FileMode.Create); byte[] info = new - UTF8Encoding(true).GetBytes($"CarStorage{Environment.NewLine}{data}"); + UTF8Encoding(true).GetBytes($"AircraftStorage{Environment.NewLine}{data}"); fs.Write(info, 0, info.Length); return; } @@ -98,54 +98,45 @@ namespace AircraftCarrier.Generics { throw new FileNotFoundException("Файл не найден"); } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader sr = new(filename)) { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + string str = sr.ReadLine(); + if (str == null || str.Length == 0) { - bufferTextFromFile += temp.GetString(b); + throw new NullReferenceException("Нет данных для загрузки"); } - } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) - { - throw new FileNotFoundException("Нет данных для загрузки"); - } - if (!strs[0].StartsWith("CarStorage")) - { - //если нет такой записи, то это не те данные - throw new FileNotFoundException("Неверный формат данных"); - } - _AircraftStorages.Clear(); - foreach (string data in strs) - { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) + if (!str.StartsWith("AircraftStorage")) { - continue; + //если нет такой записи, то это не те данные + throw new InvalidDataException("Неверный формат данных"); } - AircraftsGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords,StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) + _AircraftStorages.Clear(); + while ((str = sr.ReadLine()) != null) { - DrawningAircraft? aircraft = - elem?.CreateDrawningAircraft(_separatorForObject, _pictureWidth, _pictureHeight); - if (aircraft != null) + string[] record = str.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) { - if (collection + aircraft == -1) + continue; + } + AircraftsGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set.Reverse()) + { + DrawningAircraft? aircraft = elem?.CreateDrawningAircraft(_separatorForObject, _pictureWidth, _pictureHeight); + if (aircraft != null) { - throw new ApplicationException("Ошибка добавления в коллекцию"); + if (collection + aircraft == -1) + { + throw new ApplicationException("Ошибка добавления в коллекцию"); + } } } + _AircraftStorages.Add(record[0], collection); } - _AircraftStorages.Add(record[0], collection); } - } + + + } } diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs index a3f61ac..7793eca 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs @@ -149,7 +149,7 @@ namespace AircraftCarrier } else { - MessageBox.Show("Неудачная попытка удалить объект"); + MessageBox.Show("Неудачная попытка удалить объект"); } } catch (AircraftNotFoundException ex) @@ -175,41 +175,39 @@ namespace AircraftCarrier /// Обработка нажатия "Сохранение" private void SaveToolStripMenuItem_Click(object sender, EventArgs e) { - if (saveFileDialog.ShowDialog() == DialogResult.OK) + try { - try - { - _storage.SaveData(saveFileDialog.FileName); - MessageBox.Show("Сохранение прошло успешно", - "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - _logger.LogInformation($"Файл успешно сохранен {saveFileDialog.FileName}"); - } - catch (Exception ex) - { - MessageBox.Show($"Сохранить не удалось: {ex.Message}", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Error); - _logger.LogWarning($"Не удалось сохранить файл: {ex.Message}"); - } + _storage.SaveData(saveFileDialog.FileName); + MessageBox.Show("Сохранение прошло успешно", + "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation($"File saved successfully: {saveFileDialog.FileName}"); + } + catch (Exception ex) + { + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", + MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"File saving failed: {ex.Message}"); } } /// Обработка нажатия "Загрузка" private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - if (saveFileDialog.ShowDialog() == DialogResult.OK) + if (openFileDialog.ShowDialog() == DialogResult.OK) { try { - _storage.SaveData(saveFileDialog.FileName); - MessageBox.Show("Сохранение прошло успешно", + _storage.LoadData(openFileDialog.FileName); + MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - _logger.LogInformation($"Файл успешно сохранен: {saveFileDialog.FileName}"); + ReloadObjects(); + _logger.LogInformation($"File loaded successfully: {openFileDialog.FileName}"); } catch (Exception ex) { - MessageBox.Show($"Сбой загрузки: {ex.Message}", "Результат", + MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); - _logger.LogWarning($"Не удалось загрузить файл: {ex.Message}"); + _logger.LogWarning($"File loading failed: {ex.Message}"); } } }