Skip to content

Commit 51bb022

Browse files
committed
Benevolent teachers can decide if we increase one extra point to all studens (code logic in order to be extracted in its own method and illustrate complex if conditions)
1 parent ea56513 commit 51bb022

File tree

2 files changed

+95
-20
lines changed

2 files changed

+95
-20
lines changed

examples/java/java-student_grades-01_base_example/src/main/java/tv/codely/student_grades/StudentGradeCalculator.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,55 @@
11
package tv.codely.student_grades;
22

3+
import java.util.AbstractMap;
34
import java.util.List;
45
import java.util.Map;
56

67
public class StudentGradeCalculator {
7-
public Float calculateGrades(final List<Pair<Integer, Float>> examsGrades, final boolean hasReachedMinimumClasses) {
8+
final private Map<Integer, List<Pair<String, Boolean>>> allYearsTeachers = Map.ofEntries(
9+
new AbstractMap.SimpleImmutableEntry<>(
10+
2020,
11+
List.of(
12+
new Pair<>("Josefina", true),
13+
new Pair<>("Edonisio", true),
14+
new Pair<>("Edufasio", false)
15+
)
16+
),
17+
new AbstractMap.SimpleImmutableEntry<>(
18+
2019,
19+
List.of(
20+
new Pair<>("Eduarda", false),
21+
new Pair<>("Abelardo", false),
22+
new Pair<>("Francisca", false)
23+
)
24+
)
25+
);
26+
private final int yearToCalculate;
27+
28+
public StudentGradeCalculator(final int yearToCalculate) {
29+
this.yearToCalculate = yearToCalculate;
30+
}
31+
32+
public float calculateGrades(final List<Pair<Integer, Float>> examsGrades, final boolean hasReachedMinimumClasses) {
833
if (!examsGrades.isEmpty()) {
9-
Float gradesSum = 0f;
10-
Integer gradesWeightSum = 0;
34+
boolean hasToIncreaseOneExtraPoint = false;
35+
36+
for (Map.Entry<Integer, List<Pair<String, Boolean>>> yearlyTeachers : allYearsTeachers.entrySet()) {
37+
if (!(yearToCalculate != yearlyTeachers.getKey())) {
38+
List<Pair<String, Boolean>> teachers = yearlyTeachers.getValue();
39+
40+
for (Pair<String, Boolean> teacher : teachers) {
41+
if (teacher.second() != true) {
42+
continue;
43+
}
44+
hasToIncreaseOneExtraPoint = true;
45+
}
46+
} else {
47+
continue;
48+
}
49+
}
50+
51+
float gradesSum = 0f;
52+
int gradesWeightSum = 0;
1153

1254
for (Pair<Integer, Float> examGrade : examsGrades) {
1355
gradesSum += (examGrade.first() * examGrade.second() / 100);
@@ -16,7 +58,11 @@ public Float calculateGrades(final List<Pair<Integer, Float>> examsGrades, final
1658

1759
if (gradesWeightSum == 100) {
1860
if (hasReachedMinimumClasses) {
19-
return gradesSum;
61+
if (hasToIncreaseOneExtraPoint) {
62+
return Float.min(10f, gradesSum + 1);
63+
} else {
64+
return gradesSum;
65+
}
2066
} else {
2167
return 0f;
2268
}

examples/java/java-student_grades-01_base_example/src/test/java/tv/codely/student_grades/StudentGradeCalculatorShould.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class StudentGradeCalculatorShould {
1111
@Test
1212
void fail_given_there_are_no_exams() {
13-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
13+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
1414

1515
final List<Pair<Integer, Float>> examsGrades = Collections.emptyList();
1616
final boolean hasReachedMinimumClasses = true;
@@ -20,7 +20,7 @@ void fail_given_there_are_no_exams() {
2020

2121
@Test
2222
void calculate_same_grade_given_one_single_exam_and_attending_the_minimum_classes() {
23-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
23+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
2424

2525
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 5f));
2626
final boolean hasReachedMinimumClasses = true;
@@ -30,7 +30,7 @@ void calculate_same_grade_given_one_single_exam_and_attending_the_minimum_classe
3030

3131
@Test
3232
void calculate_average_grade_given_different_exam_grades_and_attending_the_minimum_classes() {
33-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
33+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
3434

3535
final List<Pair<Integer, Float>> examsGrades = List.of(
3636
new Pair<>(10, 4f),
@@ -51,7 +51,7 @@ void calculate_average_grade_given_different_exam_grades_and_attending_the_minim
5151

5252
@Test
5353
void round_up_to_2_decimals_given_odd_exam_grades_and_attending_the_minimum_classes() {
54-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
54+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
5555

5656
final List<Pair<Integer, Float>> examsGrades = List.of(
5757
new Pair<>(50, 4f),
@@ -66,7 +66,7 @@ void round_up_to_2_decimals_given_odd_exam_grades_and_attending_the_minimum_clas
6666

6767
@Test
6868
void fail_when_there_are_no_exams_and_has_not_attended_the_minimum_classes() {
69-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
69+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
7070

7171
final List<Pair<Integer, Float>> examsGrades = Collections.emptyList();
7272
final boolean hasReachedMinimumClasses = false;
@@ -76,7 +76,7 @@ void fail_when_there_are_no_exams_and_has_not_attended_the_minimum_classes() {
7676

7777
@Test
7878
void fail_given_one_single_exam_but_not_attending_the_minimum_classes() {
79-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
79+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
8080

8181
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 5f));
8282
final boolean hasReachedMinimumClasses = false;
@@ -86,7 +86,7 @@ void fail_given_one_single_exam_but_not_attending_the_minimum_classes() {
8686

8787
@Test
8888
void fail_given_different_exam_grades_but_not_attending_the_minimum_classes() {
89-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
89+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
9090

9191
final List<Pair<Integer, Float>> examsGrades = List.of(
9292
new Pair<>(10, 4f),
@@ -106,14 +106,11 @@ void fail_given_different_exam_grades_but_not_attending_the_minimum_classes() {
106106
}
107107

108108
@Test
109-
void fail_given_odd_exam_grades_but_not_attending_the_minimum_classes() {
110-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
109+
void fail_given_odd_exam_grades_but_attending_the_minimum_classes() {
110+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
111111

112-
final List<Pair<Integer, Float>> examsGrades = List.of(
113-
new Pair<>(50, 5f),
114-
new Pair<>(50, 4f)
115-
);
116-
final boolean hasReachedMinimumClasses = false;
112+
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 5f));
113+
final boolean hasReachedMinimumClasses = false;
117114

118115
assertEquals(0, studentGradeCalculator.calculateGrades(examsGrades, hasReachedMinimumClasses));
119116
}
@@ -122,7 +119,7 @@ void fail_given_odd_exam_grades_but_not_attending_the_minimum_classes() {
122119

123120
@Test
124121
void validate_all_exam_grades_weight_below_100() {
125-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
122+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
126123

127124
final List<Pair<Integer, Float>> examsGrades = List.of(
128125
new Pair<>(10, 4f),
@@ -135,7 +132,7 @@ void validate_all_exam_grades_weight_below_100() {
135132

136133
@Test
137134
void validate_all_exam_grades_weight_over_100() {
138-
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator();
135+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
139136

140137
final List<Pair<Integer, Float>> examsGrades = List.of(
141138
new Pair<>(90, 4f),
@@ -145,4 +142,36 @@ void validate_all_exam_grades_weight_over_100() {
145142

146143
assertEquals(-1, studentGradeCalculator.calculateGrades(examsGrades, hasReachedMinimumClasses));
147144
}
145+
146+
// hasToRaiseOnePoint
147+
148+
@Test
149+
void not_increase_one_extra_point_if_there_is_not_any_benevolent_teacher_in_the_year_to_calculate_grades() {
150+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2019);
151+
152+
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 9.8f));
153+
final boolean hasReachedMinimumClasses = true;
154+
155+
assertEquals(9.8f, studentGradeCalculator.calculateGrades(examsGrades, hasReachedMinimumClasses));
156+
}
157+
158+
@Test
159+
void increase_one_extra_point_if_there_is_any_benevolent_teacher_in_the_year_to_calculate_grades() {
160+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2020);
161+
162+
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 5f));
163+
final boolean hasReachedMinimumClasses = true;
164+
165+
assertEquals(6, studentGradeCalculator.calculateGrades(examsGrades, hasReachedMinimumClasses));
166+
}
167+
168+
@Test
169+
void maintain_10_as_the_maximum_grade_even_if_increasing_one_extra_point() {
170+
StudentGradeCalculator studentGradeCalculator = new StudentGradeCalculator(2020);
171+
172+
final List<Pair<Integer, Float>> examsGrades = List.of(new Pair<>(100, 9.8f));
173+
final boolean hasReachedMinimumClasses = true;
174+
175+
assertEquals(10, studentGradeCalculator.calculateGrades(examsGrades, hasReachedMinimumClasses));
176+
}
148177
}

0 commit comments

Comments
 (0)