diff --git a/docs/README.md b/docs/README.md index e69de29bb2d..d8e552eecdf 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,30 @@ +Application과 동일 선상에 RunLotto 클래스 생성 + +LottoManager에 필요한 기능들을 패키지로 묶어보자 + +- 도메인 모델 패키지 (domain) + - 사용자로부터 입력받은 구입금액을 저장하는 클래스 PurchaseAmount + - 난수 생성된 로또 번호가 있는 클래스 Lotto + - 사용자로부터 로또 번호 6개와 보너스 번호 1개를 입력받아 저장하는 클래스 UserInputNumbers + - 상금과 당첨 수를 저장하는 열거형 enum WinningCheck +- 비즈니스 로직을 수행하는 서비스 패키지 (service) + - 당첨 결과를 계산하는 클래스 LottoResultCalculator + - 수익률을 계산하는 클래스 LottoProfitRateCalculator + - 구입금액만큼 로또를 발행하는 클래스 LottoGenerator +- 각종 메서드가 있는 패키지 (util) + - 사용자로부터 값들을 입력받는 클래스 InputManager + - 결과들을 출력하는 클래스 OutputManager + +동작 구조 + +1. receivePurchaseAmount()로 구입금액을 입력받아 PurchaseAmount에 객체로 저장(유효성 검사) +2. 생성된 PurchaseAmount를 받아 generateLottos(PurchaseAmount purchaseAmount) + 입력받은 양 만큼 Lotto 객체로 로또를 발행(유효성 검사) -> 리턴 값은 List로 generatedLottos를 리턴 +3. generatedLottos를 매개변수로 받아 printGeneratedLottoNumbers(List lottos) 생성된 로또들을 출력 +4. calculateWinningResult(List lottos, UserInputNumbers receivedLotto) + Map을 이용해서 result를 생성 WinningCheck에 열거형으로 상금과 당첨 수를 저장 + Lotto클래스에서 각 로또 당 matchCount와 bonusMatch를 가져온다 -> WinningCheck.getPrize(matchCount, bonusMatch) + -> 각 로또 당 상금과 수를 result에 저장 후 리턴 +5. calculateProfitRate(PurchaseAmount purchaseAmount, Map result) + 구입금액과 결과를 비교하여 수익률을 계산(소수 둘째에서 반올림) -> 리턴 값은 double profitRate +6. printResults(Map result, double profitRate) 최종 결과 출력 diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba44..bea36a2b8ae 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,9 @@ package lotto; public class Application { - public static void main(String[] args) { - // TODO: 프로그램 구현 - } + + public static void main(String[] args) { + LottoManager lottoManager = new LottoManager(); + lottoManager.runLotto(); + } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 519793d1f73..00000000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,20 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException(); - } - } - - // TODO: 추가 기능 구현 -} diff --git a/src/main/java/lotto/LottoManager.java b/src/main/java/lotto/LottoManager.java new file mode 100644 index 00000000000..1ede7fe36bc --- /dev/null +++ b/src/main/java/lotto/LottoManager.java @@ -0,0 +1,38 @@ +package lotto; + +import java.util.List; +import java.util.Map; +import lotto.domain.Lotto; +import lotto.domain.PurchaseAmount; +import lotto.domain.UserInputNumbers; +import lotto.service.LottoProfitRateCalculator; +import lotto.service.LottoResultCalculator; +import lotto.util.InputManager; +import lotto.service.LottoGenerator; +import lotto.util.OutputManager; +import lotto.domain.WinningCheck; + +public class LottoManager { + + private final InputManager inputManager = new InputManager(); + private final LottoGenerator lottoGenerator = new LottoGenerator(); + private final LottoResultCalculator lottoResultCalculator = new LottoResultCalculator(); + private final LottoProfitRateCalculator lottoProfitRateCalculator = new LottoProfitRateCalculator(); + private final OutputManager outputManager = new OutputManager(); + + public void runLotto() { + PurchaseAmount purchaseAmount = inputManager.receivePurchaseAmount(); + + List generatedLottos = lottoGenerator.generateLottos(purchaseAmount); + outputManager.printGeneratedLottoNumbers(generatedLottos); + + UserInputNumbers userInputNumbers = inputManager.receiveLottoNumber(); + + Map result = lottoResultCalculator + .calculateWinningResult(generatedLottos, userInputNumbers); + double profitRate = lottoProfitRateCalculator + .calculateProfitRate(purchaseAmount, result); + + outputManager.printResults(result, profitRate); + } +} diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java new file mode 100644 index 00000000000..d321761b674 --- /dev/null +++ b/src/main/java/lotto/domain/Lotto.java @@ -0,0 +1,50 @@ +package lotto.domain; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import lotto.util.ErrorMessage; + +public class Lotto { + + public static final int MIN_LOTTO_NUMBER = 1; + public static final int MAX_LOTTO_NUMBER = 45; + public static final int SIZE_OF_LOTTO = 6; + private final List lottoNumbers; + + public Lotto(List lottoNumbers) { + validateLottoNumbers(lottoNumbers); + this.lottoNumbers = lottoNumbers; + } + + public int getMatchCount(UserInputNumbers receivedLotto) { + return (int) lottoNumbers.stream() + .filter(number -> receivedLotto.getReceivedLottoNumbers().contains(number)) + .count(); + } + + public boolean isBonusMatch(UserInputNumbers receivedLotto) { + return lottoNumbers.contains(receivedLotto.getBonusNumber()); + } + + private void validateLottoNumbers(List lottoNumbers) { + if (lottoNumbers.size() != SIZE_OF_LOTTO) { + throw new IllegalArgumentException(ErrorMessage.NOT_SIX_NUMBERS.getMessage()); + } + + for (int number : lottoNumbers) { + if (number < MIN_LOTTO_NUMBER || number > MAX_LOTTO_NUMBER) { + throw new IllegalArgumentException(ErrorMessage.NOT_IN_RANGE.getMessage()); + } + } + + Set uniqueNumbers = new HashSet<>(lottoNumbers); + if (uniqueNumbers.size() < lottoNumbers.size()) { + throw new IllegalArgumentException(ErrorMessage.MUST_NOT_DUPLICATE.getMessage()); + } + } + + public List getLottoNumbers() { + return lottoNumbers; + } +} diff --git a/src/main/java/lotto/domain/PurchaseAmount.java b/src/main/java/lotto/domain/PurchaseAmount.java new file mode 100644 index 00000000000..988d33cccd0 --- /dev/null +++ b/src/main/java/lotto/domain/PurchaseAmount.java @@ -0,0 +1,24 @@ +package lotto.domain; + +import lotto.util.ErrorMessage; + +public class PurchaseAmount { + + public static final int LOTTO_PRICE = 1000; + private final int purchaseAmount; + + public PurchaseAmount(int inputAmount) { + validatePurchaseAmount(inputAmount); + this.purchaseAmount = inputAmount; + } + + private void validatePurchaseAmount(int inputAmount) { + if (inputAmount % LOTTO_PRICE != 0) { + throw new IllegalArgumentException(ErrorMessage.NOT_A_UNIT_OF_1000.getMessage()); + } + } + + public int getPurchaseAmount() { + return purchaseAmount; + } +} diff --git a/src/main/java/lotto/domain/UserInputNumbers.java b/src/main/java/lotto/domain/UserInputNumbers.java new file mode 100644 index 00000000000..bb4f51c0901 --- /dev/null +++ b/src/main/java/lotto/domain/UserInputNumbers.java @@ -0,0 +1,58 @@ +package lotto.domain; + +import static lotto.domain.Lotto.MIN_LOTTO_NUMBER; +import static lotto.domain.Lotto.MAX_LOTTO_NUMBER; +import static lotto.domain.Lotto.SIZE_OF_LOTTO; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import lotto.util.ErrorMessage; + +public class UserInputNumbers { + + private final List receivedLottoNumbers; + private final int bonusNumber; + + public UserInputNumbers(List receivedLottoNumbers, int bonusNumber) { + validateLottoNumbers(receivedLottoNumbers); + this.receivedLottoNumbers = receivedLottoNumbers; + validateBonusNumbers(bonusNumber); + this.bonusNumber = bonusNumber; + } + + private void validateLottoNumbers(List receivedLottoNumbers) { + if (receivedLottoNumbers.size() != SIZE_OF_LOTTO) { + throw new IllegalArgumentException(ErrorMessage.NOT_SIX_NUMBERS.getMessage()); + } + + for (int number : receivedLottoNumbers) { + if (number < MIN_LOTTO_NUMBER || number > MAX_LOTTO_NUMBER) { + throw new IllegalArgumentException(ErrorMessage.NOT_IN_RANGE.getMessage()); + } + } + + Set uniqueNumbers = new HashSet<>(receivedLottoNumbers); + if (uniqueNumbers.size() < receivedLottoNumbers.size()) { + throw new IllegalArgumentException(ErrorMessage.MUST_NOT_DUPLICATE.getMessage()); + } + } + + private void validateBonusNumbers(int bonusNumber) { + if (bonusNumber < MIN_LOTTO_NUMBER || bonusNumber > MAX_LOTTO_NUMBER) { + throw new IllegalArgumentException(ErrorMessage.NOT_IN_RANGE.getMessage()); + } + + if (new HashSet<>(receivedLottoNumbers).contains(bonusNumber)) { + throw new IllegalArgumentException(ErrorMessage.MUST_NOT_DUPLICATE.getMessage()); + } + } + + public List getReceivedLottoNumbers() { // 결과 계산에 호출됨 + return receivedLottoNumbers; + } + + public int getBonusNumber() { + return bonusNumber; + } +} diff --git a/src/main/java/lotto/domain/WinningCheck.java b/src/main/java/lotto/domain/WinningCheck.java new file mode 100644 index 00000000000..5569e6b537a --- /dev/null +++ b/src/main/java/lotto/domain/WinningCheck.java @@ -0,0 +1,54 @@ +package lotto.domain; + +public enum WinningCheck { + LOSE(0, 0), + FIFTH_PRIZE(5_000, 3), + FOURTH_PRIZE(50_000, 4), + THIRD_PRIZE(1_500_000, 5, false), + SECOND_PRIZE(30_000_000, 5, true), + FIRST_PRIZE(2_000_000_000, 6); + + private final int prizeAmount; + private final int matchingCount; + private final boolean hasBonus; + + WinningCheck(int prizeAmount, int matchingCount) { + this.prizeAmount = prizeAmount; + this.matchingCount = matchingCount; + this.hasBonus = false; + } + + WinningCheck(int prizeAmount, int matchingCount, boolean hasBonus) { + this.prizeAmount = prizeAmount; + this.matchingCount = matchingCount; + this.hasBonus = hasBonus; + } + + public static WinningCheck getPrize(int matchCount, boolean bonusMatch) { + if (matchCount == 6) { + return FIRST_PRIZE; + } + if (matchCount == 5) { + return bonusMatch ? SECOND_PRIZE : THIRD_PRIZE; + } + if (matchCount == 4) { + return FOURTH_PRIZE; + } + if (matchCount == 3) { + return FIFTH_PRIZE; + } + return LOSE; + } + + public int getPrizeAmount() { + return prizeAmount; + } + + public int getMatchingCount() { + return matchingCount; + } + + public boolean hasBonus() { + return hasBonus; + } +} diff --git a/src/main/java/lotto/service/LottoGenerator.java b/src/main/java/lotto/service/LottoGenerator.java new file mode 100644 index 00000000000..4842ed4d8d6 --- /dev/null +++ b/src/main/java/lotto/service/LottoGenerator.java @@ -0,0 +1,38 @@ +package lotto.service; + +import static lotto.domain.Lotto.MAX_LOTTO_NUMBER; +import static lotto.domain.Lotto.MIN_LOTTO_NUMBER; +import static lotto.domain.Lotto.SIZE_OF_LOTTO; +import static lotto.domain.PurchaseAmount.LOTTO_PRICE; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.List; +import lotto.domain.Lotto; +import lotto.domain.PurchaseAmount; + +public class LottoGenerator { + + public static List generateLottos(PurchaseAmount purchaseAmount) { + int numberOfLotto = purchaseAmount.getPurchaseAmount() / LOTTO_PRICE; + System.out.println("\n" + numberOfLotto + "개를 구매했습니다."); + + List generatedLottos = new ArrayList<>(); + + for (int i = 0; i < numberOfLotto; i++) { + List lottoNumbers = + Randoms.pickUniqueNumbersInRange(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER, SIZE_OF_LOTTO); + + try { + Lotto lotto = new Lotto(lottoNumbers); + generatedLottos.add(lotto); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + i--; + } + } + + return generatedLottos; + } + +} diff --git a/src/main/java/lotto/service/LottoProfitRateCalculator.java b/src/main/java/lotto/service/LottoProfitRateCalculator.java new file mode 100644 index 00000000000..88b808c6054 --- /dev/null +++ b/src/main/java/lotto/service/LottoProfitRateCalculator.java @@ -0,0 +1,22 @@ +package lotto.service; + +import java.text.DecimalFormat; +import java.util.Map; +import lotto.domain.PurchaseAmount; +import lotto.domain.WinningCheck; + +public class LottoProfitRateCalculator { + + private static final int PERCENTAGE_FACTOR = 100; + + public double calculateProfitRate(PurchaseAmount purchaseAmount, + Map result) { + double totalPrize = result.entrySet().stream() + .mapToInt(entry -> entry.getKey().getPrizeAmount() * entry.getValue()) + .sum(); + + double profitRate = (totalPrize / purchaseAmount.getPurchaseAmount()) * PERCENTAGE_FACTOR; + DecimalFormat df = new DecimalFormat("#.##"); + return Double.parseDouble(df.format(profitRate)); + } +} diff --git a/src/main/java/lotto/service/LottoResultCalculator.java b/src/main/java/lotto/service/LottoResultCalculator.java new file mode 100644 index 00000000000..e27fbef52b8 --- /dev/null +++ b/src/main/java/lotto/service/LottoResultCalculator.java @@ -0,0 +1,24 @@ +package lotto.service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lotto.domain.Lotto; +import lotto.domain.UserInputNumbers; +import lotto.domain.WinningCheck; + +public class LottoResultCalculator { + + public Map calculateWinningResult(List lottos, + UserInputNumbers receivedLotto) { + Map result = new HashMap<>(); // WinningCheck 객체 정수를 키와 값으로 Map에 저장 + for (Lotto lotto : lottos) { + int matchCount = lotto.getMatchCount(receivedLotto); + boolean bonusMatch = lotto.isBonusMatch(receivedLotto); // receivedLotto는 bonus를 포함하고있음 + + WinningCheck prize = WinningCheck.getPrize(matchCount, bonusMatch); + result.merge(prize, 1, Integer::sum); + } + return result; + } +} diff --git a/src/main/java/lotto/util/ErrorMessage.java b/src/main/java/lotto/util/ErrorMessage.java new file mode 100644 index 00000000000..0e0feb48542 --- /dev/null +++ b/src/main/java/lotto/util/ErrorMessage.java @@ -0,0 +1,20 @@ +package lotto.util; + +public enum ErrorMessage { + NOT_A_NUMBER("[ERROR] 숫자여야 합니다."), + NOT_A_SINGLE_NUMBER("[ERROR] 보너스 번호는 하나의 숫자여야 합니다."), + NOT_A_UNIT_OF_1000("[ERROR] 1000 단위의 금액이어야 합니다."), + NOT_SIX_NUMBERS("[ERROR] 로또 번호는 총 6개여야 합니다."), + NOT_IN_RANGE("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."), + MUST_NOT_DUPLICATE("[ERROR] 로또 번호는 중복이 없어야 합니다."); + + private final String message; + + ErrorMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/lotto/util/InputManager.java b/src/main/java/lotto/util/InputManager.java new file mode 100644 index 00000000000..cad01f27734 --- /dev/null +++ b/src/main/java/lotto/util/InputManager.java @@ -0,0 +1,59 @@ +package lotto.util; + +import camp.nextstep.edu.missionutils.Console; +import java.util.Arrays; +import java.util.List; +import lotto.domain.PurchaseAmount; +import lotto.domain.UserInputNumbers; + +public class InputManager { + + public PurchaseAmount receivePurchaseAmount() { + while (true) { + try { + System.out.println("구입금액을 입력해 주세요."); + int inputAmount = Integer.parseInt(Console.readLine().trim()); + return new PurchaseAmount(inputAmount); + } catch (NumberFormatException e) { + System.out.println(ErrorMessage.NOT_A_NUMBER.getMessage()); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public UserInputNumbers receiveLottoNumber() { + while (true) { + try { + List receivedLottoNumbers = receiveLottoNumbers(); + int bonusNumber = receiveBonusNumber(); + return new UserInputNumbers(receivedLottoNumbers, bonusNumber); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + private List receiveLottoNumbers() { + try { + System.out.println("\n당첨 번호를 입력해 주세요."); + String inputNumbers = Console.readLine(); + String[] separatedNumbers = inputNumbers.split(","); + return Arrays.stream(separatedNumbers) + .map(String::trim) + .map(Integer::parseInt) + .toList(); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(ErrorMessage.NOT_A_NUMBER.getMessage()); + } + } + + private int receiveBonusNumber() { + try { + System.out.println("\n보너스 번호를 입력해 주세요."); + return Integer.parseInt(Console.readLine().trim()); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(ErrorMessage.NOT_A_SINGLE_NUMBER.getMessage()); + } + } +} diff --git a/src/main/java/lotto/util/OutputManager.java b/src/main/java/lotto/util/OutputManager.java new file mode 100644 index 00000000000..ec6035eb56d --- /dev/null +++ b/src/main/java/lotto/util/OutputManager.java @@ -0,0 +1,44 @@ +package lotto.util; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import lotto.domain.Lotto; +import lotto.domain.WinningCheck; + +public class OutputManager { + + public static void printGeneratedLottoNumbers(List lottos) { + for (Lotto lotto : lottos) { + List sortedLottoNumbers = new ArrayList<>(lotto.getLottoNumbers()); + Collections.sort(sortedLottoNumbers); + System.out.println(sortedLottoNumbers); + } + } + + public void printResults(Map result, double profitRate) { + printWinningResult(result); + printProfitRate(profitRate); + } + + private void printWinningResult(Map result) { + DecimalFormat decimalFormat = new DecimalFormat("#,###"); + System.out.println("\n당첨 통계\n---"); + for (WinningCheck prize : WinningCheck.values()) { + if (prize == WinningCheck.LOSE) { + continue; + } + String bonus = prize.hasBonus() ? ", 보너스 볼 일치" : ""; + String formattedPrize = decimalFormat.format(prize.getPrizeAmount()); + System.out.println( + prize.getMatchingCount() + "개 일치" + bonus + " (" + formattedPrize + "원) - " + + result.getOrDefault(prize, 0) + "개"); + } + } + + private void printProfitRate(double profitRate) { + System.out.println("총 수익률은 " + profitRate + "%입니다."); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java deleted file mode 100644 index 9f5dfe7eb83..00000000000 --- a/src/test/java/lotto/LottoTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package lotto; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -class LottoTest { - @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") - @Test - void createLottoByOverSize() { - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) - .isInstanceOf(IllegalArgumentException.class); - } - - @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") - @Test - void createLottoByDuplicatedNumber() { - // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) - .isInstanceOf(IllegalArgumentException.class); - } - - // 아래에 추가 테스트 작성 가능 -} \ No newline at end of file diff --git a/src/test/java/lotto/domain/LottoTest.java b/src/test/java/lotto/domain/LottoTest.java new file mode 100644 index 00000000000..b8b1561811d --- /dev/null +++ b/src/test/java/lotto/domain/LottoTest.java @@ -0,0 +1,59 @@ +package lotto.domain; + +import java.util.Arrays; +import lotto.domain.Lotto; +import lotto.domain.UserInputNumbers; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class LottoTest { + + @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") + @Test + void createLottoByOverSize() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호의 범위가 1~45가 아닌 경우 예외가 발생한다.") + @Test + void createLottoByInvalidNumber() { + assertThatThrownBy(() -> new Lotto(List.of(0, 2, 3, 4, 5, 6))) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 46))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호에 중복된 숫자가 있는 경우 예외가 발생한다.") + @Test + void createLottoByDuplicatedNumber() { + // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("getMatchCount()는 로또 번호와 일치하는 번호의 수를 반환한다.") + @Test + void testGetMatchCount() { + List lottoNumbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(lottoNumbers); + UserInputNumbers userInputNumbers = new UserInputNumbers(lottoNumbers, 7); + int matchCount = lotto.getMatchCount(userInputNumbers); + assertThat(matchCount).isEqualTo(6); + } + + @DisplayName("isBonusMatch()는 로또 번호와 보너스 번호가 일치하면 true를 반환한다.") + @Test + void testIsBonusMatch() { + List lottoNumbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(lottoNumbers); + UserInputNumbers userInputNumbers = new UserInputNumbers(lottoNumbers, 7); + boolean isBonusMatch = lotto.isBonusMatch(userInputNumbers); + assertThat(isBonusMatch).isFalse(); // 보너스 번호는 7이므로 일치하지 않아야 함 + } +} diff --git a/src/test/java/lotto/domain/PurchaseAmountTest.java b/src/test/java/lotto/domain/PurchaseAmountTest.java new file mode 100644 index 00000000000..ba8c18775d9 --- /dev/null +++ b/src/test/java/lotto/domain/PurchaseAmountTest.java @@ -0,0 +1,16 @@ +package lotto.domain; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PurchaseAmountTest { + + @DisplayName("구입금액이 1000 단위가 아니라면 예외가 발생한다.") + @Test + void createPurchaseAmountByInvalidAmount() { + assertThatThrownBy(() -> new PurchaseAmount(1500)) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/lotto/domain/UserInputNumbersTest.java b/src/test/java/lotto/domain/UserInputNumbersTest.java new file mode 100644 index 00000000000..2d7ca62aca3 --- /dev/null +++ b/src/test/java/lotto/domain/UserInputNumbersTest.java @@ -0,0 +1,33 @@ +package lotto.domain; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class UserInputNumbersTest { + + @DisplayName("당첨 번호의 개수가 6개를 넘어가는 경우 예외가 발생한다.") + @Test + void createNumberByOverSize() { + assertThatThrownBy(() -> new UserInputNumbers(List.of(1, 2, 3, 4, 5, 6, 7), 8)) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("당첨 번호의 범위가 1~45가 아닌 경우 예외가 발생한다.") + @Test + void createNumberByInvalidNumber() { + assertThatThrownBy(() -> new UserInputNumbers(List.of(0, 2, 3, 4, 5, 6), 7)) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> new UserInputNumbers(List.of(1, 2, 3, 4, 5, 46), 7)) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("당첨 번호에 중복된 숫자가 있는 경우 예외가 발생한다.") + @Test + void createNumberByDuplicatedNumber() { + assertThatThrownBy(() -> new UserInputNumbers(List.of(1, 2, 3, 4, 5, 5), 7)) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/lotto/util/LottoManagerTest.java b/src/test/java/lotto/util/LottoManagerTest.java new file mode 100644 index 00000000000..e6e2d04b043 --- /dev/null +++ b/src/test/java/lotto/util/LottoManagerTest.java @@ -0,0 +1,7 @@ +package lotto.util; + +import static org.junit.jupiter.api.Assertions.*; + +class LottoManagerTest { + +} \ No newline at end of file