diff --git a/src/main/java/org/example/scoreboard/api/IMatch.java b/src/main/java/org/example/scoreboard/api/IMatch.java index a331401d28279b356b1babf7eb9f81cb601f8378..d7e3ef03210c54f10ae991bad701e6c63b61ad99 100644 --- a/src/main/java/org/example/scoreboard/api/IMatch.java +++ b/src/main/java/org/example/scoreboard/api/IMatch.java @@ -1,6 +1,5 @@ package org.example.scoreboard.api; -import java.util.Date; import java.util.UUID; public interface IMatch { @@ -13,5 +12,4 @@ public interface IMatch { ITeam getAwayTeam(); IScore getAwayTeamScore(); void setAwayTeamScore(IScore awayTeamScore); - Date getStartDate(); } diff --git a/src/main/java/org/example/scoreboard/generic/AbstractMatch.java b/src/main/java/org/example/scoreboard/generic/AbstractMatch.java index 8d8e9ed885d53e9f6a1e9740a1493a96494b018e..34c3d0c92db16c5479ae9e63150b84c3331706bf 100644 --- a/src/main/java/org/example/scoreboard/generic/AbstractMatch.java +++ b/src/main/java/org/example/scoreboard/generic/AbstractMatch.java @@ -4,7 +4,6 @@ import org.example.scoreboard.api.IMatch; import org.example.scoreboard.api.IScore; import org.example.scoreboard.api.ITeam; -import java.util.Date; import java.util.UUID; import java.util.function.Supplier; @@ -14,7 +13,6 @@ public abstract class AbstractMatch implements IMatch, Comparable { private final ITeam awayTeam; private IScore homeTeamScore; private IScore awayTeamScore; - private final Date startDate; protected AbstractMatch(Supplier scoreSupplier, ITeam homeTeam, ITeam awayTeam) { this.id = UUID.randomUUID(); @@ -22,7 +20,6 @@ public abstract class AbstractMatch implements IMatch, Comparable { this.awayTeam = awayTeam; this.homeTeamScore = scoreSupplier.get(); this.awayTeamScore = scoreSupplier.get(); - this.startDate = new Date(); } public ITeam getHomeTeam() { @@ -53,10 +50,6 @@ public abstract class AbstractMatch implements IMatch, Comparable { this.awayTeamScore = awayTeamScore; } - public Date getStartDate() { - return startDate; - } - @Override public String toString() { return getHomeTeam() + " " + getHomeTeamScore() + " - " + getAwayTeam() + " " + getAwayTeamScore(); @@ -70,16 +63,8 @@ public abstract class AbstractMatch implements IMatch, Comparable { return -1; } else if (totalScoreMatch1 < totalScoreMatch2) { return 1; - } else { - Date startMatch1 = getStartDate(); - Date startMatch2 = otherMatch.getStartDate(); - if (startMatch1.after(startMatch2)) { - return -1; - } else if(startMatch1.before(startMatch2)) { - return 1; - } } - return 0; + return -1; // default return -1 to reverse the linked hash map insertion-order iterator on score collision } } diff --git a/src/main/java/org/example/scoreboard/generic/AbstractScoreboard.java b/src/main/java/org/example/scoreboard/generic/AbstractScoreboard.java index d493d2a6a26b41230e28cc827bd45c3664ab66ca..0b40a64af4a9997c66084abc6aca6416bf0f76de 100644 --- a/src/main/java/org/example/scoreboard/generic/AbstractScoreboard.java +++ b/src/main/java/org/example/scoreboard/generic/AbstractScoreboard.java @@ -8,6 +8,7 @@ import org.example.scoreboard.api.ITeam; import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.NoSuchElementException; import java.util.UUID; import java.util.function.BiFunction; @@ -19,7 +20,7 @@ public class AbstractScoreboard implements IScoreboard { BiFunction matchSupplier; HashMap teamsOnTheBoard = new HashMap<>(); - HashMap ongoingMatches = new HashMap<>(); + LinkedHashMap ongoingMatches = new LinkedHashMap<>(); private AbstractScoreboard(){} public AbstractScoreboard(BiFunction matchSupplier) { diff --git a/src/test/java/org/example/scoreboard/reference/TestScoreboard.java b/src/test/java/org/example/scoreboard/reference/TestScoreboard.java index 7c7c1b7d2661200c6d03295570ed9b309980f684..76b09f61ab568968b66fd4c2df2f03ae32054b58 100644 --- a/src/test/java/org/example/scoreboard/reference/TestScoreboard.java +++ b/src/test/java/org/example/scoreboard/reference/TestScoreboard.java @@ -10,7 +10,6 @@ import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; -import static java.lang.Thread.sleep; import static org.junit.Assert.*; import static org.mockito.Mockito.*; @@ -123,24 +122,42 @@ public class TestScoreboard { } } - // should return ordered matches_whenSummaryIsRequested + // should present matches ordered from newest to oldest when summary is requested @Test - public void shouldReturnOrderedMatches_whenSummaryIsRequested() throws InterruptedException { + public void shouldPresentFromNewestToOldest_whenSummaryIsRequestedAndAllGamesWithTheSameScore() { + IMatch match1 = scoreboard.startMatch(Mexico, Canada); + match1 = scoreboard.updateScore(match1, new Score(5), new Score(5)); + IMatch match2 = scoreboard.startMatch(Spain, Brazil); + match2 = scoreboard.updateScore(match2, new Score(5), new Score(5)); + IMatch match3 = scoreboard.startMatch(Germany, France); + match3 = scoreboard.updateScore(match3, new Score(5), new Score(5)); + IMatch match4 = scoreboard.startMatch(Uruguay, Italy); + match4 = scoreboard.updateScore(match4, new Score(5), new Score(5)); + IMatch match5 = scoreboard.startMatch(Argentina, Australia); + match5 = scoreboard.updateScore(match5, new Score(5), new Score(5)); + + ArrayList expectedResult = Stream.of(match5, match4, match3, match2, match1).collect(Collectors.toCollection(ArrayList::new)); + ArrayList gameSummary = scoreboard.getGameSummary(); + + if (!expectedResult.equals(gameSummary)) { + gameSummary.forEach(System.out::println); + fail("failed to return expected ordered summary"); + } + } + + // should present matches ordered from highest combined score to lowest, newest to oldest on collision, when summary is requested + @Test + public void shouldOrderByScoreFromHighToLowedByNewToOldOnCollision_whenSummaryIsRequested() { IMatch match1 = scoreboard.startMatch(Mexico, Canada); match1 = scoreboard.updateScore(match1, new Score(0), new Score(5)); - sleep(1); IMatch match2 = scoreboard.startMatch(Spain, Brazil); match2 = scoreboard.updateScore(match2, new Score(10), new Score(2)); - sleep(1); IMatch match3 = scoreboard.startMatch(Germany, France); match3 = scoreboard.updateScore(match3, new Score(2), new Score(2)); - sleep(1); IMatch match4 = scoreboard.startMatch(Uruguay, Italy); match4 = scoreboard.updateScore(match4, new Score(6), new Score(6)); - sleep(1); IMatch match5 = scoreboard.startMatch(Argentina, Australia); match5 = scoreboard.updateScore(match5, new Score(3), new Score(1)); - sleep(1); ArrayList expectedResult = Stream.of(match4, match2, match1, match5, match3).collect(Collectors.toCollection(ArrayList::new)); ArrayList gameSummary = scoreboard.getGameSummary();