diff --git a/src/main/java/com/kpi/fict/DefaultStudentService.java b/src/main/java/com/kpi/fict/DefaultStudentService.java index a59a60e..2a3c0ea 100644 --- a/src/main/java/com/kpi/fict/DefaultStudentService.java +++ b/src/main/java/com/kpi/fict/DefaultStudentService.java @@ -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 findStudentsWithMathRatingMoreThanAvgAndTakeEngExam() { - throw new UnsupportedOperationException("Need to make implementation"); - } - - @Override - public List getExamSumAndRatingForEachStudent() { - throw new UnsupportedOperationException("Need to make implementation"); - } - - @Override - public List 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 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 getExamSumAndRatingForEachStudent() { + return studentRepository + .findAll() + .stream() + .map(student -> student + .getExams() + .stream() + .mapToDouble(Exam::getScore) + .sum() + + "," + + student.getRating() + + "," + + student.getName()) + .collect(Collectors.toList()); + } + + @Override + public List 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 score1 = student + .getExams() + .stream() + .filter(exam -> exam.getType() == Exam.Type.ENGLISH) + .findFirst(); + Optional 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; + } }