Commit b1321304 by Paktalin

Grading strategy refactoring in Lesson 6

parent 635ac868
......@@ -4,15 +4,9 @@ package com.example.paktalin.agilejava;
* Created by Paktalin on 18/05/2018.
*/
class BasicGradingStrategy implements GradingStrategy {
@Override
class BasicGradingStrategy {
public int getGradePointsFor(Student.Grade grade) {
switch(grade) {
case A: return 4;
case B: return 3;
case C: return 2;
case D: return 1;
default: return 0;
}
return grade.getPoints();
}
}
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;
* Created by Paktalin on 18/05/2018.
*/
class HonorsGradingStrategy implements GradingStrategy {
class HonorsGradingStrategy extends BasicGradingStrategy {
@Override
public int getGradePointsFor(Student.Grade grade) {
int points = new BasicGradingStrategy().getGradePointsFor(grade);
int points = super.getGradePointsFor(grade);
if (points != 0)
points++;
return points;
......
......@@ -14,9 +14,18 @@ class Student {
static final String IN_STATE = "CO";
private String state = "";
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) {
this.name = name;
......
......@@ -17,6 +17,8 @@ public class AllTests extends TestSuite {
suite.addTestSuite(CourseReportTest.class);
suite.addTestSuite(LanguageTests.class);
suite.addTestSuite(ReportCardTest.class);
suite.addTestSuite(BasicGradingStrategyTest.class);
suite.addTestSuite(HonorsGradingStrategyTest.class);
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