diff --git a/ArtillerySerde.java b/ArtillerySerde.java index 14544e3..196b35f 100644 --- a/ArtillerySerde.java +++ b/ArtillerySerde.java @@ -1,10 +1,10 @@ import java.awt.*; public class ArtillerySerde { // Artillery Serialization/Deserialization - private static final String _separatorForObject = ":"; + private static final char _separatorForObject = ':'; public static DrawingArtillery deserialize(String info) { - String[] strings = info.split(_separatorForObject); + String[] strings = info.split(Character.toString(_separatorForObject)); int speed = Integer.parseInt(strings[0]); float weight = Float.parseFloat(strings[1]); @@ -38,7 +38,7 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization EntityArtillery artillery = drawingArtillery.getArtillery(); String result = String.format( - "%d%s%f%s%d%s%s%s%d", + "%d%c%f%c%d%c%s%c%d", artillery.getSpeed(), _separatorForObject, artillery.getWeight(), @@ -55,7 +55,7 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization } return String.format( - "%s%s%d%s%b%s%b", + "%s%c%d%c%b%c%b", result, _separatorForObject, advanced.getDopColor().getRGB(), diff --git a/MapsCollection.java b/MapsCollection.java index 5b9f24c..46eefd5 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -1,3 +1,4 @@ +import java.io.*; import java.util.HashMap; import java.util.Set; @@ -7,6 +8,9 @@ public class MapsCollection { private final int _pictureWidth; private final int _pictureHeight; + private final char separatorDict = '|'; + private final char separatorData = ';'; + public Set getKeys() { return _mapsStorage.keySet(); } @@ -38,4 +42,56 @@ public class MapsCollection { } return null; } + + @SuppressWarnings("ResultOfMethodCallIgnored") + public boolean saveData(String filename) throws IOException { + File file = new File(filename); + + if (file.exists()) { + file.delete(); + } + + file.createNewFile(); + + try (PrintWriter writer = new PrintWriter(file)) { + writer.println("MapsCollection"); + + for (var storage : _mapsStorage.entrySet()) { + writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData))); + } + } + + return true; + } + + public boolean loadData(String filename) throws IOException { + File file = new File(filename); + + if (!file.exists()) { + return false; + } + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String currentLine = reader.readLine(); + + if (currentLine == null || !currentLine.contains("MapsCollection")) { + return false; + } + + _mapsStorage.clear(); + while ((currentLine = reader.readLine()) != null) { + var elements = currentLine.split(Character.toString(separatorDict)); + + AbstractMap map = switch (elements[1]) { + case "SimpleMap" -> new SimpleMap(); + case "ForestMap" -> new ForestMap(); + }; + + _mapsStorage.put(elements[0], new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map)); + _mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "*(\n?)")); + } + } + + return true; + } }