solved s3 ex2
This commit is contained in:
parent
c24e8efdd3
commit
a7b28895ba
@ -1,3 +1,5 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.text.*;
|
import java.text.*;
|
||||||
|
|
||||||
|
61
03/src/main/java/ch/zhaw/ads/RankingServer.java
Normal file
61
03/src/main/java/ch/zhaw/ads/RankingServer.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.lang.ArrayIndexOutOfBoundsException;
|
||||||
|
|
||||||
|
public class RankingServer implements CommandExecutor {
|
||||||
|
List<Competitor> competitors;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute(String input) {
|
||||||
|
this.competitors = parseCSV(input);
|
||||||
|
|
||||||
|
StringBuffer result = new StringBuffer(100);
|
||||||
|
result.append("Competitors:\n");
|
||||||
|
for (Competitor competitor : competitors) {
|
||||||
|
result.append(competitor.toString() + "\n");
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 List<Competitor> parseCSV(String csv) {
|
||||||
|
int startNr, jg;
|
||||||
|
String name, country, time;
|
||||||
|
|
||||||
|
List<Competitor> competitors = new ArrayList<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.add(new Competitor(startNr, name, jg, country, time));
|
||||||
|
} catch (NumberFormatException | ParseException | ArrayIndexOutOfBoundsException e) {
|
||||||
|
System.err.println("Failed to add competitor.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return competitors;
|
||||||
|
}
|
||||||
|
}
|
38
03/src/test/java/ch/zhaw/ads/RankingServerTest.java
Normal file
38
03/src/test/java/ch/zhaw/ads/RankingServerTest.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.Before;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class RankingServerTest {
|
||||||
|
|
||||||
|
RankingServer rankingServer;
|
||||||
|
String csv;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
rankingServer = new RankingServer();
|
||||||
|
csv = openFile("./build/resources/main/RangZuerich.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
System.out.println(rankingServer.execute(csv));
|
||||||
|
}
|
||||||
|
|
||||||
|
// helper functions
|
||||||
|
private String openFile(String name) throws Exception {
|
||||||
|
BufferedReader br = new BufferedReader(
|
||||||
|
new InputStreamReader(new FileInputStream(name), "ISO-8859-1"));
|
||||||
|
StringBuffer b = new StringBuffer();
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
b.append(line);
|
||||||
|
b.append('\n');
|
||||||
|
}
|
||||||
|
return b.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user