Skip to content

Commit

Permalink
Implemented Forecasts section and Remove Forecasts feature
Browse files Browse the repository at this point in the history
Moved old CreateForecast to this section and refactored mainGUI and Window classes
  • Loading branch information
Botxan committed Apr 24, 2022
1 parent a9b009b commit 5212efe
Show file tree
Hide file tree
Showing 15 changed files with 1,098 additions and 695 deletions.
10 changes: 8 additions & 2 deletions src/main/java/businessLogic/BlFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,14 @@ public Question createQuestion(Event event, String question, float betMinimum)
* @param fee fee of the forecast
* @throws ForecastAlreadyExistException
*/
@WebMethod public void addForecast(Question question, String result, double fee) throws ForecastAlreadyExistException;

@WebMethod public Forecast createForecast(Question question, String result, double fee) throws ForecastAlreadyExistException;

/**
* It removes the given question.
* @param forecastID the forecast identification
*/
@WebMethod public void removeForecast(int forecastID);

/**
* It registers a user into the database
* @param username an instance of the username
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/businessLogic/BlFacadeImplementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,26 @@ public Question createQuestion(Event event, String question, float betMinimum) t
Question q = dbManager.createQuestion(event, question, betMinimum);
dbManager.close();

refreshUser();
return q;
}

@WebMethod
public void addForecast(Question question, String result, double fee) throws ForecastAlreadyExistException {
public Forecast createForecast(Question question, String result, double fee) throws ForecastAlreadyExistException {
dbManager.open(false);
Forecast f = dbManager.addForecast(question, result, fee);
dbManager.close();

return f;
}

@WebMethod
@Override
public void removeForecast(int forecastID) {
dbManager.open(false);
dbManager.addForecast(question, result, fee);
dbManager.removeForecast(forecastID);
dbManager.close();

refreshUser();
}

@WebMethod
Expand Down
612 changes: 332 additions & 280 deletions src/main/java/dataAccess/DataAccess.java

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion src/main/java/domain/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,16 @@ public Forecast setCorrectForecast(Forecast correctForecast) {
public String toString(){
return question;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Question other = (Question) obj;
return questionID == other.questionID;
}
}
108 changes: 40 additions & 68 deletions src/main/java/ui/MainGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui;

import businessLogic.BlFacade;
import domain.User;
import exceptions.UserNotFoundException;
import javafx.application.Platform;
import javafx.event.EventHandler;
Expand All @@ -20,6 +21,7 @@
import utils.Window;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;
Expand All @@ -30,7 +32,7 @@ public class MainGUI {

public Window navBarLag, welcomeLag, loginLag, registerLag, browseEventsLag,
userMenuLag, userOverviewLag, profileLag, betsLag, movementsLag,
adminMenuLag, createForecastLag, adminOverviewLag, eventsLag, questionsLag, forecastsLag;
adminMenuLag, adminOverviewLag, eventsLag, questionsLag, forecastsLag;
private BlFacade businessLogic;
private Stage stage;
private Scene scene;
Expand Down Expand Up @@ -76,58 +78,30 @@ public MainGUI(BlFacade bl) {
}

/**
* Creates a new window and assigns its corresponding value
* UI and Controller
* @param fxmlfile the name of the fxml file
* Creates a new window and assigns its corresponding UI and Controller
* @param fxmlfile the path of the fxml resource
* @param controllerFile the path to the controller's classname
* @return the new window
* @throws IOException in case de load.loader() fails.
* @throws IOException in case loader.load() fails.
*/
private Window load(String fxmlfile, String title, int width, int height) throws IOException {
Window window = new Window(title, width, height);
private Window load(String fxmlfile, String controllerFile, int width, int height) throws IOException {
Window window = new Window(width, height);
FXMLLoader loader = new FXMLLoader(MainGUI.class.getResource(fxmlfile), ResourceBundle.getBundle("Etiquetas", Locale.getDefault()));

// Set the controller constructor with business logic
loader.setControllerFactory(controllerClass -> {
if (controllerClass == NavBarController.class) {
return new NavBarController(businessLogic);
} else if (controllerClass == BrowseEventsController.class) {
return new BrowseEventsController(businessLogic);
} else if (controllerClass == LoginController.class) {
return new LoginController(businessLogic);
} else if (controllerClass == RegisterController.class) {
return new RegisterController(businessLogic);
} else if (controllerClass == WelcomeController.class) {
return new WelcomeController(businessLogic);
} else if (controllerClass == UserMenuController.class) {
return new UserMenuController(businessLogic);
}else if (controllerClass == CreateForecastController.class) {
return new CreateForecastController(businessLogic);
} else if (controllerClass == AdminMenuController.class) {
return new AdminMenuController(businessLogic);
} else if (controllerClass == UserOverviewController.class) {
return new UserOverviewController(businessLogic);
} else if (controllerClass == ProfileController.class) {
return new ProfileController(businessLogic);
} else if (controllerClass == BetsController.class) {
return new BetsController(businessLogic);
} else if (controllerClass == MovementsController.class) {
return new MovementsController(businessLogic);
} else if (controllerClass == AdminOverviewController.class) {
return new AdminOverviewController(businessLogic);
} else if (controllerClass == EventsController.class) {
return new EventsController(businessLogic);
} else if (controllerClass == QuestionsController.class) {
return new QuestionsController(businessLogic);
} else if (controllerClass == ForecastsController.class) {
return new ForecastsController(businessLogic);
} else {
// default behavior for controllerFactory:
try {
return controllerClass.getDeclaredConstructor().newInstance();
} catch (Exception exc) {
exc.printStackTrace();
throw new RuntimeException(exc); // fatal, just bail...
}
try {
// Obtain the controller by its name
Class<?> c = Class.forName("uicontrollers." + controllerFile);
// Obtain the desired constructor (there's only one, and takes the business logic as param)
Constructor<?> cons = c.getConstructor(BlFacade.class);
// Return the instance of the constructor with the name
return cons.newInstance(businessLogic);
} catch (Exception e) {
throw new RuntimeException(e);
}
});

window.setUi(loader.load());
((Controller) loader.getController()).setMainApp(this);
window.setController(loader.getController());
Expand All @@ -150,11 +124,11 @@ public void init(Stage stage) throws IOException {
// BufferedImage img = ImageIO.read(getClass().getResourceAsStream("/icon/favicon.png"));
// taskbar.setIconImage(img);

navBarLag = load("/NavBarGUI.fxml", "NavBar", SCENE_WIDTH, SCENE_HEIGHT);
welcomeLag = load("/WelcomeGUI.fxml", "Welcome", 350, 500);
loginLag = load("/Login.fxml", "Login", 700, 500);
registerLag = load("/RegisterGUI.fxml", "Register", 900, 600);
browseEventsLag = load("/BrowseEvents.fxml", "BrowseEvents", SCENE_WIDTH, SCENE_HEIGHT);
navBarLag = load("/NavBarGUI.fxml", "NavBarController", SCENE_WIDTH, SCENE_HEIGHT);
welcomeLag = load("/WelcomeGUI.fxml", "WelcomeController", 350, 500);
loginLag = load("/Login.fxml", "LoginController", 700, 500);
registerLag = load("/RegisterGUI.fxml", "RegisterController", 900, 600);
browseEventsLag = load("/BrowseEvents.fxml", "BrowseEventsController", SCENE_WIDTH, SCENE_HEIGHT);

//Update user money everytime a scene is shown.
stage.setOnShowing(new EventHandler<WindowEvent>() {
Expand Down Expand Up @@ -185,19 +159,18 @@ public void handle(WindowEvent event) {
*/
public void loadLoggedWindows() throws IOException {
// Admin windows
adminOverviewLag = load("/admin/AdminOverview.fxml", "AdminOverview", SCENE_WIDTH, SCENE_HEIGHT);
eventsLag = load("/admin/Events.fxml", "Events", SCENE_WIDTH, SCENE_HEIGHT);
questionsLag = load("/admin/Questions.fxml", "Questions", SCENE_WIDTH, SCENE_HEIGHT);
forecastsLag = load("/admin/Forecasts.fxml", "Forecasts", SCENE_WIDTH, SCENE_HEIGHT);
createForecastLag = load("/CreateForecast.fxml", "CreateForecast", SCENE_WIDTH, SCENE_HEIGHT);
adminMenuLag = load("/admin/AdminMenu.fxml", "admin.AdminMenuController", SCENE_WIDTH, SCENE_HEIGHT);
adminOverviewLag = load("/admin/AdminOverview.fxml", "admin.AdminOverviewController", SCENE_WIDTH, SCENE_HEIGHT);
eventsLag = load("/admin/Events.fxml", "admin.EventsController", SCENE_WIDTH, SCENE_HEIGHT);
questionsLag = load("/admin/Questions.fxml", "admin.QuestionsController", SCENE_WIDTH, SCENE_HEIGHT);
forecastsLag = load("/admin/Forecasts.fxml", "admin.ForecastsController", SCENE_WIDTH, SCENE_HEIGHT);

// User windows
adminMenuLag = load("/admin/AdminMenu.fxml", "AdminMenu", SCENE_WIDTH, SCENE_HEIGHT);
userMenuLag = load("/user/UserMenuGUI.fxml", "UserMenu", SCENE_WIDTH, SCENE_HEIGHT);
userOverviewLag = load("/user/UserOverview.fxml", "UserOverview", SCENE_WIDTH, SCENE_HEIGHT);
profileLag = load("/user/Profile.fxml", "Profile", SCENE_WIDTH, SCENE_HEIGHT);
betsLag = load("/user/Bets.fxml", "Bets", SCENE_WIDTH, SCENE_HEIGHT);
movementsLag = load("/user/Movements.fxml", "Movements", SCENE_WIDTH, SCENE_HEIGHT);
userMenuLag = load("/user/UserMenuGUI.fxml", "user.UserMenuController", SCENE_WIDTH, SCENE_HEIGHT);
userOverviewLag = load("/user/UserOverview.fxml", "user.UserOverviewController", SCENE_WIDTH, SCENE_HEIGHT);
profileLag = load("/user/Profile.fxml", "user.ProfileController", SCENE_WIDTH, SCENE_HEIGHT);
betsLag = load("/user/Bets.fxml", "user.BetsController", SCENE_WIDTH, SCENE_HEIGHT);
movementsLag = load("/user/Movements.fxml", "user.MovementsController", SCENE_WIDTH, SCENE_HEIGHT);
}

/**
Expand Down Expand Up @@ -256,14 +229,15 @@ public void handle(MouseEvent event) {
* @param window the window.
*/
private void showScene(Window window) {
stage.setTitle(ResourceBundle.getBundle("Etiquetas", Locale.getDefault()).getString(window.getTitle()));

// Do not show navbar in Welcome, Login, Register
if (Arrays.asList("Welcome", "Login", "Register").contains(window.getTitle()))
if (window.getController() instanceof WelcomeController ||
window.getController() instanceof LoginController ||
window.getController() instanceof RegisterController)
mainWrapper.setTop(null);
else {
mainWrapper.setTop(navBarLag.getUi());
if (Arrays.asList("UserMenu", "AdminMenu").contains(window.getTitle())) {
if (window.getController() instanceof AdminMenuController || window.getController() instanceof UserMenuController) {
((NavBarController) navBarLag.getController()).getUserBar().setVisible(false);
((NavBarController) navBarLag.getController()).getUserBar().setManaged(false);
} else {
Expand Down Expand Up @@ -332,8 +306,6 @@ public Window getWindow(String title) {
yield welcomeLag;
case "UserMenu":
yield userMenuLag;
case "CreateForecast":
yield createForecastLag;
case "AdminMenu":
yield adminMenuLag;
default: // get the welcome window
Expand Down
Loading

0 comments on commit 5212efe

Please sign in to comment.