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

feat: StringAddCalculator 추가 #5906

Merged
merged 3 commits into from
Mar 14, 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
41 changes: 41 additions & 0 deletions src/main/java/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringAddCalculator {
private static final Pattern pattern = Pattern.compile("//(.)\n(.*)");
Copy link

Choose a reason for hiding this comment

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

👍


static int splitAndSum(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
String[] tokens = splitText(text);
int[] numbers = convertStringToIntList(tokens);
return sumNumbers(numbers);
Comment on lines +11 to +13
Copy link

Choose a reason for hiding this comment

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

👍

}

private static String[] splitText(String text) {
Matcher m = pattern.matcher(text);
if (m.find()) {
return m.group(2).split(m.group(1));
}
return text.split("[,:]");
Copy link

Choose a reason for hiding this comment

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

기본구분자는 상수화해도 좋을것 같아요!

}

private static int[] convertStringToIntList(String[] numbers) {
int[] converted = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
converted[i] = Integer.parseInt(numbers[i]);
}
return converted;
}

private static int sumNumbers(int[] numbers) {
int sum = 0;
for (int number : numbers) {
if (number < 0)
throw new RuntimeException();
sum += number;
}
return sum;
}
}
51 changes: 51 additions & 0 deletions src/test/java/StringAddCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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);
}

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

@Test
public void splitAndSum_custom_구분자_negative() throws Exception {
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("//;\n-1;2;3"))
.isInstanceOf(RuntimeException.class);
}
Comment on lines +40 to +50
Copy link

Choose a reason for hiding this comment

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

문자열 계산기에 숫자 이외의 값 또는 음수를 전달하는 경우 RuntimeException 예외를 throw한다.
음수값을 검증하는 테스트코드는 잘 만들어주셨는데, 숫자 이외의 값을 테스트코드가 없네요 😅
TDD 과정이니 요구사항을 만족하는 테스트코드를 먼저 작성한뒤 기능을 구현해보시는것 추천드립니다! :)

Copy link
Author

Choose a reason for hiding this comment

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

앗 그러네요; 다음 단계에선 더 신경써보겠습니다. 감사합니다!

}