Skip to content
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

2단계 - 문자열 덧셈 계산기 #5925

Merged
merged 4 commits into from
Mar 14, 2025
Merged

Conversation

bourbonkk
Copy link

기능 요구사항

  • 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환 (예: “” => 0, "1,2" => 3, "1,2,3" => 6, “1,2:3” => 6)
  • 앞의 기본 구분자(쉼표, 콜론)외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 “//”와 “\n” 사이에 위치하는 문자를 커스텀 구분자로 사용한다. 예를 들어 “//;\n1;2;3”과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다.
  • 문자열 계산기에 숫자 이외의 값 또는 음수를 전달하는 경우 RuntimeException 예외를 throw한다.

Copy link

@mskangg mskangg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 민석님!
이번 미션 리뷰를 맡은 강민수입니다 🙇

2단계 미션 잘 구현해주셨네요!
소소하게 피드백 남겨드렸는데, 확인부탁드리고 다음 단계 진행하시면서 반영 같이 부탁드립니다!
그럼 다음 단계 진행해주세요! 🔥

Comment on lines +11 to +12
private static final String DEFAULT_DELIMITER = "[,:]";
private static final String DELIMITER_FIND_REGEX = "//(.)\n(.*)";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상수 👍

if (StringUtils.isNumeric(input)) {
return List.of(Integer.parseInt(input));
}
Pattern pattern = Pattern.compile(DELIMITER_FIND_REGEX);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern.compile 은 상수화 하는것도 좋을것 같아요!

구현체를 살펴보면 매번 새로운 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);
    }

Comment on lines +28 to +30
} else {
values = input.split(DEFAULT_DELIMITER);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📖 객체지향 생활 체조 원칙
규칙 2: else 예약어를 쓰지 않는다.

를 지켜서 해보는것도 좋을것 같아요! :)

private final static NumberSeparator numberSeparator = new NumberSeparator();


public static int splitAndSum(String o) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의미있게 변수명을 지어보는것도 좋을것 같아요 :)

Comment on lines +43 to +53
@Test
public void splitAndSum_negative() throws Exception {
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3"))
.isInstanceOf(RuntimeException.class);
}

@Test
public void splitAndSum_문자포함() throws Exception {
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("1,2,test"))
.isInstanceOf(RuntimeException.class);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예외메세지를 추가로 정의해주셨으니, 검증도 같이하는건 어떨까요?

Comment on lines +5 to +6
public StringUtils() {
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static 메서드만 갖고 있는 클래스의 기본생성자를 열어두셨네요.
이유를 알 수 있을까요? 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고민의 흔적이 남아있던것같습니다. 제거하는 쪽으로 하겠습니다

Comment on lines +46 to +51
} catch (NumberFormatException e) {
throw new RuntimeException("숫자가 아닌 값이 입력되었습니다.");
}
if (value < 0) {
throw new RuntimeException("음수는 입력할 수 없습니다.");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@mskangg mskangg merged commit f5096e9 into next-step:bourbonkk Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants