diff --git a/src/main/java/de/tims/gameexplorer/GameExplorer.java b/src/main/java/de/tims/gameexplorer/GameExplorer.java index 03c23dd..6f67e58 100644 --- a/src/main/java/de/tims/gameexplorer/GameExplorer.java +++ b/src/main/java/de/tims/gameexplorer/GameExplorer.java @@ -2,12 +2,17 @@ package de.tims.gameexplorer; import java.awt.*; 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; private JPanel explorerPanel; + private JPanel loginPanel; private JPanel gamePanel; private JPanel navigationPanel; private JPanel fleetstormPanel; @@ -20,31 +25,44 @@ 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 loginWarning; private JLabel chosenGame; + private JTextField usernameInput; private Dimension minSize; + private Dimension loginBtnSize; 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); + loginBtnSize = new Dimension(91, 20); btnSize = new Dimension(160, 40); gbc = new GridBagConstraints(); buildExplorerPanel(); + buildLoginPanel(); buildNavigationPanel(); buildGamePanels(); - frame.add(explorerPanel); + frame.add(loginPanel); frame.setMinimumSize(minSize); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -127,6 +145,39 @@ public class GameExplorer { explorerPanel.add(border5, gbc); } + private void buildLoginPanel() { + loginPanel = new JPanel(); + loginPanel.setLayout(new GridBagLayout()); + + 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; + 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, 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() { navigationPanel = new JPanel(); navigationPanel.setLayout(new GridBagLayout()); @@ -179,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) { 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 new file mode 100644 index 0000000..9bf11bd --- /dev/null +++ b/src/main/java/de/tims/player_management/Player.java @@ -0,0 +1,45 @@ +package de.tims.player_management; + +public class Player { + + private String name; + private int points; + + public Player(String name, int points) { + this.name = name; + this.points = points; + } + + 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) { + return true; + } + + if ((o == null) || this.getClass() != o.getClass()) { + return false; + } + + Player player2 = (Player) o; + + return this.points == player2.points && this.name.equals(player2.getName()); + } + + @Override + public int hashCode() { + 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 new file mode 100644 index 0000000..2e3b322 --- /dev/null +++ b/src/main/java/de/tims/player_management/PlayerManager.java @@ -0,0 +1,95 @@ +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; + +public class PlayerManager { + + private List players; + private static final int PLAYER_ATTRIBUTES = 2; + + public List getPlayers() { + return players; + } + + public void setPlayers(List players) { + this.players = players; + } + + public Player selectPlayer(String playerName) { + for (Player p : this.players) { + if (playerName.equals(p.getName())) { + return p; + } + } + + Player newPlayer = new Player(playerName, 0); + + players.add(newPlayer); + + return newPlayer; + } + + public void loadPlayers(String fileName) { + 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}; + + 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); + } + + 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) { + return; + } + } + + public void savePlayers(String fileName) { + 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/main/java/resources/player_data.csv b/src/main/java/resources/player_data.csv new file mode 100644 index 0000000..e69de29 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..d1c32f1 --- /dev/null +++ b/src/test/java/de/tims/player_management/PlayerManagerTest.java @@ -0,0 +1,110 @@ +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.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; + +class PlayerManagerTest { + + PlayerManager manager = new PlayerManager(); + + @ParameterizedTest + @MethodSource("testCasesForSelectPlayer") + 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("NoPlayersYetReturnNewPlayer", new LinkedList(List.of()), + "Tobias", new Player("Tobias", 0)), + Arguments.of("NoPlayerWithNameInListReturnNewPlayer", new LinkedList(List.of(new Player("Steffen", 40), new Player("Lorenz", 60))), + "Tobias", new Player("Tobias", 0)), + Arguments.of("PlayerWithNameInListReturnPlayerInList", new LinkedList(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/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)))); + } + + @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()), + 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); + } + +} diff --git a/src/test/java/de/tims/gameexplorer/PlayerTest.java b/src/test/java/de/tims/player_management/PlayerTest.java similarity index 82% rename from src/test/java/de/tims/gameexplorer/PlayerTest.java rename to src/test/java/de/tims/player_management/PlayerTest.java index 2d782d1..4822906 100644 --- a/src/test/java/de/tims/gameexplorer/PlayerTest.java +++ b/src/test/java/de/tims/player_management/PlayerTest.java @@ -1,4 +1,4 @@ -package de.tims.gameexplorer; +package de.tims.player_management; import static org.assertj.core.api.Assertions.*; @@ -14,8 +14,8 @@ class PlayerTest { @ParameterizedTest @MethodSource("testCasesForAddPoints") - void test(String testName, int pointsBefore, int pointsToAdd, int expectedResult) { - player = new Player(pointsBefore); + void addPointsTest(String testName, int pointsBefore, int pointsToAdd, int expectedResult) { + player = new Player("TestPlayer", pointsBefore); player.addPoints(pointsToAdd); int calculatedResult = player.getPoints(); assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); 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 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