diff --git a/.gitignore b/.gitignore index 573e847..09f21d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ .gradle/ build/ +out \ No newline at end of file diff --git a/README.md b/README.md index 7c9722c..0bd5059 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,21 @@ # ๐Ÿš€ ๋กœ๋˜ 1๋‹จ๊ณ„ - ์ž๋™ +## ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ ๋ชฉ๋ก +- [ ] ๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. +- [ ] ๊ตฌ์ž…๊ธˆ์•ก์— ๋”ฐ๋ผ ๋ช‡๊ฐœ์˜ ๋กœ๋˜๋ฅผ ๊ตฌ์ž…ํ–ˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค. +- [ ] ๋กœ๋˜์˜ ๊ฐฏ์ˆ˜์— ๋งž์ถ”์–ด 1~45๊นŒ์ง€ 6๊ฐœ์˜ ๋žœ๋ค ์ˆซ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค. +- [ ] ์ง€๋‚œ ์ฃผ ๋‹ด์ฒญ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. +- [ ] ๋ณด๋„ˆ์Šค ๋ณผ์„ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. +- [ ] ๋‹ด์ฒญ ํ†ต๊ณ„๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- [ ] ๋กœ๋˜์™€ ๋‹ด์ฒญ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ฐ ๋ช‡๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๋งž์•˜๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค. +- [ ] ์ด ์ˆ˜์ต๋ฅ ์„ ์†Œ์ˆซ์  2๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•œ๋‹ค. + +## ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ชฉ๋ก +- [ ] ๊ตฌ์ž…๊ธˆ์•ก์ด 0์›์ผ ๊ฒฝ์šฐ ๋กœ๋˜ ํ•œ์žฅ์˜ ๊ฐ€๊ฒฉ์ด 1000์›์ด๋ผ๋Š” ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ `๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”` ๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. +- [ ] ๋กœ๋˜ ๋„˜๋ฒ„์˜ ๋ฒ”์œ„๋Š” 1~45๊นŒ์ง€๋กœ ์ œํ•œํ•˜๋ฉฐ ์ด์™ธ์˜ ์ˆซ์ž๋Š” ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. +- [ ] ๋‹ด์ฒญ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์€ ์ƒํ™ฉ์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. +- [ ] ์ €๋ฒˆ์ฃผ ๋‹ด์ฒญ๋ฒˆํ˜ธ๋“ค์ด 2๊ฐœ ์ด์ƒ ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. + ## ๊ธฐ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ - ๋กœ๋˜ ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ์ž… ๊ธˆ์•ก์— ํ•ด๋‹นํ•˜๋Š” ๋กœ๋˜๋ฅผ ๋ฐœ๊ธ‰ํ•ด์•ผ ํ•œ๋‹ค. - ๋กœ๋˜ 1์žฅ์˜ ๊ฐ€๊ฒฉ์€ 1000์›์ด๋‹ค. diff --git a/src/main/java/lotto/LottoApplication.java b/src/main/java/lotto/LottoApplication.java new file mode 100644 index 0000000..0f0314c --- /dev/null +++ b/src/main/java/lotto/LottoApplication.java @@ -0,0 +1,29 @@ +package lotto; + +import lotto.domain.LottoStore; +import lotto.domain.NumberOfLottoTicket; +import lotto.domain.PurchaseAmount; +import lotto.domain.lotto.LastWeekWinningBonusBall; +import lotto.domain.lotto.LastWeekWinningLotto; +import lotto.domain.lotto.LottoAutomaticTickets; +import lotto.domain.lotto.LottoManualTickets; + +public class LottoApplication { + private final LottoStore lottoStore = new LottoStore(); + + public void run() { + PurchaseAmount purchaseAmount = lottoStore.inputLottoPurchaseAmount(); + int lottoManualTicketNumber = lottoStore.inputLottoManualTicketNumber(); + LottoManualTickets lottoManualTickets = lottoStore.inputLottoManualTickets(lottoManualTicketNumber); + NumberOfLottoTicket numberOfLottoTicket = lottoStore.informNumberOfLottoTicket(purchaseAmount, lottoManualTicketNumber); + LottoAutomaticTickets lottoAutomaticTickets = lottoStore.informLottoAutomaticTickets(lottoManualTickets, numberOfLottoTicket); + LastWeekWinningLotto lastWeekWinningLotto = lottoStore.inputLastWeekWinningLotto(); + LastWeekWinningBonusBall lastWeekWinningBonusBall = lottoStore.inputLastWeekWinningBonusBall(); + lottoStore.informLottoStatistics(numberOfLottoTicket, lottoManualTickets, lottoAutomaticTickets, lastWeekWinningLotto, lastWeekWinningBonusBall); + } + + public static void main(String[] args) { + LottoApplication app = new LottoApplication(); + app.run(); + } +} diff --git a/src/main/java/lotto/domain/LottoMachine.java b/src/main/java/lotto/domain/LottoMachine.java new file mode 100644 index 0000000..447450b --- /dev/null +++ b/src/main/java/lotto/domain/LottoMachine.java @@ -0,0 +1,146 @@ +package lotto.domain; + +import lotto.domain.lotto.*; +import lotto.domain.strategy.RandomLottoNumberGenerator; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class LottoMachine { + private final static int LOTTO_SIZE = 6; + private final static int WINNING_LOTTO_WITH_BONUS_BALL_MATCHED_COUNT = 5; + + private final RandomLottoNumberGenerator randomLottoNumberGenerator = new RandomLottoNumberGenerator(); + private final ArrayList lottoPrices = new ArrayList<>(); + + public LottoAutomaticTickets generateLottoAutomaticTicket(NumberOfLottoTicket numberOfLottoTicket) { + int numberOfLottoAutomaticTicket = numberOfLottoTicket.getAutomaticLottoTicketOfNumber(); + List lottoAutomaticTicket = IntStream.range(0, numberOfLottoAutomaticTicket). + mapToObj(lottoTicket -> new LottoAutomaticTicket(randomLottoNumberGenerator.getRandomLottoNumbers())). + collect(Collectors.toList()); + return new LottoAutomaticTickets(lottoAutomaticTicket); + } + + public List lottoManualTicketsDiscriminator( + LottoManualTickets lottoManualTickets, + LastWeekWinningLotto lastWeekWinningLotto, + LastWeekWinningBonusBall lastWeekWinningBonusBall) { + List lottoMatchStatuses = new ArrayList<>(); + + for (LottoManualTicket lottoManualTicket : lottoManualTickets.getLottoManualTickets()) { + int matchedLottoManualTicketCount = calculateMatchedLottoManualTicket(lottoManualTicket, lastWeekWinningLotto); + Boolean isMatchedBonusBall = calculateMatchedBonusBallManualTicket(lottoManualTicket, lastWeekWinningBonusBall); + LottoMatchStatus lottoMatchStatus = new LottoMatchStatus(matchedLottoManualTicketCount, isMatchedBonusBall); + lottoMatchStatuses.add(lottoMatchStatus); + } + return lottoMatchStatuses; + } + + public List lottoAutomaticTicketsDiscriminator( + LottoAutomaticTickets lottoAutomaticTickets, + LastWeekWinningLotto lastWeekWinningLotto, + LastWeekWinningBonusBall lastWeekWinningBonusBall) { + List lottoMatchStatuses = new ArrayList<>(); + + for (LottoAutomaticTicket lottoAutomaticTicket : lottoAutomaticTickets.getLottoAutomaticTickets()) { + int matchedLottoAutomaticTicketCount = calculateMatchedLottoAutomaticTicket(lottoAutomaticTicket, lastWeekWinningLotto); + Boolean isMatchedBonusBall = calculateMatchedBonusBallAutomaticTicket(lottoAutomaticTicket, lastWeekWinningBonusBall); + LottoMatchStatus lottoMatchStatus = new LottoMatchStatus(matchedLottoAutomaticTicketCount, isMatchedBonusBall); + lottoMatchStatuses.add(lottoMatchStatus); + } + return lottoMatchStatuses; + } + + private Boolean calculateMatchedBonusBallAutomaticTicket( + LottoAutomaticTicket lottoAutomaticTicket, + LastWeekWinningBonusBall lastWeekWinningBonusBall) { + + List lottoTicket = lottoAutomaticTicket.getLotto(); + int WinningBonusBalls = lastWeekWinningBonusBall.getLastWeekWinningBonusBall(); + return lottoTicket.contains(WinningBonusBalls); + } + + private int calculateMatchedLottoAutomaticTicket( + LottoAutomaticTicket lottoAutomaticTicket, + LastWeekWinningLotto lastWeekWinningLotto) { + + List lottoTicket = lottoAutomaticTicket.getLotto(); + List lastWeekWinningLottoTicket = lastWeekWinningLotto.getLotto(); + int matchedCount = 0; + for (int lottoNumber = 0; lottoNumber < LOTTO_SIZE; lottoNumber++) { + Boolean isMatchLottoNumber = lottoTicket.contains(lastWeekWinningLottoTicket.get(lottoNumber)); + matchedCount += countMatchedLottoNumber(isMatchLottoNumber); + } + return matchedCount; + + } + + private Boolean calculateMatchedBonusBallManualTicket( + LottoManualTicket lottoManualTicket, + LastWeekWinningBonusBall lastWeekWinningBonusBall) { + + List lottoTicket = lottoManualTicket.getLotto(); + int WinningBonusBalls = lastWeekWinningBonusBall.getLastWeekWinningBonusBall(); + return lottoTicket.contains(WinningBonusBalls); + } + + private int calculateMatchedLottoManualTicket( + LottoManualTicket lottoManualTicket, + LastWeekWinningLotto lastWeekWinningLotto) { + + List lottoTicket = lottoManualTicket.getLotto(); + List lastWeekWinningLottoTicket = lastWeekWinningLotto.getLotto(); + int matchedCount = 0; + for (int lottoNumber = 0; lottoNumber < LOTTO_SIZE; lottoNumber++) { + Boolean isMatchLottoNumber = lottoTicket.contains(lastWeekWinningLottoTicket.get(lottoNumber)); + matchedCount += countMatchedLottoNumber(isMatchLottoNumber); + } + return matchedCount; + } + + private int countMatchedLottoNumber(Boolean isMatchLottoNumber) { + if (isMatchLottoNumber) { + return 1; + } + return 0; + } + + public List getStatistics(List lottoAllTicketMatchStatuses) { + for (LottoMatchStatus lottoMatchStatus : lottoAllTicketMatchStatuses) { + mappingLottoTicketWithBonusBall(lottoMatchStatus); + } + return lottoPrices; + } + + private void mappingLottoTicketWithBonusBall(LottoMatchStatus lottoMatchStatus) { + for (WinningStatus winningStatus : WinningStatus.values()) { + compareWinningStatus(winningStatus, lottoMatchStatus.getMatchedCount(), lottoMatchStatus.getHasBonusBall()); + } + } + + private void compareWinningStatus(WinningStatus winningStatus, int lottoMatchedCount, Boolean hasBonusBall) { + if ((winningStatus.getMatchCount() == lottoMatchedCount) && (lottoMatchedCount != WINNING_LOTTO_WITH_BONUS_BALL_MATCHED_COUNT)) { + lottoPrices.add(winningStatus); + } + if ((winningStatus.hasBonusBall() == hasBonusBall) && (lottoMatchedCount == WINNING_LOTTO_WITH_BONUS_BALL_MATCHED_COUNT)) { + lottoPrices.add(winningStatus); + } + } + + public Map getMappingLottoWithBonusBall() { + Map mappingLottoWithBonusBall = new HashMap<>(); + + for (WinningStatus key : lottoPrices) { + mappingLottoWithBonusBall.put(key, mappingLottoWithBonusBall.getOrDefault(key, 0) + 1); + } + return mappingLottoWithBonusBall; + } + + public Profit getProfitInformation(List lottoStatistics, NumberOfLottoTicket numberOfLottoTicket) { + return new Profit(lottoStatistics, numberOfLottoTicket); + } +} diff --git a/src/main/java/lotto/domain/LottoMatchStatus.java b/src/main/java/lotto/domain/LottoMatchStatus.java new file mode 100644 index 0000000..85e7b7a --- /dev/null +++ b/src/main/java/lotto/domain/LottoMatchStatus.java @@ -0,0 +1,19 @@ +package lotto.domain; + +public class LottoMatchStatus { + private final int matchedCount; + private final Boolean hasBonusBall; + + public LottoMatchStatus(int matchedCount, Boolean hasBonusBall) { + this.matchedCount = matchedCount; + this.hasBonusBall = hasBonusBall; + } + + public int getMatchedCount() { + return matchedCount; + } + + public Boolean getHasBonusBall() { + return hasBonusBall; + } +} diff --git a/src/main/java/lotto/domain/LottoStore.java b/src/main/java/lotto/domain/LottoStore.java new file mode 100644 index 0000000..12e9685 --- /dev/null +++ b/src/main/java/lotto/domain/LottoStore.java @@ -0,0 +1,80 @@ +package lotto.domain; + +import lotto.domain.lotto.*; +import lotto.ui.Printer; +import lotto.ui.Receiver; + +import java.util.ArrayList; +import java.util.List; + +public class LottoStore { + private final Printer printer = new Printer(); + private final Receiver receiver = new Receiver(); + private final LottoMachine lottoMachine = new LottoMachine(); + + public PurchaseAmount inputLottoPurchaseAmount() { + printer.requestPurchaseAmount(); + int lottoPurchaseAmount = receiver.receiveLottoPurchaseAmount(); + + return new PurchaseAmount(lottoPurchaseAmount); + } + + public int inputLottoManualTicketNumber() { + printer.requestLottoManualTicketNumber(); + return receiver.receiverLottoManualTicketNumber(); + } + + public LottoManualTickets inputLottoManualTickets(int lottoManualTicketNumber) { + ArrayList lottoManualTicketDummy = new ArrayList<>(); + printer.requestLottoManualTicket(); + for (int lottoTicket = 0; lottoTicket < lottoManualTicketNumber; lottoTicket++) { + LottoManualTicket lottoManualTicket = new LottoManualTicket(receiver.receiverLottoManualTicket()); + lottoManualTicketDummy.add(lottoManualTicket); + } + return new LottoManualTickets(lottoManualTicketDummy); + } + + public LottoAutomaticTickets informLottoAutomaticTickets(LottoManualTickets lottoManualTickets, NumberOfLottoTicket numberOfLottoTicket) { + printer.printLottoManualTickets(lottoManualTickets); + LottoAutomaticTickets lottoAutomaticTickets = lottoMachine.generateLottoAutomaticTicket(numberOfLottoTicket); + printer.printLottoAutomaticTickets(lottoAutomaticTickets); + return lottoAutomaticTickets; + } + + public NumberOfLottoTicket informNumberOfLottoTicket(PurchaseAmount purchaseAmount, int lottoManualTicketNumber) { + NumberOfLottoTicket numberOfLottoTicket = new NumberOfLottoTicket(purchaseAmount, lottoManualTicketNumber); + printer.printNumberOfEachLottoTicket(numberOfLottoTicket); + return numberOfLottoTicket; + } + + public LastWeekWinningLotto inputLastWeekWinningLotto() { + printer.requestLastWeekLottoWinningNumber(); + List LastWeekLottoWinningNumbers = receiver.receiveLastWeekLottoWinningNumbers(); + return new LastWeekWinningLotto(LastWeekLottoWinningNumbers); + } + + public LastWeekWinningBonusBall inputLastWeekWinningBonusBall() { + printer.requestLottoBonusBallNumber(); + int BonusBall = receiver.receiveLottoBonusBallNumber(); + return new LastWeekWinningBonusBall(BonusBall); + } + + public void informLottoStatistics( + NumberOfLottoTicket numberOfLottoTicket, + LottoManualTickets lottoManualTickets, + LottoAutomaticTickets lottoAutomaticTickets, + LastWeekWinningLotto lastWeekWinningLotto, + LastWeekWinningBonusBall lastWeekWinningBonusBall) { + + List lottoAllTicketMatchStatuses = new ArrayList<>(); + List lottoManualTicketMatchStatuses = lottoMachine.lottoManualTicketsDiscriminator(lottoManualTickets, lastWeekWinningLotto, lastWeekWinningBonusBall); + List lottoAutomaticTicketMatchStatuses = lottoMachine.lottoAutomaticTicketsDiscriminator(lottoAutomaticTickets, lastWeekWinningLotto, lastWeekWinningBonusBall); + lottoAllTicketMatchStatuses.addAll(lottoManualTicketMatchStatuses); + lottoAllTicketMatchStatuses.addAll(lottoAutomaticTicketMatchStatuses); + List lottoStatistics = lottoMachine.getStatistics(lottoAllTicketMatchStatuses); + printer.printAllMatchedLottoResults(lottoMachine.getMappingLottoWithBonusBall()); + Profit profit = lottoMachine.getProfitInformation(lottoStatistics, numberOfLottoTicket); + printer.printLottoProfit(profit.getProfit()); + printer.printIsLottoProfit(profit.isProfit()); + } +} diff --git a/src/main/java/lotto/domain/NumberOfLottoTicket.java b/src/main/java/lotto/domain/NumberOfLottoTicket.java new file mode 100644 index 0000000..ebe5d52 --- /dev/null +++ b/src/main/java/lotto/domain/NumberOfLottoTicket.java @@ -0,0 +1,44 @@ +package lotto.domain; + +import lotto.domain.validation.LottoTicketOfNumberValidation; + +public class NumberOfLottoTicket { + private final static int LOTTO_TICKET_PRICE = 1000; + + private final int totalNumberOfLottoTicket; + private final int numberOfManualLottoTicket; + private final int automaticLottoTicketOfNumber; + + public NumberOfLottoTicket(PurchaseAmount purchaseAmount, int purchaseManualLottoOfNumber) { + LottoTicketOfNumberValidation lottoTicketOfNumberValidation = new LottoTicketOfNumberValidation(); + this.totalNumberOfLottoTicket = calculateLottoTicketOfNumber(purchaseAmount.getPurchaseAmount()); + this.numberOfManualLottoTicket = purchaseManualLottoOfNumber; + this.automaticLottoTicketOfNumber = calculateAutomaticLottoTicketOfNumber(totalNumberOfLottoTicket, numberOfManualLottoTicket); + lottoTicketOfNumberValidation.checkLottoTicketOfNumber(this.totalNumberOfLottoTicket); + } + + private int calculateLottoTicketOfNumber(int purchaseAmount) { + return purchaseAmount / LOTTO_TICKET_PRICE; + } + + private int calculateAutomaticLottoTicketOfNumber(int lottoTicketOfTotalNumber, int manualLottoTicketOfNumber) { + return lottoTicketOfTotalNumber - manualLottoTicketOfNumber; + } + + public int getLottoTicketPrice() { + return LOTTO_TICKET_PRICE; + } + + public int getNumberOfManualLottoTicket() { + return numberOfManualLottoTicket; + } + + public int getAutomaticLottoTicketOfNumber() { + return automaticLottoTicketOfNumber; + } + + public int getTotalNumberOfLottoTicket() { + return totalNumberOfLottoTicket; + } + +} diff --git a/src/main/java/lotto/domain/Profit.java b/src/main/java/lotto/domain/Profit.java new file mode 100644 index 0000000..bffbda6 --- /dev/null +++ b/src/main/java/lotto/domain/Profit.java @@ -0,0 +1,36 @@ +package lotto.domain; + +import java.util.List; + +public class Profit { + private static final int PROFIT_THRESHOLD = 1; + + private final int sumLottoPrices; + private final NumberOfLottoTicket numberOfLottoTicket; + private final float calculatedProfit; + + public Profit(List lottoPrices, NumberOfLottoTicket numberOfLottoTicket) { + this.sumLottoPrices = sumLottoPrices(lottoPrices); + this.numberOfLottoTicket = numberOfLottoTicket; + this.calculatedProfit = CalculatedProfit(); + } + + private int sumLottoPrices(List lottoPrices) { + return lottoPrices.stream().mapToInt(WinningStatus::getWinningMoney).sum(); + } + + private float CalculatedProfit() { + return (float) sumLottoPrices / (numberOfLottoTicket.getTotalNumberOfLottoTicket() * numberOfLottoTicket.getLottoTicketPrice()); + } + + public boolean isProfit() { + if (calculatedProfit < PROFIT_THRESHOLD) { + return false; + } + return true; + } + + public float getProfit() { + return calculatedProfit; + } +} diff --git a/src/main/java/lotto/domain/PurchaseAmount.java b/src/main/java/lotto/domain/PurchaseAmount.java new file mode 100644 index 0000000..44f77d0 --- /dev/null +++ b/src/main/java/lotto/domain/PurchaseAmount.java @@ -0,0 +1,23 @@ +package lotto.domain; + + +public class PurchaseAmount { + private static final int MIN_PURCHASE_AMOUNT = 0; + + private final int purchaseAmount; + + public PurchaseAmount(int purchaseAmount) { + this.purchaseAmount = purchaseAmount; + checkPurchaseAmountNotPositive(purchaseAmount); + } + + private void checkPurchaseAmountNotPositive(int purchaseAmount) { + if (purchaseAmount <= MIN_PURCHASE_AMOUNT) { + throw new IllegalArgumentException("๊ตฌ๋งค ๊ธˆ์•ก์€ 0์› ์ดํ•˜๊ฐ€ ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + public int getPurchaseAmount() { + return purchaseAmount; + } +} diff --git a/src/main/java/lotto/domain/WinningStatus.java b/src/main/java/lotto/domain/WinningStatus.java new file mode 100644 index 0000000..0f5645b --- /dev/null +++ b/src/main/java/lotto/domain/WinningStatus.java @@ -0,0 +1,31 @@ +package lotto.domain; + +public enum WinningStatus { + SIX_MATCH(6, false, 2000000000), + FIVE_MATCH_WITH_BONUS_BALL(5, true, 30000000), + FIVE_MATCH(5, false, 1500000), + FOUR_MATCH(4, false, 50000), + THREE_MATCH(3, false, 5000); + + private final int matchCount; + private final int winningMoney; + private final boolean BonusBall; + + WinningStatus(int matchCount, boolean BonusBall, int winningMoney) { + this.matchCount = matchCount; + this.winningMoney = winningMoney; + this.BonusBall = BonusBall; + } + + public int getMatchCount() { + return this.matchCount; + } + + public boolean hasBonusBall() { + return this.BonusBall; + } + + public int getWinningMoney() { + return this.winningMoney; + } +} diff --git a/src/main/java/lotto/domain/exception/NotDuplicatedNumberException.java b/src/main/java/lotto/domain/exception/NotDuplicatedNumberException.java new file mode 100644 index 0000000..23c6392 --- /dev/null +++ b/src/main/java/lotto/domain/exception/NotDuplicatedNumberException.java @@ -0,0 +1,9 @@ +package lotto.domain.exception; + +public class NotDuplicatedNumberException extends RuntimeException{ + private static final String MESSAGE = "์ค‘๋ณต๋˜๋Š” ๋กœ๋˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์‹ค ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."; + + public NotDuplicatedNumberException() { + super(MESSAGE); + } +} diff --git a/src/main/java/lotto/domain/exception/NotValidLottoLengthException.java b/src/main/java/lotto/domain/exception/NotValidLottoLengthException.java new file mode 100644 index 0000000..a8704c2 --- /dev/null +++ b/src/main/java/lotto/domain/exception/NotValidLottoLengthException.java @@ -0,0 +1,9 @@ +package lotto.domain.exception; + +public class NotValidLottoLengthException extends RuntimeException { + private static final String MESSAGE = "๋ฒˆํ˜ธ๋Š” 6๊ฐœ๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."; + + public NotValidLottoLengthException() { + super(MESSAGE); + } +} diff --git a/src/main/java/lotto/domain/exception/NotValidLottoTicketOfNumberException.java b/src/main/java/lotto/domain/exception/NotValidLottoTicketOfNumberException.java new file mode 100644 index 0000000..ea5f0cb --- /dev/null +++ b/src/main/java/lotto/domain/exception/NotValidLottoTicketOfNumberException.java @@ -0,0 +1,9 @@ +package lotto.domain.exception; + +public class NotValidLottoTicketOfNumberException extends RuntimeException{ + private static final String MESSAGE = "๋กœ๋˜๋ฅผ ์‚ด ๊ธˆ์•ก์ด ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค."; + + public NotValidLottoTicketOfNumberException(){ + super(MESSAGE); + } +} diff --git a/src/main/java/lotto/domain/lotto/LastWeekWinningBonusBall.java b/src/main/java/lotto/domain/lotto/LastWeekWinningBonusBall.java new file mode 100644 index 0000000..e407073 --- /dev/null +++ b/src/main/java/lotto/domain/lotto/LastWeekWinningBonusBall.java @@ -0,0 +1,13 @@ +package lotto.domain.lotto; + +public class LastWeekWinningBonusBall { + private final int bonusBall; + + public LastWeekWinningBonusBall(int bonusBall) { + this.bonusBall = bonusBall; + } + + public int getLastWeekWinningBonusBall() { + return this.bonusBall; + } +} diff --git a/src/main/java/lotto/domain/lotto/LastWeekWinningLotto.java b/src/main/java/lotto/domain/lotto/LastWeekWinningLotto.java new file mode 100644 index 0000000..ffc11f0 --- /dev/null +++ b/src/main/java/lotto/domain/lotto/LastWeekWinningLotto.java @@ -0,0 +1,17 @@ +package lotto.domain.lotto; + +import java.util.List; + +public class LastWeekWinningLotto extends Lotto { + private final List winningLotto; + + public LastWeekWinningLotto(List winningLotto) { + super(winningLotto); + this.winningLotto = winningLotto; + } + + public List getLotto() { + return winningLotto; + } + +} diff --git a/src/main/java/lotto/domain/lotto/Lotto.java b/src/main/java/lotto/domain/lotto/Lotto.java new file mode 100644 index 0000000..c9d33c5 --- /dev/null +++ b/src/main/java/lotto/domain/lotto/Lotto.java @@ -0,0 +1,18 @@ +package lotto.domain.lotto; + +import lotto.domain.validation.LottoValidation; + +import java.util.List; + +abstract class Lotto { + private final List numbers; + + public Lotto(List numbers) { + LottoValidation lottoValidation = new LottoValidation(); + lottoValidation.checkLottoLength(numbers); + lottoValidation.checkDuplicatedNumber(numbers); + this.numbers = numbers; + } + + public abstract List getLotto(); +} diff --git a/src/main/java/lotto/domain/lotto/LottoAutomaticTicket.java b/src/main/java/lotto/domain/lotto/LottoAutomaticTicket.java new file mode 100644 index 0000000..c66860a --- /dev/null +++ b/src/main/java/lotto/domain/lotto/LottoAutomaticTicket.java @@ -0,0 +1,16 @@ +package lotto.domain.lotto; + +import java.util.List; + +public class LottoAutomaticTicket extends Lotto { + private final List numbers; + + public LottoAutomaticTicket(List numbers) { + super(numbers); + this.numbers = numbers; + } + + public List getLotto() { + return numbers; + } +} diff --git a/src/main/java/lotto/domain/lotto/LottoAutomaticTickets.java b/src/main/java/lotto/domain/lotto/LottoAutomaticTickets.java new file mode 100644 index 0000000..61d7896 --- /dev/null +++ b/src/main/java/lotto/domain/lotto/LottoAutomaticTickets.java @@ -0,0 +1,15 @@ +package lotto.domain.lotto; + +import java.util.List; + +public class LottoAutomaticTickets { + private final List lottoAutomaticTickets; + + public LottoAutomaticTickets(List lottoAutomaticTickets) { + this.lottoAutomaticTickets = lottoAutomaticTickets; + } + + public List getLottoAutomaticTickets() { + return lottoAutomaticTickets; + } +} diff --git a/src/main/java/lotto/domain/lotto/LottoManualTicket.java b/src/main/java/lotto/domain/lotto/LottoManualTicket.java new file mode 100644 index 0000000..536fd87 --- /dev/null +++ b/src/main/java/lotto/domain/lotto/LottoManualTicket.java @@ -0,0 +1,16 @@ +package lotto.domain.lotto; + +import java.util.List; + +public class LottoManualTicket extends Lotto { + private final List numbers; + + public LottoManualTicket(List numbers) { + super(numbers); + this.numbers = numbers; + } + + public List getLotto() { + return numbers; + } +} diff --git a/src/main/java/lotto/domain/lotto/LottoManualTickets.java b/src/main/java/lotto/domain/lotto/LottoManualTickets.java new file mode 100644 index 0000000..f460540 --- /dev/null +++ b/src/main/java/lotto/domain/lotto/LottoManualTickets.java @@ -0,0 +1,15 @@ +package lotto.domain.lotto; + +import java.util.List; + +public class LottoManualTickets { + private final List lottoManualTickets; + + public LottoManualTickets(List lottoManualTickets) { + this.lottoManualTickets = lottoManualTickets; + } + + public List getLottoManualTickets() { + return lottoManualTickets; + } +} diff --git a/src/main/java/lotto/domain/strategy/RandomLottoNumberGenerator.java b/src/main/java/lotto/domain/strategy/RandomLottoNumberGenerator.java new file mode 100644 index 0000000..f2c8428 --- /dev/null +++ b/src/main/java/lotto/domain/strategy/RandomLottoNumberGenerator.java @@ -0,0 +1,28 @@ +package lotto.domain.strategy; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class RandomLottoNumberGenerator { + private final static int UPPER_BOUND = 45; + private final static int LOWER_BOUND = 1; + private final static int LOTTO_NUMBER_COUNT = 6; + + private List setLottoNumberRange(){ + List lottoNumberRange = new ArrayList<>(); + for (int lottoNumber = LOWER_BOUND; lottoNumber < UPPER_BOUND; lottoNumber++) { + lottoNumberRange.add(lottoNumber); + } + return lottoNumberRange; + } + + public List getRandomLottoNumbers(){ + List lottoNumberRange = setLottoNumberRange(); + Collections.shuffle(lottoNumberRange); + List collectedNumbers = lottoNumberRange.subList(0,LOTTO_NUMBER_COUNT); + Collections.sort(collectedNumbers); + return collectedNumbers; + } + +} diff --git a/src/main/java/lotto/domain/validation/LottoTicketOfNumberValidation.java b/src/main/java/lotto/domain/validation/LottoTicketOfNumberValidation.java new file mode 100644 index 0000000..fc7c514 --- /dev/null +++ b/src/main/java/lotto/domain/validation/LottoTicketOfNumberValidation.java @@ -0,0 +1,13 @@ +package lotto.domain.validation; + +import lotto.domain.exception.NotValidLottoTicketOfNumberException; + + +public class LottoTicketOfNumberValidation { + private final static int LOTTO_TICKET_OF_NUMBER_MINIMUM_THRESHOLD = 1; + + public void checkLottoTicketOfNumber(int lottoTicketOfNumber) { + if (lottoTicketOfNumber < LOTTO_TICKET_OF_NUMBER_MINIMUM_THRESHOLD) + throw new NotValidLottoTicketOfNumberException(); + } +} diff --git a/src/main/java/lotto/domain/validation/LottoValidation.java b/src/main/java/lotto/domain/validation/LottoValidation.java new file mode 100644 index 0000000..9ff125f --- /dev/null +++ b/src/main/java/lotto/domain/validation/LottoValidation.java @@ -0,0 +1,31 @@ +package lotto.domain.validation; + +import lotto.domain.exception.NotDuplicatedNumberException; +import lotto.domain.exception.NotValidLottoLengthException; + +import java.util.ArrayList; +import java.util.List; + +public class LottoValidation { + private final static int LOTTO_NUMBER_COUNT = 6; + + public void checkLottoLength(List numbers) { + if (numbers.size() != LOTTO_NUMBER_COUNT) { + throw new NotValidLottoLengthException(); + } + } + + public void checkDuplicatedNumber(List numbers) { + List duplicatedCheckNumbers = new ArrayList<>(); + for (Integer number : numbers) { + hasDuplicatedNumber(number, duplicatedCheckNumbers); + } + } + + private void hasDuplicatedNumber(Integer number, List numbers) { + if (numbers.contains(number)) { + throw new NotDuplicatedNumberException(); + } + numbers.add(number); + } +} diff --git a/src/main/java/lotto/ui/Printer.java b/src/main/java/lotto/ui/Printer.java new file mode 100644 index 0000000..1bb3812 --- /dev/null +++ b/src/main/java/lotto/ui/Printer.java @@ -0,0 +1,116 @@ +package lotto.ui; + +import lotto.domain.NumberOfLottoTicket; +import lotto.domain.WinningStatus; +import lotto.domain.lotto.LottoAutomaticTicket; +import lotto.domain.lotto.LottoAutomaticTickets; +import lotto.domain.lotto.LottoManualTicket; +import lotto.domain.lotto.LottoManualTickets; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Map; + +public class Printer { + private static final String REQUEST_PURCHASE_AMOUNT_MESSAGE = "๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String REQUEST_LOTTO_MANUAL_TICKET_NUMBER_MESSAGE = "์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋กœ๋˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String REQUEST_LOTTO_MANUAL_TICKET_MESSAGE = "์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String PRINT_NUMBER_OF_LOTTO_TICKET_MESSAGE = "์ˆ˜๋™์œผ๋กœ %d์žฅ, ์ž๋™์œผ๋กœ %d๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค."; + private static final String REQUEST_LAST_WEEK_LOTTO_WINNING_NUMBER_MESSAGE = "\n์ง€๋‚œ ์ฃผ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String REQUEST_LOTTO_BONUS_BALL_NUMBER_MESSAGE = "๋ณด๋„ˆ์Šค ๋ณผ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"; + private static final String PRINT_FINAL_MATCHED_LOTTO_RESULT_MESSAGE = "%s๊ฐœ ์ผ์น˜%s(%s์›)- "; + private static final String PRINT_MATCH_BONUS_BALL_MESSAGE = ", ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜"; + private static final String PRINT_PROFIT_LOSS_MESSAGE = "(๊ธฐ์ค€์ด 1์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ์ ์œผ๋กœ ์†ํ•ด๋ผ๋Š” ์˜๋ฏธ์ž„)"; + private static final String PRINT_LOTTO_PROFIT_MESSAGE = "์ด ์ˆ˜์ต๋ฅ ์€ %.2f์ž…๋‹ˆ๋‹ค."; + + public void requestPurchaseAmount() { + System.out.println(REQUEST_PURCHASE_AMOUNT_MESSAGE); + } + + public void requestLottoManualTicketNumber() { + System.out.println(REQUEST_LOTTO_MANUAL_TICKET_NUMBER_MESSAGE); + } + + public void requestLottoManualTicket() { + System.out.println(REQUEST_LOTTO_MANUAL_TICKET_MESSAGE); + } + + public void printNumberOfEachLottoTicket(NumberOfLottoTicket numberOfLottoTicket) { + System.out.println(String.format( + PRINT_NUMBER_OF_LOTTO_TICKET_MESSAGE, + numberOfLottoTicket.getNumberOfManualLottoTicket(), + numberOfLottoTicket.getAutomaticLottoTicketOfNumber() + )); + } + + public void printLottoAutomaticTickets(LottoAutomaticTickets lottoAutomaticTickets) { + for (LottoAutomaticTicket lottoAutomaticTicket : lottoAutomaticTickets.getLottoAutomaticTickets()) { + System.out.println(lottoAutomaticTicket.getLotto()); + } + } + + public void printLottoManualTickets(LottoManualTickets lottoManualTickets) { + for (LottoManualTicket lottoManualTicket : lottoManualTickets.getLottoManualTickets()) { + System.out.println(lottoManualTicket.getLotto()); + } + } + + public void printAllMatchedLottoResults(Map lottoPrices) { + List keySet = new ArrayList(lottoPrices.keySet()); + keySet.sort(Comparator.comparingInt(WinningStatus::getWinningMoney)); + + for (WinningStatus winningStatus : WinningStatus.values()) { + printMatchedLottoWithBonusBallFormat(winningStatus); + printMatchedLottoFormat(winningStatus); + printMatchedLottoResult(winningStatus, lottoPrices); + } + } + + public void requestLastWeekLottoWinningNumber() { + System.out.println(REQUEST_LAST_WEEK_LOTTO_WINNING_NUMBER_MESSAGE); + } + + public void requestLottoBonusBallNumber() { + System.out.println(REQUEST_LOTTO_BONUS_BALL_NUMBER_MESSAGE); + } + + private void printMatchedLottoResult(WinningStatus winningStatus, Map lottoPrices) { + if (lottoPrices.get(winningStatus) == null) { + System.out.println("0๊ฐœ"); + } + if (lottoPrices.get(winningStatus) != null) { + System.out.println(lottoPrices.get(winningStatus) + "๊ฐœ"); + } + } + + private void printMatchedLottoFormat(WinningStatus winningStatus) { + if (!winningStatus.hasBonusBall()) { + System.out.print(String.format( + PRINT_FINAL_MATCHED_LOTTO_RESULT_MESSAGE, + winningStatus.getMatchCount(), + "", + winningStatus.getWinningMoney())); + } + } + + private void printMatchedLottoWithBonusBallFormat(WinningStatus winningStatus) { + if (winningStatus.hasBonusBall()) { + System.out.print(String.format( + PRINT_FINAL_MATCHED_LOTTO_RESULT_MESSAGE, + winningStatus.getMatchCount(), + PRINT_MATCH_BONUS_BALL_MESSAGE, + winningStatus.getWinningMoney())); + } + } + + public void printLottoProfit(float calculatedProfit) { + System.out.print(String.format(PRINT_LOTTO_PROFIT_MESSAGE, calculatedProfit)); + } + + public void printIsLottoProfit(boolean isProfit) { + if (!isProfit) { + System.out.println(PRINT_PROFIT_LOSS_MESSAGE); + } + } +} diff --git a/src/main/java/lotto/ui/Receiver.java b/src/main/java/lotto/ui/Receiver.java new file mode 100644 index 0000000..e525362 --- /dev/null +++ b/src/main/java/lotto/ui/Receiver.java @@ -0,0 +1,46 @@ +package lotto.ui; + +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; + +public class Receiver { + private static final Scanner scanner = new Scanner(System.in); + private static final String DELIMITER = ","; + + public int receiveLottoPurchaseAmount() { + return scanner.nextInt(); + } + + public int receiverLottoManualTicketNumber() { + return scanner.nextInt(); + } + + public List receiverLottoManualTicket() { + String lottoTicket = scanner.next(); + List lottoManualTicket = splitInputDelimiter(lottoTicket); + return convertStringToInt(lottoManualTicket); + } + + public List receiveLastWeekLottoWinningNumbers() { + String lastWeekLottoWinningNumbers = scanner.next(); + List dividedLastWeekLottoWinningNumbers = splitInputDelimiter(lastWeekLottoWinningNumbers); + return convertStringToInt(dividedLastWeekLottoWinningNumbers); + } + + private List convertStringToInt(List lottoManualTicket) { + return lottoManualTicket.stream() + .map(Integer::parseInt) + .collect(Collectors.toList()); + } + + private List splitInputDelimiter(String lottoManualTicket) { + return Arrays.asList(lottoManualTicket.split(DELIMITER)); + } + + public int receiveLottoBonusBallNumber() { + return scanner.nextInt(); + } + +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java new file mode 100644 index 0000000..2892777 --- /dev/null +++ b/src/test/java/lotto/LottoTest.java @@ -0,0 +1,69 @@ +package lotto; + +import lotto.domain.exception.NotDuplicatedNumberException; +import lotto.domain.exception.NotValidLottoLengthException; +import lotto.domain.lotto.LottoAutomaticTicket; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +class LottoTest { + + private LottoAutomaticTicket lottoAutomaticTicket; + + @BeforeEach + void setup() { + List numbers = new ArrayList<>(Arrays.asList(6,7,8,9,10,11)); + lottoAutomaticTicket = new LottoAutomaticTicket(numbers); + } + + @Test + void ๋กœ๋˜_๊ฐ์ฒด๋ฅผ_์ƒ์„ฑํ•œ๋‹ค(){ + //given + List actual = new ArrayList<>(Arrays.asList(6,7,8,9,10,11)); + + + //when + List expected = lottoAutomaticTicket.getLotto(); + System.out.println(expected); + //then + assertThat(expected).isEqualTo(actual); + } + + @Test + void ๋กœ๋˜_์ˆซ์ž๊ฐ€_6๊ฐœ๋ฅผ_์ดˆ๊ณผํ• _๊ฒฝ์šฐ_NotValidLottoLengthException์„_๋˜์ง„๋‹ค(){ + //given + List numbers = new ArrayList<>(Arrays.asList(6,7,8,9,10,11,12)); + + //when + //then + assertThatExceptionOfType(NotValidLottoLengthException.class). + isThrownBy(() -> new LottoAutomaticTicket(numbers)); + } + @Test + void ๋กœ๋˜_์ˆซ์ž๊ฐ€_6๊ฐœ_๋ฏธ๋งŒ์ผ_๊ฒฝ์šฐ_NotValidLottoLengthException์„_๋˜์ง„๋‹ค(){ + //given + List numbers = new ArrayList<>(Arrays.asList(6,7,8,9,10)); + + //when + //then + assertThatExceptionOfType(NotValidLottoLengthException.class). + isThrownBy(() -> new LottoAutomaticTicket(numbers)); + } + + @Test + void ๋กœ๋˜_์ˆซ์ž๊ฐ€_6๊ฐœ์ผ๋•Œ_๊ฒน์น˜๋Š”_์ˆซ์ž๊ฐ€_์žˆ์„_๊ฒฝ์šฐ_NotDuplicatedNumberException_์„_๋˜์ง„๋‹ค() { + //given + List numbers = new ArrayList<>(Arrays.asList(6,6,7,8,9,10)); + + //when + //then + assertThatExceptionOfType(NotDuplicatedNumberException.class). + isThrownBy(() -> new LottoAutomaticTicket(numbers)); + } +} \ No newline at end of file diff --git a/src/test/java/lotto/LottoTicketOfNumberTest.java b/src/test/java/lotto/LottoTicketOfNumberTest.java new file mode 100644 index 0000000..307f7af --- /dev/null +++ b/src/test/java/lotto/LottoTicketOfNumberTest.java @@ -0,0 +1,57 @@ +package lotto; + +import lotto.domain.NumberOfLottoTicket; +import lotto.domain.PurchaseAmount; +import lotto.domain.exception.NotValidLottoTicketOfNumberException; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +class LottoTicketOfNumberTest { + @Test + void ๋กœ๋˜_ํ‹ฐ์ผ“์˜_์ด_์ˆ˜๋ฅผ_๊ด€๋ฆฌํ•˜๋Š”_๊ฐ์ฒด๋ฅผ_์ƒ์„ฑํ•œ๋‹ค() { + //given + int totalPrice = 11000; + PurchaseAmount purchaseAmount = new PurchaseAmount(totalPrice); + int manualLottoTicketOfNumber = 3; + int actual = 11; + NumberOfLottoTicket lottoTicketOfNumber = new NumberOfLottoTicket(purchaseAmount, manualLottoTicketOfNumber); + + //when + int expected = lottoTicketOfNumber.getTotalNumberOfLottoTicket(); + System.out.println(expected); + + //then + assertThat(expected).isEqualTo(actual); + } + + @Test + void ์ž๋™_๋กœ๋˜_ํ‹ฐ์ผ“์˜_์ˆ˜๋ฅผ_๊ฐ€์ ธ์˜จ๋‹ค() { + //given + int totalPrice = 11000; + PurchaseAmount purchaseAmount = new PurchaseAmount(totalPrice); + int manualLottoTicketOfNumber = 3; + int actual = 8; + NumberOfLottoTicket lottoTicketOfNumber = new NumberOfLottoTicket(purchaseAmount, manualLottoTicketOfNumber); + + //when + int expected = lottoTicketOfNumber.getAutomaticLottoTicketOfNumber(); + System.out.println(expected); + + //then + assertThat(expected).isEqualTo(actual); + } + + @Test + void ์ž…๋ ฅํ•œ_๋กœ๋˜_๊ตฌ์ž…_๊ธˆ์•ก์œผ๋กœ_๋กœ๋˜๋ฅผ_์‚ด_์ˆ˜_์—†๋Š”_๊ฒฝ์šฐ_NotValidLottoTicketOfNumberException_์„_๋˜์ง„๋‹ค() { + //given + int totalPrice = 900; + PurchaseAmount purchaseAmount = new PurchaseAmount(totalPrice); + int manualLottoTicketOfNumber = 3; + //when + //then + assertThatExceptionOfType(NotValidLottoTicketOfNumberException.class). + isThrownBy(() -> new NumberOfLottoTicket(purchaseAmount, manualLottoTicketOfNumber)); + } +} \ No newline at end of file diff --git a/src/test/java/lotto/PurchaseAmountTest.java b/src/test/java/lotto/PurchaseAmountTest.java new file mode 100644 index 0000000..7319fe7 --- /dev/null +++ b/src/test/java/lotto/PurchaseAmountTest.java @@ -0,0 +1,34 @@ +package lotto; + +import lotto.domain.PurchaseAmount; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +class PurchaseAmountTest { + + @Test + void ๋กœ๋˜_๊ตฌ์ž…๊ธˆ์•ก_๊ฐ์ฒด๋ฅผ_์ƒ์„ฑํ•œ๋‹ค(){ + //given + int purchaseAmountNumber = 1000; + int actual = 1000; + PurchaseAmount purchaseAmount = new PurchaseAmount(purchaseAmountNumber); + + //when + int expected = purchaseAmount.getPurchaseAmount(); + System.out.println(expected); + + //then + assertThat(expected).isEqualTo(actual); + } + + @ParameterizedTest(name = "{0}์ด ๋“ค์–ด์˜ฌ๋•Œ ์˜ˆ์™ธ ๋ฐœ์ƒ") + @ValueSource(ints = {-1000, 0}) + void ๋กœ๋˜_๊ตฌ์ž…_๊ธˆ์•ก์ด_0์ดํ•˜์ผ_๊ฒฝ์šฐ_IllegalArgumentException_์„_๋˜์ง„๋‹ค(int value) { + Assertions.assertThatIllegalArgumentException() + .isThrownBy(() -> new PurchaseAmount(value)); + } +} \ No newline at end of file diff --git a/src/test/java/lotto/domain/ProfitTest.java b/src/test/java/lotto/domain/ProfitTest.java new file mode 100644 index 0000000..35ba6c2 --- /dev/null +++ b/src/test/java/lotto/domain/ProfitTest.java @@ -0,0 +1,74 @@ +package lotto.domain; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class ProfitTest { + @Test + void Profit_๊ฐ์ฒด๋ฅผ_์ƒ์„ฑํ•˜๊ณ _์ˆ˜์ต๋ฅ ์ด_1๋ณด๋‹ค_ํด๋•Œ_true_๋ฅผ_๋ฐ˜ํ™˜ํ•œ๋‹ค() { + //given + int purchaseManualLottoOfNumber = 3; + PurchaseAmount purchaseAmount = new PurchaseAmount(4000); + NumberOfLottoTicket numberOfLottoTicket = new NumberOfLottoTicket(purchaseAmount, purchaseManualLottoOfNumber); + List lottoPrices = new ArrayList<>(); + for (WinningStatus winningStatus: WinningStatus.values()) { + if (winningStatus.getWinningMoney() == 5000) { + lottoPrices.add(winningStatus); + } + } + Profit profit = new Profit(lottoPrices, numberOfLottoTicket); + + //when + Boolean isProfit = profit.isProfit(); + + //then + assertThat(true).isEqualTo(isProfit); + } + + @Test + void Profit_๊ฐ์ฒด๋ฅผ_์ƒ์„ฑํ•˜๊ณ _์ˆ˜์ต๋ฅ ์ด_1๋ณด๋‹ค_์ž‘์„_๋•Œ_false_๋ฅผ_๋ฐ˜ํ™˜ํ•œ๋‹ค() { + //given + int purchaseManualLottoOfNumber = 3; + PurchaseAmount purchaseAmount = new PurchaseAmount(6000); + NumberOfLottoTicket numberOfLottoTicket = new NumberOfLottoTicket(purchaseAmount, purchaseManualLottoOfNumber); + List lottoPrices = new ArrayList<>(); + for (WinningStatus winningStatus: WinningStatus.values()) { + if (winningStatus.getWinningMoney() == 5000) { + lottoPrices.add(winningStatus); + } + } + Profit profit = new Profit(lottoPrices, numberOfLottoTicket); + + //when + Boolean isProfit = profit.isProfit(); + + //then + assertThat(false).isEqualTo(isProfit); + } + + @Test + void Profit_๊ฐ์ฒด๋ฅผ_์ƒ์„ฑํ•˜๊ณ _์ˆ˜์ต๋ฅ _๋ฐ˜ํ™˜ํ•œ๋‹ค() { + //given + int purchaseManualLottoOfNumber = 3; + PurchaseAmount purchaseAmount = new PurchaseAmount(4000); + NumberOfLottoTicket numberOfLottoTicket = new NumberOfLottoTicket(purchaseAmount, purchaseManualLottoOfNumber); + List lottoPrices = new ArrayList<>(); + for (WinningStatus winningStatus: WinningStatus.values()) { + if (winningStatus.getWinningMoney() == 5000) { + lottoPrices.add(winningStatus); + } + } + Profit profit = new Profit(lottoPrices, numberOfLottoTicket); + + //when + float calculatedProfit = profit.getProfit(); + + //then + assertThat(1.25).isEqualTo(calculatedProfit); + } +} \ No newline at end of file