forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScorecardService.java
52 lines (43 loc) · 1.6 KB
/
ScorecardService.java
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
package cricinfo;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
public class ScorecardService {
private static ScorecardService instance;
private final Map<String, Scorecard> scorecards;
private final AtomicInteger scorecardIdCounter;
private ScorecardService() {
scorecards = new ConcurrentHashMap<>();
scorecardIdCounter = new AtomicInteger(0);
}
public static synchronized ScorecardService getInstance() {
if (instance == null) {
instance = new ScorecardService();
}
return instance;
}
public void createScorecard(Match match) {
String scorecardId = generateScorecardId(match.getId());
Scorecard scorecard = new Scorecard(scorecardId, match);
scorecards.put(scorecardId, scorecard);
}
public Scorecard getScorecard(String scorecardId) {
return scorecards.get(scorecardId);
}
public void updateScore(String scorecardId, String teamId, int score) {
Scorecard scorecard = scorecards.get(scorecardId);
if (scorecard != null) {
scorecard.updateScore(teamId, score);
}
}
public void addInnings(String scorecardId, Innings innings) {
Scorecard scorecard = scorecards.get(scorecardId);
if (scorecard != null) {
scorecard.addInnings(innings);
}
}
private String generateScorecardId(String matchId) {
int scorecardId = scorecardIdCounter.incrementAndGet();
return "SC-" + matchId + "-" + String.format("%04d", scorecardId);
}
}