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

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

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
58 changes: 58 additions & 0 deletions src/main/java/calculator/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package calculator;
Copy link

Choose a reason for hiding this comment

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


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) {
Copy link

Choose a reason for hiding this comment

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

Number같은 클래스가 있다면 해당 조건들을 지켜주는 온전한 객체를 만들어 볼수있겠네요!

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) {
Copy link

Choose a reason for hiding this comment

The 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);
}
}
46 changes: 46 additions & 0 deletions src/test/java/calculator/StringAddCalculatorTest.java
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
Copy link

Choose a reason for hiding this comment

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

Suggested change
int result = StringAddCalculator.splitAndSum(null);
assertThat(result).isEqualTo(0);
result = StringAddCalculator.splitAndSum("");
assertThat(result).isEqualTo(0);
assertThat(StringAddCalculator.splitAndSum(null)).isEqualTo(0);
assertThat(StringAddCalculator.splitAndSum("")).isEqualTo(0);
  • 변수명을 재활용하면 추후 유지보수할때 혼란을 야기시킬수 있는 포인트가 될수도 있다고 생각합니다~
  • 좀 더 명확한 테스트 코드를 고민해보아요~
  • assertAll을 사용해도 좋겠네요~
  • @NullAndEmptySource 활용해볼수도 있겠네요!

}

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