Добавлен выброс исключений

This commit is contained in:
Никита Потапов 2023-12-18 23:57:27 +04:00
parent 5a12399986
commit 59a9be54ee
4 changed files with 48 additions and 23 deletions

View File

@ -7,5 +7,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="log4j-core-2_22_0" level="project" />
</component>
</module>

View File

@ -61,11 +61,20 @@ public class FormPlaneCollection {
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
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();
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
);
}
}
}
);

View File

@ -88,8 +88,7 @@ public class PlanesGenericStorage {
if (_planeStorages.containsKey(key)) {
collection = _planeStorages.get(key);
collection.clear();
}
else
} else
collection = new PlanesGenericCollection<>(_pictureWidth, _pictureHeight);
List<String> plainsStrings = new ArrayList<>();
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);
}

View File

@ -26,6 +26,9 @@ public class SetGeneric<T extends DrawingPlane> {
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<T extends DrawingPlane> {
}
// Удаление объекта из массива, присвоив элементу массива значение null
T plane = _places.get(position);
if (plane == null) {
throw new PlaneNotFoundException(position);
}
_places.set(position, null);
return plane;
}