Compare commits

...

6 commits

7 changed files with 403 additions and 0 deletions

10
Rangliste.csv Normal file
View file

@ -0,0 +1,10 @@
Mueller Stefan;02:31:14
Marti Adrian;02:30:09
Kiptum Daniel;02:11:31
Ancay Tarcis;02:20:02
Kreibuhl Christian;02:21:47
Ott Michael;02:33:48
Menzi Christoph;02:27:26
Oliver Ruben;02:32:12
Elmer Beat;02:33:53
Kuehni Martin;02:33:36
1 Mueller Stefan 02:31:14
2 Marti Adrian 02:30:09
3 Kiptum Daniel 02:11:31
4 Ancay Tarcis 02:20:02
5 Kreibuhl Christian 02:21:47
6 Ott Michael 02:33:48
7 Menzi Christoph 02:27:26
8 Oliver Ruben 02:32:12
9 Elmer Beat 02:33:53
10 Kuehni Martin 02:33:36

View file

@ -0,0 +1,90 @@
package ch.nuth.zhaw.exbox;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
public class Competitor implements Comparable<Competitor> {
private final String name;
private final String time;
private int rank;
public Competitor(int rank, String name, String time) {
this.rank = rank;
this.name = name;
this.time = time;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getTime() {
return time;
}
public String getName() {
return name;
}
private static long parseTime(String s) {
try {
DateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(s);
return date.getTime();
} catch (Exception e) {System.err.println(e);}
return 0;
}
private static String timeToString(int time) {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.format(new Date(time));
}
public String toString() {
return ""+ rank + " "+name+" "+time;
}
@Override
public int compareTo(Competitor o) {
int result = rank - o.rank;
if (result == 0) {
result = Long.compare(parseTime(getTime()), parseTime(o.getTime()));
if (result == 0) {
result = getName().compareTo(o.getName());
}
}
return result;
}
@Override
public boolean equals (Object o) {
if (o instanceof Competitor) {
Competitor c = (Competitor)o;
return compareTo(c) == 0;
}
else {
return false;
}
}
@Override
public int hashCode() {
return (getTime().hashCode() + getName().hashCode()) * rank;
}
}
class AlphaComparatorCompetitor implements Comparator<Competitor> {
@Override
public int compare(Competitor o1, Competitor o2) {
// TODO Implement
int c = 0;
return c;
}
}

View file

@ -0,0 +1,71 @@
package ch.nuth.zhaw.exbox;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class RankingListServer implements CommandExecutor {
/**
* The delimiter of the data.
*/
private static final String Delimiter = ";";
public List<Competitor> createList(String rankingText) {
String currentLine;
try {
try (BufferedReader reader = new BufferedReader(new StringReader(rankingText))) {
List<Competitor> result = new ArrayList<>();
while ((currentLine = reader.readLine()) != null) {
String[] fields = currentLine.split(Delimiter);
result.add(
new Competitor(
0,
fields[0],
fields[1]));
}
return result;
}
} catch (Exception e) {
System.out.println("An error occurred");
System.out.println(e);
}
return null;
}
public String createSortedText(List<Competitor> competitorList) {
List<Competitor> competitors = competitorList.stream().sorted().toList();
String previousTime = null;
int previousRank = 1;
for (int i = 0; i < competitors.size(); i++) {
Competitor competitor = competitors.get(i);
if (competitor.getTime() != previousTime) {
previousRank = i + 1;
previousTime = competitor.getTime();
}
competitor.setRank(previousRank);
}
return String.join(
System.lineSeparator(),
competitors.stream().map((competitor) -> competitor.toString()).toList());
}
public String createNameList(List<Competitor> competitorList) {
// TODO Implement
return "";
}
public String execute(String rankingList) {
List<Competitor> competitorList = createList(rankingList);
return "Rangliste\n" + createSortedText(competitorList);
}
}

View file

@ -0,0 +1,34 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ADS3_1_test {
Competitor c1, c2, c3, c4;
@BeforeEach
public void setUp() {
c1 = new Competitor(0, "Mueller Stefan", "02:31:14");
c2 = new Competitor(0, "Marti Adrian", "02:30:09");
c3 = new Competitor(0, "Kiptum Daniel", "02:11:31");
c4 = new Competitor(0, "Speedy Gonzales", "1:11:31");
}
@Test
public void testEquals() {
assertTrue(c3.equals(c3), c3.getTime() + " == "+c3.getTime());
assertTrue(!c3.equals(c2), c3.getTime() + " != "+c2.getTime());
assertTrue(!c3.equals(null), "equals(null)");
assertTrue(!c3.equals(new Object()), "equals(new Object())");
}
@Test
public void testCompare() {
assertTrue(c1.compareTo(c2) > 0, c1.getName() +" "+c1.getTime() + " > "+c2.getTime());
assertTrue(c2.compareTo(c1) < 0, c2.getName() +" "+c2.getTime() + " < "+c1.getTime());
assertTrue(c3.compareTo(c3) == 0, c3.getName() +" "+c3.getTime() + " == "+c3.getTime());
assertTrue(c4.compareTo(c2) < 0, c4.getName() +" "+c4.getTime() + " < "+c2.getTime());
}
}

View file

@ -0,0 +1,49 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.LinkedList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ADS3_2_test {
List<Competitor> rankGood;
List<Competitor> rankTest;
public List<Competitor> createList(String rankingText) {
List<Competitor> competitorList = new LinkedList<>();
String[] lines = rankingText.split("\n");
for (String line : lines) {
String name = line.split(";")[0];
String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time));
}
return competitorList;
}
@BeforeEach
public void setUp() {
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";
rankGood = createList(rangliste);
rankTest = new RankingListServer().createList(rangliste);
}
@Test
public void testCreateList() {
assertEquals(rankGood.size(), rankTest.size(), "length");
for (int i = 0; i < rankGood.size();i++) {
assertEquals(rankGood.get(i).toString(), rankTest.get(i).toString(), "rangliste["+i+"]");
}
}
}

