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));
addExtraActivityButton.setSkin(new ClickButtonSkin(addExtraActivityButton));
addExtraActivityButton2.setSkin(new ClickButtonSkin(addExtraActivityButton2));
addRandomHints();
}
@@ -254,30 +253,31 @@ public class DashBoardController {
* Sorts the scores 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 j = 0; j < users.size(); j++) {
Double firstScore = userService.getFootprint(users.get(i));
Double secondScore = userService.getFootprint(users.get(j));
if (i > j && firstScore < secondScore) {
Double first = userService.getFootprint(users.get(i));
Double second = userService.getFootprint(users.get(j));
if (i < j && first > second) {
String temp = users.get(i);
users.set(i, users.get(j));
users.set(j, temp);
}
if (i < j && firstScore > secondScore) {
if (i > j && first < second) {
String temp = users.get(i);
users.set(i, users.get(j));
users.set(j, temp);
}
}
}
return users;
}
/**
* Sorts the scores 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 j = 0; j < users.size(); j++) {
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"));
fruits.setText(inputMap.get("input_footprint_shopping_food_fruitvegetables"));
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"));
bike.setText(extraMap.get("bike"));
solarPanels.setText(extraMap.get("solar_panels"));
@@ -515,21 +517,23 @@ public class DashBoardController {
* @throws InterruptedException throws exception
*/
public void updateLeaderboard() throws InterruptedException {
List<String> userList = userService.getAllUsers();
//global leaderboard
globalLeaderboard.getItems().clear();
globalLeaderData.removeAll();
sortScores(userList);
List<String> userList = userService.getAllUsers();
List<String> firstList = 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)));
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);
List<String> secondList = sortDiffScores(userList);
for (int i = userList.size() - 1; i >= 0; i--) {
Friend user = new Friend(firstList.get(i), userService.getFootprint(firstList.get(i)));
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);
}
globalLeaderboard.setItems(globalLeaderData);
@@ -572,32 +576,32 @@ public class DashBoardController {
*/
public void updateAchievements() {
Map achievements = userService.getAchievements(userService.currentUser.getName());
if((Boolean)achievements.get("Starting off")) {
if ((Boolean)achievements.get("Starting off")) {
achieve1.setOpacity(1);
} else {
achieve1.setOpacity(0.3);
}
if((Boolean)achievements.get("Social butterfly")) {
if ((Boolean)achievements.get("Social butterfly")) {
achieve2.setOpacity(1);
} else {
achieve2.setOpacity(0.3);
}
if((Boolean)achievements.get("Green saver")) {
if ((Boolean)achievements.get("Green saver")) {
achieve3.setOpacity(1);
} else {
achieve3.setOpacity(0.3);
}
if((Boolean)achievements.get("Animal friend")) {
if ((Boolean)achievements.get("Animal friend")) {
achieve4.setOpacity(1);
} else {
achieve4.setOpacity(0.3);
}
if((Boolean)achievements.get("Tom Dumoulin")) {
if ((Boolean)achievements.get("Tom Dumoulin")) {
achieve5.setOpacity(1);
} else {
achieve5.setOpacity(0.3);
}
if((Boolean)achievements.get("Let it shine")) {
if ((Boolean)achievements.get("Let it shine")) {
achieve6.setOpacity(1);
} else {
achieve6.setOpacity(0.3);

View File

@@ -8,13 +8,10 @@ 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;
@@ -83,8 +80,6 @@ public class ExtraActivityController {
private Label publicTransportLabel;
@FXML
private Slider publicTransportSlider;
@FXML
private Button saveButton;
/**
@@ -216,29 +211,26 @@ public class ExtraActivityController {
publicTransportPane.setVisible(true);
}
/**
* The method updates the values.
*/
@SuppressWarnings("Duplicates")
public void save(ActionEvent event) throws InterruptedException {
Window owner = saveButton.getScene().getWindow();
public void save(ActionEvent event) {
try {
updateExtras();
Float footprint = userService.saveFootprint(userService.currentUser.getName());
controller.updateLeaderboard();
controller.updateAchievements();
Stage current = (Stage) owner;
current.close();
UserController.AlertHelper.showAlert(Alert.AlertType.CONFIRMATION, owner, "Activities are added!",
"Your new activities are added!");
} catch (Exception ex) {
System.out.println("continue");
}
}
/**
* The method updates the values of extras.
* @param event user clicks to button
*/
@SuppressWarnings("Duplicates")
public void updateExtras(ActionEvent event) throws InterruptedException {
public void updateExtras() throws InterruptedException {
if (!bikeLabel.getText().replace(" km", "").equals("0")) {
userService.updateExtraInput(userService.currentUser.getName(),
"bike",
@@ -251,11 +243,44 @@ public class ExtraActivityController {
}
if (!temperatureLabel.getText().replace(" Degrees", "").equals("0")) {
userService.updateExtraInput(userService.currentUser.getName(),
"bike",
"temperature",
temperatureLabel.getText().replace(" Degrees", ""));
}
if (!publicTransportLabel.getText().replace(" km", "").equals("0")) {
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 {

View File

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

View File

@@ -1,21 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?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">
<children>
@@ -84,7 +73,7 @@
<Font size="20.0" />
</font>
</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>
<ImageView fitHeight="116.0" fitWidth="156.0" pickOnBounds="true" preserveRatio="true">
<image>
@@ -112,7 +101,7 @@
<Font size="18.0" />
</font>
</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 name="System Bold Italic" size="18.0" />
</font>
@@ -158,7 +147,7 @@
</Label>
</children>
</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 name="System Bold Italic" size="18.0" />
</font>
@@ -195,7 +184,7 @@
</children>
</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" />
<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 name="System Bold Italic" size="18.0" />
</font>
@@ -217,7 +206,7 @@
<Font size="20.0" />
</font>
</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 name="System Bold Italic" size="18.0" />
</font>
@@ -245,7 +234,7 @@
<Font size="18.0" />
</font>
</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 name="System Bold Italic" size="18.0" />
</font>

View File

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

View File

@@ -91,16 +91,21 @@ public class CalculatorService {
Map<String, String> inputs = user.getFootPrintInputs();
Float netPublic = Float.parseFloat(user.getFootPrintInputs()
.get("input_footprint_transportation_publictrans"))
- Float.parseFloat(user.getExtraInputs().get("bike"));
+ Float.parseFloat(user.getExtraInputs().get("public_transport"));
Float netCar = Float.parseFloat(user.getFootPrintInputs()
.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()
.get("input_footprint_shopping_food_fruitvegetables"))
+ 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_miles1", netCar + "");
inputs.put("input_footprint_shopping_food_fruitvegetables", netVegan + "");
inputs.put("input_footprint_shopping_goods_total", netShopping + "");
user.setFootPrintInputs(inputs);
}
}