diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..54888f7 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,113 @@ +import java.util.ArrayList; public class RomanNumerals { - public int convertToInteger(String romanNum) { - // To be Implemented - return 0; + + public int convertToInteger(String romanNum) throws RomanNumeralsInvalidFormatException { + int i, res = 0, temp; + if (!invalidFormatCheck(romanNum)) { + throw new RomanNumeralsInvalidFormatException(); + } + + ArrayList splited = splitGroup(romanNum); + + if (!invalidSubtractCheck(splited)) { + throw new RomanNumeralsInvalidFormatException(); + } + + return calculateResult(splited); + } + + private int calculateResult(ArrayList splited) { + int i, res=0, temp; + int lastindex = splited.size() - 1; + int lastvalue = splited.get(lastindex); + for (i=lastindex;i>=0;i--) { + temp = splited.get(i); + if (temp >= lastvalue) { + res += temp; + } + else { + res -= temp; + } + lastvalue = temp; + } + return res; + } + + private ArrayList splitGroup(String romanNum) { + ArrayList result = new ArrayList(); + int i, count=0; + int current = getValue(romanNum.charAt(0)); + for (i=0;i romanList) { + int lastindex = romanList.size() - 1; + int lastvalue = romanList.get(lastindex); + int i, temp; + for (i=lastindex-1;i>=0;i--) { + temp = romanList.get(i); + if ((temp < lastvalue && (!isnotFive(temp))) || (temp*10 < lastvalue)) { + //only 1, 10, 100 allowed as subtractor + //I can be subtracted from maximum 10, X from 100, ... + return false; + } + } + return true; + } + + private boolean invalidFormatCheck(String romanNum) { + int lastindex = romanNum.length() - 1; + char last = romanNum.charAt(lastindex); + int i, count=1; + for (i=lastindex-1;i>=0;i--) { + if (romanNum.charAt(i)==last) { + count += 1; + if (!isnotFive(getValue(last)) || count > 3) { + return false; + } + } + else if (getValue(romanNum.charAt(i)) == 0) { + //default value, invalid character + return false; + } + else { + count = 1; + } + + } + return true; + } + + private boolean isnotFive(int c) { + if (c == 1 || c == 10 || c == 100 || c == 1000) { + return true; + } + return false; + } + + private static int getValue(char a) { + switch(a) { + case 'I': return 1; + case 'V': return 5; + case 'X': return 10; + case 'L': return 50; + case 'C': return 100; + case 'D': return 500; + case 'M': return 1000; + default: return 0; + } } } diff --git a/src/RomanNumeralsInvalidFormatException.java b/src/RomanNumeralsInvalidFormatException.java new file mode 100644 index 0000000..62b0355 --- /dev/null +++ b/src/RomanNumeralsInvalidFormatException.java @@ -0,0 +1,4 @@ + +public class RomanNumeralsInvalidFormatException extends Exception { + +} diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..b4d1eb5 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -5,8 +5,373 @@ public class TestRomanNumerals { @Test - public void test() { - fail("Not yet implemented"); + public void convertToInteger_I_1() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("I"); + //Assert + assertEquals(1, res); } + + @Test + public void convertToInteger_V_5() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("V"); + //Assert + assertEquals(5, res); + } + + @Test + public void convertToInteger_X_10() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("X"); + //Assert + assertEquals(10, res); + } + + @Test + public void convertToInteger_L_50() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("L"); + //Assert + assertEquals(50, res); + } + + @Test + public void convertToInteger_C_100() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("C"); + //Assert + assertEquals(100, res); + } + + @Test + public void convertToInteger_D_500() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("D"); + //Assert + assertEquals(500, res); + } + + @Test + public void convertToInteger_M_1000() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("M"); + //Assert + assertEquals(1000, res); + } + + @Test + public void convertToInteger_III_3() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("III"); + //Assert + assertEquals(3, res); + } + + @Test + public void convertToInteger_XXX_30() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("XXX"); + //Assert + assertEquals(30, res); + } + + @Test + public void convertToInteger_CCC_300() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("CCC"); + //Assert + assertEquals(300, res); + } + + @Test + public void convertToInteger_MMM_3000() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("MMM"); + //Assert + assertEquals(3000, res); + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_VV_notvalid() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("VV"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_LL_notvalid() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("LL"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_DD_notvalid() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("DD"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_IIII_notvalid() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("IIII"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_XXXX_notvalid() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("XXXX"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_CCCC_notvalid() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("CCCC"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_MMMM_notvalid() throws RomanNumeralsInvalidFormatException{ + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("MMMM"); + } + + @Test + public void convertToInteger_IV_4() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("IV"); + //Assert + assertEquals(4, res); + } + + @Test + public void convertToInteger_IX_9() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("IX"); + //Assert + assertEquals(9, res); + } + + @Test + public void convertToInteger_XL_40() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("XL"); + //Assert + assertEquals(40, res); + } + + @Test + public void convertToInteger_XC_90() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("XC"); + //Assert + assertEquals(90, res); + } + + @Test + public void convertToInteger_CD_400() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("CD"); + //Assert + assertEquals(400, res); + } + + @Test + public void convertToInteger_CM_900() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("CM"); + //Assert + assertEquals(900, res); + } + + @Test + public void convertToInteger_XXIV_24() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("XXIV"); + //Assert + assertEquals(24, res); + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_VL_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("VL"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_LC_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("LC"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_DM_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("DM"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_IVXMM_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("IVXMM"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_XM_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("XM"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_XD_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("XD"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_IL_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("IL"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_IC_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("IC"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_ID_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("ID"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_IM_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("IM"); + + } + + @Test + public void convertToInteger_MCMXCVI_1996() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("MCMXCVI"); + //Assert + assertEquals(1996, res); + } + + @Test + public void convertToInteger_MMMDXLIII_3543() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("MMMDXLIII"); + //Assert + assertEquals(3543, res); + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_FF_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("FF"); + + } + + @Test (expected = RomanNumeralsInvalidFormatException.class) + public void convertToInteger_MMMSXLIII_invalid() throws RomanNumeralsInvalidFormatException { + //Arrange + RomanNumerals converter = new RomanNumerals(); + //Act + int res = converter.convertToInteger("MMMSXLIII"); + + } }