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

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

int result = 0;
int currentDigit = 0;
int previousDigit = 0;

for (int i = romanNum.length() - 1; i >= 0; i--) {
char romanDigit = romanNum.charAt(i);

if (romanDigit == 'I') {
currentDigit = 1;
} else if (romanDigit == 'V') {
currentDigit = 5;
} else if (romanDigit == 'X') {
currentDigit = 10;
} else if (romanDigit == 'L') {
currentDigit = 50;
} else if (romanDigit == 'C') {
currentDigit = 100;
} else if (romanDigit == 'D') {
currentDigit = 500;
} else if (romanDigit == 'M') {
currentDigit = 1000;
}
if (previousDigit > currentDigit) {
result -= currentDigit;
} else {
result += currentDigit;
}
previousDigit = currentDigit;
}
return result;
}
}
98 changes: 96 additions & 2 deletions tests/TestRomanNumerals.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,103 @@

public class TestRomanNumerals {

// Test "prime" values
@Test
public void test() {
fail("Not yet implemented");
public void testOnes_one() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("I");
assertEquals(1, value);
}

@Test
public void testFive() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("V");
assertEquals(5, value);
}

@Test
public void testTen() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("X");
assertEquals(10, value);
}

@Test
public void testFifty() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("L");
assertEquals(50, value);
}

@Test
public void testHundred() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("C");
assertEquals(100, value);
}

@Test
public void testFiveHundred() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("D");
assertEquals(500, value);
}

@Test
public void testThousand() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("M");
assertEquals(1000, value);
}

// Test "odd, under and over"
@Test
public void testFour() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("IV");
assertEquals(4, value);
}

@Test
public void testNine() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("IX");
assertEquals(9, value);
}

@Test
public void testThree() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("III");
assertEquals(3, value);
}

@Test
public void testHundredNine() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("CIX");
assertEquals(109, value);
}

@Test
public void testTwoThousandSixteen() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("MMXVI");
assertEquals(2016, value);
}

@Test
public void testNineHundredEightyEight() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("DCCCCLXXXVIII");
assertEquals(988, value);
}

@Test
public void testNineThousandNineHundredNinetyNine() {
RomanNumerals rn = new RomanNumerals();
int value = rn.convertToInteger("MMMMMMMMMIM");
assertEquals(9999, value);
}
}