Notifications will can now appear
This commit is contained in:
@@ -10,6 +10,7 @@ import model.Session;
|
||||
import model.User;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
@@ -30,18 +31,29 @@ 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().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;
|
||||
}
|
||||
|
||||
public void getNotifications(){
|
||||
List<String> notifications = Session.session.getDataManager().getNotifications().get(Session.session.getUser().getStudentNumber());
|
||||
if (notifications != null){
|
||||
for (String s: notifications){
|
||||
popUp(Alert.AlertType.INFORMATION, "Notification!", "You have received a Notification", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void login() throws Exception {
|
||||
//validate login
|
||||
if (validateLogin(username.getText(), password.getText())){
|
||||
parent.getSession().setUserCode(username.getText());
|
||||
getNotifications();
|
||||
changeScene(SceneCode.HOME);
|
||||
}else{
|
||||
popUp(Alert.AlertType.WARNING, "Warning!", "Invalid Login!", "Your password and username do not match a record in our database.");
|
||||
|
||||
@@ -21,8 +21,9 @@ public class DataManager {
|
||||
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(List rides, List stops, List routes, List trips, List sharedTrips, Map users, Map drivers){
|
||||
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);
|
||||
this.stops = FXCollections.observableArrayList(stops);
|
||||
this.routes = FXCollections.observableArrayList(routes);
|
||||
@@ -30,10 +31,11 @@ public class DataManager {
|
||||
this.sharedTrips = FXCollections.observableArrayList(sharedTrips);
|
||||
this.users = users;
|
||||
this.drivers = drivers;
|
||||
this.notifications = notifications;
|
||||
}
|
||||
|
||||
public DataManager(){
|
||||
this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>());
|
||||
this(FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), FXCollections.observableArrayList(), new HashMap<String, User>(), new HashMap<String, Driver>(), new HashMap<String, List<String>>());
|
||||
}
|
||||
|
||||
public ObservableList<Ride> getRides() {
|
||||
@@ -132,5 +134,7 @@ public class DataManager {
|
||||
return drivers;
|
||||
}
|
||||
|
||||
|
||||
public Map<String, List<String>> getNotifications() {
|
||||
return notifications;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ package model;
|
||||
*/
|
||||
public class Session {
|
||||
|
||||
private String userCode;//this is like fwy13
|
||||
private User user;//this is like fwy13
|
||||
DataManager dataManager;
|
||||
public static Session session = new Session();
|
||||
|
||||
@@ -14,12 +14,12 @@ public class Session {
|
||||
dataManager = new DataManager();
|
||||
}
|
||||
|
||||
public String getUserCode() {
|
||||
return userCode;
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUserCode(String userCode) {
|
||||
this.userCode = userCode;
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public DataManager getDataManager() {
|
||||
|
||||
@@ -56,6 +56,10 @@ public class User {
|
||||
return p.equals(password);
|
||||
}
|
||||
|
||||
public int getStudentNumber() {
|
||||
return studentNumber;
|
||||
}
|
||||
|
||||
public void serialise(){
|
||||
try {
|
||||
BufferedImage bi = SwingFXUtils.fromFXImage(photo, null);
|
||||
|
||||
@@ -38,7 +38,8 @@ 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");
|
||||
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess && serialTripsSuccess && serialSharedTripsSuccess && serialUsersSuccess && serialDriversSuccess);
|
||||
boolean serialNotificationsSuccess = serialiseNotifications(dataManager.getNotifications(), "serialisation/notification.json");
|
||||
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess && serialTripsSuccess && serialSharedTripsSuccess && serialUsersSuccess && serialDriversSuccess && serialNotificationsSuccess);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,6 +140,20 @@ public class DataManagerSerialiser extends Serialiser {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean serialiseNotifications(Map<String, List<String>> notifications, String filepath){
|
||||
try{
|
||||
String path = getClass().getClassLoader().getResource(filepath).getPath();
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
|
||||
gson.toJson(notifications, writer);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* loads the data manager from file
|
||||
* @return whether the data was loaded properly or not
|
||||
@@ -195,7 +210,11 @@ public class DataManagerSerialiser extends Serialiser {
|
||||
drivers.get(driverkey).deserialise();
|
||||
}
|
||||
|
||||
return new DataManager(rides, stops, routes, trips, sharedTrips, users, drivers);
|
||||
Reader reader8 = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/notifications.json"), "UTF-8");
|
||||
HashMap<String, List<String>> notifications = gson.fromJson(reader8, new TypeToken<HashMap<String, List<String>>>(){}.getType());
|
||||
reader8.close();
|
||||
|
||||
return new DataManager(rides, stops, routes, trips, sharedTrips, users, drivers, notifications);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
Reference in New Issue
Block a user