5
5
import java .util .regex .Pattern ;
6
6
7
7
public class StringCalculator {
8
+ public static final String EMPTY_STRING = "문자열이 비어있습니다." ;
9
+ public static final String CUSTOM_DELIMITER_NOT_FOUND = "커스텀 구분자를 찾을 수 없습니다." ;
10
+ public static final String NEGATIVE_NUMBER_NOT_ALLOWED = "음수는 처리할 수 없습니다." ;
11
+ public static final String INVALID_STRING = "문자열은 처리할 수 없습니다." ;
8
12
private final String delimeterRegex = "[,|:]" ;
9
13
10
14
public int calculate (String str ) {
11
15
String [] tokens ;
12
16
String customDelimeter ;
13
17
14
18
if (str == null || str .isBlank ()) {
15
- throw new RuntimeException ("문자열이 비어있습니다." );
19
+ throw new RuntimeException (EMPTY_STRING );
16
20
}
17
21
18
22
customDelimeter = findCustomDelimeter (str );
@@ -38,7 +42,7 @@ public String findCustomDelimeter(String str) {
38
42
}
39
43
40
44
if ((startDelimeterIdx == -1 ) ^ (endDelimeterIdx == -1 ) || (startDelimeterIdx + 2 == endDelimeterIdx )) {
41
- throw new RuntimeException ("커스텀 구분자를 찾을 수 없습니다." );
45
+ throw new RuntimeException (CUSTOM_DELIMITER_NOT_FOUND );
42
46
}
43
47
44
48
return str .substring (startDelimeterIdx + 2 , endDelimeterIdx );
@@ -51,12 +55,12 @@ public List<Integer> parseNumber(String[] tokens) {
51
55
int number = Integer .parseInt (token .trim ());
52
56
53
57
if (number < 0 ) {
54
- throw new RuntimeException ("음수는 처리할 수 없습니다." );
58
+ throw new RuntimeException (NEGATIVE_NUMBER_NOT_ALLOWED );
55
59
}
56
60
57
61
return number ;
58
62
} catch (NumberFormatException ex ) {
59
- throw new RuntimeException ("문자열은 처리할 수 없습니다." );
63
+ throw new RuntimeException (INVALID_STRING );
60
64
}
61
65
})
62
66
.toList ();
0 commit comments