@Test public void testToDecimal() { Assert.assertEquals(1L, ConversionRulesEngine.toDecimal("I")); Assert.assertEquals(2L, ConversionRulesEngine.toDecimal("II")); Assert.assertEquals(3L, ConversionRulesEngine.toDecimal("III")); Assert.assertEquals(4L, ConversionRulesEngine.toDecimal("IV")); Assert.assertEquals(5L, ConversionRulesEngine.toDecimal("V")); Assert.assertEquals(6L, ConversionRulesEngine.toDecimal("VI")); Assert.assertEquals(7L, ConversionRulesEngine.toDecimal("VII")); Assert.assertEquals(8L, ConversionRulesEngine.toDecimal("VIII")); Assert.assertEquals(9L, ConversionRulesEngine.toDecimal("IX")); Assert.assertEquals(10L, ConversionRulesEngine.toDecimal("X")); Assert.assertEquals(11L, ConversionRulesEngine.toDecimal("XI")); Assert.assertEquals(12L, ConversionRulesEngine.toDecimal("XII")); Assert.assertEquals(13L, ConversionRulesEngine.toDecimal("XIII")); Assert.assertEquals(14L, ConversionRulesEngine.toDecimal("XIV")); Assert.assertEquals(15L, ConversionRulesEngine.toDecimal("XV")); Assert.assertEquals(1944L, ConversionRulesEngine.toDecimal("MCMXLIV")); Assert.assertEquals(1910L, ConversionRulesEngine.toDecimal("MDCCCCX")); Assert.assertEquals(1954L, ConversionRulesEngine.toDecimal("MCMLIV")); Assert.assertEquals(1990L, ConversionRulesEngine.toDecimal("MCMXC")); }
public static long toDecimal(final String romanNumber) { final int len = romanNumber.length(); int difference = 0; long decimalNumber = 0L; char currentChar = '\u0000'; char lookAheadChar = '\u0000'; for (int index = 0; index < len;) { currentChar = Character.toUpperCase(romanNumber.charAt(index)); if (index != (len - 1) && subtractionMap.containsKey(currentChar)) { lookAheadChar = Character.toUpperCase(romanNumber .charAt(index + 1)); if (subtractionMap.get(currentChar).contains(lookAheadChar)) { difference = getDecimalValue(lookAheadChar) - getDecimalValue(currentChar); decimalNumber += difference; index += 2; } else { decimalNumber += getDecimalValue(currentChar); index++; } } else { decimalNumber += getDecimalValue(currentChar); index++; } } return decimalNumber; }
ConversionRulesEngine { public static long toDecimal(final String romanNumber) { final int len = romanNumber.length(); int difference = 0; long decimalNumber = 0L; char currentChar = '\u0000'; char lookAheadChar = '\u0000'; for (int index = 0; index < len;) { currentChar = Character.toUpperCase(romanNumber.charAt(index)); if (index != (len - 1) && subtractionMap.containsKey(currentChar)) { lookAheadChar = Character.toUpperCase(romanNumber .charAt(index + 1)); if (subtractionMap.get(currentChar).contains(lookAheadChar)) { difference = getDecimalValue(lookAheadChar) - getDecimalValue(currentChar); decimalNumber += difference; index += 2; } else { decimalNumber += getDecimalValue(currentChar); index++; } } else { decimalNumber += getDecimalValue(currentChar); index++; } } return decimalNumber; } }
ConversionRulesEngine { public static long toDecimal(final String romanNumber) { final int len = romanNumber.length(); int difference = 0; long decimalNumber = 0L; char currentChar = '\u0000'; char lookAheadChar = '\u0000'; for (int index = 0; index < len;) { currentChar = Character.toUpperCase(romanNumber.charAt(index)); if (index != (len - 1) && subtractionMap.containsKey(currentChar)) { lookAheadChar = Character.toUpperCase(romanNumber .charAt(index + 1)); if (subtractionMap.get(currentChar).contains(lookAheadChar)) { difference = getDecimalValue(lookAheadChar) - getDecimalValue(currentChar); decimalNumber += difference; index += 2; } else { decimalNumber += getDecimalValue(currentChar); index++; } } else { decimalNumber += getDecimalValue(currentChar); index++; } } return decimalNumber; }  }
ConversionRulesEngine { public static long toDecimal(final String romanNumber) { final int len = romanNumber.length(); int difference = 0; long decimalNumber = 0L; char currentChar = '\u0000'; char lookAheadChar = '\u0000'; for (int index = 0; index < len;) { currentChar = Character.toUpperCase(romanNumber.charAt(index)); if (index != (len - 1) && subtractionMap.containsKey(currentChar)) { lookAheadChar = Character.toUpperCase(romanNumber .charAt(index + 1)); if (subtractionMap.get(currentChar).contains(lookAheadChar)) { difference = getDecimalValue(lookAheadChar) - getDecimalValue(currentChar); decimalNumber += difference; index += 2; } else { decimalNumber += getDecimalValue(currentChar); index++; } } else { decimalNumber += getDecimalValue(currentChar); index++; } } return decimalNumber; }  static long toDecimal(final String romanNumber); }
ConversionRulesEngine { public static long toDecimal(final String romanNumber) { final int len = romanNumber.length(); int difference = 0; long decimalNumber = 0L; char currentChar = '\u0000'; char lookAheadChar = '\u0000'; for (int index = 0; index < len;) { currentChar = Character.toUpperCase(romanNumber.charAt(index)); if (index != (len - 1) && subtractionMap.containsKey(currentChar)) { lookAheadChar = Character.toUpperCase(romanNumber .charAt(index + 1)); if (subtractionMap.get(currentChar).contains(lookAheadChar)) { difference = getDecimalValue(lookAheadChar) - getDecimalValue(currentChar); decimalNumber += difference; index += 2; } else { decimalNumber += getDecimalValue(currentChar); index++; } } else { decimalNumber += getDecimalValue(currentChar); index++; } } return decimalNumber; }  static long toDecimal(final String romanNumber); static final Map<Character, List<Character>> subtractionMap; }