From 59a9be54eeadb299daa19352feaa669fb73ab3ae Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Mon, 18 Dec 2023 23:57:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B2=D1=8B=D0=B1=D1=80=D0=BE=D1=81=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...PIbd-21_Potapov_N.S._Stormtrooper_Hard.iml | 1 + ProjectStormtrooper/FormPlaneCollection.java | 34 ++++++++++++++----- ProjectStormtrooper/PlanesGenericStorage.java | 30 ++++++++-------- ProjectStormtrooper/SetGeneric.java | 6 ++++ 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/.idea/PIbd-21_Potapov_N.S._Stormtrooper_Hard.iml b/.idea/PIbd-21_Potapov_N.S._Stormtrooper_Hard.iml index b107a2d..f5ef6d6 100644 --- a/.idea/PIbd-21_Potapov_N.S._Stormtrooper_Hard.iml +++ b/.idea/PIbd-21_Potapov_N.S._Stormtrooper_Hard.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/ProjectStormtrooper/FormPlaneCollection.java b/ProjectStormtrooper/FormPlaneCollection.java index 4c55101..50e8b68 100644 --- a/ProjectStormtrooper/FormPlaneCollection.java +++ b/ProjectStormtrooper/FormPlaneCollection.java @@ -61,10 +61,19 @@ public class FormPlaneCollection { fileChooser.setDialogTitle("Выберите файл для загрузки данных"); if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); - if (_storage.LoadData(selectedFile.getAbsolutePath())) { - JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE); - } else { - JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE); + try { + if (_storage.LoadData(selectedFile.getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE); + } + } catch (Exception ex) { + JOptionPane.showMessageDialog( + null, + "Ошибка при загрузке объектов" + ex.getMessage(), + "Результат", + JOptionPane.ERROR_MESSAGE + ); } } ReloadObjects(); @@ -78,10 +87,19 @@ public class FormPlaneCollection { fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt")); if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); - if (_storage.SaveData(selectedFile.getAbsolutePath())) - JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE); - else - JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE); + try { + if (_storage.SaveData(selectedFile.getAbsolutePath())) + JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE); + else + JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE); + } catch (Exception ex) { + JOptionPane.showMessageDialog( + null, + "Ошибка при сохранении объектов" + ex.getMessage(), + "Результат", + JOptionPane.ERROR_MESSAGE + ); + } } } ); diff --git a/ProjectStormtrooper/PlanesGenericStorage.java b/ProjectStormtrooper/PlanesGenericStorage.java index 21b0d4d..542cfb6 100644 --- a/ProjectStormtrooper/PlanesGenericStorage.java +++ b/ProjectStormtrooper/PlanesGenericStorage.java @@ -71,7 +71,7 @@ public class PlanesGenericStorage { return true; } - public boolean LoadDataSingle(String filename){ + public boolean LoadDataSingle(String filename) { if (!new File(filename).exists()) { return false; } @@ -85,15 +85,14 @@ public class PlanesGenericStorage { if (key == null || key.isEmpty()) return false; PlanesGenericCollection collection; - if (_planeStorages.containsKey(key)){ + if (_planeStorages.containsKey(key)) { collection = _planeStorages.get(key); collection.clear(); - } - else + } else collection = new PlanesGenericCollection<>(_pictureWidth, _pictureHeight); List plainsStrings = new ArrayList<>(); s = reader.readLine(); - while (s != null && !s.isEmpty()){ + while (s != null && !s.isEmpty()) { plainsStrings.add(s); s = reader.readLine(); } @@ -107,7 +106,6 @@ public class PlanesGenericStorage { ); if (plane == null || collection.Add(plane) == -1) return false; - plane.SetDrawingBounds(1000, 1000); } _planeStorages.put(key, collection); } catch (IOException e) { @@ -116,7 +114,7 @@ public class PlanesGenericStorage { return true; } - public boolean SaveData(String filename) { + public boolean SaveData(String filename) throws Exception { var file = new File(filename); if (file.exists()) { file.delete(); @@ -130,7 +128,7 @@ public class PlanesGenericStorage { data.append(record.getKey()).append(_separatorForKeyValue).append(records).append("\n"); } if (data.isEmpty()) { - return false; + throw new Exception("Невалидная операция, нет данных для сохранения"); } try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) { writer.write(_keyword + System.lineSeparator() + data); @@ -140,18 +138,18 @@ public class PlanesGenericStorage { return true; } - public boolean LoadData(String filename) { + public boolean LoadData(String filename) throws Exception { var file = new File(filename); if (!file.exists()) { - return false; + throw new Exception("Файл не найден"); } try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { String s = reader.readLine(); if (s == null || s.isEmpty()) - return false; + throw new Exception("Нет данных для загрузки"); if (!s.startsWith(_keyword)) { - return false; + throw new Exception("Неверный формат данных"); } _planeStorages.clear(); s = reader.readLine(); @@ -171,9 +169,11 @@ public class PlanesGenericStorage { _separatorForObject, _pictureWidth, _pictureHeight ); - if (plane == null || collection.Add(plane) == -1) - return false; - plane.SetDrawingBounds(1000, 1000); + if (plane != null) { + if (collection.Add(plane) == -1) { + throw new Exception("Ошибка добавления в коллекцию"); + } + } } _planeStorages.put(record[0], collection); } diff --git a/ProjectStormtrooper/SetGeneric.java b/ProjectStormtrooper/SetGeneric.java index 9d9201b..c6363f9 100644 --- a/ProjectStormtrooper/SetGeneric.java +++ b/ProjectStormtrooper/SetGeneric.java @@ -26,6 +26,9 @@ public class SetGeneric { if (position < 0 || position >= _maxCount) { return -1; } + if (Count() == _maxCount) { + throw new PlanesStorageOverflowException(_maxCount); + } // Вставка по позиции _places.add(position, plane); return position; @@ -38,6 +41,9 @@ public class SetGeneric { } // Удаление объекта из массива, присвоив элементу массива значение null T plane = _places.get(position); + if (plane == null) { + throw new PlaneNotFoundException(position); + } _places.set(position, null); return plane; }