diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..43665df 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,96 @@ public class RomanNumerals { public int convertToInteger(String romanNum) { - // To be Implemented - return 0; + // total num + // -> return this + int num = 0; + + // to avoid input errors + // just upper case everything + romanNum = romanNum.toUpperCase(); + + // save the string to character array. + char[] charArr = romanNum.toCharArray(); + + // loop through the whole array. + for(int i = 0; i < romanNum.length(); i++) { + + // current character + char c = charArr[i]; + + switch(c) { + + // 1 + case 'I': + try { + char next_c = charArr[i+1]; + + if(next_c == 'V' || next_c == 'X') { + num -= 1; + } else { + num += 1; + } + } catch (ArrayIndexOutOfBoundsException e) { + num += 1; + } + break; + + // 10 + case 'X': + try { + char next_c = charArr[i+1]; + + if(next_c == 'L' || next_c == 'C') { + num -= 10; + } else { + num += 10; + } + } catch (ArrayIndexOutOfBoundsException e) { + num += 10; + } + break; + + // 100 + case 'C': + try { + char next_c = charArr[i+1]; + + if(next_c == 'D' || next_c == 'M') { + num -= 100; + } else { + num += 100; + } + } catch (ArrayIndexOutOfBoundsException e) { + num += 100; + } + break; + + // 1000 + case 'M': + num += 1000; + break; + + // 5 + case 'V': + num += 5; + break; + + // 50 + case 'L': + num += 50; + break; + + // 500 + case 'D': + num += 500; + break; + + // invalid + default: + break; + } + } + return num; } } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..d416acb 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -4,9 +4,91 @@ public class TestRomanNumerals { + RomanNumerals rn = new RomanNumerals(); + @Test - public void test() { - fail("Not yet implemented"); + public void test_RomanNumerals_notNull() { + assertNotNull(rn); } + @Test + public void test_RomanNumerals_return_one() { + assertEquals(1, rn.convertToInteger("i")); + } + + @Test + public void test_RomanNumerals_return_two() { + assertEquals(2, rn.convertToInteger("ii")); + } + + @Test + public void test_RomanNumerals_return_three() { + assertEquals(3, rn.convertToInteger("iii")); + } + + @Test + public void test_RomanNumerals_return_four() { + assertEquals(4, rn.convertToInteger("iv")); + } + + @Test + public void test_RomanNumerals_return_five() { + assertEquals(5, rn.convertToInteger("v")); + } + + @Test + public void test_RomanNumerals_return_six() { + assertEquals(6, rn.convertToInteger("vi")); + } + + @Test + public void test_RomanNumerals_return_seven() { + assertEquals(7, rn.convertToInteger("vii")); + } + + @Test + public void test_RomanNumerals_return_eight() { + assertEquals(8, rn.convertToInteger("viii")); + } + + @Test + public void test_RomanNumerals_return_nine() { + assertEquals(9, rn.convertToInteger("ix")); + } + + @Test + public void test_RomanNumerals_return_ten() { + assertEquals(10, rn.convertToInteger("x")); + } + + @Test + public void test_RomanNumerals_return_nineteen() { + assertEquals(19, rn.convertToInteger("ixx")); + } + + @Test + public void test_RomanNumerals_return_twenty() { + assertEquals(20, rn.convertToInteger("xx")); + } + + @Test + public void test_RomanNumerals_return_ninetyNine() { + assertEquals(99, rn.convertToInteger("XCIX")); + } + + @Test + public void test_RomanNumerals_return_999() { + assertEquals(999, rn.convertToInteger("CMXCIX")); + } + + @Test + public void test_RomanNumerals_return_1984() { + assertEquals(1984, rn.convertToInteger("mcmlxxxiv")); + } + + @Test + public void test_RomanNumerals_return_2014() { + assertEquals(2014, rn.convertToInteger("mmxiv")); + } + }