-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step4 : 로또(수동) #4226
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
Mo-bile
wants to merge
17
commits into
next-step:mo-bile
Choose a base branch
from
Mo-bile:step4
base: mo-bile
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
Step4 : 로또(수동) #4226
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
276d063
feat : 캐시이용을 원할하게 하기위해 LottoNumber 의 생성방식을 정적팩터리 메서드로 변경하고, LottoNumb…
f753801
doc : step4 과제 관련 기능목록 작성
77b6b2a
feat : 로또 수동 구매 갯수 지정 및 로또 수동발행 개발완료
b0f7b63
feat : 수동입력 추가한 로또 기능 개발
2b0a81d
feat :
77b813b
refactor : 정수대신 문자열 입력으로 변경하여 미입력 시 재 입력 로직 준비
a487493
refactor : 클래스명 수정
cc149cb
refactor
8c8a98f
fix : 수동입력 미반영 문제 해결
70ac1f6
feat : 잘못입력시 재 입력 유도하게 기능 추가
a4bb709
refactor : main 부분 메서드 정리
0419968
refactor : LottoNumber 와 LottoCache 를 통합하여 LottoNumber 에서 of로 새로운 객체를…
76893fe
fix : LottoNumber 생성자 접근을 차단하기 위해 record 에서 class 로 변경
85b53db
feat : 로또 생성방식을 쉽게 갈아끼우기 위한 인터페이스 & 구현(혼합 + 수동 + 자동)개발 완료
ca2a55d
fix : 잘못된 구현체 주입순서로 무조건 `LottoCombineGenerator` 가 주입되는 문제 해결
c351e9a
refactor : 복잡해진 인터페이스를 단순화하여서 변경
a9faec2
refactor : controller에서 interface 분리 원칙에 부합하지않는 조건문 분기로 구현체 정하는 방식 제거
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 |
|---|---|---|
| @@ -1,22 +1,59 @@ | ||
| package lotto.controller; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.IntStream; | ||
| import lotto.domain.business.LottoBuy; | ||
| import lotto.domain.business.LottoGame; | ||
| import lotto.domain.model.LottoGenerator; | ||
| import lotto.domain.model.WinningResult; | ||
| import lotto.domain.model.impl.LottoCombineGenerator; | ||
| import lotto.view.InputView; | ||
| import lotto.view.ResultView; | ||
|
|
||
| public class LottoApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| int pay = InputView.inputPurchaseAmount(); | ||
| new LottoApplication().run(); | ||
| } | ||
|
|
||
| private void run() { | ||
| while(true) { | ||
| try { | ||
| play(); | ||
| return; | ||
| } catch(IllegalArgumentException e) { | ||
| ResultView.printError(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void play() { | ||
| String pay = InputView.inputPurchaseAmount(); | ||
| String manualCount = InputView.inputManulNumber(); | ||
|
|
||
| LottoGame lottoGame = new LottoGame(pay); | ||
| List<String> manualLottoNumbers = readManualLottos(manualCount); | ||
| LottoGenerator lottoGenerator = new LottoCombineGenerator(pay, manualLottoNumbers); | ||
| LottoBuy lottoBuy = lottoGenerator.generate(); | ||
|
|
||
| ResultView.printAutoManualCount(lottoBuy.combineBuyCount()); | ||
| LottoGame lottoGame = new LottoGame(pay, lottoBuy.combineLotto()); | ||
| ResultView.printLottos(lottoGame.getLottos()); | ||
|
|
||
| String winningNumbers = InputView.inputWinningNumbers(); | ||
| int bonusNumbers = InputView.inputBonusNumbers(); | ||
| WinningResult winningResult = lottoGame.calculateWinningResult(winningNumbers, bonusNumbers); | ||
| WinningResult winningResult = lottoGame.calculateWinningResult(InputView.inputWinningNumbers(), InputView.inputBonusNumbers()); | ||
| String totalReturn = winningResult.calculateTotalReturn(pay); | ||
| ResultView.printResult(winningResult, totalReturn); | ||
| } | ||
|
|
||
| private List<String> readManualLottos(String count) { | ||
| if(count.isEmpty()) { | ||
| throw new IllegalArgumentException("수동 구매 수는 0 이상의 숫자로 입력해 주세요."); | ||
| } | ||
| int manualCount = Integer.parseInt(count); | ||
| if(manualCount > 0) { | ||
| InputView.inputManulMessage(); | ||
| } | ||
| return IntStream.range(0, manualCount) | ||
| .mapToObj(i -> InputView.inputManulLotto()) | ||
| .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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package lotto.domain.business; | ||
|
|
||
| import java.util.stream.Stream; | ||
| import lotto.domain.model.Auto; | ||
| import lotto.domain.model.BuyCount; | ||
| import lotto.domain.model.LottoTickets; | ||
| import lotto.domain.model.Manual; | ||
|
|
||
| public record LottoBuy(BuyCount buyCount, Manual manual, Auto auto) { | ||
|
|
||
| public LottoTickets combineLotto() { | ||
| return new LottoTickets(Stream.concat( | ||
| this.manual.manualLottoList().stream(), | ||
| this.auto.autoLottoList().stream()) | ||
| .toList()); | ||
| } | ||
|
|
||
| public BuyCount combineBuyCount() { | ||
| return this.buyCount; | ||
| } | ||
| } | ||
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,19 @@ | ||
| package lotto.domain.model; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public record Auto(List<Lotto> autoLottoList) { | ||
|
|
||
| public Auto(int autoNumber) { | ||
| this(generateLottos(autoNumber)); | ||
| } | ||
|
|
||
| private static List<Lotto> generateLottos(int num) { | ||
| return IntStream | ||
| .range(0, num) | ||
| .mapToObj(i -> new Lotto()) | ||
| .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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package lotto.domain.model; | ||
|
|
||
| public record BuyCount(int total, int manual, int auto) { | ||
|
|
||
| public BuyCount { | ||
| validate(total, manual, auto); | ||
| } | ||
|
|
||
| void validate(int total, int manual, int auto) { | ||
| if(total != (manual + auto)) { | ||
| throw new IllegalArgumentException("지불금액 대비 구매수가 일치하지 않습니다."); | ||
| } | ||
| } | ||
| } |
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,8 @@ | ||||||
| package lotto.domain.model; | ||||||
|
|
||||||
| import lotto.domain.business.LottoBuy; | ||||||
|
|
||||||
| public interface LottoGenerator { | ||||||
|
|
||||||
| LottoBuy generate(); | ||||||
|
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.
Suggested change
LottoBuy를 생성해 반환하다보니 Auto인 경우에도 Manual, Manual인 경우에도 Auto 값을 가짐 |
||||||
| } | ||||||
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,18 +1,71 @@ | ||
| package lotto.domain.model; | ||
|
|
||
| public record LottoNumber(int value) { | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class LottoNumber { | ||
|
|
||
| private final int value; | ||
| private final static Map<Integer, LottoNumber> cache; | ||
| public static final int MIN_LOTTO_NUMBER = 1; | ||
| public static final int MAX_LOTTO_NUMBER = 45; | ||
|
|
||
| public LottoNumber(String value) { | ||
| static { | ||
| cache = getLottoNumberMap(); | ||
| } | ||
|
|
||
| private static Map<Integer, LottoNumber> getLottoNumberMap() { | ||
| return IntStream.rangeClosed(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER) | ||
| .boxed() | ||
| .collect( | ||
| Collectors.toMap( | ||
| Function.identity(), | ||
| LottoNumber::new) | ||
| ); | ||
| } | ||
|
|
||
| private LottoNumber(String value) { | ||
| this(Integer.parseInt(value)); | ||
| } | ||
|
|
||
| public LottoNumber { | ||
| validate(value); | ||
| private LottoNumber(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public static LottoNumber of(int value) { | ||
Mo-bile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LottoNumber cachedLottoNumber = cache.get(value); | ||
| validate(cachedLottoNumber); | ||
| return cachedLottoNumber; | ||
| } | ||
|
|
||
| private void validate(int value) { | ||
| if(value < 1 || value > 45) { | ||
| public static LottoNumber of(String value) { | ||
| return of(Integer.parseInt(value)); | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| private static void validate(LottoNumber result) { | ||
| if(result == null) { | ||
| throw new IllegalArgumentException("로또 번호는 1~45사이 입력하시오"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if(o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| LottoNumber that = (LottoNumber) o; | ||
| return value == that.value; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(value); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.