From 4251b7b59282550ef885858266c3620a76246723 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Wed, 5 Jan 2022 19:11:31 +0100 Subject: [PATCH] player_management: returns player from list if existing --- .../java/de/tims/player_management/Player.java | 14 ++++++++++---- .../de/tims/player_management/PlayerManager.java | 16 +++++++++++----- .../player_management/PlayerManagerTest.java | 11 ++++++++--- .../de/tims/player_management/PlayerTest.java | 4 +--- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/tims/player_management/Player.java b/src/main/java/de/tims/player_management/Player.java index 86ddd34..d5c3b22 100644 --- a/src/main/java/de/tims/player_management/Player.java +++ b/src/main/java/de/tims/player_management/Player.java @@ -2,19 +2,25 @@ 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) { diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index da39f70..fbc5746 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/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 players; + private List players; - public LinkedList getPlayers() { + public List getPlayers() { return players; } - public void setPlayers(LinkedList players) { + public void setPlayers(List 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); } } diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index 3b0b7c9..11db5ab 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/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 players, String playerName, Player expectedResult) { + void selectPlayerTest(String testName, List players, String playerName, Player expectedResult) { manager.setPlayers(players); Player calculatedResult = manager.selectPlayer(playerName); assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); } private static Stream testCasesForSelectPlayer() { - return Stream.of(Arguments.of("NoPlayersYet", new LinkedList(), "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))); } } diff --git a/src/test/java/de/tims/player_management/PlayerTest.java b/src/test/java/de/tims/player_management/PlayerTest.java index 6d5301f..4822906 100644 --- a/src/test/java/de/tims/player_management/PlayerTest.java +++ b/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);