|
| 1 | +package lotto.controller; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Set; |
| 5 | +import lotto.model.Game; |
| 6 | +import lotto.model.Lotto; |
| 7 | +import lotto.model.Player; |
| 8 | +import lotto.service.LottoService; |
| 9 | +import lotto.util.MajorErrorMessage; |
| 10 | +import lotto.util.Validator; |
| 11 | +import lotto.util.Validator.dataType; |
| 12 | +import lotto.view.LottoView; |
| 13 | + |
| 14 | +public class LottoController { |
| 15 | + final LottoView lottoView; |
| 16 | + final LottoService lottoService; |
| 17 | + |
| 18 | + |
| 19 | + public void startGame() { |
| 20 | + boolean buyContinue = true; |
| 21 | + boolean setGameContinue = true; |
| 22 | + |
| 23 | + while (buyContinue) { |
| 24 | + buyContinue = buyStep(); |
| 25 | + } |
| 26 | + while (setGameContinue) { |
| 27 | + setGameContinue = setGameStep(); |
| 28 | + } |
| 29 | + |
| 30 | + resultStep(); |
| 31 | + } |
| 32 | + |
| 33 | + public boolean buyStep() { |
| 34 | + try { |
| 35 | + Player player = setLottoMoney(); |
| 36 | + attemptLottoPurchase(player); |
| 37 | + lottoService.setPlayer(player); |
| 38 | + return false; |
| 39 | + } catch (IllegalArgumentException e) { |
| 40 | + System.out.println(e.getMessage()); |
| 41 | + return true; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public boolean setGameStep() { |
| 46 | + try { |
| 47 | + Game gameStep = setJackpot(); |
| 48 | + gameStep = setBonus(gameStep); |
| 49 | + |
| 50 | + lottoService.setGame(gameStep); |
| 51 | + return false; |
| 52 | + } catch (Exception e) { |
| 53 | + System.out.println(MajorErrorMessage.LOTTONUM_WRONG.getMessage() + e.getMessage()); |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public void resultStep() { |
| 59 | + int[] lottoResultTypes = lottoService.calculateLottoResultType(); |
| 60 | + float lottoResultProfitPercent = lottoService.calculateLottoResultProfit(lottoResultTypes); |
| 61 | + lottoView.outputLottoResult(lottoResultTypes, lottoResultProfitPercent); |
| 62 | + } |
| 63 | + |
| 64 | + Player setLottoMoney() { |
| 65 | + try { |
| 66 | + String inputLine = lottoView.inputPurchaseMoney(); |
| 67 | + return Player.setLottoMoney(Validator.isSingleInputType(inputLine, dataType.NUMBER)); |
| 68 | + } catch (Exception e) { |
| 69 | + throw new IllegalArgumentException(MajorErrorMessage.MONEY_WRONG.getMessage() + e.getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + void attemptLottoPurchase(Player player) { |
| 74 | + try { |
| 75 | + player.buyLotto(); |
| 76 | + lottoView.outputPurchasedLottoNum(player.getPurchasedLottoNum()); |
| 77 | + player.getPurchasedLottoList().forEach(lotto -> lottoView.outputPurchasedLottoDetail(lotto.getNumbers())); |
| 78 | + } catch (Exception e) { |
| 79 | + throw new IllegalArgumentException(MajorErrorMessage.LOTTONUM_WRONG.getMessage() + e.getMessage()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + Game setJackpot() { |
| 84 | + String inputLine = lottoView.inputJackpotNumber(); |
| 85 | + List<Integer> jackpotNumbers = Validator.isMultipleInputType(inputLine, dataType.NUMBER, ","); |
| 86 | + Validator.isListItemInRange(Lotto.getLottoRangeStart(), Lotto.getLottoRangeEnd(), jackpotNumbers); |
| 87 | + Set<Integer> jackpotNumberSet = Validator.isListItemDuplicated(jackpotNumbers); |
| 88 | + return Game.setJackpot(jackpotNumberSet); |
| 89 | + } |
| 90 | + |
| 91 | + Game setBonus(Game game) { |
| 92 | + String inputLine = lottoView.inputBonusNumber(); |
| 93 | + int bonusNumber = Validator.isSingleInputType(inputLine, dataType.NUMBER); |
| 94 | + game.setBonus(bonusNumber); |
| 95 | + return game; |
| 96 | + } |
| 97 | + |
| 98 | + public LottoController(LottoView lottoView, LottoService lottoService) { |
| 99 | + this.lottoView = lottoView; |
| 100 | + this.lottoService = lottoService; |
| 101 | + } |
| 102 | +} |
0 commit comments