ExperimentBox/app/src/main/java/ch/nuth/zhaw/exbox/RankingTreeServer.java

38 lines
1.1 KiB
Java

package ch.nuth.zhaw.exbox;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class RankingTreeServer implements CommandExecutor {
public Tree<Competitor> createTree(String rankingText) {
SortedBinaryTree<Competitor> result = new SortedBinaryTree<>();
List<Competitor> competitors = new RankingListServer().createList(rankingText);
for (Competitor competitor : competitors) {
result.add(competitor);
}
return result;
}
public String createSortedText(Tree<Competitor> competitorTree) {
AtomicInteger rank = new AtomicInteger(1);
StringBuilder sb = new StringBuilder();
competitorTree.traversal().inorder(
(competitor) -> {
competitor.setRank(rank.getAndIncrement());
sb.append(competitor);
sb.append(System.lineSeparator());
});
return sb.toString();
}
public String execute(String rankingList) {
Tree<Competitor> competitorTree = createTree(rankingList);
return "Rangliste (Tree)\n" + createSortedText(competitorTree);
}
}