|
| 1 | +package lotto.service; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Set; |
| 6 | +import java.util.stream.IntStream; |
| 7 | +import lotto.model.Game; |
| 8 | +import lotto.model.Player; |
| 9 | +import lotto.util.GamePrize; |
| 10 | + |
| 11 | +public class LottoService { |
| 12 | + private Game game; |
| 13 | + private Player player; |
| 14 | + private static final int PRIZE_LENGTH = GamePrize.values().length; |
| 15 | + |
| 16 | + public void setGame(Game game) { |
| 17 | + this.game = game; |
| 18 | + } |
| 19 | + |
| 20 | + public void setPlayer(Player player) { |
| 21 | + this.player = player; |
| 22 | + } |
| 23 | + |
| 24 | + public int[] calculateLottoResultType() { |
| 25 | + int[] resultList = new int[PRIZE_LENGTH]; |
| 26 | + player.getPurchasedLottoList().forEach(lotto -> updateResultList(resultList, lotto.getNumbers())); |
| 27 | + return resultList; |
| 28 | + } |
| 29 | + |
| 30 | + public float calculateLottoResultProfit(int[] resultList) { |
| 31 | + int[] prizeMoneyList = Arrays.stream(GamePrize.values()).mapToInt(GamePrize::getPrizeMoney).toArray(); |
| 32 | + int earnedMoney = IntStream.range(0, PRIZE_LENGTH).map(i -> resultList[i] * prizeMoneyList[i]).sum(); |
| 33 | + player.setProfitMoney(earnedMoney); |
| 34 | + return player.getProfitPercent(); |
| 35 | + } |
| 36 | + |
| 37 | + void updateResultList(int[] resultList, List<Integer> singleLotto) { |
| 38 | + if (checkPrizeIndex(singleLotto) > -1) { |
| 39 | + resultList[checkPrizeIndex(singleLotto)]++; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + int checkPrizeIndex(List<Integer> singleLotto) { |
| 44 | + Set<Integer> jackpotNumbers = game.getJackpotNumbers(); |
| 45 | + //일치하는 로또 공 개수 셈 |
| 46 | + int matchingLottoBall = (int) singleLotto.stream().filter(jackpotNumbers::contains).count(); |
| 47 | + if (matchingLottoBall == 6) { |
| 48 | + if (hasBonusMatch(singleLotto)) { |
| 49 | + return GamePrize.PRIZE_2ND.getIndex(); |
| 50 | + } |
| 51 | + return GamePrize.PRIZE_1ST.getIndex(); |
| 52 | + } |
| 53 | + return matchingLottoBall - 3; |
| 54 | + } |
| 55 | + |
| 56 | + boolean hasBonusMatch(List<Integer> singleLotto) { |
| 57 | + int bonus = game.getBonus(); |
| 58 | + return singleLotto.contains(bonus); |
| 59 | + } |
| 60 | +} |
0 commit comments