Browse Source

player_management: returns player from list if existing

Tobias Krause 3 years ago
committed by Lorenz Hohmann
parent
commit
ca1185a335
  1. 12
      src/main/java/de/tims/player_management/Player.java
  2. 16
      src/main/java/de/tims/player_management/PlayerManager.java
  3. 11
      src/test/java/de/tims/player_management/PlayerManagerTest.java
  4. 4
      src/test/java/de/tims/player_management/PlayerTest.java

12
src/main/java/de/tims/player_management/Player.java

@ -2,20 +2,26 @@ package de.tims.player_management;
public class Player {
private String name;
private int points;
public Player(int points) {
public Player(String name, int points) {
this.name = name;
this.points = points;
}
public void addPoints(int pointsToAdd) {
this.points = (this.points + pointsToAdd > 0) ? this.points + pointsToAdd : 0;
public String getName() {
return name;
}
public int getPoints() {
return this.points;
}
public void addPoints(int pointsToAdd) {
this.points = (this.points + pointsToAdd > 0) ? this.points + pointsToAdd : 0;
}
@Override
public boolean equals(Object o) {
if (o == this) {

16
src/main/java/de/tims/player_management/PlayerManager.java

@ -1,21 +1,27 @@
package de.tims.player_management;
import java.util.LinkedList;
import java.util.List;
public class PlayerManager {
private LinkedList<Player> players;
private List<Player> players;
public LinkedList<Player> getPlayers() {
public List<Player> getPlayers() {
return players;
}
public void setPlayers(LinkedList<Player> players) {
public void setPlayers(List<Player> players) {
this.players = players;
}
public Player selectPlayer(String playerName) {
return new Player(0);
for (Player p : this.players) {
if (playerName.equals(p.getName())) {
return p;
}
}
return new Player(playerName, 0);
}
}

11
src/test/java/de/tims/player_management/PlayerManagerTest.java

@ -2,7 +2,7 @@ package de.tims.player_management;
import static org.assertj.core.api.Assertions.*;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
@ -15,14 +15,19 @@ class PlayerManagerTest {
@ParameterizedTest
@MethodSource("testCasesForSelectPlayer")
void selectPlayerTest(String testName, LinkedList<Player> players, String playerName, Player expectedResult) {
void selectPlayerTest(String testName, List<Player> players, String playerName, Player expectedResult) {
manager.setPlayers(players);
Player calculatedResult = manager.selectPlayer(playerName);
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);
}
private static Stream<Arguments> testCasesForSelectPlayer() {
return Stream.of(Arguments.of("NoPlayersYet", new LinkedList<Player>(), "Tobias", new Player(0)));
return Stream.of(Arguments.of("NoPlayersYetReturnNewPlayer", List.of(),
"Tobias", new Player("Tobias", 0)),
Arguments.of("NoPlayerWithNameInListReturnNewPlayer", List.of(new Player("Steffen", 40), new Player("Lorenz", 60)),
"Tobias", new Player("Tobias", 0)),
Arguments.of("PlayerWithNameInListReturnPlayerInList", List.of(new Player("Steffen", 40), new Player("Tobias", 50)),
"Tobias", new Player("Tobias", 50)));
}
}

4
src/test/java/de/tims/player_management/PlayerTest.java

@ -8,8 +8,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import de.tims.player_management.Player;
class PlayerTest {
Player player;
@ -17,7 +15,7 @@ class PlayerTest {
@ParameterizedTest
@MethodSource("testCasesForAddPoints")
void addPointsTest(String testName, int pointsBefore, int pointsToAdd, int expectedResult) {
player = new Player(pointsBefore);
player = new Player("TestPlayer", pointsBefore);
player.addPoints(pointsToAdd);
int calculatedResult = player.getPoints();
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);

Loading…
Cancel
Save