-
Notifications
You must be signed in to change notification settings - Fork 1.1k
4단계 - 로또(수동) #4232
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
Open
ghtjr410
wants to merge
18
commits into
next-step:ghtjr410
Choose a base branch
from
ghtjr410:step4-lotto-manual
base: ghtjr410
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
4단계 - 로또(수동) #4232
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e68f567
docs: 로또 4단계(수동) 문서 추가 및 README 링크 추가
ghtjr410 dee80ed
feat: 수동 로또 생성기 및 테스트 추가
ghtjr410 05ceaac
docs: 수동 로또 생성기 구현 사항 문서에 추가
ghtjr410 eda4de9
feat: 수동 로또 구매 개수 도메인 및 테스트 추가
ghtjr410 7b4716c
feat: 수동 로또 개수 검증 로직 강화 및 테스트 추가
ghtjr410 07c9673
docs: 수동 구매 개수 기능 요구사항 추가
ghtjr410 5000c32
refactor: LottoCount 입력 검증 개선 및 문자열 생성자 추가
ghtjr410 0e54ee8
refactor: LottoCount 차감 기능 추가 및 테스트 보완
ghtjr410 055de05
refactor: ManualLottoCount 제거
ghtjr410 2bd391d
feat: 수동 로또 구매 흐름 추가 및 입출력 기능 확장
ghtjr410 d9c80e7
refactor: 불필요한 구매 개수 출력 로직 제거
ghtjr410 a67115d
refactor: LottoCount 차감 검증 강화 및 테스트 확장
ghtjr410 75c9e32
feat: 예외 발생 시 재입력 가능하도록 재시도 로직 추가
ghtjr410 2b66fdc
refactor: 입력/출력 static import 적용 및 보너스 번호 검증 로직 분리
ghtjr410 acfae39
refactor: 당첨 로또 생성 흐름 개선 및 중복 검증 로직 단순화
ghtjr410 dee1310
refactor: 메서드 네이밍 정리 및 당첨 로또 생성 흐름 개선
ghtjr410 f73cd13
refactor: 불필요한 생성자 제거
ghtjr410 be9ef0a
docs: 로또 수동 구매 및 WinningLotto 변경사항 문서 반영
ghtjr410 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # 4단계 - 로또(수동) | ||
| *** | ||
| ## 코드 리뷰 | ||
| > PR 링크: | ||
| > **[https://github.com/next-step/java-lotto/pull/4232](https://github.com/next-step/java-lotto/pull/4232)** | ||
| ## 나의 학습 목표 | ||
| - TDD 사이클로 구현 | ||
| - 객체지향 생활 체조 원칙 준수 | ||
| - 테스트 작성하기 쉬운 구조로 설계 | ||
| - 체크리스트 및 피드백 준수 | ||
| - 기능 추가 시 컴파일 에러를 최소화하며 점진적인 리팩터링에 도전 | ||
| ## 기능 요구 사항 | ||
| - 현재 로또 생성기는 자동 생성 기능만 제공한다. 사용자가 수동으로 추첨 번호를 입력할 수 있도록 해야 한다. | ||
| - 입력한 금액, 자동 생성 숫자, 수동 생성 번호를 입력하도록 해야 한다. | ||
| ## 힌트 | ||
| - 규칙 3: 모든 원시값과 문자열을 포장한다. | ||
| - 로또 숫자 하나는 int 타입이다. 이 숫자 하나를 추상화한 LottoNo 객체를 추가해 구현한다. | ||
| - 예외 처리를 통해 프로그램이 중단되지 않고 다시 입력할 수 있도록 구현한다. | ||
| - 사용자가 잘못된 값을 입력했을 때 java exception으로 에러 처리를 한다. | ||
| - java8에 추가된 Optional을 적용해 NullPointerException이 발생하지 않도록 한다. | ||
| ## 프로그래밍 요구사항 | ||
| - 규칙 3: 모든 원시값과 문자열을 포장한다. | ||
| - 규칙 5: 줄여쓰지 않는다(축약 금지). | ||
| - 예외 처리를 통해 에러가 발생하지 않도록 한다. | ||
| - 모든 기능을 TDD로 구현해 단위 테스트가 존재해야 한다. 단, UI(System.out, System.in) 로직은 제외 | ||
| - java enum을 적용해 프로그래밍을 구현한다. | ||
| - 규칙 8: 일급 콜렉션을 쓴다. | ||
| - indent(인덴트, 들여쓰기) depth를 2를 넘지 않도록 구현한다. 1까지만 허용한다. | ||
| - 함수(또는 메소드)의 길이가 15라인을 넘어가지 않도록 구현한다. | ||
| - 자바 코드 컨벤션을 지키면서 프로그래밍한다. | ||
| - else 예약어를 쓰지 않는다. | ||
| ## PR 전 점검 | ||
| **[체크리스트 확인하기](checklist.md)** | ||
| ## 구현 기능 목록 | ||
| ### 공통 | ||
| - [x] 금액 (Money) | ||
| - [x] 0원 이상의 정수로 생성 | ||
| - [x] 금액 비교 (미만 여부) | ||
| - [x] 나눗셈 가능 여부 판단 | ||
| - [x] 나눗셈 연산 | ||
| - [x] 비율 계산 | ||
| - [x] 곱셈 연산 | ||
| - [x] 덧셈 연산 | ||
| - [x] 원화 문자열 변환 | ||
|
|
||
| ### 로또 번호 | ||
| - [x] 로또 번호 (LottoNumber) | ||
| - [x] 1~45 범위의 정수로 생성 | ||
| - [x] 문자열 입력으로 생성 | ||
| - [x] 범위 벗어날 시 예외 발생 | ||
| - [x] 동일 값은 캐싱된 인스턴스 반환 | ||
|
|
||
| ### 로또 | ||
| - [x] 로또 (Lotto) | ||
| - [x] 6개의 로또 번호로 생성 | ||
| - [x] 중복된 번호 존재 시 예외 발생 | ||
| - [x] 번호 누락 시 예외 발생 | ||
| - [x] 6개가 아닐 시 예외 발생 | ||
| - [x] 다른 로또와 일치하는 번호 개수 계산 | ||
| - [x] 특정 번호 포함 여부 판단 | ||
| - [x] 정렬된 출력용 문자열 반환 | ||
|
|
||
| ### 로또 구매 | ||
| - [x] 로또 구매 개수 (LottoCount) | ||
| - [x] 0 이상의 정수로 생성 | ||
| - [x] 문자열 입력으로 생성 | ||
| - [x] 빈 문자열일 시 예외 발생 | ||
| - [x] 음수일 시 예외 발생 | ||
| - [x] 다른 개수와 차감 연산 | ||
| - [x] 차감 결과가 음수일 시 예외 발생 | ||
|
|
||
| - [x] 로또 구매 금액 (LottoPurchaseAmount) | ||
| - [x] 금액으로 생성 | ||
| - [x] 문자열 입력으로 생성 | ||
| - [x] 정수 입력으로 생성 | ||
| - [x] 최소 금액(1,000원) 미달 시 예외 발생 | ||
| - [x] 1,000원 단위가 아닐 시 예외 발생 | ||
| - [x] 구매 가능한 로또 개수 계산 | ||
|
|
||
| - [x] 구매한 로또 목록 (PurchasedLottos) | ||
| - [x] 로또 목록으로 생성 | ||
| - [x] 빈 목록일 시 예외 발생 | ||
| - [x] 당첨 로또와 비교하여 결과 생성 | ||
|
|
||
| ### 당첨 판정 | ||
| - [x] 당첨 로또 (WinningLotto) | ||
| - [x] 로또와 보너스 번호로 생성 | ||
| - [x] 보너스 번호가 당첨 번호와 중복 시 예외 발생 | ||
| - [x] 구매 로또와 비교하여 등수 판정 | ||
|
|
||
| - [x] 등수 (LottoRank) | ||
| - [x] 일치 개수와 보너스 일치 여부로 등수 결정 | ||
| - [x] 3개 미만 일치 시 MISS 반환 | ||
| - [x] 3개 일치 시 FIFTH (5,000원) | ||
| - [x] 4개 일치 시 FOURTH (50,000원) | ||
| - [x] 5개 일치 시 THIRD (1,500,000원) | ||
| - [x] 5개 일치 + 보너스 일치 시 SECOND (30,000,000원) | ||
| - [x] 6개 일치 시 FIRST (2,000,000,000원) | ||
| - [x] 등수별 총 상금 계산 | ||
| - [x] 일치 개수 출력용 문자열 반환 | ||
| - [x] 상금 출력용 문자열 반환 | ||
|
|
||
| - [x] 로또 결과 (LottoResult) | ||
| - [x] 빈 상태로 생성 | ||
| - [x] 등수 추가 | ||
| - [x] 총 상금 계산 | ||
| - [x] 등수별 결과 출력용 문자열 반환 | ||
|
|
||
| - [x] 수익률 (ReturnRate) | ||
| - [x] 구매금액과 총 상금으로 생성 | ||
| - [x] 수익률 0 미만일 시 예외 발생 | ||
| - [x] 수익률 출력용 문자열 반환 | ||
|
|
||
| ### 유틸리티 | ||
| - [x] 자동 로또 생성기 (AutoBasedLottoGenerator) | ||
| - [x] 1~45 범위의 번호 풀 보유 | ||
| - [x] 지정 개수만큼 로또 생성 | ||
| - [x] 셔플 방식으로 6개 번호 선택 | ||
| - [x] 수동 로또 생성기 (ManualBasedLottoGenerator) | ||
| - [x] 문자열 목록을 입력받아 로또 목록 생성 | ||
|
|
||
| - [x] 로또 번호 파서 (LottoNumberParser) | ||
| - [x] 쉼표 구분 문자열을 로또 번호 목록으로 변환 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,100 @@ | ||
| package lotto; | ||
|
|
||
| import static lotto.view.InputView.*; | ||
| import static lotto.view.ResultView.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Supplier; | ||
| import lotto.domain.*; | ||
| import lotto.utils.AutoBasedLottoGenerator; | ||
| import lotto.utils.LottoNumberParser; | ||
| import lotto.view.InputView; | ||
| import lotto.view.ResultView; | ||
| import lotto.utils.ManualBasedLottoGenerator; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| LottoPurchaseAmount purchaseAmount = getPurchaseAmount(); | ||
| PurchasedLottos purchased = new PurchasedLottos(generateLottos(purchaseAmount)); | ||
| LottoPurchaseAmount purchaseAmount = createPurchaseAmount(); | ||
| PurchasedLottos purchased = purchaseLottos(purchaseAmount); | ||
|
|
||
| printPurchasedLottos(purchased); | ||
|
|
||
| LottoResult result = purchased.result(createWinningLotto()); | ||
|
|
||
| printResult(result, purchaseAmount); | ||
| } | ||
|
|
||
| private static LottoPurchaseAmount createPurchaseAmount() { | ||
| return retryUntilSuccess(() -> new LottoPurchaseAmount(readPurchaseAmount())); | ||
| } | ||
|
|
||
| private static PurchasedLottos purchaseLottos(LottoPurchaseAmount purchaseAmount) { | ||
| LottoCount totalCount = purchaseAmount.lottoCount(); | ||
| LottoCount manualCount = createManualLottoCount(totalCount); | ||
| LottoCount autoCount = totalCount.subtract(manualCount); | ||
|
|
||
| printPurchaseCount(manualCount, autoCount); | ||
|
|
||
| return mergeLottos(createManualLottos(manualCount), generateAutoLottos(autoCount)); | ||
| } | ||
|
|
||
| private static LottoCount createManualLottoCount(LottoCount totalCount) { | ||
| return retryUntilSuccess(() -> { | ||
| LottoCount manualCount = new LottoCount(readManualLottoCount()); | ||
| totalCount.validateSubtractable(manualCount); | ||
| return manualCount; | ||
| }); | ||
| } | ||
|
|
||
| private static List<Lotto> createManualLottos(LottoCount manualCount) { | ||
| return retryUntilSuccess( | ||
| () -> new ManualBasedLottoGenerator().generate(readManualLottoNumbers(manualCount.value()))); | ||
| } | ||
|
|
||
| ResultView.printPurchaseCount(purchased); | ||
| ResultView.printPurchasedLottos(purchased); | ||
| private static List<Lotto> generateAutoLottos(LottoCount autoCount) { | ||
| return new AutoBasedLottoGenerator().generate(autoCount); | ||
| } | ||
|
|
||
| LottoResult result = purchased.result(getWinningLotto()); | ||
| private static PurchasedLottos mergeLottos(List<Lotto> manual, List<Lotto> auto) { | ||
| List<Lotto> all = new ArrayList<>(manual); | ||
| all.addAll(auto); | ||
| return new PurchasedLottos(all); | ||
| } | ||
|
|
||
| ResultView.printResult(result, purchaseAmount); | ||
| private static WinningLotto createWinningLotto() { | ||
| Lotto lotto = createLottoForWinning(); | ||
| LottoNumber bonus = createBonusNumber(lotto); | ||
| return new WinningLotto(lotto, bonus); | ||
| } | ||
|
|
||
| private static LottoPurchaseAmount getPurchaseAmount() { | ||
| return new LottoPurchaseAmount(InputView.readPurchaseAmount()); | ||
| private static Lotto createLottoForWinning() { | ||
| return retryUntilSuccess(() -> new Lotto(createWinningNumbers())); | ||
| } | ||
|
|
||
| private static List<Lotto> generateLottos(LottoPurchaseAmount purchaseAmount) { | ||
| return new AutoBasedLottoGenerator().generate(purchaseAmount.lottoCount()); | ||
| private static List<LottoNumber> createWinningNumbers() { | ||
| return new LottoNumberParser().parse(readWinningNumbers()); | ||
| } | ||
|
|
||
| private static WinningLotto getWinningLotto() { | ||
| String winningNumbers = InputView.readWinningNumbers(); | ||
| String bonusNumber = InputView.readBonusNumber(); | ||
| private static LottoNumber createBonusNumber(Lotto lotto) { | ||
| return retryUntilSuccess(() -> { | ||
| LottoNumber bonus = LottoNumber.of(readBonusNumber()); | ||
| validateBonusNotDuplicated(lotto, bonus); | ||
| return bonus; | ||
| }); | ||
| } | ||
|
|
||
| private static void validateBonusNotDuplicated(Lotto lotto, LottoNumber bonus) { | ||
| if (lotto.contains(bonus)) { | ||
| throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복될 수 없습니다."); | ||
| } | ||
| } | ||
|
|
||
| List<LottoNumber> numbers = new LottoNumberParser().parse(winningNumbers); | ||
| return new WinningLotto(numbers, bonusNumber); | ||
| private static <T> T retryUntilSuccess(Supplier<T> action) { | ||
| while (true) { | ||
| try { | ||
| return action.get(); | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,36 @@ | ||
| package lotto.domain; | ||
|
|
||
| public record LottoCount(int value) { | ||
| private static final int MIN_COUNT = 1; | ||
|
|
||
| public LottoCount(String input) { | ||
| this(toInt(input)); | ||
| } | ||
|
|
||
| private static int toInt(String input) { | ||
| if (input == null || input.isBlank()) { | ||
| throw new IllegalArgumentException("구매 개수는 필수입니다."); | ||
| } | ||
| return Integer.parseInt(input); | ||
| } | ||
|
|
||
| public LottoCount { | ||
| if (value < MIN_COUNT) { | ||
| throw new IllegalArgumentException("로또 구매 개수는 %d개 이상이어야 합니다.".formatted(MIN_COUNT)); | ||
| if (value < 0) { | ||
| throw new IllegalArgumentException("구매 개수는 0이상이어야 합니다."); | ||
| } | ||
| } | ||
|
|
||
| public LottoCount subtract(LottoCount other) { | ||
| validateSubtractable(other); | ||
| return new LottoCount(this.value - other.value); | ||
| } | ||
|
|
||
| public void validateSubtractable(LottoCount other) { | ||
| if (isLessThan(other)) { | ||
| throw new IllegalArgumentException("차감할 수 없습니다."); | ||
| } | ||
| } | ||
|
|
||
| private boolean isLessThan(LottoCount other) { | ||
| return this.value < other.value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package lotto.utils; | ||
|
|
||
| import java.util.List; | ||
| import lotto.domain.Lotto; | ||
|
|
||
| public class ManualBasedLottoGenerator { | ||
|
Contributor
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. 👍 |
||
|
|
||
| private final LottoNumberParser parser; | ||
|
|
||
| public ManualBasedLottoGenerator() { | ||
| this(new LottoNumberParser()); | ||
| } | ||
|
|
||
| private ManualBasedLottoGenerator(LottoNumberParser parser) { | ||
| this.parser = parser; | ||
| } | ||
|
|
||
| public List<Lotto> generate(List<String> inputs) { | ||
| return inputs.stream().map(parser::parse).map(Lotto::new).toList(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Application이 담당하는 책임이 너무 많아진 느낌
Lotto를 생성을 담당하는 책임을 별도의 객체로 분리해 보면 어떨까?
분리할 때 다음 피드백 참고해본다.
3단계에서 4단계로 요구사항이 변경될 때 로또를 생성하는 부분의 요구사항만 변경됐다.
로또를 생성하는 부분을 다음과 같은 구조의 인터페이스로 분리해 보는 연습을 해보면 어떨까?
이와 같이 인터페이스로 구현했을 때의 잇점에 대해 고민해 보는 시간을 가져본다.