Praktikum 3: Code fertig, Anfang Tests
This commit is contained in:
parent
408f815d93
commit
f2929c42ff
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<String> 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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package de.joethei.hs.java2.praktikum.praktikum3;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Vorlesungsverzeichnis {
|
||||
private Set<Vorlesung> vorlesungen = new HashSet<>();
|
||||
|
||||
public Vorlesungsverzeichnis(String filename) throws IOException {
|
||||
List<List<String>> datenbasis;
|
||||
datenbasis = load(filename);
|
||||
for(List<String> a : datenbasis) {
|
||||
if(a.size() != 4) throw new TextFormatException("unexpected number of Strings in line");
|
||||
vorlesungen.add(new Vorlesung(a));
|
||||
}
|
||||
}
|
||||
|
||||
private List<List<String>> load(String filename) throws IOException {
|
||||
List<List<String>> 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<String> titles() {
|
||||
List<String> list = new ArrayList<>();
|
||||
for(Vorlesung v: this.vorlesungen) {
|
||||
list.add(v.getTitle());
|
||||
}
|
||||
Collections.sort(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
public Set<String> workaholics() {
|
||||
Set<String> s = new HashSet<>();
|
||||
List<String> 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<String, List<String>> groupToTitles() {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
List<String> list = new ArrayList<>();
|
||||
Set<String> 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<String, List<String>> multipleTitles() {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
List<String> list = new ArrayList<>();
|
||||
Set<String> 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<String> l = new ArrayList<>(map.keySet());
|
||||
for(String title:l) {
|
||||
if(map.get(title).size() < 2) map.remove(title);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public List<String> descendingTitles() {
|
||||
List<String> l = new ArrayList<>();
|
||||
List<Vorlesung> vl = new ArrayList<>(vorlesungen);
|
||||
int most=0, index=0;
|
||||
while(vl.size()>0) {
|
||||
for(int i=0; i<vl.size(); i++) {
|
||||
if(Integer.parseInt(vl.get(i).getTeilnehmerzahl())>=most) {
|
||||
most = Integer.parseInt(vl.get(i).getTeilnehmerzahl());
|
||||
index=i;
|
||||
}
|
||||
}
|
||||
most=0;
|
||||
l.add(vl.get(index).getTitle());
|
||||
vl.remove(index);
|
||||
}
|
||||
return l;
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -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<String> 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue