PIbd-12_Karamushko_M.K._Air.../MapsCollection.java

150 lines
4.9 KiB
Java
Raw Permalink Normal View History

2022-12-15 14:54:34 +04:00
import java.io.*;
2022-11-25 13:49:46 +04:00
import java.util.LinkedHashMap;
import java.util.List;
2022-12-02 11:17:40 +04:00
import java.util.ListIterator;
2022-12-15 14:54:34 +04:00
import java.util.zip.DataFormatException;
2022-11-25 13:49:46 +04:00
public class MapsCollection
{
public LinkedHashMap<String, MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>> _mapStorages;
private int _pictureWidth;
private int _pictureHeight;
2022-12-02 11:17:40 +04:00
private char separatorDict = '|';
private char separatorData = ';';
2022-11-25 13:49:46 +04:00
public List<String> getKeys() { return _mapStorages.keySet().stream().toList(); }
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new LinkedHashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
2022-12-15 14:54:34 +04:00
public void SaveData(String filename) throws IOException
2022-12-02 11:17:40 +04:00
{
File file = new File(filename);
if (file.exists()) file.delete();
2022-12-15 14:54:34 +04:00
file.createNewFile();
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
FileWriter writer = new FileWriter(file);
writer.append("MapsCollection\n");
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
for(String key : _mapStorages.keySet()) {
String record = key + separatorDict + _mapStorages.get(key).GetData(separatorDict, separatorData) + "\n";
writer.append(record);
2022-12-02 11:17:40 +04:00
}
2022-12-15 14:54:34 +04:00
writer.flush();
writer.close();
2022-12-02 11:17:40 +04:00
}
2022-12-15 14:54:34 +04:00
public void LoadData(String filename) throws IOException, DataFormatException {
2022-12-02 11:17:40 +04:00
File file = new File(filename);
2022-12-15 14:54:34 +04:00
if (!file.exists()) throw new FileNotFoundException("Файл не найден");
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
BufferedReader reader = new BufferedReader(new FileReader(file));
String current = reader.readLine();
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
if (!current.equals("MapsCollection")) throw new DataFormatException("Неверный формат данных");
_mapStorages.clear();
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
while ((current = reader.readLine()) != null)
{
String elem[] = current.split("\\" + separatorDict);
AbstractMap map = null;
switch (elem[1])
2022-12-02 11:17:40 +04:00
{
2022-12-15 14:54:34 +04:00
case "SimpleMap":
map = new SimpleMap();
break;
case "MyMap":
map = new MyMap();
break;
2022-12-02 11:17:40 +04:00
}
2022-12-15 14:54:34 +04:00
_mapStorages.put(elem[0], new MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData + ""));
2022-12-02 11:17:40 +04:00
}
2022-12-15 14:54:34 +04:00
reader.close();
2022-12-02 11:17:40 +04:00
}
2022-12-15 14:54:34 +04:00
public void SaveMap(String filename, String key) throws IOException {
2022-12-02 11:17:40 +04:00
File file = new File(filename);
if(file.exists()) file.delete();
2022-12-15 14:54:34 +04:00
MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap> item = _mapStorages.get(key);
file.createNewFile();
FileWriter writer = new FileWriter(file);
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
writer.append("Map\n");
writer.append(key + "\n");
writer.append(item.getMap().getClass().getName() + "\n");
ListIterator<DrawingObjectAircraft> iter = item.getAircraftsIter();
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
while(iter.hasNext()) {
writer.append(AircraftFactory.getDataForSave(iter.next().getAircraft()) + "\n");
2022-12-02 11:17:40 +04:00
}
2022-12-15 14:54:34 +04:00
writer.flush();
writer.close();
2022-12-02 11:17:40 +04:00
}
2022-12-15 14:54:34 +04:00
public void LoadMap(String filename) throws IOException, DataFormatException {
2022-12-02 11:17:40 +04:00
File file = new File(filename);
2022-12-15 14:54:34 +04:00
if (!file.exists()) throw new FileNotFoundException("Файл не найден");
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
BufferedReader reader = new BufferedReader(new FileReader(file));
String format = reader.readLine();
if (!format.equals("Map")) throw new DataFormatException("Неверный формат данных");
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
String key = reader.readLine();
String mapStr = reader.readLine();
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
AbstractMap map = null;
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
switch (mapStr)
{
case "SimpleMap":
map = new SimpleMap();
break;
case "MyMap":
map = new MyMap();
break;
}
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
if(_mapStorages.containsKey(key)) _mapStorages.get(key).ClearMap();
else _mapStorages.put(key, new MapWithSetAircraftsGeneric<>(_pictureWidth, _pictureHeight, map));
2022-12-02 11:17:40 +04:00
2022-12-15 14:54:34 +04:00
String current = null;
while ((current = reader.readLine()) != null)
{
_mapStorages.get(key).addAircraft(DrawingObjectAircraft.Create(current));
2022-12-02 11:17:40 +04:00
}
2022-12-15 14:54:34 +04:00
reader.close();
2022-12-02 11:17:40 +04:00
}
2022-11-25 13:49:46 +04:00
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<DrawingObjectAircraft, AbstractMap> getMap(String name)
{
return _mapStorages.getOrDefault(name, null);
}
}