-
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
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단계 미션 잘 진행 해 주셨네요 :)
코멘트를 좀 남겨 뒀는데, 확인 후 다음 미션 진행 해 주세요!
고생 많으셨습니다 :)
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class ParsedInput { |
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 이런 느낌으로 명사로 끝나면 좋을 것 같아요!
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
굳이 상태를 가지고 있을 필요가 없어보이는데, 유틸클래스로 만들어보는 건 어떨까요 ?
} | ||
|
||
private void parseInput(String input) { | ||
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.
이 정규식도 상수로 정의되면 어떨까요 ?
return 0; | ||
} | ||
|
||
return sumParsedNumbers(new ParsedInput(input).getNumbers()); |
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.
파싱 된 인풋을 변수로 한번 받으면 좋을 것 같아요 ~
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.
- 주신 피드백이 혹시 아래 처럼 지역변수로 빼는게 좋을 것 같다는 말씀이신걸까요?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Stream API를 활용해보는 건 어떨까요 ?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
이 두개는 다른 인풋에 대한 테스트라 @ParameterizedTest
를 이용해보면 어떨까요 ?
No description provided.