Add files for the assignment #3

This commit is contained in:
Manuel Thalmann 2022-10-04 14:34:15 +02:00
parent bf793a8cea
commit 32799964b9
6 changed files with 335 additions and 0 deletions

View file

@ -0,0 +1,77 @@
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) {
// TODO Implement
return 0;
}
@Override
public boolean equals (Object o) {
// TODO Implement
return false;
}
@Override
public int hashCode() {
// TODO Implement
return 0;
}
}
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,26 @@
package ch.nuth.zhaw.exbox;
import java.util.List;
public class RankingListServer implements CommandExecutor {
public List<Competitor> createList(String rankingText) {
// TODO Implement
return null;
}
public String createSortedText(List<Competitor> competitorList) {
// TODO Implement
return "";
}
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+"]");
}
}
}