School grading string calculator 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&lt;Boolean, Grade&gt; grades = new TreeMap&lt;&gt;();
    grades.put(grade &lt;= 50, Grade.F);
    grades.put(grade &gt;= 51 &amp;&amp; grade &lt;= 60, Grade.D);
    grades.put(grade &gt;= 61 &amp;&amp; grade &lt;= 70, Grade.C);
    grades.put(grade &gt;= 71 &amp;&amp; grade &lt;= 80, Grade.B);
    grades.put(grade &gt; 80 &amp;&amp; grade &lt;= 100, Grade.A);

    for (Map.Entry&lt;Boolean, Grade&gt; 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?
What stands out to me is that you initially perform a check against every grade range and insert true into the map in the right one. By that point, you've already found which grade is right. But then you initiate another loop to start searching for the grade corresponding to true.

I think instead, you ought to alter your method to output a Grade enum, and delegate the string handling to another function. I feel that it's better practice to return a Grade Enum from the first method because of the strong typing-- you may want to use this information for other methods besides finding the grade name. In fact, Enums are particularly nice because you can use a switch on them.

public Grade gradeBy(int grade) {
    if (grade &lt; 50) return Grade.F;
    if (grade &lt; 61) return Grade.D;
    if (grade &lt; 71) return Grade.C;
    if (grade &lt; 81) return Grade.B;
    if (grade&lt;=100) return Grade.A;
    return Grade.F;
}
public String gradeName(Grade g) {
    return g.name()
}


Notice how as soon as the grade matches the range corresponding to something in the Grade Enum, the method immediately returns. Now an example of how you might use that switch:

public String gradeComment(Grade g) {
    switch(g) {
        case F:
        return "You need to work harder!";
        case D:
        return "I think you can do better.";
        case C:
        return "Satisfactory.";
        case B:
        return "Nice job.";
        case A:
        return "Well done!";
        default:
        return "N/A";
    }
}


Not that you need these grade messages, but that's an example of the power of holding on to the Enum type rather than converting the int grade directly into a String.

Sorry if I've made syntax errors as I've not used Java in a while.

Edit: Just realised that you might like to put gradeBy, gradeName and gradeComment into the Grade Enum itself as static methods.