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
48 changes: 45 additions & 3 deletions src/RomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class RomanNumerals {
public int convertToInteger(String romanNum) {
// To be Implemented
return 0;
public static int convertToInteger(String romanNumeral) throws RomanNumeralsException {
String input = romanNumeral.toUpperCase();
int result = 0;
int i = 0;
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();

RomanNumeralsOperators operators = new RomanNumeralsOperators();
operators.checkMaxRepeat(input);
operators.checkNeverRepeat(input);

while ((input.length() > 0) && (i < romanNumerals.size())) {
RomanNumeral symbol = romanNumerals.get(i);
if (input.startsWith(symbol.name())) {
result += symbol.getValue();
input = input.substring(symbol.name().length());
} else {
i++;
}
}

if (input.length() > 0) {
throw new IllegalArgumentException(romanNumeral);
}
return result;
}

enum RomanNumeral {
I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);

private int value;

RomanNumeral(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static List<RomanNumeral> getReverseSortedValues(){
return Arrays.stream(values()).sorted(Comparator.comparing((RomanNumeral e) -> e.value).reversed()).collect(Collectors.toList());
}
}
}
4 changes: 4 additions & 0 deletions src/RomanNumeralsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public class RomanNumeralsException extends Exception {

}
21 changes: 21 additions & 0 deletions src/RomanNumeralsOperators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

public class RomanNumeralsOperators {

public void checkMaxRepeat (String romanNumeral) throws RomanNumeralsException {

if (romanNumeral.indexOf("I", romanNumeral.indexOf("I") + 3) != -1
|| romanNumeral.indexOf("X", romanNumeral.indexOf("X") + 3) != -1
|| romanNumeral.indexOf("C", romanNumeral.indexOf("C") + 3) != -1
|| romanNumeral.indexOf("M", romanNumeral.indexOf("M") + 3) != -1){
throw new RomanNumeralsException();
}
}
public void checkNeverRepeat (String romanNumeral) throws RomanNumeralsException {

if (romanNumeral.indexOf("V", romanNumeral.indexOf("V") + 1) != -1
|| romanNumeral.indexOf("L", romanNumeral.indexOf("L") + 1) != -1
|| romanNumeral.indexOf("D", romanNumeral.indexOf("D") + 1) != -1) {
throw new RomanNumeralsException();
}
}
}
282 changes: 276 additions & 6 deletions tests/TestRomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,282 @@
import static org.junit.Assert.*;

import org.junit.Test;

public class TestRomanNumerals {

@Test
public void test() {
fail("Not yet implemented");
}
@Test
public void testThreeSymbols() throws RomanNumeralsException {
//Act
String symbol = "III";
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 3;

assertEquals (result, Arabic);
}
@Test
public void testThreeSymbols2() throws RomanNumeralsException {
//Act
String symbol = "XXX";
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 30;

assertEquals (result, Arabic);
}

@Test
public void testThreeSymbols3() throws RomanNumeralsException {
//Act
String symbol = "CCC";
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 300;

assertEquals (result, Arabic);
}

@Test
public void testThreeSymbols4() throws RomanNumeralsException {
//Act
String symbol = "MMM";
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 3000;

assertEquals (result, Arabic);
}

@Test (expected = RomanNumeralsException.class)
public void testFourSymbols() throws RomanNumeralsException {
//Act
String symbol = "IIII";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);

}

@Test (expected = RomanNumeralsException.class)
public void testFourSymbols2() throws RomanNumeralsException {
//Act
String symbol = "XXXX";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);

}

@Test (expected = RomanNumeralsException.class)
public void testFourSymbols3() throws RomanNumeralsException {
//Act
String symbol = "CCCC";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = RomanNumeralsException.class)
public void testFourSymbols4() throws RomanNumeralsException {
//Act
String symbol = "MMMM";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test
public void testNeverRepeatedSymbols1() throws RomanNumeralsException {
//Act
String symbol = "V";
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 5;

assertEquals (result, Arabic);
}

@Test
public void testNeverRepeatedSymbols2() throws RomanNumeralsException {
//Act
String symbol = "L";
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 50;

assertEquals (result, Arabic);
}

@Test
public void testNeverRepeatedSymbols3() throws RomanNumeralsException {
//Act
String symbol = "D";
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 500;

assertEquals (result, Arabic);
}

@Test (expected = RomanNumeralsException.class)
public void testFaultyRepeatedSymbols1() throws RomanNumeralsException {
//Act
String symbol = "VV";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = RomanNumeralsException.class)
public void testFaultyRepeatedSymbols2() throws RomanNumeralsException {
//Act
String symbol = "LL";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = RomanNumeralsException.class)
public void testFaultyRepeatedSymbols3() throws RomanNumeralsException {
//Act
String symbol = "DD";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test
public void testSubtraction() throws RomanNumeralsException {
//Act
String symbol = "IV";
// This statement should cause an exception
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 4;

assertEquals (result, Arabic);
}

@Test
public void testSubtraction2() throws RomanNumeralsException {
//Act
String symbol = "IX";
// This statement should cause an exception
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 9;

assertEquals (result, Arabic);
}

@Test
public void testSubtraction3() throws RomanNumeralsException {
//Act
String symbol = "XL";
// This statement should cause an exception
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 40;

assertEquals (result, Arabic);
}

@Test
public void testSubtraction4() throws RomanNumeralsException {
//Act
String symbol = "XC";
// This statement should cause an exception
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 90;

assertEquals (result, Arabic);
}

@Test
public void testSubtraction5() throws RomanNumeralsException {
//Act
String symbol = "CD";
// This statement should cause an exception
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 400;

assertEquals (result, Arabic);
}
@Test
public void testSubtraction6() throws RomanNumeralsException {
//Act
String symbol = "CM";
// This statement should cause an exception
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 900;

assertEquals (result, Arabic);
}

@Test (expected = IllegalArgumentException.class)
public void testFaultySubtraction() throws RomanNumeralsException {
//Act
String symbol = "IL";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = IllegalArgumentException.class)
public void testFaultySubtraction2() throws RomanNumeralsException {
//Act
String symbol = "XD";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = IllegalArgumentException.class)
public void testFaultySubtraction4() throws RomanNumeralsException {
//Act
String symbol = "IL";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = IllegalArgumentException.class)
public void testFaultySubtraction3() throws RomanNumeralsException {
//Act
String symbol = "XXC";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}
@Test (expected = IllegalArgumentException.class)
public void testFaultySubtraction5() throws RomanNumeralsException {
//Act
String symbol = "IIX";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = IllegalArgumentException.class)
public void testFaultySubtraction6() throws RomanNumeralsException {
//Act
String symbol = "VX";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = IllegalArgumentException.class)
public void testFaultySubtraction7() throws RomanNumeralsException {
//Act
String symbol = "LC";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test (expected = IllegalArgumentException.class)
public void testFaultySubtraction8() throws RomanNumeralsException {
//Act
String symbol = "DM";
// This statement should cause an exception
RomanNumerals.convertToInteger(symbol);
}

@Test
public void testRomanNumeralToArabic() throws RomanNumeralsException {
//Act
String symbol = "MCMLXXXIV";
// This statement should cause an exception
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 1984;

assertEquals (result, Arabic);
}
@Test
public void testRomanNumeralToArabic2() throws RomanNumeralsException {
//Act
String symbol = "MMXIV";
// This statement should cause an exception
int result = RomanNumerals.convertToInteger(symbol);
int Arabic = 2014;

assertEquals (result, Arabic);
}

}
}