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..a564f1e --- /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..d15eedf --- /dev/null +++ b/src/Server/src/main/java/greenify/server/data/model/Activity.java @@ -0,0 +1,134 @@ +package greenify.server.data.model; + +import lombok.Data; +import javax.persistence.*; +import java.util.Objects; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +@Data +@Table(name = "activities") + +public 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 Activity(long id, String name, String description, int score) { + this.id = id; + this.name = name; + this.description = 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 description. + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * 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; + } + + /** + * 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 void setDescription(String description){ + this.description = 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 + ")"; + } + + @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 8ce92ea..8f7c03b 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 @@ -3,17 +3,15 @@ package greenify.server.data.model; import lombok.Data; 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.Table; +import javax.persistence.*; import javax.validation.constraints.NotNull; +import java.util.Objects; @EnableAutoConfiguration @Entity @Data @Table(name = "users") + public class User { @Id @@ -76,9 +74,7 @@ public class User { return password; } - public void setPassword(String password) { - this.password = password; - } + public void setPassword(String password) { this.password = password; } /** * gets the number of vegan meal. @@ -88,7 +84,58 @@ public class User { return veganMeal; } - public void setVeganMeal(int veganMeal) { - this.veganMeal = veganMeal; + 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 + ")"; + } + + /** + * 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/test/java/ActivityTest.java b/src/Server/src/test/java/ActivityTest.java new file mode 100644 index 0000000..28e39b9 --- /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(1L, "Vegetarian", "Eating", 100); + Activity testActivity = new Activity(0, null, null, 0); + testActivity.setId(1L); + testActivity.setName("Vegetarian"); + testActivity.setDescription("Eating"); + testActivity.setScore(100); + assertTrue(activity.getId().equals(1L)); + 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()); + } +}