Skip to content

Commit 75e9b0a

Browse files
committed
[java] [student_grades] Decompose Conditional & Consolidate Conditional Expression Refactoring (add complexity in order to illustrate example)
1 parent cace954 commit 75e9b0a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

examples/java/java-student_grades-05_simplify_conditionals/src/main/java/tv/codely/student_grades/StudentGradeCalculator.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ public class StudentGradeCalculator {
2121
new Pair<>("Abelardo", false),
2222
new Pair<>("Francisca", false)
2323
)
24+
),
25+
new AbstractMap.SimpleImmutableEntry<>(
26+
2018,
27+
List.of(
28+
new Pair<>("Javi", false),
29+
new Pair<>("Rafa", false),
30+
new Pair<>("Nico", false),
31+
new Pair<>("Núria", false)
32+
)
33+
),
34+
new AbstractMap.SimpleImmutableEntry<>(
35+
2017,
36+
List.of(new Pair<>("Lerele", false))
2437
)
2538
);
2639
private final int yearToCalculate;
@@ -82,7 +95,7 @@ private boolean hasToIncreaseOneExtraPoint() {
8295

8396
for (Pair<String, Boolean> teacher : teachers) {
8497
Boolean isBenevolent = teacher.second();
85-
if (isBenevolent) {
98+
if (isBenevolent && yearToCalculate % 2 == 0 || teacher.first().equals("Núria")) {
8699
return true;
87100
}
88101
}

examples/java/java-student_grades-05_simplify_conditionals/src/test/java/tv/codely/student_grades/StudentGradeCalculatorShould.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void not_increase_one_extra_point_if_there_is_not_any_benevolent_teacher_in_the_
163163
}
164164

165165
@Test
166-
void increase_one_extra_point_if_there_is_any_benevolent_teacher_in_the_year_to_calculate_grades() {
166+
void increase_one_extra_point_if_there_is_any_benevolent_teacher_and_the_year_to_calculate_grades_is_even() {
167167
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2020);
168168

169169
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 5f));
@@ -181,4 +181,24 @@ void maintain_10_as_the_maximum_grade_even_if_increasing_one_extra_point() {
181181

182182
assertEquals(10, studentGradeCalculator.calculateGrades(examsGrades, hasReachedMinimumClasses));
183183
}
184+
185+
@Test
186+
void do_not_increase_one_extra_point_if_there_is_any_benevolent_teacher_but_the_year_to_calculate_grades_is_odd() {
187+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2017);
188+
189+
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 5f));
190+
final boolean hasReachedMinimumClasses = true;
191+
192+
assertEquals(5, studentGradeCalculator.calculateGrades(examsGrades, hasReachedMinimumClasses));
193+
}
194+
195+
@Test
196+
void increase_one_extra_point_if_there_is_not_any_benevolent_teacher_but_someone_is_called_nuria() {
197+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2018);
198+
199+
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 5f));
200+
final boolean hasReachedMinimumClasses = true;
201+
202+
assertEquals(6, studentGradeCalculator.calculateGrades(examsGrades, hasReachedMinimumClasses));
203+
}
184204
}

0 commit comments

Comments
 (0)