forked from MetroCS/ConsoleGameHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameHistoryTracker.java
More file actions
124 lines (117 loc) · 4.07 KB
/
GameHistoryTracker.java
File metadata and controls
124 lines (117 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.IOException;
import java.io.Serializable;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Track history and stats of games played.
* @author Jody Paul (assisted by chatGPT)
* @author Cesar Soto, Mason Proctor, Luke Ross
* @version 2
*/
class GameHistoryTracker implements Serializable {
private static final long serialVersionUID = 2L;
/** Collection of play stats for each game. */
private final HashMap<String, GameStats> statsMap = new HashMap<>();
/**
* Records a play session for a game.
* @param gameName the name of the game played
* @param score optional numeric score (nullable)
*/
public void recordPlay(final String gameName, final Integer score) {
GameStats stats = statsMap.getOrDefault(gameName, new GameStats());
stats.incrementTimesPlayed();
if (score != null) {
stats.totalScore += score;
stats.scores.add(score);
}
statsMap.put(gameName, stats);
}
/**
* Displays a summary of play history and scores.
*/
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();
}
}
/**
* Saves the game history to a file.
* @param filename the name of the file to save to
* @throws IOException if an I/O error occurs
*/
public void saveHistory(final String filename) throws IOException {
try (ObjectOutputStream out =
new ObjectOutputStream(new FileOutputStream(filename))) {
out.writeObject(this);
}
}
/**
* Clears the history file.
* @param filename takes name of file to clear history from.
*/
public void clearHistory(final String filename) {
statsMap.clear();
try {
saveHistory(filename);
} catch (IOException e) {
System.out.println("Game history save failed: " + e.getMessage());
}
}
/**
* Loads the game history from a file.
* @param filename the name of the file to load from
* @return useful game history tracker
*/
public static GameHistoryTracker loadHistory(final String filename) {
try (ObjectInputStream in
= new ObjectInputStream(new FileInputStream(filename))) {
return (GameHistoryTracker) in.readObject();
} catch (IOException | ClassNotFoundException e) {
System.err.println(
"No previous history found or failed to load. Starting fresh.");
return new GameHistoryTracker();
}
}
/**
* Inner class to track stats for a single game.
*/
private static class GameStats implements Serializable {
private static final long serialVersionUID = 2L;
/** The number of times game has been played. */
private int timesPlayed = 0;
/** The current total score. */
private int totalScore = 0;
/** All recorded scores. */
private ArrayList<Integer> scores = new ArrayList<>();
/**
* Access the number of times the game has been played.
* @return times played
*/
int getTimesPlayed() {
return this.timesPlayed;
}
/**
* Increment the number of times the game has been played.
*/
void incrementTimesPlayed() {
this.timesPlayed++;
}
}
}