Commit b1321304 by Paktalin

Grading strategy refactoring in Lesson 6

parent 635ac868
...@@ -4,15 +4,9 @@ package com.example.paktalin.agilejava; ...@@ -4,15 +4,9 @@ package com.example.paktalin.agilejava;
* Created by Paktalin on 18/05/2018. * Created by Paktalin on 18/05/2018.
*/ */
class BasicGradingStrategy implements GradingStrategy { class BasicGradingStrategy {
@Override
public int getGradePointsFor(Student.Grade grade) { public int getGradePointsFor(Student.Grade grade) {
switch(grade) { return grade.getPoints();
case A: return 4;
case B: return 3;
case C: return 2;
case D: return 1;
default: return 0;
}
} }
} }
package com.example.paktalin.agilejava;
/**
* Created by Paktalin on 18/05/2018.
*/
interface GradingStrategy {
int getGradePointsFor(Student.Grade grade);
}
...@@ -4,10 +4,11 @@ package com.example.paktalin.agilejava; ...@@ -4,10 +4,11 @@ package com.example.paktalin.agilejava;
* Created by Paktalin on 18/05/2018. * Created by Paktalin on 18/05/2018.
*/ */
class HonorsGradingStrategy implements GradingStrategy { class HonorsGradingStrategy extends BasicGradingStrategy {
@Override @Override
public int getGradePointsFor(Student.Grade grade) { public int getGradePointsFor(Student.Grade grade) {
int points = new BasicGradingStrategy().getGradePointsFor(grade); int points = super.getGradePointsFor(grade);
if (points != 0) if (points != 0)
points++; points++;
return points; return points;
......
...@@ -14,9 +14,18 @@ class Student { ...@@ -14,9 +14,18 @@ class Student {
static final String IN_STATE = "CO"; static final String IN_STATE = "CO";
private String state = ""; private String state = "";
private List<Grade> grades = new ArrayList<>(); private List<Grade> grades = new ArrayList<>();
private GradingStrategy gradingStrategy = new BasicGradingStrategy(); private BasicGradingStrategy gradingStrategy = new BasicGradingStrategy();
enum Grade {A, B, C, D, F}; enum Grade {
A(4), B(3), C(2), D(1), F(0);
private int points;
Grade(int points) {
this.points = points;
}
int getPoints() {
return points;
}
}
Student(String name) { Student(String name) {
this.name = name; this.name = name;
......
...@@ -17,6 +17,8 @@ public class AllTests extends TestSuite { ...@@ -17,6 +17,8 @@ public class AllTests extends TestSuite {
suite.addTestSuite(CourseReportTest.class); suite.addTestSuite(CourseReportTest.class);
suite.addTestSuite(LanguageTests.class); suite.addTestSuite(LanguageTests.class);
suite.addTestSuite(ReportCardTest.class); suite.addTestSuite(ReportCardTest.class);
suite.addTestSuite(BasicGradingStrategyTest.class);
suite.addTestSuite(HonorsGradingStrategyTest.class);
return suite; return suite;
} }
} }
package com.example.paktalin.agilejava;
import junit.framework.TestCase;
/**
* Created by Paktalin on 28/05/2018.
*/
public class BasicGradingStrategyTest extends TestCase {
public void testGetGradePoints() {
BasicGradingStrategy strategy = new BasicGradingStrategy();
assertEquals(4, strategy.getGradePointsFor(Student.Grade.A));
assertEquals(3, strategy.getGradePointsFor(Student.Grade.B));
assertEquals(2, strategy.getGradePointsFor(Student.Grade.C));
assertEquals(1, strategy.getGradePointsFor(Student.Grade.D));
assertEquals(0, strategy.getGradePointsFor(Student.Grade.F));
}
}
package com.example.paktalin.agilejava;
import junit.framework.TestCase;
/**
* Created by Paktalin on 28/05/2018.
*/
public class HonorsGradingStrategyTest extends TestCase {
public void testGetGradePoints() {
HonorsGradingStrategy strategy = new HonorsGradingStrategy();
assertEquals(5, strategy.getGradePointsFor(Student.Grade.A));
assertEquals(4, strategy.getGradePointsFor(Student.Grade.B));
assertEquals(3, strategy.getGradePointsFor(Student.Grade.C));
assertEquals(2, strategy.getGradePointsFor(Student.Grade.D));
assertEquals(0, strategy.getGradePointsFor(Student.Grade.F));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment