Created
March 25, 2016 10:11
-
-
Save j4cksw/9f2c9242619257046107 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example; | |
| import java.util.LinkedHashMap; | |
| import java.util.Map; | |
| public class GradeConverter { | |
| public String fromScore(Integer score) { | |
| Map<Integer, String> rules = getConversionRules(); | |
| String grade = "F"; | |
| for( Map.Entry<Integer, String> entry: rules.entrySet()) { | |
| if ( score >= entry.getKey()){ | |
| grade = entry.getValue(); | |
| break; | |
| } | |
| } | |
| return grade; | |
| } | |
| private Map<Integer, String> getConversionRules() { | |
| Map<Integer, String> rules = new LinkedHashMap<>(); | |
| rules.put(80, "A"); | |
| rules.put(70, "B"); | |
| rules.put(60, "C"); | |
| rules.put(50, "D"); | |
| return rules; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.example.GradeConverter; | |
| import org.junit.Assert; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| public class GradeConverterTest { | |
| private GradeConverter converter; | |
| @Before | |
| public void setUp() throws Exception { | |
| converter = new GradeConverter(); | |
| } | |
| @Test | |
| public void fromScore_given_90_shouldReturn_A() { | |
| Assert.assertEquals(converter.fromScore(90), "A"); | |
| } | |
| @Test | |
| public void fromScore_given_0_shouldReturn_F() { | |
| Assert.assertEquals(converter.fromScore(0), "F"); | |
| } | |
| @Test | |
| public void fromScore_given_50_shouldReturn_D() { | |
| Assert.assertEquals(converter.fromScore(50), "D"); | |
| } | |
| @Test | |
| public void fromScore_given_60_shouldReturn_C() { | |
| Assert.assertEquals(converter.fromScore(60), "C"); | |
| } | |
| @Test | |
| public void fromScore_given_70_shouldReturn_B() { | |
| Assert.assertEquals(converter.fromScore(70), "B"); | |
| } | |
| @Test | |
| public void fromScore_given_80_shouldReturn_A() { | |
| Assert.assertEquals(converter.fromScore(80), "A"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment