Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ public static void main(String[] args) {
Student steffen = new Student("Steffen", 28);
Student kid = new Student("Kind", 15);

FilteredAdultStudents adultStudents = new FilteredAdultStudents();
FilteredStudents adultStudents = new FilteredStudents((student) -> student.age > 18);
adultStudents.add(steffen);
adultStudents.add(kid);
adultStudents.printStudents();

System.out.println("-----------------------");

FilteredTeenStudents teenStudents = new FilteredTeenStudents();
FilteredStudents teenStudents = new FilteredStudents(student -> student.age < 18);
teenStudents.add(steffen);
teenStudents.add(kid);
teenStudents.printStudents();
Expand Down
19 changes: 0 additions & 19 deletions FilteredAdultStudents.java

This file was deleted.

22 changes: 22 additions & 0 deletions FilteredStudents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.ArrayList;
import java.util.function.Predicate;

public class FilteredStudents {
private ArrayList<Student> students;
private Predicate<Student> mandatoryFilter;

public FilteredStudents(Predicate<Student> filter) {
this.students = new ArrayList<>();
this.mandatoryFilter = filter;
}

public void add(Student element) {
if (this.mandatoryFilter.test(element)) {
this.students.add(element);
}
}

public void printStudents() {
this.students.forEach((student) -> System.out.println(student));
}
}
19 changes: 0 additions & 19 deletions FilteredTeenStudents.java

This file was deleted.