diff --git a/src/main/java/lottoGame/controller/LottoStore.java b/src/main/java/lottoGame/controller/LottoStore.java deleted file mode 100644 index b91c9276fe..0000000000 --- a/src/main/java/lottoGame/controller/LottoStore.java +++ /dev/null @@ -1,61 +0,0 @@ -package lottoGame.controller; - -import static lottoGame.view.Casher.askBeforeWinNums; -import static lottoGame.view.Casher.askBuyPrice; -import static lottoGame.view.Casher.informBuyCount; -import static lottoGame.view.Casher.informPublishedLottos; -import static lottoGame.view.Casher.informWinningLottoNums; - -import lottoGame.model.lotto.LottoMachine; -import lottoGame.model.lotto.Lottos; -import lottoGame.model.price.BuyPrice; -import lottoGame.model.winner.BeforeWinNums; -import lottoGame.model.winner.WinnerResult; -import lottoGame.view.WinResultDto; - -public class LottoStore { - public static final int PER_LOTTO_PRICE = 1000; - - public void start() { - BuyPrice buyPrice = getBuyPrice(); - - Lottos lottos = buyLottos(buyPrice); - - totalWinResult( - new BeforeWinNums(askBeforeWinNums()), - lottos, - buyPrice.price() - ); - } - - private Lottos buyLottos(BuyPrice buyPrice) { - Lottos lottos = publishLottos(buyPrice); - informPublishedLottos(lottos.convertRawString()); - return lottos; - } - - private BuyPrice getBuyPrice() { - BuyPrice buyPrice = new BuyPrice(askBuyPrice()); - informBuyCount(buyPrice.calculateLottoCount(PER_LOTTO_PRICE)); - - return buyPrice; - } - - private Lottos publishLottos(BuyPrice buyPrice) { - LottoMachine lottoMachine = new LottoMachine(PER_LOTTO_PRICE); - - return lottoMachine.publish(buyPrice); - } - - private void totalWinResult(BeforeWinNums beforeWinNums, Lottos lottos, int price) { - WinnerResult winnerResult = lottos.compareAndElectWinResult(beforeWinNums); - - informWinningLottoNums( - new WinResultDto( - winnerResult, - price - ) - ); - } - -} diff --git a/src/main/java/lottoGame/model/lotto/Lotto.java b/src/main/java/lottoGame/model/lotto/Lotto.java deleted file mode 100644 index 37357d8163..0000000000 --- a/src/main/java/lottoGame/model/lotto/Lotto.java +++ /dev/null @@ -1,46 +0,0 @@ -package lottoGame.model.lotto; - -import static java.lang.String.join; - -import java.util.Collections; -import java.util.List; -import lottoGame.model.winner.BeforeWinNums; -import lottoGame.model.winner.WinStandard; - -public class Lotto { - - public static final int LOTTO_NUM_COUNT = 6; - - private final List lottoNums; - - public Lotto(List lottoNums) { - if (lottoNums.size() != LOTTO_NUM_COUNT) { - throw new IllegalArgumentException("로또 갯수가 유효하지 않습니다"); - } - - Collections.sort(lottoNums); - - this.lottoNums = lottoNums; - } - - public WinStandard checkIfWin(BeforeWinNums beforeWinNums) { - long result = this.lottoNums.stream() - .mapToInt(LottoNum::value) - .filter(beforeWinNums::isContain) - .count(); - - return WinStandard.findByValue( - Long.valueOf(result).intValue() - ); - } - - public String toString() { - return "[" + join(", ", convertString()) + "]"; - } - - private List convertString() { - return this.lottoNums.stream() - .map(LottoNum::toString) - .toList(); - } -} diff --git a/src/main/java/lottoGame/model/lotto/LottoMachine.java b/src/main/java/lottoGame/model/lotto/LottoMachine.java deleted file mode 100644 index 50dda4669a..0000000000 --- a/src/main/java/lottoGame/model/lotto/LottoMachine.java +++ /dev/null @@ -1,29 +0,0 @@ -package lottoGame.model.lotto; - -import static java.util.stream.IntStream.range; -import static lottoGame.model.lotto.LottoNumFactory.createLotto; - -import java.util.List; -import lottoGame.model.price.BuyPrice; - -public class LottoMachine { - - public static final int LOTTO_NUM_COUNT = 6; - - private final int perLottoPrice; - - public LottoMachine(int perLottoPrice) { - this.perLottoPrice = perLottoPrice; - } - - public Lottos publish(BuyPrice buyPrice) { - List lottos = range(0, buyPrice.calculateLottoCount(perLottoPrice)) - .mapToObj(idx -> - new Lotto( - createLotto(LOTTO_NUM_COUNT) - ) - ).toList(); - - return new Lottos(lottos); - } -} diff --git a/src/main/java/lottoGame/model/lotto/LottoNumFactory.java b/src/main/java/lottoGame/model/lotto/LottoNumFactory.java deleted file mode 100644 index 4414cd9db6..0000000000 --- a/src/main/java/lottoGame/model/lotto/LottoNumFactory.java +++ /dev/null @@ -1,34 +0,0 @@ -package lottoGame.model.lotto; - -import static lottoGame.model.lotto.LottoNum.MAX_NUM; -import static lottoGame.model.lotto.LottoNum.MIN_NUM; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Random; -import java.util.Set; - -public class LottoNumFactory { - - public static LottoNum createLottoNum() { - return new LottoNum( - new Random().nextInt(MIN_NUM, MAX_NUM) - ); - } - - public static List createLotto(int numCount) { - if (numCount <= 0) { - throw new IllegalArgumentException("생성할 로또의 로또번호 갯수를 입력해 주세요."); - } - - Set lottoNums = new HashSet<>(); - while (lottoNums.size() < numCount) { - lottoNums.add(createLottoNum()); - } - - return new ArrayList<>(lottoNums); - } - - -} diff --git a/src/main/java/lottoGame/model/winner/BeforeWinNums.java b/src/main/java/lottoGame/model/winner/BeforeWinNums.java deleted file mode 100644 index 714cd60b88..0000000000 --- a/src/main/java/lottoGame/model/winner/BeforeWinNums.java +++ /dev/null @@ -1,16 +0,0 @@ -package lottoGame.model.winner; - -import java.util.Collections; -import java.util.List; - -public class BeforeWinNums { - private final List winNums; - - public BeforeWinNums(List winNums) { - this.winNums = winNums; - } - - public boolean isContain(int num) { - return winNums.contains(num); - } -} diff --git a/src/main/java/lottoGame/model/winner/WinStandard.java b/src/main/java/lottoGame/model/winner/WinStandard.java deleted file mode 100644 index b131a2ca22..0000000000 --- a/src/main/java/lottoGame/model/winner/WinStandard.java +++ /dev/null @@ -1,41 +0,0 @@ -package lottoGame.model.winner; - -import java.util.Arrays; - -public enum WinStandard { - FIRST("6개 일치 (2000000000원)-", 6, 2000000000), - SECOND("5개 일치 (1500000원)-", 5, 1500000), - THIRD("4개 일치 (50000원)-", 4, 50000), - FOURTH("3개 일치 (5000원)-", 3, 5000), - - NOTHING("x", -1, 0), - ; - - private final String desc; - private final int value; - private final int returnOfWin; - - WinStandard(String desc, int value, int returnOfWin) { - this.desc = desc; - this.value = value; - this.returnOfWin = returnOfWin; - } - - public static WinStandard findByValue(int value) { - return Arrays.stream(WinStandard.values()) - .filter(winStandard -> winStandard.value == value) - .findFirst().orElse(NOTHING); - } - - public boolean isNothing() { - return this == NOTHING; - } - - public String desc() { - return this.desc; - } - - public int returnOfWin() { - return this.returnOfWin; - } -} diff --git a/src/main/java/lottoGame/model/winner/WinnerResult.java b/src/main/java/lottoGame/model/winner/WinnerResult.java deleted file mode 100644 index 644250a038..0000000000 --- a/src/main/java/lottoGame/model/winner/WinnerResult.java +++ /dev/null @@ -1,49 +0,0 @@ -package lottoGame.model.winner; - -import static java.lang.Math.floor; -import static java.util.Objects.isNull; - -import java.util.ArrayList; -import java.util.List; - -public class WinnerResult { - - private final List winResults; - - public WinnerResult() { - this(new ArrayList<>()); - } - - public WinnerResult(List winResults) { - this.winResults = winResults; - } - - public void addWinResult(WinStandard winStandard) { - if (isNull(winStandard)) { - return; - } - - if (winStandard.isNothing()) { - return; - } - - winResults.add(winStandard); - } - - public int findWinCount(WinStandard winStandard) { - long count = this.winResults.stream() - .filter(winResult -> winResult.equals(winStandard)) - .count(); - - return Long.valueOf(count).intValue(); - } - - public double calculateRateOfReturn(int buyPrice) { - int totalWinReturn = this.winResults.stream() - .mapToInt(WinStandard::returnOfWin) - .sum(); - - double rawRate = (double) totalWinReturn / buyPrice * 100; - return floor(rawRate) / 100.0; - } -} diff --git a/src/main/java/lottoGame/view/Casher.java b/src/main/java/lottoGame/view/Casher.java deleted file mode 100644 index d2308848ec..0000000000 --- a/src/main/java/lottoGame/view/Casher.java +++ /dev/null @@ -1,35 +0,0 @@ -package lottoGame.view; - -import java.util.Arrays; -import java.util.List; -import lottoGame.model.winner.WinnerResult; - -public class Casher { - public static int askBuyPrice() { - OutputView.printQuestionByBuyPrice(); - return InputView.inputInt(); - } - - public static void informBuyCount(int buyCount) { - OutputView.printBuyLottoCount(buyCount); - } - - public static List askBeforeWinNums() { - OutputView.printQuestionBeforeWinNums(); - String rawValue = InputView.inputString(); - - return Arrays.stream(rawValue.split(", ")) - .map(Integer::parseInt) - .toList(); - } - - public static void informPublishedLottos(List perLottoNums) { - for (String perLottoNum : perLottoNums) { - OutputView.printPerLottoNums(perLottoNum); - } - } - - public static void informWinningLottoNums(WinResultDto winResultDto) { - OutputView.printWinResultMsg(winResultDto); - } -} diff --git a/src/main/java/lottoGame/view/WinResultDto.java b/src/main/java/lottoGame/view/WinResultDto.java deleted file mode 100644 index 39ba2bcf82..0000000000 --- a/src/main/java/lottoGame/view/WinResultDto.java +++ /dev/null @@ -1,50 +0,0 @@ -package lottoGame.view; - -import static lottoGame.model.winner.WinStandard.FIRST; -import static lottoGame.model.winner.WinStandard.FOURTH; -import static lottoGame.model.winner.WinStandard.SECOND; -import static lottoGame.model.winner.WinStandard.THIRD; - -import lottoGame.model.winner.WinStandard; -import lottoGame.model.winner.WinnerResult; - -public class WinResultDto { - - private final int fourthCount; - private final int thirdCount; - private final int secondCount; - private final int firstCount; - private final String rateOfReturn; - - public WinResultDto(WinnerResult winnerResult, int buyPrice) { - this( - winnerResult.findWinCount(FOURTH), - winnerResult.findWinCount(THIRD), - winnerResult.findWinCount(SECOND), - winnerResult.findWinCount(FIRST), - String.valueOf(winnerResult.calculateRateOfReturn(buyPrice)) - ); - } - - public WinResultDto( - int fourthCount, - int thirdCount, - int secondCount, - int firstCount, - String rateOfReturn - ) { - this.fourthCount = fourthCount; - this.thirdCount = thirdCount; - this.secondCount = secondCount; - this.firstCount = firstCount; - this.rateOfReturn = rateOfReturn; - } - - public String toString() { - return FOURTH.desc() + this.fourthCount + "개\n" - + THIRD.desc() + this.thirdCount + "개\n" - + SECOND.desc() + this.secondCount + "개\n" - + FIRST.desc() + this.firstCount + "개\n" - + "총 수익률은 " + this.rateOfReturn + "입니다."; - } -} diff --git a/src/main/java/lottoGame/Application.java b/src/main/java/lottogame/Application.java similarity index 66% rename from src/main/java/lottoGame/Application.java rename to src/main/java/lottogame/Application.java index c55658bec1..949141bdeb 100644 --- a/src/main/java/lottoGame/Application.java +++ b/src/main/java/lottogame/Application.java @@ -1,6 +1,6 @@ -package lottoGame; +package lottogame; -import lottoGame.controller.LottoStore; +import lottogame.controller.LottoStore; public class Application { diff --git a/src/main/java/lottogame/controller/LottoStore.java b/src/main/java/lottogame/controller/LottoStore.java new file mode 100644 index 0000000000..81f41a168d --- /dev/null +++ b/src/main/java/lottogame/controller/LottoStore.java @@ -0,0 +1,53 @@ +package lottogame.controller; + +import static lottogame.model.lotto.LottoMachine.PER_LOTTO_PRICE; +import static lottogame.view.Casher.askBeforeWinNums; +import static lottogame.view.Casher.askBonusNum; +import static lottogame.view.Casher.askBuyPrice; +import static lottogame.view.Casher.informBuyCount; +import static lottogame.view.Casher.informPublishedLottos; +import static lottogame.view.Casher.informWinResult; + +import java.util.Set; +import lottogame.model.lotto.Lotto; +import lottogame.model.lotto.LottoMachine; +import lottogame.model.lotto.LottoNum; +import lottogame.model.lotto.Lottos; +import lottogame.model.lotto.WinningLottoNums; +import lottogame.model.price.LottoPurchasePrice; +import lottogame.model.winner.WinnerResult; + +public class LottoStore { + + public void start() { + LottoPurchasePrice lottoPurchasePrice = getLottoPurchasePrice(); + LottoMachine lottoMachine = new LottoMachine(); + + Lottos lottos = buyLottos(lottoMachine, lottoPurchasePrice); + + WinnerResult winnerResult = lottos.compareAndElectWinResult( + getWinningLottoNums(lottoMachine)); + + informWinResult(winnerResult, lottoPurchasePrice); + } + + private LottoPurchasePrice getLottoPurchasePrice() { + LottoPurchasePrice lottoPurchasePrice = new LottoPurchasePrice(askBuyPrice()); + informBuyCount(lottoPurchasePrice.calculateLottoCount(PER_LOTTO_PRICE)); + + return lottoPurchasePrice; + } + + private Lottos buyLottos(LottoMachine lottoMachine, LottoPurchasePrice lottoPurchasePrice) { + Lottos lottos = lottoMachine.publish(lottoPurchasePrice); + informPublishedLottos(lottos.convertRawString()); + return lottos; + } + + private WinningLottoNums getWinningLottoNums(LottoMachine lottoMachine) { + Set lottoByNums = lottoMachine.createLottoByNums(askBeforeWinNums()); + LottoNum bonusNum = lottoMachine.getLottoNum(askBonusNum()); + + return new WinningLottoNums(lottoByNums, bonusNum); + } +} diff --git a/src/main/java/lottogame/model/lotto/Lotto.java b/src/main/java/lottogame/model/lotto/Lotto.java new file mode 100644 index 0000000000..4f23562be2 --- /dev/null +++ b/src/main/java/lottogame/model/lotto/Lotto.java @@ -0,0 +1,46 @@ +package lottogame.model.lotto; + +import static java.lang.String.join; +import static lottogame.model.lotto.LottoMachine.LOTTO_NUM_COUNT; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class Lotto { + + private final Set lottoNums; + + public Lotto(Set lottoNums) { + if (lottoNums.isEmpty()) { + throw new IllegalArgumentException("로또 갯수가 유효하지 않습니다"); + } + + if (lottoNums.size() != LOTTO_NUM_COUNT) { + throw new IllegalArgumentException("발행된 로또번호중 중복이 존재합니다"); + } + + this.lottoNums = lottoNums; + } + + public int findMatchCountBy(Lotto otherLotto) { + return (int) this.lottoNums.stream() + .filter(otherLotto::isContain) + .count(); + } + + public boolean isContain(LottoNum num) { + return this.lottoNums.contains(num); + } + + public String toString() { + return "[" + join(", ", convertString()) + "]"; + } + + private List convertString() { + return new ArrayList<>(this.lottoNums).stream() + .sorted() + .map(LottoNum::toString) + .toList(); + } +} diff --git a/src/main/java/lottogame/model/lotto/LottoMachine.java b/src/main/java/lottogame/model/lotto/LottoMachine.java new file mode 100644 index 0000000000..7e84f159c8 --- /dev/null +++ b/src/main/java/lottogame/model/lotto/LottoMachine.java @@ -0,0 +1,72 @@ +package lottogame.model.lotto; + +import static java.util.stream.IntStream.range; +import static lottogame.model.lotto.LottoNum.MAX_NUM; +import static lottogame.model.lotto.LottoNum.MIN_NUM; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.stream.Collectors; +import lottogame.model.price.LottoPurchasePrice; + +public class LottoMachine { + + public static final int MIN_NUM = 1; + public static final int MAX_NUM = 45; + public static final int LOTTO_NUM_COUNT = 6; + public static final int PER_LOTTO_PRICE = 1_000; + + private static final Random RANDOM = new Random(); + + private static final Map LOTTO_NUM_POOL = new HashMap<>(); + + static { + range(MIN_NUM, MAX_NUM + 1).forEach( + num -> LOTTO_NUM_POOL.put(num, new LottoNum(num)) + ); + } + + public Lottos publish(LottoPurchasePrice lottoPurchasePrice) { + List lottos = range(0, lottoPurchasePrice.calculateLottoCount(PER_LOTTO_PRICE)) + .mapToObj(idx -> + new Lotto( + createLottoByCount(LOTTO_NUM_COUNT) + ) + ).toList(); + + return new Lottos(lottos); + } + + public LottoNum getLottoNum(int num) { + return LOTTO_NUM_POOL.get(num); + } + + private Set createLottoByCount(int numCount) { + if (numCount <= 0) { + throw new IllegalArgumentException("생성할 로또의 로또번호 갯수를 입력해 주세요."); + } + + Set lottoNums = new HashSet<>(); + while (lottoNums.size() < numCount) { + LottoNum lottoNum = getLottoNum(RANDOM.nextInt(MIN_NUM, MAX_NUM)); + lottoNums.add(lottoNum); + } + + return lottoNums; + } + + public Set createLottoByNums(Set rawNums) { + if (rawNums.isEmpty() || rawNums.size() != LOTTO_NUM_COUNT) { + throw new IllegalArgumentException("생성할 로또의 로또번호 갯수를 정확히 입력해 주세요."); + } + + return rawNums.stream() + .map(LOTTO_NUM_POOL::get) + .collect(Collectors.toSet()); + } +} diff --git a/src/main/java/lottoGame/model/lotto/LottoNum.java b/src/main/java/lottogame/model/lotto/LottoNum.java similarity index 92% rename from src/main/java/lottoGame/model/lotto/LottoNum.java rename to src/main/java/lottogame/model/lotto/LottoNum.java index 2095d575b3..601ea9ddb9 100644 --- a/src/main/java/lottoGame/model/lotto/LottoNum.java +++ b/src/main/java/lottogame/model/lotto/LottoNum.java @@ -1,4 +1,4 @@ -package lottoGame.model.lotto; +package lottogame.model.lotto; import java.util.Objects; @@ -20,10 +20,6 @@ private boolean isOutOfLottoRange(int num) { return num < MIN_NUM || num > MAX_NUM; } - public int value() { - return num; - } - @Override public int compareTo(LottoNum o) { return Integer.compare(this.num, o.num); diff --git a/src/main/java/lottoGame/model/lotto/Lottos.java b/src/main/java/lottogame/model/lotto/Lottos.java similarity index 60% rename from src/main/java/lottoGame/model/lotto/Lottos.java rename to src/main/java/lottogame/model/lotto/Lottos.java index 4b01e65e84..e563eaee8c 100644 --- a/src/main/java/lottoGame/model/lotto/Lottos.java +++ b/src/main/java/lottogame/model/lotto/Lottos.java @@ -1,8 +1,8 @@ -package lottoGame.model.lotto; +package lottogame.model.lotto; import java.util.List; -import lottoGame.model.winner.BeforeWinNums; -import lottoGame.model.winner.WinnerResult; +import lottogame.model.winner.WinStandard; +import lottogame.model.winner.WinnerResult; public class Lottos { @@ -12,10 +12,11 @@ public Lottos(List lottos) { this.lottos = lottos; } - public WinnerResult compareAndElectWinResult(BeforeWinNums beforeWinNums) { + public WinnerResult compareAndElectWinResult(final WinningLottoNums winningLottoNums) { WinnerResult winnerResult = new WinnerResult(); for (Lotto lotto : lottos) { - winnerResult.addWinResult(lotto.checkIfWin(beforeWinNums)); + WinStandard winStandard = winningLottoNums.checkIfWin(lotto); + winnerResult.addWinResult(winStandard); } return winnerResult; diff --git a/src/main/java/lottogame/model/lotto/WinningLottoNums.java b/src/main/java/lottogame/model/lotto/WinningLottoNums.java new file mode 100644 index 0000000000..146e063e8a --- /dev/null +++ b/src/main/java/lottogame/model/lotto/WinningLottoNums.java @@ -0,0 +1,29 @@ +package lottogame.model.lotto; + +import java.util.Set; +import lottogame.model.winner.WinStandard; + +public class WinningLottoNums { + private final Lotto winLotto; + private final LottoNum bonusNums; + + public WinningLottoNums(Set winLottoNums, LottoNum bonusNums) { + this(new Lotto(winLottoNums), bonusNums); + } + + public WinningLottoNums(Lotto winLotto, LottoNum bonusNums) { + if (winLotto.isContain(bonusNums)) { + throw new IllegalArgumentException("당첨번호와 보너스볼은 중첩될수 없습니다"); + } + + this.winLotto = winLotto; + this.bonusNums = bonusNums; + } + + public WinStandard checkIfWin(Lotto otherLotto) { + int matchCount = this.winLotto.findMatchCountBy(otherLotto); + boolean isMatchBonus = otherLotto.isContain(bonusNums); + + return WinStandard.findBy(matchCount, isMatchBonus); + } +} diff --git a/src/main/java/lottoGame/model/price/BuyPrice.java b/src/main/java/lottogame/model/price/LottoPurchasePrice.java similarity index 57% rename from src/main/java/lottoGame/model/price/BuyPrice.java rename to src/main/java/lottogame/model/price/LottoPurchasePrice.java index a7345c29aa..9fc712fcb3 100644 --- a/src/main/java/lottoGame/model/price/BuyPrice.java +++ b/src/main/java/lottogame/model/price/LottoPurchasePrice.java @@ -1,9 +1,9 @@ -package lottoGame.model.price; +package lottogame.model.price; -public class BuyPrice { +public class LottoPurchasePrice { private final int price; - public BuyPrice(int price) { + public LottoPurchasePrice(int price) { if (price <= 0) { throw new IllegalArgumentException("로또가격은 0보다 커야 합니다."); } @@ -19,6 +19,14 @@ public int calculateLottoCount(int perLottoPrice) { return price / perLottoPrice; } + public double calculateRateOfReturn(int totalWinReturn) { + if (totalWinReturn < 0) { + throw new IllegalArgumentException("잘못된 값입니다."); + } + + return (double) totalWinReturn / this.price; + } + public int price() { return this.price; } diff --git a/src/main/java/lottogame/model/winner/WinStandard.java b/src/main/java/lottogame/model/winner/WinStandard.java new file mode 100644 index 0000000000..2f1b36a3c3 --- /dev/null +++ b/src/main/java/lottogame/model/winner/WinStandard.java @@ -0,0 +1,56 @@ +package lottogame.model.winner; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; + +public enum WinStandard { + FIRST("1등", 6, false, 2_000_000_000), + SECOND("2등", 5, true, 30_000_000), + THIRD("3등", 5, false, 1_500_000), + FOURTH("4등", 4, false, 50_000), + FIFTH("5등", 3, false, 5_000), + + NOTHING("x", -1, false,0), + ; + + private final String desc; + private final int value; + private final boolean isBonus; + private final int returnOfWin; + + WinStandard(String desc, int value, boolean isBonus, int returnOfWin) { + this.desc = desc; + this.value = value; + this.isBonus = isBonus; + this.returnOfWin = returnOfWin; + } + + public static WinStandard findBy(int value, boolean isBonus) { + return Arrays.stream(WinStandard.values()) + .filter(winStandard -> winStandard.value == value) + .filter(winStandard -> winStandard.isBonus == isBonus) + .findFirst().orElse(NOTHING); + } + + public boolean isNothing() { + return this == NOTHING; + } + + public int returnOfWin() { + return this.returnOfWin; + } + + public static Map getInitWinStandardMap() { + return Arrays.stream(WinStandard.values()) + .filter(winStandard -> !winStandard.isNothing()) + .collect(Collectors.toMap( + winStandard -> winStandard, + winStandard -> 0 + )); + } + + public int value() { + return this.value; + } +} diff --git a/src/main/java/lottogame/model/winner/WinnerResult.java b/src/main/java/lottogame/model/winner/WinnerResult.java new file mode 100644 index 0000000000..b556b7aee4 --- /dev/null +++ b/src/main/java/lottogame/model/winner/WinnerResult.java @@ -0,0 +1,64 @@ +package lottogame.model.winner; + +import static java.util.Objects.isNull; + +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class WinnerResult { + + private Map winStandardToWinCount; + + public WinnerResult() { + this(getInitWinStandardMap()); + } + + public WinnerResult(Map winStandardToWinCount) { + this.winStandardToWinCount = winStandardToWinCount; + } + + private static Map getInitWinStandardMap() { + return Arrays.stream(WinStandard.values()) + .filter(winStandard -> !winStandard.isNothing()) + .collect(Collectors.toMap( + Function.identity(), + winStandard -> 0 + )); + } + + public void addWinResult(WinStandard winStandard) { + if (isNull(winStandard)) { + return; + } + + if (winStandard.isNothing()) { + return; + } + + Integer countBy = this.winStandardToWinCount.getOrDefault(winStandard, 0); + this.winStandardToWinCount.put( + winStandard, + countBy + 1 + ); + } + + public int findWinCount(WinStandard winStandard) { + if (isNull(winStandard) || winStandard.isNothing()) { + return 0; + } + + return this.winStandardToWinCount.get(winStandard); + } + + public int sumTotalWinReturn() { + return this.winStandardToWinCount.keySet().stream() + .mapToInt(this::calculateWinReturnBy) + .sum(); + } + + private int calculateWinReturnBy(WinStandard winStandard) { + return winStandard.returnOfWin() * this.winStandardToWinCount.get(winStandard); + } +} diff --git a/src/main/java/lottogame/view/Casher.java b/src/main/java/lottogame/view/Casher.java new file mode 100644 index 0000000000..cf6b1314b3 --- /dev/null +++ b/src/main/java/lottogame/view/Casher.java @@ -0,0 +1,73 @@ +package lottogame.view; + +import static java.lang.Math.floor; +import static lottogame.model.winner.WinStandard.FIFTH; +import static lottogame.model.winner.WinStandard.FIRST; +import static lottogame.model.winner.WinStandard.FOURTH; +import static lottogame.model.winner.WinStandard.SECOND; +import static lottogame.model.winner.WinStandard.THIRD; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import lottogame.model.price.LottoPurchasePrice; +import lottogame.model.winner.WinnerResult; + +public class Casher { + + private static final String FIRST_WIN_MESSAGE = "6개 일치 (2000000000원)- "; + private static final String SECOND_WIN_MESSAGE = "5개 일치, 보너스볼 일치 (30000000원)- "; + private static final String THIRD_WIN_MESSAGE = "5개 일치 (1500000원)- "; + private static final String FOURTH_WIN_MESSAGE = "4개 일치 (50000원)- "; + private static final String FIFTH_WIN_MESSAGE = "3개 일치 (5000원)- "; + private static final int UP_TWO_DECIMAL_PLACE = 100; + private static final double DOWN_TWO_DECIMAL_PLACE = 100.0; + + public static int askBuyPrice() { + OutputView.printQuestionByBuyPrice(); + return InputView.inputInt(); + } + + public static void informBuyCount(int buyCount) { + OutputView.printBuyLottoCount(buyCount); + } + + public static Set askBeforeWinNums() { + OutputView.printQuestionBeforeWinNums(); + String rawValue = InputView.inputString(); + + return Arrays.stream(rawValue.split(", ")) + .map(Integer::parseInt) + .collect(Collectors.toSet()); + } + + public static int askBonusNum() { + OutputView.printQuestionBonusLottoNum(); + return InputView.inputInt(); + } + + public static void informPublishedLottos(List perLottoNums) { + for (String perLottoNum : perLottoNums) { + OutputView.printPerLottoNums(perLottoNum); + } + } + + public static void informWinResult(WinnerResult winnerResult, LottoPurchasePrice lottoPurchasePrice) { + String msg = FIRST_WIN_MESSAGE + winnerResult.findWinCount(FIRST) + "개\n" + + SECOND_WIN_MESSAGE + winnerResult.findWinCount(SECOND) + "개\n" + + THIRD_WIN_MESSAGE + winnerResult.findWinCount(THIRD) + "개\n" + + FOURTH_WIN_MESSAGE + winnerResult.findWinCount(FOURTH) + "개\n" + + FIFTH_WIN_MESSAGE + winnerResult.findWinCount(FIFTH) + "개\n" + + "총 수익률은 " + polishedRateOfReturn(winnerResult, lottoPurchasePrice) + "입니다."; + + OutputView.printWinResultMsg(msg); + } + + private static String polishedRateOfReturn(WinnerResult winnerResult, LottoPurchasePrice lottoPurchasePrice) { + int totalWinReturn = winnerResult.sumTotalWinReturn(); + double rateOfReturn = lottoPurchasePrice.calculateRateOfReturn(totalWinReturn) * UP_TWO_DECIMAL_PLACE; + + return String.valueOf(floor(rateOfReturn) / DOWN_TWO_DECIMAL_PLACE); + } +} diff --git a/src/main/java/lottoGame/view/InputView.java b/src/main/java/lottogame/view/InputView.java similarity index 97% rename from src/main/java/lottoGame/view/InputView.java rename to src/main/java/lottogame/view/InputView.java index faf1a3d3a0..db90e23da3 100644 --- a/src/main/java/lottoGame/view/InputView.java +++ b/src/main/java/lottogame/view/InputView.java @@ -1,4 +1,4 @@ -package lottoGame.view; +package lottogame.view; import static java.lang.Integer.parseInt; diff --git a/src/main/java/lottoGame/view/OutputView.java b/src/main/java/lottogame/view/OutputView.java similarity index 69% rename from src/main/java/lottoGame/view/OutputView.java rename to src/main/java/lottogame/view/OutputView.java index 72de184840..5e076b531d 100644 --- a/src/main/java/lottoGame/view/OutputView.java +++ b/src/main/java/lottogame/view/OutputView.java @@ -1,10 +1,4 @@ -package lottoGame.view; - -import static java.lang.String.join; - -import java.util.List; -import lottoGame.model.lotto.Lotto; -import lottoGame.model.lotto.Lottos; +package lottogame.view; public class OutputView { @@ -24,9 +18,13 @@ public static void printQuestionBeforeWinNums() { System.out.println("지난 주 당첨 번호를 입력해 주세요."); } - public static void printWinResultMsg(WinResultDto winResultDto) { + public static void printQuestionBonusLottoNum() { + System.out.println("보너스 볼을 입력해 주세요."); + } + + public static void printWinResultMsg(String winResultMessage) { System.out.println("당첨 통계"); System.out.println("---------"); - System.out.println(winResultDto.toString()); + System.out.println(winResultMessage); } } diff --git a/src/test/java/lottoGame/fixture/LottoFixture.java b/src/test/java/lottoGame/fixture/LottoFixture.java deleted file mode 100644 index 3082eb8daa..0000000000 --- a/src/test/java/lottoGame/fixture/LottoFixture.java +++ /dev/null @@ -1,46 +0,0 @@ -package lottoGame.fixture; - -import java.util.ArrayList; -import java.util.List; -import lottoGame.model.lotto.LottoNum; -import lottoGame.model.winner.BeforeWinNums; - -public class LottoFixture { - - public static List 로또번호리스트를_오름차순으로_생성한다(int size) { - return createLottoNumsInRange(0, size); - } - - public static List 로또번호리스트를_지정해서_생성한다(int start, int end) { - return createLottoNumsInRange(start, end); - } - - private static List createLottoNumsInRange(int start, int end) { - List lottoNums = new ArrayList<>(); - for (int i = start; i < end; i++) { - lottoNums.add(new LottoNum(i + 1)); - } - - return lottoNums; - } - - - public static BeforeWinNums 당첨번호리스트를_오름차순으로_생성한다(int size) { - return createIntsInRange(0, size); - } - - public static BeforeWinNums 당첨번호리스트를_지정해서_생성한다(int start, int end) { - return createIntsInRange(start, end); - } - - private static BeforeWinNums createIntsInRange(int start, int end) { - List winNums = new ArrayList<>(); - for (int i = start; i < end; i++) { - winNums.add(i + 1); - } - - return new BeforeWinNums(winNums); - } - - -} diff --git a/src/test/java/lottoGame/model/lotto/LottoMachineTest.java b/src/test/java/lottoGame/model/lotto/LottoMachineTest.java deleted file mode 100644 index 5ee8336cdf..0000000000 --- a/src/test/java/lottoGame/model/lotto/LottoMachineTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package lottoGame.model.lotto; - -import static lottoGame.controller.LottoStore.PER_LOTTO_PRICE; -import static org.assertj.core.api.Assertions.assertThat; - -import lottoGame.model.price.BuyPrice; -import org.junit.jupiter.api.Test; - -class LottoMachineTest { - - - @Test - void 사용자의금액만큼_로또를_발행할_수_있다() { - LottoMachine lottoMachine = new LottoMachine(1000); - BuyPrice buyPrice = new BuyPrice(14000); - Lottos lotto = lottoMachine.publish(buyPrice); - - assertThat( - lotto.size() - ).isEqualTo( - buyPrice.calculateLottoCount(PER_LOTTO_PRICE) - ); - } -} \ No newline at end of file diff --git a/src/test/java/lottoGame/model/lotto/LottoNumFactoryTest.java b/src/test/java/lottoGame/model/lotto/LottoNumFactoryTest.java deleted file mode 100644 index 835cf42768..0000000000 --- a/src/test/java/lottoGame/model/lotto/LottoNumFactoryTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package lottoGame.model.lotto; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatException; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.util.List; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; - -class LottoNumFactoryTest { - - @Test - void 요청받은_갯수만큼_로또번호목록을_생성할_수_있다() { - assertThat( - LottoNumFactory.createLotto(6).size() - ).isEqualTo(6); - } - - @Test - void 요청받은_갯수가_0이하면_예외처리_할_수_있다() { - assertThatThrownBy( - () -> LottoNumFactory.createLotto(0) - ).isInstanceOf(IllegalArgumentException.class); - } -} \ No newline at end of file diff --git a/src/test/java/lottoGame/model/lotto/LottoTest.java b/src/test/java/lottoGame/model/lotto/LottoTest.java deleted file mode 100644 index bb706d74ff..0000000000 --- a/src/test/java/lottoGame/model/lotto/LottoTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package lottoGame.model.lotto; - -import static lottoGame.fixture.LottoFixture.당첨번호리스트를_오름차순으로_생성한다; -import static lottoGame.fixture.LottoFixture.당첨번호리스트를_지정해서_생성한다; -import static lottoGame.fixture.LottoFixture.로또번호리스트를_오름차순으로_생성한다; -import static lottoGame.model.lotto.Lotto.LOTTO_NUM_COUNT; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import lottoGame.model.winner.BeforeWinNums; -import lottoGame.model.winner.WinStandard; -import org.junit.jupiter.api.Test; - -class LottoTest { - - @Test - void 로또생성시_로또번호리스트의_갯수가_정해진값과_다르면_예외처리_할_수_있다() { - assertThatThrownBy(() -> - new Lotto( - 로또번호리스트를_오름차순으로_생성한다(LOTTO_NUM_COUNT + 1) - ) - ).isInstanceOf(IllegalArgumentException.class); - } - - @Test - void 입력받은_당첨번호목록과_로또번호가_1등인지_비교할_수_있다() { - Lotto lotto = new Lotto(로또번호리스트를_오름차순으로_생성한다(6)); - BeforeWinNums beforeWinNums = 당첨번호리스트를_오름차순으로_생성한다(6); - - assertThat( - lotto.checkIfWin(beforeWinNums) - ).isEqualTo(WinStandard.FIRST); - } - - @Test - void 입력받은_당첨번호목록과_로또번호가_꽝인지_비교할_수_있다() { - Lotto lotto = new Lotto(로또번호리스트를_오름차순으로_생성한다(6)); - BeforeWinNums beforeWinNums = 당첨번호리스트를_지정해서_생성한다(5, 11); - - assertThat( - lotto.checkIfWin(beforeWinNums) - ).isEqualTo(WinStandard.NOTHING); - } - -} \ No newline at end of file diff --git a/src/test/java/lottoGame/model/lotto/LottosTest.java b/src/test/java/lottoGame/model/lotto/LottosTest.java deleted file mode 100644 index f1edb00ac3..0000000000 --- a/src/test/java/lottoGame/model/lotto/LottosTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package lottoGame.model.lotto; - -import static lottoGame.fixture.LottoFixture.당첨번호리스트를_오름차순으로_생성한다; -import static lottoGame.fixture.LottoFixture.로또번호리스트를_오름차순으로_생성한다; -import static lottoGame.fixture.LottoFixture.로또번호리스트를_지정해서_생성한다; -import static lottoGame.model.winner.WinStandard.FIRST; -import static lottoGame.model.winner.WinStandard.FOURTH; -import static lottoGame.model.winner.WinStandard.SECOND; -import static lottoGame.model.winner.WinStandard.THIRD; -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.List; -import lottoGame.model.winner.BeforeWinNums; -import lottoGame.model.winner.WinnerResult; -import org.junit.jupiter.api.Test; - -class LottosTest { - - @Test - void 발행된_로또들과_당첨번호를_비고해_당첨결과를_추출할_수_있다() { - Lottos lottos = new Lottos( - List.of( - new Lotto(로또번호리스트를_지정해서_생성한다(3, 9)), - new Lotto(로또번호리스트를_지정해서_생성한다(2, 8)), - new Lotto(로또번호리스트를_지정해서_생성한다(1, 7)), - new Lotto(로또번호리스트를_오름차순으로_생성한다(6)) - ) - ); - BeforeWinNums beforeWinNums = 당첨번호리스트를_오름차순으로_생성한다(6); - - WinnerResult winnerResult = lottos.compareAndElectWinResult(beforeWinNums); - - assertThat(winnerResult.findWinCount(FIRST)).isEqualTo(1); - assertThat(winnerResult.findWinCount(SECOND)).isEqualTo(1); - assertThat(winnerResult.findWinCount(THIRD)).isEqualTo(1); - assertThat(winnerResult.findWinCount(FOURTH)).isEqualTo(1); - } - - @Test - void 발행된_로또갯수를_확인할_수_있다() { - Lottos lottos = new Lottos( - List.of(new Lotto(로또번호리스트를_오름차순으로_생성한다(6))) - ); - - assertThat(lottos.size()).isEqualTo(1); - } -} \ No newline at end of file diff --git a/src/test/java/lottoGame/model/winner/BeforeWinNumsTest.java b/src/test/java/lottoGame/model/winner/BeforeWinNumsTest.java deleted file mode 100644 index d526edc18d..0000000000 --- a/src/test/java/lottoGame/model/winner/BeforeWinNumsTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package lottoGame.model.winner; - -import static lottoGame.fixture.LottoFixture.당첨번호리스트를_오름차순으로_생성한다; -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - -class BeforeWinNumsTest { - - @ParameterizedTest - @CsvSource({"6, true", "7, false"}) - void 특정번호가_당첨번호에_포함되어있는지_확인할_수_있다(int num, boolean expect) { - BeforeWinNums beforeWinNums = 당첨번호리스트를_오름차순으로_생성한다(6); - assertThat( - beforeWinNums.isContain(num) - ).isEqualTo(expect); - } -} \ No newline at end of file diff --git a/src/test/java/lottoGame/model/winner/WinnerResultTest.java b/src/test/java/lottoGame/model/winner/WinnerResultTest.java deleted file mode 100644 index be1361368c..0000000000 --- a/src/test/java/lottoGame/model/winner/WinnerResultTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package lottoGame.model.winner; - -import static lottoGame.model.winner.WinStandard.FIRST; -import static lottoGame.model.winner.WinStandard.FOURTH; -import static lottoGame.model.winner.WinStandard.NOTHING; -import static lottoGame.model.winner.WinStandard.SECOND; -import static lottoGame.model.winner.WinStandard.THIRD; -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.List; -import org.junit.jupiter.api.Test; - -class WinnerResultTest { - - @Test - void 당첨결과에서_각등수가_몇개_당첨됐는지_확인할_수_있다() { - WinnerResult winnerResult = new WinnerResult( - List.of( - WinStandard.FIRST, - WinStandard.SECOND, - WinStandard.THIRD, - WinStandard.FOURTH - ) - ); - - assertThat(winnerResult.findWinCount(FIRST)).isEqualTo(1); - assertThat(winnerResult.findWinCount(SECOND)).isEqualTo(1); - assertThat(winnerResult.findWinCount(THIRD)).isEqualTo(1); - assertThat(winnerResult.findWinCount(FOURTH)).isEqualTo(1); - } - - @Test - void 당첨결과에_미당첨은_추가하지_않을_수_있다() { - WinnerResult winnerResult = new WinnerResult(); - - winnerResult.addWinResult(NOTHING); - - assertThat(winnerResult.findWinCount(NOTHING)).isEqualTo(0); - } - - @Test - void 당첨결과에따른_수익률을_계산_할_수_있다() { - assertThat( - new WinnerResult(List.of(WinStandard.FOURTH)) - .calculateRateOfReturn(14000) - ).isEqualTo(0.35); - } -} \ No newline at end of file diff --git a/src/test/java/lottogame/fixture/LottoFixture.java b/src/test/java/lottogame/fixture/LottoFixture.java new file mode 100644 index 0000000000..3d0e2fba15 --- /dev/null +++ b/src/test/java/lottogame/fixture/LottoFixture.java @@ -0,0 +1,25 @@ +package lottogame.fixture; + +import java.util.HashSet; +import java.util.Set; +import lottogame.model.lotto.LottoNum; + +public class LottoFixture { + + public static Set 로또번호리스트를_오름차순으로_생성한다(int size) { + return createLottoNumsInRange(1, size); + } + + public static Set 로또번호리스트를_지정해서_생성한다(int start, int end) { + return createLottoNumsInRange(start, end); + } + + private static Set createLottoNumsInRange(int start, int end) { + Set lottoNums = new HashSet<>(); + for (int i = start; i <= end; i++) { + lottoNums.add(new LottoNum(i)); + } + + return lottoNums; + } +} diff --git a/src/test/java/lottogame/model/lotto/LottoMachineTest.java b/src/test/java/lottogame/model/lotto/LottoMachineTest.java new file mode 100644 index 0000000000..1c227dc7fb --- /dev/null +++ b/src/test/java/lottogame/model/lotto/LottoMachineTest.java @@ -0,0 +1,39 @@ +package lottogame.model.lotto; + +import static lottogame.model.lotto.LottoMachine.PER_LOTTO_PRICE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.Set; +import lottogame.model.price.LottoPurchasePrice; +import org.junit.jupiter.api.Test; + +class LottoMachineTest { + + @Test + void 사용자의금액만큼_로또를_발행할_수_있다() { + LottoMachine lottoMachine = new LottoMachine(); + LottoPurchasePrice lottoPurchasePrice = new LottoPurchasePrice(14000); + Lottos lotto = lottoMachine.publish(lottoPurchasePrice); + + assertThat( + lotto.size() + ).isEqualTo( + lottoPurchasePrice.calculateLottoCount(PER_LOTTO_PRICE) + ); + } + + @Test + void 요청받은_갯수만큼_로또번호목록을_생성할_수_있다() { + assertThat( + new LottoMachine().createLottoByNums(Set.of(1, 2, 3, 4, 5, 6)).size() + ).isEqualTo(6); + } + + @Test + void 요청받은_갯수가_0이하면_예외처리_할_수_있다() { + assertThatThrownBy( + () -> new LottoMachine().createLottoByNums(Set.of()) + ).isInstanceOf(IllegalArgumentException.class); + } +} \ No newline at end of file diff --git a/src/test/java/lottoGame/model/lotto/LottoNumTest.java b/src/test/java/lottogame/model/lotto/LottoNumTest.java similarity index 93% rename from src/test/java/lottoGame/model/lotto/LottoNumTest.java rename to src/test/java/lottogame/model/lotto/LottoNumTest.java index 1bf0ce40ad..dea325802e 100644 --- a/src/test/java/lottoGame/model/lotto/LottoNumTest.java +++ b/src/test/java/lottogame/model/lotto/LottoNumTest.java @@ -1,4 +1,4 @@ -package lottoGame.model.lotto; +package lottogame.model.lotto; import static org.assertj.core.api.Assertions.assertThatThrownBy; diff --git a/src/test/java/lottogame/model/lotto/LottoTest.java b/src/test/java/lottogame/model/lotto/LottoTest.java new file mode 100644 index 0000000000..2f811fb4b5 --- /dev/null +++ b/src/test/java/lottogame/model/lotto/LottoTest.java @@ -0,0 +1,44 @@ +package lottogame.model.lotto; + +import static lottogame.fixture.LottoFixture.로또번호리스트를_오름차순으로_생성한다; +import static lottogame.fixture.LottoFixture.로또번호리스트를_지정해서_생성한다; +import static lottogame.model.lotto.LottoMachine.LOTTO_NUM_COUNT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class LottoTest { + + @Test + void 로또생성시_로또번호리스트의_갯수가_정해진값과_다르면_예외처리_할_수_있다() { + assertThatThrownBy(() -> + new Lotto( + 로또번호리스트를_오름차순으로_생성한다(LOTTO_NUM_COUNT + 1) + ) + ).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void 전달받은_로또와_객체의_로또를_비교해_같은번호의_갯수를_카운팅할_수_있다() { + Lotto lotto = new Lotto(로또번호리스트를_오름차순으로_생성한다(6)); + Lotto winLotto = new Lotto(로또번호리스트를_지정해서_생성한다(2, 7)); + + assertThat( + winLotto.findMatchCountBy(lotto) + ).isEqualTo(5); + } + + + @ParameterizedTest + @CsvSource({"6, true", "7, false"}) + void 특정번호가_당첨번호에_포함되어있는지_확인할_수_있다(int num, boolean expect) { + Lotto lotto = new Lotto(로또번호리스트를_오름차순으로_생성한다(6)); + + assertThat( + lotto.isContain(new LottoNum(num)) + ).isEqualTo(expect); + } +} \ No newline at end of file diff --git a/src/test/java/lottogame/model/lotto/LottosTest.java b/src/test/java/lottogame/model/lotto/LottosTest.java new file mode 100644 index 0000000000..d1f08076ae --- /dev/null +++ b/src/test/java/lottogame/model/lotto/LottosTest.java @@ -0,0 +1,64 @@ +package lottogame.model.lotto; + +import static lottogame.fixture.LottoFixture.로또번호리스트를_오름차순으로_생성한다; +import static lottogame.fixture.LottoFixture.로또번호리스트를_지정해서_생성한다; +import static lottogame.model.winner.WinStandard.FIRST; +import static lottogame.model.winner.WinStandard.FIFTH; +import static lottogame.model.winner.WinStandard.THIRD; +import static lottogame.model.winner.WinStandard.SECOND; +import static lottogame.model.winner.WinStandard.FOURTH; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import lottogame.model.winner.WinnerResult; +import org.junit.jupiter.api.Test; + +class LottosTest { + + @Test + void 발행된_로또들과_당첨번호를_비고해_당첨결과를_추출할_수_있다() { + Lottos lottos = new Lottos( + List.of( + new Lotto(로또번호리스트를_지정해서_생성한다(4, 9)), + new Lotto(로또번호리스트를_지정해서_생성한다(3, 8)), + new Lotto(로또번호리스트를_지정해서_생성한다(2, 7)), + new Lotto(로또번호리스트를_오름차순으로_생성한다(6)) + ) + ); + Lotto winLotto = new Lotto(로또번호리스트를_오름차순으로_생성한다(6)); + WinningLottoNums winningLottoNums = new WinningLottoNums(winLotto, new LottoNum(45)); + + WinnerResult winnerResult = lottos.compareAndElectWinResult(winningLottoNums); + + assertThat(winnerResult.findWinCount(FIRST)).isEqualTo(1); + assertThat(winnerResult.findWinCount(THIRD)).isEqualTo(1); + assertThat(winnerResult.findWinCount(FOURTH)).isEqualTo(1); + assertThat(winnerResult.findWinCount(FIFTH)).isEqualTo(1); + } + + @Test + void 발행된_로또들과_당첨번호를_비고해_2등_당첨결과를_추출할_수_있다() { + Lottos lottos = new Lottos( + List.of( + new Lotto(로또번호리스트를_지정해서_생성한다(2, 7)) + ) + ); + WinningLottoNums winningLottoNums = new WinningLottoNums( + new Lotto(로또번호리스트를_오름차순으로_생성한다(6)), + new LottoNum(7) + ); + + WinnerResult winnerResult = lottos.compareAndElectWinResult(winningLottoNums); + + assertThat(winnerResult.findWinCount(SECOND)).isEqualTo(1); + } + + @Test + void 발행된_로또갯수를_확인할_수_있다() { + Lottos lottos = new Lottos( + List.of(new Lotto(로또번호리스트를_오름차순으로_생성한다(6))) + ); + + assertThat(lottos.size()).isEqualTo(1); + } +} \ No newline at end of file diff --git a/src/test/java/lottogame/model/lotto/WinningLottoNumsTest.java b/src/test/java/lottogame/model/lotto/WinningLottoNumsTest.java new file mode 100644 index 0000000000..8fc932ed5a --- /dev/null +++ b/src/test/java/lottogame/model/lotto/WinningLottoNumsTest.java @@ -0,0 +1,60 @@ +package lottogame.model.lotto; + +import static lottogame.fixture.LottoFixture.로또번호리스트를_오름차순으로_생성한다; +import static lottogame.fixture.LottoFixture.로또번호리스트를_지정해서_생성한다; +import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; + +import lottogame.model.winner.WinStandard; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class WinningLottoNumsTest { + + @Test + void 입력받은_당첨번호목록과_로또번호가_1등인지_비교할_수_있다() { + WinningLottoNums winningLottoNums = new WinningLottoNums( + new Lotto(로또번호리스트를_오름차순으로_생성한다(6)), + new LottoNum(7) + ); + Lotto lotto = new Lotto(로또번호리스트를_오름차순으로_생성한다(6)); + + assertThat( + winningLottoNums.checkIfWin(lotto) + ).isEqualTo(WinStandard.FIRST); + } + + @Test + void 입력받은_당첨번호목록과_로또번호가_꽝인지_비교할_수_있다() { + WinningLottoNums winningLottoNums = new WinningLottoNums( + new Lotto(로또번호리스트를_지정해서_생성한다(6, 11)), + new LottoNum(45) + ); + + Lotto lotto = new Lotto(로또번호리스트를_오름차순으로_생성한다(6)); + + assertThat( + winningLottoNums.checkIfWin(lotto) + ).isEqualTo(WinStandard.NOTHING); + } + + @Test + void 입력받은_당첨번호목록과_보너스볼이_2등인지_비교할_수_있다() { + WinningLottoNums winningLottoNums = new WinningLottoNums( + new Lotto(로또번호리스트를_지정해서_생성한다(2, 7)), + new LottoNum(1) + ); + Lotto lotto = new Lotto(로또번호리스트를_오름차순으로_생성한다(6)); + + assertThat( + winningLottoNums.checkIfWin(lotto) + ).isEqualTo(WinStandard.SECOND); + } + + @Test + void 입력받은_당첨번호목록에_보너스볼이_존재하면_예외처리_할_수_있다() { + assertThatThrownBy( + () -> new WinningLottoNums(new Lotto(로또번호리스트를_지정해서_생성한다(2, 7)), new LottoNum(3)) + ).isInstanceOf(IllegalArgumentException.class); + } +} \ No newline at end of file diff --git a/src/test/java/lottoGame/model/price/BuyPriceTest.java b/src/test/java/lottogame/model/price/LottoPurchasePriceTest.java similarity index 51% rename from src/test/java/lottoGame/model/price/BuyPriceTest.java rename to src/test/java/lottogame/model/price/LottoPurchasePriceTest.java index c7a8197a99..a6b658e297 100644 --- a/src/test/java/lottoGame/model/price/BuyPriceTest.java +++ b/src/test/java/lottogame/model/price/LottoPurchasePriceTest.java @@ -1,23 +1,31 @@ -package lottoGame.model.price; +package lottogame.model.price; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.within; import org.junit.jupiter.api.Test; -class BuyPriceTest { +class LottoPurchasePriceTest { @Test void 로또_구매가격이_0이하면_예외처리를_할_수_있다() { assertThatThrownBy( - () -> new BuyPrice(0) + () -> new LottoPurchasePrice(0) ).isInstanceOf(IllegalArgumentException.class); } @Test void 개당_로또가격을_기반으로_발행해야하는_로또갯수를_계산할_수_있다() { assertThat( - new BuyPrice(14000).calculateLottoCount(1000) + new LottoPurchasePrice(14000).calculateLottoCount(1000) ).isEqualTo(14); } + + @Test + void 총상금으로_구매가격에비해_수익률이_얼마인지_계산할_수_있다() { + assertThat( + new LottoPurchasePrice(14000).calculateRateOfReturn(5000) + ).isCloseTo(0.35, within(0.01)); + } } \ No newline at end of file diff --git a/src/test/java/lottogame/model/winner/WinStandardTest.java b/src/test/java/lottogame/model/winner/WinStandardTest.java new file mode 100644 index 0000000000..f52f863264 --- /dev/null +++ b/src/test/java/lottogame/model/winner/WinStandardTest.java @@ -0,0 +1,55 @@ +package lottogame.model.winner; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Map; +import org.junit.jupiter.api.Test; + +class WinStandardTest { + + @Test + void 일등을_집계할_수_있다() { + assertThat( + WinStandard.findBy(6, false) + ).isEqualTo(WinStandard.FIRST); + } + + @Test + void 이등을_집계할_수_있다() { + assertThat( + WinStandard.findBy(5, true) + ).isEqualTo(WinStandard.SECOND); + } + + @Test + void 삼등을_집계할_수_있다() { + assertThat( + WinStandard.findBy(5, false) + ).isEqualTo(WinStandard.THIRD); + } + + @Test + void 사등을_집계할_수_있다() { + assertThat( + WinStandard.findBy(4, false) + ).isEqualTo(WinStandard.FOURTH); + } + + @Test + void 오등을_집계할_수_있다() { + assertThat( + WinStandard.findBy(3, false) + ).isEqualTo(WinStandard.FIFTH); + } + + @Test + void 각등수를_키값으로하고_값이_0인_Map을_생성할_수_있다() { + Map result = WinStandard.getInitWinStandardMap(); + + assertThat(result.get(WinStandard.FIRST)).isEqualTo(0); + assertThat(result.get(WinStandard.SECOND)).isEqualTo(0); + assertThat(result.get(WinStandard.THIRD)).isEqualTo(0); + assertThat(result.get(WinStandard.FOURTH)).isEqualTo(0); + assertThat(result.get(WinStandard.FIFTH)).isEqualTo(0); + } +} \ No newline at end of file diff --git a/src/test/java/lottogame/model/winner/WinnerResultTest.java b/src/test/java/lottogame/model/winner/WinnerResultTest.java new file mode 100644 index 0000000000..0b0c941283 --- /dev/null +++ b/src/test/java/lottogame/model/winner/WinnerResultTest.java @@ -0,0 +1,45 @@ +package lottogame.model.winner; + +import static lottogame.model.winner.WinStandard.FIFTH; +import static lottogame.model.winner.WinStandard.FIRST; +import static lottogame.model.winner.WinStandard.FOURTH; +import static lottogame.model.winner.WinStandard.NOTHING; +import static lottogame.model.winner.WinStandard.THIRD; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import lottogame.model.price.LottoPurchasePrice; +import org.junit.jupiter.api.Test; + +class WinnerResultTest { + + @Test + void 당첨결과에서_각등수가_몇개_당첨됐는지_확인할_수_있다() { + WinnerResult winnerResult = new WinnerResult(); + List.of(FIRST, THIRD, FOURTH, FIFTH).forEach(winnerResult::addWinResult); + + assertThat(winnerResult.findWinCount(FIRST)).isEqualTo(1); + assertThat(winnerResult.findWinCount(THIRD)).isEqualTo(1); + assertThat(winnerResult.findWinCount(FOURTH)).isEqualTo(1); + assertThat(winnerResult.findWinCount(FIFTH)).isEqualTo(1); + } + + @Test + void 당첨결과에_미당첨은_추가하지_않을_수_있다() { + WinnerResult winnerResult = new WinnerResult(); + + winnerResult.addWinResult(NOTHING); + + assertThat(winnerResult.findWinCount(NOTHING)).isEqualTo(0); + } + + @Test + void 당첨결과에따른_총상금을_계산_할_수_있다() { + WinnerResult winnerResult = new WinnerResult(); + winnerResult.addWinResult(FIFTH); + + assertThat( + winnerResult.sumTotalWinReturn() + ).isEqualTo(FIFTH.returnOfWin()); + } +} \ No newline at end of file