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 {
private Map<String, ObservableList<Ride>> rides;
private Map<String, ObservableList<Stop>> stops;
private ObservableList<Route> routes;
private Map<String, ObservableList<Route>> routes;
private 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, 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.stops = convertStopMapToObserver(stops);
this.routes = FXCollections.observableArrayList(routes);
this.routes = convertRouteMapToObserver(routes);
this.trips = FXCollections.observableArrayList(trips);
this.sharedTrips = FXCollections.observableArrayList(sharedTrips);
this.users = users;
@@ -35,7 +35,7 @@ public class 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);
}
//////////////////////////////////
//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(){
rides.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());
}
//////////////////////////////
@@ -125,16 +149,16 @@ public class DataManager {
}
public ObservableList<Route> getRoutes() {
return routes;
return routes.get(Session.session.getUser().getID());
}
public boolean addRoute(Route route){
for (Route r: routes){
for (Route r: routes.get(Session.session.getUser().getID())){
if (route.equals(r)){
return false;
}
}
routes.add(route);
routes.get(Session.session.getUser().getID()).add(route);
return true;
}
+1
View File
@@ -20,6 +20,7 @@ public class Session {
public void setUser(User user) {
this.user = user;
dataManager.newLogged();
}
public DataManager getDataManager() {
+13 -7
View File
@@ -29,7 +29,7 @@ public class DataManagerSerialiser extends Serialiser {
//ArrayList<Ride> rides = new ArrayList<>(dataManager.getRides());
Map<String, List<Ride>> rides = dataManager.rideMapSerialised();
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<SharedTrip> sharedTrips = new ArrayList<>(dataManager.getSharedTrips());
boolean serialRidesSuccess = serialise(rides, "serialisation/rides.json");
@@ -49,9 +49,12 @@ public class DataManagerSerialiser extends Serialiser {
* @param filepath File path to serialise it to.
* @return
*/
public boolean serialiseRoutes(ArrayList<Route> routes, String filepath){
for (Route route: routes){
route.serialise();
public boolean serialiseRoutes(Map<String, List<Route>> routes, String filepath){
for(String s: routes.keySet()) {
List<Route> routeList = routes.get(s);
for (Route route : routeList) {
route.serialise();
}
}
try{
String path = getClass().getClassLoader().getResource(filepath).getPath();
@@ -174,11 +177,14 @@ public class DataManagerSerialiser extends Serialiser {
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());
HashMap<String, List<Route>> routes = gson.fromJson(reader3, new TypeToken<HashMap<String, ArrayList<Route>>>(){}.getType());
reader3.close();
for (Route route: routes){
route.deserialise();
for (String s: routes.keySet()) {
List<Route> routeList = routes.get(s);
for (Route route : routeList) {
route.deserialise();
}
}
Reader reader4 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/trips.json"), "UTF-8");