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; }