From 9fb40995409229e4ab6424ec731fa5697eef8ca9 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Wed, 5 Jan 2022 18:34:34 +0100 Subject: [PATCH] player_management: added method selectPlayer, added method equals and hashCode in class Player --- .../de/tims/player_management/Player.java | 39 +++++++++++++++++++ .../tims/player_management/PlayerManager.java | 21 ++++++++++ .../player_management/PlayerManagerTest.java | 28 +++++++++++++ .../de/tims/player_management/PlayerTest.java | 34 ++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 src/main/java/de/tims/player_management/Player.java create mode 100644 src/main/java/de/tims/player_management/PlayerManager.java create mode 100644 src/test/java/de/tims/player_management/PlayerManagerTest.java create mode 100644 src/test/java/de/tims/player_management/PlayerTest.java diff --git a/src/main/java/de/tims/player_management/Player.java b/src/main/java/de/tims/player_management/Player.java new file mode 100644 index 0000000..86ddd34 --- /dev/null +++ b/src/main/java/de/tims/player_management/Player.java @@ -0,0 +1,39 @@ +package de.tims.player_management; + +public class Player { + + private int points; + + public Player(int points) { + this.points = points; + } + + public void addPoints(int pointsToAdd) { + this.points = (this.points + pointsToAdd > 0) ? this.points + pointsToAdd : 0; + } + + public int getPoints() { + return this.points; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + + if ((o == null) || this.getClass() != o.getClass()) { + return false; + } + + Player player2 = (Player) o; + + return this.points == player2.points; + } + + @Override + public int hashCode() { + return points; + } + +} diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java new file mode 100644 index 0000000..da39f70 --- /dev/null +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -0,0 +1,21 @@ +package de.tims.player_management; + +import java.util.LinkedList; + +public class PlayerManager { + + private LinkedList players; + + public LinkedList getPlayers() { + return players; + } + + public void setPlayers(LinkedList players) { + this.players = players; + } + + public Player selectPlayer(String playerName) { + return new Player(0); + } + +} diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java new file mode 100644 index 0000000..3b0b7c9 --- /dev/null +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -0,0 +1,28 @@ +package de.tims.player_management; + +import static org.assertj.core.api.Assertions.*; + +import java.util.LinkedList; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class PlayerManagerTest { + + PlayerManager manager = new PlayerManager(); + + @ParameterizedTest + @MethodSource("testCasesForSelectPlayer") + void selectPlayerTest(String testName, LinkedList 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))); + } + +} diff --git a/src/test/java/de/tims/player_management/PlayerTest.java b/src/test/java/de/tims/player_management/PlayerTest.java new file mode 100644 index 0000000..6d5301f --- /dev/null +++ b/src/test/java/de/tims/player_management/PlayerTest.java @@ -0,0 +1,34 @@ +package de.tims.player_management; + +import static org.assertj.core.api.Assertions.*; + +import java.util.stream.Stream; + +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; + + @ParameterizedTest + @MethodSource("testCasesForAddPoints") + void addPointsTest(String testName, int pointsBefore, int pointsToAdd, int expectedResult) { + player = new Player(pointsBefore); + player.addPoints(pointsToAdd); + int calculatedResult = player.getPoints(); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); + } + + private static Stream testCasesForAddPoints() { + return Stream.of(Arguments.of("NoPointsBeforeGet0Points", 0, 0, 0), + Arguments.of("NoPointsBeforeGet10Points", 0, 10, 10), + Arguments.of("10PointsBeforeAdd10Points", 10, 10, 20), + Arguments.of("10PointsBeforeLose10Points", 10, -10, 0), + Arguments.of("LoseMorePointsThanYouHave", 10, -20, 0)); + } + +}