Fix all errors for extra activities

This commit is contained in:
cugurlu
2019-04-08 11:17:34 +02:00
parent 9310674301
commit 8195650226
6 changed files with 92 additions and 64 deletions

View File

@@ -246,7 +246,6 @@ public class DashBoardController {
addFriendButton.setSkin(new ClickButtonSkin(addFriendButton)); addFriendButton.setSkin(new ClickButtonSkin(addFriendButton));
addExtraActivityButton.setSkin(new ClickButtonSkin(addExtraActivityButton)); addExtraActivityButton.setSkin(new ClickButtonSkin(addExtraActivityButton));
addExtraActivityButton2.setSkin(new ClickButtonSkin(addExtraActivityButton2)); addExtraActivityButton2.setSkin(new ClickButtonSkin(addExtraActivityButton2));
addRandomHints(); addRandomHints();
} }
@@ -254,30 +253,31 @@ public class DashBoardController {
* Sorts the scores of users. * Sorts the scores of users.
* @param users the list of users * @param users the list of users
*/ */
public void sortScores(List<String> users) throws InterruptedException { public List<String> sortScores(List<String> users) throws InterruptedException {
for (int i = 0; i < users.size(); i++) { for (int i = 0; i < users.size(); i++) {
for (int j = 0; j < users.size(); j++) { for (int j = 0; j < users.size(); j++) {
Double firstScore = userService.getFootprint(users.get(i)); Double first = userService.getFootprint(users.get(i));
Double secondScore = userService.getFootprint(users.get(j)); Double second = userService.getFootprint(users.get(j));
if (i > j && firstScore < secondScore) { if (i < j && first > second) {
String temp = users.get(i); String temp = users.get(i);
users.set(i, users.get(j)); users.set(i, users.get(j));
users.set(j, temp); users.set(j, temp);
} }
if (i < j && firstScore > secondScore) { if (i > j && first < second) {
String temp = users.get(i); String temp = users.get(i);
users.set(i, users.get(j)); users.set(i, users.get(j));
users.set(j, temp); users.set(j, temp);
} }
} }
} }
return users;
} }
/** /**
* Sorts the scores of users. * Sorts the scores of users.
* @param users the list of users * @param users the list of users
*/ */
public void sortDiffScores(List<String> users) throws InterruptedException { public List<String> sortDiffScores(List<String> users) throws InterruptedException {
for (int i = 0; i < users.size(); i++) { for (int i = 0; i < users.size(); i++) {
for (int j = 0; j < users.size(); j++) { for (int j = 0; j < users.size(); j++) {
Double firstDiff = userService.getFirstFootprint(users.get(i)) - userService Double firstDiff = userService.getFirstFootprint(users.get(i)) - userService
@@ -296,6 +296,7 @@ public class DashBoardController {
} }
} }
} }
return users;
} }
/** /**
@@ -364,7 +365,8 @@ public class DashBoardController {
dairy.setText(inputMap.get("input_footprint_shopping_food_dairy")); dairy.setText(inputMap.get("input_footprint_shopping_food_dairy"));
fruits.setText(inputMap.get("input_footprint_shopping_food_fruitvegetables")); fruits.setText(inputMap.get("input_footprint_shopping_food_fruitvegetables"));
snacks.setText(inputMap.get("input_footprint_shopping_food_otherfood")); snacks.setText(inputMap.get("input_footprint_shopping_food_otherfood"));
Map<String, String> extraMap = userService.getExtraInputs(userService.currentUser.getName()); Map<String, String> extraMap = userService
.getExtraInputs(userService.currentUser.getName());
localProduce.setText(extraMap.get("local_produce")); localProduce.setText(extraMap.get("local_produce"));
bike.setText(extraMap.get("bike")); bike.setText(extraMap.get("bike"));
solarPanels.setText(extraMap.get("solar_panels")); solarPanels.setText(extraMap.get("solar_panels"));
@@ -515,21 +517,23 @@ public class DashBoardController {
* @throws InterruptedException throws exception * @throws InterruptedException throws exception
*/ */
public void updateLeaderboard() throws InterruptedException { public void updateLeaderboard() throws InterruptedException {
List<String> userList = userService.getAllUsers();
//global leaderboard //global leaderboard
globalLeaderboard.getItems().clear(); globalLeaderboard.getItems().clear();
globalLeaderData.removeAll(); globalLeaderData.removeAll();
sortScores(userList); List<String> userList = userService.getAllUsers();
List<String> firstList = sortScores(userList);
//development leaderboard //development leaderboard
developmentLeaderboard.getItems().clear(); developmentLeaderboard.getItems().clear();
developmentData.removeAll(); developmentData.removeAll();
sortDiffScores(userList); List<String> secondList = sortDiffScores(userList);
for (int j = 0; j < userList.size(); j++) { for (int i = userList.size() - 1; i >= 0; i--) {
Friend user = new Friend(userList.get(j), userService.getFootprint(userList.get(j))); Friend user = new Friend(firstList.get(i), userService.getFootprint(firstList.get(i)));
double diff = Math.round((userService.getFirstFootprint(userList.get(j))
- userService.getFootprint(userList.get(j))) * 10) / 10.0;
Friend diffUser = new Friend(userList.get(j), diff);
globalLeaderData.add(user); globalLeaderData.add(user);
}
for (int j = 0; j < userList.size(); j++) {
double diff = Math.round((userService.getFirstFootprint(secondList.get(j))
- userService.getFootprint(secondList.get(j))) * 10) / 10.0;
Friend diffUser = new Friend(secondList.get(j), diff);
developmentData.add(diffUser); developmentData.add(diffUser);
} }
globalLeaderboard.setItems(globalLeaderData); globalLeaderboard.setItems(globalLeaderData);
@@ -572,32 +576,32 @@ public class DashBoardController {
*/ */
public void updateAchievements() { public void updateAchievements() {
Map achievements = userService.getAchievements(userService.currentUser.getName()); Map achievements = userService.getAchievements(userService.currentUser.getName());
if((Boolean)achievements.get("Starting off")) { if ((Boolean)achievements.get("Starting off")) {
achieve1.setOpacity(1); achieve1.setOpacity(1);
} else { } else {
achieve1.setOpacity(0.3); achieve1.setOpacity(0.3);
} }
if((Boolean)achievements.get("Social butterfly")) { if ((Boolean)achievements.get("Social butterfly")) {
achieve2.setOpacity(1); achieve2.setOpacity(1);
} else { } else {
achieve2.setOpacity(0.3); achieve2.setOpacity(0.3);
} }
if((Boolean)achievements.get("Green saver")) { if ((Boolean)achievements.get("Green saver")) {
achieve3.setOpacity(1); achieve3.setOpacity(1);
} else { } else {
achieve3.setOpacity(0.3); achieve3.setOpacity(0.3);
} }
if((Boolean)achievements.get("Animal friend")) { if ((Boolean)achievements.get("Animal friend")) {
achieve4.setOpacity(1); achieve4.setOpacity(1);
} else { } else {
achieve4.setOpacity(0.3); achieve4.setOpacity(0.3);
} }
if((Boolean)achievements.get("Tom Dumoulin")) { if ((Boolean)achievements.get("Tom Dumoulin")) {
achieve5.setOpacity(1); achieve5.setOpacity(1);
} else { } else {
achieve5.setOpacity(0.3); achieve5.setOpacity(0.3);
} }
if((Boolean)achievements.get("Let it shine")) { if ((Boolean)achievements.get("Let it shine")) {
achieve6.setOpacity(1); achieve6.setOpacity(1);
} else { } else {
achieve6.setOpacity(0.3); achieve6.setOpacity(0.3);

View File

@@ -8,13 +8,10 @@ import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.Slider; import javafx.scene.control.Slider;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.util.Duration; import javafx.util.Duration;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@@ -83,8 +80,6 @@ public class ExtraActivityController {
private Label publicTransportLabel; private Label publicTransportLabel;
@FXML @FXML
private Slider publicTransportSlider; private Slider publicTransportSlider;
@FXML
private Button saveButton;
/** /**
@@ -216,29 +211,26 @@ public class ExtraActivityController {
publicTransportPane.setVisible(true); publicTransportPane.setVisible(true);
} }
/** /**
* The method updates the values. * The method updates the values.
*/ */
@SuppressWarnings("Duplicates") @SuppressWarnings("Duplicates")
public void save(ActionEvent event) throws InterruptedException { public void save(ActionEvent event) {
Window owner = saveButton.getScene().getWindow(); try {
Float footprint = userService.saveFootprint(userService.currentUser.getName()); updateExtras();
controller.updateLeaderboard(); Float footprint = userService.saveFootprint(userService.currentUser.getName());
controller.updateAchievements(); controller.updateLeaderboard();
Stage current = (Stage) owner; controller.updateAchievements();
current.close(); } catch (Exception ex) {
UserController.AlertHelper.showAlert(Alert.AlertType.CONFIRMATION, owner, "Activities are added!", System.out.println("continue");
"Your new activities are added!"); }
} }
/** /**
* The method updates the values of extras. * The method updates the values of extras.
* @param event user clicks to button
*/ */
@SuppressWarnings("Duplicates") @SuppressWarnings("Duplicates")
public void updateExtras(ActionEvent event) throws InterruptedException { public void updateExtras() throws InterruptedException {
if (!bikeLabel.getText().replace(" km", "").equals("0")) { if (!bikeLabel.getText().replace(" km", "").equals("0")) {
userService.updateExtraInput(userService.currentUser.getName(), userService.updateExtraInput(userService.currentUser.getName(),
"bike", "bike",
@@ -251,11 +243,44 @@ public class ExtraActivityController {
} }
if (!temperatureLabel.getText().replace(" Degrees", "").equals("0")) { if (!temperatureLabel.getText().replace(" Degrees", "").equals("0")) {
userService.updateExtraInput(userService.currentUser.getName(), userService.updateExtraInput(userService.currentUser.getName(),
"bike", "temperature",
temperatureLabel.getText().replace(" Degrees", "")); temperatureLabel.getText().replace(" Degrees", ""));
} }
controller.updateAchievements(); if (!publicTransportLabel.getText().replace(" km", "").equals("0")) {
controller.updateLeaderboard(); userService.updateExtraInput(userService.currentUser.getName(),
"public_transport",
publicTransportLabel.getText().replace(" km", ""));
}
}
/**
* The method updates the values of extras.
*/
@SuppressWarnings("Duplicates")
public void updateExtraVegan() throws InterruptedException {
try {
userService.updateExtraInput(userService.currentUser.getName(), "vegan", "1");
Float footprint = userService.saveFootprint(userService.currentUser.getName());
controller.updateAchievements();
controller.updateLeaderboard();
} catch (Exception ex) {
System.out.println("continue");
}
}
/**
* The method updates the values of extras.
*/
@SuppressWarnings("Duplicates")
public void updateExtraLocal() throws InterruptedException {
try {
userService.updateExtraInput(userService.currentUser.getName(), "local_produce", "1");
Float footprint = userService.saveFootprint(userService.currentUser.getName());
controller.updateAchievements();
controller.updateLeaderboard();
} catch (Exception ex) {
System.out.println("continue");
}
} }
public class TranslateButtonSkin extends ButtonSkin { public class TranslateButtonSkin extends ButtonSkin {

View File

@@ -221,7 +221,7 @@ public class RegisterWindowController {
//register the user with the provided username and password //register the user with the provided username and password
try { try {
userService.registerUser(userNameText.getText(), passwordField.getText()); userService.registerUser(userNameText.getText(), passwordField.getText());
} catch (RuntimeException ex) { } catch (Exception ex) {
UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Username Error!", UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Username Error!",
"This username has already been taken!"); "This username has already been taken!");
return; return;
@@ -484,6 +484,11 @@ public class RegisterWindowController {
"input_footprint_shopping_services_total", "input_footprint_shopping_services_total",
servicesLabel.getText().replace("€ / month", "")); servicesLabel.getText().replace("€ / month", ""));
} }
try {
extraActivityController.updateExtras();
} catch (Exception ex) {
System.out.println("Continue");
}
Float firstFootprint = userService.saveFirstFootprint(userService.currentUser.getName()); Float firstFootprint = userService.saveFirstFootprint(userService.currentUser.getName());
Float footprint = userService.saveFootprint(userService.currentUser.getName()); Float footprint = userService.saveFootprint(userService.currentUser.getName());
Window owner = saveButton.getScene().getWindow(); Window owner = saveButton.getScene().getWindow();

View File

@@ -1,21 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import javafx.scene.image.*?> <?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?> <?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?> <?import javafx.scene.text.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.shape.Line?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="611.0" prefWidth="820.0" stylesheets="@../stylesheets/extraActivitiesStyle.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greenify.client.controller.ExtraActivityController"> <AnchorPane prefHeight="611.0" prefWidth="820.0" stylesheets="@../stylesheets/extraActivitiesStyle.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greenify.client.controller.ExtraActivityController">
<children> <children>
@@ -84,7 +73,7 @@
<Font size="20.0" /> <Font size="20.0" />
</font> </font>
</Text> </Text>
<Button fx:id="addVeganMealButton" contentDisplay="TOP" layoutX="267.0" layoutY="226.0" mnemonicParsing="false" onAction="#updateExtras" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b"> <Button fx:id="addVeganMealButton" contentDisplay="TOP" layoutX="267.0" layoutY="226.0" mnemonicParsing="false" onAction="#updateExtraVegan" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b">
<graphic> <graphic>
<ImageView fitHeight="116.0" fitWidth="156.0" pickOnBounds="true" preserveRatio="true"> <ImageView fitHeight="116.0" fitWidth="156.0" pickOnBounds="true" preserveRatio="true">
<image> <image>
@@ -112,7 +101,7 @@
<Font size="18.0" /> <Font size="18.0" />
</font> </font>
</Text> </Text>
<Button fx:id="addBikeButton" contentDisplay="TOP" layoutX="267.0" layoutY="351.0" mnemonicParsing="false" onAction="#updateExtras" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b"> <Button fx:id="addBikeButton" contentDisplay="TOP" layoutX="267.0" layoutY="351.0" mnemonicParsing="false" onAction="#save" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b">
<font> <font>
<Font name="System Bold Italic" size="18.0" /> <Font name="System Bold Italic" size="18.0" />
</font> </font>
@@ -158,7 +147,7 @@
</Label> </Label>
</children> </children>
</HBox> </HBox>
<Button fx:id="addTemperatureButton" contentDisplay="TOP" layoutX="267.0" layoutY="353.0" mnemonicParsing="false" onAction="#updateExtras" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b"> <Button fx:id="addTemperatureButton" contentDisplay="TOP" layoutX="267.0" layoutY="353.0" mnemonicParsing="false" onAction="#save" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b">
<font> <font>
<Font name="System Bold Italic" size="18.0" /> <Font name="System Bold Italic" size="18.0" />
</font> </font>
@@ -195,7 +184,7 @@
</children> </children>
</HBox> </HBox>
<Slider fx:id="solarPanelsSlider" blockIncrement="1.0" layoutX="187.0" layoutY="274.0" majorTickUnit="1.0" max="10.0" min="1.0" minorTickCount="0" prefHeight="24.0" prefWidth="338.0" showTickLabels="true" showTickMarks="true" /> <Slider fx:id="solarPanelsSlider" blockIncrement="1.0" layoutX="187.0" layoutY="274.0" majorTickUnit="1.0" max="10.0" min="1.0" minorTickCount="0" prefHeight="24.0" prefWidth="338.0" showTickLabels="true" showTickMarks="true" />
<Button fx:id="addSolarPanelsButton" contentDisplay="TOP" layoutX="267.0" layoutY="378.0" mnemonicParsing="false" onAction="#updateExtras" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b"> <Button fx:id="addSolarPanelsButton" contentDisplay="TOP" layoutX="267.0" layoutY="378.0" mnemonicParsing="false" onAction="#save" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b">
<font> <font>
<Font name="System Bold Italic" size="18.0" /> <Font name="System Bold Italic" size="18.0" />
</font> </font>
@@ -217,7 +206,7 @@
<Font size="20.0" /> <Font size="20.0" />
</font> </font>
</Text> </Text>
<Button fx:id="addLocalProduceButton" contentDisplay="TOP" layoutX="267.0" layoutY="226.0" mnemonicParsing="false" onAction="#updateExtras" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b"> <Button fx:id="addLocalProduceButton" contentDisplay="TOP" layoutX="267.0" layoutY="226.0" mnemonicParsing="false" onAction="#updateExtraLocal" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b">
<font> <font>
<Font name="System Bold Italic" size="18.0" /> <Font name="System Bold Italic" size="18.0" />
</font> </font>
@@ -245,7 +234,7 @@
<Font size="18.0" /> <Font size="18.0" />
</font> </font>
</Text> </Text>
<Button fx:id="addPublicTransportButton" contentDisplay="TOP" layoutX="267.0" layoutY="351.0" mnemonicParsing="false" onAction="#updateExtras" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b"> <Button fx:id="addPublicTransportButton" contentDisplay="TOP" layoutX="267.0" layoutY="351.0" mnemonicParsing="false" onAction="#save" prefHeight="150.0" prefWidth="180.0" style="-fx-background-color: transparent;" text="Add activity!" textFill="#23652b">
<font> <font>
<Font name="System Bold Italic" size="18.0" /> <Font name="System Bold Italic" size="18.0" />
</font> </font>

View File

@@ -190,7 +190,7 @@ public class InputValidator {
map.put("vegan", "0"); map.put("vegan", "0");
map.put("local_produce", "0"); map.put("local_produce", "0");
map.put("bike", "0"); map.put("bike", "0");
map.put("car", "0"); map.put("public_transport", "0");
map.put("temperature", "0"); map.put("temperature", "0");
map.put("solar_panels", "0"); map.put("solar_panels", "0");
return map; return map;

View File

@@ -91,16 +91,21 @@ public class CalculatorService {
Map<String, String> inputs = user.getFootPrintInputs(); Map<String, String> inputs = user.getFootPrintInputs();
Float netPublic = Float.parseFloat(user.getFootPrintInputs() Float netPublic = Float.parseFloat(user.getFootPrintInputs()
.get("input_footprint_transportation_publictrans")) .get("input_footprint_transportation_publictrans"))
- Float.parseFloat(user.getExtraInputs().get("bike")); + Float.parseFloat(user.getExtraInputs().get("public_transport"));
Float netCar = Float.parseFloat(user.getFootPrintInputs() Float netCar = Float.parseFloat(user.getFootPrintInputs()
.get("input_footprint_transportation_miles1")) .get("input_footprint_transportation_miles1"))
- Float.parseFloat(user.getExtraInputs().get("car")); - Float.parseFloat(user.getExtraInputs().get("public_transport"))
- Float.parseFloat(user.getExtraInputs().get("bike"));
Float netVegan = Float.parseFloat(user.getFootPrintInputs() Float netVegan = Float.parseFloat(user.getFootPrintInputs()
.get("input_footprint_shopping_food_fruitvegetables")) .get("input_footprint_shopping_food_fruitvegetables"))
+ Float.parseFloat(user.getExtraInputs().get("vegan")); + Float.parseFloat(user.getExtraInputs().get("vegan"));
Float netShopping = Float.parseFloat(user.getFootPrintInputs()
.get("input_footprint_shopping_goods_total"))
- Float.parseFloat(user.getExtraInputs().get("local_produce")) * 100;
inputs.put("input_footprint_transportation_publictrans", netPublic + ""); inputs.put("input_footprint_transportation_publictrans", netPublic + "");
inputs.put("input_footprint_transportation_miles1", netCar + ""); inputs.put("input_footprint_transportation_miles1", netCar + "");
inputs.put("input_footprint_shopping_food_fruitvegetables", netVegan + ""); inputs.put("input_footprint_shopping_food_fruitvegetables", netVegan + "");
inputs.put("input_footprint_shopping_goods_total", netShopping + "");
user.setFootPrintInputs(inputs); user.setFootPrintInputs(inputs);
} }
} }