solved s3 ex3
This commit is contained in:
parent
a7b28895ba
commit
626f0d1bee
@ -2,8 +2,11 @@ package ch.zhaw.ads;
|
||||
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
import java.lang.Comparable;
|
||||
import java.lang.Integer;
|
||||
import java.lang.Long;
|
||||
|
||||
public class Competitor {
|
||||
public class Competitor implements Comparable<Competitor> {
|
||||
private String name;
|
||||
private String country;
|
||||
private long time;
|
||||
@ -55,4 +58,17 @@ public class Competitor {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Competitor other) {
|
||||
if (this.equals(other)) {
|
||||
return 0;
|
||||
}
|
||||
return new Long(this.getTime()).compareTo(new Long(other.getTime()));
|
||||
}
|
||||
|
||||
public boolean equals(Competitor other) {
|
||||
return this.getName().equals(other.getName())
|
||||
&& new Integer(this.getJg()).equals(new Integer(other.getJg()))
|
||||
&& new Long(this.getTime()).equals(new Long(other.getTime()));
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.text.ParseException;
|
||||
import java.lang.ArrayIndexOutOfBoundsException;
|
||||
import java.util.Collections;
|
||||
|
||||
public class RankingServer implements CommandExecutor {
|
||||
List<Competitor> competitors;
|
||||
@ -17,6 +18,15 @@ public class RankingServer implements CommandExecutor {
|
||||
for (Competitor competitor : competitors) {
|
||||
result.append(competitor.toString() + "\n");
|
||||
}
|
||||
|
||||
Collections.sort(competitors);
|
||||
result.append("\n\nSorted Competitors (by time):\n");
|
||||
// set the ranks and append to output
|
||||
for (int i = 0; i < competitors.size(); i++) {
|
||||
Competitor competitor = competitors.get(i);
|
||||
competitor.setRank(i + 1);
|
||||
result.append(competitor.toString() + "\n");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user