-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Step2 - 문자열 덧셈 계산기 #6211
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
Open
rla6755
wants to merge
3
commits into
next-step:rla6755
Choose a base branch
from
rla6755:step2
base: rla6755
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Step2 - 문자열 덧셈 계산기 #6211
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
메서드가 너무 많은 책임을 가지고 있음.
메서드가 한 가지 책임만 가지도록 리팩터링해본다.
이번 단계가 메서드 분리 연습 단계인 만큼 메서드 분리 연습하기 딱 좋은 코드네요.