Skip to content

chap2 문자열 계산기 구현 - #1

Open
dh0304 wants to merge 18 commits into
java-web-programming-nextstep-study:mainfrom
dh0304:dh
Open

dh0304 wants to merge 18 commits into
java-web-programming-nextstep-study:mainfrom
dh0304:dh

Conversation

@dh0304

@dh0304 dh0304 commented Jul 3, 2024

Copy link
Copy Markdown
Contributor

문자열 계산기 기능목록

  1. 문자열를 구분한다.
  • 기본 구분자("," ":")로 구분한다.
    • 문자열에 여러 기본 구분자가 들어가도 구분 가능하다.
  • 커스텀 구분자로 구분한다.
  1. 숫자의 합을 구한다.
  • 숫자는 음이 아닌 정수이다.
  • 음수는 더할 수 없다.

클래스 역할

StringCalculator

  • 문자열을 받아 계산하는 객체

RegexSplitPattern

  • 커스텀 구분자의 정규식을 표현한 Enum 객체

Splitter

  • 문자열을 받아 구분자로 분리하는 객체

@dh0304 dh0304 self-assigned this Jul 3, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[사소한 질문] Spring Frameowkr의 의존성을 사용하는 이유가 궁금해요~~!
질문하는 이유는 Java의 Plguins만으로도 작업할 수 있다고 생각했습니다!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

이거 사실,,, junit5 라이브러리 추가하기 귀찮아서요 ㅋㅋㅋㅋㅋ
매번 스프링부트 프로젝트 만들어서 쓰다보니까 그냥 환경만들기 쉽게 했습니다.


public class Splitter {

private static final List<String> DEFAULT_DELIMITERS = List.of(",", ":");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[칭찬] 기본구분자에 다른 구분자가 추가되었을떄 빠르게 추가할 수 있을 것 같네요!
또, 리스트 생성시에 Arrays.asList(",",":") 방식도 있을텐데, List.of(",", ":"); 가 불변객체로 생성되어 안전하게 사용한것이 좋은 것 같아요!

public class SplitterTest {

@ParameterizedTest
@ValueSource(strings = {"1,2", "1:2"})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[칭찬] JUnit5의 여러 애노테이션 사용으로 테스트가 편해지는게 좋습니다!

private final Splitter splitter;
private static final int ZERO = 0;

public StringCalculator(Splitter splitter) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[칭찬] 생성자를 통해 의존성 주입하는 방식 좋습니다!

@Test
void 음수는_더할수없다() {
StringCalculator stringCalculator = new StringCalculator(new Splitter());
assertThatThrownBy(() -> stringCalculator.add("1,-1"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[칭찬] RuntimeException 발생 여부도 assertJ에서도 확인할 수 있군요!


import org.springframework.boot.autoconfigure.SpringBootApplication;

public enum RegexSplitPattern {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[칭찬] enum(열거형) 방식 사용해서 다른 커스텀 정규패턴이 추가되면, 쉽게 추가할 수 있겠네요!!

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.

2 participants