Routes now are in a HashMap

This commit is contained in:
Fan-Wu Yang
2017-06-02 01:58:50 +12:00
parent 97d52235e2
commit 8abfd1bc51
20 changed files with 50 additions and 19 deletions
+31 -7
View File
@@ -16,17 +16,17 @@ import java.util.Map;
public class DataManager { public class DataManager {
private Map<String, ObservableList<Ride>> rides; private Map<String, ObservableList<Ride>> rides;
private Map<String, ObservableList<Stop>> stops; private Map<String, ObservableList<Stop>> stops;
private ObservableList<Route> routes; private Map<String, ObservableList<Route>> routes;
private ObservableList<Trip> trips; private ObservableList<Trip> trips;
private ObservableList<SharedTrip> sharedTrips; private ObservableList<SharedTrip> sharedTrips;
private Map<String, User> users; private Map<String, User> users;
private Map<String, Driver> drivers;//seperated from users as they are two different types of accounts 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 private Map<String, List<String>> notifications; //indexed by user name
public DataManager(Map<String, List<Ride>> rides, Map<String, List<Stop>> stops, List 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, List trips, List sharedTrips, Map users, Map drivers, Map<String, List<String>> notifications){
this.rides = convertRideMapToObserver(rides); this.rides = convertRideMapToObserver(rides);
this.stops = convertStopMapToObserver(stops); this.stops = convertStopMapToObserver(stops);
this.routes = FXCollections.observableArrayList(routes); this.routes = convertRouteMapToObserver(routes);
this.trips = FXCollections.observableArrayList(trips); this.trips = FXCollections.observableArrayList(trips);
this.sharedTrips = FXCollections.observableArrayList(sharedTrips); this.sharedTrips = FXCollections.observableArrayList(sharedTrips);
this.users = users; this.users = users;
@@ -35,7 +35,7 @@ public class DataManager {
} }
public DataManager(){ public DataManager(){
this(new HashMap<>(), new HashMap<>(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>(), new HashMap<String, List<String>>()); this(new HashMap<>(), new HashMap<>(), new HashMap<>(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>(), new HashMap<String, List<String>>());
} }
//////////////////////////////////////// ////////////////////////////////////////
@@ -86,9 +86,33 @@ public class DataManager {
return convertStopMapToList(stops); return convertStopMapToList(stops);
} }
//////////////////////////////////
//Route
//////////////////////////////////
private Map<String, ObservableList<Route>> convertRouteMapToObserver(Map<String, List<Route>> routes){
HashMap<String, ObservableList<Route>> ob = new HashMap<>();
for (String key: routes.keySet()){
ob.put(key, FXCollections.observableArrayList(routes.get(key)));
}
return ob;
}
private Map<String, List<Route>> convertRouteMapToList(Map<String, ObservableList<Route>> routes){
HashMap<String, List<Route>> ob = new HashMap<>();
for (String key: routes.keySet()){
ob.put(key, new ArrayList<>(routes.get(key)));
}
return ob;
}
public Map<String, List<Route>> routesMapSerialised(){
return convertRouteMapToList(routes);
}
public void newLogged(){ public void newLogged(){
rides.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList()); rides.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
stops.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList()); stops.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
routes.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
notifications.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList()); notifications.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
} }
////////////////////////////// //////////////////////////////
@@ -125,16 +149,16 @@ public class DataManager {
} }
public ObservableList<Route> getRoutes() { public ObservableList<Route> getRoutes() {
return routes; return routes.get(Session.session.getUser().getID());
} }
public boolean addRoute(Route route){ public boolean addRoute(Route route){
for (Route r: routes){ for (Route r: routes.get(Session.session.getUser().getID())){
if (route.equals(r)){ if (route.equals(r)){
return false; return false;
} }
} }
routes.add(route); routes.get(Session.session.getUser().getID()).add(route);
return true; return true;
} }
+1
View File
@@ -20,6 +20,7 @@ public class Session {
public void setUser(User user) { public void setUser(User user) {
this.user = user; this.user = user;
dataManager.newLogged();
} }
public DataManager getDataManager() { public DataManager getDataManager() {
+13 -7
View File
@@ -29,7 +29,7 @@ public class DataManagerSerialiser extends Serialiser {
//ArrayList<Ride> rides = new ArrayList<>(dataManager.getRides()); //ArrayList<Ride> rides = new ArrayList<>(dataManager.getRides());
Map<String, List<Ride>> rides = dataManager.rideMapSerialised(); Map<String, List<Ride>> rides = dataManager.rideMapSerialised();
Map<String, List<Stop>> stops = dataManager.stopsMapSerialised(); Map<String, List<Stop>> stops = dataManager.stopsMapSerialised();
ArrayList<Route> routes = new ArrayList<>(dataManager.getRoutes()); Map<String, List<Route>> routes = dataManager.routesMapSerialised();
ArrayList<Trip> trips = new ArrayList<>(dataManager.getTrips()); ArrayList<Trip> trips = new ArrayList<>(dataManager.getTrips());
ArrayList<SharedTrip> sharedTrips = new ArrayList<>(dataManager.getSharedTrips()); ArrayList<SharedTrip> sharedTrips = new ArrayList<>(dataManager.getSharedTrips());
boolean serialRidesSuccess = serialise(rides, "serialisation/rides.json"); boolean serialRidesSuccess = serialise(rides, "serialisation/rides.json");
@@ -49,9 +49,12 @@ public class DataManagerSerialiser extends Serialiser {
* @param filepath File path to serialise it to. * @param filepath File path to serialise it to.
* @return * @return
*/ */
public boolean serialiseRoutes(ArrayList<Route> routes, String filepath){ public boolean serialiseRoutes(Map<String, List<Route>> routes, String filepath){
for (Route route: routes){ for(String s: routes.keySet()) {
route.serialise(); List<Route> routeList = routes.get(s);
for (Route route : routeList) {
route.serialise();
}
} }
try{ try{
String path = getClass().getClassLoader().getResource(filepath).getPath(); String path = getClass().getClassLoader().getResource(filepath).getPath();
@@ -174,11 +177,14 @@ public class DataManagerSerialiser extends Serialiser {
reader2.close(); reader2.close();
Reader reader3 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/routes.json"), "UTF-8"); Reader reader3 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/routes.json"), "UTF-8");
ArrayList<Route> routes = gson.fromJson(reader3, new TypeToken<ArrayList<Route>>(){}.getType()); HashMap<String, List<Route>> routes = gson.fromJson(reader3, new TypeToken<HashMap<String, ArrayList<Route>>>(){}.getType());
reader3.close(); reader3.close();
for (Route route: routes){ for (String s: routes.keySet()) {
route.deserialise(); List<Route> routeList = routes.get(s);
for (Route route : routeList) {
route.deserialise();
}
} }
Reader reader4 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/trips.json"), "UTF-8"); Reader reader4 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/trips.json"), "UTF-8");
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -1 +1 @@
{} {"79984862":[],"79984861":[]}
+1 -1
View File
@@ -1 +1 @@
{"79984862":[{"model":"Nissan March","colour":"Baby Blue","licensePlate":"EPU001","year":2004,"numSeats":5}]} {"79984862":[{"model":"Nissan March","colour":"Baby Blue","licensePlate":"EPU001","year":2004,"numSeats":5}],"79984861":[]}
+1 -1
View File
@@ -1 +1 @@
[{"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"}]}] {"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":[]}
+1 -1
View File
@@ -1 +1 @@
{"79984862":[{"address":"1 Avonhead Road"},{"address":"100 Yaldhurst Road"},{"address":"120 Maidstone Road"},{"address":"1 University Drive"},{"address":"1 Homestead Lane"}]} {"79984862":[{"address":"1 Avonhead Road"},{"address":"100 Yaldhurst Road"},{"address":"120 Maidstone Road"},{"address":"1 University Drive"},{"address":"1 Homestead Lane"}],"79984861":[]}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.