Commit 049abcfb authored by Colin DAMON's avatar Colin DAMON
Browse files

Use conversion map for conversions

parent 435dc53b
package fr.ippon.kata.numeral; package fr.ippon.kata.numeral;
import java.util.Map.Entry;
import java.util.NavigableMap; import java.util.NavigableMap;
import java.util.TreeMap; import java.util.TreeMap;
...@@ -7,15 +8,12 @@ public class Numerals { ...@@ -7,15 +8,12 @@ public class Numerals {
private static final NavigableMap<Integer, String> CONVERSIONS = buildConversions(); private static final NavigableMap<Integer, String> CONVERSIONS = buildConversions();
public static String toRoman(int arabic) { public static String toRoman(int arabic) {
if (arabic >= 5) { if (arabic == 0) {
return "V" + toRoman(arabic - 5); return "";
} }
if (arabic == 4) { Entry<Integer, String> highestKnownConversion = CONVERSIONS.floorEntry(arabic);
return "IV"; return highestKnownConversion.getValue() + toRoman(arabic - highestKnownConversion.getKey());
}
return "I".repeat(arabic);
} }
private static NavigableMap<Integer, String> buildConversions() { private static NavigableMap<Integer, String> buildConversions() {
......
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 to comment