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
53 changes: 50 additions & 3 deletions src/RomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
import java.util.LinkedHashMap;

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

String[] num = romanNum.toUpperCase().split("");
int sum = 0;

if(!checkLegal(romanNum))
throw new Exception("Illegal numeral");

LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("M", 1000);
// map.put("CM", 900);
map.put("D", 500);
// map.put("CD", 400);
map.put("C", 100);
// map.put("XC", 90);
map.put("L", 50);
// map.put("XL", 40);
map.put("X", 10);
// map.put("IX", 9);
map.put("V", 5);
// map.put("IV", 4);
map.put("I", 1);

for (int i=0; i<num.length - 1; i++) {
if (map.get(num[i]) < map.get(num[i+1]))
sum -= map.get(num[i]);
else
sum += map.get(num[i]);

}
sum += map.get(num[(num.length -1)]) ;
return sum;



}

//Check for non Roman numeral and repetition
public boolean checkLegal(String romanNum) {
if (romanNum == "")
return false;
if (!romanNum.matches("[IVXLCDM]+"))
return false;
if (romanNum.contains("IIII") || romanNum.contains("CCCC") || romanNum.contains("XXXX") || romanNum.contains("MMMM"))
return false;
if (romanNum.contains("VV") || romanNum.contains("LL") || romanNum.contains("DD"))
return false;
if (romanNum.contains("VD") || romanNum.contains("VL") || romanNum.contains("LD"))
return false;
return true;
}
}
66 changes: 64 additions & 2 deletions tests/TestRomanNumerals.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,71 @@

public class TestRomanNumerals {

@Test (expected = Exception.class)
public void testIllegalRepeatTriple() throws Exception {
RomanNumerals num = new RomanNumerals();
num.convertToInteger("DMCCCCV");
}

@Test (expected = Exception.class)
public void testIllegalRepeatDouble() throws Exception {
RomanNumerals num = new RomanNumerals();
num.convertToInteger("DMCCVV");
}

@Test (expected = Exception.class)
public void testIllegalSymbol() throws Exception {
RomanNumerals num = new RomanNumerals();
num.convertToInteger("B");
}

@Test
public void testSingles() throws Exception {
RomanNumerals num = new RomanNumerals();
assertEquals(1,num.convertToInteger("I"));
assertEquals(5,num.convertToInteger("V"));
assertEquals(10,num.convertToInteger("X"));
assertEquals(50,num.convertToInteger("L"));
assertEquals(100,num.convertToInteger("C"));
assertEquals(500,num.convertToInteger("D"));
assertEquals(1000,num.convertToInteger("M"));
}

@Test
public void test() {
fail("Not yet implemented");
public void testDoubles() throws Exception {
RomanNumerals num = new RomanNumerals();
assertEquals(2,num.convertToInteger("II"));
assertEquals(20,num.convertToInteger("XX"));
assertEquals(200,num.convertToInteger("CC"));
assertEquals(2000,num.convertToInteger("MM"));
}

@Test
public void testAddition() throws Exception {
RomanNumerals num = new RomanNumerals();
assertEquals(11,num.convertToInteger("XI"));
assertEquals(15,num.convertToInteger("XV"));
assertEquals(32,num.convertToInteger("XXXII"));
assertEquals(102,num.convertToInteger("CII"));
assertEquals(2016,num.convertToInteger("MMXVI"));
}

@Test
public void testSubstraction() throws Exception {
RomanNumerals num = new RomanNumerals();
assertEquals(4,num.convertToInteger("IV"));
assertEquals(9,num.convertToInteger("IX"));
assertEquals(14,num.convertToInteger("XIV"));
assertEquals(19,num.convertToInteger("XIX"));
assertEquals(49,num.convertToInteger("XLIX"));
assertEquals(2453,num.convertToInteger("MMCDLIII"));
}

@Test (expected = Exception.class)
public void testIllegalSubstraction() throws Exception {
RomanNumerals num = new RomanNumerals();
num.convertToInteger("VL");
num.convertToInteger("VD");
}

}