File tree 3 files changed +15
-8
lines changed
main/java/com/thealgorithms/conversions
test/java/com/thealgorithms/conversions
3 files changed +15
-8
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"report-block-list-path-regex": [
3
3
"src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java",
4
- "src/main/java/com/thealgorithms/conversions/RomanToInteger.java",
5
4
"src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java",
6
5
"src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java",
7
6
"src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java",
Original file line number Diff line number Diff line change @@ -19,6 +19,10 @@ private RomanToInteger() {
19
19
}
20
20
};
21
21
22
+ private static int romanSymbolToInt (final char symbol ) {
23
+ return ROMAN_TO_INT .computeIfAbsent (symbol , c -> { throw new IllegalArgumentException ("Unknown Roman symbol: " + c ); });
24
+ }
25
+
22
26
// Roman Number = Roman Numerals
23
27
24
28
/**
@@ -39,10 +43,10 @@ public static int romanToInt(String a) {
39
43
40
44
if (prev != ' ' ) {
41
45
// checking current Number greater than previous or not
42
- newPrev = ROMAN_TO_INT . get (prev ) > newPrev ? ROMAN_TO_INT . get (prev ) : newPrev ;
46
+ newPrev = romanSymbolToInt (prev ) > newPrev ? romanSymbolToInt (prev ) : newPrev ;
43
47
}
44
48
45
- int currentNum = ROMAN_TO_INT . get (c );
49
+ int currentNum = romanSymbolToInt (c );
46
50
47
51
// if current number greater than prev max previous then add
48
52
if (currentNum >= newPrev ) {
@@ -57,9 +61,4 @@ public static int romanToInt(String a) {
57
61
58
62
return sum ;
59
63
}
60
-
61
- public static void main (String [] args ) {
62
- int sum = romanToInt ("MDCCCIV" );
63
- System .out .println (sum );
64
- }
65
64
}
Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .conversions ;
2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
4
5
5
6
import org .junit .jupiter .api .Test ;
6
7
@@ -10,5 +11,13 @@ public class RomanToIntegerTest {
10
11
public void testRomanToInteger () {
11
12
assertEquals (1994 , RomanToInteger .romanToInt ("MCMXCIV" ));
12
13
assertEquals (58 , RomanToInteger .romanToInt ("LVIII" ));
14
+ assertEquals (1804 , RomanToInteger .romanToInt ("MDCCCIV" ));
15
+ }
16
+
17
+ @ Test
18
+ void testRomanToIntegerThrows () {
19
+ assertThrows (IllegalArgumentException .class , () -> RomanToInteger .romanToInt ("Z" ));
20
+ assertThrows (IllegalArgumentException .class , () -> RomanToInteger .romanToInt ("MZI" ));
21
+ assertThrows (IllegalArgumentException .class , () -> RomanToInteger .romanToInt ("MMMO" ));
13
22
}
14
23
}
You can’t perform that action at this time.
0 commit comments