solved Task 4

This commit is contained in:
schrom01 2022-10-04 14:17:57 +02:00
parent 77042b8d85
commit 4d62d5b7d8
2 changed files with 14 additions and 4 deletions

View File

@ -77,7 +77,7 @@ class AlphaComparatorCompetitor implements Comparator<Competitor> {
@Override @Override
public int compare(Competitor o1, Competitor o2) { public int compare(Competitor o1, Competitor o2) {
// TODO Implement // TODO Implement
int c = o1.compareTo(o2); int c = o1.getName().compareTo(o2.getName());
return c; return c;
} }
} }

View File

@ -1,5 +1,7 @@
package ch.zhaw.ads; package ch.zhaw.ads;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -16,12 +18,18 @@ public class RankingListServer implements CommandExecutor {
public String createSortedText(List<Competitor> competitorList) { public String createSortedText(List<Competitor> competitorList) {
// TODO Implement // TODO Implement
competitorList.sort(new AlphaComparatorCompetitor()); competitorList.sort(new Comparator<Competitor>() {
@Override
public int compare(Competitor o1, Competitor o2) {
int c = o1.compareTo(o2);
return c;
}
});
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
int rank = 1; int rank = 1;
for (Competitor competitor : competitorList) { for (Competitor competitor : competitorList) {
competitor.setRank(rank); competitor.setRank(rank);
stringBuilder.append(rank).append(" ").append(competitor.getName()).append(" ").append(competitor.getTime()).append("\n"); stringBuilder.append(competitor.toString()).append("\n");
rank++; rank++;
} }
return stringBuilder.toString(); return stringBuilder.toString();
@ -29,9 +37,11 @@ public class RankingListServer implements CommandExecutor {
public String createNameList(List<Competitor> competitorList) { public String createNameList(List<Competitor> competitorList) {
// TODO Implement // TODO Implement
competitorList.sort(new AlphaComparatorCompetitor());
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
int rank = 0;
for (Competitor competitor : competitorList) { for (Competitor competitor : competitorList) {
stringBuilder.append(competitor.getName()).append(" ").append(competitor.getTime()).append("\n"); stringBuilder.append(competitor.toString()).append("\n");
} }
return stringBuilder.toString(); return stringBuilder.toString();
} }