Skip to content
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

Step2 : 문자열 덧셈 계산기 #5900

Merged
merged 2 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Empty file removed src/main/java/.gitkeep
Empty file.
30 changes: 30 additions & 0 deletions src/main/java/step2/ParsedInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package step2;

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

public class ParsedInput {

Choose a reason for hiding this comment

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

클래스명은 InputParser 이런 느낌으로 명사로 끝나면 좋을 것 같아요!

private static final String DEFAULT_DELIMITER = ",|:";

Choose a reason for hiding this comment

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

👍

private String delimiter = DEFAULT_DELIMITER;
private String[] numbers;

public ParsedInput(String input) {

Choose a reason for hiding this comment

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

굳이 상태를 가지고 있을 필요가 없어보이는데, 유틸클래스로 만들어보는 건 어떨까요 ?

parseInput(input);
}

public String[] getNumbers() {
return numbers;
}

private void parseInput(String input) {
Pattern pattern = Pattern.compile("//(.)\\n(.*)");

Choose a reason for hiding this comment

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

이 정규식도 상수로 정의되면 어떨까요 ?

Matcher matcher = pattern.matcher(input);

if (matcher.find()) {
delimiter = matcher.group(1);
input = matcher.group(2);
}

numbers = input.split(delimiter);
}
}
27 changes: 27 additions & 0 deletions src/main/java/step2/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package step2;

public class StringAddCalculator {
public static int splitAndSum(final String input) {
if (input == null || input.isBlank()) {
return 0;
}

return sumParsedNumbers(new ParsedInput(input).getNumbers());

Choose a reason for hiding this comment

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

파싱 된 인풋을 변수로 한번 받으면 좋을 것 같아요 ~

Copy link
Author

@zoolake zoolake Mar 13, 2025

Choose a reason for hiding this comment

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

  1. 주신 피드백이 혹시 아래 처럼 지역변수로 빼는게 좋을 것 같다는 말씀이신걸까요?
        ParsedInput parsedInput = new ParsedInput(input);
        return sumParsedNumbers(parsedInput.getNumbers());
  1. 위 처럼 변경하는게 주신 피드백을 제대로 이해한게 맞다면, 위의 코드 처럼 수정 했을 때 장점은 메소드 호출 시 넘기는 인자가 어떤 값인지 변수명으로 명확하게 확인이 된다는 점이라고 생각하는데, 이게 맞을까요?!

}

private static int sumParsedNumbers(String[] numbers) {
int total = 0;

for (String number : numbers) {

Choose a reason for hiding this comment

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

Stream API를 활용해보는 건 어떨까요 ?

int parsed = Integer.parseInt(number);
if (parsed < 0) {
throw new RuntimeException("음수는 입력 불가 합니다.");
}

total += parsed;
}

return total;
}

}
48 changes: 48 additions & 0 deletions src/test/java/step2/StringAddCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package step2;

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

Choose a reason for hiding this comment

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

이 두개는 다른 인풋에 대한 테스트라 @ParameterizedTest 를 이용해보면 어떨까요 ?

assertThat(result).isEqualTo(0);
}

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