Skip to content

Commit 2394319

Browse files
committed
refactor. 에러 메시지를 하드 코딩 방식에서 상수 변수로 분리
1 parent 98b0f3f commit 2394319

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/main/java/domain/Calculator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package domain;
22

33
public class Calculator {
4+
public static final String DIVIDE_BY_ZERO = "0으로 나눌 수 없습니다.";
5+
46
public int add(int num1, int num2) {
57
return Math.addExact(num1, num2);
68
}
@@ -15,7 +17,7 @@ public int mul(int num1, int num2) {
1517

1618
public int div(int num1, int num2) {
1719
if (num2 == 0)
18-
throw new ArithmeticException("0으로 나눌 수 없습니다.");
20+
throw new ArithmeticException(DIVIDE_BY_ZERO);
1921

2022
return num1 / num2;
2123
}

src/main/java/domain/StringCalculator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
import java.util.regex.Pattern;
66

77
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 = "문자열은 처리할 수 없습니다.";
812
private final String delimeterRegex = "[,|:]";
913

1014
public int calculate(String str) {
1115
String[] tokens;
1216
String customDelimeter;
1317

1418
if (str == null || str.isBlank()) {
15-
throw new RuntimeException("문자열이 비어있습니다.");
19+
throw new RuntimeException(EMPTY_STRING);
1620
}
1721

1822
customDelimeter = findCustomDelimeter(str);
@@ -38,7 +42,7 @@ public String findCustomDelimeter(String str) {
3842
}
3943

4044
if ((startDelimeterIdx == -1) ^ (endDelimeterIdx == -1) || (startDelimeterIdx + 2 == endDelimeterIdx)) {
41-
throw new RuntimeException("커스텀 구분자를 찾을 수 없습니다.");
45+
throw new RuntimeException(CUSTOM_DELIMITER_NOT_FOUND);
4246
}
4347

4448
return str.substring(startDelimeterIdx + 2, endDelimeterIdx);
@@ -51,12 +55,12 @@ public List<Integer> parseNumber(String[] tokens) {
5155
int number = Integer.parseInt(token.trim());
5256

5357
if (number < 0) {
54-
throw new RuntimeException("음수는 처리할 수 없습니다.");
58+
throw new RuntimeException(NEGATIVE_NUMBER_NOT_ALLOWED);
5559
}
5660

5761
return number;
5862
} catch (NumberFormatException ex) {
59-
throw new RuntimeException("문자열은 처리할 수 없습니다.");
63+
throw new RuntimeException(INVALID_STRING);
6064
}
6165
})
6266
.toList();

0 commit comments

Comments
 (0)