Skip to content

2단계-문자열 덧셈 계산기 #5909

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

Merged
merged 2 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/main/java/study/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package study;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringAddCalculator {
static public int splitAndSum(String text) {
if (text == null || text.isEmpty()) {
return 0;
}

if (isNumber(text)) {
return Integer.parseInt(text);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

숫자 하나만 있는 경우는 getSplitSum에서 숫자로 반환될거라 굳이 분기를 안해도 되지 않을까요 ?

}

if (text.startsWith("//")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 조건은 getCustomDelimiterSum 안에서도 다시 하고있는 것 같은데, 지금 이 스코프의 분기를 34번 라인 조건에서 해보는 건 어떨까요 ?

return getCustomDelimiterSum(text);
}

return getSplitSum(text);
}

static private boolean isNumber(String text) {
return text.matches("-?\\d+");
}

static private int getSplitSum(String text) throws RuntimeException {
String[] tokens = text.split(",|:");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구분자를 상수로 정의해보면 어떨까요 ?

return sumStringToken(tokens);
}

static private int getCustomDelimiterSum(String text) throws RuntimeException {
Matcher m = Pattern.compile("//(.)\n(.*)").matcher(text);
if (m.find()) {
String customDelimiter = m.group(1);
String[] tokens= m.group(2).split(customDelimiter);
return sumStringToken(tokens);
}

throw new IllegalArgumentException();
}

static private int sumStringToken(String[] tokens) throws RuntimeException {
int sum = 0;

for(String token: tokens) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream API를 이용해보면 어떨까요 ?

sum += getSingleNumber(token);
}
return sum;
}

static private int getSingleNumber(String text) throws RuntimeException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked exception과 unchecked exception은 어떤 차이가 있을까요 ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓기능 요구사항중 6번째에 "음수를 전달할 경우 RuntimeException 예외가 발생해야 한다."라고 되어 있어서, RunetimeException을 던진건데 혹시 제가 잘못 이해한 걸까요?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아뇨 ~ 질문의 의도는 RuntimeException을 throws 한 이유가 있는지에 대한거였습니다!

int number = Integer.parseInt(text);
if (number < 0) {
throw new RuntimeException("음수가 입력되었습니다." + number);
}
return number;
}
}
53 changes: 53 additions & 0 deletions src/test/java/study/StringAddCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package study;

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위와 아래가 엄밀하게는 다른 인풋에 대한 테스트라 @ParameterizedTest를 이용해보면 좋을 것 같아요 ~

}

@Test
public void splitAndSum_숫자하나() {
int result = StringAddCalculator.splitAndSum("1");
assertThat(result).isEqualTo(1);
}

@Test
public void splitAndSum_쉼표구분자() {
int result = StringAddCalculator.splitAndSum("1,2");
assertThat(result).isEqualTo(3);
}

@Test
public void splitAndSum_쉼표_또는_콜론_구분자() {
int result = StringAddCalculator.splitAndSum("1,2:3");
assertThat(result).isEqualTo(6);
}

@Test
public void splitAndSum_custom_구분자() {
int result = StringAddCalculator.splitAndSum("//;\n1;2;3");
assertThat(result).isEqualTo(6);
}

@Test
public void splitAndSum_negative() {
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3"))
.isInstanceOf(RuntimeException.class);
}

@Test
public void splitAndSum_숫자가아닌텍스트() {
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("ABC"))
.isInstanceOf(IllegalArgumentException.class);
}
}