import java.io.*; import java.util.LinkedHashMap; import java.util.List; import java.util.ListIterator; import java.util.zip.DataFormatException; public class MapsCollection { public LinkedHashMap> _mapStorages; private int _pictureWidth; private int _pictureHeight; private char separatorDict = '|'; private char separatorData = ';'; public List getKeys() { return _mapStorages.keySet().stream().toList(); } public MapsCollection(int pictureWidth, int pictureHeight) { _mapStorages = new LinkedHashMap<>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } public void SaveData(String filename) throws IOException { File file = new File(filename); if (file.exists()) file.delete(); file.createNewFile(); FileWriter writer = new FileWriter(file); writer.append("MapsCollection\n"); for(String key : _mapStorages.keySet()) { String record = key + separatorDict + _mapStorages.get(key).GetData(separatorDict, separatorData) + "\n"; writer.append(record); } writer.flush(); writer.close(); } public void LoadData(String filename) throws IOException, DataFormatException { File file = new File(filename); if (!file.exists()) throw new FileNotFoundException("Файл не найден"); BufferedReader reader = new BufferedReader(new FileReader(file)); String current = reader.readLine(); if (!current.equals("MapsCollection")) throw new DataFormatException("Неверный формат данных"); _mapStorages.clear(); while ((current = reader.readLine()) != null) { String elem[] = current.split("\\" + separatorDict); AbstractMap map = null; switch (elem[1]) { case "SimpleMap": map = new SimpleMap(); break; case "MyMap": map = new MyMap(); break; } _mapStorages.put(elem[0], new MapWithSetAircraftsGeneric(_pictureWidth, _pictureHeight, map)); _mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData + "")); } reader.close(); } public void SaveMap(String filename, String key) throws IOException { File file = new File(filename); if(file.exists()) file.delete(); MapWithSetAircraftsGeneric item = _mapStorages.get(key); file.createNewFile(); FileWriter writer = new FileWriter(file); writer.append("Map\n"); writer.append(key + "\n"); writer.append(item.getMap().getClass().getName() + "\n"); ListIterator iter = item.getAircraftsIter(); while(iter.hasNext()) { writer.append(AircraftFactory.getDataForSave(iter.next().getAircraft()) + "\n"); } writer.flush(); writer.close(); } public void LoadMap(String filename) throws IOException, DataFormatException { File file = new File(filename); if (!file.exists()) throw new FileNotFoundException("Файл не найден"); BufferedReader reader = new BufferedReader(new FileReader(file)); String format = reader.readLine(); if (!format.equals("Map")) throw new DataFormatException("Неверный формат данных"); String key = reader.readLine(); String mapStr = reader.readLine(); AbstractMap map = null; switch (mapStr) { case "SimpleMap": map = new SimpleMap(); break; case "MyMap": map = new MyMap(); break; } if(_mapStorages.containsKey(key)) _mapStorages.get(key).ClearMap(); else _mapStorages.put(key, new MapWithSetAircraftsGeneric<>(_pictureWidth, _pictureHeight, map)); String current = null; while ((current = reader.readLine()) != null) { _mapStorages.get(key).addAircraft(DrawingObjectAircraft.Create(current)); } reader.close(); } public void AddMap(String name, AbstractMap map) { // TODO Прописать логику для добавления boolean check = _mapStorages.containsKey(name); if (check) return; _mapStorages.put(name, new MapWithSetAircraftsGeneric(_pictureWidth, _pictureHeight, map)); } public void DelMap(String name) { // TODO Прописать логику для удаления _mapStorages.remove(name); } public MapWithSetAircraftsGeneric getMap(String name) { return _mapStorages.getOrDefault(name, null); } }