-
Notifications
You must be signed in to change notification settings - Fork 33
[Tany / Ader] 로또 3단계 - 수동구매 기능 추가 #54
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: tanyader
Are you sure you want to change the base?
Changes from all commits
7ac1b17
e3260d5
d2c7205
3eee1a0
fac2a32
abd182d
689f51f
ed14528
59c1681
344616b
677819d
b488b0a
d37b794
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,15 +1,14 @@ | ||
| package lotto.controller; | ||
|
|
||
| import lotto.domain.LottoPaper; | ||
| import lotto.domain.LottoStore; | ||
| import lotto.domain.WinningStrategy; | ||
| import lotto.domain.*; | ||
| import lotto.view.InputView; | ||
| import lotto.view.OutputView; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class LottoGame { | ||
|
|
||
|
|
@@ -19,74 +18,53 @@ public void start() { | |
| InputView.init(); | ||
|
|
||
| int purchaseAmount = InputView.getPurchaseAmount(); | ||
| LottoPaper lottoPaper = purchase(purchaseAmount); | ||
| int manualLottoCount = InputView.getManualLottoCount(); | ||
|
|
||
| List<WinningStrategy> winningStrategies = checkWinning(lottoPaper); | ||
| LottoPaper lottoPaper = purchase( | ||
| purchaseAmount, | ||
| manualLottoCount, | ||
| getManualNumbers(InputView.getManualLottoNumbers(manualLottoCount))); | ||
|
|
||
| calculateWinningStats(winningStrategies, purchaseAmount); | ||
| calculateWinningStats(checkWinning(lottoPaper), purchaseAmount); | ||
|
|
||
| InputView.close(); | ||
| } | ||
|
|
||
| private LottoPaper purchase(int purchaseAmount) { | ||
| LottoStore lottoStore = new LottoStore(); | ||
| LottoPaper lottoPaper = lottoStore.purchase(purchaseAmount); | ||
| private LottoPaper purchase(int purchaseAmount, int manualLottoCount, Map<Integer, List<Integer>> numbers) { | ||
| LottoPaper lottoPaper = new LottoStore().purchase(purchaseAmount, manualLottoCount, numbers); | ||
|
|
||
| OutputView.printPurchaseCount(lottoPaper.getLottoSize()); | ||
| OutputView.printPurchaseCount(manualLottoCount, lottoPaper.getLottoSize() - manualLottoCount); | ||
| OutputView.printLottoPaper(lottoPaper.showLottoPaper()); | ||
|
|
||
| return lottoPaper; | ||
| } | ||
|
|
||
| private List<WinningStrategy> checkWinning(LottoPaper lottoPaper) { | ||
| String inputWinningNumbers = InputView.getRequiredWinningNumber(); | ||
| String inputBonusNumber = InputView.getBonusBallNumber(); | ||
| List<Integer> winningNumbers = getNumbers(InputView.getRequiredWinningNumber()); | ||
| int bonusNumber = getBonusNumber(InputView.getBonusBallNumber(winningNumbers)); | ||
|
|
||
| List<Integer> winningNumbers = getWinningNumbers(inputWinningNumbers); | ||
| int bonusNumber = getBonusNumber(inputBonusNumber); | ||
|
|
||
| List<Integer> correctNumberCounts = lottoPaper.judgeWinning(winningNumbers); | ||
| List<Boolean> hasBonusNumbers = lottoPaper.hasBonusNumbers(bonusNumber); | ||
|
|
||
| return IntStream.range(0, correctNumberCounts.size()) | ||
| .mapToObj(index -> convertMatchNumberToWinningStrategy(correctNumberCounts.get(index), hasBonusNumbers.get(index))) | ||
| .collect(Collectors.toList()); | ||
| return lottoPaper.checkWinning(new WinningLotto(winningNumbers, bonusNumber)); | ||
| } | ||
|
|
||
| private List<Integer> getWinningNumbers(String inputWinningNumbers) { | ||
| return Arrays.stream(inputWinningNumbers.split(NUMBER_DELIMITER)) | ||
| private List<Integer> getNumbers(String inputNumbers) { | ||
| return Arrays.stream(inputNumbers.split(NUMBER_DELIMITER)) | ||
| .mapToInt(Integer::parseInt) | ||
| .boxed() | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private int getBonusNumber(String inputBonusNumber) { | ||
| return Integer.parseInt(inputBonusNumber); | ||
| } | ||
| private Map<Integer, List<Integer>> getManualNumbers(Map<Integer, String> inputNumbers) { | ||
|
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.
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. Map 자료구조를 사용한 이유는 다음과 같습니다!
<추가설명> |
||
| Map<Integer, List<Integer>> manualNumbers = new HashMap<>(); | ||
|
|
||
| private WinningStrategy convertMatchNumberToWinningStrategy(int matchNumber, boolean hasBonusNumber) { | ||
| if (matchNumber == WinningStrategy.ZERO_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.ZERO_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.ONE_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.ONE_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.TWO_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.TWO_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.THREE_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.THREE_MATCHES; | ||
| for (int i = 0; i < inputNumbers.size(); i++) { | ||
| manualNumbers.put(i, getNumbers(inputNumbers.get(i))); | ||
| } | ||
| if (matchNumber == WinningStrategy.FOUR_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.FOUR_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.FIVE_MATCHES.getCorrectNumber() && hasBonusNumber) { | ||
| return WinningStrategy.FIVE_WITH_BONUS_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.FIVE_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.FIVE_MATCHES; | ||
| } | ||
| return WinningStrategy.SIX_MATCHES; | ||
|
|
||
| return manualNumbers; | ||
| } | ||
|
|
||
| private int getBonusNumber(String inputBonusNumber) { | ||
| return Integer.parseInt(inputBonusNumber); | ||
| } | ||
|
|
||
| private void calculateWinningStats(List<WinningStrategy> winningStrategies, int purchaseAmount) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class AutoLotto extends Lotto { | ||
| private static final int PICK_NUMBER_LENGTH = 6; | ||
|
|
||
| public AutoLotto(List<Integer> numbers) { | ||
| super(numbers); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Integer> createLotto(List<Integer> numbers) { | ||
| return pickSixNumbers(numbers); | ||
| } | ||
|
Comment on lines
+14
to
+17
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. 지금 구현을 보면... 파라메터로는 항상 1부터 45까지의 숫자가 들어오는 건가요?
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. 자동로또 생성 시에는 항상 1부터 45의 숫자가 들어오게 됩니다. 😃 |
||
|
|
||
| private List<Integer> pickSixNumbers(List<Integer> numbers) { | ||
| Collections.shuffle(numbers); | ||
|
|
||
| return numbers.stream() | ||
| .limit(PICK_NUMBER_LENGTH) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,23 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Lotto { | ||
| public abstract class Lotto { | ||
|
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. 아까
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. 1부터 45의 숫자가 |
||
|
|
||
| private final List<Integer> numbers; | ||
|
|
||
| public Lotto(List<Integer> numbers) { | ||
| this.numbers = pickSixNumbers(numbers); | ||
| this.numbers = createLotto(numbers); | ||
| } | ||
|
|
||
| private List<Integer> pickSixNumbers(List<Integer> numbers) { | ||
| int pickNumberLength = 6; | ||
|
|
||
| Collections.shuffle(numbers); | ||
|
|
||
| return numbers.stream() | ||
| .limit(pickNumberLength) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
| public abstract List<Integer> createLotto(List<Integer> numbers); | ||
|
|
||
| public String showLottoNumbers() { | ||
| return numbers.toString(); | ||
| } | ||
|
|
||
| public int getCorrectNumberCount(List<Integer> winningNumbers) { | ||
| return winningNumbers.stream() | ||
| .mapToInt(this::correctNumber) | ||
| .sum(); | ||
| public boolean hasNumber(int number) { | ||
| return numbers.contains(number); | ||
| } | ||
|
|
||
| private int correctNumber(int number) { | ||
|
|
@@ -39,7 +27,11 @@ private int correctNumber(int number) { | |
| return 0; | ||
| } | ||
|
|
||
| public boolean hasBonusNumber(int bonusNumber) { | ||
| return numbers.contains(bonusNumber); | ||
| public MatchLottoResult match(WinningLotto winningLotto) { | ||
| int matchCount = (int)numbers.stream() | ||
| .filter(value -> winningLotto.hasNumber(value)) | ||
| .count(); | ||
|
|
||
| return new MatchLottoResult(matchCount, numbers.contains(winningLotto.getBonusNumber())); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ManualLotto extends Lotto { | ||
|
|
||
| public ManualLotto(List<Integer> numbers) { | ||
| super(numbers); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Integer> createLotto(List<Integer> numbers) { | ||
| return numbers.stream() | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package lotto.domain; | ||
|
|
||
| public class MatchLottoResult { | ||
|
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 int matchNumberCount; | ||
| private boolean hasBonus; | ||
|
|
||
| public MatchLottoResult(int matchNumberCount, boolean hasBonus) { | ||
| this.matchNumberCount = matchNumberCount; | ||
| this.hasBonus = hasBonus; | ||
| } | ||
|
|
||
| public int getMatchNumberCount() { | ||
| return matchNumberCount; | ||
| } | ||
|
|
||
| public boolean hasBonus() { | ||
| return hasBonus; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class WinningLotto extends Lotto { | ||
|
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. 번호 일치 카운트를 알려주는 메소드도 있으면 좋겠네요!
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. 수정 시 고려하겠습니다 😄 |
||
|
|
||
| private final int bonusNumber; | ||
|
|
||
| public WinningLotto(List<Integer> numbers, int bonusNumber) { | ||
| super(numbers); | ||
| this.bonusNumber = bonusNumber; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Integer> createLotto(List<Integer> numbers) { | ||
| return numbers.stream() | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public int getBonusNumber() { | ||
| return bonusNumber; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,4 +26,29 @@ public int getWinningPrice() { | |
| public int getCorrectNumber() { | ||
| return this.correctNumber; | ||
| } | ||
|
|
||
| public static WinningStrategy convertMatchNumberToWinningStrategy(int matchNumber, boolean hasBonusNumber) { | ||
|
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. 헉 어떻게 좀 개선할 수 있는 방법이 없을까요? 전략 패턴을 사용하기에 좋은 케이스도 아닌듯 보입니다... 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 이름입니다...
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. 그룹리뷰때도 한 분이 네이밍만 보고 전략패턴으로 오해하신 부분입니다 ㅠㅠㅠ |
||
| if (matchNumber == WinningStrategy.ZERO_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.ZERO_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.ONE_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.ONE_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.TWO_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.TWO_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.THREE_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.THREE_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.FOUR_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.FOUR_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.FIVE_MATCHES.getCorrectNumber() && hasBonusNumber) { | ||
| return WinningStrategy.FIVE_WITH_BONUS_MATCHES; | ||
| } | ||
| if (matchNumber == WinningStrategy.FIVE_MATCHES.getCorrectNumber()) { | ||
| return WinningStrategy.FIVE_MATCHES; | ||
|
Comment on lines
+31
to
+50
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. 스트림 API로 깔끔하게 개선할 수 있습니다. 시도해보시죠!
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. 스트림으로 한번 개선해보겠습니다! 키워드 감사합니다 👍 |
||
| } | ||
| return WinningStrategy.SIX_MATCHES; | ||
|
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. 만약에
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. 의도한 숫자들 이외의 값이 파라미터로 들어온다면 예외처리를 하는 로직을 추가해야겠군요! |
||
| } | ||
| } | ||
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.
LottoStore는 구매요청이 들어올 때마다 새로 만들어져야만 하는 객체인지 궁금합니다.도메인적인 요구 사항만 봐선 그렇지 않은 것 같은데요.
LottoGame이 인스턴스 변수로 관리해도 충분하다고 보여지는데 어떻게 생각하시는지요?Uh oh!
There was an error while loading. Please reload this page.
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.
맞습니다. 프로젝트 구현을 다시 되돌아보니 LottoStore는 하나만 존재하고 여러개의 LottoPaper를 발행할 수 있도록 구현했어야했습니다!
바로 수정하겠습니다 😄