I have an method to convert roman numbers to common decimal. I used here an one for cycle and a lot of "if" conditions. SonarLint in my IDE says me that Cognitive Complexity of this method is 33 while 15 is allowed. How can I reduce this? I do not mind how can I fix this. Waiting for your reccomendations!
public static int roman2Decimal(String roman) {
int decimal = 0;
char previous = 0;
for (int x = 0; x < roman.length(); x++) {
if (roman.charAt(x) == 'I')
decimal += 1;
if (roman.charAt(x) == 'V') {
System.out.println(previous);
if (previous == 'I') {
decimal -= 2;
}
decimal += 5;
}
if (roman.charAt(x) == 'X') {
if (previous == 'I') {
decimal -= 2;
}
decimal += 10;
}
if (roman.charAt(x) == 'L') {
if (previous == 'X') {
decimal -= 20;
}
decimal += 50;
}
if (roman.charAt(x) == 'C') {
if (previous == 'X') {
decimal -= 20;
}
decimal += 100;
}
if (roman.charAt(x) == 'D') {
if (previous == 'C') {
decimal -= 200;
}
decimal += 500;
}
if (roman.charAt(x) == 'M') {
if (previous == 'C') {
decimal -= 200;
}
decimal += 1000;
}
previous = roman.charAt(x);
}
return decimal;
}