ADD:: achievement class
This commit is contained in:
@@ -0,0 +1,106 @@
|
|||||||
|
package greenify.server.data.model;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Achievement {
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private boolean achieved;
|
||||||
|
|
||||||
|
public Achievement() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for an achievement.
|
||||||
|
* @param name name of the achievement
|
||||||
|
* @param description description of the achievement
|
||||||
|
* @param achieved whether the achievement is achieved or not
|
||||||
|
*/
|
||||||
|
public Achievement(String name, String description, boolean achieved) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.achieved = achieved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the name of an achievement.
|
||||||
|
* @return name of the achievement
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the name of an achievement.
|
||||||
|
* @param name name of the achievement
|
||||||
|
*/
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the description of an achievement.
|
||||||
|
* @return the description of the achievement
|
||||||
|
*/
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the name of an achievement.
|
||||||
|
* @param description the description of an achievement
|
||||||
|
*/
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the state of an achievement.
|
||||||
|
* @return achievement is (not) achieved
|
||||||
|
*/
|
||||||
|
public boolean isAchieved() {
|
||||||
|
return achieved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the state of an achievement.
|
||||||
|
* @param achieved achievement is (not) achieved
|
||||||
|
*/
|
||||||
|
public void setAchieved(boolean achieved) {
|
||||||
|
this.achieved = achieved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets a human readable (JSON) object.
|
||||||
|
* @return the JSON form of the object
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Achievement(name=" + name + ", description=" + description +
|
||||||
|
", achieved=" + achieved + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method checks whether two users are equal or not.
|
||||||
|
* @param other another achievement
|
||||||
|
* @return achievements are (not) equal
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (other instanceof Achievement) {
|
||||||
|
Achievement that = (Achievement) other;
|
||||||
|
return achieved == that.achieved &&
|
||||||
|
name.equals(that.name) &&
|
||||||
|
Objects.equals(description, that.description);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the hashcode of an achievement.
|
||||||
|
* @return hashcode of an achievement
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name, description, achieved);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package greenify.server.data.model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class AchievementTest {
|
||||||
|
private Achievement achievement = new Achievement("SavedCO2", "You saved 100 cow farts of CO2!", true);
|
||||||
|
private Achievement other = new Achievement("SavedCO2", "You saved 100 cow farts of CO2!", true);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAndSetTest() {
|
||||||
|
Achievement testAchievement = new Achievement();
|
||||||
|
testAchievement.setName("SavedCO2");
|
||||||
|
testAchievement.setDescription("You saved 100 cow farts of CO2!");
|
||||||
|
testAchievement.setAchieved(true);
|
||||||
|
assertEquals("SavedCO2", achievement.getName());
|
||||||
|
assertEquals("You saved 100 cow farts of CO2!", achievement.getDescription());
|
||||||
|
assertTrue(achievement.isAchieved());
|
||||||
|
assertEquals(achievement, testAchievement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toStringTest() {
|
||||||
|
assertEquals("Achievement(name=SavedCO2, description=You saved 100 cow farts of CO2!, achieved=true)", achievement.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void equalsTest() {
|
||||||
|
assertEquals(achievement.getName(), other.getName());
|
||||||
|
assertEquals(achievement.getDescription(), other.getDescription());
|
||||||
|
assertEquals(achievement.isAchieved(), other.isAchieved());
|
||||||
|
assertEquals(achievement, other);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hashCodeTest() {
|
||||||
|
assertEquals(achievement, other);
|
||||||
|
assertEquals(achievement.hashCode(), other.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user