diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..b8dad14e18 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,12 @@ package lotto; +import lotto.service.LottoManager; +import lotto.service.Manager; + public class Application { + public static void main(String[] args) { - // TODO: 프로그램 구현 + Manager manager = new LottoManager(); + manager.run(); } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 519793d1f7..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(); - } - } - - // TODO: 추가 기능 구현 -} diff --git a/src/main/java/lotto/domain/BonusNumber.java b/src/main/java/lotto/domain/BonusNumber.java new file mode 100644 index 0000000000..e3523ed9c2 --- /dev/null +++ b/src/main/java/lotto/domain/BonusNumber.java @@ -0,0 +1,33 @@ +package lotto.domain; + +public class BonusNumber { + + private final int number; + + private BonusNumber(int number) { + this.number = number; + } + + public static BonusNumber from(Lotto lotto, int number) { + validateRange(lotto, number); + validateDuplication(lotto, number); + return new BonusNumber(number); + } + + private static void validateRange(Lotto lotto, int number) { + if (lotto.isNotInRange(number)) { + throw new IllegalArgumentException(String.format("[ERROR] %d는 범위를 벗어난 숫자입니다.", number)); + } + + } + + private static void validateDuplication(Lotto lotto, int number) { + if (lotto.has(number)) { + throw new IllegalArgumentException(String.format("[ERROR] %d는 당첨 번호와 중복됩니다.", number)); + } + } + + public int getNumber() { + return number; + } +} diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java new file mode 100644 index 0000000000..b2a45c3736 --- /dev/null +++ b/src/main/java/lotto/domain/Lotto.java @@ -0,0 +1,72 @@ +package lotto.domain; + +import lotto.domain.generator.LottoGenerator; +import lotto.domain.result.LottoResult; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +public class Lotto { + + private static final int THE_NUMBER_OF_LOTTO = 6; + private static final int MIN_VALUE = 1; + private static final int MAX_VALUE = 45; + + private final List numbers; + + public Lotto(List numbers) { + validateSize(numbers); + validateRange(numbers); + validateDuplication(numbers); + this.numbers = new ArrayList<>(numbers); + } + + public static Lotto from(LottoGenerator lottoGenerator) { + return new Lotto(lottoGenerator.issue(MIN_VALUE, MAX_VALUE, THE_NUMBER_OF_LOTTO)); + } + + public boolean isNotInRange(int number) { + return number < MIN_VALUE || number > MAX_VALUE; + } + + public boolean has(int number) { + return numbers.contains(number); + } + + public LottoResult getResult(Lotto other, int bonusNumber) { + return LottoResult.find(getMatchingNumber(other), numbers.contains(bonusNumber)); + } + + public List getNumbers() { + return numbers; + } + + private void validateSize(List numbers) { + if (numbers.size() != THE_NUMBER_OF_LOTTO) { + throw new IllegalArgumentException(String.format("[ERROR] 로또 번호의 개수가 %d개가 아닙니다.", THE_NUMBER_OF_LOTTO)); + } + } + + private void validateRange(List numbers) { + if (hasNumberOutOfRange(numbers)) { + throw new IllegalArgumentException(String.format("[ERROR] 로또 번호는 %d부터 %d 사이의 숫자여야 합니다.", MIN_VALUE, MAX_VALUE)); + } + } + + private boolean hasNumberOutOfRange(List numbers) { + return numbers.stream().anyMatch(this::isNotInRange); + } + + private void validateDuplication(List numbers) { + if (new HashSet<>(numbers).size() != THE_NUMBER_OF_LOTTO) { + throw new IllegalArgumentException("중복되는 번호가 포함되어 있습니다."); + } + } + + private int getMatchingNumber(Lotto other) { + return (int) numbers.stream() + .filter(other.numbers::contains) + .count(); + } +} diff --git a/src/main/java/lotto/domain/Lottos.java b/src/main/java/lotto/domain/Lottos.java new file mode 100644 index 0000000000..2402962d8b --- /dev/null +++ b/src/main/java/lotto/domain/Lottos.java @@ -0,0 +1,34 @@ +package lotto.domain; + +import lotto.domain.generator.LottoGenerator; +import lotto.domain.result.LottoResult; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Lottos { + + private final List lottos; + + public Lottos(List lottos) { + this.lottos = new ArrayList<>(lottos); + } + + public static Lottos from(LottoGenerator lottoGenerator, int quantity) { + return new Lottos(Stream.generate(() -> Lotto.from(lottoGenerator)) + .limit(quantity) + .collect(Collectors.toList())); + } + + public List compare(Lotto winningLotto, int bonusNumber) { + return lottos.stream() + .map(lotto -> lotto.getResult(winningLotto, bonusNumber)) + .collect(Collectors.toList()); + } + + public List getLottos() { + return lottos; + } +} diff --git a/src/main/java/lotto/domain/PurchasingMoney.java b/src/main/java/lotto/domain/PurchasingMoney.java new file mode 100644 index 0000000000..1956584731 --- /dev/null +++ b/src/main/java/lotto/domain/PurchasingMoney.java @@ -0,0 +1,38 @@ +package lotto.domain; + +public class PurchasingMoney { + + private static final int UNIT = 1000; + + private final int money; + + private PurchasingMoney(int money) { + validatePositive(money); + validateUnit(money); + this.money = money; + } + + public static PurchasingMoney from(int money) { + return new PurchasingMoney(money); + } + + public int getMoney() { + return money; + } + + public int getQuantity() { + return money / UNIT; + } + + private void validatePositive(int money) { + if (money <= 0) { + throw new IllegalArgumentException("[ERROR] 구입금액은 양수여야 합니다."); + } + } + + private void validateUnit(int money) { + if (money % UNIT != 0) { + throw new IllegalArgumentException(String.format("[ERROR] 구입금액은 %d원 단위여야 합니다.", UNIT)); + } + } +} diff --git a/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java b/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java new file mode 100644 index 0000000000..7a0ea95c64 --- /dev/null +++ b/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java @@ -0,0 +1,26 @@ +package lotto.domain.generator; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.List; +import java.util.stream.Collectors; + +public class IssuedLottoGenerator implements LottoGenerator { + + private static final LottoGenerator ISSUED_LOTTO_GENERATOR = new IssuedLottoGenerator(); + + private IssuedLottoGenerator() { + } + + public static LottoGenerator create() { + return ISSUED_LOTTO_GENERATOR; + } + + @Override + public List issue(int minValue, int maxValue, int quantity) { + return Randoms.pickUniqueNumbersInRange(minValue, maxValue, quantity) + .stream() + .sorted() + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/lotto/domain/generator/LottoGenerator.java b/src/main/java/lotto/domain/generator/LottoGenerator.java new file mode 100644 index 0000000000..51d4651188 --- /dev/null +++ b/src/main/java/lotto/domain/generator/LottoGenerator.java @@ -0,0 +1,8 @@ +package lotto.domain.generator; + +import java.util.List; + +public interface LottoGenerator { + + List issue(int minValue, int maxValue, int quantity); +} diff --git a/src/main/java/lotto/domain/generator/WinningLottoGenerator.java b/src/main/java/lotto/domain/generator/WinningLottoGenerator.java new file mode 100644 index 0000000000..4d5513f19a --- /dev/null +++ b/src/main/java/lotto/domain/generator/WinningLottoGenerator.java @@ -0,0 +1,63 @@ +package lotto.domain.generator; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class WinningLottoGenerator implements LottoGenerator { + + private static final String DELIMITER = ","; + + private final String input; + + private WinningLottoGenerator(String input) { + this.input = input; + } + + public static WinningLottoGenerator from(String input) { + return new WinningLottoGenerator(input); + } + + @Override + public List issue(int minValue, int maxValue, int quantity) { + String[] numbers = input.split(DELIMITER); + validateDelimiter(numbers, quantity); + validateSize(numbers, quantity); + validateDuplication(numbers, quantity); + validateRange(numbers, minValue, maxValue); + return Arrays.stream(numbers) + .map(Integer::parseInt) + .collect(Collectors.toList()); + } + + private void validateDelimiter(String[] numbers, int quantity) { + if (numbers.length != quantity) { + throw new IllegalArgumentException(String.format("[ERROR] %s 외 구분자가 입력되었습니다.", DELIMITER)); + } + } + + private void validateDuplication(String[] numbers, int quantity) { + int numberWithoutDuplication = (int) Arrays.stream(numbers) + .distinct() + .count(); + if (numberWithoutDuplication != quantity) { + throw new IllegalArgumentException("[ERROR] 당첨 번호는 중복될 수 없습니다."); + } + } + + private void validateSize(String[] numbers, int quantity) { + if (numbers.length != quantity) { + throw new IllegalArgumentException(String.format("[ERROR] 당첨 번호의 개수가 %d개가 아닙니다.", quantity)); + } + } + + private void validateRange(String[] numbers, int minValue, int maxValue) { + Arrays.stream(numbers) + .mapToInt(Integer::parseInt) + .filter(number -> number < minValue || number > maxValue) + .findAny() + .ifPresent(number -> { + throw new IllegalArgumentException(String.format("[ERROR] 로또 번호는 %d부터 %d 사이의 숫자여야 합니다.", minValue, maxValue)); + }); + } +} diff --git a/src/main/java/lotto/domain/result/LottoResult.java b/src/main/java/lotto/domain/result/LottoResult.java new file mode 100644 index 0000000000..e6afe432b1 --- /dev/null +++ b/src/main/java/lotto/domain/result/LottoResult.java @@ -0,0 +1,43 @@ +package lotto.domain.result; + +import java.util.Arrays; + +public enum LottoResult { + + FIRST_PRIZE(6, false, 2_000_000_000), + SECOND_PRIZE(5, true, 30_000_000), + THIRD_PRIZE(5, false, 1_500_000), + FOURTH_PRIZE(4, false, 50_000), + FIFTH_PRIZE(3, false, 5_000), + NO_PRIZE(0, false, 0); + + private final int matchingNumber; + private final boolean hasBonusNumber; + private final int prize; + + LottoResult(int matchingNumber, boolean hasBonusNumber, int prize) { + this.matchingNumber = matchingNumber; + this.hasBonusNumber = hasBonusNumber; + this.prize = prize; + } + + public static LottoResult find(int matchingNumber, boolean hasBonusNumber) { + return Arrays.stream(values()) + .filter(value -> value.matchingNumber == matchingNumber) + .filter(value -> !value.hasBonusNumber || hasBonusNumber) + .findAny() + .orElse(NO_PRIZE); + } + + public int getPrize() { + return prize; + } + + @Override + public String toString() { + if (hasBonusNumber) { + return String.format("%d개 일치, 보너스 볼 일치 (%,d원)", matchingNumber, prize); + } + return String.format("%d개 일치 (%,d원)", matchingNumber, prize); + } +} diff --git a/src/main/java/lotto/domain/result/ProfitRate.java b/src/main/java/lotto/domain/result/ProfitRate.java new file mode 100644 index 0000000000..c0cfbc158a --- /dev/null +++ b/src/main/java/lotto/domain/result/ProfitRate.java @@ -0,0 +1,19 @@ +package lotto.domain.result; + +public class ProfitRate { + + private final double value; + + private ProfitRate(double value) { + this.value = value; + } + + public static ProfitRate from(int prize, int purchasingMoney) { + return new ProfitRate(((double) prize / purchasingMoney) * 100); + } + + @Override + public String toString() { + return String.format("%.1f", value); + } +} diff --git a/src/main/java/lotto/domain/result/WinningDetail.java b/src/main/java/lotto/domain/result/WinningDetail.java new file mode 100644 index 0000000000..728c399c9c --- /dev/null +++ b/src/main/java/lotto/domain/result/WinningDetail.java @@ -0,0 +1,43 @@ +package lotto.domain.result; + +import java.util.*; +import java.util.stream.Collectors; + +public class WinningDetail { + + private final Map lottoResults; + + private WinningDetail(Map lottoResultIntegerMap) { + this.lottoResults = new EnumMap<>(lottoResultIntegerMap); + } + + public static WinningDetail from(List lottoResults) { + Map lottoResultMap = getLottoResultMap(); + lottoResults.forEach(lottoResult -> lottoResultMap.computeIfPresent(lottoResult, (key, value) -> value + 1)); + return new WinningDetail(lottoResultMap); + } + + public int getTotalPrize() { + return lottoResults.entrySet() + .stream() + .mapToInt(lottoResult -> lottoResult.getKey().getPrize() * lottoResult.getValue()) + .sum(); + } + + @Override + public String toString() { + return lottoResults.entrySet() + .stream() + .filter(lottoResult -> lottoResult.getKey() != LottoResult.NO_PRIZE) + .sorted(Comparator.comparing(lottoResult -> lottoResult.getKey().getPrize())) + .map(lottoResult -> String.format("%s - %d개", lottoResult.getKey(), lottoResult.getValue())) + .collect(Collectors.joining("\n")); + } + + private static Map getLottoResultMap() { + Map lottoResultCounts = new EnumMap<>(LottoResult.class); + Arrays.stream(LottoResult.values()) + .forEach(lottoResult -> lottoResultCounts.put(lottoResult, 0)); + return lottoResultCounts; + } +} diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java new file mode 100644 index 0000000000..4ea294e55b --- /dev/null +++ b/src/main/java/lotto/service/LottoManager.java @@ -0,0 +1,39 @@ +package lotto.service; + +import lotto.domain.BonusNumber; +import lotto.domain.Lotto; +import lotto.domain.Lottos; +import lotto.domain.PurchasingMoney; +import lotto.domain.generator.IssuedLottoGenerator; +import lotto.domain.generator.WinningLottoGenerator; +import lotto.domain.result.ProfitRate; +import lotto.domain.result.WinningDetail; +import lotto.util.Convertor; +import lotto.util.Input; +import lotto.util.Output; + +public class LottoManager implements Manager { + + @Override + public void run() { + PurchasingMoney purchasingMoney = PurchasingMoney.from(Convertor.toInteger(Input.inputPurchasingMoney())); + Lottos issuedLottos = purchaseLotto(purchasingMoney); + + Lotto winningLotto = Lotto.from(WinningLottoGenerator.from(Input.inputWinningLotto())); + BonusNumber bonusNumber = BonusNumber.from(winningLotto, Convertor.toInteger(Input.inputBonusNumber())); + + compareLotto(purchasingMoney, issuedLottos, winningLotto, bonusNumber); + } + + private Lottos purchaseLotto(PurchasingMoney purchasingMoney) { + Lottos issuedLotto = Lottos.from(IssuedLottoGenerator.create(), purchasingMoney.getQuantity()); + Output.printIssuedLotto(issuedLotto, purchasingMoney.getQuantity()); + return issuedLotto; + } + + private void compareLotto(PurchasingMoney purchasingMoney, Lottos issuedLotto, Lotto winningLotto, BonusNumber bonusNumber) { + WinningDetail winningDetail = WinningDetail.from(issuedLotto.compare(winningLotto, bonusNumber.getNumber())); + ProfitRate profitRate = ProfitRate.from(winningDetail.getTotalPrize(), purchasingMoney.getMoney()); + Output.printWinningDetail(winningDetail, profitRate); + } +} diff --git a/src/main/java/lotto/service/Manager.java b/src/main/java/lotto/service/Manager.java new file mode 100644 index 0000000000..04ebba3fa0 --- /dev/null +++ b/src/main/java/lotto/service/Manager.java @@ -0,0 +1,6 @@ +package lotto.service; + +public interface Manager { + + void run(); +} diff --git a/src/main/java/lotto/util/Convertor.java b/src/main/java/lotto/util/Convertor.java new file mode 100644 index 0000000000..77eae6bcd9 --- /dev/null +++ b/src/main/java/lotto/util/Convertor.java @@ -0,0 +1,15 @@ +package lotto.util; + +public class Convertor { + + private Convertor() { + } + + public static int toInteger(String input) { + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 정수가 아닌 값이 포함되었습니다."); + } + } +} diff --git a/src/main/java/lotto/util/Input.java b/src/main/java/lotto/util/Input.java new file mode 100644 index 0000000000..33b893f728 --- /dev/null +++ b/src/main/java/lotto/util/Input.java @@ -0,0 +1,31 @@ +package lotto.util; + +import camp.nextstep.edu.missionutils.Console; + +public class Input { + + private static final String BLANK = " "; + private static final String DELETE = ""; + + private Input() { + } + + public static String inputPurchasingMoney() { + System.out.println("구입금액을 입력해 주세요."); + return removeBlank(Console.readLine()); + } + + public static String inputWinningLotto() { + System.out.println("\n당첨 번호를 입력해 주세요."); + return removeBlank(Console.readLine()); + } + + public static String inputBonusNumber() { + System.out.println("\n보너스 번호를 입력해 주세요."); + return removeBlank(Console.readLine()); + } + + private static String removeBlank(String input) { + return input.replaceAll(BLANK, DELETE); + } +} diff --git a/src/main/java/lotto/util/Output.java b/src/main/java/lotto/util/Output.java new file mode 100644 index 0000000000..c5fb709c5c --- /dev/null +++ b/src/main/java/lotto/util/Output.java @@ -0,0 +1,27 @@ +package lotto.util; + +import lotto.domain.Lotto; +import lotto.domain.Lottos; +import lotto.domain.result.ProfitRate; +import lotto.domain.result.WinningDetail; + +public class Output { + + private Output() { + } + + public static void printIssuedLotto(Lottos issuedLotto, int quantity) { + System.out.printf("\n%d개를 구매했습니다.%n", quantity); + issuedLotto.getLottos() + .stream() + .map(Lotto::getNumbers) + .map(String::valueOf) + .forEach(System.out::println); + } + + public static void printWinningDetail(WinningDetail winningDetail, ProfitRate profitRate) { + System.out.println("\n당첨 통계\n---\n" + + winningDetail + + String.format("\n총 수익률은 %s%%입니다.\n", profitRate)); + } +} diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index a15c7d1f52..ff06a07b1e 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -8,9 +8,9 @@ import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; class ApplicationTest extends NsTest { - private static final String ERROR_MESSAGE = "[ERROR]"; @Test void 기능_테스트() { @@ -49,8 +49,8 @@ class ApplicationTest extends NsTest { @Test void 예외_테스트() { assertSimpleTest(() -> { - runException("1000j"); - assertThat(output()).contains(ERROR_MESSAGE); + assertSimpleTest(() -> assertThatThrownBy(() -> runException("1000j")) + .isInstanceOf(IllegalArgumentException.class)); }); } diff --git a/src/test/java/lotto/domain/BonusNumberTest.java b/src/test/java/lotto/domain/BonusNumberTest.java new file mode 100644 index 0000000000..079a9f9a26 --- /dev/null +++ b/src/test/java/lotto/domain/BonusNumberTest.java @@ -0,0 +1,35 @@ +package lotto.domain; + +import lotto.domain.generator.LottoGenerator; +import lotto.domain.generator.WinningLottoGenerator; +import lotto.util.Convertor; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class BonusNumberTest { + + @ParameterizedTest + @ValueSource(strings = {"7", "12", "45"}) + void 당첨번호와_미중복시_보너스번호_생성(String input) { + // given + LottoGenerator winningLottoGenerator = WinningLottoGenerator.from("1,2,3,4,5,6"); + Lotto lotto = Lotto.from(winningLottoGenerator); + // when & then + assertThatCode(() -> BonusNumber.from(lotto, Convertor.toInteger(input))) + .doesNotThrowAnyException(); + } + + @ParameterizedTest + @ValueSource(strings = {"6", "k2", "46"}) + void 보너스번호_검증실패시_예외발생(String input) { + // given + LottoGenerator winningLottoGenerator = WinningLottoGenerator.from("1,2,3,4,5,6"); + Lotto lotto = Lotto.from(winningLottoGenerator); + // when & then + assertThatThrownBy(() -> BonusNumber.from(lotto, Convertor.toInteger(input))) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/domain/LottoTest.java similarity index 92% rename from src/test/java/lotto/LottoTest.java rename to src/test/java/lotto/domain/LottoTest.java index 0f3af0f6c4..1b241d2357 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/domain/LottoTest.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.domain; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -8,6 +8,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; class LottoTest { + @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") @Test void createLottoByOverSize() { @@ -22,6 +23,4 @@ void createLottoByDuplicatedNumber() { assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) .isInstanceOf(IllegalArgumentException.class); } - - // 아래에 추가 테스트 작성 가능 } diff --git a/src/test/java/lotto/domain/generator/IssuedLottoGeneratorTest.java b/src/test/java/lotto/domain/generator/IssuedLottoGeneratorTest.java new file mode 100644 index 0000000000..270f2605dd --- /dev/null +++ b/src/test/java/lotto/domain/generator/IssuedLottoGeneratorTest.java @@ -0,0 +1,19 @@ +package lotto.domain.generator; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatCode; + +public class IssuedLottoGeneratorTest { + + @DisplayName("로또 발행") + @Test + void 로또_발행() { + // given + LottoGenerator issuedLottoGenerator = IssuedLottoGenerator.create(); + // when & then + assertThatCode(() -> issuedLottoGenerator.issue(1, 45, 6)) + .doesNotThrowAnyException(); + } +} diff --git a/src/test/java/lotto/domain/generator/WinningLottoGeneratorTest.java b/src/test/java/lotto/domain/generator/WinningLottoGeneratorTest.java new file mode 100644 index 0000000000..5293f67385 --- /dev/null +++ b/src/test/java/lotto/domain/generator/WinningLottoGeneratorTest.java @@ -0,0 +1,30 @@ +package lotto.domain.generator; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class WinningLottoGeneratorTest { + + @ParameterizedTest + @ValueSource(strings = {"1,2,3,4,5,6", "31,22,45,10,8,29"}) + void 입력받은_당첨번호로_로또번호_생성(String input) { + // given + LottoGenerator winningLottoGenerator = WinningLottoGenerator.from(input); + // when & then + assertThatCode(() -> winningLottoGenerator.issue(1, 45, 6)) + .doesNotThrowAnyException(); + } + + @ParameterizedTest + @ValueSource(strings = {"1,2,3,4,5", "31,22,42,10,8,46", "1-2-3-4-5-6", "1,2,3-4-5-6", "1,1,2,3,4,5", "k,1,2,3,4,5"}) + void 입력받은_당첨번호_검증실패시_예외발생(String input) { + // given + LottoGenerator winningLottoGenerator = WinningLottoGenerator.from(input); + // when & then + assertThatThrownBy(() -> winningLottoGenerator.issue(1, 45, 6)) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/src/test/java/lotto/domain/result/ProfitRateTest.java b/src/test/java/lotto/domain/result/ProfitRateTest.java new file mode 100644 index 0000000000..6dca68c72b --- /dev/null +++ b/src/test/java/lotto/domain/result/ProfitRateTest.java @@ -0,0 +1,19 @@ +package lotto.domain.result; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ProfitRateTest { + + @Test + void 수익률_소숫점_둘째_자리에서_반올림() { + // given + int prize = 55000; + int purchasingMoney = 7000; + // when + ProfitRate profitRate = ProfitRate.from(prize, purchasingMoney); + // then + assertThat(profitRate).isEqualTo("785.7"); + } +}