diff --git a/README.md b/README.md index 5fa2560b46..74d083181a 100644 --- a/README.md +++ b/README.md @@ -1 +1,119 @@ # java-lotto-precourse + +## 입출력 기능 + +### 입력 기능 + +- 구입 금액 입력 기능 + - 로또 구입 금액을 1,000원 단위로 입력 받는다. + - 유효성 검증 + - 숫자가 아닌 문자(공백, 특수기호)를 입력받으면 예외 처리 + - 1,000원으로 나누어 떨어지지 않는 경우 예외 처리 + - 0을 입력받으면 예외 처리 + - 구입 금액이 0으로 시작하면 예외 처리 (01000원) + +- 당첨 번호 입력 기능 + - `,`를 기준으로 6개의 당첨 번호를 입력 받는다. + - 유효성 검증 + - 당첨 번호가 6개가 아닌 경우 예외 처리 + - 중복된 당첨 번호가 있다면 예외 처리 + - 당첨 번호가 숫자가 아닌 경우 예외 처리 + - 당첨 번호가 0으로 시작하면 예외 처리 + - 당첨 번호가 1 ~ 45의 범위에 해당하지 않으면 예외 처리 + +- 보너스 번호 입력 기능 + - 보너스 번호를 입력 받는다. + - 유효성 검증 + - 보너스 번호가 숫자가 아닌 경우 예외 처리 + - 보너스 번호가 당첨 번호와 중복된다면 예외 처리 + - 보너스 번호가 1 ~45의 범위에 해당하지 않으면 예외 처리 + - 보너스 번호가 0으로 시작하면 예외 처리 + +- 예외 처리 + - 오입력에대한 예외 처리는 `IllegalArgumentException`을 발생 후 에러 메시지 출력 후 입력을 다시 받는다. + - 에러메시지의 형식은 `[ERROR]`로 시작한다. + +### 출력 기능 + +- 발행한 로또 수량 및 번호 출력 기능 + - 발행한 로또 수량 및 번호 출력 + - 로또 번호는 오름차순으로 정렬 + ```text + 8개를 구매했습니다. + [8, 21, 23, 41, 42, 43] + [3, 5, 11, 16, 32, 38] + [7, 11, 16, 35, 36, 44] + [1, 8, 11, 31, 41, 42] + [13, 14, 16, 38, 42, 45] + [7, 11, 30, 40, 42, 43] + [2, 13, 22, 32, 38, 45] + [1, 3, 5, 14, 22, 45] + ``` + +- 당첨 내역 출력 기능 + - 발행한 로또의 당첨 내역(당첨 개수)을 출력 + ```text + 3개 일치 (5,000원) - 1개 + 4개 일치 (50,000원) - 0개 + 5개 일치 (1,500,000원) - 0개 + 5개 일치, 보너스 볼 일치 (30,000,000원) - 0개 + 6개 일치 (2,000,000,000원) - 0개 + ``` + +- 수익률 출력 기능 + - 소수점 둘째 자리에서 반올림 + - `총 수익률은 62.5%입니다.` + +- 에러 메시지 출력 기능 + - 예외 발생 시 `[ERROR]`로 시작하는 에러 문구를 출력한다. + - `[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.` + +## Domain 기능 + +### Lotto + +- 로또 당첨 번호를 저장하는 클래스 + - 6개의 숫자를 List에 저장 + - 유효성 검증 + - List의 길이가 6이 아니면 예외 처리 + - 로또 번호의 중복된 번호가 있다면 예외 처리 + - 로또 번호가 1 ~ 45의 범위에 해당하지 않는다면 예외 처리 + +### BonusNumber + +- 보너스 번호를 저장하는 클래스 + - 유효성 검증 + - 보너스 번호가 로또 번호에 있다면 예외 처리 + - 보너스 번호가 1 ~ 45의 범위에 해당하지 않는다면 예외 처리 + +### PurchaseAmount + +- 구입 금액을 저장하는 클래스 + - 유효성 검증 + - 구입 금액이 0이면 예외 처리 + - 구입 금액이 1000으로 나누어 떨어지지 않는다면 예외 처리 + +## Enum 기능 + +### LottoRank + +- 등수와 상금을 저장하는 enum + - 등수와 상금을 저장 + +## Service 기능 + +- 구입 금액에 대해 로또를 생성하는 기능 +- 구입한 로또가 당첨되었는지 판단하는 기능 + +## View 기능 + +- 입출력 메시지를 출력하는 기능 +- 구입한 로또의 정보를 출력하는 기능 +- 로또의 통계를 출력하는 기능 + +## Controller 기능 + +- 로또를 실행시키는 Controller + - 로또를 구매하는 기능 + - 당첨, 보너스 번호를 저장하는 기능 + - 로또의 결과를 출력하는 기능 \ No newline at end of file diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..c8e8376944 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,10 @@ package lotto; +import lotto.controller.LottoController; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + LottoController lottoController = new LottoController(); + lottoController.playLottoGame(); } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 88fc5cf12b..0000000000 --- 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("[ERROR] 로또 번호는 6개여야 합니다."); - } - } - - // TODO: 추가 기능 구현 -} diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java new file mode 100644 index 0000000000..7a4673e98e --- /dev/null +++ b/src/main/java/lotto/controller/LottoController.java @@ -0,0 +1,102 @@ +package lotto.controller; + +import java.util.HashMap; +import java.util.List; +import lotto.domain.BonusNumber; +import lotto.domain.Lotto; +import lotto.domain.PurchaseAmount; +import lotto.enums.LottoRank; +import lotto.service.LottoService; +import lotto.util.InputUtil; +import lotto.view.LottoInputView; +import lotto.view.LottoOutputView; + +public class LottoController { + private final LottoService lottoService; + private final LottoInputView lottoInputView; + private final LottoOutputView lottoOutputView; + private int ticketCount; + private PurchaseAmount purchaseAmount; + private List totalLottoNumbers; + private Lotto winningLottoNumbers; + private BonusNumber winningBonusNumber; + + public LottoController() { + this.lottoService = new LottoService(); + this.lottoInputView = new LottoInputView(); + this.lottoOutputView = new LottoOutputView(); + } + + public void playLottoGame() { + purchaseLotto(); + makeWinningLotto(); + showLottoGameResult(); + } + + private void showLottoGameResult() { + HashMap winningResults = lottoService.getWinningResults(totalLottoNumbers, + winningLottoNumbers, winningBonusNumber); + for (int rank = 5; rank > 0; rank--) { + LottoRank lottoRank = LottoRank.getEnumsValue(rank); + lottoOutputView.printLottoResultMessage(lottoRank, winningResults.get(lottoRank)); + } + double rateOfReturn = lottoService.calculateRateOfReturn(winningResults, purchaseAmount); + lottoOutputView.printLottoRateOfReturnMessage(rateOfReturn); + } + + private void makeWinningLotto() { + makeLotto(); + makeBonusNumber(); + } + + private void makeBonusNumber() { + boolean checkInput = false; + + while (!checkInput) { + try { + String inputBonusNumber = lottoInputView.readBonusNumberInput(); + winningBonusNumber = new BonusNumber(InputUtil.convertStringToInt(inputBonusNumber)); + checkInput = true; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + private void makeLotto() { + boolean checkInput = false; + + while (!checkInput) { + try { + String inputLotto = lottoInputView.readLottoNumberInput(); + winningLottoNumbers = new Lotto(InputUtil.parseInputToLottoList(inputLotto)); + checkInput = true; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + private void purchaseLotto() { + getPurchaseAmount(); + lottoOutputView.printPurchaseAmountMessage(ticketCount); + totalLottoNumbers = lottoService.generateTotalLottoNumbers(ticketCount); + lottoOutputView.printRandomLottoNumberMessage(totalLottoNumbers); + } + + private void getPurchaseAmount() { + boolean checkInput = false; + + while (!checkInput) { + try { + String inputCash = lottoInputView.readPurchaseAmountInput(); + int purchaseCash = InputUtil.convertStringToInt(inputCash); + purchaseAmount = new PurchaseAmount(purchaseCash); + ticketCount = lottoService.calculateLottoTickets(purchaseAmount); + checkInput = true; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } +} diff --git a/src/main/java/lotto/domain/BonusNumber.java b/src/main/java/lotto/domain/BonusNumber.java new file mode 100644 index 0000000000..02c185640e --- /dev/null +++ b/src/main/java/lotto/domain/BonusNumber.java @@ -0,0 +1,28 @@ +package lotto.domain; + +public class BonusNumber { + private final int bonusNumber; + + public BonusNumber(int bonusNumber) { + validateBonusNumber(bonusNumber); + this.bonusNumber = bonusNumber; + } + + private void validateBonusNumber(int bonusNumber) { + if (bonusNumber <= 0 || bonusNumber > 45) { + throw new IllegalArgumentException("[ERROR] 보너스 번호는 1 ~ 45인 정수여야 합니다."); + } + } + + public void validateExistLottoNumber(Lotto lotto) { + for (int number : lotto.getNumbers()) { + if (number == bonusNumber) { + throw new IllegalArgumentException("[ERROR] 보너스 번호는 로또 번호와 중복되지 않은 수여야 합니다."); + } + } + } + + public int getBonusNumber() { + return bonusNumber; + } +} diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java new file mode 100644 index 0000000000..0e4c318ad3 --- /dev/null +++ b/src/main/java/lotto/domain/Lotto.java @@ -0,0 +1,39 @@ +package lotto.domain; + +import java.util.List; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validate(numbers); + validateLottoNumber(numbers); + validateUniqueLottoNumber(numbers); + this.numbers = numbers; + } + + private void validate(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } + + private void validateLottoNumber(List numbers) { + for (int number : numbers) { + if (number <= 0 || number > 45) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 1 ~ 45인 정수여야 합니다."); + } + } + } + + private void validateUniqueLottoNumber(List numbers) { + long uniqueCount = numbers.stream().distinct().count(); + if (uniqueCount != numbers.size()) { + throw new IllegalArgumentException("[ERROR] 로또 숫자는 중복되지 않은 수여야 합니다."); + } + } + + public List getNumbers() { + return numbers; + } +} diff --git a/src/main/java/lotto/domain/PurchaseAmount.java b/src/main/java/lotto/domain/PurchaseAmount.java new file mode 100644 index 0000000000..3e952ef5d0 --- /dev/null +++ b/src/main/java/lotto/domain/PurchaseAmount.java @@ -0,0 +1,27 @@ +package lotto.domain; + +public class PurchaseAmount { + private final int money; + + public PurchaseAmount(int money) { + validateMoney(money); + validateDivide1000(money); + this.money = money; + } + + private void validateMoney(int money) { + if (money < 1000) { + throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원이상의 값으로 입력되어야 합니다."); + } + } + + private void validateDivide1000(int money) { + if (money % 1000 != 0) { + throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원 단위로 입력되어야 합니다."); + } + } + + public int getMoney() { + return money; + } +} diff --git a/src/main/java/lotto/enums/LottoRank.java b/src/main/java/lotto/enums/LottoRank.java new file mode 100644 index 0000000000..b5f88b1f85 --- /dev/null +++ b/src/main/java/lotto/enums/LottoRank.java @@ -0,0 +1,47 @@ +package lotto.enums; + +public enum LottoRank { + FIRST_RANK(1, 6, 2_000_000_000, "2,000,000,000"), + SECOND_RANK(2, 5, 30_000_000, "30,000,000"), + THIRD_RANK(3, 5, 1_500_000, "1,500,000"), + FOURTH_RANK(4, 4, 50_000, "50,000"), + FIFTH_RANK(5, 3, 5_000, "5,000"), + NONE_RANK(0, 0, 0, "0"); + + private final int rank; + private final int count; + private final int prize; + private final String prizeInfo; + + LottoRank(int rank, int count, int prize, String prizeInfo) { + this.rank = rank; + this.count = count; + this.prize = prize; + this.prizeInfo = prizeInfo; + } + + public int getRank() { + return rank; + } + + public int getCount() { + return count; + } + + public int getPrize() { + return prize; + } + + public String getPrizeInfo() { + return prizeInfo; + } + + public static LottoRank getEnumsValue(int rank) { + for (LottoRank lottoRank : LottoRank.values()) { + if (lottoRank.getRank() == rank) { + return lottoRank; + } + } + return NONE_RANK; + } +} diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java new file mode 100644 index 0000000000..f5157b6ba9 --- /dev/null +++ b/src/main/java/lotto/service/LottoService.java @@ -0,0 +1,86 @@ +package lotto.service; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import lotto.domain.BonusNumber; +import lotto.domain.Lotto; +import lotto.domain.PurchaseAmount; +import lotto.enums.LottoRank; + +public class LottoService { + private static final int MAX_LOTTO_NUMBER = 45; + private static final int MIN_LOTTO_NUMBER = 1; + private static final int DIVISOR = 1000; + + public int calculateLottoTickets(PurchaseAmount purchaseAmount) { + return purchaseAmount.getMoney() / DIVISOR; + } + + public List generateTotalLottoNumbers(int ticketCount) { + List totalLottoNumbers = new ArrayList<>(); + for (int i = 0; i < ticketCount; i++) { + Lotto newLotto = generateLottoNumbers(); + totalLottoNumbers.add(newLotto); + } + return totalLottoNumbers; + } + + public Lotto generateLottoNumbers() { + List numbers = Randoms.pickUniqueNumbersInRange(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER, 6); + return new Lotto(numbers); + } + + public HashMap getWinningResults(List lotto, Lotto winningLotto, + BonusNumber bonusNumber) { + HashMap winningResult = new HashMap<>(); + for (int rank = 0; rank <= 5; rank++) { + winningResult.putIfAbsent(LottoRank.getEnumsValue(rank), 0); + } + for (Lotto curLotto : lotto) { + LottoRank curLottoRank = calculateLottoRank(curLotto.getNumbers(), winningLotto.getNumbers(), bonusNumber); + winningResult.putIfAbsent(curLottoRank, 0); + winningResult.put(curLottoRank, winningResult.get(curLottoRank) + 1); + } + return winningResult; + } + + private LottoRank calculateLottoRank(List lotto, List winningLotto, BonusNumber bonusNumber) { + int correctCount = 0; + boolean correctBonusNumber = false; + for (int winningNumber : winningLotto) { + if (lotto.contains(winningNumber)) { + correctCount++; + } + } + if (lotto.contains(bonusNumber.getBonusNumber())) { + correctCount++; + correctBonusNumber = true; + } + return convertCountToRank(correctCount, correctBonusNumber); + } + + private LottoRank convertCountToRank(int count, boolean rightBonusNumber) { + if (count == 6) { + if (rightBonusNumber) { + return LottoRank.SECOND_RANK; + } + return LottoRank.FIRST_RANK; + } + if (count < 3) { + return LottoRank.NONE_RANK; + } + return LottoRank.getEnumsValue(8 - count); + } + + public double calculateRateOfReturn(HashMap results, PurchaseAmount purchaseAmount) { + long sum = 0; + for (int rank = 1; rank <= 5; rank++) { + LottoRank lottoRank = LottoRank.getEnumsValue(rank); + int count = results.get(lottoRank); + sum += (long) lottoRank.getPrize() * count; + } + return (sum / (double) purchaseAmount.getMoney()) * 100; + } +} diff --git a/src/main/java/lotto/util/InputUtil.java b/src/main/java/lotto/util/InputUtil.java new file mode 100644 index 0000000000..97d13f5065 --- /dev/null +++ b/src/main/java/lotto/util/InputUtil.java @@ -0,0 +1,34 @@ +package lotto.util; + +import camp.nextstep.edu.missionutils.Console; +import java.util.ArrayList; +import java.util.List; + +public class InputUtil { + static final String DELIMITER = ","; + + public static String readInput() { + String input = Console.readLine(); + Validator.validateNullInput(input); + Validator.validateWhitespaceInput(input); + return input; + } + + public static List parseInputToLottoList(String input) { + Validator.validateRightDelimiter(input); + String[] numbers = input.split(DELIMITER); + List lottoList = new ArrayList<>(); + for (String number : numbers) { + Validator.validateNullInput(number); + Validator.validateWhitespaceInput(number); + int num = Validator.validateLottoNumber(number); + lottoList.add(num); + } + return lottoList; + } + + public static int convertStringToInt(String input) { + Validator.validateRightNumber(input); + return Validator.validateLottoNumber(input); + } +} diff --git a/src/main/java/lotto/util/Validator.java b/src/main/java/lotto/util/Validator.java new file mode 100644 index 0000000000..88726ee897 --- /dev/null +++ b/src/main/java/lotto/util/Validator.java @@ -0,0 +1,36 @@ +package lotto.util; + +public class Validator { + public static void validateWhitespaceInput(String input) { + if (input.startsWith(" ") || input.endsWith(" ")) { + throw new IllegalArgumentException("[ERROR] 입력값은 공백으로 시작하거나 끝날 수 없습니다."); + } + } + + public static void validateNullInput(String input) { + if (input == null || input.isEmpty()) { + throw new IllegalArgumentException("[ERROR] 입력값은 빈 문자열이 될 수 없습니다."); + } + } + + public static int validateLottoNumber(String number) { + try { + validateRightNumber(number); + return Integer.parseInt(number); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 숫자가 아닌 문자는 입력할 수 없습니다."); + } + } + + public static void validateRightNumber(String number) { + if (number.charAt(0) == '0') { + throw new IllegalArgumentException("[ERROR] 올바른 형식의 숫자를 입력해주세요."); + } + } + + public static void validateRightDelimiter(String input) { + if (input.startsWith(InputUtil.DELIMITER) || input.endsWith(InputUtil.DELIMITER)) { + throw new IllegalArgumentException("[ERROR] 구분자는 반드시 숫자 사이에 있어야합니다."); + } + } +} diff --git a/src/main/java/lotto/view/LottoInputView.java b/src/main/java/lotto/view/LottoInputView.java new file mode 100644 index 0000000000..ce02e23e7b --- /dev/null +++ b/src/main/java/lotto/view/LottoInputView.java @@ -0,0 +1,24 @@ +package lotto.view; + +import lotto.util.InputUtil; + +public class LottoInputView { + private static final String INPUT_PURCHASE_AMOUNT_MESSAGE = "구입금액을 입력해 주세요."; + private static final String INPUT_LOTTO_NUMBER_MESSAGE = "당첨 번호를 입력해 주세요."; + private static final String INPUT_BONUS_NUMBER_MESSAGE = "보너스 번호를 입력해 주세요."; + + public String readPurchaseAmountInput() { + System.out.println(INPUT_PURCHASE_AMOUNT_MESSAGE); + return InputUtil.readInput(); + } + + public String readLottoNumberInput() { + System.out.println(INPUT_LOTTO_NUMBER_MESSAGE); + return InputUtil.readInput(); + } + + public String readBonusNumberInput() { + System.out.println(INPUT_BONUS_NUMBER_MESSAGE); + return InputUtil.readInput(); + } +} diff --git a/src/main/java/lotto/view/LottoOutputView.java b/src/main/java/lotto/view/LottoOutputView.java new file mode 100644 index 0000000000..f1c2f75c4c --- /dev/null +++ b/src/main/java/lotto/view/LottoOutputView.java @@ -0,0 +1,39 @@ +package lotto.view; + +import java.util.List; +import lotto.domain.Lotto; +import lotto.enums.LottoRank; + +public class LottoOutputView { + private static final String OUTPUT_PURCHASE_AMOUNT_MESSAGE = "%d개를 구매했습니다.\n"; + private static final String OUTPUT_LOTTO_RESULT_MESSAGE = "당첨 통계\n---"; + private static final String OUTPUT_LOTTO_RANK_RESULT_MESSAGE = "%d개 일치 (%s원) - %d개\n"; + private static final String OUTPUT_LOTTO_SECOND_RANK_RESULT_MESSAGE = "%d개 일치, 보너스 볼 일치 (%s원) - %d개\n"; + private static final String OUTPUT_LOTTO_RATE_OF_RETURN_MESSAGE = "총 수익률은 %.1f%%입니다."; + + public void printPurchaseAmountMessage(int purchaseAmountInput) { + System.out.printf(OUTPUT_PURCHASE_AMOUNT_MESSAGE, purchaseAmountInput); + } + + public void printRandomLottoNumberMessage(List numbers) { + for (Lotto lotto : numbers) { + System.out.println(lotto.getNumbers()); + } + } + + public void printLottoResultMessage(LottoRank lottoRank, int result) { + if (lottoRank.getRank() == 5) { + System.out.println(OUTPUT_LOTTO_RESULT_MESSAGE); + } + if (lottoRank.getRank() == 2) { + System.out.printf(OUTPUT_LOTTO_SECOND_RANK_RESULT_MESSAGE, lottoRank.getCount(), lottoRank.getPrizeInfo(), + result); + return; + } + System.out.printf(OUTPUT_LOTTO_RANK_RESULT_MESSAGE, lottoRank.getCount(), lottoRank.getPrizeInfo(), result); + } + + public void printLottoRateOfReturnMessage(double rate) { + System.out.printf(OUTPUT_LOTTO_RATE_OF_RETURN_MESSAGE, rate); + } +} diff --git a/src/test/java/lotto/domain/BonusNumberTest.java b/src/test/java/lotto/domain/BonusNumberTest.java new file mode 100644 index 0000000000..f03a7fb877 --- /dev/null +++ b/src/test/java/lotto/domain/BonusNumberTest.java @@ -0,0 +1,13 @@ +package lotto.domain; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +public class BonusNumberTest { + @Test + void 로또_보너스_번호가_범위에서_넘어가면_예외가_발생한다() { + assertThatThrownBy(() -> new BonusNumber(50)) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/domain/LottoTest.java similarity index 74% rename from src/test/java/lotto/LottoTest.java rename to src/test/java/lotto/domain/LottoTest.java index 309f4e50ae..a6ab75fcfc 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/domain/LottoTest.java @@ -1,11 +1,10 @@ -package lotto; +package lotto.domain; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.List; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; class LottoTest { @Test @@ -21,5 +20,9 @@ class LottoTest { .isInstanceOf(IllegalArgumentException.class); } - // TODO: 추가 기능 구현에 따른 테스트 코드 작성 + @Test + void 로또_번호에_범위_밖의_숫자가_있으면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 55, 6))) + .isInstanceOf(IllegalArgumentException.class); + } } diff --git a/src/test/java/lotto/domain/PurchaseAmountTest.java b/src/test/java/lotto/domain/PurchaseAmountTest.java new file mode 100644 index 0000000000..d9ca7040cb --- /dev/null +++ b/src/test/java/lotto/domain/PurchaseAmountTest.java @@ -0,0 +1,19 @@ +package lotto.domain; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +public class PurchaseAmountTest { + @Test + void 구매_가격이_1000원보다_작으면_예외가_발생한다() { + assertThatThrownBy(() -> new PurchaseAmount(0)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void 구매_가격이_1000원으로_나누어_떨어지지_않는다면_예외가_발생한다() { + assertThatThrownBy(() -> new PurchaseAmount(1500)) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/lotto/util/InputUtilTest.java b/src/test/java/lotto/util/InputUtilTest.java new file mode 100644 index 0000000000..3b651197d4 --- /dev/null +++ b/src/test/java/lotto/util/InputUtilTest.java @@ -0,0 +1,26 @@ +package lotto.util; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class InputUtilTest { + @DisplayName("로또 번호 입력이 잘못된 경우 예외가 발생한다.") + @Test + void 로또_번호_입력이_잘못된_경우_예외가_발생한다() { + assertThatThrownBy(() -> InputUtil.parseInputToLottoList("1,2,,3")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void 로또_번호_파싱_테스트() { + String input = "1,2,3,4,5,6"; + List expectedResult = List.of(1,2,3,4,5,6); + List actualResult = InputUtil.parseInputToLottoList(input); + + assertThat(actualResult).isEqualTo(expectedResult); + } +} diff --git a/src/test/java/lotto/util/ValidatorTest.java b/src/test/java/lotto/util/ValidatorTest.java new file mode 100644 index 0000000000..46decb7b1d --- /dev/null +++ b/src/test/java/lotto/util/ValidatorTest.java @@ -0,0 +1,29 @@ +package lotto.util; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class ValidatorTest { + @DisplayName("로또 번호가 숫자가 아닌 경우 예외가 발생한다.") + @Test + void 로또_번호가_숫자가_아닌_경우_예외가_발생한다() { + assertThatThrownBy(() -> Validator.validateLottoNumber("a")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("올바른 숫자가 아닌 경우 예외가 발생한다.") + @Test + void 올바른_숫자가_아닌_경우_예외가_발생한다() { + assertThatThrownBy(() -> Validator.validateRightNumber("0001")) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("구분자의 위치가 올바르지 않은 경우 예외가 발생한다.") + @Test + void 구분자의_위치가_올바르지_않은_경우_예외가_발생한다() { + assertThatThrownBy(() -> Validator.validateRightDelimiter("12,3,4,")) + .isInstanceOf(IllegalArgumentException.class); + } +}