From ee13b9ac0c7584ab8822f47836150ee996abf913 Mon Sep 17 00:00:00 2001 From: shadowik Date: Tue, 29 Nov 2022 07:32:13 +0400 Subject: [PATCH] FirstTry --- .../ControllerMapWithSetBus.java | 74 ++++++++++++++++++- .../example/doubledeckerbus/DrawingBus.java | 11 +++ .../example/doubledeckerbus/DrawingDDB.java | 6 ++ .../doubledeckerbus/DrawingObjectBus.java | 9 +++ .../example/doubledeckerbus/ExtensionBus.java | 62 ++++++++++++++++ .../doubledeckerbus/IDrawingObject.java | 2 + .../MapWithSetBusesGeneric.java | 19 +++++ .../doubledeckerbus/MapsCollection.java | 68 +++++++++++++++-- .../doubledeckerbus/FormMapWithSetBus.fxml | 11 +++ 9 files changed, 254 insertions(+), 8 deletions(-) create mode 100644 DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ExtensionBus.java diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java index e9e5dcb..dc860b7 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java @@ -10,8 +10,10 @@ import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.*; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; +import javafx.stage.FileChooser; import javafx.stage.Stage; +import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Objects; @@ -40,6 +42,9 @@ public class ControllerMapWithSetBus { @FXML private Canvas canvasBus; + @FXML + private Button buttonLeft; + @FXML private ChoiceBox comboBoxSelectorMap; @@ -128,11 +133,11 @@ public class ControllerMapWithSetBus { { return; } - DrawingObjectBus objectArmoredVehicle = new DrawingObjectBus(bus); + DrawingObjectBus objectBus = new DrawingObjectBus(bus); String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem(); Alert alert; - if (selectedMapName != null && selectedMapName.length() != 0 && _mapsCollection.get(selectedMapName).add(objectArmoredVehicle) != -1) + if (selectedMapName != null && selectedMapName.length() != 0 && _mapsCollection.get(selectedMapName).add(objectBus) != -1) { alert = new Alert(Alert.AlertType.INFORMATION, "Объект добавлен", ButtonType.OK); _mapsCollection.get(selectedMapName).ShowSet(gc); @@ -185,7 +190,7 @@ public class ControllerMapWithSetBus { { return; } - DrawingObjectBus deleteBus = _mapsCollection.get(selected).getDeletedBus(); + DrawingObjectBus deleteBus = (DrawingObjectBus) _mapsCollection.get(selected).getDeletedBus(); if (deleteBus != null) { ControllerBus._bus = deleteBus.getBus(); FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml")); @@ -276,6 +281,68 @@ public class ControllerMapWithSetBus { _mapsCollection.GetId(selected).MoveObject(dir); } + @FXML + private void ButtonSave_Click(ActionEvent event) throws IOException { + Alert infoAlert; + Stage stage = (Stage)(canvasBus.getScene().getWindow()); + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Save"); + + FileChooser.ExtensionFilter fileExtensions = new FileChooser.ExtensionFilter("TEXT files (*.txt)", + "*.txt"); + fileChooser.getExtensionFilters().add(fileExtensions); + + File selectedDirectory = fileChooser.showSaveDialog(stage); + if (selectedDirectory != null) + { + String filepath = selectedDirectory.getPath(); + if (_mapsCollection.SaveData(filepath)) + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "Save was successful", ButtonType.OK); + } + else + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "The file was not saved", ButtonType.OK); + } + } + else + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "The file was not saved", ButtonType.OK); + } + infoAlert.showAndWait(); + } + + @FXML + private void ButtonLoad_Click(ActionEvent event) throws IOException { + Alert infoAlert; + Stage stage = (Stage)(buttonLeft.getScene().getWindow()); + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Load"); + + FileChooser.ExtensionFilter fileExtensions = new FileChooser.ExtensionFilter("TEXT files (*.txt)", "*.txt"); + fileChooser.getExtensionFilters().add(fileExtensions); + + File selectedDirectory = fileChooser.showOpenDialog(stage); + if (selectedDirectory != null) + { + String filepath = selectedDirectory.getPath(); + if (_mapsCollection.LoadData(filepath)) + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "Load was successful", ButtonType.OK); + ReloadMaps(); + } + else + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "The file was not loaded", ButtonType.OK); + } + } + else + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "The file was not loaded", ButtonType.OK); + } + infoAlert.showAndWait(); + } + private void showStorage() { if (selected == null) { @@ -286,6 +353,7 @@ public class ControllerMapWithSetBus { _mapsCollection.GetId(selected).ShowSet(gc); } + } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java index 8b96671..3fdcc85 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java @@ -26,6 +26,17 @@ public class DrawingBus { Bus = CreateBus(speed, weight, bodyColor, countOfDoors); } + public DrawingBus(int speed, float weight, Color bodyColor, int countOfDoors, IDrawingDoors TypeDoors) { + _speed = speed; + _weight = weight; + _bodyColor = bodyColor; + _countOfDoors = countOfDoors; + EntityBus bus = new EntityBus(speed, weight, bodyColor); + Doors = TypeDoors; + Doors.setCountOfDoors(countOfDoors); + Bus = bus; + } + private EntityBus CreateBus(int speed, float weight, Color bodyColor, int countOfDoors) { _speed = speed; _weight = weight; diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java index 8884246..b4184dd 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java @@ -14,6 +14,12 @@ public class DrawingDDB extends DrawingBus{ Bus = CreateDDB(speed, weight, bodyColor, countOfDoors, extraColor, ladder, secondStage); } + public DrawingDDB(int speed, float weight, Color bodyColor, int countOfDoors, IDrawingDoors typeDoor, + Color extraColor, boolean ladder, boolean secondStage) { + super(speed, weight, bodyColor, countOfDoors, typeDoor); + Bus = CreateDDB(speed, weight, bodyColor, countOfDoors, extraColor, ladder, secondStage); + } + private EntityDDB CreateDDB(int speed, float weight, Color bodyColor, int countOfDoors, Color extraColor, boolean ladder, boolean secondStage) { _speed = speed; diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java index 3abfd35..c90b0fa 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java @@ -25,6 +25,11 @@ public class DrawingObjectBus implements IDrawingObject { return null; } + @Override + public String GetInfo() { + return ExtensionBus.GetDataForSave(_bus); + } + public void MoveObject(Direction direction) { _bus.MoveTransport(direction); @@ -43,4 +48,8 @@ public class DrawingObjectBus implements IDrawingObject { public DrawingBus getBus() { return _bus; } + + public static IDrawingObject Create(String data) { + return new DrawingObjectBus(ExtensionBus.CreateDrawingBus(data)); + } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ExtensionBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ExtensionBus.java new file mode 100644 index 0000000..7154de6 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ExtensionBus.java @@ -0,0 +1,62 @@ +package com.example.doubledeckerbus; + +import javafx.scene.paint.Color; + +public class ExtensionBus { + private static char _separatorForObject = ':'; + + public static DrawingBus CreateDrawingBus(String info) { + String[] strs = info.split(String.valueOf(_separatorForObject)); + if (strs.length == 5) { + return new DrawingBus(Integer.parseInt(strs[0]), + Float.parseFloat(strs[1].replace(',', '.')), Color.web(strs[2]), + Integer.parseInt(strs[3]), CreateDoors(strs[4])); + } + if (strs.length == 8) { + return new DrawingDDB(Integer.parseInt(strs[0]), Float.parseFloat(strs[1].replace(',', '.')), + Color.web(strs[2]), Integer.parseInt(strs[3]), CreateDoors(strs[4]), + Color.web(strs[5]), Boolean.parseBoolean(strs[6]), + Boolean.parseBoolean(strs[7])); + } + return null; + } + + private static IDrawingDoors CreateDoors(String name) { + switch (name) { + case "Triangle" -> { + return new DrawingTriangleDoors(); + } + case "Oval" -> { + return new DrawingEllipsoidDoors(); + } + case "Rect" -> { + return new DrawingDoors(); + } + } + return new DrawingDoors(); + } + + private static String CreateDoors(IDrawingDoors doors) { + if (doors instanceof DrawingEllipsoidDoors) { + return "Oval"; + } + else if (doors instanceof DrawingTriangleDoors) { + return "Triangle"; + } + else { + return "Rect"; + } + } + + public static String GetDataForSave(DrawingBus drawingBus) + { + var bus = drawingBus.Bus; + var str = String.format("%d:%f:%s:%d:%s", drawingBus._speed, drawingBus._weight, drawingBus._bodyColor.toString(), + drawingBus._countOfDoors, CreateDoors(drawingBus.Doors));; + if (!(bus instanceof EntityDDB ddb)) + { + return str; + } + return str + String.format(":%s:%b:%b", ddb.ExtraColor.toString(), ddb.Ladder, ddb.SecondStage); + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/IDrawingObject.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/IDrawingObject.java index b7e76f9..b56edc2 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/IDrawingObject.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/IDrawingObject.java @@ -12,4 +12,6 @@ public interface IDrawingObject { void DrawingObject(GraphicsContext g); Position GetCurrentPosition(); + + String GetInfo(); } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java index 43074d1..cd33e38 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java @@ -147,5 +147,24 @@ public class MapWithSetBusesGeneric> _mapStorages; + HashMap> _mapStorages; + private final char separatorDict = '|'; + private final char separatorData = ';'; public List Keys() { return _mapStorages.keySet().stream().toList(); @@ -27,7 +33,7 @@ public class MapsCollection { public void AddMap(String name, AbstractMap map) { if (Keys().contains(name)) return; - _mapStorages.put(name, new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.put(name, new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); } public void DelMap(String name) @@ -35,7 +41,7 @@ public class MapsCollection { _mapStorages.remove(name); } - public MapWithSetBusesGeneric GetId(String id) + public MapWithSetBusesGeneric GetId(String id) { return _mapStorages.get(id); } @@ -46,7 +52,7 @@ public class MapsCollection { return result; } - public MapWithSetBusesGeneric get(String id) { + public MapWithSetBusesGeneric get(String id) { if (_mapStorages.containsKey(id)) { return _mapStorages.get(id); @@ -54,7 +60,7 @@ public class MapsCollection { return null; } - public DrawingObjectBus get(String name, int id) { + public IDrawingObject get(String name, int id) { if (_mapStorages.containsKey(name)) { return _mapStorages.get(name).getBus(id); @@ -62,4 +68,56 @@ public class MapsCollection { return null; } + public boolean SaveData(String filename) throws IOException + { + Files.deleteIfExists(Paths.get(filename)); + + try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), + StandardCharsets.UTF_8))) + { + writer.write("MapsCollection" + System.lineSeparator()); + for (String storageKey : _mapStorages.keySet()) + { + MapWithSetBusesGeneric storage = _mapStorages.get(storageKey); + writer.write(String.format("%s|%s\n", storageKey, storage.GetData(separatorDict, separatorData))); + } + } + return true; + } + + public boolean LoadData(String filename) throws IOException + { + File file = new File(filename); + if(!file.exists() || file.isDirectory()) + { + return false; + } + + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) + { + String str = reader.readLine(); + if (str == null || !str.contains("MapsCollection")) + { + return false; + } + _mapStorages.clear(); + while ((str = reader.readLine()) != null) + { + String[] elem = str.split(String.format("\\%c", separatorDict)); + AbstractMap map = switch (elem[1]) + { + case "SimpleMap" -> new SimpleMap(); + case "WaterMap" -> new WaterMap(); + default -> null; + }; + _mapStorages.put(elem[0], new MapWithSetBusesGeneric<>(_pictureWidth, _pictureHeight, map)); + if (elem.length == 3) + { + _mapStorages.get(elem[0]).LoadData(elem[2].split(String.format("%c", separatorData))); + } + } + } + return true; + } + } diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml index 0047568..6888adb 100644 --- a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml @@ -6,6 +6,7 @@ + @@ -25,6 +26,16 @@ + + + + +