Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions src/RomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,73 @@


public class RomanNumerals {
public int convertToInteger(String romanNum) {
// To be Implemented
return 0;
public int convertToInteger(String romanNum) throws RomanNumeralsException {

int length=romanNum.length();
int i=0;
int number = 0;

if (romanNum.indexOf("IIII") >= 0)
throw new RomanNumeralsException("More than three I's in row is not accepted.");
else if (romanNum.indexOf("XXXX") >= 0)
throw new RomanNumeralsException("More than three X's in row is not accepted.");
else if (romanNum.indexOf("CCCC") >= 0)
throw new RomanNumeralsException("More than three C's in row is not accepted.");
else if (romanNum.indexOf("MMMM") >= 0)
throw new RomanNumeralsException("More than three M's in row is not accepted.");

if (romanNum.indexOf("VV") >= 0)
throw new RomanNumeralsException("More than two V's in row is not accepted.");
else if (romanNum.indexOf("LL") >= 0)
throw new RomanNumeralsException("More than two L's in row is not accepted.");
else if (romanNum.indexOf("DD") >= 0)
throw new RomanNumeralsException("More than two D's in row is not accepted.");

// todo: two or more smaller before big results exception

while(i<length)
{

if ((i+1)<length && valueOfLetter(romanNum.charAt(i)) < valueOfLetter(romanNum.charAt(i+1))){
number += valueOfLetter(romanNum.charAt(i+1)) - valueOfLetter(romanNum.charAt(i));
i+=2;
}
else {
number += valueOfLetter(romanNum.charAt(i));
i++;
}
}

return number;
}

private int valueOfLetter(Character letter) {
int number = 0;

switch (letter) {
case 'I':
number = 1;
break;
case 'V':
number = 5;
break;
case 'X':
number = 10;
break;
case 'L':
number = 50;
break;
case 'C':
number = 100;
break;
case 'D':
number = 500;
break;
case 'M':
number = 1000;
break;
}

return number;
}
}
7 changes: 7 additions & 0 deletions src/RomanNumeralsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

public class RomanNumeralsException extends Exception {
public RomanNumeralsException(String message)
{
super(message);
}
}
144 changes: 142 additions & 2 deletions tests/TestRomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,152 @@
import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;

public class TestRomanNumerals {

RomanNumerals roman = new RomanNumerals();

@Test
public void test() {
fail("Not yet implemented");
public void test_convert_I_to_1() throws RomanNumeralsException {
int i = roman.convertToInteger("I");
assertEquals(1, i);
}

@Test
public void test_convert_II_to_2() throws RomanNumeralsException {
int i = roman.convertToInteger("II");
assertEquals(2, i);
}

@Test
public void test_convert_III_to_3() throws RomanNumeralsException {
int i = roman.convertToInteger("III");
assertEquals(3, i);
}

@Test
public void test_convert_IV_to_4() throws RomanNumeralsException {
int i = roman.convertToInteger("IV");
assertEquals(4, i);
}

@Test
public void test_convert_V_to_5() throws RomanNumeralsException {
int i = roman.convertToInteger("V");
assertEquals(5, i);
}

@Test
public void test_convert_VI_to_6() throws RomanNumeralsException {
int i = roman.convertToInteger("VI");
assertEquals(6, i);
}

@Test
public void test_convert_VII_to_7() throws RomanNumeralsException {
int i = roman.convertToInteger("VII");
assertEquals(7, i);
}

@Test
public void test_convert_VIII_to_8() throws RomanNumeralsException {
int i = roman.convertToInteger("VIII");
assertEquals(8, i);
}

@Test
public void test_convert_IX_to_9() throws RomanNumeralsException {
int i = roman.convertToInteger("IX");
assertEquals(9, i);
}

@Test
public void test_convert_X_to_10() throws RomanNumeralsException {
int i = roman.convertToInteger("X");
assertEquals(10, i);
}

@Test
public void test_convert_XX_to_20() throws RomanNumeralsException {
int i = roman.convertToInteger("XX");
assertEquals(20, i);
}

@Test
public void test_convert_XL_to_40() throws RomanNumeralsException {
int i = roman.convertToInteger("XL");
assertEquals(40, i);
}

@Test
public void test_convert_C_to_100() throws RomanNumeralsException {
int i = roman.convertToInteger("C");
assertEquals(100, i);
}

@Test
public void test_convert_D_to_500() throws RomanNumeralsException {
int i = roman.convertToInteger("D");
assertEquals(500, i);
}

@Test
public void test_convert_M_to_1000() throws RomanNumeralsException {
int i = roman.convertToInteger("M");
assertEquals(1000, i);
}

@Test
public void test_convert_MCMLXXXIV_to_1984() throws RomanNumeralsException {
int i = roman.convertToInteger("MCMLXXXIV");
assertEquals(1984, i);
}

@Test
public void test_convert_MMXIV_to_2014() throws RomanNumeralsException {
int i = roman.convertToInteger("MMXIV");
assertEquals(2014, i);
}

@Test(expected=RomanNumeralsException.class)
public void test_convert_XXC_throws_exception() throws RomanNumeralsException {
int i = roman.convertToInteger("XXC");
}

@Test(expected=RomanNumeralsException.class)
public void test_convert_IIIIM_throws_exception() throws RomanNumeralsException {
int i = roman.convertToInteger("IIIIM");
}

@Test(expected=RomanNumeralsException.class)
public void test_convert_XXXXM_throws_exception() throws RomanNumeralsException {
int i = roman.convertToInteger("XXXXM");
}

@Test(expected=RomanNumeralsException.class)
public void test_convert_CCCCM_throws_exception() throws RomanNumeralsException {
int i = roman.convertToInteger("CCCCM");
}

@Test(expected=RomanNumeralsException.class)
public void test_convert_MMMMM_throws_exception() throws RomanNumeralsException {
int i = roman.convertToInteger("MMMMM");
}

@Test(expected=RomanNumeralsException.class)
public void test_convert_VV_throws_exception() throws RomanNumeralsException {
int i = roman.convertToInteger("VV");
}

@Test(expected=RomanNumeralsException.class)
public void test_convert_LL_throws_exception() throws RomanNumeralsException {
int i = roman.convertToInteger("LL");
}

@Test(expected=RomanNumeralsException.class)
public void test_convert_DD_throws_exception() throws RomanNumeralsException {
int i = roman.convertToInteger("DD");
}

}