2023-12-17 17:20:41 +04:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.*;
|
2023-11-04 19:58:26 +04:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
public class CarsGenericStorage {
|
|
|
|
final HashMap<String, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> _carStorages;
|
|
|
|
public ArrayList<String> Keys()
|
|
|
|
{
|
|
|
|
return new ArrayList<>(_carStorages.keySet());
|
|
|
|
}
|
|
|
|
private final int _pictureWidth;
|
|
|
|
private final int _pictureHeight;
|
2023-12-17 17:20:41 +04:00
|
|
|
private static String _separatorForKeyValueWR = "|";
|
|
|
|
private static String _separatorForKeyValue = "\\|";
|
|
|
|
private static String _separatorForObjectWR = ":";
|
|
|
|
private static String _separatorForObject = "\\:";
|
|
|
|
private String _separatorRecordsWR = ";";
|
|
|
|
private String _separatorRecords = "\\;";
|
|
|
|
|
2023-11-04 19:58:26 +04:00
|
|
|
public CarsGenericStorage(int pictureWidth, int pictureHeight)
|
|
|
|
{
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
_carStorages = new HashMap<>();
|
|
|
|
}
|
2023-12-17 17:20:41 +04:00
|
|
|
|
|
|
|
public boolean SaveDataSingle(String filename, String key) {
|
|
|
|
if (new File(filename).exists()) {
|
|
|
|
new File(filename).delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder data = new StringBuilder();
|
|
|
|
data.append(key).append("\n");
|
|
|
|
if (_carStorages.get(key) == null)
|
|
|
|
return false;
|
|
|
|
if (_carStorages.get(key).getTanker(100) == null)
|
|
|
|
return false;
|
|
|
|
for (DrawTanker elem : _carStorages.get(key).getTanker(100))
|
|
|
|
{
|
|
|
|
data.append(elem != null ? ExtentionDrawingTanker.GetDataForSave(elem, _separatorForObjectWR) + "\n" : "");
|
|
|
|
}
|
|
|
|
if (data.length() == 0)
|
|
|
|
return false;
|
|
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename)))
|
|
|
|
{
|
|
|
|
writer.write("TankerStorage" + System.lineSeparator() + data.toString());
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean LoadDataSingle(String filename){
|
|
|
|
if (!new File(filename).exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
|
|
|
String s = reader.readLine();
|
|
|
|
if (s == null || s.length() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!s.startsWith("TankerStorage"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
String key = reader.readLine();
|
|
|
|
if (key == null || key.length() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection;
|
|
|
|
if (_carStorages.containsKey(key)){
|
|
|
|
collection = _carStorages.get(key);
|
|
|
|
collection.clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
collection = new CarsGenericCollection<>(_pictureWidth, _pictureHeight);
|
|
|
|
|
|
|
|
List<String> tankerString = new ArrayList<String>();
|
|
|
|
|
|
|
|
s = reader.readLine();
|
|
|
|
while (s != null && s.length() != 0){
|
|
|
|
tankerString.add(s);
|
|
|
|
s = reader.readLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.reverse(tankerString);
|
|
|
|
for (String elem : tankerString) {
|
|
|
|
DrawTanker tanker = ExtentionDrawingTanker.CreateDrawingTanker(elem, _separatorForObject, _pictureWidth, _pictureHeight);
|
|
|
|
if (tanker == null || collection.plus(tanker) == -1)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_carStorages.put(key, collection);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean SaveData(String filename)
|
|
|
|
{
|
|
|
|
if (new File(filename).exists()) {
|
|
|
|
new File(filename).delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder data = new StringBuilder();
|
|
|
|
|
|
|
|
for (Map.Entry<String, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> record : _carStorages.entrySet()) {
|
|
|
|
StringBuilder records = new StringBuilder();
|
|
|
|
for (DrawTanker elem : record.getValue().getTanker(100)) {
|
|
|
|
records.append(elem != null ? ExtentionDrawingTanker.GetDataForSave(elem, _separatorForObjectWR) + _separatorRecordsWR : "");
|
|
|
|
}
|
|
|
|
data.append(record.getKey()).append(_separatorForKeyValueWR).append(records).append("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.length() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
|
|
|
writer.write("TrainStorage" + System.lineSeparator() + data.toString());
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean LoadData(String filename) {
|
|
|
|
if (!new File(filename).exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
|
|
|
String s = reader.readLine();
|
|
|
|
if (s == null || s.length() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!s.startsWith("TrainStorage"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_carStorages.clear();
|
|
|
|
s = reader.readLine();
|
|
|
|
while (s != null && s.length() != 0) {
|
|
|
|
String[] record = s.split(_separatorForKeyValue);
|
|
|
|
s = reader.readLine();
|
|
|
|
if (record.length != 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection = new CarsGenericCollection<>(_pictureWidth, _pictureHeight);
|
|
|
|
String[] set = record[1].split(_separatorRecords);
|
|
|
|
List<String> reversedSet = Arrays.asList(set);
|
|
|
|
Collections.reverse(reversedSet);
|
|
|
|
for (String elem : reversedSet) {
|
|
|
|
DrawTanker train = ExtentionDrawingTanker.CreateDrawingTanker(elem, _separatorForObject, _pictureWidth, _pictureHeight);
|
|
|
|
if (train == null || collection.plus(train) == -1)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_carStorages.put(record[0], collection);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-04 19:58:26 +04:00
|
|
|
public void AddSet(String name)
|
|
|
|
{
|
|
|
|
if (Keys().contains(name))
|
|
|
|
return;
|
|
|
|
_carStorages.put(name, new CarsGenericCollection<>(_pictureWidth, _pictureHeight));
|
|
|
|
}
|
|
|
|
public void DelSet(String name)
|
|
|
|
{
|
|
|
|
if (!Keys().contains(name))
|
|
|
|
return;
|
|
|
|
_carStorages.remove(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CarsGenericCollection<DrawTanker, DrawingObjectTanker> get(String ind)
|
|
|
|
{
|
|
|
|
if (Keys().contains(ind))
|
|
|
|
return _carStorages.get(ind);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public DrawingObjectTanker get(String name, int ind)
|
|
|
|
{
|
|
|
|
if (!Keys().contains(name))
|
|
|
|
return null;
|
|
|
|
return _carStorages.get(name).GetU(ind);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|