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
77 changes: 75 additions & 2 deletions src/RomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,81 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RomanNumerals {
public int convertToInteger(String romanNum) {
// To be Implemented
return 0;
final Matcher matcher = Pattern.compile("M|CM|D|CD|C|XC|L|XL|X|IX|V|IV|I").matcher(romanNum);
final int[] decimalValues = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
final String[] romanNumerals = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int result = 0;

while (matcher.find()) {
for (int i = 0; i < romanNumerals.length; i++)
if (romanNumerals[i].equals(matcher.group(0)))
result += decimalValues[i];
}
return result;

}

public int processDecimal(int decimal, int lastNumber, int lastDecimal) {
if(lastNumber > decimal) {
return lastDecimal - decimal;
}else {
return lastDecimal + decimal;
}
}

public boolean checkRomanNumerals(String romanNum) throws NumeralException{
if(romanNum.matches("M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})")) {
return true;
}else {
throw new NumeralException();
}
}

public boolean checkRepetitionForIXCM(String romanNum) throws NumeralException {
String capsed = romanNum.toUpperCase();
if(capsed.contains("IIII") || capsed.contains("XXXX") || capsed.contains("CCCC") || capsed.contains("MMMM")) {
throw new NumeralException();
}
return true;
}

public boolean checkRepetitionForVLD(String romanNum) throws NumeralException{
String capsed = romanNum.toUpperCase();
int count1 = 0;
int count2 = 0;
int count3 = 0;
int len = capsed.length();
for(int i = 0; i< len; i++) {
if(capsed.charAt(i) == 'V') {
count1++;
}
if(capsed.charAt(i) == 'L') {
count2++;
}
if(capsed.charAt(i) == 'D') {
count3++;
}
if(count1 > 1 || count2 > 1 || count3 > 1) {
throw new NumeralException();
}
}
return true;
}

public int convertRomanToInt(String romanNum) throws NumeralException{
boolean VLD = checkRepetitionForVLD(romanNum);
boolean IXCM = checkRepetitionForIXCM(romanNum);
boolean rr = checkRomanNumerals(romanNum);
if(VLD && IXCM && rr) {
int a = convertToInteger(romanNum);
return a;
}else {
return 0;
}
}



}
78 changes: 76 additions & 2 deletions tests/TestRomanNumerals.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,84 @@
import org.junit.Test;

public class TestRomanNumerals {

@Test
public void test_checkRomanNumerals() throws NumeralException {
String romanNum = "MCMLXXXIV";
RomanNumerals converter = new RomanNumerals();
boolean result = converter.checkRomanNumerals(romanNum);
assertEquals(true, result);
romanNum = "MMXIV";
result = converter.checkRomanNumerals(romanNum);
assertEquals(true, result);
}

@Test (expected = NumeralException.class)
public void test_checkRomanNumerals_exception() throws NumeralException{
String romanNum = "MCMLXXIH";
RomanNumerals converter = new RomanNumerals();
boolean result = converter.checkRomanNumerals(romanNum); //Should throw an exception
}

@Test
public void test() {
fail("Not yet implemented");
public void test_checkRepetitionForIXCM() throws NumeralException {
String romanNum = "MCMLXXXIV";
RomanNumerals converter = new RomanNumerals();
boolean result = converter.checkRepetitionForIXCM(romanNum);
assertEquals(true, result);
romanNum = "MMXIV";
result = converter.checkRepetitionForIXCM(romanNum);
assertEquals(true, result);
}

@Test (expected = NumeralException.class)
public void test_checkRepetitionForIXCM_exception() throws NumeralException {
String romanNum = "XXMMMMMM";
RomanNumerals converter = new RomanNumerals();
boolean result = converter.checkRepetitionForIXCM(romanNum); //This should cause exception
}

@Test
public void test_checkRepetitionForVLD() throws NumeralException {
String romanNum = "MCMLXXXIV";
RomanNumerals converter = new RomanNumerals();
boolean result = converter.checkRepetitionForVLD(romanNum);
assertEquals(true, result);
romanNum = "MMXIV";
result = converter.checkRepetitionForVLD(romanNum);
assertEquals(true, result);
}

@Test (expected = NumeralException.class)
public void test_checkRepetitionForVLD_exception() throws NumeralException{
String romanNum = "MCMLXXXIVV";
RomanNumerals converter = new RomanNumerals();
boolean result = converter.checkRepetitionForVLD(romanNum); // Should cause a exception
}

@Test
public void test_convertToInteger() throws NumeralException{
RomanNumerals converter = new RomanNumerals();
String romanNum = "MMXIV";
int result = converter.convertToInteger(romanNum);
assertEquals(2014, result);
romanNum = "MCMLXXXIV";
result = converter.convertToInteger(romanNum);
assertEquals(1984, result);
}

@Test
public void test_convertRomanToInt() throws NumeralException{
RomanNumerals converter = new RomanNumerals();
String romanNum = "MMXIV";
int result = converter.convertRomanToInt(romanNum);
assertEquals(2014, result);
romanNum = "MCMLXXXIV";
result = converter.convertRomanToInt(romanNum);
assertEquals(1984, result);
romanNum = "IV";
result = converter.convertRomanToInt(romanNum);
assertEquals(4, result);
}

}