import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.zip.DataFormatException; public class MapsCollection { /// Словарь (хранилище) с картами final HashMap> _mapStorages; /// Возвращение списка названий карт public ArrayList keys() { return new ArrayList(_mapStorages.keySet()); } /// Ширина окна отрисовки private final int _pictureWidth; /// Высота окна отрисовки private final int _pictureHeight; // Сепараторы private final String separatorDict = "~"; private final char separatorData = ';'; /// Конструктор public MapsCollection(int pictureWidth, int pictureHeight) { _mapStorages = new HashMap>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } /// Добавление карты public void AddMap(String name, AbstractMap map) { // Логика для добавления if (!_mapStorages.containsKey(name)) _mapStorages.put(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); } /// Удаление карты public void DelMap(String name) { // Логика для удаления if (_mapStorages.containsKey(name)) _mapStorages.remove(name); } /// Доступ к парковке public MapWithSetLocomotivesGeneric Get(String ind) { // Логика получения объекта if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind); return null; } // Доп.индексатор из задания public IDrawningObject Get (String name, int position) { if (_mapStorages.containsKey(name)) return _mapStorages.get(name)._setLocomotives.Get(position); else return null; } /// Сохранение информации по локомотивам в хранилище в файл public void SaveData(String filename) throws IOException { File file = new File(filename); if (file.exists()) { file.delete(); } BufferedWriter bw = new BufferedWriter(new FileWriter(filename)); bw.write("MapsCollection\n"); for (var storage : _mapStorages.entrySet()) { bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n"); } bw.flush(); } // Сохранение одной выбранной карты public void SaveMap(String map_name, String filename) throws IOException{ File file = new File(filename); MapWithSetLocomotivesGeneric map = Get(map_name); if (file.exists()) { file.delete(); } BufferedWriter bw = new BufferedWriter(new FileWriter(filename)); bw.write("SingleMap\n"); bw.write("" + map_name + "\n"); bw.write("" + map.getClass() + "\n"); for (var locomotive : map._setLocomotives.GetLocomotives()) { bw.write("" + locomotive.getInfo() + "\n"); } bw.flush(); } // Загрузка одной карты public void LoadMap(String filename) throws StorageOverflowException, IOException, DataFormatException{ File file = new File(filename); if (!file.exists()) { throw new FileNotFoundException("Файл не найден"); } BufferedReader br = new BufferedReader(new FileReader(filename)); String curLine = br.readLine(); if (!curLine.contains("SingleMap")) { throw new DataFormatException("Неверный формат данных"); } String mapName = br.readLine(); String mapClass = br.readLine(); if (_mapStorages.containsKey(mapName)) { _mapStorages.get(mapName)._setLocomotives.Clear(); while((curLine = br.readLine()) != null) { _mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine)); } _mapStorages.get(mapName)._setLocomotives.ReversePlaces(); } AbstractMap map = null; switch (mapClass) { case "class SimpleMap": map = new SimpleMap(); break; case "class SpikeMap": map = new SpikeMap(); break; case "class RailMap": map = new RailMap(); break; } _mapStorages.put(mapName, new MapWithSetLocomotivesGeneric<>(_pictureWidth, _pictureHeight, map)); while((curLine = br.readLine()) != null) { _mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine)); } _mapStorages.get(mapName)._setLocomotives.ReversePlaces(); } /// Загрузка информации по локомотивам в депо из файла public void LoadData(String filename) throws DataFormatException, IOException, StorageOverflowException { File file = new File(filename); if (!file.exists()) { throw new FileNotFoundException("Файл не найден"); } BufferedReader br = new BufferedReader(new FileReader(filename)); String curLine = br.readLine(); if (!curLine.contains("MapsCollection")) { throw new DataFormatException("Неверный формат данных"); } _mapStorages.clear(); while ((curLine = br.readLine()) != null) { var elems = curLine.split(separatorDict); AbstractMap map = null; switch (elems[1]) { case "class SimpleMap": map = new SimpleMap(); break; case "class SpikeMap": map = new SpikeMap(); break; case "class RailMap": map = new RailMap(); break; } _mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); _mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData))); } } }