From 7b2494067bc4edc07532495d6f7121568dd7f65f Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Wed, 5 Jan 2022 18:34:34 +0100 Subject: [PATCH 01/11] 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)); + } + +} From d9e499352cba4f078f7c69b0feab129a42291781 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Wed, 5 Jan 2022 19:11:31 +0100 Subject: [PATCH 02/11] 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); From 227efc379570fd790fab8f9e413ed4bc0e67cf43 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Wed, 5 Jan 2022 19:55:27 +0100 Subject: [PATCH 03/11] player_management: added method loadPlayers --- .../de/tims/player_management/Player.java | 2 +- .../tims/player_management/PlayerManager.java | 4 +++ .../player_management/PlayerManagerTest.java | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/player_management/Player.java b/src/main/java/de/tims/player_management/Player.java index d5c3b22..50f86b7 100644 --- a/src/main/java/de/tims/player_management/Player.java +++ b/src/main/java/de/tims/player_management/Player.java @@ -39,7 +39,7 @@ public class Player { @Override public int hashCode() { - return points; + return points + name.hashCode(); } } diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index fbc5746..b21fe73 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -24,4 +24,8 @@ public class PlayerManager { return new Player(playerName, 0); } + public void loadPlayers(String fileName) { + players = List.of(); + } + } diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index 11db5ab..5def1ec 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -2,6 +2,9 @@ package de.tims.player_management; import static org.assertj.core.api.Assertions.*; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.List; import java.util.stream.Stream; @@ -29,5 +32,27 @@ class PlayerManagerTest { Arguments.of("PlayerWithNameInListReturnPlayerInList", List.of(new Player("Steffen", 40), new Player("Tobias", 50)), "Tobias", new Player("Tobias", 50))); } + + @ParameterizedTest + @MethodSource("testCasesForLoadPlayers") + void loadPlayersTest(String testName, String fileContent, String fileName, List expectedResult) { + File playerData = new File(fileName); + + try { + FileWriter fw = new FileWriter(playerData, false); + fw.write(fileContent); + fw.close(); + } catch (IOException e) { + fail("Cannot open file"); + } + + manager.loadPlayers(fileName); + List calculatedResult = manager.getPlayers(); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); + } + + private static Stream testCasesForLoadPlayers() { + return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/main/java/resources/player_data.csv", List.of())); + } } From 738a632f69b5c0d48d638c44228abc5faf16fcf8 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Tue, 11 Jan 2022 19:27:38 +0100 Subject: [PATCH 04/11] player_management: loadPlayers reads player from csv-file --- .../java/de/tims/gameexplorer/Player.java | 19 -------- .../de/tims/player_management/Player.java | 2 +- .../tims/player_management/PlayerManager.java | 45 ++++++++++++++++++- src/main/java/resources/player_data.csv | 1 + .../java/de/tims/gameexplorer/PlayerTest.java | 32 ------------- .../player_management/PlayerManagerTest.java | 3 +- 6 files changed, 48 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/de/tims/gameexplorer/Player.java create mode 100644 src/main/java/resources/player_data.csv delete mode 100644 src/test/java/de/tims/gameexplorer/PlayerTest.java diff --git a/src/main/java/de/tims/gameexplorer/Player.java b/src/main/java/de/tims/gameexplorer/Player.java deleted file mode 100644 index 0200dc8..0000000 --- a/src/main/java/de/tims/gameexplorer/Player.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.tims.gameexplorer; - -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; - } - -} diff --git a/src/main/java/de/tims/player_management/Player.java b/src/main/java/de/tims/player_management/Player.java index 50f86b7..9bf11bd 100644 --- a/src/main/java/de/tims/player_management/Player.java +++ b/src/main/java/de/tims/player_management/Player.java @@ -34,7 +34,7 @@ public class Player { Player player2 = (Player) o; - return this.points == player2.points; + return this.points == player2.points && this.name.equals(player2.getName()); } @Override diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index b21fe73..84a54b9 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -1,10 +1,15 @@ package de.tims.player_management; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.LinkedList; import java.util.List; public class PlayerManager { private List players; + private static final int PLAYER_ATTRIBUTES = 2; public List getPlayers() { return players; @@ -25,7 +30,45 @@ public class PlayerManager { } public void loadPlayers(String fileName) { - players = List.of(); + players = new LinkedList(); + + File playerData = new File(fileName); + + try { + FileReader fr = new FileReader(playerData); + + int c = -1; + String playerName; + int playerPoints; + StringBuilder nameBuilder = new StringBuilder(); + StringBuilder pointBuilder = new StringBuilder(); + StringBuilder[] sb = {nameBuilder, pointBuilder}; + + for (int i = 0; i < PLAYER_ATTRIBUTES; i++) { + do { + c = fr.read(); + if (c == ';' || c == '\n' || c == -1) { + break; + } + + sb[i].append((char) c); + } while (c != ';' && c != '\n' && c != -1); + } + + if (!(nameBuilder.toString().equals("") || pointBuilder.toString().equals(""))) { + playerName = nameBuilder.toString(); + playerPoints = Integer.parseInt(pointBuilder.toString()); + + players.add(new Player(playerName, playerPoints)); + + nameBuilder.delete(0, nameBuilder.length()); + pointBuilder.delete(0, pointBuilder.length()); + } + + fr.close(); + } catch (IOException e) { + return; + } } } diff --git a/src/main/java/resources/player_data.csv b/src/main/java/resources/player_data.csv new file mode 100644 index 0000000..8decaa8 --- /dev/null +++ b/src/main/java/resources/player_data.csv @@ -0,0 +1 @@ +Tobias;50 \ No newline at end of file diff --git a/src/test/java/de/tims/gameexplorer/PlayerTest.java b/src/test/java/de/tims/gameexplorer/PlayerTest.java deleted file mode 100644 index 2d782d1..0000000 --- a/src/test/java/de/tims/gameexplorer/PlayerTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package de.tims.gameexplorer; - -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; - -class PlayerTest { - - Player player; - - @ParameterizedTest - @MethodSource("testCasesForAddPoints") - void test(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)); - } - -} diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index 5def1ec..2b9d2a5 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -52,7 +52,8 @@ class PlayerManagerTest { } private static Stream testCasesForLoadPlayers() { - return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/main/java/resources/player_data.csv", List.of())); + return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/main/java/resources/player_data.csv", List.of()), + Arguments.of("OnePlayerInFileReturnsListWithOneElement", "Tobias;50", "src/main/java/resources/player_data.csv", List.of(new Player("Tobias", 50)))); } } From f2276c42339149b6186a4865a0083a50fcda0634 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Tue, 11 Jan 2022 19:41:03 +0100 Subject: [PATCH 05/11] player_management: loadPlayers reads list of players from csv-file --- .../tims/player_management/PlayerManager.java | 38 +++++++++---------- src/main/java/resources/player_data.csv | 1 - .../player_management/PlayerManagerTest.java | 6 ++- src/test/java/resources/player_testdata.csv | 3 ++ 4 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 src/test/java/resources/player_testdata.csv diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index 84a54b9..ce6ae39 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -44,26 +44,26 @@ public class PlayerManager { StringBuilder pointBuilder = new StringBuilder(); StringBuilder[] sb = {nameBuilder, pointBuilder}; - for (int i = 0; i < PLAYER_ATTRIBUTES; i++) { - do { - c = fr.read(); - if (c == ';' || c == '\n' || c == -1) { - break; - } - - sb[i].append((char) c); - } while (c != ';' && c != '\n' && c != -1); - } - - if (!(nameBuilder.toString().equals("") || pointBuilder.toString().equals(""))) { - playerName = nameBuilder.toString(); - playerPoints = Integer.parseInt(pointBuilder.toString()); + do { + for (int i = 0; i < PLAYER_ATTRIBUTES; i++) { + do { + c = fr.read(); + if (c != ';' && c != '\n' && c != -1) { + sb[i].append((char) c); + } + } while (c != ';' && c != '\n' && c != -1); + } - players.add(new Player(playerName, playerPoints)); - - nameBuilder.delete(0, nameBuilder.length()); - pointBuilder.delete(0, pointBuilder.length()); - } + if (!nameBuilder.toString().equals("") && !pointBuilder.toString().equals("")) { + playerName = nameBuilder.toString(); + playerPoints = Integer.parseInt(pointBuilder.toString()); + + players.add(new Player(playerName, playerPoints)); + + nameBuilder.delete(0, nameBuilder.length()); + pointBuilder.delete(0, pointBuilder.length()); + } + } while (c != -1); fr.close(); } catch (IOException e) { diff --git a/src/main/java/resources/player_data.csv b/src/main/java/resources/player_data.csv index 8decaa8..e69de29 100644 --- a/src/main/java/resources/player_data.csv +++ b/src/main/java/resources/player_data.csv @@ -1 +0,0 @@ -Tobias;50 \ No newline at end of file diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index 2b9d2a5..ec7a5a9 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -52,8 +52,10 @@ class PlayerManagerTest { } private static Stream testCasesForLoadPlayers() { - return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/main/java/resources/player_data.csv", List.of()), - Arguments.of("OnePlayerInFileReturnsListWithOneElement", "Tobias;50", "src/main/java/resources/player_data.csv", List.of(new Player("Tobias", 50)))); + return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/test/java/resources/player_testdata.csv", List.of()), + Arguments.of("OnePlayerInFileReturnsListWithOneElement", "Tobias;50", "src/test/java/resources/player_testdata.csv", List.of(new Player("Tobias", 50))), + Arguments.of("MorePlayersInFileReturnLongerList", "Tobias;50\nLorenz;40\nSteffen;60", "src/test/java/resources/player_testdata.csv", + List.of(new Player("Tobias", 50), new Player("Lorenz", 40), new Player("Steffen", 60)))); } } diff --git a/src/test/java/resources/player_testdata.csv b/src/test/java/resources/player_testdata.csv new file mode 100644 index 0000000..5396826 --- /dev/null +++ b/src/test/java/resources/player_testdata.csv @@ -0,0 +1,3 @@ +Tobias;50 +Lorenz;40 +Steffen;60 \ No newline at end of file From 506d42e0866133ef2bb79a5062d1e7095b38d824 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Tue, 11 Jan 2022 20:03:16 +0100 Subject: [PATCH 06/11] player_management: added method savePlayers --- .../de/tims/player_management/PlayerManager.java | 5 +++++ .../tims/player_management/PlayerManagerTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index ce6ae39..fcf1443 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -71,4 +71,9 @@ public class PlayerManager { } } + public void savePlayers(String fileName) { + // TODO Auto-generated method stub + + } + } diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index ec7a5a9..ae7c9b0 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -57,5 +57,19 @@ class PlayerManagerTest { Arguments.of("MorePlayersInFileReturnLongerList", "Tobias;50\nLorenz;40\nSteffen;60", "src/test/java/resources/player_testdata.csv", List.of(new Player("Tobias", 50), new Player("Lorenz", 40), new Player("Steffen", 60)))); } + + @ParameterizedTest + @MethodSource("testCasesForSavePlayers") + void savePlayersTest(String testName, String fileName, List expectedResult) { + manager.setPlayers(expectedResult); + manager.savePlayers(fileName); + manager.loadPlayers(fileName); + List calculatedResult = manager.getPlayers(); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); + } + + private static Stream testCasesForSavePlayers() { + return Stream.of(Arguments.of("EmptyListIsWrittenAsEmptyFile", "src/test/java/resources/player_testdata2.csv", List.of())); + } } From 32c8acbd653c13abbabb8cade66097bdc1c77f5b Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Wed, 12 Jan 2022 15:38:49 +0100 Subject: [PATCH 07/11] player_management: savePlayers writes player list to file --- .../de/tims/player_management/PlayerManager.java | 14 +++++++++++++- .../tims/player_management/PlayerManagerTest.java | 7 +++++-- src/test/java/resources/player_testdata2.csv | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/test/java/resources/player_testdata2.csv diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index fcf1443..8ab6b68 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -2,6 +2,7 @@ package de.tims.player_management; import java.io.File; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.util.LinkedList; import java.util.List; @@ -72,8 +73,19 @@ public class PlayerManager { } public void savePlayers(String fileName) { - // TODO Auto-generated method stub + File playerData = new File(fileName); + try { + FileWriter fw = new FileWriter(playerData, false); + + for (Player elem : players) { + fw.write(elem.getName() + ";" + elem.getPoints() + "\n"); + } + + fw.close(); + } catch (IOException e) { + return; + } } } diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index ae7c9b0..b3c9dcd 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -53,7 +53,8 @@ class PlayerManagerTest { private static Stream testCasesForLoadPlayers() { return Stream.of(Arguments.of("EmptyFileReturnsEmtpyList", "", "src/test/java/resources/player_testdata.csv", List.of()), - Arguments.of("OnePlayerInFileReturnsListWithOneElement", "Tobias;50", "src/test/java/resources/player_testdata.csv", List.of(new Player("Tobias", 50))), + Arguments.of("OnePlayerInFileReturnsListWithOneElement", "Tobias;50", "src/test/java/resources/player_testdata.csv", + List.of(new Player("Tobias", 50))), Arguments.of("MorePlayersInFileReturnLongerList", "Tobias;50\nLorenz;40\nSteffen;60", "src/test/java/resources/player_testdata.csv", List.of(new Player("Tobias", 50), new Player("Lorenz", 40), new Player("Steffen", 60)))); } @@ -69,7 +70,9 @@ class PlayerManagerTest { } private static Stream testCasesForSavePlayers() { - return Stream.of(Arguments.of("EmptyListIsWrittenAsEmptyFile", "src/test/java/resources/player_testdata2.csv", List.of())); + return Stream.of(Arguments.of("EmptyListIsWrittenAsEmptyFile", "src/test/java/resources/player_testdata2.csv", List.of()), + Arguments.of("WriteElementsOfListToFile", "src/test/java/resources/player_testdata2.csv", + List.of(new Player("Tobias", 50), new Player("Steffen", 60)))); } } diff --git a/src/test/java/resources/player_testdata2.csv b/src/test/java/resources/player_testdata2.csv new file mode 100644 index 0000000..fd135b4 --- /dev/null +++ b/src/test/java/resources/player_testdata2.csv @@ -0,0 +1,2 @@ +Tobias;50 +Steffen;60 From 868feaa049e5664687297fcd39a833ca7ec85bd1 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 15 Jan 2022 19:23:37 +0100 Subject: [PATCH 08/11] player_management: selectPlayer adds new players to player-list --- .../tims/player_management/PlayerManager.java | 6 ++- .../player_management/PlayerManagerTest.java | 38 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/player_management/PlayerManager.java b/src/main/java/de/tims/player_management/PlayerManager.java index 8ab6b68..2e3b322 100644 --- a/src/main/java/de/tims/player_management/PlayerManager.java +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -27,7 +27,11 @@ public class PlayerManager { } } - return new Player(playerName, 0); + Player newPlayer = new Player(playerName, 0); + + players.add(newPlayer); + + return newPlayer; } public void loadPlayers(String fileName) { diff --git a/src/test/java/de/tims/player_management/PlayerManagerTest.java b/src/test/java/de/tims/player_management/PlayerManagerTest.java index b3c9dcd..d1c32f1 100644 --- a/src/test/java/de/tims/player_management/PlayerManagerTest.java +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -5,9 +5,11 @@ import static org.assertj.core.api.Assertions.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.util.LinkedList; import java.util.List; import java.util.stream.Stream; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -25,11 +27,11 @@ class PlayerManagerTest { } private static Stream testCasesForSelectPlayer() { - return Stream.of(Arguments.of("NoPlayersYetReturnNewPlayer", List.of(), + return Stream.of(Arguments.of("NoPlayersYetReturnNewPlayer", new LinkedList(List.of()), "Tobias", new Player("Tobias", 0)), - Arguments.of("NoPlayerWithNameInListReturnNewPlayer", List.of(new Player("Steffen", 40), new Player("Lorenz", 60)), + Arguments.of("NoPlayerWithNameInListReturnNewPlayer", new LinkedList(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)), + Arguments.of("PlayerWithNameInListReturnPlayerInList", new LinkedList(List.of(new Player("Steffen", 40), new Player("Tobias", 50))), "Tobias", new Player("Tobias", 50))); } @@ -74,5 +76,35 @@ class PlayerManagerTest { Arguments.of("WriteElementsOfListToFile", "src/test/java/resources/player_testdata2.csv", List.of(new Player("Tobias", 50), new Player("Steffen", 60)))); } + + @Test + void selectPlayerAddsNewPlayersToList() { + LinkedList players = new LinkedList(); + players.add(new Player("Steffen", 40)); + players.add(new Player("Max", 60)); + + manager.setPlayers(players); + manager.selectPlayer("Tobias"); + + List expectedResult = List.of(new Player("Steffen", 40), new Player("Max", 60), new Player("Tobias", 0)); + List calculatedResult = manager.getPlayers(); + + assertThat(calculatedResult).describedAs("SelectNewPlayerAddsNewPlayerToPlayersList").isEqualTo(expectedResult); + } + + @Test + void selectExistingPlayerDoesntChangeTheList() { + LinkedList players = new LinkedList(); + players.add(new Player("Steffen", 40)); + players.add(new Player("Max", 60)); + + manager.setPlayers(players); + manager.selectPlayer("Max"); + + List expectedResult = List.of(new Player("Steffen", 40), new Player("Max", 60)); + List calculatedResult = manager.getPlayers(); + + assertThat(calculatedResult).describedAs("SelectNewPlayerAddsNewPlayerToPlayersList").isEqualTo(expectedResult); + } } From f17ed64f52dda32dfa50bb7cb1927a418c1ff49d Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 15 Jan 2022 19:43:04 +0100 Subject: [PATCH 09/11] player_management: added LoginPanel to gameexplorer --- .../de/tims/gameexplorer/GameExplorer.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index 03c23dd..8630841 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -8,6 +8,7 @@ public class GameExplorer { private JFrame frame; private JPanel explorerPanel; + private JPanel loginPanel; private JPanel gamePanel; private JPanel navigationPanel; private JPanel fleetstormPanel; @@ -20,12 +21,15 @@ public class GameExplorer { private JPanel border4; private JPanel border5; private JPanel border6; + private JButton loginBtn; private JButton fleetstormBtn; private JButton fourwinsBtn; private JButton tictactoeBtn; private JButton leaderboardBtn; private JButton backBtn; + private JLabel username; private JLabel chosenGame; + private JTextField usernameInput; private Dimension minSize; private Dimension btnSize; private GridBagConstraints gbc; @@ -41,10 +45,11 @@ public class GameExplorer { gbc = new GridBagConstraints(); buildExplorerPanel(); + buildLoginPanel(); buildNavigationPanel(); buildGamePanels(); - frame.add(explorerPanel); + frame.add(loginPanel); frame.setMinimumSize(minSize); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -127,6 +132,18 @@ public class GameExplorer { explorerPanel.add(border5, gbc); } + private void buildLoginPanel() { + loginPanel = new JPanel(); + + loginBtn = new JButton("Login"); + username = new JLabel("Name eingeben:"); + usernameInput = new JTextField(10); + + loginPanel.add(username); + loginPanel.add(usernameInput); + loginPanel.add(loginBtn); + } + private void buildNavigationPanel() { navigationPanel = new JPanel(); navigationPanel.setLayout(new GridBagLayout()); From dc1607b32bae63e9f80da718ff2143f3d204f679 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 15 Jan 2022 19:51:24 +0100 Subject: [PATCH 10/11] player_management: added layout for LoginPanel --- .../de/tims/gameexplorer/GameExplorer.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index 8630841..ab88fa0 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -2,6 +2,7 @@ package de.tims.gameexplorer; import java.awt.*; import java.awt.event.*; + import javax.swing.*; public class GameExplorer { @@ -31,6 +32,7 @@ public class GameExplorer { private JLabel chosenGame; private JTextField usernameInput; private Dimension minSize; + private Dimension loginBtnSize; private Dimension btnSize; private GridBagConstraints gbc; @@ -41,6 +43,7 @@ public class GameExplorer { frame = new JFrame("1000 infomagische Spiele"); minSize = new Dimension(400, 300); + loginBtnSize = new Dimension(91, 20); btnSize = new Dimension(160, 40); gbc = new GridBagConstraints(); @@ -134,14 +137,28 @@ public class GameExplorer { private void buildLoginPanel() { loginPanel = new JPanel(); + loginPanel.setLayout(new GridBagLayout()); loginBtn = new JButton("Login"); + loginBtn.setPreferredSize(loginBtnSize); username = new JLabel("Name eingeben:"); - usernameInput = new JTextField(10); + usernameInput = new JTextField(8); - loginPanel.add(username); - loginPanel.add(usernameInput); - loginPanel.add(loginBtn); + gbc.weighty = 0; + gbc.gridx = 0; + gbc.gridy = 0; + gbc.insets = new Insets(0, 0, 5, 0); + loginPanel.add(username, gbc); + + gbc.gridx = 0; + gbc.gridy = 1; + gbc.insets = new Insets(5, 0, 5, 0); + loginPanel.add(usernameInput, gbc); + + gbc.gridx = 0; + gbc.gridy = 2; + gbc.insets = new Insets(5, 0, 0, 0); + loginPanel.add(loginBtn, gbc); } private void buildNavigationPanel() { From fb18c0bf482fb197e95da1cad18dcfa9e4ce91f4 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 15 Jan 2022 20:07:09 +0100 Subject: [PATCH 11/11] player_management: login loads existing player from file or creates new player --- .../de/tims/gameexplorer/GameExplorer.java | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index ab88fa0..6f67e58 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -5,6 +5,9 @@ import java.awt.event.*; import javax.swing.*; +import de.tims.player_management.Player; +import de.tims.player_management.PlayerManager; + public class GameExplorer { private JFrame frame; @@ -29,6 +32,7 @@ public class GameExplorer { private JButton leaderboardBtn; private JButton backBtn; private JLabel username; + private JLabel loginWarning; private JLabel chosenGame; private JTextField usernameInput; private Dimension minSize; @@ -36,10 +40,16 @@ public class GameExplorer { private Dimension btnSize; private GridBagConstraints gbc; + private static final String playerFile = "src/main/java/resources/player_data.csv"; + private enum Game { FLEETSTORM, FOURWINS, TICTACTOE, LEADERBOARD }; private Game actualGame; + private PlayerManager manager; + private Player actualPlayer; public GameExplorer() { + manager = new PlayerManager(); + frame = new JFrame("1000 infomagische Spiele"); minSize = new Dimension(400, 300); @@ -141,7 +151,9 @@ public class GameExplorer { loginBtn = new JButton("Login"); loginBtn.setPreferredSize(loginBtnSize); + loginBtn.addActionListener(new LoginAction()); username = new JLabel("Name eingeben:"); + loginWarning = new JLabel(); usernameInput = new JTextField(8); gbc.weighty = 0; @@ -157,8 +169,13 @@ public class GameExplorer { gbc.gridx = 0; gbc.gridy = 2; - gbc.insets = new Insets(5, 0, 0, 0); + gbc.insets = new Insets(5, 0, 5, 0); loginPanel.add(loginBtn, gbc); + + gbc.gridx = 0; + gbc.gridy = 3; + gbc.insets = new Insets(5, 0, 0, 0); + loginPanel.add(loginWarning, gbc); } private void buildNavigationPanel() { @@ -213,6 +230,30 @@ public class GameExplorer { leaderboardPanel.setBackground(Color.RED); } + private class LoginAction implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + String userInput = usernameInput.getText(); + + if (!userInput.equals("")) { + loginWarning.setText(""); + + manager.loadPlayers(playerFile); + actualPlayer = manager.selectPlayer(userInput); + + frame.remove(loginPanel); + frame.add(explorerPanel); + frame.revalidate(); + frame.repaint(); + + System.out.println("Actual Player: " + actualPlayer.getName() + ", Points: " + actualPlayer.getPoints()); + } else { + loginWarning.setText("Kein Name eingegeben!"); + } + + } + } + private class GameAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) {