From 24be00d28ab7dc9d9e38ce5757c90e83c8f9d1f8 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sat, 6 Apr 2019 21:29:49 +0200 Subject: [PATCH] Update methods for extras --- .../controller/ExtraActivityController.java | 50 ++++++++++++++++++- .../greenify/client/rest/UserService.java | 20 ++++---- .../main/resources/fxml/FirstCalculator.fxml | 15 +----- .../main/resources/fxml/RegisterWindow.fxml | 3 +- .../src/main/resources/fxml/dashboard.fxml | 24 ++++----- .../main/resources/fxml/extraActivities.fxml | 28 ++++++----- src/Client/src/test/java/UserServiceTest.java | 38 +++++++------- 7 files changed, 105 insertions(+), 73 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/controller/ExtraActivityController.java b/src/Client/src/main/java/greenify/client/controller/ExtraActivityController.java index 63d1d03..e14e30f 100644 --- a/src/Client/src/main/java/greenify/client/controller/ExtraActivityController.java +++ b/src/Client/src/main/java/greenify/client/controller/ExtraActivityController.java @@ -8,20 +8,28 @@ import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; +import javafx.stage.Window; import javafx.util.Duration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class ExtraActivityController { - @Autowired UserService userService; + @Autowired + CalculatorController calculatorController; + + @Autowired + DashBoardController controller; + @FXML private AnchorPane veganMealPane; @FXML @@ -59,6 +67,8 @@ public class ExtraActivityController { private Slider solarPanelsSlider; @FXML private Label solarPanelsLabel; + @FXML + private Button saveButton; /** * initializes the sliders and labels before loading. @@ -78,7 +88,6 @@ public class ExtraActivityController { displayBikeButton.setSkin(new TranslateButtonSkin(displayBikeButton)); displayTemperatureButton.setSkin(new TranslateButtonSkin(displayTemperatureButton)); displaySolarPanelButton.setSkin(new TranslateButtonSkin(displaySolarPanelButton)); - } /** @@ -150,6 +159,43 @@ public class ExtraActivityController { solarPanelPane.setVisible(true); } + /** + * The method updates the values + */ + @SuppressWarnings("Duplicates") + public void save(ActionEvent event) throws InterruptedException { + Window owner = saveButton.getScene().getWindow(); + Float footprint = userService.saveFootprint(userService.currentUser.getName()); + controller.updateLeaderboard(); + Stage current = (Stage) owner; + current.close(); + UserController.AlertHelper.showAlert(Alert.AlertType.CONFIRMATION, owner, "Activities are added!", + "Your new activities are added!"); + } + + /** + * The method updates the values of extras. + * @param event user clicks to button + */ + @SuppressWarnings("Duplicates") + public void updateExtras(ActionEvent event) throws InterruptedException { + if (!bikeLabel.getText().replace(" km", "").equals("0")) { + userService.updateExtraInput(userService.currentUser.getName(), + "bike", + bikeLabel.getText().replace(" km", "")); + } + if (!solarPanelsLabel.getText().equals("0")) { + userService.updateExtraInput(userService.currentUser.getName(), + "solar_panels", + solarPanelsLabel.getText()); + } + if (!temperatureLabel.getText().replace(" Degrees", "").equals("0")) { + userService.updateExtraInput(userService.currentUser.getName(), + "bike", + temperatureLabel.getText().replace(" Degrees", "")); + } + } + public class TranslateButtonSkin extends ButtonSkin { /** * button skin that sets a translate animation on entering and exiting the button. 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 bb19bf5..699137e 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -109,13 +109,13 @@ public class UserService { * @param value value of the input */ @SuppressWarnings("Duplicates") - public void updateExtraInput(String name, String inputName, Boolean value) { + public void updateExtraInput(String name, String inputName, String 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); + .queryParam("value", value); new HttpEntity<>(headers); System.out.println(builder.build().encode().toUri()); ResponseEntity authenticateResponse = this.restTemplate.getForEntity(builder.build() @@ -128,15 +128,16 @@ public class UserService { * @return returns the footprint score */ @SuppressWarnings("Duplicates") - public Float getFootprint(String name) { + public double getFootprint(String name) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFootprint") .queryParam("name", name); new HttpEntity<>(headers); System.out.println(builder.build().encode().toUri()); - Float result = this.restTemplate.getForObject(builder + Float footprint = this.restTemplate.getForObject(builder .build().encode().toUri(), Float.class); + double result = Math.round(footprint * 10) / 10.0; return result; } @@ -145,7 +146,7 @@ public class UserService { * @param name name of the user * @return returns the footprint score */ - public Float getFirstFootprint(String name) { + public double getFirstFootprint(String name) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFirst") @@ -154,7 +155,8 @@ public class UserService { System.out.println(builder.build().encode().toUri()); Float footprint = this.restTemplate.getForObject(builder .build().encode().toUri(), Float.class); - return footprint; + double result = Math.round(footprint * 10) / 10.0; + return result; } /** @@ -270,14 +272,14 @@ public class UserService { * @param name the username of the current user. */ @SuppressWarnings("Duplicates") - public Map getExtraInputs(String name) { + 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() + Map result = this.restTemplate.getForObject(builder.build() .encode().toUri(), Map.class); return result; } @@ -313,4 +315,4 @@ public class UserService { .build().encode().toUri(), List.class); return result; } -} +} \ No newline at end of file diff --git a/src/Client/src/main/resources/fxml/FirstCalculator.fxml b/src/Client/src/main/resources/fxml/FirstCalculator.fxml index 3304516..2fea111 100644 --- a/src/Client/src/main/resources/fxml/FirstCalculator.fxml +++ b/src/Client/src/main/resources/fxml/FirstCalculator.fxml @@ -1,26 +1,13 @@ - - - - - - - - - - - - - - + diff --git a/src/Client/src/main/resources/fxml/RegisterWindow.fxml b/src/Client/src/main/resources/fxml/RegisterWindow.fxml index 6c82691..5764438 100644 --- a/src/Client/src/main/resources/fxml/RegisterWindow.fxml +++ b/src/Client/src/main/resources/fxml/RegisterWindow.fxml @@ -1,6 +1,5 @@ - @@ -25,7 +24,7 @@ - - +