-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step4 - 로또(수동) #4241
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
Merged
Merged
Step4 - 로또(수동) #4241
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
6b373c8
[refactor] LottoNumberCache를 제거하고 LottoNumber에 정적 팩토리 메소드를 추가
GunBros e97eb4f
[feat] 수동 로또 수 입력 UI 추가
GunBros 776b1b6
[feat] LottoCount 클래스 구현
GunBros db48a73
[refactor] LottoCount 리팩토링
GunBros 956c6ed
[feat] 구매 가격을 통한 LottoCount 생성자 추가
GunBros 46c7cc1
[feat] LottoCount 대소 비교 함수 구현
GunBros 86081b3
[feat] 수동숫자 입력 UI 구현
GunBros e7ce95e
[feat] LottoCount 증가 구현
GunBros 6e8b44c
[feat] 수동 로또 번호 입력 처리
GunBros 4b2bb27
[chore] 불필요한 print 제거
GunBros 40727e7
[feat] 로또 개수의 차이를 구하는 메소드
GunBros 81f5688
[feat] 카운트가 같은지 체크하는 메소드
GunBros 9c64390
[refactor] price 대신 LottoCount를 사용하도록 수정
GunBros 7476206
[feat] LottoCount toString 추가
GunBros c0d561a
[feat] toCountString을 사용하도록 수정
GunBros 32f9ffe
[feat] 로또 테스트 수정
GunBros 9efa8c3
[feat] 수동 로또 추가
GunBros 2a09ff9
[feat] 수동 로또 로직 추가
GunBros 4b0c25f
[docs] 수동 로또 문서 추가
GunBros 2057c2a
[refactor] LottoGenerator 인터페이스 추가
GunBros b19f25e
[refactor] Lotto에서 count 필드 제거
GunBros c71a1c3
[refactor] LottoCount 관리하는 Controller 분리
GunBros 36a9637
[refactor] 수동과 자동 분리
GunBros 27b45bb
[refactor] 로또 생성자 추가
GunBros 583bc6b
[refactor] 로또 개수 UI 수정
GunBros 4f35f19
[refactor] 로또 앱 수정
GunBros 39131b1
[style] 코드 포매팅
GunBros 9a168a4
[refactor] 불필요한 코드 삭제
GunBros 168d93c
[feat] 로또 개수 예외 처리 추가
GunBros 748557d
[chore] 에러 메시지 수정
GunBros afdc5c2
[feat] 에러 메시지 출력 UI 구현
GunBros c0aab8d
[feat] 에러 처리 추가
GunBros 5cb08fb
[refactor] LottCountController 제거
GunBros 2ab95e2
[refactor] View에서 입력받은 결과를 리턴하도록 수정
GunBros e8a9c67
[refactor] 가격에서 로또 개수 차이를 구하는 함수 구현
GunBros 640e1ca
[refactor] LottoPrice에서 예외 처리
GunBros bd424a4
[refactor] 컴포지트 패턴을 활용한 제너레이터 생성
GunBros f4aeb2d
[refactor] 테스트 수정
GunBros 5cb65ea
[refactor] Application 수정
GunBros fb5fc18
[style] 코드 포매팅
GunBros bab2015
[refactor] AutoLottoMachine으로 리팩토링
GunBros 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
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,30 +1,39 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.controller.LottoMachine; | ||
| import lotto.domain.WinningLotto; | ||
| import lotto.domain.Lotto; | ||
| import lotto.domain.LottoNumber; | ||
| import lotto.domain.LottoResult; | ||
| import lotto.controller.LottosBundleGenerator; | ||
| import lotto.domain.*; | ||
| import lotto.ui.InputView; | ||
| import lotto.ui.ResultView; | ||
| import lotto.util.LottoNumberParser; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class LottoApplication { | ||
| public static void main(String[] args) { | ||
| String purchaseAmount = InputView.getPurchaseAmount(); | ||
| try { | ||
| String purchaseAmount = InputView.getPurchaseAmount(); | ||
| String manualLottoCount = InputView.getManualNumberCount(); | ||
|
|
||
| LottoPrice price = new LottoPrice(purchaseAmount); | ||
| LottoCount manualCount = new LottoCount(manualLottoCount); | ||
|
|
||
| InputView.printStartManualNumbersInput(); | ||
| List<String> manualLottos = InputView.getManualLottosNumbers(new LottoCount(manualLottoCount)); | ||
|
|
||
| LottoMachine lottoMachine = new LottoMachine(purchaseAmount); | ||
| Lotto lotto = lottoMachine.generate(); | ||
| LottosBundleGenerator lottosBundleGenerator = new LottosBundleGenerator(price, manualCount, manualLottos); | ||
| Lotto lotto = lottosBundleGenerator.generate(); | ||
|
|
||
| ResultView.printLotto(lotto); | ||
| ResultView.printLotto(manualCount, manualCount.diffFromPrice(price), lotto); | ||
|
|
||
| String winningNumbers = InputView.getWinningNumber(); | ||
| String bonusNumber = InputView.getBonusNumber(); | ||
| String winningNumbers = InputView.getWinningNumber(); | ||
| String bonusNumber = InputView.getBonusNumber(); | ||
|
|
||
| WinningLotto winningLotto = new WinningLotto(winningNumbers, bonusNumber); | ||
| LottoResult result = lotto.getMatchResult(winningLotto); | ||
| WinningLotto winningLotto = new WinningLotto(winningNumbers, bonusNumber); | ||
| LottoResult result = lotto.getMatchResult(winningLotto); | ||
|
|
||
| ResultView.printLottoResult(result.toString()); | ||
| ResultView.printProfit(result.getProfit()); | ||
| ResultView.printLottoResult(result.toString()); | ||
| ResultView.printProfit(result.getProfit()); | ||
| } catch (Exception ex) { | ||
| ResultView.printError(ex.getMessage()); | ||
| } | ||
| } | ||
| } |
20 changes: 8 additions & 12 deletions
20
...n/java/lotto/controller/LottoMachine.java → ...va/lotto/controller/AutoLottoMachine.java
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,32 +1,28 @@ | ||
| package lotto.controller; | ||
|
|
||
| import lotto.domain.Lotto; | ||
| import lotto.domain.LottoCount; | ||
| import lotto.domain.LottoNumbers; | ||
| import lotto.domain.LottoPrice; | ||
| import lotto.util.LottoNumberParser; | ||
| import lotto.util.RandomNumbersGenerator; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class LottoMachine { | ||
| private final LottoPrice lottoPrice; | ||
| public class AutoLottoMachine implements LottoGenerator { | ||
| private final LottoCount autoCount; | ||
|
|
||
| public LottoMachine(String price) { | ||
| this(new LottoPrice(price)); | ||
| } | ||
|
|
||
| public LottoMachine(LottoPrice lottoPrice) { | ||
| this.lottoPrice = lottoPrice; | ||
| public AutoLottoMachine(LottoCount autoCount) { | ||
| this.autoCount = autoCount; | ||
| } | ||
|
|
||
| @Override | ||
| public Lotto generate() { | ||
| List<LottoNumbers> lottoNumbersList = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < lottoPrice.count(); i++) { | ||
| for (int i = 0; !autoCount.isCountSame(i); i++) { | ||
| lottoNumbersList.add(new LottoNumbers(LottoNumberParser.parse(RandomNumbersGenerator.randomNumbers()))); | ||
| } | ||
|
|
||
| return new Lotto(lottoPrice, lottoNumbersList); | ||
| return new Lotto(lottoNumbersList); | ||
| } | ||
| } |
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,7 @@ | ||
| package lotto.controller; | ||
|
|
||
| import lotto.domain.Lotto; | ||
|
|
||
| public interface LottoGenerator { | ||
| Lotto generate(); | ||
| } |
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,34 @@ | ||
| package lotto.controller; | ||
|
|
||
| import lotto.domain.Lotto; | ||
| import lotto.domain.LottoCount; | ||
| import lotto.domain.LottoPrice; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class LottosBundleGenerator implements LottoGenerator { | ||
| private final List<LottoGenerator> lottosGenerators; | ||
|
|
||
| public LottosBundleGenerator(LottoPrice price, LottoCount manualCount, List<String> manualLottoText) { | ||
| this(toLottosGenerators(price, manualCount, manualLottoText)); | ||
| } | ||
|
|
||
| public LottosBundleGenerator(List<LottoGenerator> lottosGenerators) { | ||
| this.lottosGenerators = lottosGenerators; | ||
| } | ||
|
|
||
| private static List<LottoGenerator> toLottosGenerators(LottoPrice price, LottoCount manualCount, List<String> manualLottoText) { | ||
| return List.of(new AutoLottoMachine(manualCount.diffFromPrice(price)), new ManualLottoMachine(manualLottoText)); | ||
| } | ||
|
|
||
| @Override | ||
| public Lotto generate() { | ||
| Lotto lotto = new Lotto(); | ||
|
|
||
| for (LottoGenerator lottosGenerator : lottosGenerators) { | ||
| lotto = new Lotto(lotto, lottosGenerator.generate()); | ||
| } | ||
|
|
||
| return lotto; | ||
| } | ||
| } | ||
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,27 @@ | ||
| package lotto.controller; | ||
|
|
||
| import lotto.domain.Lotto; | ||
| import lotto.domain.LottoNumbers; | ||
| import lotto.util.LottoNumberParser; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class ManualLottoMachine implements LottoGenerator { | ||
| private final List<String> lottoNumbersList; | ||
|
|
||
| public ManualLottoMachine(List<String> manualLottoNumbers) { | ||
| this.lottoNumbersList = manualLottoNumbers; | ||
| } | ||
|
|
||
| @Override | ||
| public Lotto generate() { | ||
| List<LottoNumbers> lottoNumbersList = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < this.lottoNumbersList.size(); i++) { | ||
| lottoNumbersList.add(new LottoNumbers(LottoNumberParser.parse(this.lottoNumbersList.get(i)))); | ||
| } | ||
|
|
||
| return new Lotto(lottoNumbersList); | ||
| } | ||
| } |
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,44 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class LottoCount { | ||
| private final int count; | ||
|
|
||
| public LottoCount(String count) { | ||
| this(Integer.parseInt(count)); | ||
| } | ||
|
|
||
| public LottoCount(int count) { | ||
| this.count = count; | ||
| } | ||
|
|
||
| public LottoCount increase() { | ||
| return new LottoCount(this.count + 1); | ||
| } | ||
|
|
||
| public boolean isCountSame(int count) { | ||
| return this.count == count; | ||
| } | ||
|
|
||
| public LottoCount diffFromPrice(LottoPrice price) { | ||
| return new LottoCount(price.count() - count); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| LottoCount that = (LottoCount) o; | ||
| return count == that.count; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(count); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.valueOf(count); | ||
| } | ||
| } |
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,39 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| public class LottoNumber { | ||
| private final int number; | ||
| private static final int MIN = 1; | ||
| private static final int MAX = 45; | ||
| private static final LottoNumber NONE = new LottoNumber(0); | ||
| private static final Map<Integer, LottoNumber> Cache = new HashMap<>(); | ||
|
|
||
| public LottoNumber(String number) { | ||
| this(Integer.parseInt(number)); | ||
| static { | ||
| for (int i = MIN; i <= MAX; i++) { | ||
| Cache.put(i, new LottoNumber(i)); | ||
| } | ||
| } | ||
|
|
||
| public LottoNumber(int number) { | ||
| private final int number; | ||
|
|
||
| private LottoNumber(int number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| public static LottoNumber get(int value) { | ||
|
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. 👍 |
||
| LottoNumber lottoNumber = Cache.getOrDefault(value, NONE); | ||
| if (lottoNumber == NONE) { | ||
| throw new IllegalArgumentException("로또의 숫자는 1에서 45 사이여야합니다."); | ||
| } | ||
| return Cache.get(value); | ||
| } | ||
|
|
||
| public static LottoNumber get(String value) { | ||
| return get(Integer.parseInt(value)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
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
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.
manualLottoText 크기가 manualCount인데 manualCount는 필요없는 값이지 않을까?