From 692ca8871ab4f5333dd4bbfd7f47d6ce5fdfaa49 Mon Sep 17 00:00:00 2001 From: oskari Date: Mon, 15 Oct 2018 14:40:34 +0300 Subject: [PATCH] Assignment finished --- src/RomanNumerals.java | 77 ++++++++++++++++++++++++++++++++++- tests/TestRomanNumerals.java | 78 +++++++++++++++++++++++++++++++++++- 2 files changed, 151 insertions(+), 4 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..d4d05fb 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -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; + } + } + + + } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..459ef4d 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -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); } }