-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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단계-문자열 덧셈 계산기 #5909
2단계-문자열 덧셈 계산기 #5909
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package study; | ||
|
||
import java.util.Arrays; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class StringAddCalculator { | ||
private static final String DELIM = ",|:"; | ||
|
||
static public int splitAndSum(String text) { | ||
if (text == null || text.isEmpty()) { | ||
return 0; | ||
} | ||
|
||
Matcher m = Pattern.compile("//(.)\n(.*)").matcher(text); | ||
String[] tokens; | ||
if (m.find()) { | ||
String customDelimiter = m.group(1); | ||
tokens = m.group(2).split(customDelimiter); | ||
} else { | ||
tokens = text.split(StringAddCalculator.DELIM); | ||
} | ||
|
||
return sumStringToken(tokens); | ||
} | ||
|
||
static private int sumStringToken(String[] tokens) throws RuntimeException { | ||
return Arrays.stream(tokens) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
.mapToInt(StringAddCalculator::getSingleNumber) | ||
.sum(); | ||
} | ||
|
||
static private int getSingleNumber(String text) throws RuntimeException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checked exception과 unchecked exception은 어떤 차이가 있을까요 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓기능 요구사항중 6번째에 "음수를 전달할 경우 RuntimeException 예외가 발생해야 한다."라고 되어 있어서, RunetimeException을 던진건데 혹시 제가 잘못 이해한 걸까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아뇨 ~ 질문의 의도는 RuntimeException을 throws 한 이유가 있는지에 대한거였습니다! |
||
int number = Integer.parseInt(text); | ||
if (number < 0) { | ||
throw new RuntimeException("음수가 입력되었습니다." + number); | ||
} | ||
return number; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package study; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class StringAddCalculatorTest { | ||
@ParameterizedTest | ||
@NullSource | ||
@ValueSource( strings = {""}) | ||
public void splitAndSum_null_또는_빈문자(String input) { | ||
int result = StringAddCalculator.splitAndSum(input); | ||
assertThat(result).isEqualTo(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위와 아래가 엄밀하게는 다른 인풋에 대한 테스트라 |
||
} | ||
|
||
@Test | ||
public void splitAndSum_숫자하나() { | ||
int result = StringAddCalculator.splitAndSum("1"); | ||
assertThat(result).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_쉼표구분자() { | ||
int result = StringAddCalculator.splitAndSum("1,2"); | ||
assertThat(result).isEqualTo(3); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_쉼표_또는_콜론_구분자() { | ||
int result = StringAddCalculator.splitAndSum("1,2:3"); | ||
assertThat(result).isEqualTo(6); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_custom_구분자() { | ||
int result = StringAddCalculator.splitAndSum("//;\n1;2;3"); | ||
assertThat(result).isEqualTo(6); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_negative() { | ||
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3")) | ||
.isInstanceOf(RuntimeException.class); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_숫자가아닌텍스트() { | ||
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("ABC")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