Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions GameHistoryTracker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.IOException;
Expand Down Expand Up @@ -35,24 +34,37 @@ public void recordPlay(final String gameName, final Integer score) {
}

/**
* Displays a summary of play history and scores.
* Displays a summary of play history sorted in descending order
* along with corresponding scores, if any.
*/
public void displayHistory() {
System.out.println("\n=== Game Play History ===");
if (statsMap.isEmpty()) {
System.out.println("No games played yet.");
return;
}
for (Map.Entry<String, GameStats> entry : statsMap.entrySet()) {
String game = entry.getKey();
GameStats stats = entry.getValue();
System.out.printf("%s - Played: %d", game, stats.timesPlayed);
if (!stats.scores.isEmpty()) {
double avg = stats.totalScore / (double) stats.scores.size();
System.out.printf(", Avg Score: %.2f", avg);
}
System.out.println();
}
statsMap.entrySet().stream()
.sorted((a, b) -> {
int cmp = Integer.compare(
b.getValue().getTimesPlayed(),
a.getValue().getTimesPlayed()
);
return (cmp == 0)
? a.getKey().compareToIgnoreCase(b.getKey())
: cmp;
})
.forEach(entry -> {
String game = entry.getKey();
GameStats stats = entry.getValue();
System.out.printf("%s - Played: %d",
game, stats.getTimesPlayed());
if (!stats.scores.isEmpty()) {
double avg =
stats.totalScore / (double) stats.scores.size();
System.out.printf(", Avg Score: %.2f", avg);
}
System.out.println();
});
}

/**
Expand Down