-
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 #5926
Step2 #5926
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,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); | ||
} | ||
} | ||
|
||
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); | ||
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.
구현체를 살펴보면 매번 새로운 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; | ||
} | ||
} |
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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
case 가 한개이니, 얼리 리턴 방식이 더 나을것 같기도하네요 🤔