-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: StringAddCalculator 추가 #5906
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
Changes from 1 commit
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,39 @@ | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class StringAddCalculator { | ||
static int splitAndSum(String text) { | ||
if (text == null || text.isEmpty()) { | ||
return 0; | ||
} | ||
|
||
Matcher m = Pattern.compile("//(.)\n(.*)").matcher(text); | ||
if (m.find()) { | ||
String customDelimiter = m.group(1); | ||
String[] numbers = m.group(2).split(customDelimiter); | ||
return sumNumbers(numbers); | ||
} else { | ||
try { | ||
int number = Integer.parseInt(text); | ||
if (number < 0) | ||
throw new RuntimeException(); | ||
} catch (NumberFormatException ignored) {} | ||
|
||
String[] numbers = text.split("[,:]"); | ||
return sumNumbers(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.
String[] 문자열_구분자로_분할()
int[] 문자열에서_숫자변환()
int 숫자전부더하기() 크게 위와 같이 기능을 분리할 수 있을것 같아요! 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 static int sumNumbers(String[] numbers) { | ||
int sum = 0; | ||
try { | ||
for (String number : numbers) { | ||
int parsed = Integer.parseInt(number); | ||
if (parsed < 0) | ||
throw new RuntimeException(); | ||
sum += Integer.parseInt(number); | ||
} | ||
} catch (NumberFormatException ignored) {} | ||
return sum; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_custom_구분자_negative() throws Exception { | ||
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("//;\n-1;2;3")) | ||
.isInstanceOf(RuntimeException.class); | ||
} | ||
Comment on lines
+40
to
+50
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. 앗 그러네요; 다음 단계에선 더 신경써보겠습니다. 감사합니다! |
||
} |
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.
Pattern.compile("//(.)\n(.*)")
은 상수화 하는것도 좋을것 같아요!구현체를 살펴보면 매번 새로운 Pattern 을 생성하기 때문이에요 🙃
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.
pattern 상수화 했습니다!