diff --git a/docs/README.md b/docs/README.md index e69de29bb2d..8ccc6243617 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,67 @@ +## ๐Ÿ’ฒ๋กœ๋˜ ๊ฒŒ์ž„ + +--- + +### **๐Ÿš€ User Flow** + +--- + +1. ๋กœ๋˜ ํ‹ฐ์ผ“ ๊ตฌ๋งค์— ์‚ฌ์šฉํ•  ๊ธˆ์•ก ์ž…๋ ฅ +2. ๊ตฌ๋งค ๊ธˆ์•ก์— ๋งž๋Š” ๊ฐœ์ˆ˜ ๋งŒํผ ๋กœ๋˜ ํ‹ฐ์ผ“์„ ์ƒ์„ฑ +3. ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ +4. ๋กœ๋˜ ํ‹ฐ์ผ“๊ณผ ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐ +5. ๊ฐ ๋“ฑ์ˆ˜๋ณ„ ์ผ์น˜ ๋ฒˆํ˜ธ ์ˆ˜ ๋ฐ ์ˆ˜์ต์„ ํฌํ•จํ•œ ๋‹น์ฒจ ๋‚ด์—ญ ๊ฒฐ๊ณผ ์ถœ๋ ฅ +6. ์ด ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ + +### **๐Ÿš€ ๊ธฐ๋Šฅ ๊ตฌํ˜„** + +--- + +### **์ž…๋ ฅ ๊ธฐ๋Šฅ** +- ๋กœ๋˜ ํ‹ฐ์ผ“ ๊ตฌ๋งค ๊ธˆ์•ก์„ ์ž…๋ ฅ +- ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ + +### **์ถœ๋ ฅ ๊ธฐ๋Šฅ** +- ๊ตฌ๋งคํ•œ ๋กœ๋˜ ํ‹ฐ์ผ“ ์ˆ˜์™€ ๊ฐ ํ‹ฐ์ผ“์˜ ๋ฒˆํ˜ธ๋ฅผ ํ‘œ์‹œ + - ํ•œ์žฅ๋‹น 1๋ถ€ํ„ฐ 45๊นŒ์ง€ ์„œ๋กœ ๋‹ค๋ฅธ 6๊ฐœ์˜ ์ž„์˜์˜ ์ˆ˜๋ฅผ ์ƒ์„ฑ +- ๊ฐ ๋“ฑ์ˆ˜๋ณ„ ์ผ์น˜ํ•˜๋Š” ๋ฒˆํ˜ธ์˜ ์ˆ˜์™€ ์ˆ˜์ต์„ ๋ณด์—ฌ์ฃผ๋Š” ๋‹น์ฒจ ๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œ +- ํˆฌ์ž ์ˆ˜์ต๋ฅ ์„ ๋ฐฑ๋ถ„์œจ๋กœ ๊ณ„์‚ฐํ•˜๊ณ  ํ‘œ์‹œ + +### **์„œ๋น„์Šค ๊ธฐ๋Šฅ** +- ๊ตฌ๋งค ๊ธˆ์•ก์— ๋”ฐ๋ผ ๊ฐœ์ˆ˜ ๋งŒํผ ๋กœ๋˜ ํ‹ฐ์ผ“์„ ์ƒ์„ฑ +- ์‚ฌ์šฉ์ž์˜ ํ‹ฐ์ผ“๊ณผ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹น์ฒจ ํ™•์ธ +- ์ผ์น˜ํ•˜๋Š” ๋ฒˆํ˜ธ์— ๋”ฐ๋ผ ์ˆ˜์ต๊ณผ ์ˆ˜์ต๋ฅ ์„ ๊ณ„์‚ฐ + +### **์˜ˆ์™ธ ์ฒ˜๋ฆฌ (์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’ ๊ฒ€์ฆ)** +- ํ‹ฐ์ผ“ ๊ตฌ๋งค + - ์ž…๋ ฅ ๋ฐ›์€ ํ‹ฐ์ผ“ ๊ตฌ๋งค ๊ธˆ์•ก์ด 1000์› ๋‹จ์œ„๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ +- ๋‹น์ฒจ ๋ฒˆํ˜ธ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ๊ฐ€ 6๊ฐœ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ + - ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๊ฐ€ 1๊ฐœ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ์— ์ค‘๋ณต๊ฐ’ ์กด์žฌ + - ๋‹น์ฒจ ๋ฒˆํ˜ธ๊ฐ€ 1~45 ์‚ฌ์ด์˜ ๊ฐ’์ด ์•„๋‹Œ ๊ฒฝ์šฐ + - ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๊ฐ€ 1~45 ์‚ฌ์ด์˜ ๊ฐ’์ด ์•„๋‹Œ ๊ฒฝ์šฐ + +### **์ถ”๊ฐ€ ์š”๊ตฌ ์‚ฌํ•ญ** +- ๋ฉ”์†Œ๋“œ๋Š” 15๋ผ์ธ์„ ๋„˜์ง€ ์•Š์•„์•ผ +- else์™€ switch/case ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ +- ๋กœ๋˜ ๋“ฑ์ˆ˜์™€ ๊ฒฐ๊ณผ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด Java Enum์„ ์ ์šฉ +- ๋„๋ฉ”์ธ ๋กœ์ง์— ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ + - ๋‹จ, UI(System.out, System.in, Scanner) ๋กœ์ง์€ ์ œ์™ธ + - ํ•ต์‹ฌ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์™€ UI๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•ด ๊ตฌํ˜„ + +### **ํด๋ž˜์Šค ์ •๋ฆฌ** + +controller +- LottoController + domain + - Lotto + - LottoNumbers + - LottoAmount + - Ranking + - Results + view + - InputView + - OutputView + +Application(์ปจํŠธ๋กค๋Ÿฌ ์‹คํ–‰) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba44..eed486edc16 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -2,6 +2,7 @@ public class Application { public static void main(String[] args) { - // TODO: ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ + LottoController controller = new LottoController(); + controller.play(); } } diff --git a/src/main/java/lotto/InputView.java b/src/main/java/lotto/InputView.java new file mode 100644 index 00000000000..5cc8e9df671 --- /dev/null +++ b/src/main/java/lotto/InputView.java @@ -0,0 +1,101 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class InputView { + private static final String PURCHASE_AMOUNT_MESSAGE = "๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String WINNING_NUMBERS_MESSAGE = "\n๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final String BONUS_NUMBER_MESSAGE = "\n๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + + public static int inputPurchaseAmount() { + while (true) { + System.out.println(PURCHASE_AMOUNT_MESSAGE); + String input = Console.readLine(); + try { + int amount = Integer.parseInt(input); + validateAmount(amount); + return amount; + } catch (NumberFormatException e) { + System.out.println("[ERROR] ๊ตฌ์ž… ๊ธˆ์•ก์€ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + // continue the loop for another attempt + } + } + } + + public static List inputWinningNumbers() { + while (true) { + System.out.println(WINNING_NUMBERS_MESSAGE); + String input = Console.readLine(); + try { + List winningNumbers = parseToIntList(input); + validateWinningNumbers(winningNumbers); + return winningNumbers; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + // continue the loop for another attempt + } + } + } + + public static int inputBonusNumber() { + while (true) { + System.out.println(BONUS_NUMBER_MESSAGE); + String input = Console.readLine(); + try { + int bonusNumber = parseToInt(input); + validateBonusNumber(bonusNumber); + return bonusNumber; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + // continue the loop for another attempt + } + } + } + + private static int parseToInt(String input) { + try { + return Integer.parseInt(input.trim()); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] ์ˆซ์ž๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + private static List parseToIntList(String input) { + try { + return Arrays.stream(input.split(",")) + .map(String::trim) + .map(Integer::parseInt) + .collect(Collectors.toList()); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."); + } + } + + private static void validateAmount(int amount) { + if (amount <= 0) { + throw new IllegalArgumentException("[ERROR] ๊ตฌ์ž… ๊ธˆ์•ก์€ 0์› ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + if (amount % 1000 != 0) { + throw new IllegalArgumentException("[ERROR] ๊ตฌ์ž… ๊ธˆ์•ก์€ 1,000์› ๋‹จ์œ„์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + private static void validateWinningNumbers(List numbers) { + if (numbers.size() != 6 || numbers.stream().anyMatch(n -> n < 1 || n > 45)) { + throw new IllegalArgumentException("[ERROR] ๋‹น์ฒจ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ์˜ 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + if (numbers.stream().distinct().count() != 6) { + throw new IllegalArgumentException("[ERROR] ๋‹น์ฒจ ๋ฒˆํ˜ธ๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + private static void validateBonusNumber(int bonusNumber) { + if (bonusNumber < 1 || bonusNumber > 45) { + throw new IllegalArgumentException("[ERROR] ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d1f73..49e73f7dc80 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,20 +1,50 @@ package lotto; import java.util.List; +import java.util.HashSet; +import java.util.stream.Collectors; public class Lotto { private final List numbers; public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; + validateTotalCount(numbers); + validateNumbersRange(numbers); + validateUniqueNumbers(numbers); + this.numbers = sortNumbers(numbers); } - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException(); + public List getNumbers() { + return numbers; + } + + private void validateTotalCount(List numbers) { + if (numbers == null || numbers.size() != 6) { + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” ์ •ํ™•ํžˆ 6๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + private void validateNumbersRange(List numbers) { + if (numbers.stream().anyMatch(number -> number < 1 || number > 45)) { + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค."); } } - // TODO: ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ + private void validateUniqueNumbers(List numbers) { + if (new HashSet<>(numbers).size() != 6) { + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + private List sortNumbers(List numbers) { + return numbers.stream() + .sorted() + .collect(Collectors.toList()); + } + + public int countMatch(List winningNumbers) { + return (int) numbers.stream() + .filter(winningNumbers::contains) + .count(); + } } diff --git a/src/main/java/lotto/LottoAmount.java b/src/main/java/lotto/LottoAmount.java new file mode 100644 index 00000000000..0f54ba2e641 --- /dev/null +++ b/src/main/java/lotto/LottoAmount.java @@ -0,0 +1,28 @@ +package lotto; + +public class LottoAmount { + public static final int LOTTO_PRICE = 1000; + private final int amount; + + public LottoAmount(int amount) { + validatePrice(amount); + validateDivisible(amount); + this.amount = amount; + } + + private static void validatePrice(int amount) { + if (amount < LOTTO_PRICE) { + throw new IllegalArgumentException("[ERROR] ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์› ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + private static void validateDivisible(int amount) { + if (amount % LOTTO_PRICE != 0) { + throw new IllegalArgumentException("[ERROR] ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์› ๋‹จ์œ„์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + + public int calculateAmount() { + return amount / LOTTO_PRICE; + } +} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java new file mode 100644 index 00000000000..db6afa81726 --- /dev/null +++ b/src/main/java/lotto/LottoController.java @@ -0,0 +1,28 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; + +public class LottoController { + + public void play() { + int purchaseAmount = InputView.inputPurchaseAmount(); + LottoAmount lottoAmount = new LottoAmount(purchaseAmount); + int lottoCount = lottoAmount.calculateAmount(); + + List lottos = new ArrayList<>(); + for (int i = 0; i < lottoCount; i++) { + List numbers = LottoNumbers.setRandomNumbers(); + lottos.add(new Lotto(numbers)); + } + OutputView.printLottos(lottos); + + List winningNumbers = InputView.inputWinningNumbers(); + int bonusNumber = InputView.inputBonusNumber(); + Lotto winningLotto = new Lotto(winningNumbers); + + Results results = new Results(lottos, winningLotto, bonusNumber, purchaseAmount); + + OutputView.printResults(results); + } +} diff --git a/src/main/java/lotto/LottoNumbers.java b/src/main/java/lotto/LottoNumbers.java new file mode 100644 index 00000000000..2dee6b18230 --- /dev/null +++ b/src/main/java/lotto/LottoNumbers.java @@ -0,0 +1,22 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class LottoNumbers { + + private static final int COUNT_LOTTO_NUMBERS = 6; + private static final int MIN_LOTTO_NUMBER = 1; + private static final int MAX_LOTTO_NUMBER = 45; + + private static List lottoNumberList; + + public static List setRandomNumbers() { + lottoNumberList = Randoms.pickUniqueNumbersInRange(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER, COUNT_LOTTO_NUMBERS); + List lottoTicketNumberList = new ArrayList<>(lottoNumberList); + Collections.sort(lottoTicketNumberList); + return lottoTicketNumberList; + } +} diff --git a/src/main/java/lotto/OutputView.java b/src/main/java/lotto/OutputView.java new file mode 100644 index 00000000000..da887dc93a9 --- /dev/null +++ b/src/main/java/lotto/OutputView.java @@ -0,0 +1,33 @@ +package lotto; + +import java.util.List; +import java.util.stream.Collectors; +public class OutputView { + + public static void printResults(Results lottoResult) { + System.out.println("\n๋‹น์ฒจ ํ†ต๊ณ„"); + System.out.println("----"); + + for (Ranking ranking : Ranking.values()) { + if (ranking != Ranking.NONE) { + int count = lottoResult.getCount(ranking); + System.out.println(ranking.getMessage() + " - " + count + "๊ฐœ"); + } + } + + System.out.printf("์ด ์ˆ˜์ต๋ฅ ์€ %.1f%%์ž…๋‹ˆ๋‹ค.", lottoResult.calculateEarningsRate()); + } + + public static void printLottos(List lottos) { + System.out.println(lottos.size() + "๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค."); + for (Lotto lotto : lottos) { + System.out.println(formatLottoNumbers(lotto.getNumbers())); + } + } + + private static String formatLottoNumbers(List numbers) { + return "[" + numbers.stream() + .map(String::valueOf) + .collect(Collectors.joining(", ")) + "]"; + } +} diff --git a/src/main/java/lotto/Ranking.java b/src/main/java/lotto/Ranking.java new file mode 100644 index 00000000000..f4e0c25803b --- /dev/null +++ b/src/main/java/lotto/Ranking.java @@ -0,0 +1,49 @@ +package lotto; + +public enum Ranking { + FIRST(3, 5_000, "3๊ฐœ ์ผ์น˜ (5,000์›)"), + SECOND(4, 50_000, "4๊ฐœ ์ผ์น˜ (50,000์›)"), + THIRD(5, 1_500_000, "5๊ฐœ ์ผ์น˜ (1,500,000์›)"), + FOURTH(5, 30_000_000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ (30,000,000์›)"), + FIFTH(6, 2_000_000_000, "6๊ฐœ ์ผ์น˜ (2,000,000,000์›)"), + NONE(0, 0, ""); + + private final int matchCount; + private final int prizeMoney; + private final String Message; + + Ranking(int matchCount, int prizeMoney, String Message) { + this.matchCount = matchCount; + this.prizeMoney = prizeMoney; + this.Message = Message; + } + + public int getMatchCount() { + return matchCount; + } + + public int getPrizeMoney() { + return prizeMoney; + } + + public String getMessage() { + return Message; + } + + public static Ranking getRank(int matchCount, boolean matchBonus) { + if (matchCount == 6) { + return FIRST; + } else if (matchCount == 5 && matchBonus) { + return SECOND; + } else if (matchCount == 5) { + return THIRD; + } else { + for (Ranking ranking : Ranking.values()) { + if (ranking.getMatchCount() == matchCount && ranking != SECOND && ranking != THIRD) { + return ranking; + } + } + } + return NONE; + } +} diff --git a/src/main/java/lotto/Results.java b/src/main/java/lotto/Results.java new file mode 100644 index 00000000000..8f9db32a6d9 --- /dev/null +++ b/src/main/java/lotto/Results.java @@ -0,0 +1,42 @@ +package lotto; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Results { + private final Map results; + private final int totalAmountSpent; + + public Results(List lottoTickets, Lotto winningLotto, int bonusNumber, int totalAmountSpent) { + this.totalAmountSpent = totalAmountSpent; + results = new HashMap<>(); + + for (Lotto lotto : lottoTickets) { + int matchCount = lotto.countMatch(winningLotto.getNumbers()); + boolean matchBonus = lotto.getNumbers().contains(bonusNumber); + Ranking ranking = Ranking.getRank(matchCount, matchBonus); + results.put(ranking, results.getOrDefault(ranking, 0) + 1); + } + } + + public int getCount(Ranking ranking) { + return results.getOrDefault(ranking, 0); + } + + public long getTotalPrize() { + return results.entrySet().stream() + .mapToLong(e -> (long) e.getKey().getPrizeMoney() * e.getValue()) + .sum(); + } + + public double calculateEarningsRate() { + long totalPrize = getTotalPrize(); + double earningsRate = (double) totalPrize / totalAmountSpent * 100; + return Math.round(earningsRate * 100) / 100.0; + } + + public int getTotalAmountSpent() { + return totalAmountSpent; + } +} diff --git a/src/test/java/lotto/LottoAmountTest.java b/src/test/java/lotto/LottoAmountTest.java new file mode 100644 index 00000000000..ebf07586128 --- /dev/null +++ b/src/test/java/lotto/LottoAmountTest.java @@ -0,0 +1,34 @@ +package lotto; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; + +class LottoAmountTest { + + @DisplayName("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ธˆ์•ก (1000์› ๋ฏธ๋งŒ) ์ž…๋ ฅ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ") + @Test + void invalidAmountLessThanLottoPrice() { + int invalidAmount = 900; + assertThatThrownBy(() -> new LottoAmount(invalidAmount)) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("๊ธˆ์•ก์ด 1000์› ๋‹จ์œ„๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ") + @Test + void invalidAmountNotMultipleOfLottoPrice() { + int invalidAmount = 1500; + assertThatThrownBy(() -> new LottoAmount(invalidAmount)) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("์˜ฌ๋ฐ”๋ฅธ ๊ธˆ์•ก์œผ๋กœ ๋กœ๋˜ ๊ตฌ๋งค ๊ฐ€๋Šฅ ์žฅ์ˆ˜ ๊ณ„์‚ฐ") + @Test + void calculateNumberOfLottos() { + int amount = 5000; + LottoAmount lottoAmount = new LottoAmount(amount); + assertThat(lottoAmount.calculateAmount()).isEqualTo(5); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 9f5dfe7eb83..2e3cef07772 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -6,6 +6,7 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; class LottoTest { @DisplayName("๋กœ๋˜ ๋ฒˆํ˜ธ์˜ ๊ฐœ์ˆ˜๊ฐ€ 6๊ฐœ๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") @@ -23,5 +24,17 @@ void createLottoByDuplicatedNumber() { .isInstanceOf(IllegalArgumentException.class); } - // ์•„๋ž˜์— ์ถ”๊ฐ€ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ ๊ฐ€๋Šฅ -} \ No newline at end of file + @DisplayName("๋กœ๋˜ ๋ฒˆํ˜ธ ๋ฒ”์œ„๊ฐ€ 1-45๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void createLottoWithInvalidNumbers() { + assertThatThrownBy(() -> new Lotto(List.of(0, 2, 3, 4, 5, 6))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("๋กœ๋˜ ๋ฒˆํ˜ธ๊ฐ€ 6๊ฐœ ๋ฏธ๋งŒ์ด๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.") + @Test + void createLottoByUnderSize() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5))) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/lotto/ResultsTest.java b/src/test/java/lotto/ResultsTest.java new file mode 100644 index 00000000000..cfb8c2fbcbc --- /dev/null +++ b/src/test/java/lotto/ResultsTest.java @@ -0,0 +1,30 @@ +package lotto; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class ResultsTest { + + @DisplayName("๋กœ๋˜ ๋‹น์ฒจ ๊ฒฐ๊ณผ๋ฅผ ์ •ํ™•ํžˆ ๊ณ„์‚ฐ") + @Test + void calculateResults() { + List purchasedLottos = Arrays.asList( + new Lotto(Arrays.asList(1, 2, 3, 4, 5, 6)), + new Lotto(Arrays.asList(7, 8, 9, 10, 11, 12)) + ); + Lotto winningLotto = new Lotto(Arrays.asList(1, 2, 3, 4, 5, 6)); + int bonusNumber = 45; + int totalAmountSpent = 2000; + + Results results = new Results(purchasedLottos, winningLotto, bonusNumber, totalAmountSpent); + + assertThat(results.getCount(Ranking.FIRST)).isEqualTo(1); + assertThat(results.getTotalPrize()).isGreaterThan(0); + assertThat(results.calculateEarningsRate()).isGreaterThan(0); + } +}