Trips are now in a hash map

main
Fan-Wu Yang 9 years ago
parent 8abfd1bc51
commit b48041242e

@ -17,17 +17,17 @@ public class DataManager {
private Map<String, ObservableList<Ride>> rides;
private Map<String, ObservableList<Stop>> stops;
private Map<String, ObservableList<Route>> routes;
private ObservableList<Trip> trips;
private Map<String, ObservableList<Trip>> trips;
private ObservableList<SharedTrip> sharedTrips;
private Map<String, User> users;
private Map<String, Driver> drivers;//seperated from users as they are two different types of accounts
private Map<String, List<String>> notifications; //indexed by user name
public DataManager(Map<String, List<Ride>> rides, Map<String, List<Stop>> stops, Map<String, List<Route>> routes, List trips, List sharedTrips, Map users, Map drivers, Map<String, List<String>> notifications){
public DataManager(Map<String, List<Ride>> rides, Map<String, List<Stop>> stops, Map<String, List<Route>> routes, Map<String, List<Trip>> trips, List sharedTrips, Map users, Map drivers, Map<String, List<String>> notifications){
this.rides = convertRideMapToObserver(rides);
this.stops = convertStopMapToObserver(stops);
this.routes = convertRouteMapToObserver(routes);
this.trips = FXCollections.observableArrayList(trips);
this.trips = convertTripMapToObserver(trips);
this.sharedTrips = FXCollections.observableArrayList(sharedTrips);
this.users = users;
this.drivers = drivers;
@ -35,7 +35,7 @@ public class DataManager {
}
public DataManager(){
this(new HashMap<>(), new HashMap<>(), new HashMap<>(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>(), new HashMap<String, List<String>>());
this(new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>(), new HashMap<String, List<String>>());
}
////////////////////////////////////////
@ -109,10 +109,34 @@ public class DataManager {
return convertRouteMapToList(routes);
}
//////////////////////////////////
//Route
//////////////////////////////////
private Map<String, ObservableList<Trip>> convertTripMapToObserver(Map<String, List<Trip>> trips){
HashMap<String, ObservableList<Trip>> ob = new HashMap<>();
for (String key: trips.keySet()){
ob.put(key, FXCollections.observableArrayList(trips.get(key)));
}
return ob;
}
private Map<String, List<Trip>> convertTripMapToList(Map<String, ObservableList<Trip>> trips){
HashMap<String, List<Trip>> ob = new HashMap<>();
for (String key: trips.keySet()){
ob.put(key, new ArrayList<>(trips.get(key)));
}
return ob;
}
public Map<String, List<Trip>> tripsMapSerialised(){
return convertTripMapToList(trips);
}
public void newLogged(){
rides.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
stops.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
routes.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
trips.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
notifications.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
}
//////////////////////////////
@ -163,16 +187,16 @@ public class DataManager {
}
public ObservableList<Trip> getTrips() {
return trips;
return trips.get(Session.session.getUser().getID());
}
public boolean addTrip(Trip trip){
for (Trip t: trips){
for (Trip t: trips.get(Session.session.getUser().getID())){
if (trip.equals(t)){
return false;
}
}
trips.add(trip);
trips.get(Session.session.getUser().getID()).add(trip);
return true;
}

@ -30,7 +30,7 @@ public class DataManagerSerialiser extends Serialiser {
Map<String, List<Ride>> rides = dataManager.rideMapSerialised();
Map<String, List<Stop>> stops = dataManager.stopsMapSerialised();
Map<String, List<Route>> routes = dataManager.routesMapSerialised();
ArrayList<Trip> trips = new ArrayList<>(dataManager.getTrips());
Map<String, List<Trip>> trips = dataManager.tripsMapSerialised();
ArrayList<SharedTrip> sharedTrips = new ArrayList<>(dataManager.getSharedTrips());
boolean serialRidesSuccess = serialise(rides, "serialisation/rides.json");
boolean serialStopsSuccess = serialise(stops, "serialisation/stops.json");
@ -74,9 +74,12 @@ public class DataManagerSerialiser extends Serialiser {
* @param filepath File path to serialise it to.
* @return
*/
public boolean serialiseTrips(ArrayList<Trip> trips, String filepath){
for (Trip trip: trips){
trip.serialise();
public boolean serialiseTrips(Map<String, List<Trip>> trips, String filepath){
for (String key: trips.keySet()) {
List<Trip> tripsList = trips.get(key);
for (Trip trip : tripsList) {
trip.serialise();
}
}
try{
String path = getClass().getClassLoader().getResource(filepath).getPath();
@ -188,11 +191,14 @@ public class DataManagerSerialiser extends Serialiser {
}
Reader reader4 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/trips.json"), "UTF-8");
ArrayList<Trip> trips = gson.fromJson(reader4, new TypeToken<ArrayList<Trip>>(){}.getType());
HashMap<String, List<Trip>> trips = gson.fromJson(reader4, new TypeToken<HashMap<String, ArrayList<Trip>>>(){}.getType());
reader4.close();
for (Trip trip: trips){
trip.deserialise();
for (String key: trips.keySet()) {
List<Trip> tripsList = trips.get(key);
for (Trip trip : tripsList) {
trip.deserialise();
}
}
Reader reader5 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/sharedtrips.json"), "UTF-8");

File diff suppressed because one or more lines are too long

@ -1 +1 @@
{"79984862":[],"79984861":[]}
{"79984862":[]}

@ -1 +1 @@
{"79984862":[{"model":"Nissan March","colour":"Baby Blue","licensePlate":"EPU001","year":2004,"numSeats":5}],"79984861":[]}
{"79984862":[{"model":"Nissan March","colour":"Baby Blue","licensePlate":"EPU001","year":2004,"numSeats":5}]}

@ -1 +1 @@
{"79984862":[{"name":"Home to University","serialisedStops":[{"address":"1 Avonhead Road"},{"address":"1 University Drive"}]},{"name":"Friends Route","serialisedStops":[{"address":"100 Yaldhurst Road"},{"address":"120 Maidstone Road"},{"address":"1 Homestead Lane"}]}],"79984861":[]}
{"79984862":[{"name":"Home to University","serialisedStops":[{"address":"1 Avonhead Road"},{"address":"1 University Drive"}]},{"name":"Friends Route","serialisedStops":[{"address":"100 Yaldhurst Road"},{"address":"120 Maidstone Road"},{"address":"1 Homestead Lane"}]}]}

@ -1 +1 @@
{"79984862":[{"address":"1 Avonhead Road"},{"address":"100 Yaldhurst Road"},{"address":"120 Maidstone Road"},{"address":"1 University Drive"},{"address":"1 Homestead Lane"}],"79984861":[]}
{"79984862":[{"address":"1 Avonhead Road"},{"address":"100 Yaldhurst Road"},{"address":"120 Maidstone Road"},{"address":"1 University Drive"},{"address":"1 Homestead Lane"}]}

@ -1 +1 @@
[{"serialisedRoute":[{"serialiseTime":"9:00","serialiseName":"1 Avonhead Road"},{"serialiseTime":"9:15","serialiseName":"1 University Drive"}],"direction":"University","ride":{"model":"Nissan March","colour":"Baby Blue","licensePlate":"EPU001","year":2004,"numSeats":5},"days":[false,true,true,false,true,false,false],"reoccur":true,"endDate":"30-12-2017","name":"Home to Uni"}]
{"79984862":[{"serialisedRoute":[{"serialiseTime":"9:00","serialiseName":"1 Avonhead Road"},{"serialiseTime":"9:15","serialiseName":"1 University Drive"}],"direction":"University","ride":{"model":"Nissan March","colour":"Baby Blue","licensePlate":"EPU001","year":2004,"numSeats":5},"days":[false,true,true,false,true,false,false],"reoccur":true,"endDate":"30-12-2017","name":"Home to Uni"}]}
Loading…
Cancel
Save