Лаба 6 хард готова, осталось поискать баги и почистить код

This commit is contained in:
Данила Мочалов 2022-11-19 21:53:44 +04:00
parent 2fe33e7569
commit f329788ce7
3 changed files with 106 additions and 2 deletions

View File

@ -263,8 +263,9 @@ public class FormMapWithSetLocomotives extends JComponent {
dialog.setVisible(true); dialog.setVisible(true);
} }
}); });
// Сохранения и загрузки
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
// Сохранение всех хранилищ
JButton saveButton = new JButton("Save"); JButton saveButton = new JButton("Save");
saveButton.addActionListener(e -> { saveButton.addActionListener(e -> {
fileChooser.setDialogTitle("Saving"); fileChooser.setDialogTitle("Saving");
@ -278,6 +279,7 @@ public class FormMapWithSetLocomotives extends JComponent {
}); });
statusPanel.add(saveButton); statusPanel.add(saveButton);
// Загрузка всех хранилищ
JButton loadButton = new JButton("Load"); JButton loadButton = new JButton("Load");
loadButton.addActionListener(e -> { loadButton.addActionListener(e -> {
fileChooser.setDialogTitle("Loading"); fileChooser.setDialogTitle("Loading");
@ -293,6 +295,42 @@ public class FormMapWithSetLocomotives extends JComponent {
}); });
statusPanel.add(loadButton); statusPanel.add(loadButton);
// Сохранение выбранной карты
JButton saveMapButton = new JButton("Save Map");
saveMapButton.addActionListener(e -> {
fileChooser.setDialogTitle("Saving Map");
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
if (result == JFileChooser.APPROVE_OPTION ) {
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
if(listBoxMaps.getSelectedValue().toString() == null) return;
if (_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath())) {
JOptionPane.showMessageDialog(null, "Map saving success");
}
else JOptionPane.showMessageDialog(null, "Map saving fail");
repaint();
}
});
statusPanel.add(saveMapButton);
// Загрузка одной карты
JButton loadMapButton = new JButton("Load Map");
loadMapButton.addActionListener(e -> {
fileChooser.setDialogTitle("Loading");
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
if (result == JFileChooser.APPROVE_OPTION ) {
if (_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath())) {
JOptionPane.showMessageDialog(null, "Load Map success");
}
else JOptionPane.showMessageDialog(null, "Load Map failed");
}
ReloadMaps();
repaint();
});
statusPanel.add(loadMapButton);
formFrame.getContentPane().add(this); formFrame.getContentPane().add(this);
formFrame.setVisible(true); formFrame.setVisible(true);
} }

View File

@ -1,5 +1,6 @@
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
public class MapsCollection { public class MapsCollection {
@ -69,6 +70,67 @@ public class MapsCollection {
return true; 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) public boolean LoadData(String filename)
{ {
@ -90,7 +152,7 @@ public class MapsCollection {
AbstractMap map = null; AbstractMap map = null;
switch (elems[1]) switch (elems[1])
{ //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! {
case "class SimpleMap": case "class SimpleMap":
map = new SimpleMap(); map = new SimpleMap();
break; break;

View File

@ -34,6 +34,10 @@ public class SetLocomotivesGeneric <T>
return result; return result;
} }
public void Clear() {
_places.clear();
}
public T Get(int position) public T Get(int position)
{ {
if (position >= _maxCount || position < 0) if (position >= _maxCount || position < 0)