diff --git a/src/main/java/lotto/LottoApplication.java b/src/main/java/lotto/LottoApplication.java index 0bbf162798..2b4fb67f5e 100644 --- a/src/main/java/lotto/LottoApplication.java +++ b/src/main/java/lotto/LottoApplication.java @@ -1,22 +1,19 @@ package lotto; -import lotto.model.PurchaseAmount; -import lotto.model.Lotto; -import lotto.model.LottoResults; -import lotto.model.Lottos; +import lotto.model.*; import lotto.view.InputView; import lotto.view.OutputView; public class LottoApplication { public static void main(String[] args) { - PurchaseAmount purchaseAmount = InputView.readBudgetInput(); - Lotto winningLotto = InputView.readWinningLottoInput(); + Money purchaseAmount = InputView.readPurchaseAmountInput(); + WinningLotto winningLotto = InputView.readWinningLottoInput(); OutputView.printPurchaseCount(purchaseAmount.countLottoTickets()); Lottos lottos = purchaseAmount.buyLottos(); OutputView.printBoughtLottos(lottos); - LottoResults result = lottos.calculateResults(winningLotto); + LottoResults result = lottos.match(winningLotto); OutputView.printResults(result, purchaseAmount); } } diff --git a/src/main/java/lotto/README.md b/src/main/java/lotto/README.md index f95741a40f..9cd8d4e41e 100644 --- a/src/main/java/lotto/README.md +++ b/src/main/java/lotto/README.md @@ -4,7 +4,9 @@ - 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급한다. (로또 1장에 1,000원) - 로또 번호는 1부터 45까지의 숫자 중 6개를 랜덤으로 선택한다. - 당첨 번호 6개를 입력하면, 구입한 로또와 비교하여 당첨 결과를 알려준다. +- 보너스 번호를 뽑아 당첨 번호 5개와 보너스 번호 1개가 일치하는 로또는 2등상을 준다. - 당첨 결과는 3개, 4개, 5개, 6개 일치에 따라 각각 5,000원, 50,000원, 1,500,000원, 2,000,000,000원의 상금을 지급한다. +- 2등은 30,000,000원을 지급한다. - 수익률을 계산하여 출력한다. (수익률 = 총 당첨 금액 / 구입 금액) - 수익률은 소수점 둘째 자리까지 표시한다. - 1 이상인 경우 이익, 1 미만인 경우 손해로 표시한다. \ No newline at end of file diff --git a/src/main/java/lotto/model/Lotto.java b/src/main/java/lotto/model/Lotto.java index 691dd93ae2..7c2e7198da 100644 --- a/src/main/java/lotto/model/Lotto.java +++ b/src/main/java/lotto/model/Lotto.java @@ -1,68 +1,54 @@ package lotto.model; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Lotto { - private final List numbers; + private final static int LOTTO_NUMBER_SIZE = 6; + private final static List rangedInts = IntStream.rangeClosed(1, 45).boxed().collect(Collectors.toList()); + private final Set numbers; public Lotto() { this(generateRandomNumbers()); } public Lotto(int... numbers) { - this(Arrays.stream(numbers).boxed().collect(Collectors.toList())); + this(Arrays.stream(numbers).mapToObj(LottoNumber::of).collect(Collectors.toSet())); } - public Lotto(List numbers) { + public Lotto(Set numbers) { checkValidity(numbers); - Collections.sort(numbers); - this.numbers = numbers.stream().map(LottoNumber::new).collect(Collectors.toList()); - } - - public List value() { - return Collections.unmodifiableList(this.numbers); + this.numbers = numbers; } public int countMatchNumbers(Lotto lotto) { - int matchCount = 0; - for (LottoNumber number : this.numbers) { - matchCount += addMatchCount(number, lotto); - } - return matchCount; + return Math.toIntExact(this.numbers.stream().filter(lotto::contains).count()); } - private void checkValidity(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException("로또 번호는 6개여야 합니다."); - } - if (hasDuplicated(numbers)) { - throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다."); - } + public boolean matchesBonusNumber(LottoNumber bonusNumber) { + return contains(bonusNumber); } - private boolean hasDuplicated(List numbers) { - long distinctCount = numbers.stream().distinct().count(); - return distinctCount != numbers.size(); + @Override + public String toString() { + List lottoNumbers = new ArrayList<>(this.numbers); + Collections.sort(lottoNumbers); + return lottoNumbers.toString(); } - private int addMatchCount(LottoNumber number, Lotto lotto) { - if (lotto.contains(number)) { - return 1; - } - return 0; + public boolean contains(LottoNumber number) { + return numbers.contains(number); } - private boolean contains(LottoNumber number) { - return numbers.contains(number); + private void checkValidity(Set numbers) { + if (numbers.size() != LOTTO_NUMBER_SIZE) { + throw new IllegalArgumentException("로또 번호는 6개여야 합니다."); + } } - private static List generateRandomNumbers() { - List array = IntStream.rangeClosed(1, 45).boxed().collect(Collectors.toList()); - Collections.shuffle(array); - return array.subList(0, 6); + private static Set generateRandomNumbers() { + Collections.shuffle(rangedInts); + return rangedInts.subList(0, LOTTO_NUMBER_SIZE).stream().map(LottoNumber::of).collect(Collectors.toSet()); } } diff --git a/src/main/java/lotto/model/LottoNumber.java b/src/main/java/lotto/model/LottoNumber.java index 9c7daec0be..fecb047230 100644 --- a/src/main/java/lotto/model/LottoNumber.java +++ b/src/main/java/lotto/model/LottoNumber.java @@ -1,19 +1,22 @@ package lotto.model; +import java.util.Map; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.IntStream; -public class LottoNumber { - private final int number; +public class LottoNumber implements Comparable { + private final static Map CACHE; - public LottoNumber(int number) { - if (!isValid(number)) { - throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - this.number = number; + static { + CACHE = new ConcurrentHashMap<>(); + IntStream.rangeClosed(1, 45).forEach(i -> CACHE.put(i, new LottoNumber(i))); } - private boolean isValid(int number) { - return number >= 1 && number <= 45; + private final int number; + + private LottoNumber(int number) { + this.number = number; } public int getNumber() { @@ -36,4 +39,24 @@ public int hashCode() { public String toString() { return String.valueOf(number); } + + @Override + public int compareTo(LottoNumber another) { + return Integer.compare(this.number, another.number); + } + + private static boolean isValid(int number) { + return CACHE.containsKey(number); + } + + public static LottoNumber of(int number) { + if (!isValid(number)) { + throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + return CACHE.get(number); + } + + public static LottoNumber of(String input) { + return of(Integer.parseInt(input)); + } } diff --git a/src/main/java/lotto/model/LottoResults.java b/src/main/java/lotto/model/LottoResults.java index 8a9e15bf93..1eb405c7c7 100644 --- a/src/main/java/lotto/model/LottoResults.java +++ b/src/main/java/lotto/model/LottoResults.java @@ -1,59 +1,56 @@ package lotto.model; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Objects; public class LottoResults { - private final Map matchCounts; + private final Map prizeCounts; public LottoResults() { this(new HashMap<>()); } - public LottoResults(Map matchCounts) { - this.matchCounts = matchCounts; + public LottoResults(Map prizeCounts) { + this.prizeCounts = prizeCounts; } - public void updateMatchCount(int matchCount) { - if (matchCount < 3) { - return; - } - matchCounts.put(matchCount, matchCounts.getOrDefault(matchCount, 0) + 1); + public void update(Prize prize) { + prizeCounts.put(prize, prizeCounts.getOrDefault(prize, 0) + 1); + } + + public Money getTotalPrizeValue() { + return new Money(prizeCounts.entrySet().stream() + .mapToLong(entry -> (long) entry.getValue() * entry.getKey().value()) + .sum()); } - public long getPrizeValue() { - return matchCounts.entrySet().stream() - .mapToLong(entry -> (long) entry.getValue() * Prize.fromMatchCount(entry.getKey()).value()) - .sum(); + public double getReturnRate(Money purchaseAmount) { + Money totalPrize = getTotalPrizeValue(); + return calculateReturnRate(purchaseAmount, totalPrize); } - public double getReturnRate(PurchaseAmount purchaseAmount) { - long totalPrize = getPrizeValue(); - return purchaseAmount.getReturnRate(totalPrize); + private double calculateReturnRate(Money purchaseAmount, Money totalPrize) { + if (purchaseAmount.isZero()) { + return 0; + } + double rate = totalPrize.divideBy(purchaseAmount); + return Math.floor(rate * 100) / 100.0; } - public Integer getMatchCount(int matchCount) { - return matchCounts.getOrDefault(matchCount, 0); + public Integer getPrizeCount(Prize prize) { + return prizeCounts.getOrDefault(prize, 0); } @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; LottoResults that = (LottoResults) o; - return Objects.equals(matchCounts, that.matchCounts); + return Objects.equals(prizeCounts, that.prizeCounts); } @Override public int hashCode() { - return Objects.hashCode(matchCounts); - } - - @Override - public String toString() { - return "LottoResults{" + - "matchCounts=" + matchCounts + - '}'; + return Objects.hashCode(prizeCounts); } } diff --git a/src/main/java/lotto/model/Lottos.java b/src/main/java/lotto/model/Lottos.java index 737a013b2e..832ca26c50 100644 --- a/src/main/java/lotto/model/Lottos.java +++ b/src/main/java/lotto/model/Lottos.java @@ -1,46 +1,41 @@ package lotto.model; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class Lottos { private final List lottos; public Lottos(int count) { - this(new ArrayList<>()); - for (int i = 0; i < count; i++) { - lottos.add(new Lotto()); - } + this(createLottos(count)); } public Lottos(List lottos) { this.lottos = lottos; } - public LottoResults calculateResults(Lotto winningLotto) { - LottoResults results = new LottoResults(); - - for (Lotto lotto : lottos) { - int matchCount = lotto.countMatchNumbers(winningLotto); - results.updateMatchCount(matchCount); - } - - return results; - } - - private void addMatchCount(int matchCount, List results) { - if (matchCount >= 3 && matchCount <= 6) { - int idx = matchCount - 3; - results.set(idx, results.get(idx) + 1); + public LottoResults match(WinningLotto winningLotto) { + LottoResults lottoResults = new LottoResults(); + for (Lotto lotto : this.lottos) { + lottoResults.update(winningLotto.calculatePrize(lotto)); } + return lottoResults; } + @Override public String toString() { StringBuilder sb = new StringBuilder(); for (Lotto lotto : lottos) { - sb.append(lotto.value().toString()).append("\n"); + sb.append(lotto.toString()).append("\n"); } return sb.toString(); } + + private static List createLottos(int count) { + List lottos = new ArrayList<>(); + for (int i = 0; i < count; i++) { + lottos.add(new Lotto()); + } + return lottos; + } } diff --git a/src/main/java/lotto/model/PurchaseAmount.java b/src/main/java/lotto/model/Money.java similarity index 57% rename from src/main/java/lotto/model/PurchaseAmount.java rename to src/main/java/lotto/model/Money.java index a016411247..cb878def8f 100644 --- a/src/main/java/lotto/model/PurchaseAmount.java +++ b/src/main/java/lotto/model/Money.java @@ -2,10 +2,14 @@ import java.util.Objects; -public class PurchaseAmount { - private final int amount; +public class Money { + private final long amount; - public PurchaseAmount(int amount) { + public Money(int amount) { + this((long) amount); + } + + public Money(long amount) { if (!isValid(amount)) { throw new IllegalArgumentException("예산은 0 이상이며 1000원 단위로만 설정 가능합니다."); } @@ -13,30 +17,30 @@ public PurchaseAmount(int amount) { } public int countLottoTickets() { - return amount / 1_000; + return Math.toIntExact(amount / 1_000L); } public Lottos buyLottos() { return new Lottos(countLottoTickets()); } - public double getReturnRate(Long totalPrize) { - if (amount == 0) { - return 0; - } - double rate = (double) totalPrize / amount; - return Math.floor(rate * 100) / 100.0; + public boolean isZero() { + return amount == 0; + } + + public double divideBy(Money money) { + return (double) this.amount / money.amount; } - private boolean isValid(int amount) { + private boolean isValid(Long amount) { return amount >= 0 && amount % 1000 == 0; } @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; - PurchaseAmount purchaseAmount = (PurchaseAmount) o; - return amount == purchaseAmount.amount; + Money money = (Money) o; + return amount == money.amount; } @Override diff --git a/src/main/java/lotto/model/Prize.java b/src/main/java/lotto/model/Prize.java index 6f08b711c8..3acba2245c 100644 --- a/src/main/java/lotto/model/Prize.java +++ b/src/main/java/lotto/model/Prize.java @@ -4,10 +4,12 @@ import java.util.Arrays; public enum Prize { - THREE_MATCHES(3, 5_000), - FOUR_MATCHES(4, 50_000), - FIVE_MATCHES(5, 1_500_000), - SIX_MATCHES(6, 2_000_000_000); + MISS(0, 0), + FIFTH(3, 5_000), + FOURTH(4, 50_000), + THIRD(5, 1_500_000), + SECOND(5, 30_000_000), + FIRST(6, 2_000_000_000); private final int matchCount; private final int prizeValue; @@ -23,13 +25,22 @@ public int value() { @Override public String toString() { - return String.format("%d개 일치 (%s원)", matchCount, prizeValue); + if (this.equals(SECOND)) { + return String.format("%d개 일치, 보너스 볼 일치(%s원)", matchCount, prizeValue); + } + return String.format("%d개 일치(%s원)", matchCount, prizeValue); } - static Prize fromMatchCount(int matchCount) { + static Prize valueOf(int matchCount, boolean matchBonus) { + if (matchCount < 3) { + return MISS; + } + if (matchCount == 5 && matchBonus) { + return SECOND; + } return Arrays.stream(Prize.values()) .filter(prize -> prize.matchCount == matchCount) .findFirst() - .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 일치 값입니다. " + matchCount)); + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 일치 값입니다. " + matchCount + matchBonus)); } } diff --git a/src/main/java/lotto/model/WinningLotto.java b/src/main/java/lotto/model/WinningLotto.java new file mode 100644 index 0000000000..a429ac90ca --- /dev/null +++ b/src/main/java/lotto/model/WinningLotto.java @@ -0,0 +1,24 @@ +package lotto.model; + +public class WinningLotto { + private final Lotto winningLotto; + private final LottoNumber bonusNumber; + + public WinningLotto(Lotto lotto, int bonusNumber) { + this(lotto, LottoNumber.of(bonusNumber)); + } + + public WinningLotto(Lotto lotto, LottoNumber bonusNumber) { + if (lotto.contains(bonusNumber)) { + throw new IllegalArgumentException("당첨 번호는 보너스볼의 번호를 포함할 수 없습니다."); + } + this.winningLotto = lotto; + this.bonusNumber = bonusNumber; + } + + public Prize calculatePrize(Lotto lotto) { + int matchCount = lotto.countMatchNumbers(winningLotto); + boolean matchBonus = lotto.matchesBonusNumber(bonusNumber); + return Prize.valueOf(matchCount, matchBonus); + } +} diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java index a02f739613..fd91e4d6ea 100644 --- a/src/main/java/lotto/view/InputView.java +++ b/src/main/java/lotto/view/InputView.java @@ -1,31 +1,40 @@ package lotto.view; -import lotto.model.PurchaseAmount; -import lotto.model.Lotto; +import lotto.model.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; +import java.util.*; public class InputView { - public static PurchaseAmount readBudgetInput() { + public static Money readPurchaseAmountInput() { Scanner scanner = new Scanner(System.in); print("구입금액을 입력해 주세요."); - return new PurchaseAmount(scanner.nextInt()); + return new Money(scanner.nextInt()); } - public static Lotto readWinningLottoInput() { + public static WinningLotto readWinningLottoInput() { + Lotto lotto = readLottoInput(); + LottoNumber lottoNumber = readBonusNumberInput(); + return new WinningLotto(lotto, lottoNumber); + } + + private static Lotto readLottoInput() { Scanner scanner = new Scanner(System.in); print("지난 주 당첨 번호를 입력해 주세요."); String[] inputs = scanner.nextLine().split(","); - List numbers = new ArrayList<>(); + Set numbers = new HashSet<>(); for (String input : inputs) { - int number = Integer.parseInt(input.trim()); - numbers.add(number); + numbers.add(LottoNumber.of(input.trim())); } return new Lotto(numbers); } + private static LottoNumber readBonusNumberInput() { + Scanner scanner = new Scanner(System.in); + print("보너스 볼을 입력해 주세요."); + int bonusNumber = scanner.nextInt(); + return LottoNumber.of(bonusNumber); + } + private static void print(String message) { System.out.println(message); } diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java index 5f1c51bd55..c1728f10f9 100644 --- a/src/main/java/lotto/view/OutputView.java +++ b/src/main/java/lotto/view/OutputView.java @@ -1,7 +1,7 @@ package lotto.view; import lotto.model.Prize; -import lotto.model.PurchaseAmount; +import lotto.model.Money; import lotto.model.LottoResults; import lotto.model.Lottos; @@ -10,7 +10,7 @@ public static void printPurchaseCount(int count) { System.out.printf("%s개를 구매했습니다.%n", count); } - public static void printResults(LottoResults result, PurchaseAmount purchaseAmount) { + public static void printResults(LottoResults result, Money purchaseAmount) { printResultHeader(); printMatchCounts(result); printRate(result, purchaseAmount); @@ -26,13 +26,14 @@ private static void printResultHeader() { } private static void printMatchCounts(LottoResults results) { - System.out.printf("%s- %s개%n", Prize.THREE_MATCHES, results.getMatchCount(3)); - System.out.printf("%s- %s개%n", Prize.FOUR_MATCHES, results.getMatchCount(4)); - System.out.printf("%s- %s개%n", Prize.FIVE_MATCHES, results.getMatchCount(5)); - System.out.printf("%s- %s개%n", Prize.SIX_MATCHES, results.getMatchCount(6)); + System.out.printf("%s- %s개%n", Prize.FIFTH, results.getPrizeCount(Prize.FIFTH)); + System.out.printf("%s- %s개%n", Prize.FOURTH, results.getPrizeCount(Prize.FOURTH)); + System.out.printf("%s- %s개%n", Prize.THIRD, results.getPrizeCount(Prize.THIRD)); + System.out.printf("%s- %s개%n", Prize.SECOND, results.getPrizeCount(Prize.SECOND)); + System.out.printf("%s- %s개%n", Prize.FIRST, results.getPrizeCount(Prize.FIRST)); } - private static void printRate(LottoResults results, PurchaseAmount purchaseAmount) { + private static void printRate(LottoResults results, Money purchaseAmount) { double rate = results.getReturnRate(purchaseAmount); System.out.printf("총 수익률은 %.2f입니다.", rate); if (rate >= 1) { diff --git a/src/test/java/lotto/model/LottoNumberTest.java b/src/test/java/lotto/model/LottoNumberTest.java index 751d2247e7..b35b1f5caf 100644 --- a/src/test/java/lotto/model/LottoNumberTest.java +++ b/src/test/java/lotto/model/LottoNumberTest.java @@ -1,6 +1,7 @@ package lotto.model; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -8,17 +9,23 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; class LottoNumberTest { - @ParameterizedTest - @DisplayName("로또 번호는 1부터 45 사이의 숫자여야 한다.") - @ValueSource(ints = {1, 45}) - void validLottoNumberTest(int number) { - assertThat(new LottoNumber(number)).isEqualTo(new LottoNumber(number)); + @Test + @DisplayName("LottoNumber은 매번 메모리 캐시에서 가져온 동일한 객체이다.") + void getLottoInstanceTest() { + assertThat(LottoNumber.of(1) == LottoNumber.of(1)).isTrue(); + + } + + @Test + @DisplayName("String 인자를 넣어 가져온 LottoNumber도 매번 동일한 객체이다.") + void getLottoInstanceFromStringParameterTest() { + assertThat(LottoNumber.of("2") == LottoNumber.of(2)).isTrue(); } @ParameterizedTest @DisplayName("로또 번호는 1부터 45 사이의 숫자가 아니면 예외가 발생한다.") @ValueSource(ints = {0, 46}) void invalidLottoNumberTest(int number) { - assertThatIllegalArgumentException().isThrownBy(() -> new LottoNumber(number)); + assertThatIllegalArgumentException().isThrownBy(() -> LottoNumber.of(number)); } } \ No newline at end of file diff --git a/src/test/java/lotto/model/LottoResultsTest.java b/src/test/java/lotto/model/LottoResultsTest.java index 611dd775be..34ab8435c9 100644 --- a/src/test/java/lotto/model/LottoResultsTest.java +++ b/src/test/java/lotto/model/LottoResultsTest.java @@ -15,25 +15,27 @@ class LottoResultsTest { @Test @DisplayName("당첨금 합계를 계산한다.") - void getPrizeValue() { - assertThat(new LottoResults(Map.of(3, 1, 4, 1, 5, 1, 6, 1)).getPrizeValue()) - .isEqualTo(2001555000L); - assertThat(new LottoResults(Map.of(3, 0, 4, 1, 5, 1, 6, 4)).getPrizeValue()) - .isEqualTo(8001550000L); + void getTotalPrizeValue() { + assertThat(new LottoResults(Map.of(Prize.FIFTH, 1, Prize.FOURTH, 1, Prize.THIRD, 1, Prize.FIRST, 1)).getTotalPrizeValue()) + .isEqualTo(new Money(2_001_555_000L)); + assertThat(new LottoResults(Map.of(Prize.FIFTH, 0, Prize.FOURTH, 1, Prize.THIRD, 1, Prize.FIRST, 4)).getTotalPrizeValue()) + .isEqualTo(new Money(8_001_550_000L)); + assertThat(new LottoResults(Map.of(Prize.SECOND, 3, Prize.FIRST, 2)).getTotalPrizeValue()) + .isEqualTo(new Money(4_090_000_000L)); } @ParameterizedTest @DisplayName("당첨금과 구매 금액을 통해 수익률을 계산한다.") @MethodSource("returnRateProvider") void getReturnRate(LottoResults results, double expectedRate) { - assertThat(results.getReturnRate(new PurchaseAmount(14000))).isEqualTo(expectedRate); + assertThat(results.getReturnRate(new Money(14000))).isEqualTo(expectedRate); } static Stream returnRateProvider() { return Stream.of( - Arguments.of(new LottoResults(Map.of(3, 0, 4, 0, 5, 0, 6, 0)), 0.0), - Arguments.of(new LottoResults(Map.of(3, 1, 4, 0, 5, 0, 6, 0)), 0.35), - Arguments.of(new LottoResults(Map.of(3, 0, 4, 0, 5, 0, 6, 1)), 142857.14) + Arguments.of(new LottoResults(Map.of(Prize.FIFTH, 0, Prize.FOURTH, 0, Prize.THIRD, 0, Prize.FIRST, 0)), 0.0), + Arguments.of(new LottoResults(Map.of(Prize.FIFTH, 1, Prize.FOURTH, 0, Prize.THIRD, 0, Prize.FIRST, 0)), 0.35), + Arguments.of(new LottoResults(Map.of(Prize.FIFTH, 0, Prize.FOURTH, 0, Prize.SECOND, 1, Prize.FIRST, 1)), 145000.0) ); } } \ No newline at end of file diff --git a/src/test/java/lotto/model/LottoTest.java b/src/test/java/lotto/model/LottoTest.java index a3bbc36006..411c6bdd4a 100644 --- a/src/test/java/lotto/model/LottoTest.java +++ b/src/test/java/lotto/model/LottoTest.java @@ -3,8 +3,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.List; - import static org.assertj.core.api.Assertions.*; class LottoTest { @@ -17,11 +15,10 @@ void getMatchCountTest() { } @Test - @DisplayName("Lotto 객체는 1과 45 사이 외의 숫자나 6개 이상의 숫자, 중복된 숫자를 가지면 에러를 반환한다.") + @DisplayName("Lotto 객체는 1과 45 사이 외의 숫자나 6개 이상의 숫자를 가지면 에러를 반환한다.") void invalidLottoTest() { assertThatIllegalArgumentException().isThrownBy(() -> new Lotto(0, 2, 3, 4, 5, 6)); assertThatIllegalArgumentException().isThrownBy(() -> new Lotto(1, 2, 3, 4, 5, 46)); - assertThatIllegalArgumentException().isThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))); - assertThatIllegalArgumentException().isThrownBy(() -> new Lotto(List.of(1, 1, 2, 3, 4, 5))); + assertThatIllegalArgumentException().isThrownBy(() -> new Lotto(1, 2, 3, 4, 5, 6, 7)); } } \ No newline at end of file diff --git a/src/test/java/lotto/model/LottosTest.java b/src/test/java/lotto/model/LottosTest.java deleted file mode 100644 index 4777a069d5..0000000000 --- a/src/test/java/lotto/model/LottosTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package lotto.model; - -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; - -class LottosTest { - @Test - void getLottosResult() { - Lottos lottos = new Lottos( - Arrays.asList( - new Lotto(1, 2, 3, 4, 5, 6), - new Lotto(2, 3, 4, 5, 6, 7), - new Lotto(3, 4, 5, 6, 7, 8) - ) - ); - Lotto winningLotto = new Lotto(1, 2, 3, 4, 5, 6); - assertThat(lottos.calculateResults(winningLotto)) - .isEqualTo(new LottoResults(Map.of(4, 1, 5, 1, 6, 1))); - } - -} \ No newline at end of file diff --git a/src/test/java/lotto/model/PurchaseAmountTest.java b/src/test/java/lotto/model/MoneyTest.java similarity index 71% rename from src/test/java/lotto/model/PurchaseAmountTest.java rename to src/test/java/lotto/model/MoneyTest.java index f41fe85142..b139830498 100644 --- a/src/test/java/lotto/model/PurchaseAmountTest.java +++ b/src/test/java/lotto/model/MoneyTest.java @@ -5,17 +5,17 @@ import static org.assertj.core.api.Assertions.*; -class PurchaseAmountTest { +class MoneyTest { @Test @DisplayName("로또 구입 금액은 1000원 단위여야 한다.") public void validTest() { - PurchaseAmount purchaseAmount = new PurchaseAmount(10000); - assertThat(purchaseAmount).isEqualTo(new PurchaseAmount(10000)); + Money money = new Money(10000); + assertThat(money).isEqualTo(new Money(10000)); } @Test @DisplayName("로또 구입 금액이 1000원 단위가 아니면 예외가 발생한다.") public void invalidTest() { - assertThatIllegalArgumentException().isThrownBy(() -> new PurchaseAmount(1500)); + assertThatIllegalArgumentException().isThrownBy(() -> new Money(1500)); } } \ No newline at end of file diff --git a/src/test/java/lotto/model/PrizeTest.java b/src/test/java/lotto/model/PrizeTest.java index ac19b1436c..f0295e92df 100644 --- a/src/test/java/lotto/model/PrizeTest.java +++ b/src/test/java/lotto/model/PrizeTest.java @@ -8,11 +8,11 @@ class PrizeTest { @Test - @DisplayName("일치 개수로 상금을 올바르게 반환한다.") + @DisplayName("로또 숫자 일치 개수에 따른 상금을 올바르게 반환한다.") void fromMatchCount() { - assertThat(Prize.fromMatchCount(3)).isEqualTo(Prize.THREE_MATCHES); - assertThat(Prize.fromMatchCount(4)).isEqualTo(Prize.FOUR_MATCHES); - assertThat(Prize.fromMatchCount(5)).isEqualTo(Prize.FIVE_MATCHES); - assertThat(Prize.fromMatchCount(6)).isEqualTo(Prize.SIX_MATCHES); + assertThat(Prize.valueOf(2, false)).isEqualTo(Prize.MISS); + assertThat(Prize.valueOf(5, false)).isEqualTo(Prize.THIRD); + assertThat(Prize.valueOf(5, true)).isEqualTo(Prize.SECOND); + assertThat(Prize.valueOf(6, false)).isEqualTo(Prize.FIRST); } } \ No newline at end of file diff --git a/src/test/java/lotto/model/WinningLottoTest.java b/src/test/java/lotto/model/WinningLottoTest.java new file mode 100644 index 0000000000..544e73a76c --- /dev/null +++ b/src/test/java/lotto/model/WinningLottoTest.java @@ -0,0 +1,39 @@ +package lotto.model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +class WinningLottoTest { + @Test + @DisplayName("로또 당첨 번호가 보너스볼의 번호를 포함하면 예외가 발생한다.") + void createWrongWinningLotto() { + assertThatIllegalArgumentException().isThrownBy(() -> new WinningLotto(new Lotto(1, 2, 3, 4, 5, 6), 1)); + } + + @Test + @DisplayName("로또의 당첨 순위 1등을 계산해 반환한다.") + void calculateFirstPrize() { + Lotto lotto = new Lotto(1, 2, 3, 4, 5, 6); + WinningLotto winningLotto = new WinningLotto(new Lotto(1, 2, 3, 4, 5, 6), 45); + assertThat(winningLotto.calculatePrize(lotto)).isEqualTo(Prize.FIRST); + } + + @Test + @DisplayName("로또의 당첨 순위 2등을 계산해 반환한다.") + void calculateSecondPrize() { + Lotto lotto = new Lotto(2, 3, 4, 5, 6, 45); + WinningLotto winningLotto = new WinningLotto(new Lotto(1, 2, 3, 4, 5, 6), 45); + assertThat(winningLotto.calculatePrize(lotto)).isEqualTo(Prize.SECOND); + } + + @Test + @DisplayName("로또 당첨 실패를 계산해 반환한다.") + void calculateMissPrize() { + Lotto lotto = new Lotto(1, 2, 3, 4, 5, 6); + WinningLotto winningLotto = new WinningLotto(new Lotto(38, 39, 40, 41, 42, 43), 45); + assertThat(winningLotto.calculatePrize(lotto)).isEqualTo(Prize.MISS); + } +} \ No newline at end of file