PIbd-23_Mochalov_D.V._Locom.../MapsCollection.java

178 lines
6.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class MapsCollection {
/// Словарь (хранилище) с картами
final HashMap<String, MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>> _mapStorages;
/// Возвращение списка названий карт
public ArrayList<String> keys() {
return new ArrayList<String>(_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<String, MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// Добавление карты
public void AddMap(String name, AbstractMap map)
{
// Логика для добавления
if (!_mapStorages.containsKey(name)) _mapStorages.put(name, new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
/// Удаление карты
public void DelMap(String name)
{
// Логика для удаления
if (_mapStorages.containsKey(name)) _mapStorages.remove(name);
}
/// Доступ к парковке
public MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> 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 boolean SaveData(String filename)
{
File file = new File(filename);
if (file.exists())
{
file.delete();
}
try(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");
}
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
return true;
}
// Сохранение одной выбранной карты
public boolean SaveMap(String map_name, String filename) {
File file = new File(filename);
MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> map = Get(map_name);
if (file.exists())
{
file.delete();
}
try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
bw.write("SingleMap\n");
bw.write("" + map_name + separatorDict +map.GetData(separatorDict.charAt(0), separatorData) + "\n");
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
return true;
}
// Загрузка одной карты
public boolean LoadMap(String filename){
File file = new File(filename);
if (!file.exists())
{
return false;
}
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String curLine = br.readLine();
if (!curLine.contains("SingleMap")) {
return false;
}
curLine = br.readLine();
var elems = curLine.split(separatorDict);
if (_mapStorages.containsKey(elems[0])) {
_mapStorages.get(elems[0])._setLocomotives.Clear();
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
return true;
}
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<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
return true;
}
/// Загрузка информации по локомотивам в депо из файла
public boolean LoadData(String filename)
{
File file = new File(filename);
if (!file.exists())
{
return false;
}
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String curLine = br.readLine();
if (!curLine.contains("MapsCollection")) {
return false;
}
_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<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
return true;
}
}