diff --git a/src/main/java/auto/AutoLotteryApplication.java b/src/main/java/auto/AutoLotteryApplication.java deleted file mode 100644 index 6cd91417062..00000000000 --- a/src/main/java/auto/AutoLotteryApplication.java +++ /dev/null @@ -1,27 +0,0 @@ -package auto; - -import auto.application.AutoLotteryService; -import auto.application.MatchLotteryService; -import auto.view.InputView; -import auto.view.ResultView; - -import java.util.List; -import java.util.Map; - -public class AutoLotteryApplication { - private static final AutoLotteryService autoLotteryService = new AutoLotteryService(); - private static final MatchLotteryService matchLotteryService = new MatchLotteryService(); - - public static void main(String[] args) { - int purchaseAmount = InputView.inputPurchaseAmount(); - int lotteryCount = autoLotteryService.getLotteryCount(purchaseAmount); - ResultView.printLotteryCounts(lotteryCount); - - List> totalLotteryNumbers = autoLotteryService.createLotteryNumbersList(lotteryCount); - ResultView.printTotalLotteryNumbers(totalLotteryNumbers); - - List winningNumbersLastWeek = InputView.inputWinningNumbersLastWeek(); - Map matchedCountMap = matchLotteryService.getMatchedCountMap(totalLotteryNumbers, winningNumbersLastWeek); - ResultView.printLotteryStats(matchedCountMap, purchaseAmount); - } -} diff --git a/src/main/java/auto/AutoLottoApplication.java b/src/main/java/auto/AutoLottoApplication.java new file mode 100644 index 00000000000..3abbfcc1b7d --- /dev/null +++ b/src/main/java/auto/AutoLottoApplication.java @@ -0,0 +1,33 @@ +package auto; + +import auto.application.AutoLottoService; +import auto.application.MatchLottoService; +import auto.application.MatchedAmount; +import auto.domain.Lotto; +import auto.domain.Lottos; +import auto.view.InputView; +import auto.view.ResultView; + +import java.util.Map; + +public class AutoLottoApplication { + private static final AutoLottoService autoService = new AutoLottoService(); + private static final MatchLottoService matchService = new MatchLottoService(); + + public static void main(String[] args) { + int purchaseAmount = InputView.inputPurchaseAmount(); + int lottoCount = autoService.getLottoCount(purchaseAmount); + ResultView.printLottoCounts(lottoCount); + + Lottos totalLottoNumbers = Lottos.createLottoNumbersList(lottoCount); + ResultView.printTotalLottoNumbers(totalLottoNumbers); + + Lotto winningNumbersLastWeek = InputView.inputWinningNumbersLastWeek(); + int bonusBallNumber = InputView.inputBonusBallNumber(winningNumbersLastWeek); + + Map matchedCountMap = matchService.getMatchedCountMap(totalLottoNumbers, + winningNumbersLastWeek, + bonusBallNumber); + ResultView.printLottoStats(matchedCountMap, purchaseAmount); + } +} diff --git a/src/main/java/auto/application/AutoLotteryService.java b/src/main/java/auto/application/AutoLotteryService.java deleted file mode 100644 index 555387874f1..00000000000 --- a/src/main/java/auto/application/AutoLotteryService.java +++ /dev/null @@ -1,25 +0,0 @@ -package auto.application; - -import auto.domain.AutoLotteryRepositoryImpl; -import auto.infrastructure.AutoLotteryRepository; - -import java.math.BigDecimal; -import java.util.List; - -public class AutoLotteryService { - private static final int LOTTERY_PRICE = 1000; - private static final AutoLotteryRepository autoLotteryRepository = new AutoLotteryRepositoryImpl(); - - public List> createLotteryNumbersList(int lotteryCount) { - return autoLotteryRepository.createLotteryNumbersList(lotteryCount); - } - - public int getLotteryCount(int purchaseAmount) { - return purchaseAmount / LOTTERY_PRICE; - } - - public static BigDecimal getReturnRate(int totalAmount, int amount) { - return BigDecimal.valueOf(totalAmount) - .divide(BigDecimal.valueOf(amount)); - } -} diff --git a/src/main/java/auto/application/AutoLottoService.java b/src/main/java/auto/application/AutoLottoService.java new file mode 100644 index 00000000000..0f935c4d426 --- /dev/null +++ b/src/main/java/auto/application/AutoLottoService.java @@ -0,0 +1,16 @@ +package auto.application; + +import java.math.BigDecimal; + +public class AutoLottoService { + private static final int LOTTO_PRICE = 1000; + + public int getLottoCount(int purchaseAmount) { + return purchaseAmount / LOTTO_PRICE; + } + + public static BigDecimal getReturnRate(int totalAmount, int amount) { + return BigDecimal.valueOf(totalAmount) + .divide(BigDecimal.valueOf(amount)); + } +} diff --git a/src/main/java/auto/application/MatchLotteryService.java b/src/main/java/auto/application/MatchLotteryService.java deleted file mode 100644 index 8bf325803cd..00000000000 --- a/src/main/java/auto/application/MatchLotteryService.java +++ /dev/null @@ -1,37 +0,0 @@ -package auto.application; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentMap; - -import static auto.application.MatchedAmount.findByCount; - -public class MatchLotteryService { - private static final ConcurrentMap matchedCountMap = MatchedAmount.getMatchedCountMap(); - - public Map getMatchedCountMap(List> lotteryNumbersList, - List winningNumbersLastWeek) { - - for (List lotteryNumbers : lotteryNumbersList) { - setMatchedCountMap(lotteryNumbers, winningNumbersLastWeek); - } - return matchedCountMap; - } - - private void setMatchedCountMap(List lotteryNumbers, List winningNumbersLastWeek) { - int matchedCountSum = lotteryNumbers.stream() - .mapToInt(number -> getMatchCount(winningNumbersLastWeek, number)) - .sum(); - int matchedCount = findByCount(matchedCountSum).getCount(); - if (matchedCount > 0) { - matchedCountMap.put(matchedCount, matchedCountMap.get(matchedCount) + 1); - } - } - - private int getMatchCount(List winningNumbersLastWeek, Integer number) { - if (winningNumbersLastWeek.contains(number)) { - return 1; - } - return 0; - } -} diff --git a/src/main/java/auto/application/MatchLottoService.java b/src/main/java/auto/application/MatchLottoService.java new file mode 100644 index 00000000000..650035ba986 --- /dev/null +++ b/src/main/java/auto/application/MatchLottoService.java @@ -0,0 +1,59 @@ +package auto.application; + +import auto.domain.Lotto; +import auto.domain.Lottos; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static auto.application.MatchedAmount.NONE; +import static auto.application.MatchedAmount.findByCount; + +public class MatchLottoService { + private static final Map matchedCountMap = new HashMap<>();// = MatchedAmount.getMatchedCountMap(); + + private static void setUpWinningStatisticsMap() { + EnumSet.allOf(MatchedAmount.class) + .stream() + .filter(matchedAmount -> matchedAmount != NONE) + .sorted() + .forEach(matchedAmount -> matchedCountMap.put(matchedAmount, 0)); + } + + static boolean isMatchedBonusNumber(List lottoNumbers, + List winningNumbersLastWeek, + int bonusBallNumber) { + List copyLottoNumbers = new ArrayList<>(lottoNumbers); + copyLottoNumbers.removeAll(winningNumbersLastWeek); + return copyLottoNumbers.contains(bonusBallNumber); + } + + public Map getMatchedCountMap(Lottos lottoNumbersList, + Lotto winningNumbersLastWeek, + int bonusBallNumber) { + setUpWinningStatisticsMap(); + for (Lotto lotto : lottoNumbersList.getLottoList()) { + setMatchedCountMap(lotto.getNumbers(), + winningNumbersLastWeek.getNumbers(), + isMatchedBonusNumber(lotto.getNumbers(), + winningNumbersLastWeek.getNumbers(), + bonusBallNumber)); + } + return matchedCountMap; + } + + private void setMatchedCountMap(List lottoNumbers, + List winningNumbersLastWeek, + boolean isMatchedBonusNumber) { + int matchedCountSum = (int) lottoNumbers.stream() + .filter(winningNumbersLastWeek::contains) + .count(); + MatchedAmount matchedAmount = findByCount(matchedCountSum, isMatchedBonusNumber); + if (matchedAmount.getCount() > 0) { + matchedCountMap.put(matchedAmount, matchedCountMap.get(matchedAmount) + 1); + } + } +} diff --git a/src/main/java/auto/application/MatchedAmount.java b/src/main/java/auto/application/MatchedAmount.java index 47782bc1532..aa70d8c2c75 100644 --- a/src/main/java/auto/application/MatchedAmount.java +++ b/src/main/java/auto/application/MatchedAmount.java @@ -10,6 +10,7 @@ public enum MatchedAmount { THREE(3, 5_000), FOUR(4, 50_000), FIVE(5, 1_500_000), + FIVE_AND_BONUS(5, 30_000_000), SIX(6, 2_000_000_000), NONE(0, 0); @@ -28,7 +29,10 @@ public enum MatchedAmount { this.amount = amount; } - public static MatchedAmount findByCount(int count) { + public static MatchedAmount findByCount(int count, boolean isMatchedBonus) { // findByNumberWithMatchedBonus + if (count == FIVE.getCount()) { + return isMatchedBonus ? FIVE_AND_BONUS : FIVE; + } if (!MATCHED_AMOUNT_MAP.containsKey(count)) { return NONE; } @@ -42,6 +46,10 @@ public static ConcurrentMap getMatchedCountMap() { .collect(Collectors.toConcurrentMap(MatchedAmount::getCount, u -> 0, (u, v) -> u)); } + public static boolean isMatchedNumberFiveAndBonus(MatchedAmount matchedAmount) { + return FIVE_AND_BONUS == matchedAmount; + } + public int getCount() { return count; } diff --git a/src/main/java/auto/domain/AutoLotteryRepositoryImpl.java b/src/main/java/auto/domain/AutoLotteryRepositoryImpl.java deleted file mode 100644 index dc6dcc0cf33..00000000000 --- a/src/main/java/auto/domain/AutoLotteryRepositoryImpl.java +++ /dev/null @@ -1,33 +0,0 @@ -package auto.domain; - -import auto.infrastructure.AutoLotteryRepository; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -public class AutoLotteryRepositoryImpl implements AutoLotteryRepository { - private final static int LOTTERY_MIN_NUMBER = 1; - private final static int LOTTERY_MAX_NUMBER = 45; - private static List numbers = new ArrayList<>(); - - static { - numbers = IntStream.range(LOTTERY_MIN_NUMBER, LOTTERY_MAX_NUMBER + 1) - .boxed() - .collect(Collectors.toList()); - } - - @Override - public List> createLotteryNumbersList(int lotteryCount) { - return IntStream.range(0, lotteryCount) - .mapToObj(i -> createLotteryNumbers()) - .collect(Collectors.toCollection(ArrayList::new)); - } - - private List createLotteryNumbers() { - Collections.shuffle(numbers); - return numbers.subList(0, 6); - } -} diff --git a/src/main/java/auto/domain/Lotto.java b/src/main/java/auto/domain/Lotto.java new file mode 100644 index 00000000000..28acbb8d42f --- /dev/null +++ b/src/main/java/auto/domain/Lotto.java @@ -0,0 +1,38 @@ +package auto.domain; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Lotto { + public final static int LOTTO_MIN_NUMBER = 1; + public final static int LOTTO_MAX_NUMBER = 45; + private static final String LOTTO_NUMBER_SIZE_ERROR_TEXT = "로또 번호는 6개의 숫자로 이루어져야 합니다."; + private static final int MAXIMUM_LOTTO_NUMBER_SIZE = 6; + private final List numbers; + + public Lotto(List numbers) { + if (numbers == null || numbers.size() != MAXIMUM_LOTTO_NUMBER_SIZE) { + throw new IllegalArgumentException(LOTTO_NUMBER_SIZE_ERROR_TEXT); + } + this.numbers = numbers; + } + + public List getNumbers() { + return Collections.unmodifiableList(numbers); + } + + @Override + public String toString() { + return "" + numbers; + } + + public static Lotto createLottoNumbers() { + List numbers = IntStream.range(LOTTO_MIN_NUMBER, LOTTO_MAX_NUMBER + 1) + .boxed() + .collect(Collectors.toList()); + Collections.shuffle(numbers); + return new Lotto(numbers.subList(0, 6)); + } +} diff --git a/src/main/java/auto/domain/Lottos.java b/src/main/java/auto/domain/Lottos.java new file mode 100644 index 00000000000..c04804dd783 --- /dev/null +++ b/src/main/java/auto/domain/Lottos.java @@ -0,0 +1,24 @@ +package auto.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Lottos { + private final List lottoList; + + public Lottos(List lottoList) { + this.lottoList = lottoList; + } + + public static Lottos createLottoNumbersList(int lottoCount) { + return new Lottos(IntStream.range(0, lottoCount) + .mapToObj(i -> Lotto.createLottoNumbers()) + .collect(Collectors.toCollection(ArrayList::new))); + } + + public List getLottoList() { + return lottoList; + } +} diff --git a/src/main/java/auto/infrastructure/AutoLotteryRepository.java b/src/main/java/auto/infrastructure/AutoLotteryRepository.java deleted file mode 100644 index 947cefc114c..00000000000 --- a/src/main/java/auto/infrastructure/AutoLotteryRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package auto.infrastructure; - -import java.util.List; - -public interface AutoLotteryRepository { - List> createLotteryNumbersList(int lotteryCount); -} diff --git a/src/main/java/auto/view/InputView.java b/src/main/java/auto/view/InputView.java index 8b94b9f4852..fce1eebcbc8 100644 --- a/src/main/java/auto/view/InputView.java +++ b/src/main/java/auto/view/InputView.java @@ -1,23 +1,45 @@ package auto.view; +import auto.domain.Lotto; + import java.util.Arrays; -import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; +import static auto.domain.Lotto.LOTTO_MAX_NUMBER; +import static auto.domain.Lotto.LOTTO_MIN_NUMBER; + public class InputView { private static final Scanner scanner = new Scanner(System.in); + private static final String EMPTY_BONUS_BALL_MESSAGE = "보너스 볼이 비어있습니다."; + private static final String IN_LOTTO_BONUS_BALL_MESSAGE = "로또 번호에 포함된 보너스 볼입니다."; + private static final String NOT_RANGE_LOTTO_NUMBER_MESSAGE = "번호는 1 - 45까지 입력할 수 있습니다."; public static int inputPurchaseAmount() { System.out.println("구입금액을 입력해 주세요."); return Integer.parseInt(scanner.nextLine()); } - public static List inputWinningNumbersLastWeek() { + public static Lotto inputWinningNumbersLastWeek() { System.out.println("지난 주 당첨 번호를 입력해 주세요."); String winningNumbersLastWeek = scanner.nextLine(); - return Arrays.stream(winningNumbersLastWeek.split(", ")) - .map(Integer::parseInt) - .collect(Collectors.toList()); + return new Lotto(Arrays.stream(winningNumbersLastWeek.split(", ")) + .map(Integer::parseInt) + .collect(Collectors.toList())); + } + + public static int inputBonusBallNumber(Lotto lastWeekWinningNumber) { + System.out.println(EMPTY_BONUS_BALL_MESSAGE); + int bonusNumber = Integer.parseInt(scanner.nextLine()); + if (lastWeekWinningNumber.getNumbers() + .contains(bonusNumber)) + throw new IllegalArgumentException(IN_LOTTO_BONUS_BALL_MESSAGE); + if (isNotInLottoNumberRange(bonusNumber)) + throw new IllegalArgumentException(NOT_RANGE_LOTTO_NUMBER_MESSAGE); + return bonusNumber; + } + + private static boolean isNotInLottoNumberRange(int bonusNumber) { + return !(bonusNumber >= LOTTO_MIN_NUMBER && bonusNumber <= LOTTO_MAX_NUMBER); } } diff --git a/src/main/java/auto/view/ResultView.java b/src/main/java/auto/view/ResultView.java index 7492b3063f6..a296d1074f2 100644 --- a/src/main/java/auto/view/ResultView.java +++ b/src/main/java/auto/view/ResultView.java @@ -1,29 +1,31 @@ package auto.view; -import auto.application.AutoLotteryService; +import auto.application.AutoLottoService; import auto.application.MatchedAmount; +import auto.domain.Lottos; import java.math.BigDecimal; -import java.util.List; import java.util.Map; public class ResultView { - private static final String WINNING_COUNT_MESSAGE = "%s개 일치 (%s원)- %s개"; + private static final String WINNING_COUNT_MESSAGE = "%s개 일치%s(%s원)- %s개"; + private static final String BONUS_BALL_TEXT = ", 보너스 볼 일치"; - public static void printLotteryCounts(int count) { + public static void printLottoCounts(int count) { System.out.println(count + "개를 구매했습니다."); } - public static void printTotalLotteryNumbers(List> totalLotteryNumbers) { - totalLotteryNumbers.forEach(System.out::println); + public static void printTotalLottoNumbers(Lottos totalLottoNumbers) { + totalLottoNumbers.getLottoList() + .forEach(System.out::println); } - public static void printLotteryStats(Map matchedCountMap, int amount) { + public static void printLottoStats(Map matchedCountMap, int amount) { System.out.println("당첨 통계"); System.out.println("---------"); int totalAmount = getTotalAmountAndPrintMatchedText(matchedCountMap); - BigDecimal returnRate = AutoLotteryService.getReturnRate(totalAmount, amount); + BigDecimal returnRate = AutoLottoService.getReturnRate(totalAmount, amount); System.out.println(getTotalReturnRateMessage(returnRate)); } @@ -33,21 +35,22 @@ private static String getTotalReturnRateMessage(BigDecimal returnRate) { return totalReturnRateMessage; } - public static int getTotalAmountAndPrintMatchedText(Map matchedCountMap) { + public static int getTotalAmountAndPrintMatchedText(Map matchedCountMap) { int totalAmount = 0; - for (Integer matchedCount : matchedCountMap.keySet()) { - MatchedAmount matchedAmount = MatchedAmount.findByCount(matchedCount); - int winningCount = matchedCountMap.get(matchedCount); - printMatchedCountText(matchedCount, matchedAmount, winningCount); + for (MatchedAmount matchedAmount : matchedCountMap.keySet()) { + int winningCount = matchedCountMap.get(matchedAmount); + System.out.println(String.format(WINNING_COUNT_MESSAGE, + matchedAmount.getCount(), + getBonusBallText(matchedAmount), + matchedAmount.getAmount(), + winningCount)); + totalAmount += matchedAmount.getAmount() * winningCount; } return totalAmount; } - private static void printMatchedCountText(Integer matchedCount, MatchedAmount matchedAmount, int winningCount) { - System.out.println(String.format(WINNING_COUNT_MESSAGE, - matchedCount, - matchedAmount.getAmount(), - winningCount)); + static String getBonusBallText(MatchedAmount matchedAmount) { + return MatchedAmount.isMatchedNumberFiveAndBonus(matchedAmount) ? BONUS_BALL_TEXT : " "; } } diff --git a/src/test/java/auto/application/AutoLotteryServiceTest.java b/src/test/java/auto/application/AutoLottoServiceTest.java similarity index 52% rename from src/test/java/auto/application/AutoLotteryServiceTest.java rename to src/test/java/auto/application/AutoLottoServiceTest.java index 741f2cf8c9d..c6982f57b25 100644 --- a/src/test/java/auto/application/AutoLotteryServiceTest.java +++ b/src/test/java/auto/application/AutoLottoServiceTest.java @@ -1,5 +1,7 @@ package auto.application; +import auto.domain.Lotto; +import auto.domain.Lottos; import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -11,14 +13,14 @@ import static org.assertj.core.api.Assertions.assertThat; -class AutoLotteryServiceTest { - private final AutoLotteryService autoLotteryService = new AutoLotteryService(); +class AutoLottoServiceTest { + private final AutoLottoService autoLottoService = new AutoLottoService(); @ParameterizedTest @CsvSource(value = {"0, 0", "1000, 1", "1100,1"}) - void getLotteryCountTest(int purchaseAmount, int expected) { + void getLottoCountTest(int purchaseAmount, int expected) { // when - int lotteryCount = autoLotteryService.getLotteryCount(purchaseAmount); + int lotteryCount = autoLottoService.getLottoCount(purchaseAmount); // then assertThat(lotteryCount).isEqualTo(expected); } @@ -26,25 +28,26 @@ void getLotteryCountTest(int purchaseAmount, int expected) { @Test @DisplayName("설정한 lotteryCount 만큼 로또 리스트 갯수가 만들어지고" + "각 로또 번호는 1 ~ 45 사이이다.") - void createLotteryNumbersListTest() { + void createLottoNumbersListTest() { // given int lotteryCount = 5; int MIN_LOTTERY_NUMBER = 1; int MAX_LOTTERY_NUMBER = 45; // when - List> lotteryNumbersList = autoLotteryService.createLotteryNumbersList(lotteryCount); + Lottos lotteryNumbersList = Lottos.createLottoNumbersList(lotteryCount); // then SoftAssertions.assertSoftly((assertions) -> { - assertions.assertThat(lotteryNumbersList).hasSize(lotteryCount); - assertLotteryRangeNumbers(MIN_LOTTERY_NUMBER, MAX_LOTTERY_NUMBER, lotteryNumbersList, assertions); + assertions.assertThat(lotteryNumbersList.getLottoList()).hasSize(lotteryCount); + assertLottoRangeNumbers(MIN_LOTTERY_NUMBER, MAX_LOTTERY_NUMBER, lotteryNumbersList.getLottoList(), assertions); }); } - private void assertLotteryRangeNumbers(int MIN_LOTTERY_NUMBER, int MAX_LOTTERY_NUMBER, List> lotteryNumbersList, SoftAssertions assertions) { + private void assertLottoRangeNumbers(int MIN_LOTTERY_NUMBER, int MAX_LOTTERY_NUMBER, List lotteryNumbersList, SoftAssertions assertions) { lotteryNumbersList.forEach((lotteryNumbers) -> { - lotteryNumbers.forEach((lotteryNumber) -> { - assertions.assertThat(lotteryNumber).isBetween(MIN_LOTTERY_NUMBER, MAX_LOTTERY_NUMBER + 1); - }); + lotteryNumbers.getNumbers() + .forEach((lotteryNumber) -> { + assertions.assertThat(lotteryNumber).isBetween(MIN_LOTTERY_NUMBER, MAX_LOTTERY_NUMBER + 1); + }); }); } @@ -54,7 +57,7 @@ void getReturnRateTest() { int totalAmount = 5000; int amount = 10000; // when - BigDecimal returnRate = AutoLotteryService.getReturnRate(totalAmount, amount); + BigDecimal returnRate = AutoLottoService.getReturnRate(totalAmount, amount); // then assertThat(returnRate).isEqualTo(new BigDecimal(0.5)); } diff --git a/src/test/java/auto/application/MatchLotteryServiceTest.java b/src/test/java/auto/application/MatchLotteryServiceTest.java deleted file mode 100644 index de90d77f297..00000000000 --- a/src/test/java/auto/application/MatchLotteryServiceTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package auto.application; - -import org.assertj.core.api.SoftAssertions; -import org.junit.jupiter.api.Test; - -import java.util.List; -import java.util.Map; - -class MatchLotteryServiceTest { - private final MatchLotteryService matchLotteryService = new MatchLotteryService(); - - @Test - void getMatchedCountMapTest() { - // given - var lotteryNumbersList = List.of( - List.of(1, 2, 3, 9, 10, 11), - - List.of(1, 2, 3, 4, 10, 11), - List.of(1, 2, 3, 4, 10, 11), - - List.of(1, 2, 3, 4, 5, 11), - List.of(1, 2, 3, 4, 5, 11), - List.of(1, 2, 3, 4, 5, 11)); - var winningNumbersLastWeek = List.of(1, 2, 3, 4, 5, 6); - // when - Map matchedCountMap = matchLotteryService.getMatchedCountMap(lotteryNumbersList, winningNumbersLastWeek); - // then - SoftAssertions.assertSoftly(assertions -> { - assertions.assertThat(matchedCountMap.get(3)).isEqualTo(1); - assertions.assertThat(matchedCountMap.get(4)).isEqualTo(2); - assertions.assertThat(matchedCountMap.get(5)).isEqualTo(3); - assertions.assertThat(matchedCountMap.get(6)).isEqualTo(0); - }); - } -} diff --git a/src/test/java/auto/application/MatchLottoServiceTest.java b/src/test/java/auto/application/MatchLottoServiceTest.java new file mode 100644 index 00000000000..53adb11052b --- /dev/null +++ b/src/test/java/auto/application/MatchLottoServiceTest.java @@ -0,0 +1,37 @@ +package auto.application; + +import auto.domain.Lotto; +import auto.domain.Lottos; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +class MatchLottoServiceTest { + private final MatchLottoService matchLottoService = new MatchLottoService(); + + @Test + void getMatchedCountMapTest() { + // given + var lotteryNumbersList = new Lottos( + List.of(new Lotto(List.of(1, 2, 3, 9, 10, 11)), + + new Lotto(List.of(1, 2, 3, 4, 10, 11)), + new Lotto(List.of(1, 2, 3, 4, 10, 11)), + + new Lotto(List.of(1, 2, 3, 4, 5, 11)), + new Lotto(List.of(1, 2, 3, 4, 5, 11)), + new Lotto(List.of(1, 2, 3, 4, 5, 11)))); + var winningNumbersLastWeek = new Lotto(List.of(1, 2, 3, 4, 5, 6)); + // when + Map matchedCountMap = matchLottoService.getMatchedCountMap(lotteryNumbersList, winningNumbersLastWeek, 9); + // then + SoftAssertions.assertSoftly(assertions -> { + assertions.assertThat(matchedCountMap.get(MatchedAmount.THREE)).isEqualTo(1); + assertions.assertThat(matchedCountMap.get(MatchedAmount.FOUR)).isEqualTo(2); + assertions.assertThat(matchedCountMap.get(MatchedAmount.FIVE)).isEqualTo(3); + assertions.assertThat(matchedCountMap.get(MatchedAmount.SIX)).isEqualTo(0); + }); + } +} diff --git a/src/test/java/auto/application/MatchedAmountTest.java b/src/test/java/auto/application/MatchedAmountTest.java index bdb73996b82..30ac80f1515 100644 --- a/src/test/java/auto/application/MatchedAmountTest.java +++ b/src/test/java/auto/application/MatchedAmountTest.java @@ -4,9 +4,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import java.util.Map; -import java.util.Set; - import static org.assertj.core.api.Assertions.assertThat; class MatchedAmountTest { @@ -14,16 +11,16 @@ class MatchedAmountTest { @CsvSource(value = {"3,THREE", "4,FOUR", "5,FIVE", "6,SIX"}) void findByCountTest(int count, String expectedEnum) { // when - MatchedAmount matchedAmount = MatchedAmount.findByCount(count); + MatchedAmount matchedAmount = MatchedAmount.findByCount(count, false); // then assertThat(matchedAmount).isEqualTo(MatchedAmount.valueOf(expectedEnum)); } @Test - void getMatchedCountMapTest() { + void findByCountTest() { // when - Map matchedCountMap = MatchedAmount.getMatchedCountMap(); + MatchedAmount matchedAmount = MatchedAmount.findByCount(5, true); // then - assertThat(matchedCountMap.keySet()).isEqualTo(Set.of(3, 4, 5, 6)); + assertThat(matchedAmount).isEqualTo(MatchedAmount.FIVE_AND_BONUS); } }