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 #5926

Merged
merged 1 commit into from
Mar 14, 2025
Merged

Step2 #5926

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
54 changes: 54 additions & 0 deletions src/main/java/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringAddCalculator {
private static final int DEFAULT_VALUE = 0;

public static int splitAndSum(String input) {
if (input == null || input.isEmpty()) {
return DEFAULT_VALUE;
}

switch (input.length()) {
case 1:
return processSingleCharacter(input);
default:
return processMultipleCharacter(input);
}
Comment on lines +12 to +17
Copy link

Choose a reason for hiding this comment

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

case 가 한개이니, 얼리 리턴 방식이 더 나을것 같기도하네요 🤔

Suggested change
switch (input.length()) {
case 1:
return processSingleCharacter(input);
default:
return processMultipleCharacter(input);
}
if (input.length() == 1) {
return processSingleCharacter(input);
}
return processMultipleCharacter(input);

}

private static int processSingleCharacter(String input) throws RuntimeException {
int num;
try {
num = Integer.parseInt(input);
if (num < 0) {
throw new RuntimeException("Negative number");
}
} catch (NumberFormatException e) {
throw new RuntimeException("Not number value");
}

return num;
}

private static int processMultipleCharacter(String input) {
Matcher m = Pattern.compile("//(.)\n(.*)").matcher(input);
Copy link

Choose a reason for hiding this comment

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

Pattern.compile 은 상수화 하는것도 좋을것 같아요!

구현체를 살펴보면 매번 새로운 Pattern 을 생성하기 때문이에요 🙃

    /**
     * Compiles the given regular expression into a pattern.
     *
     * @param  regex
     *         The expression to be compiled
     * @return the given regular expression compiled into a pattern
     * @throws  PatternSyntaxException
     *          If the expression's syntax is invalid
     */
    public static Pattern compile(String regex) {
        return new Pattern(regex, 0);
    }

if (m.find()) {
String customDelimiter = m.group(1);
String[] tokens = m.group(2).split(customDelimiter);

return addStringNums(tokens);
}

return addStringNums(input.split(",|:"));
}

private static int addStringNums(String[] tokens) {
int num = 0;
for (String numStr : tokens) {
num += processSingleCharacter(numStr);
}

return num;
}
}
45 changes: 45 additions & 0 deletions src/test/java/StringAddCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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);
}
Comment on lines +40 to +44
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 과정이니 요구사항을 검증할 수 있는 테스트코드를 먼저 작성한 뒤 기능을 구현해보시는것 추천드립니다!

}