diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..37200f0 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,123 @@ -public class RomanNumerals { - public int convertToInteger(String romanNum) { - // To be Implemented +/* +The symbols 'I', 'X', 'C', and 'M' can be repeated at most 3 times in a row. +The symbols 'V', 'L', and 'D' can never be repeated. +The '1' symbols ('I', 'X', and 'C') can only be subtracted from the 2 next highest values ('IV' and +'IX', 'XL' and 'XC', 'CD' and 'CM'). +Only one subtraction can be made per numeral ('XC' is allowed, 'XXC' is not). +The '5' symbols ('V', 'L', and 'D') can never be subtracted. + */ + +public class RomanNumerals +{ + + public int convertToInteger(String romanNum) + { + // in case someone sends small letters that we use for + // other purposes in the program + romanNum = romanNum.toUpperCase(); + // checks that the 2 / 4 same letter combo is not present + if(!checkValidity(romanNum)) return -1; + // checks that IXC can be deducted only from two next letters + if(!illegalCombos(romanNum)) return -1; + // changes substracted IXC values of VXLDM to vxlcdm whcih stand for 4, 9, 40, 90, 400 and 900 + romanNum=cheatCalculation(romanNum); + // counts the total + return sumUp(romanNum); + } + + public int convertOne(char x) + { + switch (x) + { + case 'I' : return 1; + case 'v' : return 4; + case 'V' : return 5; + case 'x' : return 9; + case 'X' : return 10; + case 'l' : return 40; + case 'L' : return 50; + case 'c' : return 90; + case 'C' : return 100; + case 'd' : return 400; + case 'D' : return 500; + case 'm' : return 900; + case 'M' : return 1000; + } return 0; + } + + public int sumUp(String rNum) + { + int sum=0; + for (int i=0; i