Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.uniwue.epp.festivalplaner;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Collection;
- import java.util.Comparator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- /**
- * Created by Lennart Fries.
- */
- public class Festival {
- private final String name;
- private final String location;
- private final LocalDate begin;
- private final LocalDate end;
- Collection<Appearance> appearances;
- /**
- *
- * @param name
- * Name des Festivals
- * @param location
- * Ort des Festivals
- * @param begin
- * Beginn des Festivals
- * @param end
- * Ende des Festivals
- * @param appearances
- * Auftritte beim Festival
- */
- public Festival(String name, String location, LocalDate begin, LocalDate end, Collection<Appearance> appearances) {
- if (name == null || location == null || begin == null || end == null ) {
- throw new NullPointerException("Übergabewert null nicht erlaubt");
- }
- if (end.isBefore(begin)) {
- throw new IllegalArgumentException("Ende kann nicht vor Beginn liegen");
- }
- AppearanceIntersectionComparator comp = new AppearanceIntersectionComparator();
- int acounter = 0;
- int bcounter = 0;
- for (Appearance a : appearances) {
- acounter++;
- for (Appearance b : appearances) {
- bcounter++;
- if (acounter == bcounter) {
- break;
- }
- if (a.getStage() == b.getStage() && comp.compare(a,b) == 0) {
- throw new IllegalArgumentException("Appearances must not intercept");
- }
- }
- bcounter = 0;
- }
- if (name.equals("") || location.equals("")) {
- throw new IllegalArgumentException("Leere Übergabewerte sind nicht erlaubt");
- }
- this.name = name;
- this.location = location;
- this.begin = begin;
- this.end = end;
- this.appearances = appearances;
- }
- /**
- * Gibt den Namen des Festivals zurück
- * @return name
- */
- public String getName() {
- return name;
- }
- /**
- * Gibt den Ort des Festivals zurück
- * @return location
- */
- public String getLocation() {
- return location;
- }
- /**
- * Gibt den Beginn des Festivals zurück
- * @return begin
- */
- public LocalDate getBegin() {
- return begin;
- }
- /**
- * Gibt das Ende des Festivals zurück
- * @return end
- */
- public LocalDate getEnd() {
- return end;
- }
- /**
- * Gibt eine Liste der Auftritte an jeweiligem Tag zurück
- * @param day
- * Tag
- * @return Liste der Auftritte an day
- */
- public List<Appearance> getAppearances(LocalDate day) {
- return appearances.stream()
- .filter(a -> a.getBegin().toLocalDate().isEqual(day) || a.getEnd().toLocalDate().isEqual(day))
- .sorted()
- .collect(Collectors.toList());
- }
- /**
- * Gibt eine Liste der Auftritte an jeweiligem Tag und Bühne zurück
- * @param day
- * Tag
- * @param stage
- * Bühne
- * @return Liste der Auftritte an day und stage
- */
- public List<Appearance> getAppearances(LocalDate day, Stage stage) {
- return appearances.stream()
- .filter(a -> ((a.getBegin().toLocalDate().isEqual(day) || a.getEnd().toLocalDate().isEqual(day))
- && a.getStage().compareTo(stage) == 0))
- .sorted()
- .collect(Collectors.toList());
- }
- /**
- * Gibt eine Liste der Auftritte an Bühne zurück
- * @param stage
- * Bühne
- * @return Liste der Auftritte an stage
- */
- public List<Appearance> getAppearances(Stage stage) {
- return appearances.stream()
- .filter(a -> a.getStage().compareTo(stage) == 0)
- .sorted()
- .collect(Collectors.toList());
- }
- /**
- * Gibt eine Liste der Auftritte von Genres zurück
- * @param kind
- * Genre
- * @return Liste der Auftritte von kind
- */
- public List<Appearance> getAppearances(Kind kind) {
- return appearances.stream()
- .filter(a -> a.getPerformer().getKind().compareTo(kind) == 0)
- .sorted()
- .collect(Collectors.toList());
- }
- /**
- * Kreirt eine Reihenfolge der zu besuchenden Auftritte unter Berücksichtigung von Preference
- * @param performers
- * Künstler
- * @param preference
- * Wert der Bevorzugung
- * @return Liste der zu besuchenden Auftritte
- */
- public List<Appearance> createRunningOrder(Collection<Performer> performers,
- Function<Appearance,Integer> preference) {
- List<Appearance> appear = new LinkedList<>();
- AppearanceIntersectionComparator comp = new AppearanceIntersectionComparator();
- appearances.stream()
- .filter(a -> performers.contains(a.getPerformer()))
- .sorted(Comparator.comparingInt(i -> preference.apply(i).intValue()))
- .forEachOrdered(a -> {
- if (appear.stream().noneMatch(b -> comp.compare(a,b) == 0)) {
- appear.add(a);
- }
- });
- appear.sort(null);
- return appear;
- }
- /**
- * Stringrepräsentation des Festivals
- * @return StringBuilder sb als String
- */
- public String toString() {
- DateTimeFormatter formatter = (DateTimeFormatter.ofPattern("EEE, dd.MM.yyyy"));
- StringBuilder sb = new StringBuilder(getName());
- sb.append("@");
- sb.append(getLocation());
- sb.append("\n");
- for (LocalDate currentDate = getBegin();
- !currentDate.isAfter(getEnd()); currentDate = currentDate.plusDays(1)) {
- sb.append(currentDate.format(formatter));
- sb.append(":\n");
- for (Stage currentStage : Stage.values()) {
- sb.append(currentStage);
- sb.append(":\n");
- for (Appearance currentAppearance : getAppearances(currentDate, currentStage)) {
- if (currentDate.isEqual(currentAppearance.getBegin().toLocalDate())) {
- sb.append(currentAppearance.getBegin().toLocalTime());
- } else {
- sb.append("00:00");
- }
- sb.append("-");
- if (currentDate.isEqual(currentAppearance.getEnd().toLocalDate())) {
- sb.append(currentAppearance.getEnd().toLocalTime());
- } else {
- sb.append("23:59");
- }
- sb.append(": ");
- sb.append(currentAppearance.getPerformer().toString());
- sb.append("\n");
- }
- sb.append("\n");
- }
- sb.append("\n");
- }
- sb.setLength(sb.length() - 3);
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement