Skip to content

Commit

Permalink
minor fixes and changes to the leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
floerer committed Apr 15, 2019
1 parent f6ec011 commit d4702c1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
17 changes: 17 additions & 0 deletions client/src/main/java/groupxii/client/connector/UserConnector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package groupxii.client.connector;

import static groupxii.client.connector.Connector.postRequest;

public class UserConnector {

/**
* communicates with the target server, and updates the
* total co2 reduction of the user in the database.
* @param amount of CO2 the user has reduced
* @return JSON String
*/
public static String updateReducedCo2(int amount) {
return postRequest("/increaseReducedCo2OfUser?amount="
+ amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
Expand All @@ -18,18 +17,16 @@
import javafx.scene.text.Text;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

public class LeaderboardController implements Initializable {
public class LeaderboardController {

private static int userId = 1;

@FXML
private static Text addedFriend = new Text();
private Text addedFriend = new Text();

private String overallListStr = "";
private String friendListStr = "";
Expand All @@ -41,7 +38,7 @@ public class LeaderboardController implements Initializable {
@FXML
private ListView friendsLeaderboard = new ListView();

public static class HBoxCell extends HBox {
public class HBoxCell extends HBox {
Label label = new Label();
Button button = new Button();

Expand All @@ -63,8 +60,7 @@ public void handle(ActionEvent event) {
}
}

@Override
public void initialize(URL location, ResourceBundle resources) {
public void initialize() {

/*
try {
Expand Down Expand Up @@ -119,13 +115,11 @@ public void initialize(URL location, ResourceBundle resources) {
//friendsLeaderboard.setItems(friendsLeaderboardObservableList);
}

public static void addFriend(int friendId) {
public String addFriend(int friendId) {
LeaderboardConnector.addFriend(userId, friendId);
addedFriend.setText("Ädded new friend!");
}

public void setUserId(int userId) {
this.userId = userId;
addedFriend.setText("Added new friend!");
initialize();
return "Added new friend!";
}

@FXML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import groupxii.client.Main;
import groupxii.client.connector.Connector;
import groupxii.client.connector.LocalProductsConnector;
import groupxii.client.connector.UserConnector;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
Expand Down Expand Up @@ -92,8 +93,14 @@ public String getShopLocation() {
}
}

public void safeLocalProduct() throws IOException {
Connector.instance.postRequest("/increaseReducedCO2?Id=" + userId + "&ReducedCO2=300");
public void safeLocalProduct() {
if (localShops.getSelectionModel().getSelectedItem() == null) {
textfield.setText("Please choose a shop first");
} else{
UserConnector.updateReducedCo2(300);
textfield.setText("You saved 300 grams of CO2 emission!");
}
UserConnector.updateReducedCo2(300);
textfield.setText("You saved 300 grams of CO2 emission!");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public int getReducedCo2OfUser(Principal principal) {
int reducedCo2 = user.getReducedCo2();
return reducedCo2;
}

@RequestMapping(method = RequestMethod.POST, value = "/increaseReducedCo2OfUser")
public int increaserReducedCo2OfUser(Principal principal,
@RequestParam(value = "amount",
defaultValue = "0") int amount){
String username = principal.getName();
//UserEntry user = Database.instance.findUserByName(username);
Database.instance.incrementReducedCo2(username, amount);
return amount;
}


}


0 comments on commit d4702c1

Please sign in to comment.