From 609b56a964b5733b27c06e1a68e75deac49b3697 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 27 Nov 2016 18:10:18 +0100 Subject: [PATCH] Removing duplications. We need to map X and V to numbers. We achive it by means of an enum. --- .../romannumerals/RomanNumeralGenerator.java | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) 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; + } + } } -- 2.1.4