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단계 - 문자열 덧셈 계산기 #5924

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

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

public class StringAddCalculator {

private static final String COMMENT_PREFIX = "//";
private static final Pattern COMMENT_PATTERN = Pattern.compile("//(.)\n(.*)");

public static int splitAndSum(String text) {
if (isEmpty(text)) {
return 0;
}
String[] tokens = extractTokens(text);
return add(tokens);
}

// 입력이 null이거나 빈 문자열인지 확인
private static boolean isEmpty(String text) {
return text == null || text.isEmpty();
}

// 텍스트에서 토큰을 추출 (커스텀 구분자 지원)
private static String[] extractTokens(String text) {
if (text.startsWith(COMMENT_PREFIX)) {
return extractCustomDelimiterTokens(text);
}
return text.split(",|:");
}

// 커스텀 구분자 형식에서 토큰을 추출
private static String[] extractCustomDelimiterTokens(String text) {
Matcher m = COMMENT_PATTERN.matcher(text);
if (m.find()) {
String customDelimiter = m.group(1);
return m.group(2).split(Pattern.quote(customDelimiter));
}
throw new RuntimeException("커스텀 구분자 형식이 올바르지 않습니다.");
}

// 토큰들의 합을 계산
private static int add(String[] tokens) {
int sum = 0;
for (String token : tokens) {
sum += parseAndValidateNumber(token);
}
return sum;
}

// 문자열을 정수로 변환하고 유효성 검사 (음수, 숫자가 아닌 값 체크)
private static int parseAndValidateNumber(String token) {
int number;
try {
number = Integer.parseInt(token);
} catch (NumberFormatException e) {
throw new RuntimeException("숫자가 아닌 값이 포함되어 있습니다: " + token);
}
if (number < 0) {
throw new RuntimeException("음수는 허용되지 않습니다: " + number);
}
return number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package study.StringAddCalculator;

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