-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leaderboard.java
145 lines (110 loc) · 3.21 KB
/
Leaderboard.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Construct a leadership with the following methods
*
* add(String username, int score)
*
* getPosition(int score)
*
* getScore(int username)
*
*
*/
import java.util.*;
class User {
public String username;
public int score;
User(String username, int score) {
this.username = username;
this.score = score;
}
public String getUsername() {
return this.username;
}
public int getScore() {
return this.score;
}
}
public class Leaderboard {
private ArrayList<User> list;
Leaderboard() {
list = new ArrayList<>();
}
public int findByUsername(String username) {
int index = -1;
int flag = -1;
for (User user: list) {
index++;
String userName = user.getUsername();
if (username.equals(userName)) {
flag = index;
break;
}
}
return flag;
}
public int findByScore(int score) {
int index = -1;
int flag = -1;
for (User user: list) {
index++;
int userScore = user.getScore();
if (score == userScore) {
flag = index;
break;
}
}
return flag;
}
public void add(String username, int score) {
int flag = this.findByUsername(username);
if (flag == -1) {
User user = new User(username, score);
list.add(user);
} else {
User user = (User) list.get(flag);
user.score = score;
list.set(flag, user);
}
Collections.sort(list, (a, b) -> Integer.compare(b.getScore(), a.getScore()));
}
public int getPosition(int score) {
int position = this.findByScore(score);
return position;
}
public int getScore(String username) {
int position = this.findByUsername(username);
if (position == -1) {
throw new NotFoundException("username '" + username + "' not found");
}
User user = (User) list.get(position);
return user.getScore();
}
public void print() {
System.out.println("-----begin");
for (User user: list) {
System.out.printf("[%s, %s] \n", user.getUsername(), user.getScore());
}
System.out.println("-----end");
}
public static void main(String[] args) {
Leaderboard leaderboard = new Leaderboard();
leaderboard.add("user1", 10);
leaderboard.add("user3", 20);
leaderboard.add("user5", 3);
leaderboard.add("user9", 11);
leaderboard.add("user2", 9);
leaderboard.add("user8", 17);
leaderboard.print();
leaderboard.add("user2", 13);
leaderboard.print();
System.out.printf("position of user2 with score 13 is %s\n", leaderboard.getPosition(13));
System.out.printf("score of user5 is %s\n", leaderboard.getScore("user5"));
// should throw error
System.out.printf("score of unknown is %s\n", leaderboard.getScore("unknown"));
}
}
class NotFoundException extends RuntimeException {
public NotFoundException(String msg) {
super(msg);
}
}