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
47 changes: 42 additions & 5 deletions src/main/java/com/kpi/fict/DefaultStudentService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.kpi.fict;

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

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

public class DefaultStudentService implements StudentService {
private StudentRepository studentRepository;
Expand All @@ -14,27 +16,62 @@ public DefaultStudentService(StudentRepository studentRepository) {

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

@Override
public List<Student> findStudentsWhoTakeMathAndOneMoreExam() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository.findAll().stream().filter(student ->
student
.getExams()
.stream()
.anyMatch(exam -> exam.getType() == Exam.Type.MATH)
&&
student
.getExams()
.size() == 2
).collect(Collectors.toList());
}

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

Double avg = studentRepository.findAll().stream().mapToDouble(Student::getRating).average().orElseThrow(NoSuchFieldError::new);

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

@Override
public List<String> getExamSumAndRatingForEachStudent() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository.findAll().stream().map(student ->
String.join(
",",
student.getExams().stream().map(exam -> exam.getScore()).reduce(0.0, (a, v) -> a + v).toString(),
Double.toString(student.getRating()),
student.getName()
)
).collect(Collectors.toList());
}

@Override
public Student findFirstWithoutMath() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository.findAll().stream().filter(student ->
student
.getExams()
.stream()
.filter(exam -> exam.getType() == Exam.Type.MATH)
.findAny()
.isEmpty()
).findFirst().orElseThrow(NoSuchFieldError::new);
}

public StudentRepository getStudentRepository() {
Expand Down