diff --git a/src/main/java/controllers/AddRideController.java b/src/main/java/controllers/AddRideController.java new file mode 100644 index 0000000..f4b5f65 --- /dev/null +++ b/src/main/java/controllers/AddRideController.java @@ -0,0 +1,73 @@ +package controllers; + +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.TextField; +import model.Ride; +import model.SceneCode; + +import java.net.URL; +import java.util.ResourceBundle; + +/** + * Created by Gondr on 4/04/2017. + */ +public class AddRideController extends Controller{ + @FXML + TextField model; + @FXML + TextField colour; + @FXML + TextField license; + @FXML + TextField year; + @FXML + TextField numSeats; + + public void addRide() throws Exception { + //check for invalid data + String failure = ""; + if (model.getText().length() == 0){ + failure += "Mandatory Field model is not entered.\n"; + } + if (colour.getText().length() == 0){ + failure += "Mandatory Field colour is not entered.\n"; + } + if (license.getText().length() == 0){ + failure += "Mandatory Field license is not entered.\n"; + } + if (year.getText().length() == 0){ + failure += "Mandatory Field year is not entered.\n"; + } + int numYear = 0; + try{ + numYear = Integer.parseInt(year.getText()); + //TODO other checks here like the year cannot be too old, cannot be more than this year etc + }catch (Exception e){ + failure += "Year must be a Integer.\n"; + } + if (numSeats.getText().length() == 0){ + failure += "Mandatory Field number of seats is not entered.\n"; + } + int seats = 0; + try{ + seats = Integer.parseInt(numSeats.getText()); + //TODO other checks here like the year cannot be too old, cannot be more than this year etc + }catch (Exception e){ + failure += "Seats must be a Integer.\n"; + } + if (failure == ""){ + //pass + Ride ride = new Ride(model.getText(), colour.getText(), license.getText(), numYear, seats); + parent.getSession().getDataManager().addRides(ride); + changeScene(SceneCode.MY_RIDES); + }else{ + popUp(Alert.AlertType.WARNING, "Unable to Add Ride!", "Invalid Data", failure); + } + } + + @Override + public void initialize(URL location, ResourceBundle resources) { + + } +} diff --git a/src/main/java/controllers/BaseController.java b/src/main/java/controllers/BaseController.java new file mode 100644 index 0000000..8859385 --- /dev/null +++ b/src/main/java/controllers/BaseController.java @@ -0,0 +1,48 @@ +package controllers; + +import javafx.fxml.FXML; +import javafx.scene.Parent; +import javafx.scene.control.Label; +import javafx.scene.layout.GridPane; +import model.SceneCode; + +import java.net.URL; +import java.util.ResourceBundle; + +/** + * Created by Gondr on 4/04/2017. + */ +public class BaseController extends Controller { + + @FXML + GridPane base; + @FXML + Label loggedInAs; + + private final int childNum = 3;//number of children before appending + + public void addRide() throws Exception { + changeScene(SceneCode.ADD_RIDE); + } + + public void myRides() throws Exception{ + changeScene(SceneCode.MY_RIDES); + } + + public void setContent(Parent parent1){ + //remove all children that do not belong to the original fxml + while (base.getChildren().size() > childNum) { + base.getChildren().remove(base.getChildren().size() - 1); + } + base.add(parent1, 1, 0); + loggedInAs.setText(String.format("Logged in as %s", parent.getSession().getUserCode())); + } + + public void logout() throws Exception { + changeScene(SceneCode.MAIN); + } + + @Override + public void initialize(URL location, ResourceBundle resources) { + } +} diff --git a/src/main/java/controllers/Controller.java b/src/main/java/controllers/Controller.java new file mode 100644 index 0000000..99daa37 --- /dev/null +++ b/src/main/java/controllers/Controller.java @@ -0,0 +1,47 @@ +package controllers; + + +import javafx.fxml.Initializable; +import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; +import javafx.stage.Modality; +import javafx.stage.Stage; +import model.SceneCode; + +import java.net.URL; +import java.util.ResourceBundle; + +/** + * Created by Gondr on 4/04/2017. + */ +public abstract class Controller implements Initializable { + + protected Main parent; + + public Controller(){ + + } + + public void setParent(Main parent){ + this.parent = parent; + } + + public void changeScene(SceneCode scene) throws Exception { + parent.replaceSceneContent(scene); + } + + public void runLater(){ + + } + + public void popUp(Alert.AlertType alertType, String title, String header, String content){ + Alert alert = new Alert(alertType); + alert.setTitle(title); + alert.setHeaderText(header); + alert.setContentText(content); + alert.showAndWait(); + } + +} diff --git a/src/main/java/controllers/HomeController.java b/src/main/java/controllers/HomeController.java new file mode 100644 index 0000000..108e147 --- /dev/null +++ b/src/main/java/controllers/HomeController.java @@ -0,0 +1,15 @@ +package controllers; + +import java.net.URL; +import java.util.ResourceBundle; + +/** + * Created by Gondr on 4/04/2017. + */ +public class HomeController extends Controller{ + + @Override + public void initialize(URL location, ResourceBundle resources) { + + } +} diff --git a/src/main/java/Main.java b/src/main/java/controllers/Main.java similarity index 54% rename from src/main/java/Main.java rename to src/main/java/controllers/Main.java index 021e0fb..a9bb73f 100644 --- a/src/main/java/Main.java +++ b/src/main/java/controllers/Main.java @@ -1,3 +1,4 @@ +package controllers; import javafx.application.Application; import javafx.fxml.FXMLLoader; @@ -7,6 +8,7 @@ import javafx.scene.Scene; import javafx.stage.Stage; import model.SceneCode; +import model.Session; import java.io.InputStream; @@ -17,17 +19,25 @@ import java.io.InputStream; public class Main extends Application { private Stage primaryStage; + private Scene base; + private BaseController baseController; + private Session session; public void start(Stage stage) throws Exception { this.primaryStage = stage; replaceSceneContent(SceneCode.MAIN); - //FXMLLoader loader = new FXMLLoader(getClass().getResource("scenes/main.fxml")); + //load up base pane + FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("scenes/base.fxml")); + Parent root = loader.load(); + base = new Scene(root, SceneCode.prefWidth, SceneCode.prefHeight); + baseController = (BaseController) loader.getController(); + baseController.setParent(this); - //Parent root = loader.load(); - //Scene scene = new Scene(root, 500, 400); + this.session = new Session(); + + //set up stage stage.setTitle("UC RSS"); - //stage.setScene(scene); stage.show(); } @@ -38,7 +48,7 @@ public class Main extends Application { * @return * @throws Exception */ - public Initializable replaceSceneContent(SceneCode fxml) throws Exception { + public void replaceSceneContent(SceneCode fxml) throws Exception { FXMLLoader loader = new FXMLLoader(); InputStream in = getClass().getClassLoader().getResourceAsStream(fxml.getPath()); Parent page; @@ -49,11 +59,24 @@ public class Main extends Application { } Scene scene = null; if (fxml.isLoadMenu()){ - + scene = base; + baseController.setContent(page); }else { scene = new Scene(page, SceneCode.prefWidth, SceneCode.prefHeight); } primaryStage.setScene(scene); - return (Initializable) loader.getController(); + Controller controller = loader.getController(); + if (controller != null) { + controller.setParent(this); + controller.runLater(); + } + } + + public Session getSession() { + return session; + } + + public Stage getPrimaryStage() { + return primaryStage; } } diff --git a/src/main/java/controllers/MainController.java b/src/main/java/controllers/MainController.java index 44922d6..23cc980 100644 --- a/src/main/java/controllers/MainController.java +++ b/src/main/java/controllers/MainController.java @@ -1,6 +1,12 @@ package controllers; +import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Alert; +import javafx.scene.control.Control; +import javafx.scene.control.PasswordField; +import javafx.scene.control.TextField; +import model.SceneCode; import java.net.URL; import java.util.ResourceBundle; @@ -8,7 +14,29 @@ import java.util.ResourceBundle; /** * Created by samschofield on 15/03/17. */ -public class MainController implements Initializable { +public class MainController extends Controller{ + + @FXML + TextField username; + @FXML + PasswordField password; + + public boolean validateLogin(){ + if (username.getText().equals(password.getText())){ + return true; + } + return false; + } + + public void login() throws Exception { + //validate login + if (validateLogin()){ + parent.getSession().setUserCode(username.getText()); + changeScene(SceneCode.HOME); + }else{ + popUp(Alert.AlertType.WARNING, "Warning!", "Invalid Login!", "Your password and username do not match a record in our database."); + } + } @Override public void initialize(URL location, ResourceBundle resources) { diff --git a/src/main/java/controllers/MyRidesController.java b/src/main/java/controllers/MyRidesController.java new file mode 100644 index 0000000..586e842 --- /dev/null +++ b/src/main/java/controllers/MyRidesController.java @@ -0,0 +1,28 @@ +package controllers; + +import javafx.fxml.FXML; +import javafx.scene.control.ListCell; +import javafx.scene.control.ListView; +import model.Ride; + +import java.net.URL; +import java.util.ResourceBundle; + +/** + * Created by Gondr on 4/04/2017. + */ +public class MyRidesController extends Controller{ + + @FXML + ListView rideList; + + @Override + public void initialize(URL location, ResourceBundle resources) { + + } + + @Override + public void runLater() { + rideList.setItems(parent.getSession().getDataManager().getRides()); + } +} diff --git a/src/main/java/model/DataManager.java b/src/main/java/model/DataManager.java new file mode 100644 index 0000000..7fc4817 --- /dev/null +++ b/src/main/java/model/DataManager.java @@ -0,0 +1,39 @@ +package model; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableArray; +import javafx.collections.ObservableList; +import javafx.collections.ObservableSet; + +import java.util.ArrayList; + +/** + * Created by Gondr on 4/04/2017. + */ +public class DataManager { + private ObservableList rides; + + public DataManager(){ + rides = FXCollections.observableArrayList(); + } + + public ObservableList getRides() { + return rides; + } + + public void setRides(ObservableList rides) { + this.rides = rides; + } + + public void addRides(Ride ride){ + rides.add(ride); + } + + public void removeRide(Ride ride){ + rides.remove(ride); + } + + public void removeRide(int i){ + rides.remove(i); + } +} diff --git a/src/main/java/model/Ride.java b/src/main/java/model/Ride.java new file mode 100644 index 0000000..f1c6be1 --- /dev/null +++ b/src/main/java/model/Ride.java @@ -0,0 +1,25 @@ +package model; + +/** + * Created by Gondr on 4/04/2017. + */ +public class Ride { + + private String model, colour, licensePlate; + private int year, numSeats; + + public Ride(String model, String colour, String licensePlate, int year, int numSeats){ + //model,colour, license plate, year and the number of physical seats + this.model = model; + this.colour = colour; + this.licensePlate = licensePlate; + this.year = year; + this.numSeats = numSeats; + } + + + @Override + public String toString(){ + return String.format("%s - %s %s", licensePlate, colour, model); + } +} diff --git a/src/main/java/model/SceneCode.java b/src/main/java/model/SceneCode.java index 51cd8ac..392bb51 100644 --- a/src/main/java/model/SceneCode.java +++ b/src/main/java/model/SceneCode.java @@ -11,19 +11,22 @@ import java.io.InputStream; * Created by fwy13 on 4/04/17. */ public enum SceneCode { - MAIN("main", false); + //screens before log in + MAIN("main", false), BASE("base", false), + //screens after login + HOME("home"),ADD_RIDE("addride"),MY_RIDES("myrides"); private String path; private boolean loadMenu; - public static int prefWidth = 1200; - public static int prefHeight = 900; + public static int prefWidth = 600; + public static int prefHeight = 400; SceneCode(String path){ this(path, true); } SceneCode(String path, boolean loadMenu){ - this.path = path; + this.path = "scenes/"+path+".fxml"; this.loadMenu = loadMenu; } diff --git a/src/main/java/model/Session.java b/src/main/java/model/Session.java index 4c26079..ce67d7f 100644 --- a/src/main/java/model/Session.java +++ b/src/main/java/model/Session.java @@ -6,8 +6,25 @@ package model; public class Session { private String userCode;//this is like fwy13 + DataManager dataManager; public Session(){ + dataManager = new DataManager(); + } + + public String getUserCode() { + return userCode; + } + + public void setUserCode(String userCode) { + this.userCode = userCode; + } + + public DataManager getDataManager() { + return dataManager; + } + public void setDataManager(DataManager dataManager) { + this.dataManager = dataManager; } } diff --git a/src/main/resources/scenes/addride.fxml b/src/main/resources/scenes/addride.fxml new file mode 100644 index 0000000..b8a6917 --- /dev/null +++ b/src/main/resources/scenes/addride.fxml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +