Skip to content
Open
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.14.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.14.0'
testImplementation 'org.junit.platform:junit-platform-launcher:1.14.0'
implementation 'org.apache.commons:commons-lang3:3.17.0'
}

java {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/study/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package study;

import org.apache.commons.lang3.StringUtils;

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

public class StringAddCalculator {
public static int splitAndSum(String text) {
Copy link
Contributor

Choose a reason for hiding this comment

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

메서드가 너무 많은 책임을 가지고 있음.
메서드가 한 가지 책임만 가지도록 리팩터링해본다.
이번 단계가 메서드 분리 연습 단계인 만큼 메서드 분리 연습하기 딱 좋은 코드네요.

if(StringUtils.isBlank(text)) {
return 0;
}

if(text.contains(",") || text.contains(":")) {
int sum = 0;
String[] numbers = text.split("[,:]");

for(String number : numbers) {
if(Integer.parseInt(number) < 0) {
throw new RuntimeException(" RuntimeException 예외 발생");
}

sum += Integer.parseInt(number);
}

return sum;
}

if(text.contains("//") || text.contains("\n")) {
int sum = 0;
Matcher m = Pattern.compile("//(.)\n(.*)").matcher(text);
if(m.find()) {

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

for(String token : tokens) {
sum += Integer.parseInt(token);
}

return sum;
}
}



return Integer.parseInt(text);
}
}
47 changes: 47 additions & 0 deletions src/test/java/study/StringAddCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package study;

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

public class StringAddCalculatorTest {
@Test
public void 문자열_덧셈_null_또는_빈문자열() {
int result = StringAddCalculator.splitAndSum(null);
assertThat(result).isEqualTo(0);

result = StringAddCalculator.splitAndSum("");
assertThat(result).isEqualTo(0);
}

@Test
void 문자열_덧셈_숫자하나() {
int result = StringAddCalculator.splitAndSum("2");
assertThat(result).isEqualTo(2);
}

@Test
void 문자열_덧셈_구분자_덧셈() {
int result = StringAddCalculator.splitAndSum("1,2");
assertThat(result).isEqualTo(3);
}

@Test
void 문자열_덧셈_여러구분자_덧셈() {
int result = StringAddCalculator.splitAndSum("1,2:3");
assertThat(result).isEqualTo(6);
}

@Test
void 문자열_덧셈_커스텀구분자_덧셈() {
int result = StringAddCalculator.splitAndSum("//;\n1;2;3");
assertThat(result).isEqualTo(6);
}

@Test
void 문자열_덧셈_음수예외() {
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3"))
.isInstanceOf(RuntimeException.class);
}

}