-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[로또] 이준섭 미션 제출합니다. #2112
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
base: main
Are you sure you want to change the base?
[로또] 이준섭 미션 제출합니다. #2112
Changes from 5 commits
6cbdbef
7276e90
b6f34e0
5fb75e0
0bf4e6f
93faf63
14f83cd
c3ec26b
96f6737
0ccdc47
e4cf662
c2c7bc9
68d01e7
cb5e447
177b560
c12be99
2b5fd6f
c0fda34
1829f9c
32f7bd4
d62f6cd
e22152e
3e0d8fc
76224f2
58013f8
c331b18
ea0ea00
71be87d
9aa81c9
3ae6d9e
63bf89d
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 |
|---|---|---|
| @@ -1,32 +1,24 @@ | ||
| package lotto.domain; | ||
|
|
||
| import lotto.util.ErrorMessage; | ||
|
|
||
| public class PurchaseAmount { | ||
|
|
||
| public static final int LOTTO_PRICE = 1000; | ||
| private int amount; | ||
|
|
||
| public PurchaseAmount(String input) { | ||
| validateIsNumeric(input); | ||
| int inputAmount = Integer.parseInt(input); | ||
| validatePurchaseAmount(amount); | ||
| this.amount = inputAmount; | ||
| } | ||
| private final int purchaseAmount; | ||
|
|
||
| private void validateIsNumeric(String inputAmount) { | ||
| try { | ||
| Integer.parseInt(inputAmount); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("[ERROR] 구입금액은 숫자여야합니다."); | ||
| } | ||
| public PurchaseAmount(int inputAmount) { | ||
| validatePurchaseAmount(inputAmount); | ||
| this.purchaseAmount = inputAmount; | ||
| } | ||
|
|
||
| private void validatePurchaseAmount(int inputAmount) { | ||
| if (inputAmount % LOTTO_PRICE != 0) { | ||
| throw new IllegalArgumentException("[ERROR] 1000 단위의 금액을 입력하세요."); | ||
| throw new IllegalArgumentException(ErrorMessage.NOT_A_UNIT_OF_1000.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public int getAmount() { | ||
| return amount; | ||
| public int getPurchaseAmount() { | ||
| return purchaseAmount; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package lotto.util; | ||
|
|
||
| public enum ErrorMessage { | ||
|
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. enum으로 관리하는 것 vs 클래스의 상수로 두는 것 어떤 장단점이 있을까요!?
Author
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. enum으로 관리했을 때의 상수로 관리했을 때의 |
||
| NOT_A_NUMBER("[ERROR] 숫자여야 합니다."), | ||
| NOT_A_SINGLE_NUMBER("[ERROR] 보너스 번호는 하나의 숫자여야 합니다."), | ||
| NOT_A_UNIT_OF_1000("[ERROR] 1000 단위의 금액이어야 합니다."), | ||
| NOT_SIX_NUMBERS("[ERROR] 로또 번호는 총 6개여야 합니다."), | ||
| NOT_IN_RANGE("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."), | ||
| MUST_NOT_DUPLICATE("[ERROR] 로또 번호는 중복이 없어야 합니다."); | ||
|
|
||
| private final String message; | ||
|
|
||
| ErrorMessage(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.Arrays; | ||
| import lotto.domain.Lotto; | ||
| import lotto.domain.UserInputNumbers; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
|
||
| class LottoTest { | ||
|
|
||
| @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") | ||
| @Test | ||
| void createLottoByOverSize() { | ||
| assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @DisplayName("로또 번호의 범위가 1~45가 아닌 경우 예외가 발생한다.") | ||
| @Test | ||
| void createLottoByInvalidNumber() { | ||
| assertThatThrownBy(() -> new Lotto(List.of(0, 2, 3, 4, 5, 6))) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 46))) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
Comment on lines
+16
to
+30
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. 경계값 테스트 매우 훌륭하네요.. |
||
|
|
||
| @DisplayName("로또 번호에 중복된 숫자가 있는 경우 예외가 발생한다.") | ||
| @Test | ||
| void createLottoByDuplicatedNumber() { | ||
| // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 | ||
| assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @DisplayName("getMatchCount()는 로또 번호와 일치하는 번호의 수를 반환한다.") | ||
| @Test | ||
| void testGetMatchCount() { | ||
| List<Integer> lottoNumbers = Arrays.asList(1, 2, 3, 4, 5, 6); | ||
| Lotto lotto = new Lotto(lottoNumbers); | ||
| UserInputNumbers userInputNumbers = new UserInputNumbers(lottoNumbers, 7); | ||
| int matchCount = lotto.getMatchCount(userInputNumbers); | ||
| assertThat(matchCount).isEqualTo(6); | ||
| } | ||
|
|
||
| @DisplayName("isBonusMatch()는 로또 번호와 보너스 번호가 일치하면 true를 반환한다.") | ||
| @Test | ||
| void testIsBonusMatch() { | ||
| List<Integer> lottoNumbers = Arrays.asList(1, 2, 3, 4, 5, 6); | ||
| Lotto lotto = new Lotto(lottoNumbers); | ||
| UserInputNumbers userInputNumbers = new UserInputNumbers(lottoNumbers, 7); | ||
| boolean isBonusMatch = lotto.isBonusMatch(userInputNumbers); | ||
| assertThat(isBonusMatch).isFalse(); // 보너스 번호는 7이므로 일치하지 않아야 함 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package lotto.domain; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class PurchaseAmountTest { | ||
|
|
||
| @DisplayName("구입금액이 1000 단위가 아니라면 예외가 발생한다.") | ||
| @Test | ||
| void createPurchaseAmountByInvalidAmount() { | ||
| assertThatThrownBy(() -> new PurchaseAmount(1500)) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package lotto.domain; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
|
||
| import java.util.List; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class UserInputNumbersTest { | ||
|
|
||
| @DisplayName("당첨 번호의 개수가 6개를 넘어가는 경우 예외가 발생한다.") | ||
| @Test | ||
| void createNumberByOverSize() { | ||
| assertThatThrownBy(() -> new UserInputNumbers(List.of(1, 2, 3, 4, 5, 6, 7), 8)) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @DisplayName("당첨 번호의 범위가 1~45가 아닌 경우 예외가 발생한다.") | ||
| @Test | ||
| void createNumberByInvalidNumber() { | ||
| assertThatThrownBy(() -> new UserInputNumbers(List.of(0, 2, 3, 4, 5, 6), 7)) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| assertThatThrownBy(() -> new UserInputNumbers(List.of(1, 2, 3, 4, 5, 46), 7)) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @DisplayName("당첨 번호에 중복된 숫자가 있는 경우 예외가 발생한다.") | ||
| @Test | ||
| void createNumberByDuplicatedNumber() { | ||
| assertThatThrownBy(() -> new UserInputNumbers(List.of(1, 2, 3, 4, 5, 5), 7)) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
|
Comment on lines
+11
to
+31
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. input(입력), output(출력) 을 도메인로직으로부터 분리하면 이렇게 테스트하기 좋은 코드가 된다는 것을 알려드리고 싶었습니다. 👍👍 |
||
| } | ||
| } | ||
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.
이 코드는 Validation 만 수행하고 있는 클래스네요🤔 이 클래스의 책임으로 줄 수 있는 건 뭘까요!?
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.
좋습니다ㅋㅋㅋㅋ 사실 저도 요구사항 자체가 간단해서 책임 할당은 이 정도로 충분하다고 생각해요 🧐