2022-11-19 16:11:28 +04:00
|
|
|
|
import java.io.*;
|
2022-11-05 13:19:00 +04:00
|
|
|
|
import java.util.ArrayList;
|
2022-11-19 21:53:44 +04:00
|
|
|
|
import java.util.Collections;
|
2022-11-05 13:19:00 +04:00
|
|
|
|
import java.util.HashMap;
|
2022-12-02 18:34:48 +04:00
|
|
|
|
import java.util.zip.DataFormatException;
|
2022-11-05 13:19:00 +04:00
|
|
|
|
|
|
|
|
|
public class MapsCollection {
|
|
|
|
|
/// Словарь (хранилище) с картами
|
2022-11-19 16:11:28 +04:00
|
|
|
|
final HashMap<String, MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>> _mapStorages;
|
2022-11-05 18:49:21 +04:00
|
|
|
|
|
2022-11-05 13:19:00 +04:00
|
|
|
|
/// Возвращение списка названий карт
|
|
|
|
|
public ArrayList<String> keys() {
|
|
|
|
|
return new ArrayList<String>(_mapStorages.keySet());
|
|
|
|
|
}
|
|
|
|
|
/// Ширина окна отрисовки
|
|
|
|
|
private final int _pictureWidth;
|
|
|
|
|
/// Высота окна отрисовки
|
|
|
|
|
private final int _pictureHeight;
|
2022-11-19 16:11:28 +04:00
|
|
|
|
// Сепараторы
|
2022-11-19 19:58:47 +04:00
|
|
|
|
private final String separatorDict = "~";
|
2022-11-19 16:11:28 +04:00
|
|
|
|
private final char separatorData = ';';
|
2022-11-05 13:19:00 +04:00
|
|
|
|
/// Конструктор
|
|
|
|
|
public MapsCollection(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
2022-11-19 16:11:28 +04:00
|
|
|
|
_mapStorages = new HashMap<String, MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>>();
|
2022-11-05 13:19:00 +04:00
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
/// Добавление карты
|
|
|
|
|
public void AddMap(String name, AbstractMap map)
|
|
|
|
|
{
|
|
|
|
|
// Логика для добавления
|
2022-11-19 16:11:28 +04:00
|
|
|
|
if (!_mapStorages.containsKey(name)) _mapStorages.put(name, new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
2022-11-05 13:19:00 +04:00
|
|
|
|
}
|
|
|
|
|
/// Удаление карты
|
|
|
|
|
public void DelMap(String name)
|
|
|
|
|
{
|
|
|
|
|
// Логика для удаления
|
|
|
|
|
if (_mapStorages.containsKey(name)) _mapStorages.remove(name);
|
|
|
|
|
}
|
|
|
|
|
/// Доступ к парковке
|
2022-11-19 16:11:28 +04:00
|
|
|
|
public MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> Get(String ind)
|
2022-11-05 13:19:00 +04:00
|
|
|
|
{
|
|
|
|
|
// Логика получения объекта
|
|
|
|
|
if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-11-05 18:49:21 +04:00
|
|
|
|
// Доп.индексатор из задания
|
2022-11-19 16:11:28 +04:00
|
|
|
|
public IDrawningObject Get (String name, int position) {
|
2022-11-05 18:49:21 +04:00
|
|
|
|
if (_mapStorages.containsKey(name)) return _mapStorages.get(name)._setLocomotives.Get(position);
|
|
|
|
|
else return null;
|
|
|
|
|
}
|
2022-11-19 16:11:28 +04:00
|
|
|
|
|
|
|
|
|
/// Сохранение информации по локомотивам в хранилище в файл
|
2022-12-02 18:34:48 +04:00
|
|
|
|
public void SaveData(String filename) throws IOException
|
2022-11-19 16:11:28 +04:00
|
|
|
|
{
|
|
|
|
|
File file = new File(filename);
|
|
|
|
|
if (file.exists())
|
|
|
|
|
{
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
2022-12-02 18:34:48 +04:00
|
|
|
|
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");
|
2022-11-19 16:11:28 +04:00
|
|
|
|
}
|
2022-12-02 21:20:08 +04:00
|
|
|
|
bw.flush();
|
2022-11-19 16:11:28 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 21:53:44 +04:00
|
|
|
|
// Сохранение одной выбранной карты
|
2022-12-02 18:34:48 +04:00
|
|
|
|
public void SaveMap(String map_name, String filename) throws IOException{
|
2022-11-19 21:53:44 +04:00
|
|
|
|
File file = new File(filename);
|
|
|
|
|
MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> map = Get(map_name);
|
|
|
|
|
if (file.exists())
|
|
|
|
|
{
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
2022-12-02 18:34:48 +04:00
|
|
|
|
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");
|
2022-11-19 21:53:44 +04:00
|
|
|
|
}
|
2022-12-02 21:20:08 +04:00
|
|
|
|
bw.flush();
|
2022-12-02 18:34:48 +04:00
|
|
|
|
|
2022-11-19 21:53:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Загрузка одной карты
|
2022-12-02 18:34:48 +04:00
|
|
|
|
public void LoadMap(String filename) throws StorageOverflowException, IOException, DataFormatException{
|
|
|
|
|
File file = new File(filename);
|
|
|
|
|
if (!file.exists())
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("Файл не найден");
|
|
|
|
|
}
|
2022-11-19 21:53:44 +04:00
|
|
|
|
BufferedReader br = new BufferedReader(new FileReader(filename));
|
|
|
|
|
String curLine = br.readLine();
|
|
|
|
|
if (!curLine.contains("SingleMap")) {
|
2022-12-02 18:34:48 +04:00
|
|
|
|
throw new DataFormatException("Неверный формат данных");
|
2022-11-19 21:53:44 +04:00
|
|
|
|
}
|
2022-11-20 20:37:09 +04:00
|
|
|
|
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();
|
2022-11-19 21:53:44 +04:00
|
|
|
|
}
|
|
|
|
|
AbstractMap map = null;
|
2022-11-20 20:37:09 +04:00
|
|
|
|
switch (mapClass)
|
2022-11-19 21:53:44 +04:00
|
|
|
|
{
|
|
|
|
|
case "class SimpleMap":
|
|
|
|
|
map = new SimpleMap();
|
|
|
|
|
break;
|
|
|
|
|
case "class SpikeMap":
|
|
|
|
|
map = new SpikeMap();
|
|
|
|
|
break;
|
|
|
|
|
case "class RailMap":
|
|
|
|
|
map = new RailMap();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-11-20 20:37:09 +04:00
|
|
|
|
_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();
|
2022-11-19 21:53:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 16:11:28 +04:00
|
|
|
|
/// Загрузка информации по локомотивам в депо из файла
|
2022-12-02 18:34:48 +04:00
|
|
|
|
public void LoadData(String filename) throws DataFormatException, IOException, StorageOverflowException
|
2022-11-19 16:11:28 +04:00
|
|
|
|
{
|
|
|
|
|
File file = new File(filename);
|
|
|
|
|
if (!file.exists())
|
|
|
|
|
{
|
2022-12-02 18:34:48 +04:00
|
|
|
|
throw new FileNotFoundException("Файл не найден");
|
2022-11-19 16:11:28 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 18:34:48 +04:00
|
|
|
|
BufferedReader br = new BufferedReader(new FileReader(filename));
|
|
|
|
|
String curLine = br.readLine();
|
|
|
|
|
if (!curLine.contains("MapsCollection")) {
|
|
|
|
|
throw new DataFormatException("Неверный формат данных");
|
|
|
|
|
}
|
2022-11-19 16:11:28 +04:00
|
|
|
|
|
2022-12-02 18:34:48 +04:00
|
|
|
|
_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;
|
2022-11-19 16:11:28 +04:00
|
|
|
|
}
|
2022-12-02 18:34:48 +04:00
|
|
|
|
|
|
|
|
|
_mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
|
|
|
|
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
|
2022-11-19 16:11:28 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-11-05 13:19:00 +04:00
|
|
|
|
}
|