Serialises Routes, stops and rides.
This commit is contained in:
@@ -61,7 +61,7 @@ public class AddTripController extends Controller{
|
||||
private void addTrip() throws Exception {
|
||||
boolean[] days = {mon.isSelected(), tues.isSelected(), wed.isSelected(), thur.isSelected(), fri.isSelected(),
|
||||
sat.isSelected(), sun.isSelected()};
|
||||
boolean boolReoccur = reoccur.getValue() == "Yes"? true: false;
|
||||
boolean boolReoccur = reoccur.getValue() == "Yes";
|
||||
String failure = "";
|
||||
if (stops.size() == 0){
|
||||
failure += "You have not selected a route.\n";
|
||||
@@ -70,7 +70,7 @@ public class AddTripController extends Controller{
|
||||
failure += "You must select a ride (vehicle) for this trip.\n";
|
||||
}
|
||||
|
||||
if (failure != "") {
|
||||
if (!failure.equals("")) {
|
||||
Trip trip = new Trip(stops, direction.getValue(), ride.getValue(), days, boolReoccur, endDate.getValue().toString());
|
||||
System.out.println(trip);
|
||||
Optional<ButtonType> result = popUp(Alert.AlertType.WARNING, "Warning!", "Crucial Information missing", failure, ButtonSets.YesNo);
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
package controllers;
|
||||
|
||||
import controllers.BaseController;
|
||||
import controllers.Controller;
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
|
||||
import javafx.stage.Stage;
|
||||
import model.DataManager;
|
||||
import model.SceneCode;
|
||||
import model.Session;
|
||||
import utils.DataManagerSerialiser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
||||
@@ -34,8 +40,14 @@ public class Main extends Application {
|
||||
base = root;
|
||||
baseController = (BaseController) loader.getController();
|
||||
baseController.setParent(this);
|
||||
|
||||
this.session = new Session();
|
||||
try{
|
||||
DataManagerSerialiser dataManagerSerialiser = new DataManagerSerialiser();
|
||||
DataManager dataManager = dataManagerSerialiser.load();
|
||||
this.session.setDataManager(dataManager);
|
||||
} catch (IOException e){
|
||||
|
||||
}
|
||||
|
||||
//set up stage
|
||||
stage.setTitle("UC RSS");
|
||||
@@ -43,6 +55,12 @@ public class Main extends Application {
|
||||
stage.setMinWidth(600);
|
||||
stage.show();
|
||||
|
||||
stage.setOnCloseRequest(e -> {
|
||||
DataManagerSerialiser dataManagerSerialiser = new DataManagerSerialiser();
|
||||
dataManagerSerialiser.serialise(session.getDataManager());
|
||||
Platform.exit();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,4 +107,8 @@ public class Main extends Application {
|
||||
public Stage getPrimaryStage() {
|
||||
return primaryStage;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import javafx.collections.ObservableList;
|
||||
import javafx.collections.ObservableSet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 4/04/2017.
|
||||
@@ -15,10 +16,18 @@ public class DataManager {
|
||||
private ObservableList<Stop> stops;
|
||||
private ObservableList<Route> routes;
|
||||
|
||||
|
||||
public DataManager(List rides, List stops, List routes){
|
||||
this.rides = FXCollections.observableArrayList(rides);
|
||||
this.stops = FXCollections.observableArrayList(stops);
|
||||
this.routes = FXCollections.observableArrayList(routes);
|
||||
}
|
||||
|
||||
public DataManager(){
|
||||
rides = FXCollections.observableArrayList();
|
||||
this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList());
|
||||
/*rides = FXCollections.observableArrayList();
|
||||
stops = FXCollections.observableArrayList();
|
||||
routes = FXCollections.observableArrayList();
|
||||
routes = FXCollections.observableArrayList();*/
|
||||
}
|
||||
|
||||
public ObservableList<Ride> getRides() {
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package utils;
|
||||
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import model.DataManager;
|
||||
import model.Ride;
|
||||
import model.Route;
|
||||
import model.Stop;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 19/05/2017.
|
||||
*/
|
||||
public class DataManagerSerialiser extends Serialiser {
|
||||
|
||||
public DataManagerSerialiser(){
|
||||
|
||||
}
|
||||
|
||||
public boolean serialise(DataManager dataManager){
|
||||
ArrayList<Ride> rides = new ArrayList<>(dataManager.getRides());
|
||||
ArrayList<Stop> stops = new ArrayList<>(dataManager.getStops());
|
||||
ArrayList<Route> routes = new ArrayList<>(dataManager.getRoutes());
|
||||
boolean serialRidesSuccess = serialise(rides, "serialisation/rides.json");
|
||||
boolean serialStopsSuccess = serialise(stops, "serialisation/stops.json");
|
||||
boolean serialRoutesSuccess = serialise(routes, "serialisation/routes.json");
|
||||
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess);
|
||||
}
|
||||
|
||||
public DataManager load() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/rides.json"), "UTF-8");
|
||||
ArrayList<Ride> rides = gson.fromJson(reader, new TypeToken<ArrayList<Ride>>(){}.getType());
|
||||
reader.close();
|
||||
|
||||
Reader reader2 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/stops.json"), "UTF-8");
|
||||
ArrayList<Stop> stops = gson.fromJson(reader2, new TypeToken<ArrayList<Stop>>(){}.getType());
|
||||
reader2.close();
|
||||
|
||||
Reader reader3 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/routes.json"), "UTF-8");
|
||||
ArrayList<Route> routes = gson.fromJson(reader3, new TypeToken<ArrayList<Route>>(){}.getType());
|
||||
reader3.close();
|
||||
|
||||
// Constructor c = DataManager.class.getDeclaredConstructor(List.class, List.class, List.class);
|
||||
// DataManager dataManager = (DataManager) c.newInstance(rides, stops, routes);
|
||||
// return dataManager;
|
||||
|
||||
return new DataManager(rides, stops, routes);
|
||||
// return new DataManager();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Gondr on 19/05/2017.
|
||||
*/
|
||||
public abstract class Serialiser {
|
||||
protected static Gson gson = new GsonBuilder().create();
|
||||
|
||||
protected boolean serialise(List list, String filepath){
|
||||
try{
|
||||
String path = getClass().getClassLoader().getResource(filepath).getPath();
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
|
||||
gson.toJson(list, writer);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"rides":[],"stops":[{"address":"a"}],"routes":[]}
|
||||
@@ -0,0 +1 @@
|
||||
[]
|
||||
@@ -0,0 +1 @@
|
||||
[]
|
||||
@@ -0,0 +1 @@
|
||||
[]
|
||||
Reference in New Issue
Block a user