Skip to content

Commit 3320955

Browse files
committed
[js][student_grades][extract_subclass] Refactor to extract subclass
1 parent e065b1b commit 3320955

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {StudentGradeCalculator} from "./StudentGradeCalculator";
2+
3+
export class StudentGrade2019Calculator extends StudentGradeCalculator {
4+
thisYearTeachers() {
5+
return [
6+
["Eduarda", false],
7+
["Abelardo", false],
8+
["Francisca", false],
9+
];
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {StudentGradeCalculator} from "./StudentGradeCalculator";
2+
3+
export class StudentGrade2020Calculator extends StudentGradeCalculator {
4+
thisYearTeachers() {
5+
return [
6+
["Josefina", true],
7+
["Edonisio", true],
8+
["Edufasio", false],
9+
];
10+
}
11+
}

examples/js/js-student_grades-02_extract_subclass/src/StudentGradeCalculator.js

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
export class StudentGradeCalculator {
2-
allYearsTeachers = {
3-
2020: [
4-
["Josefina", true],
5-
["Edonisio", true],
6-
["Edufasio", false],
7-
],
8-
2019: [
9-
["Eduarda", false],
10-
["Abelardo", false],
11-
["Francisca", false],
12-
]
13-
};
14-
15-
constructor(yearToCalculate) {
16-
this.yearToCalculate = yearToCalculate;
2+
thisYearTeachers() {
3+
throw 'This method should be implemented';
174
}
185

196
calculate(examGrades, hasReachedMinimumClasses) {
207
if (examGrades.length !== 0) {
218
let hasToIncreaseOneExtraPoint = false;
22-
23-
for (let [year, teachers] of Object.entries(this.allYearsTeachers)) {
24-
if (!(this.yearToCalculate != year)) {
25-
for (let teacher of teachers) {
26-
if (teacher[1] != true) {
27-
continue;
28-
}
29-
30-
hasToIncreaseOneExtraPoint = true;
31-
}
32-
} else {
9+
10+
for (let teacher of this.thisYearTeachers()) {
11+
if (teacher[1] != true) {
3312
continue;
3413
}
14+
15+
hasToIncreaseOneExtraPoint = true;
3516
}
17+
3618

3719
let gradesSum = 0;
3820
let gradesWeightSum = 0;

examples/js/js-student_grades-02_extract_subclass/tests/StudentGradeCalculator.test.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {StudentGradeCalculator} from '../src/StudentGradeCalculator';
1+
import {StudentGrade2019Calculator} from "../src/StudentGrade2019Calculator";
2+
import {StudentGrade2020Calculator} from "../src/StudentGrade2020Calculator";
23

34
describe('StudentGradeCalculator should', () => {
45
it('fail given there are no exams', () => {
5-
const calculator = new StudentGradeCalculator(2019);
6+
const calculator = new StudentGrade2019Calculator();
67

78
const examsGrades = [];
89
const hasReachedMinimumClasses = true;
@@ -11,7 +12,7 @@ describe('StudentGradeCalculator should', () => {
1112
});
1213

1314
it('calculate same grade given one single exam and attending the minimum classes', () => {
14-
const calculator = new StudentGradeCalculator(2019);
15+
const calculator = new StudentGrade2019Calculator();
1516

1617
const examsGrades = [[100, 5]];
1718
const hasReachedMinimumClasses = true;
@@ -20,7 +21,7 @@ describe('StudentGradeCalculator should', () => {
2021
});
2122

2223
it('calculate average grade given different exam grades and attending the minimum classes', () => {
23-
const calculator = new StudentGradeCalculator(2019);
24+
const calculator = new StudentGrade2019Calculator();
2425

2526
const examsGrades = [
2627
[10, 4],
@@ -40,7 +41,7 @@ describe('StudentGradeCalculator should', () => {
4041
});
4142

4243
it('round up to 2 decimals given odd exam grades and attending the minimum classes', () => {
43-
const calculator = new StudentGradeCalculator(2019);
44+
const calculator = new StudentGrade2019Calculator();
4445

4546
const examsGrades = [
4647
[50, 4],
@@ -54,7 +55,7 @@ describe('StudentGradeCalculator should', () => {
5455
// hasReachedMinimumClasses
5556

5657
it('fail when there are no exams and has not attended the minimum classes', () => {
57-
const calculator = new StudentGradeCalculator(2019);
58+
const calculator = new StudentGrade2019Calculator();
5859

5960
const examsGrades = [];
6061
const hasReachedMinimumClasses = false;
@@ -63,7 +64,7 @@ describe('StudentGradeCalculator should', () => {
6364
});
6465

6566
it('fail given one single exam but not attending the minimum classes', () => {
66-
const calculator = new StudentGradeCalculator(2019);
67+
const calculator = new StudentGrade2019Calculator();
6768

6869
const examsGrades = [[100, 5]];
6970
const hasReachedMinimumClasses = false;
@@ -72,7 +73,7 @@ describe('StudentGradeCalculator should', () => {
7273
});
7374

7475
it('fail given different exam grades but not attending the minimum classes', () => {
75-
const calculator = new StudentGradeCalculator(2019);
76+
const calculator = new StudentGrade2019Calculator();
7677

7778
const examsGrades = [
7879
[10, 4],
@@ -92,7 +93,7 @@ describe('StudentGradeCalculator should', () => {
9293
});
9394

9495
it('fail given odd exam grades but not attending the minimum classes', () => {
95-
const calculator = new StudentGradeCalculator(2019);
96+
const calculator = new StudentGrade2019Calculator();
9697

9798
const examsGrades = [[50, 5], [50, 4]];
9899
const hasReachedMinimumClasses = false;
@@ -103,7 +104,7 @@ describe('StudentGradeCalculator should', () => {
103104
// Weight
104105

105106
it('validate all exam grades weight below 100', () => {
106-
const calculator = new StudentGradeCalculator(2019);
107+
const calculator = new StudentGrade2019Calculator();
107108

108109
const examsGrades = [
109110
[10, 4],
@@ -115,7 +116,7 @@ describe('StudentGradeCalculator should', () => {
115116
});
116117

117118
it('validate all exam grades weight over 100', () => {
118-
const calculator = new StudentGradeCalculator(2019);
119+
const calculator = new StudentGrade2019Calculator();
119120

120121
const examsGrades = [
121122
[90, 4],
@@ -129,16 +130,16 @@ describe('StudentGradeCalculator should', () => {
129130
// hasToRaiseOnePoint
130131

131132
it('not increase one extra point if there is not any benevolent teacher in the year to calculate grades', () => {
132-
const calculator = new StudentGradeCalculator(2019);
133+
const calculator = new StudentGrade2019Calculator();
133134

134135
const examsGrades = [[100, 9.8]];
135136
const hasReachedMinimumClasses = true;
136137

137138
expect(calculator.calculate(examsGrades, hasReachedMinimumClasses)).toBe(9.8);
138139
});
139-
140+
140141
it('increase one extra point if there is any benevolent teacher in the year to calculate grades', () => {
141-
const calculator = new StudentGradeCalculator(2020);
142+
const calculator = new StudentGrade2020Calculator();
142143

143144
const examsGrades = [[100, 5]];
144145
const hasReachedMinimumClasses = true;
@@ -147,7 +148,7 @@ describe('StudentGradeCalculator should', () => {
147148
});
148149

149150
it('maintain 10 as the maximum grade even if increasing one extra point', () => {
150-
const calculator = new StudentGradeCalculator(2020);
151+
const calculator = new StudentGrade2020Calculator();
151152

152153
const examsGrades = [[100, 9.8]];
153154
const hasReachedMinimumClasses = true;

0 commit comments

Comments
 (0)