solved s09 ex3
This commit is contained in:
parent
52fde3d49c
commit
b4d1461048
@ -1,3 +1,5 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.text.*;
|
import java.text.*;
|
||||||
|
|
||||||
@ -43,6 +45,9 @@ public class Competitor implements Comparable<Competitor> {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S");
|
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S");
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(" Rank: ");
|
||||||
|
sb.append(rank);
|
||||||
|
sb.append(" ");
|
||||||
sb.append(" Start Nr: ");
|
sb.append(" Start Nr: ");
|
||||||
sb.append(startNr);
|
sb.append(startNr);
|
||||||
sb.append(" ");
|
sb.append(" ");
|
||||||
@ -59,7 +64,7 @@ public class Competitor implements Comparable<Competitor> {
|
|||||||
@Override
|
@Override
|
||||||
public int compareTo(Competitor toCompare) {
|
public int compareTo(Competitor toCompare) {
|
||||||
int c = name.compareTo(toCompare.name);
|
int c = name.compareTo(toCompare.name);
|
||||||
c = (c != 0)?c:jg - toCompare.jg;
|
c = (c != 0) ? c : jg - toCompare.jg;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,4 +83,4 @@ public class Competitor implements Comparable<Competitor> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
72
09/src/main/java/ch/zhaw/ads/HashServer.java
Normal file
72
09/src/main/java/ch/zhaw/ads/HashServer.java
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
|
||||||
|
public class HashServer implements CommandExecutor {
|
||||||
|
|
||||||
|
private Map<String, Competitor> 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<String, Competitor> parseCSV(String csv) {
|
||||||
|
int startNr, jg;
|
||||||
|
String name, country, time;
|
||||||
|
|
||||||
|
Map<String, Competitor> competitors = new HashMap<String, Competitor>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user