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 89720f6..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 @@ -5,6 +5,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import javax.persistence.*; import javax.validation.constraints.NotNull; +import java.util.Objects; @EnableAutoConfiguration @Entity @@ -79,4 +80,57 @@ public class User { } 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/UserTest.java b/src/Server/src/test/java/UserTest.java index 35b3ea0..3923d0b 100644 --- a/src/Server/src/test/java/UserTest.java +++ b/src/Server/src/test/java/UserTest.java @@ -16,13 +16,25 @@ public class UserTest { assertEquals(user.getName(), "greenify"); assertEquals(user.getPassword(), "password"); assertEquals(user.getVeganMeal(), 3); - assertEquals(user, testUser); + + } + + @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", 3); - assertEquals("User(id=1, name=greenify, password=password, veganMeal=3)", user.toString()); + System.out.println("String is " + user.toString()); + assertTrue("User(id=1, name=greenify, password=password, veganMeal=3)".equals(user.toString())); } @Test