Friend list to JSON method added

This commit is contained in:
Merel Steenbergen
2019-03-26 11:07:04 +01:00
parent cc9f8196bc
commit 4a6d4c7043
2 changed files with 22 additions and 2 deletions

View File

@@ -143,6 +143,17 @@ public class User {
+ this.password + ")";
}
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) {

View File

@@ -96,7 +96,6 @@ public class UserTest {
User first = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password");
assertEquals(first.getFriends(), second.getFriends());
System.out.print(second);
first.addFriend(second);
ArrayList<User> test = new ArrayList<User>();
test.add(second);
@@ -109,6 +108,16 @@ public class UserTest {
assertEquals(test.getFriends(), new ArrayList<User>());
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}]");
}
}