Trips are now in a hash map
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user