Skip to content

Commit

Permalink
Add tests for calculation and refactor calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
liamskydamien authored and aykborstelmann committed Nov 22, 2019
1 parent 3c96239 commit 42d01f0
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public StudentScoreController(StudentRepository studentRepository, SportResultRe
this.calculationInformationService = calculationInformationService;
}



@GetMapping("/score")
public Integer calculateScore(@PathVariable("id") Long id) {
int scoreResult = 0;
Expand All @@ -37,28 +39,18 @@ public Integer calculateScore(@PathVariable("id") Long id) {
List<SportResult> sportResults = sportResultRepository.findByStudent(student);
for (SportResult sportResult : sportResults) {
if (sportResult.getDiscipline().isRUN()) {
double d = sportResult.getDiscipline().getDistance();
double m = sportResult.getResult();
double z = 0.0;
if(d <= 300 ){
z = 0.24;
}
else if(d > 300 && d <= 400){
z = 0.14;
}
else if(d > 400){
z = 0.0;
}

double distance = sportResult.getDiscipline().getDistance();
double measurement = sportResult.getResult();
double extra = getExtra(sportResult.getDiscipline().getDistance());
double a = calculationInformationService.getAValue(student.getFemale(), sportResult.getDiscipline());
double c = calculationInformationService.getCValue(student.getFemale(), sportResult.getDiscipline());
double scoreRun = (d / (m + z) - a) / c;
double scoreRun = (distance / (measurement + extra) - a) / c;
scoreResult = scoreResult + (int) Math.floor(scoreRun);
} else {
double m = Math.sqrt(sportResult.getResult());
double measurement = Math.sqrt(sportResult.getResult());
double a = calculationInformationService.getAValue(student.getFemale(), sportResult.getDiscipline());
double c = calculationInformationService.getCValue(student.getFemale(), sportResult.getDiscipline());
double scoreOther = (m - a) / c;
double scoreOther = (measurement - a) / c;
scoreResult = scoreResult + (int) Math.floor(scoreOther);
}
}
Expand All @@ -69,9 +61,16 @@ else if(d > 400){
return null;
}

}



private double getExtra(int distance) {
double extra = 0.0;
if(distance <= 300) {
extra = 0.24;
}
else if(distance > 300 && distance <= 400) {
extra = 0.14;
}

return extra;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

Expand Down Expand Up @@ -49,12 +50,109 @@ public void test_calculation_run_50() {

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(3.64800).when(calculationInformationService).getAValue(true, DisciplineType.RUN_50);
doReturn(0.00660).when(calculationInformationService).getCValue(true, DisciplineType.RUN_50);
doReturn(3.79000).when(calculationInformationService).getAValue(true, DisciplineType.RUN_50);
doReturn(0.00690).when(calculationInformationService).getCValue(true, DisciplineType.RUN_50);

Integer integer = studentScoreController.calculateScore(1L);

assertThat(integer).isEqualTo(493);
assertThat(integer).isEqualTo(451);
}

@Test
public void test_calculation_run_75() {
Student student = new Student();
student.setFemale(true);
SportResult sportResult = new SportResult();

sportResult.setDiscipline(DisciplineType.RUN_75);
sportResult.setResult(9.00F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(4.10000).when(calculationInformationService).getAValue(true, DisciplineType.RUN_75);
doReturn(0.00664).when(calculationInformationService).getCValue(true, DisciplineType.RUN_75);

Integer integer = studentScoreController.calculateScore(1L);

assertThat(integer).isEqualTo(604);
}

@Test
public void test_calculation_run_100() {
Student student = new Student();
student.setFemale(true);
SportResult sportResult = new SportResult();

sportResult.setDiscipline(DisciplineType.RUN_100);
sportResult.setResult(12.00F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(4.341).when(calculationInformationService).getAValue(true, DisciplineType.RUN_100);
doReturn(0.00676).when(calculationInformationService).getCValue(true, DisciplineType.RUN_100);

Integer integer = studentScoreController.calculateScore(1L);

assertThat(integer).isEqualTo(566);
}

@Test
public void test_calculation_high_jump() {
Student student = new Student();
student.setFemale(true);
SportResult sportResult = new SportResult();

sportResult.setDiscipline(DisciplineType.HIGH_JUMP);
sportResult.setResult(1.50F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(0.841).when(calculationInformationService).getAValue(true, DisciplineType.HIGH_JUMP);
doReturn(0.00080).when(calculationInformationService).getCValue(true, DisciplineType.HIGH_JUMP);

Integer integer = studentScoreController.calculateScore(1L);

assertThat(integer).isEqualTo(479);
}


@Test
public void test_calculation_general() {
Student student = new Student();
student.setFemale(false);

SportResult resultRUN_100 = new SportResult();
resultRUN_100.setDiscipline(DisciplineType.RUN_100);
resultRUN_100.setResult(12.00F);

SportResult resultRUN_800 = new SportResult();
resultRUN_800.setDiscipline(DisciplineType.RUN_800);
resultRUN_800.setResult(210.00F);

SportResult resultJUMP = new SportResult();
resultJUMP.setDiscipline(DisciplineType.LONG_JUMP);
resultJUMP.setResult(4.20F);

SportResult resultTHROW_200 = new SportResult();
resultTHROW_200.setDiscipline(DisciplineType.BALL_THROWING_200);
resultTHROW_200.setResult(35.00F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Arrays.asList(resultJUMP, resultRUN_100, resultRUN_800, resultTHROW_200)).when(sportResultRepository).findByStudent(student);

doReturn(4.00620).when(calculationInformationService).getAValue(false, DisciplineType.RUN_100);
doReturn(2.02320).when(calculationInformationService).getAValue(false, DisciplineType.RUN_800);
doReturn(1.09350).when(calculationInformationService).getAValue(false, DisciplineType.LONG_JUMP);
doReturn(1.41490).when(calculationInformationService).getAValue(false, DisciplineType.BALL_THROWING_200);

doReturn(0.00656).when(calculationInformationService).getCValue(false, DisciplineType.RUN_100);
doReturn(0.00647).when(calculationInformationService).getCValue(false, DisciplineType.RUN_800);
doReturn(0.00208).when(calculationInformationService).getCValue(false, DisciplineType.LONG_JUMP);
doReturn(0.01039).when(calculationInformationService).getCValue(false, DisciplineType.BALL_THROWING_200);

Integer integer = studentScoreController.calculateScore(1L);

assertThat(integer).isEqualTo(1802);
}

}

0 comments on commit 42d01f0

Please sign in to comment.