diff --git a/README.md b/README.md index 07de1d6..87cc6a7 100644 --- a/README.md +++ b/README.md @@ -6,26 +6,6 @@ Run `maven install` ([Intellij](https://www.jetbrains.com/help/idea/2016.3/getti **Checkstyle**: Run `maven site` -# Daan Sneep (4849515, dsneep) - -![My Picture](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/Daan_Sneep.png "The one and only") - -## Personal Development Plan - -My Core Quadrant is visible ![here](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/Daan_Sneep_PDP_Core_Quadrant.PNG) - -### G – Goal -After completing this project, I want to be able to work on a programming project efficiently with other people. I have never attempted anything of this scale and find that it is a core requirement of becoming a good programmer, as in most companies programming is done within teams, not alone. - -### R – Reality -With this project I hope to work towards this goal. I have never programmed as a team on this scale, so this project will be the first step towards my goal. - -### O – Options -During this quarter the project will be my only option towards achieving this goal. If I am unhappy with my progress towards my goal by the end of this quarter, I must try to implement the same structure of teamwork in my next projects, and try to learn from the mistakes I made when working as a team in this project. - -### W – Will -I will try to make sure to adhere to the structure we establish as a group which describes how we will work together. This way, we all do the work we are supposed to do, and none of us will be overloaded with work. I will also try to make use of all the tools available to us which can improve the quality of programming together (scrum, git, GitLab etc.). - # Merel Steenbergen (masteenbergen) ![Picture:](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/Merel_Steenbergen.jpg) diff --git a/doc/Pictures Group Members/Daan_Sneep.png b/doc/Pictures Group Members/Daan_Sneep.png deleted file mode 100644 index 623c64e..0000000 Binary files a/doc/Pictures Group Members/Daan_Sneep.png and /dev/null differ diff --git a/doc/Pictures Group Members/Daan_Sneep_PDP_Core_Quadrant.PNG b/doc/Pictures Group Members/Daan_Sneep_PDP_Core_Quadrant.PNG deleted file mode 100644 index c747447..0000000 Binary files a/doc/Pictures Group Members/Daan_Sneep_PDP_Core_Quadrant.PNG and /dev/null differ diff --git a/doc/Pictures Group Members/KristinPeneva.jpg b/doc/Pictures Group Members/KristinPeneva.jpg deleted file mode 100644 index ab2c35d..0000000 Binary files a/doc/Pictures Group Members/KristinPeneva.jpg and /dev/null differ diff --git a/doc/Pictures Group Members/corequadrant-KP.png b/doc/Pictures Group Members/corequadrant-KP.png deleted file mode 100644 index 2e7b84b..0000000 Binary files a/doc/Pictures Group Members/corequadrant-KP.png and /dev/null differ diff --git a/doc/meetings/week7/20190325_notes.pdf b/doc/meetings/week7/20190325_notes.pdf new file mode 100644 index 0000000..35d20a4 Binary files /dev/null and b/doc/meetings/week7/20190325_notes.pdf differ diff --git a/src/Client/src/main/java/greenify/client/controller/CalculatorController.java b/src/Client/src/main/java/greenify/client/controller/CalculatorController.java new file mode 100644 index 0000000..8c6c98f --- /dev/null +++ b/src/Client/src/main/java/greenify/client/controller/CalculatorController.java @@ -0,0 +1,154 @@ +package greenify.client.controller; + +import greenify.client.rest.UserService; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.Slider; +import javafx.scene.layout.AnchorPane; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; + +@Controller +public class CalculatorController { + @Autowired + UserService userService; + + @FXML + private Button calculatorGetStartedButton; + @FXML + private Button calculatorTravelButton; + @FXML + private Button calculatorHomeButton; + @FXML + private Button calculatorFoodButton; + @FXML + private Button calculatorShoppingButton; + @FXML + private AnchorPane getStartedPane; + @FXML + private ScrollPane travelPane; + @FXML + private AnchorPane homePane; + @FXML + private AnchorPane foodPane; + @FXML + private AnchorPane shoppingPane; + @FXML + private Slider peopleInHouseholdSLider; + @FXML + private Label peopleInHouseHoldLabel; + @FXML + private Slider annualIncomeSlider; + @FXML + private Label annualIncomeLabel; + @FXML + private Button saveButton; + @FXML + private Button getStartedNextButton; + + /** + * initializes the window, performs some actions before loading all other things. + * it sets the sliders to snap to the ticks + * it adds listeners to all the sliders for updating the label next to them + */ + public void initialize() { + peopleInHouseholdSLider.setSnapToTicks(true); + annualIncomeSlider.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)); + } + }); + } + + /** + * displays the 'get started' section of the calculator. + * Activated when the designated button (navigation button) is clicked + * @param event the click of the button + */ + public void displayGetStarted(ActionEvent event) { + getStartedPane.setVisible(true); + travelPane.setVisible(false); + homePane.setVisible(false); + foodPane.setVisible(false); + shoppingPane.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 + */ + public void displayTravel(ActionEvent event) { + getStartedPane.setVisible(false); + travelPane.setVisible(true); + homePane.setVisible(false); + foodPane.setVisible(false); + shoppingPane.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 + */ + public void displayHome(ActionEvent event) { + getStartedPane.setVisible(false); + travelPane.setVisible(false); + homePane.setVisible(true); + foodPane.setVisible(false); + shoppingPane.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 + */ + public void displayFood(ActionEvent event) { + getStartedPane.setVisible(false); + travelPane.setVisible(false); + homePane.setVisible(false); + foodPane.setVisible(true); + shoppingPane.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 + */ + public void displayShopping(ActionEvent event) { + getStartedPane.setVisible(false); + travelPane.setVisible(false); + homePane.setVisible(false); + foodPane.setVisible(false); + shoppingPane.setVisible(true); + } + + + + + + + + +} 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 daa9e24..eb11baf 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -1,6 +1,7 @@ package greenify.client.controller; import com.sun.javafx.scene.control.skin.ButtonSkin; +import greenify.client.Application; import greenify.client.rest.UserService; import javafx.animation.FadeTransition; import javafx.animation.PathTransition; @@ -8,10 +9,13 @@ import javafx.animation.ScaleTransition; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.Node; +import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.AnchorPane; import javafx.scene.shape.Line; +import javafx.stage.Stage; import javafx.util.Duration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -55,6 +59,9 @@ public class DashBoardController { private AnchorPane menuBar; @FXML private Button addNewActivityButton; + @FXML + private Button calculateFootPrintButton; + @@ -71,6 +78,12 @@ public class DashBoardController { activitiesButton.setSkin(new MyButtonSkin(activitiesButton)); userButton.setSkin(new MyButtonSkin(userButton)); friendsButton.setSkin(new MyButtonSkin(friendsButton)); + + + } + + public UserService getUserService() { + return userService; } /** @@ -139,7 +152,6 @@ public class DashBoardController { userPane.setVisible(false); activitiesPane.setVisible(false); friendsPane.setVisible(true); - } //sets the slide in transition for startup @@ -148,6 +160,18 @@ public class DashBoardController { pathTrans.play(); } + public void openCalculator() throws IOException { + Parent calc = Application.load(this.getClass().getClassLoader() + .getResource("fxml/calculator.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(); + } + //class for the animations on the navigation buttons public class MyButtonSkin extends ButtonSkin { /** @@ -172,5 +196,6 @@ public class DashBoardController { scaleDown.setToX(1.0); button.setOnMouseExited(e -> scaleDown.playFromStart()); } + } } 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 c177e28..c52250e 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -98,4 +98,18 @@ public class UserService { return this.restTemplate.getForObject(builder.build() .encode().toUri(), String.class); } + + @SuppressWarnings("Duplicates") + public String addFriend(String name, String friend) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addFriend") + .queryParam("name", name) + .queryParam("friend",friend); + HttpEntity entity = new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + String result = this.restTemplate.getForObject(builder.build() + .encode().toUri(), String.class); + return result; + } } diff --git a/src/Client/src/main/resources/fxml/calculator.fxml b/src/Client/src/main/resources/fxml/calculator.fxml new file mode 100644 index 0000000..e4b7627 --- /dev/null +++ b/src/Client/src/main/resources/fxml/calculator.fxml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -160,7 +162,7 @@ - + @@ -181,8 +183,22 @@ + + + + + + + + + + - + - - + diff --git a/src/Client/src/main/resources/addActivity1.png b/src/Client/src/main/resources/icons/addActivity1.png similarity index 100% rename from src/Client/src/main/resources/addActivity1.png rename to src/Client/src/main/resources/icons/addActivity1.png diff --git a/src/Client/src/main/resources/addActivity2.png b/src/Client/src/main/resources/icons/addActivity2.png similarity index 100% rename from src/Client/src/main/resources/addActivity2.png rename to src/Client/src/main/resources/icons/addActivity2.png diff --git a/src/Client/src/main/resources/icons/cloud.png b/src/Client/src/main/resources/icons/cloud.png new file mode 100644 index 0000000..1cc8c2b Binary files /dev/null and b/src/Client/src/main/resources/icons/cloud.png differ diff --git a/src/Client/src/main/resources/icons/co2.png b/src/Client/src/main/resources/icons/co2.png new file mode 100644 index 0000000..bac53df Binary files /dev/null and b/src/Client/src/main/resources/icons/co2.png differ diff --git a/src/Client/src/main/resources/icons/co2_2.png b/src/Client/src/main/resources/icons/co2_2.png new file mode 100644 index 0000000..ef3516a Binary files /dev/null and b/src/Client/src/main/resources/icons/co2_2.png differ diff --git a/src/Client/src/main/resources/icons/coffee-cup.png b/src/Client/src/main/resources/icons/coffee-cup.png new file mode 100644 index 0000000..310c098 Binary files /dev/null and b/src/Client/src/main/resources/icons/coffee-cup.png differ diff --git a/src/Client/src/main/resources/icons/home.png b/src/Client/src/main/resources/icons/home.png new file mode 100644 index 0000000..9cecc79 Binary files /dev/null and b/src/Client/src/main/resources/icons/home.png differ diff --git a/src/Client/src/main/resources/icons/shopping-cart.png b/src/Client/src/main/resources/icons/shopping-cart.png new file mode 100644 index 0000000..7c76dcc Binary files /dev/null and b/src/Client/src/main/resources/icons/shopping-cart.png differ diff --git a/src/Client/src/main/resources/icons/world.png b/src/Client/src/main/resources/icons/world.png new file mode 100644 index 0000000..92643b8 Binary files /dev/null and b/src/Client/src/main/resources/icons/world.png differ diff --git a/src/Client/src/main/resources/stylesheets/calculatorStyle.css b/src/Client/src/main/resources/stylesheets/calculatorStyle.css new file mode 100644 index 0000000..af59819 --- /dev/null +++ b/src/Client/src/main/resources/stylesheets/calculatorStyle.css @@ -0,0 +1,35 @@ + +#calculatorTabs .navButton { + -fx-background-color: transparent; + -fx-font-size: 16px; +} +#calculatorTabs .navButton:pressed { + -fx-border-color: #677069; +} +#calculatorTabs .navButton:hover { + -fx-background-color: #707a72; +} +#calculatorTabs #saveButton { + -fx-background-color: #89a888; + -fx-font-size: 16px; + -fx-font-weight: regular; +} + +.nextButton { + -fx-background-color: #89a888; + -fx-font-size: 16px; + -fx-font-family: Corbel; +} +.nextButton:hover { + -fx-background-color: #9ec19c; +} + +.slider { + -show-value-on-interaction: true; + -fx-text-fill: red; +} + + + + + diff --git a/src/Client/src/main/resources/stylesheets/dashboardStyle.css b/src/Client/src/main/resources/stylesheets/dashboardStyle.css index 77bceaa..eb074d6 100644 --- a/src/Client/src/main/resources/stylesheets/dashboardStyle.css +++ b/src/Client/src/main/resources/stylesheets/dashboardStyle.css @@ -48,10 +48,15 @@ -fx-background-color: #b7e2c2; } + #addNewActivityButton:pressed { -fx-border-color: #497251; } +#calculateFootPrintButton:pressed { + -fx-border-color: #497251; +} + /*friends table*/ #friendsTable .column-header { /*-fx-background-color: linear-gradient(#4b7a3d, #588c58);*/ diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index 0fb5ccf..5232674 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -1,18 +1,17 @@ package greenify.server.data.model; +import greenify.common.ApplicationException; import greenify.server.InputValidator; import lombok.Data; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.*; -import javax.persistence.ElementCollection; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; import javax.validation.constraints.NotNull; @Entity @@ -21,6 +20,7 @@ import javax.validation.constraints.NotNull; public class User { @Id + @NotNull @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @@ -36,7 +36,11 @@ public class User { @ElementCollection private Map footPrintInputs = new HashMap<>(); - User() { } + @ManyToMany + @JoinColumn + private Collection friends; + + public User() {} /** * This method makes a user object. @@ -49,6 +53,7 @@ public class User { this.name = name; this.password = password; this.setFootPrintInputs(InputValidator.getDefaultValues()); + this.friends = new ArrayList(); } /** @@ -131,6 +136,29 @@ public class User { this.footPrintInputs = footPrintInputs; } + public ArrayList getFriends() { + return (ArrayList)this.friends; + } + + /** + * Adds a friend to the friendslist of the user. + * @param user the friend you want to add. + */ + public void addFriend(User user) { + if (user.equals(this)) { + throw new ApplicationException("Cannot add yourself as a friend"); + } + else { + friends.add(user); + System.out.print("Friend added!"); + } + } + + public void setFootPrint(Float footPrint) { + this.footPrint = footPrint; + } + + /** * This method gets a human readable (JSON) object. * @return the JSON form of the object. @@ -142,10 +170,20 @@ public class User { } /** - * This method checks whether two users are equal or not. - * @param other an other user - * @return users are (not) equal + * Returns the name and score of the friends in JSON. Needed for the leaderboard. + * @return a JSON object of the friendlist with only names and scores. */ + public String friendsToString(){ + String result = "friends=["; + for (User u : friends) { + result += "{name=" + u.getName() + ", footprint=" + u.getFootPrint() + "}, "; + } + if (result.endsWith(", ")) { + return result.substring(0, result.lastIndexOf(",")) + "]"; + } + return result + "]"; + } + @Override public boolean equals(Object other) { if (other instanceof User) { diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java index 9eb28d4..227a3cc 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -60,10 +60,35 @@ public class UserService { } /** - * This method sets input for a user. - * @param name name of the user - * @param inputName name of the input of the user - * @param value value of the input + * Adds a friend to the friendlist of the user. + * @param name the username of the user + * @param friend the name of the friend you want to add. + * @return a userDTO of the logged in user + */ + public void addFriend(String name, String friend) { + User user = userRepository.findByName(name); + User add = userRepository.findByName(friend); + if (add == null) { + throw new ApplicationException("User does not exist"); + } + user.addFriend(add); + } + + /** + * Returns the friendlist of the user in JSON. + * @param name the username of the user + * @return a userDTO of the logged in user + */ + public String getLeaderboard(String name) { + User user = userRepository.findByName(name); + return user.friendsToString(); + } + + /** + * The method sets input value. + * @param name of the user + * @param inputName is the name of the setting input + * @param value of the input */ public void setInput(String name, String inputName, String value) { User user = userRepository.findByName(name); diff --git a/src/Server/src/test/java/greenify/server/data/model/UserTest.java b/src/Server/src/test/java/greenify/server/data/model/UserTest.java index 0be519d..bec1398 100644 --- a/src/Server/src/test/java/greenify/server/data/model/UserTest.java +++ b/src/Server/src/test/java/greenify/server/data/model/UserTest.java @@ -1,10 +1,15 @@ package greenify.server.data.model; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import greenify.common.ApplicationException; import org.junit.Test; +import java.util.ArrayList; + public class UserTest { @Test @@ -79,4 +84,42 @@ public class UserTest { assertEquals(first.hashCode(), second.hashCode()); } + @Test + public void getFriendEmpty() { + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "merel", "password"); + assertEquals(first.getFriends(), second.getFriends()); + assertEquals(first.getFriends(), new ArrayList()); + } + + @Test + public void addFriendTest() { + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "merel", "password"); + assertEquals(first.getFriends(), second.getFriends()); + first.addFriend(second); + ArrayList test = new ArrayList(); + test.add(second); + assertEquals(first.getFriends(), test); + } + + @Test + public void addYourselfTest() { + User test = new User(1L, "greenify", "password"); + assertEquals(test.getFriends(), new ArrayList()); + assertThrows(ApplicationException.class, () -> { + test.addFriend(test); + }); + } + + + @Test + public void friendsToStringTest() { + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "merel", "password"); + first.addFriend(second); + assertEquals(first.friendsToString(), "friends=[{name=merel, footprint=0.0}]"); + } +} + } diff --git a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java index 7994f72..5a883ff 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -17,6 +17,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Bean; import org.springframework.test.context.junit4.SpringRunner; +import java.util.ArrayList; + @RunWith(SpringRunner.class) public class UserServiceTest { @TestConfiguration @@ -44,6 +46,9 @@ public class UserServiceTest { User alex = new User(1L, "alex", "password"); when(userRepository.findByName(alex.getName())) .thenReturn(alex); + User lola = new User(2L, "lola", "password"); + when(userRepository.findByName(lola.getName())) + .thenReturn(lola); } @Test @@ -144,4 +149,24 @@ public class UserServiceTest { assertThrows(ApplicationException.class, () -> userService.loginUser(null, null)); } + @Test + public void addFriendTest() { + User alex = userRepository.findByName("alex"); + User lola = userRepository.findByName("lola"); + assertEquals(lola.getFriends(), alex.getFriends()); + userService.addFriend("alex", "lola"); + ArrayList test = new ArrayList(); + test.add(lola); + assertEquals(alex.getFriends(), test); + } + + @Test + public void leaderboardTest() { + User alex = userRepository.findByName("alex"); + User lola = userRepository.findByName("lola"); + userService.addFriend("alex", "lola"); + assertEquals(userService.getLeaderboard("alex"), "friends=[{name=lola, footprint=0.0}]"); + + } + }