Skip to content
56 changes: 51 additions & 5 deletions src/main/java/com/kpi/fict/DefaultStudentService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.kpi.fict;

import com.kpi.fict.entities.Exam;
import com.kpi.fict.entities.Student;
import com.kpi.fict.repositories.StudentRepository;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class DefaultStudentService implements StudentService {
private StudentRepository studentRepository;
Expand All @@ -14,28 +17,71 @@ public DefaultStudentService(StudentRepository studentRepository) {

@Override
public List<Student> findAllWithRatingLessThan300() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository
.findAll()
.stream()
.filter(s -> s
.getExams()
.stream()
.map(Exam::getScore)
.reduce(0.0, Double::sum) < 300)
.collect(Collectors.toList());
}

@Override
public double findAvgRatingOfMathExam() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository
.findAll()
.stream()
.map(Student::getExams)
.flatMap(Collection::stream)
.filter(e -> e.getType() == Exam.Type.MATH)
.mapToDouble(Exam::getScore)
.average()
.orElse(0.0);
}

//Delimiter: ','
@Override
public List<String> calculateAvgRatingForEachStudent() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository
.findAll()
.stream()
.map(s -> s.getName() + "," + s
.getExams()
.stream()
.mapToDouble(Exam::getScore)
.average()
.orElse(0.0))
.collect(Collectors.toList());
}

@Override
public List<Student> findStudentsWithoutExams() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository
.findAll()
.stream()
.filter(s -> s.getExams().size() == 0)
.collect(Collectors.toList());
}

@Override
public List<Student> findStudentsWithRatingMoreThanAvgAndTakeMathExam() {
throw new UnsupportedOperationException("Need to make implementation");
double avgRating = studentRepository
.findAll()
.stream()
.mapToDouble(Student::getRating)
.average()
.orElse(0.0);

return studentRepository
.findAll()
.stream()
.filter(s -> s.getRating() > avgRating && s
.getExams()
.stream()
.anyMatch(e -> e.getType() == Exam.Type.MATH))
.collect(Collectors.toList());
}

public StudentRepository getStudentRepository() {
Expand Down