diff --git a/README.md b/README.md index 8a4f22ed0..376174e91 100644 --- a/README.md +++ b/README.md @@ -1 +1,59 @@ # java-planetlotto-precourse + + +## 기본 요구 사항 + +### 로또 구매 + +- [x] 구입금액 입력 프롬프트를 출력한다. + - 메시지: "구입금액을 입력해 주세요." +- [x] 구입금액을 입력받는다. + - [x] `IllegalArgumentException`: 정수가 아닌 경우 + - [x] `IllegalArgumentException`: 500의 배수인 자연수가 아닌 경우 +- [x] 로또 구매 개수를 출력한다. + - 메시지: 개수 + "개를 구매했습니다." +- [x] 구매 개수만큼 로또를 발행한다. + - [x] 각 로또에 대해, 1~30 사이의 중복되지 않는 숫자 5개를 뽑는다. +- [x] 각 로또에 대해 오름차순으로 정렬해, 구매한 로또들의 번호를 출력한다. + - 메시지 예시: "[8, 11, 13, 21, 22]" + +### 로또 추첨 + +- [x] 당첨 번호 입력 프롬프트를 출력한다. + - 메시지: "당첨 번호를 입력해 주세요." +- [x] 당첨 번호를 쉼표를 기준으로 5개 입력받는다. + - [x] `IllegalArgumentException`: 정수가 아닌게 포함된 경우 + - [x] `IllegalArgumentException`: 1~30 사이가 아닌 값이 포함된 경우 + - [x] `IllegalArgumentException`: 중복된 숫자가 포함된 경우 +- [x] 보너스 번호를 입력받는다. + - [x] `IllegalArgumentException`: 정수가 아닌 경우 + - [x] `IllegalArgumentException`: 1~30 사이가 아닌 경우 + - [x] `IllegalArgumentException`: 당첨 번호에 같은 숫자가 포함 경우 +- [x] 당첨을 진행한다. + - [x] 각 로또에 대해, 당첨번호 + 보너스 번호와 비교하여 등수를 계산한다. + - 1등: 5개 번호 일치 / 100,000,000원 + - 2등: 4개 번호 + 보너스 번호 일치 / 10,000,000원 + - 3등: 4개 번호 일치 / 1,500,000원 + - 4등: 3개 번호 일치 + 보너스 번호 일치 / 500,000원 + - 5등: 2개 번호 일치 + 보너스 번호 일치 / 5,000원 + - 로또 구입 금액을 입력하면 구입 금액에 해당하는 만큼 로또를 발행해야 한다. + +### 당첨 통계 + +- [x] 각 등수 별로, 해당되는 개수를 계산한다. +- [x] 1등부터 차례대로, 등수 별 해당되는 개수를 출력한다. + - 메시지: 등수 + "개 일치 (" + String.format("%,d", 상금) + "원) - " + 해당_개수 + "개" + +## 기능 확장 + +### 목표 +사용자 경험 개선 + +### 문제 상황 +- 전체 프로세스에 대한 설명이 부족하다. + - 어떤 식으로 어떻게 진행되는지에 대해, 처음에 설명하도록 한다. + +### 로또 규칙 설명 +- [x] 프로그램 시작 시, 프로그램에 대한 소개를 한다. + - [x] 전체적인 절차를 설명한다. + - [x] 로또의 가격이나 당첨 기준과 같은 규칙을 설명한다. \ No newline at end of file diff --git a/src/main/java/planetlotto/Application.java b/src/main/java/planetlotto/Application.java index 27d0a8f96..92e3b96c4 100644 --- a/src/main/java/planetlotto/Application.java +++ b/src/main/java/planetlotto/Application.java @@ -1,7 +1,11 @@ package planetlotto; +import planetlotto.controller.PlanetLottoController; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + PlanetLottoController planetLottoController = new PlanetLottoController(); + planetLottoController.run(); } } diff --git a/src/main/java/planetlotto/controller/PlanetLottoController.java b/src/main/java/planetlotto/controller/PlanetLottoController.java new file mode 100644 index 000000000..3eb4fdaa8 --- /dev/null +++ b/src/main/java/planetlotto/controller/PlanetLottoController.java @@ -0,0 +1,70 @@ +package planetlotto.controller; + +import java.util.List; +import planetlotto.model.lotto.Lottos; +import planetlotto.model.lotto.BonusNumber; +import planetlotto.model.lotto.Lotto; +import planetlotto.model.winningDetail.WinningDetail; +import planetlotto.view.InputView; +import planetlotto.view.OutputView; + +public class PlanetLottoController { + + public PlanetLottoController() { + } + + public void run() { + Lottos lottos = purchaseLottos(); + OutputView.printPurchasedLottos(lottos.getElementsForOutput()); + + Lotto winningLotto = inputWinningLotto(); + BonusNumber bonusNumber = inputBonusNumber(winningLotto); + + WinningDetail winningDetail = lottos.computeWinningDetailWith(winningLotto, bonusNumber); + + OutputView.printResult(winningDetail.getCountsByRankForOutput()); + } + + private Lottos purchaseLottos() { + Lottos lottos; + + try { + OutputView.printIntroduction(); + int purchaseAmount = InputView.askAmount(); + lottos = new Lottos(purchaseAmount); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e.getMessage()); + lottos = purchaseLottos(); + } + + return lottos; + } + + private Lotto inputWinningLotto() { + Lotto winningLotto; + + try { + List winningNumbers = InputView.askWinningLotto(); + winningLotto = new Lotto(winningNumbers); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e.getMessage()); + winningLotto = inputWinningLotto(); + } + + return winningLotto; + } + + private BonusNumber inputBonusNumber(Lotto winningLotto) { + BonusNumber bonusNumber; + + try { + int bonusNumberInput = InputView.askBonusNumber(); + bonusNumber = new BonusNumber(bonusNumberInput, winningLotto); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e.getMessage()); + bonusNumber = inputBonusNumber(winningLotto); + } + + return bonusNumber; + } +} \ No newline at end of file diff --git a/src/main/java/planetlotto/model/LottoRules.java b/src/main/java/planetlotto/model/LottoRules.java new file mode 100644 index 000000000..7c2d8483a --- /dev/null +++ b/src/main/java/planetlotto/model/LottoRules.java @@ -0,0 +1,9 @@ +package planetlotto.model; + +public class LottoRules { + + public static final int LOTTO_PRICE = 500; + public static final int LOTTO_START_NUMBER = 1; + public static final int LOTTO_END_NUMBER = 30; + public static final int LOTTO_NUMBER_COUNT = 5; +} diff --git a/src/main/java/planetlotto/model/lotto/BonusNumber.java b/src/main/java/planetlotto/model/lotto/BonusNumber.java new file mode 100644 index 000000000..c8a5f8ff8 --- /dev/null +++ b/src/main/java/planetlotto/model/lotto/BonusNumber.java @@ -0,0 +1,30 @@ +package planetlotto.model.lotto; + +import static planetlotto.model.LottoRules.LOTTO_END_NUMBER; +import static planetlotto.model.LottoRules.LOTTO_START_NUMBER; + +import planetlotto.model.LottoRules; + +public class BonusNumber { + + private final int value; + + public BonusNumber(int value, Lotto winningLotto) { + validateValue(value, winningLotto); + this.value = value; + } + + private void validateValue(int value, Lotto winningLotto) { + if (value < LottoRules.LOTTO_START_NUMBER || value > LottoRules.LOTTO_END_NUMBER) { + throw new IllegalArgumentException("보너스 번호는 " + LOTTO_START_NUMBER + "에서 " + LOTTO_END_NUMBER + " 사이여야 합니다."); + } + + if (winningLotto.getNumbers().contains(value)) { + throw new IllegalArgumentException("보너스 번호는 당첨 번호의 모든 숫자와 달라야 합니다."); + } + } + + public int getValue() { + return value; + } +} diff --git a/src/main/java/planetlotto/model/lotto/Lotto.java b/src/main/java/planetlotto/model/lotto/Lotto.java new file mode 100644 index 000000000..cd19b8c47 --- /dev/null +++ b/src/main/java/planetlotto/model/lotto/Lotto.java @@ -0,0 +1,56 @@ +package planetlotto.model.lotto; + +import static planetlotto.model.LottoRules.LOTTO_END_NUMBER; +import static planetlotto.model.LottoRules.LOTTO_NUMBER_COUNT; +import static planetlotto.model.LottoRules.LOTTO_START_NUMBER; + +import java.util.HashSet; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validateNumbers(numbers); + this.numbers = numbers.stream().sorted().toList(); + } + + private void validateNumbers(List numbers) { + if (numbers.size() != LOTTO_NUMBER_COUNT) { + throw new IllegalArgumentException("로또 번호는 " + LOTTO_NUMBER_COUNT + "개여야 합니다."); + } + + if (new HashSet<>(numbers).size() < numbers.size()) { + throw new IllegalArgumentException("중복된 번호가 포함됐습니다."); + } + + numbers.forEach(Lotto::validateNumberRange); + } + + public static void validateNumberRange(int number) { + if (number < LOTTO_START_NUMBER || number > LOTTO_END_NUMBER) { + throw new IllegalArgumentException("로또 번호는 " + LOTTO_START_NUMBER + "에서 " + LOTTO_END_NUMBER + " 사이여야 합니다."); + } + } + + public List getNumbers() { + return numbers; + } + + public int countMatchingNumbersWith(Lotto winningLotto) { + List winningNumbers = winningLotto.getNumbers(); + AtomicInteger count = new AtomicInteger(); + + numbers.stream() + .filter(winningNumbers::contains) + .forEach(other -> count.getAndIncrement()); + + return count.get(); + } + + public boolean isMatchedWith(BonusNumber bonusNumber) { + return numbers.contains(bonusNumber.getValue()); + } +} + diff --git a/src/main/java/planetlotto/model/lotto/Lottos.java b/src/main/java/planetlotto/model/lotto/Lottos.java new file mode 100644 index 000000000..b47ee37ca --- /dev/null +++ b/src/main/java/planetlotto/model/lotto/Lottos.java @@ -0,0 +1,62 @@ +package planetlotto.model.lotto; + +import static planetlotto.model.LottoRules.LOTTO_END_NUMBER; +import static planetlotto.model.LottoRules.LOTTO_NUMBER_COUNT; +import static planetlotto.model.LottoRules.LOTTO_PRICE; +import static planetlotto.model.LottoRules.LOTTO_START_NUMBER; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.List; +import planetlotto.model.winningDetail.WinningDetail; + +public class Lottos { + private final int purchaseAmount; + private final List elements; + + public Lottos(int purchaseAmount) { + validatePurchaseAmount(purchaseAmount); + + this.purchaseAmount = purchaseAmount; + this.elements = createElements(); + } + + public Lottos(List elements) { + this.elements = elements; + this.purchaseAmount = elements.size() * LOTTO_PRICE; + } + + private void validatePurchaseAmount(int purchaseAmount) { + if ((purchaseAmount < LOTTO_PRICE) || ((purchaseAmount % LOTTO_PRICE) != 0)) { + throw new IllegalArgumentException("구입금액은 " + LOTTO_PRICE + "원 단위여야 합니다: " + purchaseAmount); + } + } + + private List createElements() { + List elements = new ArrayList<>(); + int count = purchaseAmount / LOTTO_PRICE; + + for (int i = 0; i < count; i++) { + elements.add(new Lotto( + Randoms.pickUniqueNumbersInRange(LOTTO_START_NUMBER, LOTTO_END_NUMBER, LOTTO_NUMBER_COUNT)) + ); + } + + return elements; + } + + public List getElements() { + return elements; + } + + public List> getElementsForOutput() { + return elements.stream() + .map(Lotto::getNumbers) + .toList(); + } + + public WinningDetail computeWinningDetailWith(Lotto winningLotto, BonusNumber bonusNumber) { + return new WinningDetail(this, winningLotto, bonusNumber); + } + +} diff --git a/src/main/java/planetlotto/model/winningDetail/Rank.java b/src/main/java/planetlotto/model/winningDetail/Rank.java new file mode 100644 index 000000000..87c237dcc --- /dev/null +++ b/src/main/java/planetlotto/model/winningDetail/Rank.java @@ -0,0 +1,46 @@ +package planetlotto.model.winningDetail; + +public enum Rank { + FIRST(1,5, false, 100000000), + SECOND(2,4, true, 10000000), + THIRD(3,4, false, 1500000), + FIRTH(4, 3, false, 500000), + FIFTH(5, 2, false, 5000), + NONE(0, 0, false, 0); + + private final int order; + private final int matchingCount; + private final boolean isMatchedWithBonusNumber; + private final int prize; + + Rank(int order, int matchingCount, boolean isMatchedWithBonusNumber, int prize) { + this.order = order; + this.matchingCount = matchingCount; + this.isMatchedWithBonusNumber = isMatchedWithBonusNumber; + this.prize = prize; + } + + public int getOrder() { + return order; + } + + public int getPrize() { + return prize; + } + + public static Rank of(int matchingCount, boolean isMatchedWithBonusNumber) { + if (matchingCount == 5) { + return FIRST; + } else if (matchingCount == 4 && isMatchedWithBonusNumber) { + return SECOND; + } else if (matchingCount == 4) { + return THIRD; + } else if (matchingCount == 3) { + return FIRTH; + } else if (matchingCount == 2) { + return FIFTH; + } + + return NONE; + } +} diff --git a/src/main/java/planetlotto/model/winningDetail/WinningDetail.java b/src/main/java/planetlotto/model/winningDetail/WinningDetail.java new file mode 100644 index 000000000..4f7e92444 --- /dev/null +++ b/src/main/java/planetlotto/model/winningDetail/WinningDetail.java @@ -0,0 +1,51 @@ +package planetlotto.model.winningDetail; + +import java.util.Collections; +import java.util.Map; +import java.util.TreeMap; +import planetlotto.model.lotto.BonusNumber; +import planetlotto.model.lotto.Lotto; +import planetlotto.model.lotto.Lottos; + +public class WinningDetail { + + private final Lottos purchasedLottos; + private final Lotto winningLotto; + private final BonusNumber bonusNumber; + private final Map countsByRank; + + public WinningDetail(Lottos purchasedLottos, + Lotto winningLotto, + BonusNumber bonusNumber) { + this.purchasedLottos = purchasedLottos; + this.winningLotto = winningLotto; + this.bonusNumber = bonusNumber; + this.countsByRank = createCountsByRank(); + } + + private Map createCountsByRank() { + Map countsByRank = new TreeMap<>(Collections.reverseOrder()); + + purchasedLottos.getElements().forEach(lotto -> { + Rank rank = Rank.of( + lotto.countMatchingNumbersWith(winningLotto), + lotto.isMatchedWith(bonusNumber) + ); + + countsByRank.put(rank, countsByRank.getOrDefault(rank, 0) + 1); + }); + + return countsByRank; + } + + public Map getCountsByRank() { + return countsByRank; + } + + public Map getCountsByRankForOutput() { + Map rankCountsForOutput = new TreeMap<>(Collections.reverseOrder()); + countsByRank.forEach((rank, count) -> rankCountsForOutput.put(rank.getOrder(),count)); + return rankCountsForOutput; + } +} + diff --git a/src/main/java/planetlotto/view/InputView.java b/src/main/java/planetlotto/view/InputView.java index c1eb55343..f5029d8cf 100644 --- a/src/main/java/planetlotto/view/InputView.java +++ b/src/main/java/planetlotto/view/InputView.java @@ -5,7 +5,6 @@ import java.util.Arrays; import java.util.List; import java.util.function.Predicate; -import java.util.stream.Collectors; public class InputView { public static int askAmount() { diff --git a/src/main/java/planetlotto/view/OutputView.java b/src/main/java/planetlotto/view/OutputView.java index 0452898da..8b2529173 100644 --- a/src/main/java/planetlotto/view/OutputView.java +++ b/src/main/java/planetlotto/view/OutputView.java @@ -7,6 +7,58 @@ import static java.lang.System.lineSeparator; public class OutputView { + + private static final String NEW_LINE = "\n"; + + public static void printIntroduction() { + System.out.println("행성 로또가 시작되었습니다!" + NEW_LINE); + printSteps(); + System.out.print(NEW_LINE); + printRules(); + System.out.println("=================================================================================="); + System.out.println("===================================================================================" + NEW_LINE); + } + + private static void printSteps() { + System.out.println("[절차]"); + System.out.println("1. 구입 금액 입력 (500원 단위)"); + System.out.println("2. 구입한 로또 수량 및 번호 출력"); + System.out.println("3. 당첨 번호 입력 (1에서 30 사이의 서로 다른 숫자 5개)"); + System.out.println("4. 보너스 번호 입력 (당첨 번호와 다른 1에서 30 사이의 숫자)"); + System.out.println("5. 당첨 통계 출력"); + System.out.println("6. 재진행 여부 입력 (Y 또는 N)"); + } + + public static void printRules() { + System.out.println("[규칙]"); + System.out.println("1. 로또의 가격은 500원 입니다."); + System.out.println("2. 로또는 1에서 30 사이의 서로 다른 숫자 5개를 순서 없이 가집니다."); + System.out.println("3. 당첨 번호는 1에서 30 사이의 서로 다른 숫자 5개로 구성되야 합니다. 이 때 숫자의 순서는 상관 없습니다."); + System.out.println("4. 보너스 번호는 당첨 번호에 포함되지 않은 1에서 30 사이의 숫자여야 합니다."); + } + + public static void printPreviousProfitRate(List histories) { + if (!histories.isEmpty()) { + double totalProfitRate = calculateTotalProfitRate(histories); + System.out.println("이전 구매에서의 평균 수익률은 " + totalProfitRate + "%입니다."); + return; + } + + System.out.println("이전 구매에서의 평균 수익률에 대한 정보가 존재하지 않습니다."); + } + + private static double calculateTotalProfitRate(List histories) { + double sum = 0; + for (Double history : histories) { + sum += history; + } + + double totalProfitRate = sum / histories.size(); + totalProfitRate = Math.round(totalProfitRate * 10) / 10.0; + + return totalProfitRate; + } + public static void printPurchasedLottos(final List> lottos) { final String header = String.format("%d개를 구매했습니다.", lottos.size()); final String output = lottos.stream() diff --git a/src/test/java/planetlotto/model/lotto/BonusNumberTest.java b/src/test/java/planetlotto/model/lotto/BonusNumberTest.java new file mode 100644 index 000000000..76f4cdf6b --- /dev/null +++ b/src/test/java/planetlotto/model/lotto/BonusNumberTest.java @@ -0,0 +1,26 @@ +package planetlotto.model.lotto; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class BonusNumberTest { + + @DisplayName("당첨 번호에 포함되지 않는 1에서 30 사이의 숫자가 아닌 경우 예외를 발생시킨다") + @Test + void BonusNumber() { + //given + Lotto winningLotto = new Lotto(List.of(1,2,3,4,5)); + List wrongs = List.of(1, 31); + + //when && then + wrongs.forEach(wrong -> { + Assertions.assertThatThrownBy(() -> new BonusNumber(wrong, winningLotto)) + .isInstanceOf(IllegalArgumentException.class); + }); + + } +} \ No newline at end of file diff --git a/src/test/java/planetlotto/model/lotto/LottoTest.java b/src/test/java/planetlotto/model/lotto/LottoTest.java new file mode 100644 index 000000000..3724937a2 --- /dev/null +++ b/src/test/java/planetlotto/model/lotto/LottoTest.java @@ -0,0 +1,58 @@ +package planetlotto.model.lotto; + +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; + +class LottoTest { + + @DisplayName("로또_번호의_개수가_5개가_넘어가면_예외가_발생한다") + @Test + void 로또_번호의_개수가_5개가_넘어가면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") + @Test + void 로또_번호에_중복된_숫자가_있으면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 4))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호에 1에서 30 사이가 아닌 값이 포함되면 예외가 발생한다.") + @Test + void 로또_번호에_1에서_30_사이가_아닌_숫자가_있으면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 31))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("당첨번호와 일치하는 번호의 개수를 반환한다.") + @Test + void countMatchingNumbersWith() { + //given + Lotto winningLotto = new Lotto(List.of(1,2,3,4,5)); + + List lottos = List.of( + new Lotto(List.of(1,2,3,4,5)), // 5 개 일치 + new Lotto(List.of(1,2,3,4,7)), // 4 개 일치 + new Lotto(List.of(1, 2, 3, 7, 8)) // 3 개 일치 + ); + + //when && then + assertThat(lottos.get(0).countMatchingNumbersWith(winningLotto)).isEqualTo(5); + assertThat(lottos.get(1).countMatchingNumbersWith(winningLotto)).isEqualTo(4); + assertThat(lottos.get(2).countMatchingNumbersWith(winningLotto)).isEqualTo(3); + } + + @DisplayName("보너스 번호와의 일치 여부를 반환한다.") + @Test + void isMatchedWith() { + assertThat(new Lotto(List.of(1,2,3,4,5)) + .isMatchedWith(new BonusNumber(5, new Lotto(List.of(6,7,8,9,10))))) + .isTrue(); + } +} \ No newline at end of file diff --git a/src/test/java/planetlotto/model/lotto/LottosTest.java b/src/test/java/planetlotto/model/lotto/LottosTest.java new file mode 100644 index 000000000..f4ae8c1e1 --- /dev/null +++ b/src/test/java/planetlotto/model/lotto/LottosTest.java @@ -0,0 +1,45 @@ +package planetlotto.model.lotto; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.List; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class LottosTest { + + @DisplayName("구입 금액이 500원 단위가 아니면 예외가 발생한다.") + @Test + void validatePurchaseAmount() { + assertThatThrownBy(() -> new Lottos(100)) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("구입한 로또를 발행한다.") + @Test + void Lottos() { + //given + Lottos lottos = new Lottos(1000); + + //when && then + Assertions.assertThat(lottos.getElements().size()).isEqualTo(2); + } + + @DisplayName("구입한 로또들의 번호들을 List> 타입으로 반환한다.") + @Test + void getElementsForOutput() { + //given + Lottos lottos = new Lottos(List.of( + new Lotto(List.of(1,2,3,4,5)), + new Lotto(List.of(6,7,8,9,10)) + )); + + //when && then + assertThat(lottos.getElementsForOutput()).contains( + List.of(1,2,3,4,5), + List.of(6,7,8,9,10) + ); + } +} \ No newline at end of file diff --git a/src/test/java/planetlotto/model/winningDetail/RankTest.java b/src/test/java/planetlotto/model/winningDetail/RankTest.java new file mode 100644 index 000000000..200832aea --- /dev/null +++ b/src/test/java/planetlotto/model/winningDetail/RankTest.java @@ -0,0 +1,46 @@ +package planetlotto.model.winningDetail; + +import static org.assertj.core.api.Assertions.assertThat; +import static planetlotto.model.winningDetail.Rank.FIFTH; +import static planetlotto.model.winningDetail.Rank.FIRST; +import static planetlotto.model.winningDetail.Rank.FIRTH; +import static planetlotto.model.winningDetail.Rank.NONE; +import static planetlotto.model.winningDetail.Rank.SECOND; +import static planetlotto.model.winningDetail.Rank.THIRD; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class RankTest { + + @DisplayName("당첨번호와_보너스번호와_비교해_로또의_등수를_판단한다") + @Test + void of() { + //given + List matchingCounts = List.of( + 5, 5, 4, 4, 3, 3, 2, 2, 1, 0 + ); + + List isMatchedList = List.of( + true, false, true, false, true, false, true, false, false, false + ); + + //when + List ranks = new ArrayList<>(); + + for (int i = 0; i < matchingCounts.size(); i++) { + ranks.add(Rank.of(matchingCounts.get(i), isMatchedList.get(i))); + } + + //then + assertThat(ranks).containsExactly( + FIRST, FIRST, + SECOND, THIRD, + FIRTH, FIRTH, + FIFTH, FIFTH, + NONE, NONE + ); + } +} \ No newline at end of file diff --git a/src/test/java/planetlotto/model/winningDetail/WinningDetailTest.java b/src/test/java/planetlotto/model/winningDetail/WinningDetailTest.java new file mode 100644 index 000000000..25b9eafd0 --- /dev/null +++ b/src/test/java/planetlotto/model/winningDetail/WinningDetailTest.java @@ -0,0 +1,66 @@ +package planetlotto.model.winningDetail; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import planetlotto.model.lotto.BonusNumber; +import planetlotto.model.lotto.Lotto; +import planetlotto.model.lotto.Lottos; + +class WinningDetailTest { + + @DisplayName("각 등수 별 개수를 센다.") + @Test + void WinningDetail() { + //given + Lottos lottos = new Lottos(List.of( + new Lotto(List.of(1,2,3,4,5)), //1등 + new Lotto(List.of(1,2,3,4,6)), //2등 + new Lotto(List.of(1,2,3,4,7)), //3등 + new Lotto(List.of(1, 2, 3, 7, 8)), //4등 + new Lotto(List.of(1, 2, 6, 7, 8)), //5등 + new Lotto(List.of(6,7,8,9,10)) //등외 + )); + + Lotto winningLotto = new Lotto(List.of(1,2,3,4,5)); + BonusNumber bonusNumber = new BonusNumber(6, winningLotto); + + //when + WinningDetail winningDetail = new WinningDetail(lottos, winningLotto, bonusNumber); + + //then + winningDetail.getCountsByRank().values().forEach(count -> + assertThat(count).isEqualTo(1) + ); + } + + @DisplayName("각 등수와 해당 등수의 숫자를 정수 형태로 담아 반환한다.") + @Test + void getCountsByRankForOutput() { + //given + Lottos lottos = new Lottos(List.of( + new Lotto(List.of(1,2,3,4,5)), //1등 + new Lotto(List.of(1,2,3,4,7)), //3등 + new Lotto(List.of(1, 2, 6, 7, 8)), //5등 + new Lotto(List.of(6,7,8,9,10)) //등외 + )); + + Lotto winningLotto = new Lotto(List.of(1,2,3,4,5)); + BonusNumber bonusNumber = new BonusNumber(6, winningLotto); + + WinningDetail winningDetail = new WinningDetail(lottos, winningLotto, bonusNumber); + + //when + Map countsByRankForOutput = winningDetail.getCountsByRankForOutput(); + + //then + assertThat(countsByRankForOutput.get(1)).isEqualTo(1); + assertThat(countsByRankForOutput.get(3)).isEqualTo(1); + assertThat(countsByRankForOutput.get(5)).isEqualTo(1); + } +} \ No newline at end of file