-
Notifications
You must be signed in to change notification settings - Fork 177
[OptimistLabyrinth] ๐ 5๋จ๊ณ - ๋ก๋(์๋) #949
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: optimistlabyrinth
Are you sure you want to change the base?
Changes from all commits
0d907ff
1d9a83f
fc268bf
caa6e53
39b31b5
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 |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| import lotto2.model.*; | ||
| import lotto2.model.enums.LottoPrize; | ||
| import lotto2.model.enums.WinningRank; | ||
| import lotto2.model.generator.LottoGeneratorFromWinningNumbers; | ||
| import lotto2.model.generator.LottoGeneratorFromUserInput; | ||
| import lotto2.model.generator.LottoGeneratorRandom; | ||
| import lotto2.view.InputView; | ||
| import lotto2.view.OutputView; | ||
|
|
@@ -20,59 +20,73 @@ public LottoController(InputView inputView, OutputView outputView) { | |
| } | ||
|
|
||
| public void run() { | ||
| final MoneyToBuy money = acceptInputMoney(); | ||
| final List<Lotto> lottoBucket = generateManyLotto(money); | ||
| final Lotto winningNumbers = acceptWinningNumbers(); | ||
| final LottoNumber bonusNumber = acceptBonusNumber(); | ||
| final Map<WinningRank, Integer> result = calculateLotto(lottoBucket, winningNumbers, bonusNumber); | ||
| final MoneyToBuy moneyToBuy = acceptInputMoney(); | ||
| final ManualLottoCount manualLottoCount = acceptManualCount(moneyToBuy); | ||
| final List<Lotto> lottoBucket = acceptManyManualLotto(manualLottoCount); | ||
| final int automaticLottoCount = getAutomaticLottoCount(moneyToBuy, manualLottoCount); | ||
| generateManyAutomaticLotto(automaticLottoCount, lottoBucket); | ||
| displayAllLotto(manualLottoCount, automaticLottoCount, lottoBucket); | ||
| final WinningLotto winningLotto = acceptWinningNumbers(); | ||
| acceptBonusNumber(winningLotto); | ||
| final Map<WinningRank, Integer> result = calculateLotto(lottoBucket, winningLotto); | ||
| final List<WinningRankCountDto> winningRankCounts = winningRankCountsAsArray(result); | ||
| displayStatistics(winningRankCounts); | ||
| final double profitRatio = calculateProfitRatio(money, winningRankCounts); | ||
| final double profitRatio = calculateProfitRatio(moneyToBuy, winningRankCounts); | ||
| displayProfitRatio(profitRatio); | ||
| } | ||
|
|
||
| private MoneyToBuy acceptInputMoney() { | ||
| final MoneyToBuy money = inputView.inputMoney(); | ||
| outputView.printNumberOfBoughtLotto(money); | ||
| return money; | ||
| return inputView.inputMoney(); | ||
| } | ||
|
|
||
| private List<Lotto> generateManyLotto(MoneyToBuy money) { | ||
| final List<Lotto> lottoBucket = new ArrayList<>(); | ||
| final Round round = new Round(money.getCount()); | ||
| private ManualLottoCount acceptManualCount(MoneyToBuy moneyToBuy) { | ||
| return inputView.inputManualCount(moneyToBuy); | ||
| } | ||
|
|
||
| private List<Lotto> acceptManyManualLotto(ManualLottoCount lottoCount) { | ||
|
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 inputView.inputManyManualLotto(lottoCount); | ||
| } | ||
|
|
||
| private int getAutomaticLottoCount(MoneyToBuy moneyToBuy, ManualLottoCount manualLottoCount) { | ||
|
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 moneyToBuy.count() - manualLottoCount.count(); | ||
| } | ||
|
|
||
| private List<Lotto> generateManyAutomaticLotto(int count, List<Lotto> lottoBucket) { | ||
| final Round round = new Round(count); | ||
| while (round.hasNext()) { | ||
| round.goNext(); | ||
| lottoBucket.add(generateEachLotto()); | ||
| } | ||
| outputView.printListOfLotto(lottoBucket); | ||
| return lottoBucket; | ||
| } | ||
|
|
||
| private Lotto generateEachLotto() { | ||
| return LottoGeneratorRandom.generate(); | ||
| } | ||
|
|
||
| private Lotto acceptWinningNumbers() { | ||
| private void displayAllLotto(ManualLottoCount manualLottoCount, int automaticLottoCount, List<Lotto> lottoBucket) { | ||
| outputView.printListOfLotto(manualLottoCount, automaticLottoCount, lottoBucket); | ||
| } | ||
|
|
||
| private WinningLotto acceptWinningNumbers() { | ||
| final String input = inputView.inputWinningNumbers(); | ||
| final LottoGeneratorFromWinningNumbers lottoGenerator = new LottoGeneratorFromWinningNumbers(input); | ||
| return lottoGenerator.generate(); | ||
| final LottoGeneratorFromUserInput lottoGenerator = new LottoGeneratorFromUserInput(input); | ||
| return new WinningLotto(lottoGenerator.generate()); | ||
| } | ||
|
|
||
| private LottoNumber acceptBonusNumber() { | ||
| return inputView.inputBonusNumber(); | ||
| private WinningLotto acceptBonusNumber(WinningLotto winningLotto) { | ||
| final LottoNumber bonusNumber = inputView.inputBonusNumber(); | ||
| winningLotto.setBonusNumber(bonusNumber); | ||
| return winningLotto; | ||
| } | ||
|
|
||
| private Map<WinningRank, Integer> calculateLotto( | ||
| List<Lotto> lottoBucket, | ||
| Lotto winningNumbers, | ||
| LottoNumber bonusNumber) { | ||
| private Map<WinningRank, Integer> calculateLotto(List<Lotto> lottoBucket, WinningLotto winningLotto) { | ||
| final LottoCalculationUtils lottoCalculationUtils = new LottoCalculationUtils(); | ||
| Map<WinningRank, Integer> countForEachWinningRank = lottoCalculationUtils.initializeCountMap(); | ||
| for (Lotto eachLotto : lottoBucket) { | ||
| final int matchCount = lottoCalculationUtils.getMatchCount(eachLotto, winningNumbers); | ||
| final WinningRank winningRank = lottoCalculationUtils.winningRankForMatchCount( | ||
| matchCount, | ||
| eachLotto.contains(bonusNumber)); | ||
| final int matchCount = eachLotto.getMatchCount(winningLotto.getLotto()); | ||
| final WinningRank winningRank = lottoCalculationUtils.winningRankForMatchCount(matchCount, | ||
| eachLotto.contains(winningLotto.getBonusNumber())); | ||
| countForEachWinningRank = lottoCalculationUtils.setCountForEachWinningRank( | ||
| countForEachWinningRank, winningRank); | ||
| } | ||
|
|
@@ -93,12 +107,12 @@ private void displayStatistics(List<WinningRankCountDto> winningRankCounts) { | |
| outputView.printStatistics(winningRankCounts); | ||
| } | ||
|
|
||
| private double calculateProfitRatio(MoneyToBuy money, List<WinningRankCountDto> winningRankCounts) { | ||
| private double calculateProfitRatio(MoneyToBuy moneyToBuy, List<WinningRankCountDto> winningRankCounts) { | ||
| int totalPrize = 0; | ||
| for (final WinningRankCountDto winningRankCount : winningRankCounts) { | ||
| totalPrize += winningRankCount.getLottoPrize().getPrize() * winningRankCount.getCount(); | ||
| } | ||
| return (double) totalPrize / money.getValue(); | ||
| return (double) totalPrize / moneyToBuy.value(); | ||
| } | ||
|
|
||
| private void displayProfitRatio(double profitRatio) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,24 @@ | ||
| package lotto2.model; | ||
|
|
||
| import java.util.List; | ||
| import lotto2.model.constant.LottoConstant; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class Lotto { | ||
| private final List<LottoNumber> lottoNumbers; | ||
|
|
||
| public Lotto(List<LottoNumber> lottoNumbers) { | ||
| checkDuplicateNumberInList(lottoNumbers); | ||
| this.lottoNumbers = lottoNumbers; | ||
| } | ||
|
|
||
| private void checkDuplicateNumberInList(List<LottoNumber> winningLottoNumbers) throws IllegalArgumentException { | ||
|
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.
|
||
| final Set<LottoNumber> duplicateCheck = new HashSet<>(winningLottoNumbers); | ||
| if (duplicateCheck.size() != LottoConstant.COUNT_OF_NUMBER_IN_LOTTO) { | ||
| throw new IllegalArgumentException("์ต์ข ์ ์ผ๋ก ๋น์ฒจ ๋ฒํธ๋ ๋ฐ๋์ 6 ๊ฐ๊ฐ ๋์ด์ผ ํฉ๋๋ค."); | ||
| } | ||
| } | ||
|
|
||
| public boolean contains(LottoNumber lottoNumber) { | ||
| return lottoNumbers.contains(lottoNumber); | ||
| } | ||
|
|
@@ -17,8 +27,21 @@ public List<LottoNumber> lottoNumbers() { | |
| return lottoNumbers; | ||
| } | ||
|
|
||
| public int getMatchCount(Lotto other) { | ||
|
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. ๋ก๋๊ฐ ์ค์ค๋ก ๋ค๋ฅธ ๋ก๋์ ์ผ์นํ๋ ๋ฒํธ์ ๊ฐ์๋ฅผ ์นด์ดํธํ ์ ์์ด์, ๋ฅ๋์ ์ธ ๊ฐ์ฒด ์ญํ ์ ํ ์ ์๊ฒ ๋ ๊ฒ ๊ฐ๋ค์! |
||
| final List<LottoNumber> otherNumbers = other.lottoNumbers(); | ||
| int matchCount = 0; | ||
| for (LottoNumber lottoNumber : otherNumbers) { | ||
| if (lottoNumbers.contains(lottoNumber)) { | ||
| ++matchCount; | ||
| } | ||
| } | ||
| return matchCount; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return lottoNumbers.toString(); | ||
| final List<LottoNumber> listToPrint = new ArrayList<>(lottoNumbers); | ||
|
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. ๋ก๋์ ์
์ถ๋ ฅ์ ์ฝ์์์ ์น์ผ๋ก ๋ณ๊ฒฝํ๋ค๊ณ ์์ํด๋ณด๋ฉด ์ด๋จ๊น์? ์ ์๊ฐ์๋ |
||
| Collections.sort(listToPrint, Comparator.comparingInt(LottoNumber::value)); | ||
| return listToPrint.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package lotto2.model; | ||
|
|
||
| public class ManualLottoCount { | ||
| private final int count; | ||
|
|
||
| public ManualLottoCount(String input, MoneyToBuy moneyToBuy) { | ||
| this.count = parseInt(input, moneyToBuy); | ||
| } | ||
|
|
||
| private int parseInt(String input, MoneyToBuy moneyToBuy) { | ||
| int count; | ||
| try { | ||
| count = Integer.parseInt(input); | ||
| } catch (Exception e) { | ||
| throw new NumberFormatException("์๋์ผ๋ก ๊ตฌ๋งคํ ๋ก๋๋ ์ซ์๋ฅผ ์ ๋ ฅํด์ผ ํฉ๋๋ค."); | ||
| } | ||
| if (isNegative(count)) { | ||
| throw new IllegalArgumentException("์๋์ผ๋ก ๊ตฌ๋งคํ ๋ก๋๋ ๋ฐ๋์ 0 ์ด์์ ์ ์์ฌ์ผ ํฉ๋๋ค."); | ||
| } | ||
| if (isMoreThanTotalCount(moneyToBuy, count)) { | ||
| throw new IllegalArgumentException("์๋์ผ๋ก ๊ตฌ๋งคํ ๋ก๋๋ ์ ๋ ฅํ ๊ธ์ก์ผ๋ก ๊ตฌ์ ๊ฐ๋ฅํ ์ด ๊ฐ์๋ณด๋ค ๋ง์ ์ ์์ต๋๋ค."); | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| private boolean isNegative(int count) { | ||
| return count < 0; | ||
| } | ||
|
|
||
| private boolean isMoreThanTotalCount(MoneyToBuy moneyToBuy, int count) { | ||
| return moneyToBuy.count() < count; | ||
| } | ||
|
|
||
| public int count() { | ||
| return count; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package lotto2.model; | ||
|
|
||
| public class WinningLotto { | ||
| private final Lotto lotto; | ||
| private LottoNumber bonusNumber; | ||
|
|
||
| public WinningLotto(Lotto lotto) { | ||
| this.lotto = lotto; | ||
| } | ||
|
|
||
| public Lotto getLotto() { | ||
| return lotto; | ||
| } | ||
|
|
||
| public LottoNumber getBonusNumber() { | ||
| return bonusNumber; | ||
| } | ||
|
|
||
| public void setBonusNumber(LottoNumber bonusNumber) { | ||
|
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.
๋ณด๋์ค ๋ฒํธ๊ฐ ๋น์ฒจ๋ฒํธ์ ์ค๋ณต๋ ์ ์๋ค๋ ๋ฐธ๋ฆฌ๋ฐ์ด์ ๋ ์ถ๊ฐํด๋ณด์๋ฉด ์ข๊ฒ ์ต๋๋ค. :) |
||
| this.bonusNumber = bonusNumber; | ||
| } | ||
| } | ||
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.
์ด์ PR์ ์ฝ๋ฉํธ ๋จ๊ฒจ์ฃผ์ ๋ถ๋ถ์ ๋ํด ๋ต๋ณ ๋๋ฆฝ๋๋ค. :)
ํ์ฌ
calculateLotto์์ ๋ฑ์ ๊ณ์ฐ์ ์ ์ฅํ๊ธฐ ์ํดWinningRank๋ฅผ ์ฌ์ฉํ๊ณ ์๋๋ฐ,WinningRank๋์LottoPrize๋ฅผ ํค๋ก ์ฌ์ฉํด๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์์.WinningRank๋ฅผ ์ฌ์ฉํ๊ณ ์๋ ๋ชจ๋ ๋ถ๋ถ์LottoPrize๊ฐ ๋ค์ด๊ฐ๋ ์ฝ๋๊ฐ ์ ๋์ํ๋๋ก ๊ตฌํ์ด ๊ฐ๋ฅํ ๊ฒ ๊ฐ์ต๋๋ค. :)WinningRank์๋ ๋ฑ์์ ๋งค์น๋๋ ๊ฐ์๋ฅผ ๊ด๋ฆฌํ๊ณ ์๋๋ฐ์. 1.calculateLotto์์ ์ด ๊ฐ์ ์ฌ์ฉํ์ง ์๊ณ ์์ต๋๋ค. 2. ์ด ๊ฐ์ ์ฌ์ฉํ๋๋ผ๋,LottoPrize์์๋ ๋์ผํ๊ฒ ๋ฑ์์ ๋งค์น๋๋ ๊ฐ์๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฏ๋ก,LottoPrize์ ์๋ ๊ฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์,WinningRank๋์LottoPrize์ ๊ฐ์ ์ฌ์ฉํ๊ณ ,WinningRankํด๋์ค๋ ์ญ์ ํด๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค.์ฝ๋์ ๋ํ ์ฝ๋ฉํธ์ ๋ณ๊ฐ๋ก enum ์ ํ์ฉ๋ฒ์ ๋์ค์ ์๊ฐ์ด ๋์ค ๋ ํ์ตํด๋ณด์๋ฉด ๋์์ด ๋ ๊ฒ ๊ฐ์ต๋๋ค.
enum์ ์ฃผ๋ก ์์๋ฅผ ์ ์ํ ๋ ์ฌ์ฉํ๊ธฐ๋ ํ์ง๋ง, enum์ ์ ์๋ ๊ฐ์ ๋ชจ๋ ํ๋์ ๊ฐ์ฒด์ด๋ฏ๋ก, ์ด ์ ์ ์ ์ฌ์ฉํ๋ฉด ์กฐ๊ธ ๋ ๋ณ๊ฒฝ์ ์ ์ฐํ๊ฒ ๋์ํ ์ ์๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. enum ์ผ๋ก ์ฑ๊ธํด ํจํด ๊ตฌํํ๊ธฐ, enum ์ผ๋ก ์ ๋ต ํจํด ๊ตฌํํ๊ธฐ ๋ฑ์ ํค์๋๋ก ํ์ตํด๋ณด์๋ฉด ๋์์ด ๋ ๊ฒ ๊ฐ์ต๋๋ค. :)