import java.io.*; import java.nio.file.NoSuchFileException; import java.util.HashMap; import java.util.Set; public class MapsCollection { private final HashMap> _mapsStorage; private final int _pictureWidth; private final int _pictureHeight; private final char separatorDict = '|'; private final char separatorData = ';'; public Set getKeys() { return _mapsStorage.keySet(); } public MapsCollection(int pictureWidth, int pictureHeight) { _mapsStorage = new HashMap<>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void addMap(String name, AbstractMap map) { if (!_mapsStorage.containsKey(name)) { _mapsStorage.put(name, new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map)); } } public void deleteMap(String name) { _mapsStorage.remove(name); } public MapWithSetArtilleriesGeneric getMap(String name) { return _mapsStorage.getOrDefault(name, null); } public IDrawingObject getArtillery(String mapName, int index) { var map = _mapsStorage.getOrDefault(mapName, null); if (map != null) { return map._setArtilleries.get(index); } return null; } @SuppressWarnings("ResultOfMethodCallIgnored") public void 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))); } } } public void loadData(String filename) throws IOException { File file = new File(filename); if (!file.exists()) { throw new FileNotFoundException("Файл не найден"); } try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String currentLine = reader.readLine(); if (currentLine == null || !currentLine.contains("MapsCollection")) { throw new FileFormatException("Неверный формат файла"); } _mapsStorage.clear(); while ((currentLine = reader.readLine()) != null) { var elements = currentLine.split(String.format("\\%c", separatorDict)); AbstractMap map = switch (elements[1]) { case "SimpleMap" -> new SimpleMap(); case "ForestMap" -> new ForestMap(); default -> null; }; _mapsStorage.put(elements[0], new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map)); _mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?")); } } } @SuppressWarnings("ResultOfMethodCallIgnored") public void saveMap(String mapName, String filename) throws Exception { File file = new File(filename); if (file.exists()) { file.delete(); } file.createNewFile(); MapWithSetArtilleriesGeneric map = _mapsStorage.getOrDefault(mapName, null); if (map == null) { throw new IndexOutOfBoundsException(); } try (PrintWriter writer = new PrintWriter(file)) { writer.println("Map"); writer.println(mapName); writer.println(map.getMap().getClass().getSimpleName()); for (var artillery : map._setArtilleries.getArtilleries()) { writer.println(artillery.getInfo()); } } } public void loadMap(String filename) throws IOException { File file = new File(filename); if (!file.exists()) { throw new FileNotFoundException("Файл не найден"); } try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String currentLine = reader.readLine(); if (currentLine == null || !currentLine.contains("Map")) { throw new FileFormatException("Неверный формат файла"); } String mapName = reader.readLine(); MapWithSetArtilleriesGeneric map; if (_mapsStorage.containsKey(mapName)) { map = _mapsStorage.get(mapName); if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) { throw new FileFormatException("Неверный формат файла"); } map._setArtilleries.clear(); } else { map = switch (reader.readLine()) { case "SimpleMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap()); case "ForestMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new ForestMap()); default -> null; }; } while ((currentLine = reader.readLine()) != null) { map._setArtilleries.insert(DrawingObjectArtillery.create(currentLine)); } _mapsStorage.put(mapName, map); } } }