View file

@ -0,0 +1,70 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ADS3_3_test {
List<Competitor> rankGood;
List<Competitor> rankTest;
String textGood;
String textTest;
public List<Competitor> createList(String rankingText) {
List<Competitor> competitorList = new LinkedList<>();
String[] lines = rankingText.split("\n");
for (String line : lines) {
String name = line.split(";")[0];
String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time));
}
return competitorList;
}
public String createSortedText(List<Competitor> competitorList) {
Collections.sort(competitorList);
int rank = 1;
StringBuilder sb = new StringBuilder();
for (Competitor c : competitorList) {
c.setRank(rank++);
sb.append(c).append("\n");
}
return sb.toString();
}
@BeforeEach
public void setUp() {
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";
rankGood = createList(rangliste);
textGood = createSortedText(rankGood);
rankTest = new RankingListServer().createList(rangliste);
textTest = new RankingListServer().createSortedText(rankTest);
}
private String clean(String s) {
return s.trim();
}
@Test
public void testCreateText() {
String[] good = textGood.split("\n");
String[] test = textTest.split("\n");
assertEquals(good.length, test.length, "length");
for (int i = 0; i < good.length;i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste["+i+"]");
}
}
}

View file

@ -0,0 +1,79 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ADS3_4_test {
List<Competitor> rankGood;
List<Competitor> rankTest;
String textGood;
String textTest;
public List<Competitor> createList(String rankingText) {
List<Competitor> competitorList = new LinkedList<>();
String[] lines = rankingText.split("\n");
for (String line : lines) {
String name = line.split(";")[0];
String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time));
}
return competitorList;
}
public String createSortedText(List<Competitor> competitorList) {
StringBuilder sb = new StringBuilder();
for (Competitor c : competitorList) {
sb.append(c).append("\n");
}
return sb.toString();
}
@BeforeEach
public void setUp() {
String ranglisteOrdered =
"Ancay Tarcis;02:20:02\n" +
"Elmer Beat;02:33:53\n" +
"Kiptum Daniel;02:11:31\n" +
"Kreibuhl Christian;02:21:47\n" +
"Kuehni Martin;02:33:36\n" +
"Marti Adrian;02:30:09\n" +
"Menzi Christoph;02:27:26\n" +
"Mueller Stefan;02:31:14\n" +
"Oliver Ruben;02:32:12\n" +
"Ott Michael;02:33:48\n";
String ranglisteUnordered = "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";
rankGood = createList(ranglisteOrdered);
textGood = createSortedText(rankGood);
rankTest = createList(ranglisteUnordered);
textTest = new RankingListServer().createNameList(rankTest);
}
private String clean(String s) {
return s.trim();
}
@Test
public void testCreateText() {
String[] good = textGood.split("\n");
String[] test = textTest.split("\n");
assertEquals(good.length, test.length, "length");
for (int i = 0; i < good.length;i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste["+i+"]");
}
}
}