Перенесена база лаб6

This commit is contained in:
Данила Мочалов 2022-11-19 19:32:56 +04:00
parent 1d30c011aa
commit 9f39107385
7 changed files with 43 additions and 16 deletions

View File

@ -1,4 +1,5 @@
import java.awt.*; import java.awt.*;
import java.util.Objects;
import java.util.Random; import java.util.Random;
public class DrawningLocomotive { public class DrawningLocomotive {
@ -177,9 +178,9 @@ public class DrawningLocomotive {
IDrawningExtra drawningExtra = null; IDrawningExtra drawningExtra = null;
EntityLocomotive Locomotive = null; EntityLocomotive Locomotive = null;
String[] strs = info.split(Character.toString(_separatorForObject)); String[] strs = info.split(Character.toString(_separatorForObject));
if (strs[5] == "Simple") drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); if (strs[5].equals("Simple")) drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
if (strs[5] == "Star") drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); if (strs[5].equals("Star")) drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
if (strs[5] == "Round") drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); if (Objects.equals(strs[5], "Round")) drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
if (drawningExtra == null) return null; if (drawningExtra == null) return null;
if (strs.length == 7) if (strs.length == 7)
{ {

View File

@ -6,11 +6,11 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{
private Color color; private Color color;
public void setExtraNum(int num) { public void setExtraNum(int num) {
switch (num) { switch (num) {
case 0: { case 3: {
wheelsCount = WheelsCount.Three; wheelsCount = WheelsCount.Three;
break; break;
} }
case 1: { case 4: {
wheelsCount = WheelsCount.Four; wheelsCount = WheelsCount.Four;
break; break;
} }

View File

@ -6,11 +6,11 @@ public class ExtraStarWheelDraw implements IDrawningExtra{
private Color color; private Color color;
public void setExtraNum(int num) { public void setExtraNum(int num) {
switch (num) { switch (num) {
case 0: { case 3: {
wheelsCount = WheelsCount.Three; wheelsCount = WheelsCount.Three;
break; break;
} }
case 1: { case 4: {
wheelsCount = WheelsCount.Four; wheelsCount = WheelsCount.Four;
break; break;
} }

View File

@ -108,7 +108,6 @@ public class FormLocomotive extends JComponent{
super.repaint(); super.repaint();
} }
public static void main(String[] args) { public static void main(String[] args) {
new FormMapWithSetLocomotives(); new FormMapWithSetLocomotives();
} }
} }

View File

@ -263,7 +263,35 @@ public class FormMapWithSetLocomotives extends JComponent {
dialog.setVisible(true); dialog.setVisible(true);
} }
}); });
statusPanel.add(showDeletedButton);
JFileChooser fileChooser = new JFileChooser();
JButton saveButton = new JButton("Save");
saveButton.addActionListener(e -> {
fileChooser.setDialogTitle("Saving");
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
if (result == JFileChooser.APPROVE_OPTION ) {
if (_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath())) {
JOptionPane.showMessageDialog(null, "Save success");
}
else JOptionPane.showMessageDialog(null, "Save failed");
}
});
statusPanel.add(saveButton);
JButton loadButton = new JButton("Load");
loadButton.addActionListener(e -> {
fileChooser.setDialogTitle("Loading");
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
if (result == JFileChooser.APPROVE_OPTION ) {
if (_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath())) {
JOptionPane.showMessageDialog(null, "Load success");
}
else JOptionPane.showMessageDialog(null, "Load failed");
}
ReloadMaps();
repaint();
});
statusPanel.add(loadButton);
formFrame.getContentPane().add(this); formFrame.getContentPane().add(this);
formFrame.setVisible(true); formFrame.setVisible(true);

View File

@ -180,7 +180,6 @@ public class MapWithSetLocomotivesGeneric
/// Получение данных в виде строки /// Получение данных в виде строки
public String GetData(char separatorType, char separatorData) public String GetData(char separatorType, char separatorData)
{ {
//string data = $"{_map.GetType().Name}{separatorType}";
String data = ""+ _map.getClass() + separatorType; String data = ""+ _map.getClass() + separatorType;
for (var locomotive : _setLocomotives.GetLocomotives()) for (var locomotive : _setLocomotives.GetLocomotives())
{ {

View File

@ -15,7 +15,7 @@ public class MapsCollection {
/// Высота окна отрисовки /// Высота окна отрисовки
private final int _pictureHeight; private final int _pictureHeight;
// Сепараторы // Сепараторы
private final char separatorDict = '|'; private final String separatorDict = "\\|";
private final char separatorData = ';'; private final char separatorData = ';';
/// Конструктор /// Конструктор
public MapsCollection(int pictureWidth, int pictureHeight) public MapsCollection(int pictureWidth, int pictureHeight)
@ -60,7 +60,7 @@ public class MapsCollection {
try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
bw.write("MapsCollection\n"); bw.write("MapsCollection\n");
for (var storage : _mapStorages.entrySet()) { for (var storage : _mapStorages.entrySet()) {
bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict, separatorData) + "\n"); bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n");
} }
} }
catch (IOException ex) { catch (IOException ex) {
@ -86,18 +86,18 @@ public class MapsCollection {
_mapStorages.clear(); _mapStorages.clear();
while ((curLine = br.readLine()) != null) { while ((curLine = br.readLine()) != null) {
var elems = curLine.split(Character.toString(separatorDict)); var elems = curLine.split(separatorDict);
AbstractMap map = null; AbstractMap map = null;
switch (elems[1]) switch (elems[1])
{ //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! { //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case "SimpleMap": case "class SimpleMap":
map = new SimpleMap(); map = new SimpleMap();
break; break;
case "SpikeMap": case "class SpikeMap":
map = new SpikeMap(); map = new SpikeMap();
break; break;
case "RailMap": case "class RailMap":
map = new RailMap(); map = new RailMap();
break; break;
} }