-
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
Step2 : 문자열 덧셈 계산기 #5900
Step2 : 문자열 덧셈 계산기 #5900
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,30 @@ | ||
package step2; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class ParsedInput { | ||
private static final String DEFAULT_DELIMITER = ",|:"; | ||
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. 👍 |
||
private String delimiter = DEFAULT_DELIMITER; | ||
private String[] numbers; | ||
|
||
public ParsedInput(String input) { | ||
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. 굳이 상태를 가지고 있을 필요가 없어보이는데, 유틸클래스로 만들어보는 건 어떨까요 ? |
||
parseInput(input); | ||
} | ||
|
||
public String[] getNumbers() { | ||
return numbers; | ||
} | ||
|
||
private void parseInput(String input) { | ||
Pattern pattern = Pattern.compile("//(.)\\n(.*)"); | ||
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. 이 정규식도 상수로 정의되면 어떨까요 ? |
||
Matcher matcher = pattern.matcher(input); | ||
|
||
if (matcher.find()) { | ||
delimiter = matcher.group(1); | ||
input = matcher.group(2); | ||
} | ||
|
||
numbers = input.split(delimiter); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package step2; | ||
|
||
public class StringAddCalculator { | ||
public static int splitAndSum(final String input) { | ||
if (input == null || input.isBlank()) { | ||
return 0; | ||
} | ||
|
||
return sumParsedNumbers(new ParsedInput(input).getNumbers()); | ||
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. 파싱 된 인풋을 변수로 한번 받으면 좋을 것 같아요 ~ 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.
ParsedInput parsedInput = new ParsedInput(input);
return sumParsedNumbers(parsedInput.getNumbers());
|
||
} | ||
|
||
private static int sumParsedNumbers(String[] numbers) { | ||
int total = 0; | ||
|
||
for (String number : numbers) { | ||
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. Stream API를 활용해보는 건 어떨까요 ? |
||
int parsed = Integer.parseInt(number); | ||
if (parsed < 0) { | ||
throw new RuntimeException("음수는 입력 불가 합니다."); | ||
} | ||
|
||
total += parsed; | ||
} | ||
|
||
return total; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package step2; | ||
|
||
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(""); | ||
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. 이 두개는 다른 인풋에 대한 테스트라 |
||
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); | ||
} | ||
} |
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.
클래스명은 InputParser 이런 느낌으로 명사로 끝나면 좋을 것 같아요!