diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..da6d19b 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,109 @@ +import java.util.*; public class RomanNumerals { + + private TreeMap numbers; + + public RomanNumerals(){ + numbers = new TreeMap(); + numbers.put("I", 1); + numbers.put("V", 5); + numbers.put("X", 10); + numbers.put("L", 50); + numbers.put("C", 100); + numbers.put("D", 500); + numbers.put("M", 1000); + } + public int convertToInteger(String romanNum) { - // To be Implemented + + return convertMultiple(romanNum); + + } + private int convertMultiple(String romanNum){ + String[] n = romanNum.split(""); + if(checkThreeTimes(n) && substractOkay(n)){ + return count(n); + } return 0; } + //count for checking the total + private int count(String[] n){ + int c = numbers.get(n[n.length - 1]); + String prev = n[n.length - 1]; + for(int i = n.length -2; i >= 0; i--){ + if(numbers.get(prev) > numbers.get(n[i])){ + c -= numbers.get(n[i]); + } + else{ + c += numbers.get(n[i]); + } + prev = n[i]; + } + return c; + } + private boolean substractOkay(String[] n){ + String prev = n[n.length - 1]; + int times = 0; + for(int i = n.length - 2; i >= 0; i--){ + if(prev.equals("M") || prev.equals("D")){ + if(!n[i].equals("C")){ + return false; + } + else{ + times++; + } + } + if(prev.equals("C") || prev.equals("L")){ + if(!n[i].equals("X")){ + return false; + } + else{ + times++; + } + } + if(prev.equals("X") || prev.equals("V")){ + if(!n[i].equals("I")){ + return false; + } + else{ + times++; + } + } + if(n[i].equals("V") || n[i].equals("L") || n[i].equals("D")){ + if(numbers.get(n[i]) < numbers.get(prev)){ + return false; + } + } + if(!n[i].equals("I") || !n[i].equals("X") || !n[i].equals("C")){ + times = 0; + } + if(times > 1){ + return false; + } + prev = n[i]; + } + return true; + } + private boolean checkThreeTimes(String[] n){ + + String prev = n[n.length - 1]; + int times = 0; + for(int i = n.length - 2; i >= 0; i--){ + if(prev.equals(n[i])){ + times++; + } + if(times > 2){ + return false; + } + if(!prev.equals(n[i])){ + times = 0; + } + prev = n[i]; + } + return true; + + } + } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..ca16f2e 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -4,9 +4,69 @@ public class TestRomanNumerals { + RomanNumerals num = new RomanNumerals(); @Test - public void test() { - fail("Not yet implemented"); + public void test_ConvertToInteger() { + + int arabic = num.convertToInteger("I"); + assertEquals("Number is not one", 1, arabic); } + @Test + public void test_ConvertToIntegerThree(){ + + int arabic = num.convertToInteger("III"); + assertEquals("Number is not three", 3, arabic); + } + @Test + public void test_ConvertToIntegerFive(){ + + int arabic = num.convertToInteger("V"); + assertEquals("Number is not five", 5, arabic); + } + @Test + public void test_ConvertToIntegerTen(){ + + int arabic = num.convertToInteger("X"); + assertEquals("Number is not ten", 10, arabic); + } + @Test + public void test_ConvertToIntegerFifty(){ + + int arabic = num.convertToInteger("L"); + assertEquals("Number is not fifty", 50, arabic); + } + @Test + public void test_ConvertToIntegerHundred(){ + + int arabic = num.convertToInteger("C"); + assertEquals("Number is not hundred", 100, arabic); + } + @Test + public void test_ConvertToIntegerFour(){ + + int arabic = num.convertToInteger("IV"); + assertEquals("Number is not four", 4, arabic); + } + @Test + public void test_ConvertToIntegerSeve(){ + int arabic = num.convertToInteger("VII"); + assertEquals("Number is not seven", 7, arabic); + } + @Test + public void test_ConvertToIntegerNine(){ + int arabic = num.convertToInteger("IX"); + assertEquals("Number is not nine", 9, arabic); + } + @Test + public void test_NotRepeated(){ + int arabic = num.convertToInteger("VV"); + assertEquals("Number cannot be repeated", 0, arabic); + } + @Test + public void test_Substraction(){ + int arabic = num.convertToInteger("XXC"); + assertEquals("Number is substracted even though not possible", 0, arabic); + } + }