From 13b54d1e884734e5a7191c91f98e1b71da46298c Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 18 Oct 2022 13:48:27 +0200 Subject: [PATCH] Implement `RankingTreeServer` --- .../ch/nuth/zhaw/exbox/RankingTreeServer.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/RankingTreeServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/RankingTreeServer.java index 5a85573..0e638ca 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/RankingTreeServer.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/RankingTreeServer.java @@ -1,18 +1,32 @@ 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) { - // TODO Implement - return null; + 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(); // TODO Implement + + competitorTree.traversal().inorder( + (competitor) -> { + competitor.setRank(rank.getAndIncrement()); + sb.append(competitor); + sb.append(System.lineSeparator()); + }); + return sb.toString(); } @@ -20,19 +34,4 @@ public class RankingTreeServer implements CommandExecutor { Tree competitorTree = createTree(rankingList); return "Rangliste (Tree)\n" + createSortedText(competitorTree); } - - public static void main(String[] args) { - String rangliste = "Mueller Stefan;02:31:14\n" + - "Marti Adrian;02:30:09\n" + - "Kiptum Daniel;02:11:31\n" + - "Ancay Tarcis;02:20:02\n" + - "Kreibuhl Christian;02:21:47\n" + - "Ott Michael;02:33:48\n" + - "Menzi Christoph;02:27:26\n" + - "Oliver Ruben;02:32:12\n" + - "Elmer Beat;02:33:53\n" + - "Kuehni Martin;02:33:36\n"; - RankingTreeServer server = new RankingTreeServer(); - System.out.println(server.execute(rangliste)); - } }