Skip to content
Open
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
142 changes: 104 additions & 38 deletions src/main/java/com/kpi/fict/DefaultStudentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,108 @@
import java.util.List;

public class DefaultStudentService implements StudentService {
private StudentRepository studentRepository;

public DefaultStudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}

@Override
public Student findFirstWithoutMath() {
throw new UnsupportedOperationException("Need to make implementation");
}

@Override
public List<Student> findStudentsWithMathRatingMoreThanAvgAndTakeEngExam() {
throw new UnsupportedOperationException("Need to make implementation");
}

@Override
public List<String> getExamSumAndRatingForEachStudent() {
throw new UnsupportedOperationException("Need to make implementation");
}

@Override
public List<Student> findStudentsWithTwoExams() {
throw new UnsupportedOperationException("Need to make implementation");
}

@Override
public Student findStudentWithMaxEngRating() {
throw new UnsupportedOperationException("Need to make implementation");
}

public StudentRepository getStudentRepository() {
return studentRepository;
}

public void setStudentRepository(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
private StudentRepository studentRepository;

public DefaultStudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}

@Override
public Student findFirstWithoutMath() {
return studentRepository
.findAll()
.stream()
.filter(student -> student
.getExams()
.stream()
.noneMatch(exam -> exam.getType() == Exam.Type.MATH))
.findFirst()
.orElseThrow(NoSuchFieldError::new);
}

@Override
public List<Student> findStudentsWithMathRatingMoreThanAvgAndTakeEngExam() {
OptionalDouble average = studentRepository
.findAll()
.stream()
.map(Student::getExams)
.flatMap(Collection::stream)
.filter(exam -> exam.getType() == Exam.Type.MATH)
.mapToDouble(Exam::getScore)
.average();

double avg = average.orElseThrow(NoSuchFieldError::new);

return studentRepository.findAll()
.stream()
.filter(student ->
student.getExams()
.stream()
.anyMatch(exam -> exam.getType() == Exam.Type.ENGLISH)
&&
student.getExams()
.stream()
.anyMatch(exam -> exam.getType() == Exam.Type.MATH
&&
exam.getScore() > avg))
.collect(Collectors.toList());
}

@Override
public List<String> getExamSumAndRatingForEachStudent() {
return studentRepository
.findAll()
.stream()
.map(student -> student
.getExams()
.stream()
.mapToDouble(Exam::getScore)
.sum()
+ ","
+ student.getRating()
+ ","
+ student.getName())
.collect(Collectors.toList());
}

@Override
public List<Student> findStudentsWithTwoExams() {
return studentRepository
.findAll()
.stream()
.filter(student -> student.getExams().size() == 2)
.collect(Collectors.toList());
}

@Override
public Student findStudentWithMaxEngRating() {
return studentRepository
.findAll()
.stream()
.filter(student -> student
.getExams()
.stream()
.anyMatch(exam -> exam.getType() == Exam.Type.ENGLISH))
.max((student, t1) -> {
Optional<Exam> score1 = student
.getExams()
.stream()
.filter(exam -> exam.getType() == Exam.Type.ENGLISH)
.findFirst();
Optional<Exam> score2 = t1
.getExams()
.stream()
.filter(exam -> exam.getType() == Exam.Type.ENGLISH)
.findFirst();
return (int) (score1.get().getScore() - score2.get().getScore());
}).orElseThrow(NoSuchElementException::new);
}

public StudentRepository getStudentRepository() {
return studentRepository;
}

public void setStudentRepository(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
}