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
60 changes: 56 additions & 4 deletions src/RomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@

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


public int convertToInteger(String str) {
int result = 0;
if(str==null||str.isEmpty()||!(isValid(str))){
return 0;
}
else{
for (int i = 0; i < str.length(); i++){

int s1 = getIntegerValue(str.charAt(i));
if (i + 1 < str.length()){

int s2 = getIntegerValue(str.charAt(i + 1));
if (s1 >= s2){
result = result + s1;
} else
{
result = result + s2 - s1;
i++;
}
} else {
result = result + s1;
i++;
}
}
}
return result;

}

public int getIntegerValue(char r) {
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
public boolean isValid(String str){
if(str.matches("IIII")||str.matches("XXXX")||str.matches("CCCC")||str.matches("MMMM")){
return false;
}
if(str.matches("DD")||str.matches("LL")||str.matches("VV")){
return false;
}
return true;
}

}
31 changes: 28 additions & 3 deletions tests/TestRomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
import static org.junit.Assert.*;

import org.junit.Test;

public class TestRomanNumerals {
@Test
public void testUnitExpression() {
String roman = "VI";
RomanNumerals rn = new RomanNumerals();
assertEquals("Unit conversion fails", 6, rn.convertToInteger(roman));

}

@Test
public void test() {
fail("Not yet implemented");
public void testTenExpression() {
String roman = "XII";
RomanNumerals rn = new RomanNumerals();
assertEquals("Ten conversion fails", 12, rn.convertToInteger(roman));

}

@Test
public void testHunderedExpression() {
String roman = "CII";
RomanNumerals rn = new RomanNumerals();
assertEquals("Hundered conversion fails", 102, rn.convertToInteger(roman));

}

@Test
public void testThousandExpression() {
String roman = "MCMLXXXIV";
RomanNumerals rn = new RomanNumerals();
assertEquals("Thousand conversion fails", 1984, rn.convertToInteger(roman));

}

}