-
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
feat: StringAddCalculator 추가 #5906
Conversation
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.
안녕하세요 태룡님!
이번 미션 리뷰를 맡은 강민수입니다 🙇
2단계 구현 잘해주셨네요! 👍
몇가지 피드백 남겨드렸는데, 확인해보시고 반영 부탁드립니다! 🙏
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
큰 차이가 있을까 싶었는데 말씀하신 것처럼 분리하니까 훨씬 깔끔하네요! 감사합니다.
혹시 더 개선할 부분이 있으면 말씀해주세요.
return 0; | ||
} | ||
|
||
Matcher m = Pattern.compile("//(.)\n(.*)").matcher(text); |
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 을 생성하기 때문이에요 🙃
/**
* 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);
}
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 상수화 했습니다!
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.
피드백 반영 잘해주셨네요!
추가적으로 코멘트 남겼는데, 확인해주시고 다음 단계 진행하시면서 반영 부탁드립니다.
그럼 다음 단계 진행해주세요! 🔥
String[] tokens = splitText(text); | ||
int[] numbers = convertStringToIntList(tokens); | ||
return sumNumbers(numbers); |
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.
👍
import java.util.regex.Pattern; | ||
|
||
public class StringAddCalculator { | ||
private static final Pattern pattern = Pattern.compile("//(.)\n(.*)"); |
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.
👍
if (m.find()) { | ||
return m.group(2).split(m.group(1)); | ||
} | ||
return text.split("[,:]"); |
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.
기본구분자는 상수화해도 좋을것 같아요!
@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); | ||
} |
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.
문자열 계산기에 숫자 이외의 값 또는 음수를 전달하는 경우 RuntimeException 예외를 throw한다.
음수값을 검증하는 테스트코드는 잘 만들어주셨는데, 숫자 이외의 값을 테스트코드가 없네요 😅
TDD 과정이니 요구사항을 만족하는 테스트코드를 먼저 작성한뒤 기능을 구현해보시는것 추천드립니다! :)
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.
앗 그러네요; 다음 단계에선 더 신경써보겠습니다. 감사합니다!
https://edu.nextstep.camp/s/Ie5Dwep0/ls/oD18sXzO