From 631fb4c6986a07ebb5fb42feaeed25172bbea91c Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Wed, 12 Jan 2022 15:38:49 +0100 Subject: [PATCH] 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