From 1e724d8e80d0de6d7ac6909d63720343d3309fdf Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Mon, 17 Jan 2022 12:02:31 +0100 Subject: [PATCH] leaderboard: getTop10Players returns first 10 Elements of sorted list --- src/main/java/de/tims/leaderboard/Leaderboard.java | 6 ++++-- .../java/de/tims/leaderboard/LeaderboardTest.java | 11 ++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/leaderboard/Leaderboard.java b/src/main/java/de/tims/leaderboard/Leaderboard.java index 98c16d8..fd4d8f7 100644 --- a/src/main/java/de/tims/leaderboard/Leaderboard.java +++ b/src/main/java/de/tims/leaderboard/Leaderboard.java @@ -6,6 +6,8 @@ import de.tims.player_management.Player; public class Leaderboard { + private static final int NUM_PLAYERS_ON_LEADERBOARD = 10; + private List allPlayers; public Leaderboard(List allPlayers, String actualPlayer) { @@ -13,9 +15,9 @@ public class Leaderboard { } public Player[] getTop10Players() { - Player[] top10Players = new Player[10]; + Player[] top10Players = new Player[NUM_PLAYERS_ON_LEADERBOARD]; - for (int i = 0; i < allPlayers.size(); i++) { + for (int i = 0; i < allPlayers.size() && i < NUM_PLAYERS_ON_LEADERBOARD; i++) { top10Players[i] = allPlayers.get(i); } diff --git a/src/test/java/de/tims/leaderboard/LeaderboardTest.java b/src/test/java/de/tims/leaderboard/LeaderboardTest.java index 8e2b606..899fa90 100644 --- a/src/test/java/de/tims/leaderboard/LeaderboardTest.java +++ b/src/test/java/de/tims/leaderboard/LeaderboardTest.java @@ -31,7 +31,16 @@ class LeaderboardTest { new Player("Fuenf", 40), new Player("Sechs", 30), new Player("Sieben", 20), new Player("Acht", 10)), "TestPlayer", new Player[] {new Player("Eins", 80), new Player("Zwei", 70), new Player("Drei", 60), new Player("Vier", 50), - new Player("Fuenf", 40), new Player("Sechs", 30), new Player("Sieben", 20), new Player("Acht", 10), null, null})); + new Player("Fuenf", 40), new Player("Sechs", 30), new Player("Sieben", 20), new Player("Acht", 10), null, null}), + Arguments.of("SortedListWithMoreThan10ElementsReturnsFirst10ElementsAsArray", + List.of(new Player("Eins", 140), new Player("Zwei", 130), new Player("Drei", 120), new Player("Vier", 110), + new Player("Fuenf", 100), new Player("Sechs", 90), new Player("Sieben", 80), new Player("Acht", 70), + new Player("Neun", 60), new Player("Zehn", 50), new Player("Elf", 40), new Player("Zwoelf", 30), + new Player("Dreizehn", 20), new Player("Vierzehn", 10)), + "TestPlayer", + new Player[] {new Player("Eins", 140), new Player("Zwei", 130), new Player("Drei", 120), new Player("Vier", 110), + new Player("Fuenf", 100), new Player("Sechs", 90), new Player("Sieben", 80), new Player("Acht", 70), + new Player("Neun", 60), new Player("Zehn", 50)})); } }