-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2단계 - 문자열 덧셈 계산기 #5921
2단계 - 문자열 덧셈 계산기 #5921
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package calculator; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class StringAddCalculator { | ||
private static final String DEFAULT_DELIMITER = ",|:"; | ||
private static final Pattern CUSTOM_DELIMITER = Pattern.compile("//(.)\n(.*)"); | ||
private static final Pattern INTEGER_PATTERN = Pattern.compile("^-?\\d+$"); | ||
private static final int DELIMITER_GROUP_INDEX = 1; | ||
private static final int INPUT_GROUP_INDEX = 2; | ||
|
||
private StringAddCalculator() { | ||
throw new UnsupportedOperationException("유틸 클래스의 인스턴스를 생성할 수 없습니다."); | ||
} | ||
|
||
private static boolean isNullOrEmpty(String str) { | ||
return str == null || str.isEmpty(); | ||
} | ||
|
||
private static String[] split(String delimiter, String str) { | ||
return str.split(delimiter); | ||
} | ||
|
||
private static void validatePositiveIntegerFromString(String str) { | ||
if (!INTEGER_PATTERN.matcher(str).matches()) { | ||
throw new RuntimeException("숫자만 입력가능하지만 입력받은 문자열은 " + str + "입니다!"); | ||
} | ||
if (Integer.parseInt(str) <= 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
throw new RuntimeException("양수만 입력가능하지만 입력받은 문자열은 " + str + "입니다!"); | ||
} | ||
} | ||
|
||
private static int sum(String[] numbers){ | ||
int result = 0; | ||
for (String number : numbers) { | ||
validatePositiveIntegerFromString(number); | ||
result += Integer.parseInt(number); | ||
} | ||
return result; | ||
} | ||
|
||
public static int splitAndSum(String input) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
다소 많은 기능으르 수행하고있는데 더 작은 단위로 분리할수 없을지 고민해보면 좋겠네요! |
||
if (isNullOrEmpty(input)) { | ||
return 0; | ||
} | ||
|
||
String delimiter = DEFAULT_DELIMITER; | ||
Matcher customDelimiter = CUSTOM_DELIMITER.matcher(input); | ||
if (customDelimiter.find()) { | ||
delimiter = customDelimiter.group(DELIMITER_GROUP_INDEX); | ||
input = customDelimiter.group(INPUT_GROUP_INDEX); | ||
} | ||
|
||
String[] numbers = split(delimiter, input); | ||
return sum(numbers); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||||||||||||
package calculator; | ||||||||||||||||
import org.junit.jupiter.api.Test; | ||||||||||||||||
|
||||||||||||||||
import static org.assertj.core.api.Assertions.assertThat; | ||||||||||||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||||||||||||||||
|
||||||||||||||||
public class StringAddCalculatorTest { | ||||||||||||||||
@Test | ||||||||||||||||
public void splitAndSum_null_또는_빈문자() { | ||||||||||||||||
int result = StringAddCalculator.splitAndSum(null); | ||||||||||||||||
assertThat(result).isEqualTo(0); | ||||||||||||||||
|
||||||||||||||||
result = StringAddCalculator.splitAndSum(""); | ||||||||||||||||
assertThat(result).isEqualTo(0); | ||||||||||||||||
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Test | ||||||||||||||||
public void splitAndSum_숫자하나() throws Exception { | ||||||||||||||||
int result = StringAddCalculator.splitAndSum("1"); | ||||||||||||||||
assertThat(result).isEqualTo(1); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Test | ||||||||||||||||
public void splitAndSum_쉼표구분자() throws Exception { | ||||||||||||||||
int result = StringAddCalculator.splitAndSum("1,2"); | ||||||||||||||||
assertThat(result).isEqualTo(3); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Test | ||||||||||||||||
public void splitAndSum_쉼표_또는_콜론_구분자() throws Exception { | ||||||||||||||||
int result = StringAddCalculator.splitAndSum("1,2:3"); | ||||||||||||||||
assertThat(result).isEqualTo(6); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Test | ||||||||||||||||
public void splitAndSum_custom_구분자() throws Exception { | ||||||||||||||||
int result = StringAddCalculator.splitAndSum("//;\n1;2;3"); | ||||||||||||||||
assertThat(result).isEqualTo(6); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@Test | ||||||||||||||||
public void splitAndSum_negative() throws Exception { | ||||||||||||||||
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3")) | ||||||||||||||||
.isInstanceOf(RuntimeException.class); | ||||||||||||||||
} | ||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
참고: [Git] 커밋 메시지 규약 정리