For my training, i've been given an assignment to build a simple, extendable 'grade' String calculator by a given grade.
For now, I had to work by this table:
When the grade was higher then 100, I had to return the String "You must have been cheating".
What I currently came up with is:
public String gradeBy(int grade) {
TreeMap<Boolean, Grade> grades = new TreeMap<>();
grades.put(grade <= 50, Grade.F);
grades.put(grade >= 51 && grade <= 60, Grade.D);
grades.put(grade >= 61 && grade <= 70, Grade.C);
grades.put(grade >= 71 && grade <= 80, Grade.B);
grades.put(grade > 80 && grade <= 100, Grade.A);
for (Map.Entry<Boolean, Grade> currentGrade : grades.entrySet()) {
if (currentGrade.getKey() != true) {
continue;
}
return currentGrade.getValue().name();
}
return "You must have been cheating";
}
enum Grade {
F,
D,
C,
B,
A
}
Where I test it as:
@Test
public void gradeBy() throws Exception {
int[] grades = {45, 52, 66, 93, 102};
for(int grade : grades) {
String gradeString = this.grading.gradeBy(grade);
System.out.println(grade + "gives :"+ gradeString);
}
}
Can this be improved in a more OOP way? Can the grading list be apart from the grading system it self?
