Skip to content

Commit 431ca8f

Browse files
committed
[java] [student_grades] Replace Nested Conditional with Guard Clauses Refactoring
1 parent e8a58d9 commit 431ca8f

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

examples/java/java-student_grades-03_if_guard_clause/src/main/java/tv/codely/student_grades/StudentGradeCalculator.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,29 @@ public StudentGradeCalculator(final int yearToCalculate) {
3030
}
3131

3232
public float calculateGrades(final List<Pair<Integer, Float>> examsGrades, final boolean hasReachedMinimumClasses) {
33-
if (!examsGrades.isEmpty()) {
34-
float gradesSum = gradesSum(examsGrades);
35-
int gradesWeightSum = gradesWeightSum(examsGrades);
33+
if (examsGrades.isEmpty()) {
34+
return 0f;
35+
}
3636

37-
if (gradesWeightSum == 100) {
38-
if (hasReachedMinimumClasses) {
39-
if (hasToIncreaseOneExtraPoint()) {
40-
return Float.min(10f, gradesSum + 1);
41-
} else {
42-
return gradesSum;
43-
}
44-
} else {
45-
return 0f;
46-
}
47-
} else if (gradesWeightSum > 100) {
48-
return -1f;
49-
} else {
50-
return -2f;
51-
}
52-
} else {
37+
if (!hasReachedMinimumClasses) {
5338
return 0f;
5439
}
40+
41+
int gradesWeightSum = gradesWeightSum(examsGrades);
42+
43+
if (gradesWeightSum > 100) {
44+
return -1f;
45+
} else if (gradesWeightSum < 100) {
46+
return -2f;
47+
}
48+
49+
float gradesSum = gradesSum(examsGrades);
50+
51+
if (hasToIncreaseOneExtraPoint()) {
52+
return Float.min(10f, gradesSum + 1);
53+
}
54+
55+
return gradesSum;
5556
}
5657

5758
private float gradesSum(List<Pair<Integer, Float>> examsGrades) {

0 commit comments

Comments
 (0)