Refactored Rides to be only for the user

main
Fan-Wu Yang 9 years ago
parent 61400c3c1f
commit dc2e94a297

@ -5,6 +5,7 @@ import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import model.SceneCode;
import model.Session;
import java.net.URL;
import java.util.ResourceBundle;
@ -71,7 +72,7 @@ public class BaseController extends Controller {
base.getChildren().remove(base.getChildren().size() - 1);
}
base.add(parent1, 1, 0);
loggedInAs.setText(String.format("Logged in as %s", parent.getSession().getUserCode()));
loggedInAs.setText(String.format("Logged in as %d", Session.session.getUser().getStudentNumber()));
}
public void logout() throws Exception {

@ -48,6 +48,7 @@ public class Main extends Application {
} catch (Exception e){
//this exception is for when the files is formatted wrong.
this.session.setDataManager(new DataManager());
Session.session.setUser(new User(79984862, null, null, null, null, null, null));
Ride ride = new Ride("Nissan March", "Baby Blue", "EPU001", 2004, 5);
Stop stop1 = new Stop("1 Avonhead Road");

@ -30,13 +30,17 @@ public class MainController extends Controller{
}
public boolean validateLogin(String user, String pass){
if (Session.session.getDataManager().getUsers().get(user).checkPassword(pass) && User.AccountType.getValueOf(accountType.getValue()) == User.AccountType.PASSENGER){
Session.session.setUser(Session.session.getDataManager().getUsers().get(user));
return true;
if (Session.session.getDataManager().getUsers().get(user) != null) {
if (Session.session.getDataManager().getUsers().get(user).checkPassword(pass) && User.AccountType.getValueOf(accountType.getValue()) == User.AccountType.PASSENGER) {
Session.session.setUser(Session.session.getDataManager().getUsers().get(user));
return true;
}
}
if (Session.session.getDataManager().getDrivers().get(user).checkPassword(pass) && User.AccountType.getValueOf(accountType.getValue()) == User.AccountType.DRIVER){
Session.session.setUser(Session.session.getDataManager().getDrivers().get(user));
return true;
if (Session.session.getDataManager().getDrivers().get(user) != null) {
if (Session.session.getDataManager().getDrivers().get(user).checkPassword(pass) && User.AccountType.getValueOf(accountType.getValue()) == User.AccountType.DRIVER) {
Session.session.setUser(Session.session.getDataManager().getDrivers().get(user));
return true;
}
}
return false;
}

@ -132,7 +132,7 @@ public class SharedTripsController extends Controller{
//book button
Button bookRide = new Button("Book Ride");
bookRide.setOnAction(e->{
trip.bookSeat(parent.getSession().getUserCode());
trip.bookSeat(parent.getSession().session.getUser());
tripDetails.close();
search();
popUp(Alert.AlertType.CONFIRMATION, "Success!", "You have Successfully booked a ride!", "The ride has been successfully booked and can be viewed in your booked rides.");

@ -14,7 +14,7 @@ import java.util.Map;
* Created by Gondr on 4/04/2017.
*/
public class DataManager {
private ObservableList<Ride> rides;
private Map<String, ObservableList<Ride>> rides;
private ObservableList<Stop> stops;
private ObservableList<Route> routes;
private ObservableList<Trip> trips;
@ -23,8 +23,8 @@ public class DataManager {
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(List rides, List stops, List routes, List trips, List sharedTrips, Map users, Map drivers, Map<String, List<String>> notifications){
this.rides = FXCollections.observableArrayList(rides);
public DataManager(Map<String, List<Ride>> rides, List stops, List routes, List trips, List sharedTrips, Map users, Map drivers, Map<String, List<String>> notifications){
this.rides = convertRideMapToObserver(rides);
this.stops = FXCollections.observableArrayList(stops);
this.routes = FXCollections.observableArrayList(routes);
this.trips = FXCollections.observableArrayList(trips);
@ -35,23 +35,48 @@ public class DataManager {
}
public DataManager(){
this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>(), new HashMap<String, List<String>>());
this(new HashMap<String, List<Ride>>(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>(), new HashMap<String, List<String>>());
}
private Map<String, ObservableList<Ride>> convertRideMapToObserver(Map<String, List<Ride>> rides){
HashMap<String, ObservableList<Ride>> ob = new HashMap<>();
for (String key: rides.keySet()){
ob.put(key, FXCollections.observableArrayList(rides.get(key)));
}
return ob;
}
private Map<String, List<Ride>> convertRideMapToList(Map<String, ObservableList<Ride>> rides){
HashMap<String, List<Ride>> ob = new HashMap<>();
for (String key: rides.keySet()){
ob.put(key, new ArrayList<Ride>(rides.get(key)));
}
return ob;
}
public Map<String, List<Ride>> rideMapSerialised(){
return convertRideMapToList(rides);
}
public ObservableList<Ride> getRides() {
return rides;
return rides.get(Session.session.getUser().getID());
}
public void addRides(Ride ride){
rides.add(ride);
rides.putIfAbsent(Session.session.getUser().getID(), FXCollections.observableArrayList());
rides.get(Session.session.getUser().getID()).add(ride);
}
public void removeRide(Ride ride){
rides.remove(ride);
if (rides.get(Session.session.getUser().getID()) != null) {
rides.get(Session.session.getUser().getID()).remove(ride);
}
}
public void removeRide(int i){
rides.remove(i);
if (rides.get(Session.session.getUser().getID()) != null) {
rides.get(Session.session.getUser().getID()).remove(i);
}
}
public ObservableList<Stop> getStops() {

@ -7,11 +7,11 @@ public class SharedTrip extends Trip {
private int seatsAvailable;
private String[] userBookings;
private User[] userBookings;
public SharedTrip (Trip trip, int seatsAvailable){
super(trip.name, trip.route, trip.direction, trip.ride, trip.days, trip.reoccur, trip.endDate);
userBookings = new String[this.ride.getNumSeats() - 1];
userBookings = new User[this.ride.getNumSeats() - 1];
this.seatsAvailable = seatsAvailable;
}
@ -32,7 +32,7 @@ public class SharedTrip extends Trip {
return seatsAvailable;
}
public void bookSeat(String user){
public void bookSeat(User user){
userBookings[ride.getNumSeats() - seatsAvailable - 1] = user;
seatsAvailable -= 1;
}

@ -20,6 +20,7 @@ public class User {
protected AccountType accountType;
transient protected Image photo;
private byte[] image;
protected String ID;
public enum AccountType{
PASSENGER("Passenger"), DRIVER("Driver");
@ -42,6 +43,7 @@ public class User {
public User(int studentNumber, String email, String password, String address, String homephone, String mobilephone,
Image photo){
this.studentNumber = studentNumber;
this.ID = String.valueOf(studentNumber);
this.email = email;
this.password = password;
this.address = address;
@ -60,6 +62,10 @@ public class User {
return studentNumber;
}
public String getID(){
return ID;
}
public void serialise(){
try {
BufferedImage bi = SwingFXUtils.fromFXImage(photo, null);

@ -26,7 +26,8 @@ public class DataManagerSerialiser extends Serialiser {
* @return
*/
public boolean serialise(DataManager dataManager){
ArrayList<Ride> rides = new ArrayList<>(dataManager.getRides());
//ArrayList<Ride> rides = new ArrayList<>(dataManager.getRides());
Map<String, List<Ride>> rides = dataManager.rideMapSerialised();
ArrayList<Stop> stops = new ArrayList<>(dataManager.getStops());
ArrayList<Route> routes = new ArrayList<>(dataManager.getRoutes());
ArrayList<Trip> trips = new ArrayList<>(dataManager.getTrips());
@ -38,7 +39,7 @@ public class DataManagerSerialiser extends Serialiser {
boolean serialSharedTripsSuccess = serialiseSharedTrips(sharedTrips, "serialisation/sharedtrips.json");
boolean serialUsersSuccess = serialiseUsers(dataManager.getUsers(), "serialisation/users.json");
boolean serialDriversSuccess = serialiseDrivers(dataManager.getDrivers(), "serialisation/drivers.json");
boolean serialNotificationsSuccess = serialiseNotifications(dataManager.getNotifications(), "serialisation/notification.json");
boolean serialNotificationsSuccess = serialiseNotifications(dataManager.getNotifications(), "serialisation/notifications.json");
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess && serialTripsSuccess && serialSharedTripsSuccess && serialUsersSuccess && serialDriversSuccess && serialNotificationsSuccess);
}
@ -165,7 +166,7 @@ public class DataManagerSerialiser extends Serialiser {
*/
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());
HashMap<String, List<Ride>> rides = gson.fromJson(reader, new TypeToken<HashMap<String, ArrayList<Ride>>>(){}.getType());
reader.close();
Reader reader2 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/stops.json"), "UTF-8");

@ -7,6 +7,7 @@ import com.google.gson.reflect.TypeToken;
import java.io.*;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
/**
@ -28,4 +29,17 @@ public abstract class Serialiser {
return true;
}
protected boolean serialise(Map map, String filepath){
try{
String path = getClass().getClassLoader().getResource(filepath).getPath();
Writer writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
gson.toJson(map, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}

Binary file not shown.

File diff suppressed because one or more lines are too long

@ -1 +1 @@
[{"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}]}

@ -1 +1 @@
[{"seatsAvailable":0,"userBookings":["a","a","a","a"],"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"}]
[]

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save