159 lines
5.5 KiB
Java
159 lines
5.5 KiB
Java
|
|
import java.io.*;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
|
|
public class MapsCollection {
|
|
|
|
private final HashMap<String,MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap>> _mapStorages;
|
|
private final int _pictureWidth;
|
|
private final int _pictureHeight;
|
|
private final char separatorDict='|';
|
|
private final char separatorData=';';
|
|
|
|
public MapsCollection(int pictureWidth,int pictureHeight){
|
|
_mapStorages=new HashMap<>();
|
|
_pictureWidth=pictureWidth;
|
|
_pictureHeight=pictureHeight;
|
|
}
|
|
|
|
public ArrayList<String> Keys(){
|
|
return new ArrayList<>(_mapStorages.keySet());
|
|
}
|
|
|
|
public void AddMap(String name, AbstractMap map){
|
|
if (_mapStorages.containsKey(name)){
|
|
return;
|
|
}
|
|
else{
|
|
_mapStorages.put(name,new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
|
|
}
|
|
}
|
|
|
|
public void DelMap(String name){
|
|
_mapStorages.remove(name);
|
|
}
|
|
|
|
public MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap> get(String name){
|
|
if (_mapStorages.containsKey(name)){
|
|
return _mapStorages.get(name);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public DrawingObjectWarship get(String name,int i) {
|
|
if (_mapStorages.containsKey(name))
|
|
{
|
|
return _mapStorages.get(name).GetWarshipInList(i);
|
|
}
|
|
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 : _mapStorages.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 FileDataFormatException("Формат данных не верный");
|
|
|
|
_mapStorages.clear();
|
|
|
|
while ((currentLine = reader.readLine()) != null) {
|
|
var elements = currentLine.split(String.format("\\%c", separatorDict));
|
|
AbstractMap map = switch (elements[1]) {
|
|
case "SimpleMap" -> new SimpleMap();
|
|
case "SecondMap" -> new SecondMap();
|
|
default -> null;
|
|
};
|
|
_mapStorages.put(elements[0], new MapWithSetWarshipsGeneric<>(_pictureWidth, _pictureHeight, map));
|
|
_mapStorages.get(elements[0]).LoadData(elements[2].split(separatorData + "\n?"));
|
|
}
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
|
public void SaveMap(String mapName, String filename) throws IOException {
|
|
File file = new File(filename);
|
|
|
|
if (file.exists())
|
|
file.delete();
|
|
|
|
file.createNewFile();
|
|
|
|
MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> map = _mapStorages.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 warship : map._setWarship.GetWarship()) {
|
|
writer.println(warship.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 FileDataFormatException("Формат данных не верный");
|
|
|
|
String mapName = reader.readLine();
|
|
|
|
MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> map;
|
|
if (_mapStorages.containsKey(mapName)) {
|
|
map = _mapStorages.get(mapName);
|
|
if (!map.GetMap().getClass().getSimpleName().equals(reader.readLine())) {
|
|
throw new FileDataFormatException("Формат данных не верный");
|
|
}
|
|
map._setWarship.Clear();
|
|
} else {
|
|
map = switch (reader.readLine()) {
|
|
case "SimpleMap" -> new MapWithSetWarshipsGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap());
|
|
case "SecondMap" -> new MapWithSetWarshipsGeneric<>(_pictureWidth, _pictureHeight, new SecondMap());
|
|
default -> null;
|
|
};
|
|
_mapStorages.put(mapName, map);
|
|
}
|
|
while ((currentLine = reader.readLine()) != null) {
|
|
map._setWarship.Insert((DrawingObjectWarship) DrawingObjectWarship.Create(currentLine));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|