From: Gustavo Martin Morcuende Date: Sun, 27 Nov 2016 17:10:18 +0000 (+0100) Subject: Removing duplications. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=609b56a964b5733b27c06e1a68e75deac49b3697;p=JavaForFun Removing duplications. We need to map X and V to numbers. We achive it by means of an enum. --- diff --git a/TDD/sandromancuso/romannumerals/src/main/java/org/craftedsw/romannumerals/RomanNumeralGenerator.java b/TDD/sandromancuso/romannumerals/src/main/java/org/craftedsw/romannumerals/RomanNumeralGenerator.java index c9ebc6a..18f422e 100644 --- a/TDD/sandromancuso/romannumerals/src/main/java/org/craftedsw/romannumerals/RomanNumeralGenerator.java +++ b/TDD/sandromancuso/romannumerals/src/main/java/org/craftedsw/romannumerals/RomanNumeralGenerator.java @@ -4,13 +4,11 @@ public class RomanNumeralGenerator { public static String romanFor(int decimal) { String roman = ""; - if (decimal >= 10) { - roman += "X"; - decimal -= 10; - } - if (decimal >= 5) { - roman += "V"; - decimal -= 5; + for (RomanToDecimal romanToDecimal : RomanToDecimal.values()) { + if (decimal >= romanToDecimal.decimal) { + roman += romanToDecimal.roman; + decimal -= romanToDecimal.decimal; + } } for (int i = 0; i < decimal; i++) { roman += "I"; @@ -19,4 +17,16 @@ public class RomanNumeralGenerator { return roman; } + enum RomanToDecimal { + TEN("X", 10), + FIVE("V", 5); + + private final String roman; + private final int decimal; + + private RomanToDecimal(String roman, int decimal) { + this.roman = roman; + this.decimal = decimal; + } + } }