YOu can now add trips and you can also serialise them.

main
Fan-Wu Yang 9 years ago
parent 6a4fb731d5
commit caed3e64b3

@ -63,17 +63,32 @@ public class AddTripController extends Controller{
sat.isSelected(), sun.isSelected()};
boolean boolReoccur = reoccur.getValue() == "Yes";
String failure = "";
boolean fail = false;
if (stops.size() == 0){
failure += "You have not selected a route.\n";
fail = true;
}
if (ride.getValue() == null){
failure += "You must select a ride (vehicle) for this trip.\n";
fail = true;
}
for (TripStop stop: stops){
if (stop.getTime().equals("")){
failure += "You must have all times for stops filled out.\n";
fail = true;
break;
}
}
if (!failure.equals("")) {
Trip trip = new Trip(stops, direction.getValue(), ride.getValue(), days, boolReoccur, endDate.getValue().toString());
if (!fail) {
String date = "";
if (endDate.getValue() != null && boolReoccur == true){
date = endDate.getValue().toString();
}
Trip trip = new Trip(stops, direction.getValue(), ride.getValue(), days, boolReoccur, date);
System.out.println(trip);
Optional<ButtonType> result = popUp(Alert.AlertType.WARNING, "Warning!", "Crucial Information missing", failure, ButtonSets.YesNo);
parent.getSession().getDataManager().addTrip(trip);
if (result.get() == ButtonTypes.No){
System.out.println("My Trips page to show up and impletmeneted");//TODO implement here.
//changeScene(SceneCode.MY_TRIPS);

@ -15,19 +15,17 @@ public class DataManager {
private ObservableList<Ride> rides;
private ObservableList<Stop> stops;
private ObservableList<Route> routes;
private ObservableList<Trip> trips;
public DataManager(List rides, List stops, List routes){
public DataManager(List rides, List stops, List routes, List trips){
this.rides = FXCollections.observableArrayList(rides);
this.stops = FXCollections.observableArrayList(stops);
this.routes = FXCollections.observableArrayList(routes);
this.trips = FXCollections.observableArrayList(trips);
}
public DataManager(){
this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList());
/*rides = FXCollections.observableArrayList();
stops = FXCollections.observableArrayList();
routes = FXCollections.observableArrayList();*/
this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList());
}
public ObservableList<Ride> getRides() {
@ -74,4 +72,18 @@ public class DataManager {
return true;
}
public ObservableList<Trip> getTrips() {
return trips;
}
public boolean addTrip(Trip trip){
for (Trip t: trips){
if (trip.equals(t)){
return false;
}
}
trips.add(trip);
return true;
}
}

@ -46,6 +46,16 @@ public class Route {
return true;
}
@Override
public Route clone(){
ObservableList<Stop> stopObservableList = FXCollections.observableArrayList();
for (Stop stop: stops){
stopObservableList.add(stop.clone());
}
Route route = new Route(this.name, stopObservableList);
return route;
}
public String toString(){
return this.name;
}

@ -30,4 +30,9 @@ public class Stop {
public String toString(){
return address;
}
@Override
public Stop clone(){
return new Stop(this.getAddress());
}
}

@ -1,18 +1,23 @@
package model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by Gondr on 5/04/2017.
*/
public class Trip {
ObservableList<TripStop> route;
String direction;
Ride ride;
boolean[] days;
boolean reoccur;
String endDate;
protected transient ObservableList<TripStop> route;
private ArrayList<TripStop> serialisedRoute;
protected String direction;
protected Ride ride;
protected boolean[] days;
protected boolean reoccur;
protected String endDate;
static int MONDAY = 0;
static int TUESDAY = 1;
@ -36,4 +41,31 @@ public class Trip {
return tripString;
}
public boolean equals(Trip trip){
if (trip.direction.equals(direction) && ride.equals(trip.ride) && Arrays.equals(trip.days,days) && trip.reoccur == reoccur
&& trip.endDate.equals(endDate)){
for (int i = 0; i < trip.route.size(); i++){
if (!route.get(i).equals(trip.route.get(i))){
return false;
}
}
return true;
}
return false;
}
public void serialise(){
serialisedRoute = new ArrayList<>(route);
for (TripStop trip: serialisedRoute){
trip.serialise();
}
}
public void deserialise(){
route = FXCollections.observableArrayList(serialisedRoute);
for (TripStop trip: serialisedRoute){
trip.deserialise();
}
}
}

@ -9,8 +9,10 @@ import javafx.collections.ObservableList;
*/
public class TripStop{
private StringProperty time;
private StringProperty name;
private transient StringProperty time;
private transient StringProperty name;
private String serialiseTime;
private String serialiseName;
public TripStop(String name, String time){
this.name = new SimpleStringProperty(name);
@ -36,4 +38,20 @@ public class TripStop{
public StringProperty nameProperty() {
return name;
}
protected boolean equals(TripStop trip){
if (getTime().equals(trip.getTime()) && getName().equals(trip.getName())){
return true;
}
return false;
}
public void serialise(){
serialiseTime = time.getValue();
serialiseName = name.getValue();
}
public void deserialise(){
time = new SimpleStringProperty(serialiseTime);
name = new SimpleStringProperty(serialiseName);
}
}

@ -1,10 +1,7 @@
package utils;
import com.google.gson.reflect.TypeToken;
import model.DataManager;
import model.Ride;
import model.Route;
import model.Stop;
import model.*;
import java.io.*;
import java.lang.reflect.Constructor;
@ -23,17 +20,30 @@ public class DataManagerSerialiser extends Serialiser {
}
/**
* serialise DataManger
* @param dataManager
* @return
*/
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());
ArrayList<Trip> trips = new ArrayList<>(dataManager.getTrips());
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);
boolean serialRoutesSuccess = serialiseRoutes(routes, "serialisation/routes.json");
boolean serialTripsSuccess = serialiseTrips(trips, "serialisation/trips.json");
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess && serialTripsSuccess);
}
public boolean serialise(ArrayList<Route> routes, String filepath){
/**
* Serialises routes
* @param routes Routes to be serialised
* @param filepath File path to serialise it to.
* @return
*/
public boolean serialiseRoutes(ArrayList<Route> routes, String filepath){
for (Route route: routes){
route.serialise();
}
@ -49,6 +59,37 @@ public class DataManagerSerialiser extends Serialiser {
return true;
}
/**
* Serialises trips
* @param trips Trips to be serialised
* @param filepath File path to serialise it to.
* @return
*/
public boolean serialiseTrips(ArrayList<Trip> trips, String filepath){
for (Trip trip: trips){
trip.serialise();
}
try{
String path = getClass().getClassLoader().getResource(filepath).getPath();
Writer writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
gson.toJson(trips, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* loads the data manager from file
* @return whether the data was loaded properly or not
* @throws IOException No File
* @throws NoSuchMethodException No Method to Write
* @throws IllegalAccessException No access allowed to file
* @throws InvocationTargetException Thread error
* @throws InstantiationException Nothing set
*/
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());
@ -66,12 +107,15 @@ public class DataManagerSerialiser extends Serialiser {
route.deserialise();
}
// Constructor c = DataManager.class.getDeclaredConstructor(List.class, List.class, List.class);
// DataManager dataManager = (DataManager) c.newInstance(rides, stops, routes);
// return dataManager;
Reader reader4 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/trips.json"), "UTF-8");
ArrayList<Trip> trips = gson.fromJson(reader4, new TypeToken<ArrayList<Trip>>(){}.getType());
reader4.close();
for (Trip trip: trips){
trip.deserialise();
}
return new DataManager(rides, stops, routes);
// return new DataManager();
return new DataManager(rides, stops, routes, trips);
}
}

@ -1 +0,0 @@
{"rides":[],"stops":[{"address":"a"}],"routes":[]}

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1 +0,0 @@
{"rides":[],"stops":[{"address":"a"}],"routes":[]}

@ -1 +1 @@
[]
[{"model":"Nissan March","colour":"Baby Blue","licensePlate":"H19405661","year":2004,"numSeats":4}]

@ -0,0 +1 @@
[{"serialisedRoute":[{"serialiseTime":"3:00","serialiseName":"1 University Drive"},{"serialiseTime":"3:10","serialiseName":"1 Homestead Lane"}],"direction":"Home","ride":{"model":"Nissan March","colour":"Baby Blue","licensePlate":"H19405661","year":2004,"numSeats":4},"days":[true,false,false,false,false,false,false],"reoccur":false,"endDate":""}]
Loading…
Cancel
Save