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

38 lines
1.1 KiB
Java
Raw Normal View History

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