|
10 | 10 |
|
11 | 11 | public class Queries { |
12 | 12 |
|
13 | | - private ArrayList<Footballer> footballers; |
14 | | - |
15 | | - public Queries(ArrayList<Footballer> footballers) { |
16 | | - this.footballers = footballers; |
17 | | - } |
18 | | - |
19 | | - public int getTotalOfAllGoalsByMidfielders() { |
20 | | - return footballers.stream() |
21 | | - .filter(footballer -> footballer.position().equals(Position.MIDFIELDER)) |
22 | | - .mapToInt(footballer -> footballer.numberOfGoals()) |
23 | | - .sum(); |
24 | | - } |
25 | | - |
26 | | - public Optional<Footballer> getNameOfVfLWolfsburgFootballerWithMostPlayedGames() { |
27 | | - Predicate<Footballer> isFromWolfsburg = footballer -> footballer |
28 | | - .footballClub().equals(new FootballClub("VfL Wolfsburg", 3, 1145)); |
29 | | - Comparator<Footballer> sortNumberOfGoalsAscending = (footballer1, footballer2) -> Integer |
30 | | - .valueOf(footballer1.numberOfGames()).compareTo(footballer2.numberOfGames()); |
31 | | - |
32 | | - return footballers.stream() |
33 | | - .filter(isFromWolfsburg) |
34 | | - .max(sortNumberOfGoalsAscending); |
35 | | - } |
36 | | - |
37 | | - public List<FootballClub> getNameOfAllFootballClubs() { |
38 | | - return footballers.stream() |
39 | | - .map(footballer -> footballer.footballClub()) |
40 | | - .distinct() |
41 | | - .toList(); |
42 | | - } |
43 | | - |
44 | | - public boolean isFootballerWithSizeInCmLT170AndNumbreOfGoalsBT0() { |
45 | | - return footballers.stream() |
46 | | - .anyMatch(footballer -> footballer.sizeInCm() < 170 && footballer.numberOfGoals() > 0); |
47 | | - } |
48 | | - |
49 | | - public Map<Integer, List<Footballer>> getAllFootballersByBirthyear() { |
50 | | - Function<Footballer, Integer> birthYear = footballer -> footballer.birthDate().getYear(); |
51 | | - return footballers.stream() |
52 | | - .collect(Collectors.groupingBy(birthYear)); |
53 | | - } |
54 | | - |
55 | | - public OptionalDouble getAverageNumberOfPointsFromAllBundesligaFootballClubs() { |
56 | | - return footballers.stream() |
57 | | - .map(footballer -> footballer.footballClub()) |
58 | | - .distinct() |
59 | | - .filter(footballClub -> footballClub.positionInAllTimeTable() != -1) |
60 | | - .mapToInt(footballClub -> footballClub.pointsInAllTimeTable()) |
61 | | - .average(); |
62 | | - } |
| 13 | + private ArrayList<Footballer> footballers; |
| 14 | + |
| 15 | + public Queries(ArrayList<Footballer> footballers) { |
| 16 | + this.footballers = footballers; |
| 17 | + } |
| 18 | + |
| 19 | + public int getTotalOfAllGoalsByMidfielders() { |
| 20 | + return footballers.stream() |
| 21 | + .filter(footballer -> footballer.position().equals(Position.MIDFIELDER)) |
| 22 | + .mapToInt(footballer -> footballer.numberOfGoals()) |
| 23 | + .sum(); |
| 24 | + } |
| 25 | + |
| 26 | + public Optional<Footballer> getNameOfVfLWolfsburgFootballerWithMostPlayedGames() { |
| 27 | + Predicate<Footballer> isFromWolfsburg = |
| 28 | + footballer -> footballer.footballClub().equals(new FootballClub("VfL Wolfsburg", 3, 1145)); |
| 29 | + Comparator<Footballer> sortNumberOfGoalsAscending = |
| 30 | + (footballer1, footballer2) -> |
| 31 | + Integer.valueOf(footballer1.numberOfGames()).compareTo(footballer2.numberOfGames()); |
| 32 | + |
| 33 | + return footballers.stream().filter(isFromWolfsburg).max(sortNumberOfGoalsAscending); |
| 34 | + } |
| 35 | + |
| 36 | + public List<FootballClub> getNameOfAllFootballClubs() { |
| 37 | + return footballers.stream().map(footballer -> footballer.footballClub()).distinct().toList(); |
| 38 | + } |
| 39 | + |
| 40 | + public boolean isFootballerWithSizeInCmLT170AndNumbreOfGoalsBT0() { |
| 41 | + return footballers.stream() |
| 42 | + .anyMatch(footballer -> footballer.sizeInCm() < 170 && footballer.numberOfGoals() > 0); |
| 43 | + } |
| 44 | + |
| 45 | + public Map<Integer, List<Footballer>> getAllFootballersByBirthyear() { |
| 46 | + Function<Footballer, Integer> birthYear = footballer -> footballer.birthDate().getYear(); |
| 47 | + return footballers.stream().collect(Collectors.groupingBy(birthYear)); |
| 48 | + } |
| 49 | + |
| 50 | + public OptionalDouble getAverageNumberOfPointsFromAllBundesligaFootballClubs() { |
| 51 | + return footballers.stream() |
| 52 | + .map(footballer -> footballer.footballClub()) |
| 53 | + .distinct() |
| 54 | + .filter(footballClub -> footballClub.positionInAllTimeTable() != -1) |
| 55 | + .mapToInt(footballClub -> footballClub.pointsInAllTimeTable()) |
| 56 | + .average(); |
| 57 | + } |
63 | 58 | } |
0 commit comments