From 779c64494d2e638df9a63da98d853632e6685ff2 Mon Sep 17 00:00:00 2001 From: nmarkuks18 <43221549+nmarkuks18@users.noreply.github.com> Date: Mon, 15 Oct 2018 16:21:56 +0300 Subject: [PATCH] Assignment finished --- src/RomanNumerals.java | 137 ++++++++++++++++++++++++++++++++++- tests/TestRomanNumerals.java | 64 +++++++++++++++- 2 files changed, 196 insertions(+), 5 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..a4b5445 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,141 @@ public class RomanNumerals { public int convertToInteger(String romanNum) { - // To be Implemented - return 0; + String input=romanNum; + int numeric=0; + int subtraction=0; + int lenght=input.length(); + int i=0; + checkIfRepeated(input);//L, D or V can't be repeated + checkIf4inARow(input);//I, X, C or M can be repeated max 3 times in a row + checkTooHighSubtraction(input);//symbols I, X, and C can only be subtracted from the 2 next highest values + checkSubtractionsPerNumeral(input);//only one subtraction can be made per numeral + checkSubtractionOf5Symbols(input);//V, L and D can never be subtracted + checkInputCharacters(input);//only accept roman numbers as input + + while(i0) { + if(getNumber(input.charAt(i-1))<=getNumber(input.charAt(i))) { + throw new IllegalArgumentException(); + } + }} + i++; + } + + } + + private void checkTooHighSubtraction(String input) { + int a=input.indexOf("IL"); + int b=input.indexOf("IC"); + int c=input.indexOf("ID"); + int d=input.indexOf("IM"); + int e=input.indexOf("XD"); + int f=input.indexOf("XM"); + + if(a>=0 || b>=0 || c>=0 || d>=0 || e>=0 || f>=0) { + throw new IllegalArgumentException(); + } + + } + + private void checkIf4inARow(String input) { + int a=input.indexOf("IIII"); + int b=input.indexOf("XXXX"); + int c=input.indexOf("CCCC"); + int d=input.indexOf("MMMM"); + + if (a>=0 || b>=0 || c>=0 || d>=0) { + throw new IllegalArgumentException("Invalid input"); + } + + } + + private void checkIfRepeated(String input) { + int a=input.indexOf("VV"); + int b=input.indexOf("LL"); + int c=input.indexOf("DD"); + + if (a>=0 || b>=0 || c>=0) { + throw new IllegalArgumentException("Invalid input"); + } + } + + public int getNumber(char c) { + + int number=0; + char symbol=c; + + switch (symbol) { + case ('I'): + number = 1; + break; + case ('V'): + number = 5; + break; + case ('X'): + number = 10; + break; + case ('L'): + number = 50; + break; + case ('C'): + number = 100; + break; + case ('D'): + number = 500; + break; + case ('M'): + number = 1000; + break; + } + + return number; } + } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..9866962 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -5,8 +5,66 @@ public class TestRomanNumerals { @Test - public void test() { - fail("Not yet implemented"); + public void RomanIReturns1() { + RomanNumerals nums = new RomanNumerals(); + assertEquals("Fail",1, nums.convertToInteger("I")); + } - + @Test + public void RomanMReturns1000() { + RomanNumerals nums = new RomanNumerals(); + assertEquals("Fail",1000, nums.convertToInteger("M")); + + } + @Test + public void AddSymbolValuesTogether() { + RomanNumerals nums = new RomanNumerals(); + assertEquals("Fail", 200, nums.convertToInteger("CC")); + } + @Test + public void TestSubtractionCase() { + RomanNumerals nums = new RomanNumerals(); + assertEquals("Fail", 4, nums.convertToInteger("IV")); + } + @Test + public void TestLongCombinations() { + RomanNumerals nums = new RomanNumerals(); + assertEquals("Fail", 1984, nums.convertToInteger("MCMLXXXIV")); + } + @Test(expected=IllegalArgumentException.class) + public void RepeatedLVDthrowsException() { + RomanNumerals nums = new RomanNumerals(); + nums.convertToInteger("IVV"); + } + @Test(expected=IllegalArgumentException.class) + public void MoreThan3inARowthrowsException() { + RomanNumerals nums = new RomanNumerals(); + nums.convertToInteger("MXXXXI"); + } + @Test(expected=IllegalArgumentException.class) + public void TestInvalidSubtraction() { + RomanNumerals nums = new RomanNumerals(); + nums.convertToInteger("XMD"); + } + @Test(expected=IllegalArgumentException.class) + public void TwoSubtractionsPerNumeralThrowsException() { + RomanNumerals nums = new RomanNumerals(); + nums.convertToInteger("CCM"); + } + @Test(expected=IllegalArgumentException.class) + public void SubtractingLDorVThrowsException() { + RomanNumerals nums = new RomanNumerals(); + nums.convertToInteger("DM"); + } + @Test(expected=IllegalArgumentException.class) + public void NonRomaSymbolsThrowsException() { + RomanNumerals nums = new RomanNumerals(); + nums.convertToInteger("X1V"); + } + @Test + public void TestFinishedProgramme() { + RomanNumerals nums = new RomanNumerals(); + assertEquals("Fail", 2014, nums.convertToInteger("MMXIV")); + } + }