package ch.nuth.zhaw.exbox; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; public class RankingTreeServer implements CommandExecutor { public Tree createTree(String rankingText) { SortedBinaryTree result = new SortedBinaryTree<>(); List competitors = new RankingListServer().createList(rankingText); for (Competitor competitor : competitors) { result.add(competitor); } return result; } public String createSortedText(Tree 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 competitorTree = createTree(rankingList); return "Rangliste (Tree)\n" + createSortedText(competitorTree); } }