Added exceptions to MapsCollection

This commit is contained in:
Сергей Полевой 2022-12-05 22:34:50 +04:00
parent 60bc67a8f5
commit 36a610081e

View File

@ -1,4 +1,5 @@
import java.io.*;
import java.nio.file.NoSuchFileException;
import java.util.HashMap;
import java.util.Set;
@ -44,7 +45,7 @@ public class MapsCollection {
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public boolean saveData(String filename) throws IOException {
public void saveData(String filename) throws IOException {
File file = new File(filename);
if (file.exists()) {
@ -60,22 +61,20 @@ public class MapsCollection {
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData)));
}
}
return true;
}
public boolean loadData(String filename) throws IOException {
public void loadData(String filename) throws IOException {
File file = new File(filename);
if (!file.exists()) {
return false;
throw new FileNotFoundException("Файл не найден");
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String currentLine = reader.readLine();
if (currentLine == null || !currentLine.contains("MapsCollection")) {
return false;
throw new FileFormatException("Неверный формат файла");
}
_mapsStorage.clear();
@ -92,12 +91,10 @@ public class MapsCollection {
_mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?"));
}
}
return true;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public boolean saveMap(String mapName, String filename) throws IOException {
public void saveMap(String mapName, String filename) throws Exception {
File file = new File(filename);
if (file.exists()) {
@ -109,7 +106,7 @@ public class MapsCollection {
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map = _mapsStorage.getOrDefault(mapName, null);
if (map == null) {
return false;
throw new IndexOutOfBoundsException();
}
try (PrintWriter writer = new PrintWriter(file)) {
@ -121,22 +118,20 @@ public class MapsCollection {
writer.println(artillery.getInfo());
}
}
return true;
}
public boolean loadMap(String filename) throws IOException {
public void loadMap(String filename) throws IOException {
File file = new File(filename);
if (!file.exists()) {
return false;
throw new FileNotFoundException("Файл не найден");
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String currentLine = reader.readLine();
if (currentLine == null || !currentLine.contains("Map")) {
return false;
throw new FileFormatException("Неверный формат файла");
}
String mapName = reader.readLine();
@ -145,7 +140,7 @@ public class MapsCollection {
if (_mapsStorage.containsKey(mapName)) {
map = _mapsStorage.get(mapName);
if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) {
return false;
throw new FileFormatException("Неверный формат файла");
}
map._setArtilleries.clear();
} else {
@ -162,7 +157,5 @@ public class MapsCollection {
_mapsStorage.put(mapName, map);
}
return true;
}
}