Advertisement
Guest User

Festival

a guest
Mar 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. package de.uniwue.epp.festivalplaner;
  2.  
  3. import java.time.LocalDate;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.Collection;
  6. import java.util.Comparator;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.function.Function;
  10. import java.util.stream.Collectors;
  11.  
  12. /**
  13. * Created by Lennart Fries.
  14. */
  15.  
  16. public class Festival {
  17. private final String name;
  18. private final String location;
  19. private final LocalDate begin;
  20. private final LocalDate end;
  21. Collection<Appearance> appearances;
  22.  
  23.  
  24. /**
  25. *
  26. * @param name
  27. * Name des Festivals
  28. * @param location
  29. * Ort des Festivals
  30. * @param begin
  31. * Beginn des Festivals
  32. * @param end
  33. * Ende des Festivals
  34. * @param appearances
  35. * Auftritte beim Festival
  36. */
  37. public Festival(String name, String location, LocalDate begin, LocalDate end, Collection<Appearance> appearances) {
  38. if (name == null || location == null || begin == null || end == null ) {
  39. throw new NullPointerException("Übergabewert null nicht erlaubt");
  40. }
  41.  
  42. if (end.isBefore(begin)) {
  43. throw new IllegalArgumentException("Ende kann nicht vor Beginn liegen");
  44. }
  45.  
  46. AppearanceIntersectionComparator comp = new AppearanceIntersectionComparator();
  47. int acounter = 0;
  48. int bcounter = 0;
  49. for (Appearance a : appearances) {
  50. acounter++;
  51. for (Appearance b : appearances) {
  52. bcounter++;
  53. if (acounter == bcounter) {
  54. break;
  55. }
  56. if (a.getStage() == b.getStage() && comp.compare(a,b) == 0) {
  57. throw new IllegalArgumentException("Appearances must not intercept");
  58. }
  59. }
  60. bcounter = 0;
  61. }
  62.  
  63.  
  64. if (name.equals("") || location.equals("")) {
  65. throw new IllegalArgumentException("Leere Übergabewerte sind nicht erlaubt");
  66. }
  67.  
  68. this.name = name;
  69. this.location = location;
  70. this.begin = begin;
  71. this.end = end;
  72. this.appearances = appearances;
  73. }
  74.  
  75. /**
  76. * Gibt den Namen des Festivals zurück
  77. * @return name
  78. */
  79. public String getName() {
  80. return name;
  81. }
  82.  
  83. /**
  84. * Gibt den Ort des Festivals zurück
  85. * @return location
  86. */
  87. public String getLocation() {
  88. return location;
  89. }
  90.  
  91. /**
  92. * Gibt den Beginn des Festivals zurück
  93. * @return begin
  94. */
  95. public LocalDate getBegin() {
  96. return begin;
  97. }
  98.  
  99. /**
  100. * Gibt das Ende des Festivals zurück
  101. * @return end
  102. */
  103. public LocalDate getEnd() {
  104. return end;
  105. }
  106.  
  107. /**
  108. * Gibt eine Liste der Auftritte an jeweiligem Tag zurück
  109. * @param day
  110. * Tag
  111. * @return Liste der Auftritte an day
  112. */
  113. public List<Appearance> getAppearances(LocalDate day) {
  114. return appearances.stream()
  115. .filter(a -> a.getBegin().toLocalDate().isEqual(day) || a.getEnd().toLocalDate().isEqual(day))
  116. .sorted()
  117. .collect(Collectors.toList());
  118. }
  119.  
  120. /**
  121. * Gibt eine Liste der Auftritte an jeweiligem Tag und Bühne zurück
  122. * @param day
  123. * Tag
  124. * @param stage
  125. * Bühne
  126. * @return Liste der Auftritte an day und stage
  127. */
  128. public List<Appearance> getAppearances(LocalDate day, Stage stage) {
  129. return appearances.stream()
  130. .filter(a -> ((a.getBegin().toLocalDate().isEqual(day) || a.getEnd().toLocalDate().isEqual(day))
  131. && a.getStage().compareTo(stage) == 0))
  132. .sorted()
  133. .collect(Collectors.toList());
  134. }
  135.  
  136. /**
  137. * Gibt eine Liste der Auftritte an Bühne zurück
  138. * @param stage
  139. * Bühne
  140. * @return Liste der Auftritte an stage
  141. */
  142. public List<Appearance> getAppearances(Stage stage) {
  143. return appearances.stream()
  144. .filter(a -> a.getStage().compareTo(stage) == 0)
  145. .sorted()
  146. .collect(Collectors.toList());
  147. }
  148.  
  149. /**
  150. * Gibt eine Liste der Auftritte von Genres zurück
  151. * @param kind
  152. * Genre
  153. * @return Liste der Auftritte von kind
  154. */
  155. public List<Appearance> getAppearances(Kind kind) {
  156. return appearances.stream()
  157. .filter(a -> a.getPerformer().getKind().compareTo(kind) == 0)
  158. .sorted()
  159. .collect(Collectors.toList());
  160. }
  161.  
  162. /**
  163. * Kreirt eine Reihenfolge der zu besuchenden Auftritte unter Berücksichtigung von Preference
  164. * @param performers
  165. * Künstler
  166. * @param preference
  167. * Wert der Bevorzugung
  168. * @return Liste der zu besuchenden Auftritte
  169. */
  170. public List<Appearance> createRunningOrder(Collection<Performer> performers,
  171. Function<Appearance,Integer> preference) {
  172. List<Appearance> appear = new LinkedList<>();
  173. AppearanceIntersectionComparator comp = new AppearanceIntersectionComparator();
  174. appearances.stream()
  175. .filter(a -> performers.contains(a.getPerformer()))
  176. .sorted(Comparator.comparingInt(i -> preference.apply(i).intValue()))
  177. .forEachOrdered(a -> {
  178. if (appear.stream().noneMatch(b -> comp.compare(a,b) == 0)) {
  179. appear.add(a);
  180. }
  181. });
  182. appear.sort(null);
  183.  
  184. return appear;
  185. }
  186.  
  187. /**
  188. * Stringrepräsentation des Festivals
  189. * @return StringBuilder sb als String
  190. */
  191. public String toString() {
  192. DateTimeFormatter formatter = (DateTimeFormatter.ofPattern("EEE, dd.MM.yyyy"));
  193. StringBuilder sb = new StringBuilder(getName());
  194. sb.append("@");
  195. sb.append(getLocation());
  196. sb.append("\n");
  197. for (LocalDate currentDate = getBegin();
  198. !currentDate.isAfter(getEnd()); currentDate = currentDate.plusDays(1)) {
  199. sb.append(currentDate.format(formatter));
  200. sb.append(":\n");
  201. for (Stage currentStage : Stage.values()) {
  202. sb.append(currentStage);
  203. sb.append(":\n");
  204. for (Appearance currentAppearance : getAppearances(currentDate, currentStage)) {
  205. if (currentDate.isEqual(currentAppearance.getBegin().toLocalDate())) {
  206. sb.append(currentAppearance.getBegin().toLocalTime());
  207. } else {
  208. sb.append("00:00");
  209. }
  210. sb.append("-");
  211. if (currentDate.isEqual(currentAppearance.getEnd().toLocalDate())) {
  212. sb.append(currentAppearance.getEnd().toLocalTime());
  213. } else {
  214. sb.append("23:59");
  215. }
  216. sb.append(": ");
  217. sb.append(currentAppearance.getPerformer().toString());
  218. sb.append("\n");
  219. }
  220. sb.append("\n");
  221. }
  222. sb.append("\n");
  223. }
  224. sb.setLength(sb.length() - 3);
  225. return sb.toString();
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement