-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeaderBoard.java
47 lines (41 loc) · 1.25 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
public class LeaderBoard implements Comparable<LeaderBoard>{
private final String name;
public final int level;
public final double time;
public LeaderBoard(String name, int level, double time){
this.name = name;
this.level = level;
this.time = time;
}
public String getName() {
return name;
}
public int getLevel() {
return level;
}
public double getTime() {
return time;
}
@Override
public String toString() {
return name + ": " + level + " level " + time + " seconds";
}
/**
* Compares two LeaderBoard objects by their level value, then by time value, and finally by name.
* @param o second LeaderBoard object
* @return a negative value if this is less than the second object, 0 if equal, and positive if more than.
*/
@Override
public int compareTo(LeaderBoard o) {
int levelDif = -Integer.compare(this.level, o.level);
int timeDif = (int) Double.compare(this.time, o.time);
int nameDif = this.name.compareTo(o.name);
if(levelDif != 0){
return levelDif;
}
else if(timeDif != 0){
return timeDif;
}
else return nameDif;
}
}