From cea68e93c9418ccdc68114584127aad9c5a0193c Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 14:04:04 +0100 Subject: [PATCH 1/4] Add outline of activity class, has bugs --- .../java/greenify/common/ActivityDTO.java | 34 ++++++ .../greenify/server/data/model/Activity.java | 102 ++++++++++++++++++ .../java/greenify/server/data/model/User.java | 1 - 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/Common/src/main/java/greenify/common/ActivityDTO.java create mode 100644 src/Server/src/main/java/greenify/server/data/model/Activity.java diff --git a/src/Common/src/main/java/greenify/common/ActivityDTO.java b/src/Common/src/main/java/greenify/common/ActivityDTO.java new file mode 100644 index 0000000..6ed2fbc --- /dev/null +++ b/src/Common/src/main/java/greenify/common/ActivityDTO.java @@ -0,0 +1,34 @@ +package greenify.common; + +public class ActivityDTO { + private Long id; + private String name; + private String description; + private int score; + + public ActivityDTO() { + } + + public ActivityDTO(Long id, String name, String description, int score) { + this.id = id; + this.name = name; + this.description=description; + this.score= score; + } + + public String getName() { + return name; + } + + public Long getId() { + return id; + } + + public String getDescription() { + return description; + } + + public int getScore() { + return score; + } +} diff --git a/src/Server/src/main/java/greenify/server/data/model/Activity.java b/src/Server/src/main/java/greenify/server/data/model/Activity.java new file mode 100644 index 0000000..99635e4 --- /dev/null +++ b/src/Server/src/main/java/greenify/server/data/model/Activity.java @@ -0,0 +1,102 @@ +package greenify.server.data.model; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Scanner; + +@Entity +@Data +//@AllArgsConstructor +public abstract class Activity { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + Long id; + String name; + String description; + int score; + + /** + * makes an activity object. + * @param id the id of the activity. + * @param name the name of the feature. + * @param description the description of the feature. + * @param score the amount of points a user gets for doing this activity. + */ + public void User(Long id, String name, String description, int score) { + this.id = id; + this.name = name; + this.description = setDes(description); + this.score = score; + } + + /** + * gets the id. + * @return the id + */ + public Long getId() { + return id; + } + + /** + * gets the name. + * @return the name + */ + public String getName() { + return name; + } + + /** + * gets the password. + * @return the password + */ + public String getDescription() { + return description; + } + + public void setId(Long id) { + this.id = id; + } + + /** + * sets the name. + * @param name the you want to assign to this.name. + */ + public void setName(String name) { + this.name = name; + } + + /** + * sets the description. + * @param description the description to be set. + */ + public String setDes(String description){ + Scanner scan = new Scanner(System.in); + System.out.println("Please enter the description"); + description = scan.nextLine(); + scan.close(); + return description; + } + + /** + * sets the score you get for an activity. + * @param score the you want to assign to the activity. + */ + public void setScore(int score) { + this.score = score; + } + + /** + * Returns a human readable object. It's in JSON. + * @return the JSON form of the object. + */ + @Override + public String toString() { + return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")"; + } +} \ No newline at end of file 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 5d3057a..38b6187 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 @@ -11,7 +11,6 @@ import javax.persistence.Id; @Entity @Data //@AllArgsConstructor -@NoArgsConstructor public class User { @Id From cca49bc9d6b21871fb19ef184d42bf86aa09ccdd Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 14:31:38 +0100 Subject: [PATCH 2/4] Fix bugs. Feature abstract class added --- src/Server/src/main/java/greenify/server/data/model/User.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 38b6187..6e703d1 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 @@ -11,6 +11,7 @@ import javax.persistence.Id; @Entity @Data //@AllArgsConstructor +@NoArgsConstructor public class User { @Id @@ -72,4 +73,4 @@ public class User { return "User(id=" + this.id + ", name=" + this.name + ", password=" + this.password + ")"; } -} \ No newline at end of file +} From 8bbe3f0fe950188a3393f6b261ece72f7481c722 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 15:08:49 +0100 Subject: [PATCH 3/4] Some more work done on the feature object --- .../greenify/client/rest/ActivityService.java | 25 ++++++++++++++++++ .../java/greenify/common/ActivityDTO.java | 2 +- .../server/rest/ActivityController.java | 26 +++++++++++++++++++ .../server/service/ActivityService.java | 6 +++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/Client/src/main/java/greenify/client/rest/ActivityService.java create mode 100644 src/Server/src/main/java/greenify/server/rest/ActivityController.java create mode 100644 src/Server/src/main/java/greenify/server/service/ActivityService.java diff --git a/src/Client/src/main/java/greenify/client/rest/ActivityService.java b/src/Client/src/main/java/greenify/client/rest/ActivityService.java new file mode 100644 index 0000000..fe69b90 --- /dev/null +++ b/src/Client/src/main/java/greenify/client/rest/ActivityService.java @@ -0,0 +1,25 @@ +package greenify.client.rest; + +import greenify.common.ActivityDTO; +import greenify.common.ApplicationException; +import greenify.common.UserDTO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +public class ActivityService { + + Logger logger = LoggerFactory.getLogger(greenify.server.service.ActivityService.class); + @Autowired + UserRepository userRepository; + + /** + * Adds the activity to the history of the user and adds the score. + * @param name the name of the activity + * @return a activityDTO of activity. + */ + public ActivityDTO addActivity(String name) { + + } + +} diff --git a/src/Common/src/main/java/greenify/common/ActivityDTO.java b/src/Common/src/main/java/greenify/common/ActivityDTO.java index 6ed2fbc..a564f1e 100644 --- a/src/Common/src/main/java/greenify/common/ActivityDTO.java +++ b/src/Common/src/main/java/greenify/common/ActivityDTO.java @@ -12,7 +12,7 @@ public class ActivityDTO { public ActivityDTO(Long id, String name, String description, int score) { this.id = id; this.name = name; - this.description=description; + this.description = description; this.score= score; } diff --git a/src/Server/src/main/java/greenify/server/rest/ActivityController.java b/src/Server/src/main/java/greenify/server/rest/ActivityController.java new file mode 100644 index 0000000..e2f9f2d --- /dev/null +++ b/src/Server/src/main/java/greenify/server/rest/ActivityController.java @@ -0,0 +1,26 @@ +package greenify.server.rest; + +import greenify.common.UserDTO; +import greenify.server.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ActivityController { + @Autowired + UserService userService; + + @RequestMapping("/addActivity") + public UserDTO registerUser(@RequestParam(value = "name") String name, + @RequestParam(value = "password") String password) { + return userService.registerUser(name, password); + } + + @RequestMapping("/login") + public UserDTO login(@RequestParam(value = "name") String name, + @RequestParam(value = "password") String password) { + return userService.login(name, password); + } +} \ No newline at end of file diff --git a/src/Server/src/main/java/greenify/server/service/ActivityService.java b/src/Server/src/main/java/greenify/server/service/ActivityService.java new file mode 100644 index 0000000..a9c2bbc --- /dev/null +++ b/src/Server/src/main/java/greenify/server/service/ActivityService.java @@ -0,0 +1,6 @@ +package greenify.server.service; + +public class ActivityService { + + +} From 34d098f1889ba2d983ef46b2213fcbae3c9b2331 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Sun, 17 Mar 2019 16:57:09 +0100 Subject: [PATCH 4/4] Finish activity class and add tests --- .../greenify/client/rest/ActivityService.java | 25 ----- .../greenify/server/data/model/Activity.java | 68 ++++++++---- .../java/greenify/server/data/model/User.java | 100 ++++++++++++++---- .../server/service/ActivityService.java | 6 -- .../greenify/server/service/UserService.java | 2 +- src/Server/src/test/java/ActivityTest.java | 48 +++++++++ src/Server/src/test/java/UserTest.java | 33 ++++-- 7 files changed, 203 insertions(+), 79 deletions(-) delete mode 100644 src/Client/src/main/java/greenify/client/rest/ActivityService.java delete mode 100644 src/Server/src/main/java/greenify/server/service/ActivityService.java create mode 100644 src/Server/src/test/java/ActivityTest.java diff --git a/src/Client/src/main/java/greenify/client/rest/ActivityService.java b/src/Client/src/main/java/greenify/client/rest/ActivityService.java deleted file mode 100644 index fe69b90..0000000 --- a/src/Client/src/main/java/greenify/client/rest/ActivityService.java +++ /dev/null @@ -1,25 +0,0 @@ -package greenify.client.rest; - -import greenify.common.ActivityDTO; -import greenify.common.ApplicationException; -import greenify.common.UserDTO; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -public class ActivityService { - - Logger logger = LoggerFactory.getLogger(greenify.server.service.ActivityService.class); - @Autowired - UserRepository userRepository; - - /** - * Adds the activity to the history of the user and adds the score. - * @param name the name of the activity - * @return a activityDTO of activity. - */ - public ActivityDTO addActivity(String name) { - - } - -} diff --git a/src/Server/src/main/java/greenify/server/data/model/Activity.java b/src/Server/src/main/java/greenify/server/data/model/Activity.java index 99635e4..01a2e02 100644 --- a/src/Server/src/main/java/greenify/server/data/model/Activity.java +++ b/src/Server/src/main/java/greenify/server/data/model/Activity.java @@ -3,20 +3,20 @@ package greenify.server.data.model; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.accessibility.AccessibleValue; +import javax.persistence.*; +import java.util.Objects; import java.util.Scanner; @Entity @Data +@Table(name = "activities") //@AllArgsConstructor -public abstract class Activity { +public class Activity { @Id @GeneratedValue(strategy = GenerationType.AUTO) - Long id; + long id; String name; String description; int score; @@ -28,10 +28,10 @@ public abstract class Activity { * @param description the description of the feature. * @param score the amount of points a user gets for doing this activity. */ - public void User(Long id, String name, String description, int score) { + public Activity(long id, String name, String description, int score) { this.id = id; this.name = name; - this.description = setDes(description); + this.description = description; this.score = score; } @@ -39,7 +39,7 @@ public abstract class Activity { * gets the id. * @return the id */ - public Long getId() { + public long getId() { return id; } @@ -52,14 +52,27 @@ public abstract class Activity { } /** - * gets the password. - * @return the password + * gets the description. + * @return the description */ public String getDescription() { return description; } - public void setId(Long id) { + /** + * gets the score. + * @return the score + */ + public int getScore() { + return score; + } + + + /** + * sets the id. + * @param id the you want to assign to this.id. + */ + public void setId(long id) { this.id = id; } @@ -75,12 +88,8 @@ public abstract class Activity { * sets the description. * @param description the description to be set. */ - public String setDes(String description){ - Scanner scan = new Scanner(System.in); - System.out.println("Please enter the description"); - description = scan.nextLine(); - scan.close(); - return description; + public void setDescription(String description){ + this.description = description; } /** @@ -91,6 +100,7 @@ public abstract class Activity { this.score = score; } + /** * Returns a human readable object. It's in JSON. * @return the JSON form of the object. @@ -99,4 +109,26 @@ public abstract class Activity { public String toString() { return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")"; } + + @Override + public boolean equals(Object other) { + if(other instanceof Activity){ + Activity that = (Activity)other; + if(that.id != this.id) + return false; + if(!that.name.equals(this.name)) + return false; + if(!that.description.equals(this.description)) + return false; + if(that.score != this.score) + return false; + return true; + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(id, name, description, score); + } } \ No newline at end of file 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 6e703d1..d588eb1 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,35 +1,44 @@ package greenify.server.data.model; import lombok.Data; -import lombok.NoArgsConstructor; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; +import javax.validation.constraints.NotNull; +import java.util.Objects; +@EnableAutoConfiguration @Entity @Data -//@AllArgsConstructor -@NoArgsConstructor +@Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) - Long id; - String name; - String password; + private Long id; + + @NotNull + private String name; + + @NotNull + private String password; + + private int veganMeal; + + public User() {} /** * makes a user object. * @param id the id of the user. * @param name the supplied username * @param password the supplied password + * @param veganMeal the supplied number of vegan meal */ - public User(Long id, String name, String password) { + public User(Long id, String name, String password, int veganMeal) { this.id = id; this.name = name; this.password = password; + this.veganMeal = veganMeal; } /** @@ -40,6 +49,8 @@ public class User { return id; } + public void setId(Long id) { this.id = id; } + /** * gets the name. * @return the name @@ -48,6 +59,8 @@ public class User { return name; } + public void setName(String name) { this.name = name; } + /** * gets the password. * @return the password @@ -56,21 +69,68 @@ public class User { return password; } - public void setId(Long id) { - this.id = id; + public void setPassword(String password) { this.password = password; } + + /** + * gets the number of vegan meal. + * @return the veganMeal + */ + public int getVeganMeal() { + return veganMeal; } - public void setName(String name) { - this.name = name; - } - - public void setPassword(String password) { - this.password = password; + public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; } + + /** + * checks if two users are equal. + * @param other the object to compare the user with + * @return a boolean value of true if the user is equal to the object + */ + @Override + public boolean equals(Object other) { + + if (this == other) { + return true; + } + if (other == null) { + return false; + } + if (getClass() != other.getClass()) { + return false; + } + if (other instanceof User) { + User that = (User) other; + return this.getName().equals(that.getName()) + && this.getId().equals(that.getId()) + && this.getPassword().equals(that.getPassword()) + && this.getVeganMeal() == that.getVeganMeal(); + } + return false; } + /** + * creates a string of the user object. + * in the form of: User(id=, name=, password=, veganMeal=) + * @return a string of the user object + */ @Override public String toString() { + return "User(id=" + + this.id + + ", name=" + + this.name + + ", password=" + + this.password + + ", veganMeal=" + + this.veganMeal + ")"; + } - return "User(id=" + this.id + ", name=" + this.name + ", password=" + this.password + ")"; + /** + * hashes the User object. + * @return a hashcode for the user object + */ + @Override + public int hashCode() { + return Objects.hash(id, name, password, veganMeal); } } diff --git a/src/Server/src/main/java/greenify/server/service/ActivityService.java b/src/Server/src/main/java/greenify/server/service/ActivityService.java deleted file mode 100644 index a9c2bbc..0000000 --- a/src/Server/src/main/java/greenify/server/service/ActivityService.java +++ /dev/null @@ -1,6 +0,0 @@ -package greenify.server.service; - -public class ActivityService { - - -} 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 4756726..a844a1d 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -26,7 +26,7 @@ public class UserService { if (user != null) { throw new ApplicationException("User already exists"); } else { - user = userRepository.save(new User(null, name, password)); + user = userRepository.save(new User(null, name, password, 0)); } logger.info("Created user id=" + user.getId() + ", name=" + user.getName()); return new UserDTO(user.getId(), user.getName()); diff --git a/src/Server/src/test/java/ActivityTest.java b/src/Server/src/test/java/ActivityTest.java new file mode 100644 index 0000000..7a491e2 --- /dev/null +++ b/src/Server/src/test/java/ActivityTest.java @@ -0,0 +1,48 @@ +import greenify.server.data.model.Activity; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ActivityTest { + @Test + public void setAndGetTest() { + Activity activity = new Activity(1, "Vegetarian", "Eating", 100); + Activity testActivity = new Activity(0, null, null, 0); + testActivity.setId(1); + testActivity.setName("Vegetarian"); + testActivity.setDescription("Eating"); + testActivity.setScore(100); + assertEquals(activity.getId(), 1); + assertEquals(activity.getName(), "Vegetarian"); + assertEquals(activity.getDescription(), "Eating"); + assertEquals(activity.getScore(), 100); + assertEquals(activity, testActivity); + } + + @Test + public void toStringTest() { + Activity activity = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals("Activity(id=1, name=Solar panels, description=Installed, score=10000)", activity.toString()); + } + + @Test + public void equalsTest() { + Activity first = new Activity(1, "Solar panels", "Installed", 10000); + Activity second = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals(first.getId(), second.getId()); + assertEquals(first.getName(), second.getName()); + assertEquals(first.getDescription(), second.getDescription()); + assertEquals(first.getScore(), second.getScore()); + assertTrue(first.equals(second)); + } + + @Test + public void hashCodeTest() { + Activity first = new Activity(1, "Solar panels", "Installed", 10000); + Activity second = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals(first, second); + assertEquals(first.hashCode(), second.hashCode()); +// assertTrue(first.hashCode() == second.hashCode()); + } +} diff --git a/src/Server/src/test/java/UserTest.java b/src/Server/src/test/java/UserTest.java index ae359c9..3e557ca 100644 --- a/src/Server/src/test/java/UserTest.java +++ b/src/Server/src/test/java/UserTest.java @@ -6,36 +6,51 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class UserTest { @Test public void setAndGetTest() { - User user = new User(1L, "greenify", "password"); - User testUser = new User(null, null, null); + User user = new User(1L, "greenify", "password", 3); + User testUser = new User(); testUser.setId(1L); testUser.setName("greenify"); testUser.setPassword("password"); + testUser.setVeganMeal(3); assertTrue(user.getId().equals(1L)); assertEquals(user.getName(), "greenify"); assertEquals(user.getPassword(), "password"); - assertEquals(user, testUser); + assertEquals(user.getVeganMeal(), 3); + + } + + @Test + public void equalsMethodTest() { + User user = new User(1L, "greenify", "password", 3); + User testUser = new User(); + testUser.setId(1L); + testUser.setName("greenify"); + testUser.setPassword("password"); + testUser.setVeganMeal(3); + assertTrue(user.equals(testUser)); } @Test public void toStringTest() { - User user = new User(1L, "greenify", "password"); - assertEquals("User(id=1, name=greenify, password=password)", user.toString()); + User user = new User(1L, "greenify", "password", 3); + System.out.println("String is " + user.toString()); + assertTrue("User(id=1, name=greenify, password=password, veganMeal=3)".equals(user.toString())); } @Test public void equalsTest() { - User first = new User(1L, "greenify", "password"); - User second = new User(1L, "greenify", "password"); + User first = new User(1L, "greenify", "password", 3); + User second = new User(1L, "greenify", "password", 3); assertEquals(first.getId(), second.getId()); assertEquals(first.getName(), second.getName()); assertEquals(first.getPassword(), second.getPassword()); + assertEquals(first.getVeganMeal(), second.getVeganMeal()); } @Test public void hashCodeTest() { - User first = new User(1L, "greenify", "password"); - User second = new User(1L, "greenify", "password"); + User first = new User(1L, "greenify", "password", 3); + User second = new User(1L, "greenify", "password", 3); assertTrue(first.equals(second) && second.equals(first)); assertTrue(first.hashCode() == second.hashCode()); }