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<T, U> 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<DrawningAircraft, DrawningObjectAircraft> - 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<DrawningAircraft, DrawningObjectAircraft> 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}"); } } }