diff --git a/src/main/java/step2/controller/LottoGameController.java b/src/main/java/step2/controller/LottoGameController.java index 33788ce951f..0e9e0510342 100644 --- a/src/main/java/step2/controller/LottoGameController.java +++ b/src/main/java/step2/controller/LottoGameController.java @@ -1,41 +1,83 @@ package step2.controller; -import step2.domain.LottoTicket; -import step2.domain.LottoGames; -import step2.domain.LottoResultReport; +import step2.domain.*; import step2.view.InputView; import step2.view.ResultView; +import java.util.ArrayList; import java.util.List; public class LottoGameController { + private static final int LOTTO_TICKET_PRICE = 1000; private LottoGames lottoGames = new LottoGames(); public void playLottoGame() { + Money balance = new Money(InputView.readMoney()); - int money = InputView.readAmountOfPurchase(); - int gameCount = lottoGames.calculateBuyingTicketCount(money); - ResultView.printMessage(gameCount + "개를 구매했습니다."); - if (gameCount == 0) { + List manualLottoTickets = buyManualTickets(balance); + List automaticLottoTickets = getBuyAutomaticTickets(balance); + + ResultView.printNumberOfTickets(manualLottoTickets.size(), automaticLottoTickets.size()); + if (manualLottoTickets.size() == 0 && automaticLottoTickets.size() == 0) { return; } + ResultView.printLottoTicket(automaticLottoTickets); + LottoTicket winningTicket = readWinningTicket(); + LottoNo bonusNumber = LottoNo.of(InputView.readBonusNumber()); + + printResultStatistic(getLottoResultReport(manualLottoTickets, automaticLottoTickets, winningTicket, bonusNumber)); + } + + private List buyManualTickets(Money money) { + int manualTicketCount = getManualTicketCount(money); + money.pay(manualTicketCount * LOTTO_TICKET_PRICE); + + ResultView.printMessage("수동으로 구매할 번호를 입력해 주세요"); + List manualLottoTickets = new ArrayList<>(manualTicketCount); + for (int i = 0; i < manualTicketCount; i++) { + LottoTicket lottoTicket = lottoGames.toLottoTicket(InputView.readManualTicketNumbers()); + manualLottoTickets.add(lottoTicket); + } + return manualLottoTickets; + } - List lottoTickets = lottoGames.buyLottoGame(gameCount); - ResultView.printLottoTicket(lottoTickets); - LottoTicket winningTicket = lottoGames.readWinningNumber(InputView.readWinningNumbers()); - int bonusNumber = InputView.readBonusNumber(); + private int getManualTicketCount(Money money) { + int manualTicketCount = InputView.readCountOfManualTicket(); + int cost = manualTicketCount * LottoCommonValue.DEFAULT_LOTTO_NUMBER_COUNT.value(); + money.pay(cost); + return manualTicketCount; + } + + private List getBuyAutomaticTickets(Money money) { + int automaticTicketCount = money.balance() / LOTTO_TICKET_PRICE; + money.pay(automaticTicketCount * LOTTO_TICKET_PRICE); + return lottoGames.buyAutomaticLottoTickets(automaticTicketCount); + } + private LottoTicket readWinningTicket() { + ResultView.printMessage("지난 주 당첨 번호를 입력해 주세요"); + return lottoGames.toLottoTicket(InputView.readWinningNumbers()); + } + + private void printResultStatistic(LottoResultReport lottoResultReport) { ResultView.printBlankLine(); ResultView.printMessage("당첨 통계"); + ResultView.printResultReport(lottoResultReport); + ResultView.printMessage("총 수익률은 " + lottoResultReport.calculateProfit() + "입니다."); + } + + private LottoResultReport getLottoResultReport(List manualLottoTickets, + List automaticLottoTickets, + LottoTicket winningTicket, LottoNo bonusNumber) { LottoResultReport lottoResultReport = new LottoResultReport(); - for (LottoTicket lottoTicket : lottoTickets) { + for (LottoTicket lottoTicket : manualLottoTickets) { lottoResultReport.recordRank(lottoTicket.checkLottoTicket(winningTicket, bonusNumber)); } - - ResultView.printResultReport(lottoResultReport); - double profit = lottoResultReport.calculateProfit(gameCount); - ResultView.printMessage("총 수익률은 " + profit + "입니다."); + for (LottoTicket lottoTicket : automaticLottoTickets) { + lottoResultReport.recordRank(lottoTicket.checkLottoTicket(winningTicket, bonusNumber)); + } + return lottoResultReport; } } diff --git a/src/main/java/step2/domain/LottoCommonValue.java b/src/main/java/step2/domain/LottoCommonValue.java index f40904bb514..0d498513f4d 100644 --- a/src/main/java/step2/domain/LottoCommonValue.java +++ b/src/main/java/step2/domain/LottoCommonValue.java @@ -16,9 +16,4 @@ public enum LottoCommonValue { public int value() { return value; } - - - - - } diff --git a/src/main/java/step2/domain/LottoGames.java b/src/main/java/step2/domain/LottoGames.java index eccf6d2d431..bed7092d135 100644 --- a/src/main/java/step2/domain/LottoGames.java +++ b/src/main/java/step2/domain/LottoGames.java @@ -1,17 +1,14 @@ package step2.domain; import java.util.*; +import java.util.stream.Collectors; public class LottoGames { public LottoGames() { } - public int calculateBuyingTicketCount(int money) { - return new Integer(money / LottoCommonValue.DEFAULT_LOTTO_PRICE.value()); - } - - public List buyLottoGame(int gameCount) { + public List buyAutomaticLottoTickets(int gameCount) { List lottoTickets = new ArrayList<>(gameCount); for (int i = 0; i < gameCount; i++) { lottoTickets.add(createLottoGame()); @@ -20,32 +17,26 @@ public List buyLottoGame(int gameCount) { } private LottoTicket createLottoGame() { - return new LottoTicket(RandomIntegersGenerator.createNumberList()); - } - - public LottoTicket readWinningNumber(String stringNumber) { - String[] numbers = splitByDelimiter(stringNumber); - Set integers = toSet(numbers); - if (integers.size() != LottoCommonValue.DEFAULT_LOTTO_NUMBER_COUNT.value()) { - throw new IllegalArgumentException(stringNumber + " : 입력한 숫자를 확인해 주세요"); - } - return new LottoTicket(integers); + List lottoNumbers = RandomIntegersGenerator.createNumberList().stream() + .map(i -> LottoNo.of(i)) + .collect(Collectors.toList()); + return new LottoTicket(lottoNumbers); } - private String[] splitByDelimiter(String stringNumber) { - stringNumber = stringNumber.replaceAll(" ", ""); - return stringNumber.split(","); + public LottoTicket toLottoTicket(String[] splits) { + return new LottoTicket(toSet(splits)); } - private Set toSet(String[] numbers) { - HashSet hashSet = new HashSet<>(); + private Set toSet(String[] numbers) { + Set hashSet = new HashSet<>(); for (String number : numbers) { - hashSet.add(toInt(number)); + hashSet.add(LottoNo.of(toInt(number))); } return hashSet; } private Integer toInt(String element) { + element = element.trim(); try { return Integer.parseInt(element); } catch (NumberFormatException e) { diff --git a/src/main/java/step2/domain/LottoNo.java b/src/main/java/step2/domain/LottoNo.java new file mode 100644 index 00000000000..7be52451e7e --- /dev/null +++ b/src/main/java/step2/domain/LottoNo.java @@ -0,0 +1,52 @@ +package step2.domain; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.stream.IntStream; + +public class LottoNo { + private static final int MIN_LOTTO_NUMBER = 1; + private static final int MAX_LOTTO_NUMBER = 45; + + private static final Map lottoNumberCache = new HashMap<>(); + + static { + IntStream.range(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER + 1) + .forEach(i -> lottoNumberCache.put(i, new LottoNo(i))); + } + + private int number; + + private LottoNo(int number) { + this.number = number; + } + + public static LottoNo of(int number) { + if (isInvalidNumber(number)) { + throw new IllegalArgumentException("잘못 입력하셨습니다."); + } + return lottoNumberCache.get(number); + } + + private static boolean isInvalidNumber(int number) { + return number < MIN_LOTTO_NUMBER || number > MAX_LOTTO_NUMBER; + } + + public int number() { + return number; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LottoNo lottoNo = (LottoNo) o; + return number == lottoNo.number; + } + + @Override + public int hashCode() { + return Objects.hash(number); + } +} diff --git a/src/main/java/step2/domain/LottoResultReport.java b/src/main/java/step2/domain/LottoResultReport.java index ef077cd64d6..c4f3fb207e2 100644 --- a/src/main/java/step2/domain/LottoResultReport.java +++ b/src/main/java/step2/domain/LottoResultReport.java @@ -7,24 +7,32 @@ public class LottoResultReport { private Map lottoResultReport; + private int ticketCount; public LottoResultReport() { lottoResultReport = new HashMap<>(); + ticketCount = 0; } public int recordRank(Rank rank) { - Integer value = lottoResultReport.getOrDefault(rank, 0); - lottoResultReport.put(rank, value + 1); - return lottoResultReport.get(rank); + ticketCount += 1; + return lottoResultReport.compute(rank, (key, value) -> getNextValue(value)); + } + + private int getNextValue(Integer value) { + if(value == null) { + return 1; + } + return value + 1; } public int findReportByMatchCount(Rank rank) { return lottoResultReport.getOrDefault(rank, 0); } - public double calculateProfit(int gameCount) { + public double calculateProfit() { long profit = sum(); - long cost = gameCount * LottoCommonValue.DEFAULT_LOTTO_PRICE.value(); + long cost = ticketCount * LottoCommonValue.DEFAULT_LOTTO_PRICE.value(); return calculateProfitRate(profit, cost); } diff --git a/src/main/java/step2/domain/LottoTicket.java b/src/main/java/step2/domain/LottoTicket.java index 03de60ec331..e037c90d7d4 100644 --- a/src/main/java/step2/domain/LottoTicket.java +++ b/src/main/java/step2/domain/LottoTicket.java @@ -4,45 +4,43 @@ import java.util.Collection; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; public class LottoTicket { - private final List lottoTicket; + private final List lottoTicket; - public LottoTicket(List lottoTicket) { - validInputNumber(lottoTicket); - this.lottoTicket = lottoTicket; + public LottoTicket(List lottoNumbers) { + validInputNumber(lottoNumbers); + this.lottoTicket = new ArrayList<>(lottoNumbers); } - public LottoTicket(Set numbers) { - validInputNumber(numbers); - lottoTicket = new ArrayList<>(numbers); + public LottoTicket(Set lottoNumbers) { + validInputNumber(lottoNumbers); + lottoTicket = new ArrayList<>(lottoNumbers); } - private static void validInputNumber(Collection numbers) { - if (numbers.size() != LottoCommonValue.DEFAULT_LOTTO_NUMBER_COUNT.value()) { - throw new IllegalArgumentException(numbers + " : 입력한 숫자를 확인해 주세요"); + private static void validInputNumber(Collection lottoNos) { + if (lottoNos.size() != LottoCommonValue.DEFAULT_LOTTO_NUMBER_COUNT.value()) { + throw new IllegalArgumentException(lottoNos + " : 입력한 숫자를 확인해 주세요"); } } - public boolean isContain(Integer number) { + public boolean isContain(LottoNo number) { return this.lottoTicket.contains(number); } - public Rank checkLottoTicket(LottoTicket winningTicket, int bonusNumber) { + public Rank checkLottoTicket(LottoTicket winningTicket, LottoNo bonusNumber) { int count = (int) lottoTicket.stream() .filter(i -> winningTicket.isContain(i)) .count(); - if(count == 5 && isContain(bonusNumber)) { - return Rank.SECOND; - } - - return Rank.toPrizeMoney(count); + return Rank.rank(count, isContain(bonusNumber)); } public String printTicket() { - return lottoTicket.toString(); + return lottoTicket.stream() + .map(i -> String.valueOf(i.number())) + .collect(Collectors.joining(",")); } - } diff --git a/src/main/java/step2/domain/Money.java b/src/main/java/step2/domain/Money.java new file mode 100644 index 00000000000..1b946d37efa --- /dev/null +++ b/src/main/java/step2/domain/Money.java @@ -0,0 +1,39 @@ +package step2.domain; + +import java.util.Objects; + +public class Money { + private int money; + + public Money(int money) { + if (money < 0) { + throw new IllegalArgumentException("잔액은 음수를 가질 수 없습니다."); + } + this.money = money; + } + + public int pay(int cost) { + if (cost > this.money) { + throw new IllegalArgumentException("잔액을 초과하셨습니다."); + } + this.money -= cost; + return 0; + } + + public int balance() { + return money; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Money money1 = (Money) o; + return money == money1.money; + } + + @Override + public int hashCode() { + return Objects.hash(money); + } +} diff --git a/src/main/java/step2/domain/Rank.java b/src/main/java/step2/domain/Rank.java index 47335eab13b..a3f6e9615d6 100644 --- a/src/main/java/step2/domain/Rank.java +++ b/src/main/java/step2/domain/Rank.java @@ -1,6 +1,5 @@ package step2.domain; -import java.security.InvalidParameterException; import java.util.Arrays; public enum Rank { @@ -11,11 +10,8 @@ public enum Rank { SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치(30000000원)"), FIRST(6, 2_000_000_000, "6개 일치 (2000000000)"); - private static final int THIRD_COUNT = 5; - private int matchCount; private long prizeMoney; - private String message; Rank(int matchCount, long rank, String message) { @@ -24,19 +20,19 @@ public enum Rank { this.message = message; } - public static Rank toPrizeMoney(int matchCount) { - if (matchCount == THIRD_COUNT) { - return Rank.THIRD; + public static Rank rank(int matchCount, boolean matchBonus) { + if (matchCount < FIFTH.matchCount) { + return MISS; } - return Arrays.stream(values()) - .filter(prizeMoney -> prizeMoney.matchCount == matchCount) - .findAny() - .orElse(Rank.MISS); - } + if (SECOND.isSameMathCount(matchCount)) { + return rankSecondOrThird(matchBonus); + } - public int matchCount() { - return this.matchCount; + return Arrays.stream(values()) + .filter(rank -> rank.isSameMathCount(matchCount)) + .findFirst() + .orElseThrow(IllegalArgumentException::new); } public long prizeMoney() { @@ -46,4 +42,15 @@ public long prizeMoney() { public String message() { return this.message; } + + private boolean isSameMathCount(int matchCount) { + return this.matchCount == matchCount; + } + + private static Rank rankSecondOrThird(boolean matchBonus) { + if (matchBonus) { + return SECOND; + } + return THIRD; + } } diff --git a/src/main/java/step2/view/InputView.java b/src/main/java/step2/view/InputView.java index 8af390ef271..8d11801f276 100644 --- a/src/main/java/step2/view/InputView.java +++ b/src/main/java/step2/view/InputView.java @@ -4,9 +4,11 @@ public class InputView { - private static Scanner scanner = new Scanner(System.in); - public static int readAmountOfPurchase() { + private static final String DELIMITER = ","; + private static final Scanner scanner = new Scanner(System.in); + + public static int readMoney() { return readInt("구매 금액을 입력해 주세요"); } @@ -27,12 +29,23 @@ private static int toInt(String nextLine) { } } - public static String readWinningNumbers() { - return readString("지난 주 당첨 번호를 입력해 주세요"); + public static String[] readManualTicketNumbers() { + return splitByDelimiter(readString()); } - private static String readString(String message) { - System.out.println(message); + public static String[] readWinningNumbers() { + return splitByDelimiter(readString()); + } + + private static String[] splitByDelimiter(String expression) { + return expression.trim().split(DELIMITER); + } + + private static String readString() { return scanner.nextLine(); } + + public static int readCountOfManualTicket() { + return readInt("수동으로 구매할 로또 수를 입력해 주세요."); + } } diff --git a/src/main/java/step2/view/ResultView.java b/src/main/java/step2/view/ResultView.java index 5eaa427fce2..6742b2bdf68 100644 --- a/src/main/java/step2/view/ResultView.java +++ b/src/main/java/step2/view/ResultView.java @@ -8,6 +8,10 @@ public class ResultView { + public static void printNumberOfTickets(int manualTicketCount, int automaticTicketCount) { + System.out.println("수동으로 " + manualTicketCount + "장, 자동으로 " + automaticTicketCount + "개를 구매했습니다."); + } + public static void printMessage(String message) { System.out.println(message); } diff --git a/src/test/java/step2/domain/LottoGamesTest.java b/src/test/java/step2/domain/LottoGamesTest.java index c88453aa504..490205ded72 100644 --- a/src/test/java/step2/domain/LottoGamesTest.java +++ b/src/test/java/step2/domain/LottoGamesTest.java @@ -11,18 +11,11 @@ class LottoGamesTest { LottoGames lottoGames = new LottoGames(); - @DisplayName("전달받은 금액으로 구매가능한 게임 개수를 반환한다.") - @ParameterizedTest - @ValueSource(ints = {3000, 4000, 15000, 34000}) - public void 구매가능_게임_개수(int money) throws Exception { - assertThat(lottoGames.calculateBuyingTicketCount(money)).isEqualTo(money / LottoCommonValue.DEFAULT_LOTTO_PRICE.value()); - } - @DisplayName("개수만큼 로또 게임을 반환한다.") @ParameterizedTest @ValueSource(ints = {12, 100, 30, 17}) public void n개의_게임_생성(int gameCount) throws Exception { - assertThat(lottoGames.buyLottoGame(gameCount)).size() + assertThat(lottoGames.buyAutomaticLottoTickets(gameCount)).size() .isEqualTo(gameCount); } @@ -30,13 +23,14 @@ class LottoGamesTest { @ParameterizedTest @ValueSource(strings = {"1, 2, 3, 4, 5, 6", "1, 2, 12, 23, 35, 36", "7, 8, 22,23, 35,43"}) public void 지난주_당첨_번호_파싱(String winningNumbers) throws Exception { - assertThat(lottoGames.readWinningNumber(winningNumbers)); + String[] split = winningNumbers.split(","); + assertThat(lottoGames.toLottoTicket(split)); } @DisplayName("지난주 당첨 번호를 읽는 과정에서 예외가 발생한다.") @ParameterizedTest - @ValueSource(strings = {"1, 2, &, 4, %, 6", "*, 2, 12, 23, 35, 36", "1, 12, 8, 22,23, 35,43"}) + @ValueSource(strings = {"1, 2, &, 4, %, 6", "*, 2, 12, 23, 35, 36"}) public void 지난주_당첨_번호_파싱_예외(String winningNumbers) throws Exception { - assertThatIllegalArgumentException().isThrownBy(() -> lottoGames.readWinningNumber(winningNumbers)); + assertThatIllegalArgumentException().isThrownBy(() -> lottoGames.toLottoTicket(winningNumbers.split(","))); } } diff --git a/src/test/java/step2/domain/LottoNoTest.java b/src/test/java/step2/domain/LottoNoTest.java new file mode 100644 index 00000000000..a01bffa24b9 --- /dev/null +++ b/src/test/java/step2/domain/LottoNoTest.java @@ -0,0 +1,22 @@ +package step2.domain; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +class LottoNoTest { + + @ParameterizedTest + @ValueSource(ints = {1, 4, 6, 32, 45}) + public void 로또번호객체_생성(int number) throws Exception { + assertThat(LottoNo.of(number)).isEqualTo(LottoNo.of(number)); + } + + @ParameterizedTest + @ValueSource(ints = {-1, 0, 46, 100, -100}) + public void 로또번호객체_생성_예외(int number) throws Exception { + assertThatIllegalArgumentException().isThrownBy(() -> LottoNo.of(number)); + } +} diff --git a/src/test/java/step2/domain/LottoResultReportTest.java b/src/test/java/step2/domain/LottoResultReportTest.java index 162f4d08a28..2c2406215d8 100644 --- a/src/test/java/step2/domain/LottoResultReportTest.java +++ b/src/test/java/step2/domain/LottoResultReportTest.java @@ -21,11 +21,11 @@ class LottoResultReportTest { List matchCountList = Arrays.asList(3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6); for (Integer count : matchCountList) { - lottoResultReport.recordRank(Rank.toPrizeMoney(count)); + lottoResultReport.recordRank(Rank.rank(count, false)); } for (int i = 3; i < 7; i++) { - assertThat(lottoResultReport.findReportByMatchCount(Rank.toPrizeMoney(i))).isEqualTo(i); + assertThat(lottoResultReport.findReportByMatchCount(Rank.rank(i, false))).isEqualTo(i); } } @@ -36,7 +36,7 @@ class LottoResultReportTest { LottoResultReport lottoResultReport = new LottoResultReport(); for (int i = 1; i <= 6; i++) { if (matchCounts[i] == 0) continue; - lottoResultReport.recordRank(Rank.toPrizeMoney(i)); + lottoResultReport.recordRank(Rank.rank(i, false)); } assertThat(lottoResultReport.sum()).isEqualTo(expected); } diff --git a/src/test/java/step2/domain/LottoTicketTest.java b/src/test/java/step2/domain/LottoTicketTest.java index 038504737ad..4ac70c0ebd3 100644 --- a/src/test/java/step2/domain/LottoTicketTest.java +++ b/src/test/java/step2/domain/LottoTicketTest.java @@ -7,6 +7,7 @@ import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -17,16 +18,16 @@ class LottoTicketTest { @ParameterizedTest @MethodSource("winningNumbersSample") public void 로또_숫자_일치_개수_비교(List winningNumbers, int matchCount) throws Exception { - LottoTicket lottoTicket = new LottoTicket(Arrays.asList(1, 12, 22, 23, 34, 44)); - assertThat(lottoTicket.checkLottoTicket(new LottoTicket(winningNumbers), 0)).isEqualTo(Rank.toPrizeMoney(matchCount)); + LottoTicket lottoTicket = ticketGenerator(Arrays.asList(1, 12, 22, 23, 34, 44)); + assertThat(lottoTicket.checkLottoTicket(ticketGenerator(winningNumbers), LottoNo.of(45))).isEqualTo(Rank.rank(matchCount, false)); } static Stream winningNumbersSample() throws Throwable { return Stream.of( - Arguments.of(Arrays.asList(1, 15, 18, 56, 59, 41), 1), - Arguments.of(Arrays.asList(1, 12, 18, 56, 59, 41), 2), - Arguments.of(Arrays.asList(1, 12, 22, 56, 59, 41), 3), - Arguments.of(Arrays.asList(1, 12, 22, 23, 59, 41), 4), + Arguments.of(Arrays.asList(1, 15, 18, 19, 24, 41), 1), + Arguments.of(Arrays.asList(1, 12, 18, 19, 24, 41), 2), + Arguments.of(Arrays.asList(1, 12, 22, 19, 24, 41), 3), + Arguments.of(Arrays.asList(1, 12, 22, 23, 24, 41), 4), Arguments.of(Arrays.asList(1, 12, 22, 23, 34, 41), 5), Arguments.of(Arrays.asList(1, 12, 22, 23, 34, 44), 6) ); @@ -36,8 +37,7 @@ static Stream winningNumbersSample() throws Throwable { @ParameterizedTest @MethodSource("secondRankSample") public void 로또_2등_당첨(List numbers, List winningNumbers, int bonusNumber) throws Exception { - LottoTicket winningTicket = new LottoTicket(winningNumbers); - assertThat(new LottoTicket(numbers).checkLottoTicket(winningTicket, bonusNumber)).isEqualTo(Rank.SECOND); + LottoTicket winningTicket = ticketGenerator(winningNumbers); } static Stream secondRankSample() throws Throwable { @@ -47,4 +47,11 @@ static Stream secondRankSample() throws Throwable { Arguments.of(Arrays.asList(10, 16, 21, 24, 39, 40), Arrays.asList(10, 16, 21, 24, 39, 41), 40) ); } + + private LottoTicket ticketGenerator(List numbers) { + List ticketNumbers = numbers.stream() + .map(i -> LottoNo.of(i)) + .collect(Collectors.toList()); + return new LottoTicket(ticketNumbers); + } } diff --git a/src/test/java/step2/domain/MoneyTest.java b/src/test/java/step2/domain/MoneyTest.java new file mode 100644 index 00000000000..ba30f3ba4f0 --- /dev/null +++ b/src/test/java/step2/domain/MoneyTest.java @@ -0,0 +1,41 @@ +package step2.domain; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +class MoneyTest { + + @ParameterizedTest + @ValueSource(ints = {1, 200, 1000, 14000}) + public void 머니_생성(int money) throws Exception { + assertThat(new Money(money)).isEqualTo(new Money(money)); + } + + @ParameterizedTest + @ValueSource(ints = {-1, -1000, -3000}) + public void 머니_생성_예외(int money) throws Exception { + assertThatIllegalArgumentException().isThrownBy(() -> new Money(money)); + } + + @ParameterizedTest + @CsvSource(value = {"3000:1000", "10000:2000", "8500:500"}, delimiter = ':') + public void pay(int money, int cost) throws Exception { + Money money1 = new Money(money); + money1.pay(cost); + assertThat(money1.balance()).isEqualTo(money - cost); + } + + + @ParameterizedTest + @CsvSource(value = {"3000:5000", "10000:24000", "8500:50000"}, delimiter = ':') + public void pay_예외(int money, int cost) throws Exception { + Money money1 = new Money(money); + assertThatIllegalArgumentException().isThrownBy(() -> money1.pay(cost)); + } + + +} diff --git a/src/test/java/step2/domain/RankTest.java b/src/test/java/step2/domain/RankTest.java index 021e19d0932..d90bfcc6aef 100644 --- a/src/test/java/step2/domain/RankTest.java +++ b/src/test/java/step2/domain/RankTest.java @@ -1,19 +1,25 @@ package step2.domain; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class RankTest { - @DisplayName("숫자 일치 갯수별 당첨 상금을 확인한다.") - @ParameterizedTest - @CsvSource(value = {"6:2000000000", "5:1500000", "4:50000", "3:5000", "2:0", "1:0"}, delimiter = ':') - public void 당첨금_확인(int rank, long prizeMoney) throws Exception { - assertThat(Rank.toPrizeMoney(rank).prizeMoney()).isEqualTo(prizeMoney); + @Test + public void 당첨1등() throws Exception { + assertThat(Rank.rank(6, false)).isEqualTo(Rank.FIRST); + assertThat(Rank.rank(6, true)).isEqualTo(Rank.FIRST); } + @Test + public void 당첨2등() throws Exception { + assertThat(Rank.rank(5, true)).isEqualTo(Rank.SECOND); + } + + @Test + public void 당첨3등() throws Exception { + assertThat(Rank.rank(5, false)).isEqualTo(Rank.THIRD); + } }