From b4d146104811dea993d9eb37f1bb0cb58cb13085 Mon Sep 17 00:00:00 2001 From: "navid.sassan" Date: Mon, 23 Nov 2020 17:03:39 +0100 Subject: [PATCH] solved s09 ex3 --- 09/src/main/java/ch/zhaw/ads/Competitor.java | 9 ++- 09/src/main/java/ch/zhaw/ads/HashServer.java | 72 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 09/src/main/java/ch/zhaw/ads/HashServer.java diff --git a/09/src/main/java/ch/zhaw/ads/Competitor.java b/09/src/main/java/ch/zhaw/ads/Competitor.java index 33635c7..714001b 100644 --- a/09/src/main/java/ch/zhaw/ads/Competitor.java +++ b/09/src/main/java/ch/zhaw/ads/Competitor.java @@ -1,3 +1,5 @@ +package ch.zhaw.ads; + import java.util.*; import java.text.*; @@ -43,6 +45,9 @@ public class Competitor implements Comparable { public String toString() { SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S"); StringBuilder sb = new StringBuilder(); + sb.append(" Rank: "); + sb.append(rank); + sb.append(" "); sb.append(" Start Nr: "); sb.append(startNr); sb.append(" "); @@ -59,7 +64,7 @@ public class Competitor implements Comparable { @Override public int compareTo(Competitor toCompare) { int c = name.compareTo(toCompare.name); - c = (c != 0)?c:jg - toCompare.jg; + c = (c != 0) ? c : jg - toCompare.jg; return c; } @@ -78,4 +83,4 @@ public class Competitor implements Comparable { } -} \ No newline at end of file +} diff --git a/09/src/main/java/ch/zhaw/ads/HashServer.java b/09/src/main/java/ch/zhaw/ads/HashServer.java new file mode 100644 index 0000000..2b51c96 --- /dev/null +++ b/09/src/main/java/ch/zhaw/ads/HashServer.java @@ -0,0 +1,72 @@ +package ch.zhaw.ads; + +import java.util.Map; +import java.util.HashMap; + + +public class HashServer implements CommandExecutor { + + private Map competitors; + + @Override + public String execute(String command) { + if (command.toUpperCase().startsWith("GET")) { + Competitor competitor = competitors.get(command.split(" ", 2)[1]); + if (competitor == null) { + return "Competitor not found.\n"; + } + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(competitor.getName()); + stringBuilder.append(" "); + stringBuilder.append(competitor.getJg()); + stringBuilder.append(" -> "); + stringBuilder.append(competitor.toString()); + stringBuilder.append("\n"); + + return stringBuilder.toString(); + } else { + this.competitors = this.parseCSV(command); + return "Parsed CSV.\n"; + } + } + + /** + * NOTE ABOUT CSV FORMAT (from `man virt-diff`) + * Comma-separated values (CSV) is a deceptive format. It seems like it should be easy to parse, + * but it is definitely not easy to parse. + * + * Myth: Just split fields at commas. Reality: This does not work reliably. This example has two + * columns: + * + * "foo,bar",baz + * + * Myth: Read the file one line at a time. Reality: This does not work reliably. This example has + * one row: + * + * "foo + * bar",baz + */ + private Map parseCSV(String csv) { + int startNr, jg; + String name, country, time; + + Map competitors = new HashMap(); + for (String record : csv.split("\\r?\\n")) { + String[] values = record.split(";"); + + try { + startNr = Integer.valueOf(values[0]); + name = values[1]; + jg = Integer.valueOf(values[2]); + country = values[3]; + time = values[4]; + competitors.put(String.join(";", name, values[2]), new Competitor(startNr, name, jg, country, time)); + } catch (Exception e) { + System.err.println("Failed to add competitor."); + e.printStackTrace(); + continue; + } + } + return competitors; + } +}