From 21b07d077f9740ddde10c1dd7ef2fae72023c669 Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Mon, 17 Jan 2022 11:52:56 +0100 Subject: [PATCH] leaderboard: getTop10Players returns elements of sorted list --- src/main/java/de/tims/leaderboard/Leaderboard.java | 12 ++++++++++-- .../java/de/tims/leaderboard/LeaderboardTest.java | 8 +++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/leaderboard/Leaderboard.java b/src/main/java/de/tims/leaderboard/Leaderboard.java index 6f78e09..98c16d8 100644 --- a/src/main/java/de/tims/leaderboard/Leaderboard.java +++ b/src/main/java/de/tims/leaderboard/Leaderboard.java @@ -6,12 +6,20 @@ import de.tims.player_management.Player; public class Leaderboard { + private List allPlayers; + public Leaderboard(List allPlayers, String actualPlayer) { - // TODO Auto-generated constructor stub + this.allPlayers = allPlayers; } public Player[] getTop10Players() { - return new Player[] {null, null, null, null, null, null, null, null, null, null}; + Player[] top10Players = new Player[10]; + + for (int i = 0; i < allPlayers.size(); i++) { + top10Players[i] = allPlayers.get(i); + } + + return top10Players; } } diff --git a/src/test/java/de/tims/leaderboard/LeaderboardTest.java b/src/test/java/de/tims/leaderboard/LeaderboardTest.java index 5e0fabc..8e2b606 100644 --- a/src/test/java/de/tims/leaderboard/LeaderboardTest.java +++ b/src/test/java/de/tims/leaderboard/LeaderboardTest.java @@ -25,7 +25,13 @@ class LeaderboardTest { } private static Stream testCasesForGetTop10Players() { - return Stream.of(Arguments.of("EmptyListReturnsEmptyArray", List.of(), "TestPlayer", new Player[] {null, null, null, null, null, null, null, null, null, null})); + return Stream.of(Arguments.of("EmptyListReturnsEmptyArray", List.of(), "TestPlayer", new Player[] {null, null, null, null, null, null, null, null, null, null}), + Arguments.of("SortedListWithLessThan10ElementsReturnsListAsArray", + List.of(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)), + "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})); } }