finished s05 ex3

This commit is contained in:
navid.sassan 2020-10-25 21:35:08 +01:00
parent 8daced1696
commit 18750736b4
4 changed files with 3350 additions and 0 deletions

View File

@ -0,0 +1,72 @@
package ch.zhaw.ads;
import java.lang.ArrayIndexOutOfBoundsException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class BinaryTreeRankingServer implements CommandExecutor {
SortedBinaryTree<Competitor> competitors;
int startingRank;
StringBuffer result;
@Override
public String execute(String input) {
this.competitors = parseCSV(input);
result = new StringBuffer(100);
startingRank = 1;
result.append("\n\nSorted Competitors (by time):\n");
competitors.traversal().inorder(visitor);
return result.toString();
}
// This works only because of
// https://stackoverflow.com/questions/29029849/why-do-java-8-lambdas-allow-access-to-non-final-class-variables
Visitor<Competitor> visitor = competitor -> {
competitor.setRank(startingRank++);
result.append(competitor.toString() + "\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 SortedBinaryTree<Competitor> parseCSV(String csv) {
int startNr, jg;
String name, country, time;
SortedBinaryTree<Competitor> competitors = new SortedBinaryTree<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;
}
}

View File

@ -0,0 +1,74 @@
package ch.zhaw.ads;
import java.util.*;
import java.text.*;
import java.lang.Comparable;
import java.lang.Integer;
import java.lang.Long;
public class Competitor implements Comparable<Competitor> {
private String name;
private String country;
private long time;
private int jg;
private int startNr;
private int rank;
public Competitor(int startNr, String name, int jg, String country, String time) throws ParseException {
this.startNr = startNr;
this.name = name;
this.jg = jg;
this.country = country;
this.time = this.parseTime(time);
}
public void setRank(int rank) {
this.rank = rank;
}
public void setTime(long time) {
this.time = time;
}
public long getTime() {
return time;
}
public String getName() {
return name;
}
public int getJg() {
return jg;
}
private static long parseTime(String s) throws ParseException {
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
Date date = sdf.parse(s);
return date.getTime();
}
public String toString() {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S");
StringBuilder sb = new StringBuilder();
sb.append(rank);sb.append(" ");
sb.append(name); sb.append(" ");
sb.append(Integer.toString(jg)); sb.append(" ");
sb.append(df.format(new Date(time)));
return sb.toString();
}
@Override
public int compareTo(Competitor other) {
if (this.equals(other)) {
return 0;
}
return Long.valueOf(this.getTime()).compareTo(Long.valueOf(other.getTime()));
}
public boolean equals(Competitor other) {
return this.getName().equals(other.getName())
&& Integer.valueOf(this.getJg()).equals(Integer.valueOf(other.getJg()))
&& Long.valueOf(this.getTime()).equals(Long.valueOf(other.getTime()));
}
}

File diff suppressed because it is too large Load Diff

View 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 BinaryTreeRankingServerTest {
BinaryTreeRankingServer rankingServer;
String csv;
@Before
public void setUp() throws Exception {
rankingServer = new BinaryTreeRankingServer();
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();
}
}