112 lines
4.2 KiB
Java
112 lines
4.2 KiB
Java
package de.joethei.hs.java2.praktikum.praktikum3;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class Vorlesungsverzeichnis implements Comparator<Vorlesung>{
|
|
|
|
private Set<Vorlesung> vorlesungen = new HashSet<>();
|
|
|
|
public Vorlesungsverzeichnis(String filename) throws IOException, TextFileFormatException {
|
|
List<List<String>> datenbasis = load(filename);
|
|
for (List<String> entry : datenbasis) {
|
|
if (entry.size() != 4) throw new TextFileFormatException("unexpected number of Strings in line");
|
|
for (String s : entry) if (s.isEmpty()) throw new TextFileFormatException("empty attribute");
|
|
|
|
vorlesungen.add(new Vorlesung(entry));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Lädt die Datenbasis von einer Datei
|
|
* @param filename Dateiname
|
|
* @return Liste der Einträge
|
|
* @throws IOException Dateifehler
|
|
*/
|
|
private List<List<String>> load(String filename) throws IOException {
|
|
List<List<String>> result = new ArrayList<>();
|
|
try(BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
|
for (String line = br.readLine(); line != null; line = br.readLine())
|
|
result.add(Arrays.asList(line.split(":")));
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return eine alphabetisch sortierte Liste mit den Titeln aller Vorlesungen.
|
|
*/
|
|
public List<String> titles() {
|
|
SortedSet<String> set = new TreeSet<>();
|
|
vorlesungen.forEach(vorlesung -> set.add(vorlesung.getTitle()));
|
|
return new ArrayList<>(set);
|
|
}
|
|
|
|
/**
|
|
* @return die Menge derjenigen Dozenten, die zwei oder mehr Vorlesungen halten.
|
|
*/
|
|
public Set<String> workaholics() {
|
|
Map<String, Set<String>> map = vorlesungen.stream().collect(
|
|
Collectors.groupingBy(Vorlesung::getDozent,
|
|
Collectors.mapping(Vorlesung::getTitle, Collectors.toSet())));
|
|
Set<String> set = new HashSet<>();
|
|
map.forEach((s, strings) -> {
|
|
if(strings.size() >= 2) set.add(s);
|
|
});
|
|
return set;
|
|
}
|
|
|
|
/**
|
|
* @return eine Map, die Studiengruppen auf Listen von Vorlesungstiteln abbildet.
|
|
*/
|
|
public Map<String, List<String>> groupToTitles() {
|
|
Map<String, List<String>> map = vorlesungen.stream().collect(
|
|
Collectors.groupingBy(Vorlesung::getStudiengruppe,
|
|
Collectors.mapping(Vorlesung::getTitle, Collectors.toList())));
|
|
map.forEach((s, strings) -> Collections.sort(strings));
|
|
return map;
|
|
}
|
|
|
|
/**
|
|
* @return eine Map, die Vorlesungen auf Listen von Dozenten, die diese Vorlesungen halten, abbildet.
|
|
* Als Schlüssel werden in der Map nur Vorlesungen verwendet, die von unterschiedlichen Dozenten gehalten werden.
|
|
*/
|
|
public Map<String, List<String>> multipleTitles() {
|
|
Map<String, List<String>> map = vorlesungen.stream().collect(
|
|
Collectors.groupingBy(Vorlesung::getTitle,
|
|
Collectors.mapping(Vorlesung::getDozent, Collectors.toList())));
|
|
|
|
Iterator<Map.Entry<String, List<String>>> iterator = map.entrySet().iterator();
|
|
while (iterator.hasNext()) {
|
|
Map.Entry<String, List<String>> entry = iterator.next();
|
|
if(entry.getValue().size() < 2) iterator.remove();
|
|
Collections.sort(entry.getValue());
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
/**
|
|
* @return Liste mit Vorlesungstitel absteigend sortiert nach Teilnehmerzahl
|
|
*/
|
|
public List<String> descendingTitles() {
|
|
SortedSet<Vorlesung> set = new TreeSet<>(this.reversed());
|
|
set.addAll(vorlesungen);
|
|
|
|
List<String> list = new ArrayList<>();
|
|
set.forEach(vorlesung -> list.add(vorlesung.getTitle()));
|
|
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
* für die aufsteigende Sortierung
|
|
*/
|
|
@Override
|
|
public int compare(Vorlesung o1, Vorlesung o2) {
|
|
return Integer.compare(o1.getTeilnehmerzahl(), o2.getTeilnehmerzahl());
|
|
}
|
|
} |