From e78be7ac9fd762d8c9ca7a312d3084e3851b5d89 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Mon, 1 Apr 2019 13:59:54 +0200 Subject: [PATCH] Add co2 saved property tables and tests --- .../controller/CalculatorController.java | 22 +- .../controller/DashBoardController.java | 140 +++- .../client/controller/FriendController.java | 8 - .../controller/RegisterWindowController.java | 486 +++++++++++++- .../greenify/client/rest/UserService.java | 73 ++ .../main/resources/fxml/FirstCalculator.fxml | 632 ++++++++++++++++++ .../src/main/resources/fxml/dashboard.fxml | 75 ++- src/Client/src/test/java/UserServiceTest.java | 37 + .../java/greenify/server/InputValidator.java | 13 + .../java/greenify/server/data/model/User.java | 39 ++ .../greenify/server/rest/UserController.java | 43 ++ .../server/service/CalculatorService.java | 16 +- .../greenify/server/service/UserService.java | 48 ++ .../server/rest/UserControllerTest.java | 80 +++ .../server/service/CalculatorServiceTest.java | 23 + .../server/service/UserServiceTest.java | 46 ++ 16 files changed, 1716 insertions(+), 65 deletions(-) create mode 100644 src/Client/src/main/resources/fxml/FirstCalculator.fxml diff --git a/src/Client/src/main/java/greenify/client/controller/CalculatorController.java b/src/Client/src/main/java/greenify/client/controller/CalculatorController.java index e860ec3..164df99 100644 --- a/src/Client/src/main/java/greenify/client/controller/CalculatorController.java +++ b/src/Client/src/main/java/greenify/client/controller/CalculatorController.java @@ -201,10 +201,6 @@ public class CalculatorController { addSliderListenerGoodsServices(goodsSlider, goodsLabel); addSliderListenerGoodsServices(servicesSlider, servicesLabel); - - - - } /** @@ -471,19 +467,27 @@ public class CalculatorController { "input_footprint_shopping_services_total", servicesLabel.getText().replace("€ / month", "")); } - Float footprint = userService.saveFootprint(userService.currentUser.getName()); if (localProduceCheckbox.isSelected()) { - controller.localProduce.setSelected(true); + localProduceCheckbox.setSelected(true); + userService.updateExtraInput(userService.currentUser.getName(), + "local_produce", true); } if (bikeCheckbox.isSelected()) { - controller.bike.setSelected(true); + bikeCheckbox.setSelected(true); + userService.updateExtraInput(userService.currentUser.getName(), + "bike", true); } if (temperatureCheckbox.isSelected()) { - controller.loweringTemp.setSelected(true); + temperatureCheckbox.setSelected(true); + userService.updateExtraInput(userService.currentUser.getName(), + "temperature", true); } if (solarPanelsCheckbox.isSelected()) { - controller.solarPanels.setSelected(true); + solarPanelsCheckbox.setSelected(true); + userService.updateExtraInput(userService.currentUser.getName(), + "solar_panels", true); } + Float footprint = userService.saveFootprint(userService.currentUser.getName()); Window owner = saveButton.getScene().getWindow(); Stage current = (Stage) owner; current.close(); diff --git a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java index 19d8623..259ed42 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -40,21 +40,7 @@ public class DashBoardController { public static ObservableList data = FXCollections.observableArrayList(); public ObservableList friendLeaderData = FXCollections.observableArrayList(); public ObservableList globalLeaderData = FXCollections.observableArrayList(); - - //these need to be public because they are used by the calculatorController - //suppressing the checkstyle warnings because the fields have to be public - @SuppressWarnings("CheckStyle") - @FXML - public CheckBox localProduce; - @SuppressWarnings("CheckStyle") - @FXML - public CheckBox loweringTemp; - @SuppressWarnings("CheckStyle") - @FXML - public CheckBox bike; - @SuppressWarnings("CheckStyle") - @FXML - public CheckBox solarPanels; + public ObservableList developmentData = FXCollections.observableArrayList(); @Autowired UserService userService; @@ -84,12 +70,14 @@ public class DashBoardController { @FXML private AnchorPane menuBar; @FXML - private Button addNewActivityButton; - @FXML private Button calculateFootPrintButton; @FXML private Label footprintLabel; @FXML + private Label firstFootprintLabel; + @FXML + private Label differenceLabel; + @FXML private Button addFriendButton; @FXML private TableView friendsTable; @@ -104,6 +92,12 @@ public class DashBoardController { @FXML private TableColumn globalScore; @FXML + private TableView developmentLeaderboard; + @FXML + private TableColumn developmentUser; + @FXML + private TableColumn developmentScore; + @FXML private TableView friendLeaderboard; @FXML private TableColumn friendUser; @@ -159,6 +153,17 @@ public class DashBoardController { private Label fruits; @FXML private Label snacks; + @FXML + private CheckBox localProduce; + @SuppressWarnings("CheckStyle") + @FXML + private CheckBox loweringTemp; + @SuppressWarnings("CheckStyle") + @FXML + private CheckBox bike; + @SuppressWarnings("CheckStyle") + @FXML + private CheckBox solarPanels; /** * Loads the the necessary things before anything else. @@ -177,6 +182,8 @@ public class DashBoardController { scoreColumn.setCellValueFactory(new PropertyValueFactory<>("Score")); globalUser.setCellValueFactory(new PropertyValueFactory<>("Friend")); globalScore.setCellValueFactory(new PropertyValueFactory<>("Score")); + developmentUser.setCellValueFactory(new PropertyValueFactory<>("Friend")); + developmentScore.setCellValueFactory(new PropertyValueFactory<>("Score")); friendUser.setCellValueFactory(new PropertyValueFactory<>("Friend")); friendScore.setCellValueFactory(new PropertyValueFactory<>("Score")); if (pieChart != null) { @@ -226,6 +233,31 @@ public class DashBoardController { } } + /** + * Sorts the scores of users. + * @param users the list of users + */ + public void sortDiffScores(List users) throws InterruptedException { + for (int i = 0; i < users.size(); i++) { + for (int j = 0; j < users.size(); j++) { + Float firstDiff = userService.getFirstFootprint(users.get(i)) - userService + .getFootprint(users.get(i)); + Float secondDiff = userService.getFirstFootprint(users.get(j)) - userService + .getFootprint(users.get(j)); + if (i < j && firstDiff < secondDiff) { + String temp = users.get(i); + users.set(i, users.get(j)); + users.set(j, temp); + } + if (i > j && firstDiff > secondDiff) { + String temp = users.get(i); + users.set(i, users.get(j)); + users.set(j, temp); + } + } + } + } + /** * Adds a fade transition for switching between the different panes. * @param node the node on which the transition needs to act @@ -291,7 +323,18 @@ public class DashBoardController { dairy.setText(inputMap.get("input_footprint_shopping_food_dairy")); fruits.setText(inputMap.get("input_footprint_shopping_food_fruitvegetables")); snacks.setText(inputMap.get("input_footprint_shopping_food_otherfood")); - localProduce.setSelected(true); + if (userService.getExtraInputs(userService.currentUser.getName()).get("local_produce")) { + localProduce.setSelected(true); + } + if (userService.getExtraInputs(userService.currentUser.getName()).get("bike")) { + bike.setSelected(true); + } + if (userService.getExtraInputs(userService.currentUser.getName()).get("temperature")) { + loweringTemp.setSelected(true); + } + if (userService.getExtraInputs(userService.currentUser.getName()).get("solar_panels")) { + solarPanels.setSelected(true); + } } /** @@ -302,6 +345,11 @@ public class DashBoardController { System.out.println(userService.currentUser.getName()); System.out.println(userService.getFootprint(userService.currentUser.getName())); footprintLabel.setText("" + userService.getFootprint(userService.currentUser.getName())); + firstFootprintLabel.setText("" + userService + .getFirstFootprint(userService.currentUser.getName())); + Float diff = userService.getFirstFootprint(userService.currentUser.getName()) - userService + .getFootprint(userService.currentUser.getName()); + differenceLabel.setText( "" + diff); usernameLabel.setText("" + userService.currentUser.getName()); addFadeTransition(userPane); System.out.println("display user"); @@ -316,13 +364,14 @@ public class DashBoardController { * Displays the friends pane. * @param event the event (clicking the button) */ - public void displayFriends(ActionEvent event) { + public void displayFriends(ActionEvent event) throws InterruptedException { addFadeTransition(friendsPane); System.out.println("display friends"); dashboardPane.setVisible(false); userPane.setVisible(false); activitiesPane.setVisible(false); friendsPane.setVisible(true); + updateFriends(); } //sets the slide in transition for startup @@ -363,32 +412,59 @@ public class DashBoardController { } /** - * Leaderboard is updaating. + * Leaderboard is updating. * @throws InterruptedException throws exception */ public void updateLeaderboard() throws InterruptedException { - friendLeaderboard.getItems().clear(); - globalLeaderboard.getItems().clear(); - //global leaderboard - globalLeaderData.removeAll(); List userList = userService.getAllUsers(); + //global leaderboard + globalLeaderboard.getItems().clear(); + globalLeaderData.removeAll(); sortScores(userList); + //development leaderboard + developmentLeaderboard.getItems().clear(); + developmentData.removeAll(); + sortDiffScores(userList); for (int j = 0; j < userList.size(); j++) { Friend user = new Friend(userList.get(j), userService.getFootprint(userList.get(j))); + Friend diffUser = new Friend(userList.get(j), userService + .getFirstFootprint(userList.get(j)) + - userService.getFootprint(userList.get(j))); globalLeaderData.add(user); + developmentData.add(diffUser); } globalLeaderboard.setItems(globalLeaderData); - // friend leaderboard - friendLeaderData.removeAll(); - String name = userService.currentUser.getName(); - List friendList = userService.getFriendNames(name); - friendList.add(name); + developmentLeaderboard.setItems(developmentData); + } + + /** + * Friends tables are updating. + * @throws InterruptedException throws exception + */ + public void updateFriends() throws InterruptedException { + List wholeList = userService.getFriendNames(userService.currentUser.getName()); + wholeList.add(userService.currentUser.getName()); + //friend list + friendsTable.getItems().clear(); + data.removeAll(); + List friendList = userService.getFriendNames(userService.currentUser.getName()); sortScores(friendList); + //friends leaderboard + friendLeaderboard.getItems().clear(); + friendLeaderData.removeAll(); + sortDiffScores(wholeList); for (int i = 0; i < friendList.size(); i++) { - Friend friend = new Friend(friendList.get(i), - userService.getFootprint(friendList.get(i))); - friendLeaderData.add(friend); + Friend user = new Friend(friendList.get(i), userService + .getFootprint(friendList.get(i))); + data.add(user); } + for (int j = 0; j < wholeList.size(); j++) { + Friend diffUser = new Friend(wholeList.get(j), + userService.getFirstFootprint(wholeList.get(j)) + - userService.getFootprint(wholeList.get(j))); + friendLeaderData.add(diffUser); + } + friendsTable.setItems(data); friendLeaderboard.setItems(friendLeaderData); } diff --git a/src/Client/src/main/java/greenify/client/controller/FriendController.java b/src/Client/src/main/java/greenify/client/controller/FriendController.java index 25992cb..a623de5 100644 --- a/src/Client/src/main/java/greenify/client/controller/FriendController.java +++ b/src/Client/src/main/java/greenify/client/controller/FriendController.java @@ -1,6 +1,5 @@ package greenify.client.controller; -import greenify.client.Friend; import greenify.client.rest.UserService; import javafx.event.ActionEvent; import javafx.fxml.FXML; @@ -17,9 +16,6 @@ public class FriendController { @Autowired UserService userService; - @Autowired - DashBoardController controller; - @FXML private Button addButton; @FXML @@ -55,12 +51,8 @@ public class FriendController { } //add friend to the current user userService.addFriend(userService.currentUser.getName(), userNameText.getText()); - Friend friend = new Friend(userNameText.getText(), - userService.getFootprint(userNameText.getText())); - DashBoardController.data.add(friend); //close the register window after the user has entered all the credentials Stage current = (Stage) owner; current.close(); - controller.updateLeaderboard(); } } diff --git a/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java index c3d3bb4..11cb71f 100644 --- a/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java +++ b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java @@ -1,20 +1,37 @@ package greenify.client.controller; +import greenify.client.Application; import greenify.client.rest.UserService; +import javafx.animation.Interpolator; +import javafx.animation.KeyFrame; +import javafx.animation.KeyValue; +import javafx.animation.Timeline; import javafx.animation.TranslateTransition; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.Node; +import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; +import javafx.scene.control.Label; import javafx.scene.control.PasswordField; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.Slider; import javafx.scene.control.TextField; +import javafx.scene.layout.AnchorPane; +import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.stage.Window; import javafx.util.Duration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import java.io.IOException; +import java.text.DecimalFormat; import java.util.concurrent.TimeUnit; /** @@ -26,6 +43,123 @@ public class RegisterWindowController { @Autowired UserService userService; + @Autowired + DashBoardController controller; + + //navigation panes + @FXML + private AnchorPane getStartedPane; + @FXML + private AnchorPane travelPane; + @FXML + private AnchorPane homePane; + @FXML + private ScrollPane foodPane; + @FXML + private AnchorPane shoppingPane; + + //'get started' pane + @FXML + private Slider peopleInHouseholdSlider; + @FXML + private Label peopleInHouseHoldLabel; + @FXML + private Slider annualIncomeSlider; + @FXML + private Label annualIncomeLabel; + @FXML + private Button saveButton; + + //travel pane + @FXML + private TextField publicTransitField; + @FXML + private TextField airplaneTravelField; + @FXML + private TextField carTravelGasolineField; + @FXML + private Slider carTravelGasolineSlider; + @FXML + private Label carTravelGasolineLabel; + @FXML + private TextField carTravelDieselField; + @FXML + private Slider carTravelDieselSlider; + @FXML + private Label carTravelDieselLabel; + @FXML + private TextField carTravelElectricField; + @FXML + private Slider carTravelElectricSlider; + @FXML + private Label carTravelElectricLabel; + + //home pane + @FXML + private TextField electricityField; + @FXML + private Slider cleanEnergyPurchasedSlider; + @FXML + private Label cleanEnergyPurchasedLabel; + @FXML + private TextField naturalGasField; + @FXML + private TextField heatingOilField; + @FXML + private TextField livingSpaceField; + @FXML + private Slider waterUsageSlider; + @FXML + private Label waterUsageLabel; + + //food pane + @FXML + private Slider meatFishEggsSlider; + @FXML + private Label meatFishEggsLabel; + @FXML + private Slider grainsBakedGoodsSlider; + @FXML + private Label grainsBakedGoodsLabel; + @FXML + private Slider dairySlider; + @FXML + private Label dairyLabel; + @FXML + private Slider fruitsVegetablesSlider; + @FXML + private Label fruitsVegetablesLabel; + @FXML + private Slider snacksDrinksSlider; + @FXML + private Label snacksDrinksLabel; + + //shopping pane + @FXML + private Text goodsField; + @FXML + private Text servicesField; + @FXML + private Slider goodsSlider; + @FXML + private Label goodsLabel; + @FXML + private Slider servicesSlider; + @FXML + private Label servicesLabel; + + //extra pane + @FXML + private AnchorPane extraPane; + @FXML + private CheckBox localProduceCheckbox; + @FXML + private CheckBox bikeCheckbox; + @FXML + private CheckBox temperatureCheckbox; + @FXML + private CheckBox solarPanelsCheckbox; + @FXML private TextField userNameText; @FXML @@ -42,14 +176,10 @@ public class RegisterWindowController { * @throws InterruptedException exception when interrupted */ public void initialize() throws InterruptedException { - // PathTransition pathTransUName = new PathTransition(Duration.millis(1100), - // uNamePathLine, userNameText); - // pathTransUName.play(); addSlideAnimation(1100, userNameText, -300); addSlideAnimation(1100, passwordField, 300); TimeUnit.MILLISECONDS.sleep(300); addSlideAnimation(1100, passwordField2, -420); - } /** @@ -70,7 +200,7 @@ public class RegisterWindowController { * @param event the click of the sign up button */ @FXML - public void handleSignUpButton(ActionEvent event) { + public void handleSignUpButton(ActionEvent event) throws IOException { //set the window to the current window (for displaying the alerts) Window owner = signUpButton.getScene().getWindow(); //check if the username field is empty @@ -99,9 +229,353 @@ public class RegisterWindowController { //register the user with the provided username and password userService.registerUser(userNameText.getText(), passwordField.getText()); - //close the register window after the user has entered all the credentials Stage current = (Stage) owner; current.close(); + + Parent calc = Application.load(this.getClass().getClassLoader() + .getResource("fxml/FirstCalculator.fxml")); + Scene scene = new Scene(calc); + scene.getStylesheets().add(getClass().getClassLoader() + .getResource("stylesheets/calculatorStyle.css").toExternalForm()); + Stage calcStage = new Stage(); + + calcStage.setScene(scene); + calcStage.setTitle("Calculate CO2 footprint - " + userService.currentUser.getName()); + calcStage.show(); + + peopleInHouseholdSlider.setSnapToTicks(true); + //add listener to slider for amount of people in household + peopleInHouseholdSlider.valueProperty().addListener(new ChangeListener() { + + public void changed(ObservableValue observable, + Number oldValue, Number newValue) { + peopleInHouseHoldLabel.setText("" + newValue.intValue()); + } + }); + + //add listener to slider for annual income + annualIncomeSlider.valueProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, + Number oldValue, Number newValue) { + annualIncomeLabel.setText("" + (newValue.intValue() * 1000)); + } + }); + + addSliderListenerCarUsage(carTravelGasolineSlider, carTravelGasolineLabel, " mpg"); + addSliderListenerCarUsage(carTravelDieselSlider, carTravelDieselLabel, " mpg"); + addSliderListenerCarUsage(carTravelElectricSlider, carTravelElectricLabel, " mpge"); + + cleanEnergyPurchasedSlider.valueProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, + Number oldValue, Number newValue) { + cleanEnergyPurchasedLabel.setText(newValue.intValue() + " %"); + } + }); + + waterUsageSlider.valueProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, + Number oldValue, Number newValue) { + waterUsageLabel.setText(newValue.intValue() + "% of similar households"); + } + }); + + addSliderListenerDailyServing(meatFishEggsSlider, meatFishEggsLabel); + addSliderListenerDailyServing(grainsBakedGoodsSlider, grainsBakedGoodsLabel); + addSliderListenerDailyServing(dairySlider, dairyLabel); + addSliderListenerDailyServing(fruitsVegetablesSlider, fruitsVegetablesLabel); + addSliderListenerDailyServing(snacksDrinksSlider, snacksDrinksLabel); + + addSliderListenerGoodsServices(goodsSlider, goodsLabel); + addSliderListenerGoodsServices(servicesSlider, servicesLabel); + } + + /** + * adds the listener to the given slider and displays it's output on a given label. + * @param slider the slider to attach the listener to. + * @param label the label to display the slider output on. + */ + private void addSliderListenerCarUsage(Slider slider, Label label, String unit) { + slider.valueProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, + Number oldValue, Number newValue) { + label.setText(newValue.intValue() + unit); + } + }); + } + + private void addSliderListenerDailyServing(Slider slider, Label label) { + DecimalFormat df = new DecimalFormat("0.0"); + slider.valueProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, + Number oldValue, Number newValue) { + label.setText(df.format(newValue.doubleValue()) + " daily servings per person"); + } + }); + } + + private void addSliderListenerGoodsServices(Slider slider, Label label) { + slider.valueProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, + Number oldValue, Number newValue) { + label.setText(newValue.intValue() + "€ / month"); + } + }); + } + + private void addSlideInAnimation(Node node) { + Timeline timeline = new Timeline(); + KeyValue keyValue = new KeyValue(node.translateXProperty(), 0, Interpolator.EASE_OUT); + KeyFrame keyFrame = new KeyFrame(Duration.millis(600), keyValue); + timeline.getKeyFrames().add(keyFrame); + timeline.play(); + } + + /** + * displays the 'get started' section of the calculator. + * Activated when the designated button (navigation button) is clicked + * @param event the click of the button + */ + @SuppressWarnings("Duplicates") + public void displayGetStarted(ActionEvent event) { + getStartedPane.setVisible(true); + travelPane.setVisible(false); + homePane.setVisible(false); + foodPane.setVisible(false); + shoppingPane.setVisible(false); + extraPane.setVisible(false); + } + + /** + * displays the 'travel' section of the calculator. + * Activated when the designated button (navigation button) is clicked + * @param event the click of the button + */ + @SuppressWarnings("Duplicates") + public void displayTravel(ActionEvent event) { + addSlideInAnimation(travelPane); + getStartedPane.setVisible(false); + travelPane.setVisible(true); + homePane.setVisible(false); + foodPane.setVisible(false); + shoppingPane.setVisible(false); + extraPane.setVisible(false); + + } + + /** + * displays the 'home' section of the calculator. + * Activated when the designated button (navigation button) is clicked + * @param event the click of the button + */ + @SuppressWarnings("Duplicates") + public void displayHome(ActionEvent event) { + getStartedPane.setVisible(false); + travelPane.setVisible(false); + homePane.setVisible(true); + foodPane.setVisible(false); + shoppingPane.setVisible(false); + extraPane.setVisible(false); + } + + /** + * displays the 'food' section of the calculator. + * Activated when the designated button (navigation button) is clicked + * @param event the click of the button + */ + @SuppressWarnings("Duplicates") + public void displayFood(ActionEvent event) { + getStartedPane.setVisible(false); + travelPane.setVisible(false); + homePane.setVisible(false); + foodPane.setVisible(true); + shoppingPane.setVisible(false); + extraPane.setVisible(false); + } + + /** + * displays the 'shopping' section of the calculator. + * Activated when the designated button (navigation button) is clicked + * @param event the click of the button + */ + @SuppressWarnings("Duplicates") + public void displayShopping(ActionEvent event) { + getStartedPane.setVisible(false); + travelPane.setVisible(false); + homePane.setVisible(false); + foodPane.setVisible(false); + shoppingPane.setVisible(true); + extraPane.setVisible(false); + } + + /** + * displays the 'Extra' section of the calculator. + * Activated when the designated button (navigation button) is clicked + * @param event the click of the designated button + */ + @SuppressWarnings("Duplicates") + public void displayExtra(ActionEvent event) { + getStartedPane.setVisible(false); + travelPane.setVisible(false); + homePane.setVisible(false); + foodPane.setVisible(false); + shoppingPane.setVisible(false); + extraPane.setVisible(true); + + } + + /** + * The method saves the calculation. + * @param event user clicks to button + */ + @SuppressWarnings("Duplicates") + public void saveCalc(ActionEvent event) throws InterruptedException { + getStartedPane.setVisible(false); + travelPane.setVisible(false); + homePane.setVisible(false); + foodPane.setVisible(false); + shoppingPane.setVisible(false); + if (!annualIncomeLabel.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), "input_income", + annualIncomeLabel.getText()); + } + if (!peopleInHouseHoldLabel.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), "input_size", + peopleInHouseHoldLabel.getText()); + } + if (!publicTransitField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_transportation_publictrans", + publicTransitField.getText()); + } + if (!airplaneTravelField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_transportation_airtotal", + airplaneTravelField.getText()); + } + if (!carTravelGasolineField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_transportation_miles1", + carTravelGasolineField.getText()); + } + if (!carTravelGasolineLabel.getText().replace(" mpg", "").equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_transportation_mpg1", + carTravelGasolineLabel.getText().replace(" mpg", "")); + } + if (!carTravelDieselField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_transportation_miles2", + carTravelDieselField.getText()); + } + if (!carTravelDieselLabel.getText().replace(" mpg", "").equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_transportation_mpg2", + carTravelDieselLabel.getText().replace(" mpg", "")); + } + if (!carTravelElectricField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_transportation_miles3", + carTravelElectricField.getText()); + } + if (!carTravelElectricLabel.getText().replace(" mpge", "").equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_transportation_mpg3", + carTravelElectricLabel.getText().replace(" mpge", "")); + } + if (!electricityField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_housing_electricity_dollars", + electricityField.getText()); + } + if (!cleanEnergyPurchasedLabel.getText().replace(" %", "").equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_housing_gco2_per_kwh", + cleanEnergyPurchasedLabel.getText().replace(" %", "")); + } + if (!naturalGasField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_housing_naturalgas_dollars", + naturalGasField.getText()); + } + if (!heatingOilField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_housing_heatingoil_dollars", + heatingOilField.getText()); + } + if (!livingSpaceField.getText().equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_housing_squarefeet", + livingSpaceField.getText()); + } + if (!waterUsageLabel.getText().replace("% of similar households", "").equals("0")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_housing_watersewage", + waterUsageLabel.getText().replace("% of similar households", "")); + } + if (!meatFishEggsLabel.getText().replace(" daily servings per person", "").equals("2.6")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_shopping_food_meatfisheggs", + meatFishEggsLabel.getText().replace(" daily servings per person", "")); + } + if (!grainsBakedGoodsLabel.getText() + .replace(" daily servings per person", "").equals("4.4")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_shopping_food_cereals", + grainsBakedGoodsLabel.getText().replace(" daily servings per person", "")); + } + if (!dairyLabel.getText().replace(" daily servings per person", "").equals("2.4")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_shopping_food_dairy", + dairyLabel.getText().replace(" daily servings per person", "")); + } + if (!fruitsVegetablesLabel.getText() + .replace(" daily servings per person", "").equals("3.9")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_shopping_food_fruitvegetables", + fruitsVegetablesLabel.getText().replace(" daily servings per person", "")); + } + if (!snacksDrinksLabel.getText().replace(" daily servings per person", "").equals("3.7")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_shopping_food_otherfood", + snacksDrinksLabel.getText().replace(" daily servings per person", "")); + } + if (!goodsLabel.getText().replace(" € / month", "").equals("1520")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_shopping_goods_total", + goodsLabel.getText().replace("€ / month", "")); + } + if (!servicesLabel.getText().replace(" € / month", "").equals("3428")) { + userService.updateInput(userService.currentUser.getName(), + "input_footprint_shopping_services_total", + servicesLabel.getText().replace("€ / month", "")); + } + if (localProduceCheckbox.isSelected()) { + userService.updateExtraInput(userService.currentUser.getName(), + "local_produce", true); + } + if (bikeCheckbox.isSelected()) { + userService.updateExtraInput(userService.currentUser.getName(), + "bike", true); + } + if (temperatureCheckbox.isSelected()) { + userService.updateExtraInput(userService.currentUser.getName(), + "temperature", true); + } + if (solarPanelsCheckbox.isSelected()) { + userService.updateExtraInput(userService.currentUser.getName(), + "solar_panels", true); + } + Float firstFootprint = userService.saveFirstFootprint(userService.currentUser.getName()); + Float footprint = userService.saveFootprint(userService.currentUser.getName()); + Window owner = saveButton.getScene().getWindow(); + Stage current = (Stage) owner; + current.close(); } } diff --git a/src/Client/src/main/java/greenify/client/rest/UserService.java b/src/Client/src/main/java/greenify/client/rest/UserService.java index c512dda..9252cc8 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -102,6 +102,26 @@ public class UserService { .encode().toUri(), String.class); } + /** + * Updates the extra input of the user. + * @param name name of the user + * @param inputName name of the input + * @param value value of the input + */ + @SuppressWarnings("Duplicates") + public void updateExtraInput(String name, String inputName, Boolean value) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/setExtraInput") + .queryParam("name", name) + .queryParam("inputName", inputName) + .queryParam("value",value); + new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + ResponseEntity authenticateResponse = this.restTemplate.getForEntity(builder.build() + .encode().toUri(), String.class); + } + /** * Gets the footprint of the user. * @param name name of the user @@ -120,6 +140,23 @@ public class UserService { return result; } + /** + * Gets the first footprint of the user. + * @param name name of the user + * @return returns the footprint score + */ + public Float getFirstFootprint(String name) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFirst") + .queryParam("name", name); + new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + Float footprint = this.restTemplate.getForObject(builder + .build().encode().toUri(), Float.class); + return footprint; + } + /** * Saves the footprint of the user. * @param name name of the user @@ -138,6 +175,25 @@ public class UserService { return result; } + + /** + * Saves the first footprint of the user. + * @param name name of the user + * @return returns the footprint score + */ + @SuppressWarnings("Duplicates") + public Float saveFirstFootprint(String name) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/saveFirstFootprint") + .queryParam("name", name); + new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + Float result = this.restTemplate.getForObject(builder + .build().encode().toUri(), Float.class); + return result; + } + /** * Gets the friend list of the user. * @param name name of the user @@ -191,6 +247,23 @@ public class UserService { return result; } + /** + * Gets the footprint inputs of the user. + * @param name the username of the current user. + */ + @SuppressWarnings("Duplicates") + public Map getExtraInputs(String name) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getExtraInputs") + .queryParam("name", name); + HttpEntity entity = new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + Map result = this.restTemplate.getForObject(builder.build() + .encode().toUri(), Map.class); + return result; + } + /** * Gets the list of all users. */ diff --git a/src/Client/src/main/resources/fxml/FirstCalculator.fxml b/src/Client/src/main/resources/fxml/FirstCalculator.fxml new file mode 100644 index 0000000..71554f6 --- /dev/null +++ b/src/Client/src/main/resources/fxml/FirstCalculator.fxml @@ -0,0 +1,632 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +