-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreboardTable.java
More file actions
41 lines (31 loc) · 1.09 KB
/
ScoreboardTable.java
File metadata and controls
41 lines (31 loc) · 1.09 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
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class ScoreboardTable {
/**
* table that stores all users and their results after the games
*/
private ConcurrentMap<String,Integer> scoreboardTable = new ConcurrentHashMap<String,Integer>();
private String scoreboardUsers = "";
/*
* initially every user enters the table with 0 points
*/
public void add(String nickname) {
scoreboardTable.put(nickname, 0);
}
// get the score
public int getScore(String nickname){
return scoreboardTable.get(nickname);
}
// update the score - add 1 if the user won a game
public void updateScore(String nickname){
scoreboardTable.replace(nickname, this.getScore(nickname), this.getScore(nickname) + 1);
}
// transform all data from the table into a string - and return it
public String getScoreboard(){
scoreboardUsers = "";
for ( String key : scoreboardTable.keySet() ) {
scoreboardUsers+=key + ": " + this.getScore(key) + " point(s);";
}
return scoreboardUsers;
}
}