From f2929c42ff6d55e9a1c72143158b034f6dbf44cd Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 20 Apr 2018 07:03:19 +0200 Subject: [PATCH] Praktikum 3: Code fertig, Anfang Tests --- .../praktikum3/TextFormatException.java | 10 ++ .../java2/praktikum/praktikum3/Vorlesung.java | 39 +++++++ .../praktikum3/Vorlesungsverzeichnis.java | 108 ++++++++++++++++++ src/main/resources/vorlesungen.txt | 6 + .../tests/VorlesungsverzeichnisTest.java | 43 +++++++ 5 files changed, 206 insertions(+) create mode 100644 src/main/java/de/joethei/hs/java2/praktikum/praktikum3/TextFormatException.java create mode 100644 src/main/java/de/joethei/hs/java2/praktikum/praktikum3/Vorlesung.java create mode 100644 src/main/java/de/joethei/hs/java2/praktikum/praktikum3/Vorlesungsverzeichnis.java create mode 100644 src/main/resources/vorlesungen.txt create mode 100644 src/test/java/de/joethei/hs/java2/tests/VorlesungsverzeichnisTest.java diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/TextFormatException.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/TextFormatException.java new file mode 100644 index 0000000..a46429f --- /dev/null +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/TextFormatException.java @@ -0,0 +1,10 @@ +package de.joethei.hs.java2.praktikum.praktikum3; + +import java.io.IOException; + +public class TextFormatException extends IOException { + + public TextFormatException(String s) { + super(s); + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/Vorlesung.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/Vorlesung.java new file mode 100644 index 0000000..9fa7081 --- /dev/null +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/Vorlesung.java @@ -0,0 +1,39 @@ +package de.joethei.hs.java2.praktikum.praktikum3; + +import java.util.List; + +public class Vorlesung { + private String studiengruppe; + private String title; + private String dozent; + private String teilnehmerzahl; + + public Vorlesung(List list) throws TextFormatException{ + if(list.size() <1 || list.size() > 4) throw new TextFormatException("invalid number of Strings in list"); + this.studiengruppe = list.get(0); + this.title = list.get(1); + this.dozent = list.get(2); + this.teilnehmerzahl = list.get(3); + } + + public String getStudiengruppe() { + return studiengruppe; + } + + public String getTitle() { + return title; + } + + public String getDozent() { + return dozent; + } + + public String getTeilnehmerzahl() { + return teilnehmerzahl; + } + + @Override + public String toString() { + return studiengruppe + ":" + title + ":" + dozent + ":" + teilnehmerzahl; + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/Vorlesungsverzeichnis.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/Vorlesungsverzeichnis.java new file mode 100644 index 0000000..f2d87ff --- /dev/null +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum3/Vorlesungsverzeichnis.java @@ -0,0 +1,108 @@ +package de.joethei.hs.java2.praktikum.praktikum3; + +import java.io.*; +import java.util.*; + +public class Vorlesungsverzeichnis { + private Set vorlesungen = new HashSet<>(); + + public Vorlesungsverzeichnis(String filename) throws IOException { + List> datenbasis; + datenbasis = load(filename); + for(List a : datenbasis) { + if(a.size() != 4) throw new TextFormatException("unexpected number of Strings in line"); + vorlesungen.add(new Vorlesung(a)); + } + } + + private List> load(String filename) throws IOException { + List> result = new ArrayList<>(); + BufferedReader br = new BufferedReader(new FileReader(filename)); + for (String line=br.readLine(); line!=null; line=br.readLine()) + result.add(Arrays.asList(line.split(":"))); + br.close(); + return result; + } + + public List titles() { + List list = new ArrayList<>(); + for(Vorlesung v: this.vorlesungen) { + list.add(v.getTitle()); + } + Collections.sort(list); + return list; + } + + public Set workaholics() { + Set s = new HashSet<>(); + List list = new ArrayList<>(); + for(Vorlesung v:vorlesungen) { + list.add(v.getDozent()); + s.add(v.getDozent()); + } + for(String name:s) { + list.remove(name); + } + return new HashSet<>(list); + } + + public Map> groupToTitles() { + Map> map = new HashMap<>(); + List list = new ArrayList<>(); + Set s = new HashSet<>(); + for(Vorlesung v:vorlesungen) { + s.add(v.getStudiengruppe()); + } + for(String gruppe : s) { + for(Vorlesung v : vorlesungen) { + if(gruppe.equals(v.getStudiengruppe())) { + list.add(v.getTitle()); + } + } + map.put(gruppe, list); + list = new ArrayList<>(); + } + return map; + } + + public Map> multipleTitles() { + Map> map = new HashMap<>(); + List list = new ArrayList<>(); + Set s = new HashSet<>(); + for(Vorlesung v:vorlesungen) { + s.add(v.getTitle()); + } + for(String title : s) { + for(Vorlesung v:vorlesungen) { + if(title.equals(v.getTitle())) { + list.add(v.getDozent()); + } + } + map.put(title, list); + list = new ArrayList<>(); + } + List l = new ArrayList<>(map.keySet()); + for(String title:l) { + if(map.get(title).size() < 2) map.remove(title); + } + return map; + } + + public List descendingTitles() { + List l = new ArrayList<>(); + List vl = new ArrayList<>(vorlesungen); + int most=0, index=0; + while(vl.size()>0) { + for(int i=0; i=most) { + most = Integer.parseInt(vl.get(i).getTeilnehmerzahl()); + index=i; + } + } + most=0; + l.add(vl.get(index).getTitle()); + vl.remove(index); + } + return l; + } +} diff --git a/src/main/resources/vorlesungen.txt b/src/main/resources/vorlesungen.txt new file mode 100644 index 0000000..f4e574b --- /dev/null +++ b/src/main/resources/vorlesungen.txt @@ -0,0 +1,6 @@ +I2:Java 2:Rump:100 +I2:Algorithmen und Datenstrukturen:Totzauer:80 +MT2:Mathematik 2:von Coelln:60 +MT2:Audio-/Videotechnik:Lemke:50 +E2:Mathematik 2:Rabe:70 +I4:Rechnerarchitekturen:von Coelln:45 \ No newline at end of file diff --git a/src/test/java/de/joethei/hs/java2/tests/VorlesungsverzeichnisTest.java b/src/test/java/de/joethei/hs/java2/tests/VorlesungsverzeichnisTest.java new file mode 100644 index 0000000..9eadf8b --- /dev/null +++ b/src/test/java/de/joethei/hs/java2/tests/VorlesungsverzeichnisTest.java @@ -0,0 +1,43 @@ +package de.joethei.hs.java2.tests; + +import de.joethei.hs.java2.praktikum.praktikum3.Vorlesungsverzeichnis; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static org.junit.Assert.*; + +public class VorlesungsverzeichnisTest { + + @Test(expected = IOException.class) + public void constructors() throws IOException { + new Vorlesungsverzeichnis("HalloWelt"); + } + + @Test + public void titles() { + try { + Vorlesungsverzeichnis vorlesungsverzeichnis = new Vorlesungsverzeichnis(Objects.requireNonNull(getClass().getClassLoader().getResource("vorlesungen.txt")).getFile()); + List titles = new ArrayList<>(); + titles.add("Algorithmen und Datenstrukturen"); + titles.add("Audio-/Videotechnik"); + titles.add("Java 2"); + titles.add("Mathematik 2"); + titles.add("Mathematik 2"); + titles.add("Rechnerarchitekturen"); + + assertEquals(vorlesungsverzeichnis.titles(), titles); + + titles.add("Hallo Welt"); + assertNotEquals(vorlesungsverzeichnis.titles(), titles); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + +} \ No newline at end of file