From 5abe2a254a2d1a236ea06b949902c22e5e79643b Mon Sep 17 00:00:00 2001 From: Mint Date: Tue, 18 Jun 2024 16:50:00 +0900 Subject: [PATCH 01/27] =?UTF-8?q?refactor:=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 도메인과 밀접한 상수는 도메인에서 관리하도록 이동한다. - 초기화는 기존 값을 초기값으로 변경하는 의미를 가지므로, 특정 값으로 객체를 생성한다는 의미에 맞춰 메서드명을 변경한다. --- src/main/java/org/duckstudy/model/Price.java | 4 ++-- .../java/org/duckstudy/model/lotto/Lotto.java | 6 +++--- .../org/duckstudy/model/lotto/LottoNumber.java | 17 ++++++++--------- .../model/lotto/constant/LottoNumberRange.java | 17 ----------------- 4 files changed, 13 insertions(+), 31 deletions(-) delete mode 100644 src/main/java/org/duckstudy/model/lotto/constant/LottoNumberRange.java diff --git a/src/main/java/org/duckstudy/model/Price.java b/src/main/java/org/duckstudy/model/Price.java index e1184485..d69241f1 100644 --- a/src/main/java/org/duckstudy/model/Price.java +++ b/src/main/java/org/duckstudy/model/Price.java @@ -18,7 +18,7 @@ public Price(int price) { this.value = price; } - public static Price initialize() { + public static Price zero() { return new Price(INCLUSIVE_MIN_PRICE); } @@ -59,7 +59,7 @@ public int calculateLottoCount() { } public double calculateProfitRate(LottoResult result) { - Price profit = Price.initialize(); + Price profit = Price.zero(); for (WinningRank winningRank : WinningRank.values()) { profit = profit.accumulateProfit(winningRank, result.getMatchingCount(winningRank.getKey())); } diff --git a/src/main/java/org/duckstudy/model/lotto/Lotto.java b/src/main/java/org/duckstudy/model/lotto/Lotto.java index aeeef811..4a489d88 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lotto.java +++ b/src/main/java/org/duckstudy/model/lotto/Lotto.java @@ -3,8 +3,8 @@ import static java.util.Collections.shuffle; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; -import static org.duckstudy.model.lotto.constant.LottoNumberRange.END_INCLUSIVE_NUMBER; -import static org.duckstudy.model.lotto.constant.LottoNumberRange.START_INCLUSIVE_NUMBER; +import static org.duckstudy.model.lotto.LottoNumber.END_INCLUSIVE_NUMBER; +import static org.duckstudy.model.lotto.LottoNumber.START_INCLUSIVE_NUMBER; import java.util.Collections; import java.util.List; @@ -18,7 +18,7 @@ public class Lotto { private static final int LOTTO_SIZE = 6; static { - NUMBERS = IntStream.range(START_INCLUSIVE_NUMBER.getValue(), END_INCLUSIVE_NUMBER.getValue() + 1) + NUMBERS = IntStream.range(START_INCLUSIVE_NUMBER, END_INCLUSIVE_NUMBER + 1) .boxed() .toList(); } diff --git a/src/main/java/org/duckstudy/model/lotto/LottoNumber.java b/src/main/java/org/duckstudy/model/lotto/LottoNumber.java index 62a18119..a077d3cd 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoNumber.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoNumber.java @@ -1,19 +1,18 @@ package org.duckstudy.model.lotto; -import static org.duckstudy.model.lotto.constant.LottoNumberRange.END_INCLUSIVE_NUMBER; -import static org.duckstudy.model.lotto.constant.LottoNumberRange.START_INCLUSIVE_NUMBER; - import java.util.List; import java.util.Objects; import java.util.stream.IntStream; public class LottoNumber { + public static final int START_INCLUSIVE_NUMBER = 1; + public static final int END_INCLUSIVE_NUMBER = 45; private static final List cache; static { - cache = IntStream.range(0, END_INCLUSIVE_NUMBER.getValue()) - .mapToObj(i -> new LottoNumber(START_INCLUSIVE_NUMBER.getValue() + i)) + cache = IntStream.range(0, END_INCLUSIVE_NUMBER) + .mapToObj(i -> new LottoNumber(START_INCLUSIVE_NUMBER + i)) .toList(); } @@ -26,14 +25,14 @@ private LottoNumber(int number) { public static LottoNumber valueOf(int number) { validateNumber(number); - return cache.get(number - START_INCLUSIVE_NUMBER.getValue()); + return cache.get(number - START_INCLUSIVE_NUMBER); } private static void validateNumber(int number) { - if (number < START_INCLUSIVE_NUMBER.getValue() || number > END_INCLUSIVE_NUMBER.getValue()) { + if (number < START_INCLUSIVE_NUMBER || number > END_INCLUSIVE_NUMBER) { throw new IllegalArgumentException( - String.format("로또 번호는 %d 이상 %d 이하의 숫자여야 합니다.", START_INCLUSIVE_NUMBER.getValue(), - END_INCLUSIVE_NUMBER.getValue())); + String.format("로또 번호는 %d 이상 %d 이하의 숫자여야 합니다.", START_INCLUSIVE_NUMBER, + END_INCLUSIVE_NUMBER)); } } diff --git a/src/main/java/org/duckstudy/model/lotto/constant/LottoNumberRange.java b/src/main/java/org/duckstudy/model/lotto/constant/LottoNumberRange.java deleted file mode 100644 index f499c792..00000000 --- a/src/main/java/org/duckstudy/model/lotto/constant/LottoNumberRange.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.duckstudy.model.lotto.constant; - -public enum LottoNumberRange { - - START_INCLUSIVE_NUMBER(1), - END_INCLUSIVE_NUMBER(45); - - private final int value; - - LottoNumberRange(int value) { - this.value = value; - } - - public int getValue() { - return value; - } -} From 685ce55bc94f412eeb6d7ae2ddb75a08b100721e Mon Sep 17 00:00:00 2001 From: Mint Date: Tue, 18 Jun 2024 16:52:00 +0900 Subject: [PATCH 02/27] =?UTF-8?q?fix:=20=EC=9A=B0=EC=8A=B9=EC=9E=90=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EB=B2=84=EA=B7=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 매칭되지 않은 경우를 이미 filter에서 확인하고 있으므로 그 외의 경우 예외를 발생시키도록 수정한다(orElseThrow()). --- .../model/lotto/constant/WinningRank.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java index 277b72ce..7c68a5cc 100644 --- a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java +++ b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java @@ -5,11 +5,11 @@ public enum WinningRank { NONE(0, 0, 0), - FIFTH(3, 5000, 3), - FOURTH(4, 50000, 4), - THIRD(5, 1500000, 5), - SECOND(5, 30000000, -5), - FIRST(6, 2000000000, 6); + FIFTH(3, 5_000, 3), + FOURTH(4, 50_000, 4), + THIRD(5, 1_500_000, 5), + SECOND(5, 30_000_000, -5), + FIRST(6, 2_000_000_000, 6); private final int matchCount; private final int price; @@ -22,13 +22,20 @@ public enum WinningRank { } public static WinningRank findByMatchCountAndBonus(int matchCount, boolean matchBonus) { - if (matchCount == SECOND.getMatchCount() && matchBonus) { - return SECOND; - } return Stream.of(values()) - .filter(winningLank -> winningLank.getMatchCount() == matchCount) + .filter(winningLank -> winningLank.isMatch(matchCount, matchBonus)) .findFirst() - .orElse(NONE); + .orElseThrow(() -> new IllegalArgumentException("적절한 당첨 등수를 찾을 수 없습니다.")); + } + + private boolean isMatch(int matchCount, boolean matchBonus) { + if (this == SECOND) { + return matchCount == 5 && matchBonus; + } + if (this == THIRD) { + return matchCount == 5 && !matchBonus; + } + return this.matchCount == matchCount; } public int getMatchCount() { From 0f84cc294e848237879c1a65af44a392d0739a34 Mon Sep 17 00:00:00 2001 From: Mint Date: Tue, 18 Jun 2024 17:10:12 +0900 Subject: [PATCH 03/27] =?UTF-8?q?refactor:=20=ED=95=A8=EC=88=98=ED=98=95?= =?UTF-8?q?=20=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=ED=99=9C?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - BiPredicate를 사용하여 매칭 여부를 판단하도록 작성한다. - enum 객체 내에 함수를 정의하여 캡슐화한다. --- .../model/lotto/constant/WinningRank.java | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java index 7c68a5cc..7fb7b705 100644 --- a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java +++ b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java @@ -1,24 +1,27 @@ package org.duckstudy.model.lotto.constant; +import java.util.function.BiPredicate; import java.util.stream.Stream; public enum WinningRank { - NONE(0, 0, 0), - FIFTH(3, 5_000, 3), - FOURTH(4, 50_000, 4), - THIRD(5, 1_500_000, 5), - SECOND(5, 30_000_000, -5), - FIRST(6, 2_000_000_000, 6); + NONE(0, 0, 0, (matchCount, matchBonus) -> matchCount < 3), + FIFTH(3, 5_000, 3, (matchCount, matchBonus) -> matchCount == 3), + FOURTH(4, 50_000, 4, (matchCount, matchBonus) -> matchCount == 4), + THIRD(5, 1_500_000, 5, (matchCount, matchBonus) -> matchCount == 5 && !matchBonus), + SECOND(5, 30_000_000, -5, (matchCount, matchBonus) -> matchCount == 5 && matchBonus), + FIRST(6, 2_000_000_000, 6, (matchCount, matchBonus) -> matchCount == 6); private final int matchCount; private final int price; private final int key; + private final BiPredicate isMatchPredicate; - WinningRank(int matchCount, int price, int key) { + WinningRank(int matchCount, int price, int key, BiPredicate isMatchPredicate) { this.matchCount = matchCount; this.price = price; this.key = key; + this.isMatchPredicate = isMatchPredicate; } public static WinningRank findByMatchCountAndBonus(int matchCount, boolean matchBonus) { @@ -28,14 +31,8 @@ public static WinningRank findByMatchCountAndBonus(int matchCount, boolean match .orElseThrow(() -> new IllegalArgumentException("적절한 당첨 등수를 찾을 수 없습니다.")); } - private boolean isMatch(int matchCount, boolean matchBonus) { - if (this == SECOND) { - return matchCount == 5 && matchBonus; - } - if (this == THIRD) { - return matchCount == 5 && !matchBonus; - } - return this.matchCount == matchCount; + public boolean isMatch(int matchCount, boolean matchBonus) { + return isMatchPredicate.test(matchCount, matchBonus); } public int getMatchCount() { From 7def5fbdf522a3194bd7532f2ba605e48a2c7ad9 Mon Sep 17 00:00:00 2001 From: Mint Date: Tue, 18 Jun 2024 17:36:37 +0900 Subject: [PATCH 04/27] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20private=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 클래스 내부에서 사용하는 메서드는 private으로 변경한다. --- .../java/org/duckstudy/model/lotto/constant/WinningRank.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java index 7fb7b705..488a5b7a 100644 --- a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java +++ b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java @@ -31,7 +31,7 @@ public static WinningRank findByMatchCountAndBonus(int matchCount, boolean match .orElseThrow(() -> new IllegalArgumentException("적절한 당첨 등수를 찾을 수 없습니다.")); } - public boolean isMatch(int matchCount, boolean matchBonus) { + private boolean isMatch(int matchCount, boolean matchBonus) { return isMatchPredicate.test(matchCount, matchBonus); } From 6fbbe0b100154d750ee21123113d10e322f4d5f0 Mon Sep 17 00:00:00 2001 From: Mint Date: Tue, 18 Jun 2024 17:37:02 +0900 Subject: [PATCH 05/27] =?UTF-8?q?test:=20WinningRank=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/lotto/constant/WinningRankTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java diff --git a/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java b/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java new file mode 100644 index 00000000..13ccca00 --- /dev/null +++ b/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java @@ -0,0 +1,33 @@ +package org.duckstudy.model.lotto.constant; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.stream.Stream; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +@DisplayName("당첨 순위 테스트") +class WinningRankTest { + + @DisplayName("당첨 횟수와 보너스 볼 여부를 입력하면 당첨 순위를 반환한다") + @ParameterizedTest + @MethodSource("generateMatchCountAndMatchBonus") + void findWinningRankSuccess(int matchCount, boolean matchBonus, + WinningRank expected) { + assertThat(WinningRank.findByMatchCountAndBonus(matchCount, matchBonus)) + .isEqualTo(expected); + } + + static Stream generateMatchCountAndMatchBonus() { + return Stream.of( + Arguments.of(0, false, WinningRank.NONE), + Arguments.of(3, false, WinningRank.FIFTH), + Arguments.of(4, false, WinningRank.FOURTH), + Arguments.of(5, false, WinningRank.THIRD), + Arguments.of(5, true, WinningRank.SECOND), + Arguments.of(6, false, WinningRank.FIRST) + ); + } +} From 204eec4c702bc34ffc11c36bfae99e00a8f62133 Mon Sep 17 00:00:00 2001 From: Mint Date: Tue, 18 Jun 2024 17:44:16 +0900 Subject: [PATCH 06/27] =?UTF-8?q?refactor:=20=EB=B0=98=ED=99=98=EA=B0=92?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - WinningRank의 findByMatchCountAndBonus 메서드는 호출하는 곳에서 key값만 사용하므로 반환값을 key 값으로 수정한다. --- .../org/duckstudy/model/lotto/LottoResult.java | 3 +-- .../model/lotto/constant/WinningRank.java | 3 ++- .../model/lotto/constant/WinningRankTest.java | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/duckstudy/model/lotto/LottoResult.java b/src/main/java/org/duckstudy/model/lotto/LottoResult.java index ec43bcc3..8afd3106 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoResult.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoResult.java @@ -21,8 +21,7 @@ public static LottoResult createLottoResult(Lotto lotto, Lotto winningLotto, Lot int matchingCount = lotto.countMatchingNumber(winningLotto); boolean matchBonus = lotto.containsNumber(bonusNumber); - int key = WinningRank.findByMatchCountAndBonus(matchingCount, matchBonus) - .getKey(); + int key = WinningRank.findByMatchCountAndBonus(matchingCount, matchBonus); return new LottoResult(Map.of(key, DEFAULT_FREQUENCY)); } diff --git a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java index 488a5b7a..06274171 100644 --- a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java +++ b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java @@ -24,10 +24,11 @@ public enum WinningRank { this.isMatchPredicate = isMatchPredicate; } - public static WinningRank findByMatchCountAndBonus(int matchCount, boolean matchBonus) { + public static int findByMatchCountAndBonus(int matchCount, boolean matchBonus) { return Stream.of(values()) .filter(winningLank -> winningLank.isMatch(matchCount, matchBonus)) .findFirst() + .map(WinningRank::getKey) .orElseThrow(() -> new IllegalArgumentException("적절한 당첨 등수를 찾을 수 없습니다.")); } diff --git a/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java b/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java index 13ccca00..dae79097 100644 --- a/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java +++ b/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java @@ -15,19 +15,19 @@ class WinningRankTest { @ParameterizedTest @MethodSource("generateMatchCountAndMatchBonus") void findWinningRankSuccess(int matchCount, boolean matchBonus, - WinningRank expected) { + int expected) { assertThat(WinningRank.findByMatchCountAndBonus(matchCount, matchBonus)) .isEqualTo(expected); } static Stream generateMatchCountAndMatchBonus() { return Stream.of( - Arguments.of(0, false, WinningRank.NONE), - Arguments.of(3, false, WinningRank.FIFTH), - Arguments.of(4, false, WinningRank.FOURTH), - Arguments.of(5, false, WinningRank.THIRD), - Arguments.of(5, true, WinningRank.SECOND), - Arguments.of(6, false, WinningRank.FIRST) + Arguments.of(0, false, WinningRank.NONE.getKey()), + Arguments.of(3, false, WinningRank.FIFTH.getKey()), + Arguments.of(4, false, WinningRank.FOURTH.getKey()), + Arguments.of(5, false, WinningRank.THIRD.getKey()), + Arguments.of(5, true, WinningRank.SECOND.getKey()), + Arguments.of(6, false, WinningRank.FIRST.getKey()) ); } } From dd3feb161c7c078b4cbe7053c98d90b83f7db73d Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 17:43:00 +0900 Subject: [PATCH 07/27] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EC=88=98?= =?UTF-8?q?=EB=8F=99=20=EA=B5=AC=EB=A7=A4=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로또 수동 구매 기능 지원을 위한 inputView, outputView 메서드 추가, 도메인 메서드 호출 - Lottos: generateLottosByPrice 메서드명 변경 price는 lottoCount를 구하는데에 사용되므로 price가 아닌 lottoCount를 인자로 넘긴다. --- .../duckstudy/controller/LottoController.java | 71 ++++++++++++++++--- .../org/duckstudy/model/lotto/Lottos.java | 15 ++-- .../java/org/duckstudy/view/InputView.java | 18 +++++ .../java/org/duckstudy/view/OutputView.java | 12 ++-- 4 files changed, 95 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index 6d570936..918ac51f 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -1,5 +1,7 @@ package org.duckstudy.controller; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import org.duckstudy.model.Price; import org.duckstudy.model.lotto.Lotto; import org.duckstudy.model.lotto.LottoNumber; @@ -20,13 +22,21 @@ public LottoController(OutputView outputView, InputView inputView) { public void run() { Price price = createPrice(); - Lottos lottos = Lottos.generateLottosByPrice(price); - outputView.printLottos(lottos); + int totalLottoCount = price.calculateLottoCount(); + + int manualLottoCount = getManualLottoCount(totalLottoCount); + Lottos manualLottos = createManualLottos(manualLottoCount); + + int autoLottoCount = totalLottoCount - manualLottoCount; + Lottos autoLottos = Lottos.generateLottos(autoLottoCount); + + Lottos totalLottos = manualLottos.merge(autoLottos); + outputView.printLottos(manualLottoCount, autoLottoCount, totalLottos); Lotto winningLotto = createWinningLotto(); LottoNumber bonusNumber = createBonusNumber(winningLotto); - getWinningResult(price, lottos, winningLotto, bonusNumber); + getWinningResult(price, totalLottos, winningLotto, bonusNumber); } private Price createPrice() { @@ -40,6 +50,43 @@ private Price createPrice() { } } + private Lottos createManualLottos(int manualLottoCount) { + outputView.printInputManualLotto(); + + return new Lottos(IntStream.range(0, manualLottoCount) + .mapToObj(i -> createManualLotto()) + .collect(Collectors.toList())); + } + + private int getManualLottoCount(int lottoCount) { + try { + int manualLottoCount = inputView.inputManualLottoCount(); + validateManualLottoCount(lottoCount, manualLottoCount); + return manualLottoCount; + } catch (IllegalArgumentException e) { + outputView.printException(e); + return getManualLottoCount(lottoCount); + } + } + + private void validateManualLottoCount(int lottoCount, int manualLottoCount) { + if (manualLottoCount < 0) { + throw new IllegalArgumentException("음수로 입력할 수 없습니다.\n"); + } + if (manualLottoCount > lottoCount) { + throw new IllegalArgumentException("수동으로 구매할 로또 수가 전체 로또 수를 초과합니다.\n"); + } + } + + private Lotto createManualLotto() { + try { + return Lotto.from(inputView.inputManualLotto()); + } catch (IllegalArgumentException e) { + outputView.printException(e); + return createManualLotto(); + } + } + private Lotto createWinningLotto() { try { return Lotto.from(inputView.inputWinningLotto()); @@ -50,18 +97,24 @@ private Lotto createWinningLotto() { } private LottoNumber createBonusNumber(Lotto winningLotto) { - LottoNumber bonusNumber = LottoNumber.valueOf(inputView.inputBonusNumber()); - if (winningLotto.containsNumber(bonusNumber)) { - outputView.printExceptionForBonusNumber(); + try { + LottoNumber bonusNumber = LottoNumber.valueOf(inputView.inputBonusNumber()); + validateBonusNumber(winningLotto, bonusNumber); + return bonusNumber; + } catch (IllegalArgumentException e) { + outputView.printException(e); return createBonusNumber(winningLotto); } - outputView.printExceptionForBonusNumber(); - return bonusNumber; + } + + private void validateBonusNumber(Lotto winningLotto, LottoNumber bonusNumber) { + if (winningLotto.containsNumber(bonusNumber)) { + throw new IllegalArgumentException("당첨 번호와 중복되는 보너스 볼은 입력할 수 없습니다."); + } } private void getWinningResult(Price price, Lottos lottos, Lotto winningLotto, LottoNumber bonusNumber) { LottoResult result = createLottoResult(lottos, winningLotto, bonusNumber); - calculateProfitRate(price, result); } diff --git a/src/main/java/org/duckstudy/model/lotto/Lottos.java b/src/main/java/org/duckstudy/model/lotto/Lottos.java index 9c4690e3..fd69d782 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lottos.java +++ b/src/main/java/org/duckstudy/model/lotto/Lottos.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Stream; -import org.duckstudy.model.Price; public class Lottos { @@ -17,9 +16,7 @@ public Lottos(List lottos) { this.lottos = Collections.unmodifiableList(lottos); } - public static Lottos generateLottosByPrice(Price price) { - int lottoCount = price.calculateLottoCount(); - + public static Lottos generateLottos(int lottoCount) { return Stream.generate(Lotto::createRandomLotto) .limit(lottoCount) .collect(collectingAndThen(toList(), Lottos::new)); @@ -31,11 +28,13 @@ public LottoResult accumulateLottoResult(Lotto winningLotto, LottoNumber bonusNu .reduce(new LottoResult(Map.of()), LottoResult::merge); } - public List getLottos() { - return lottos; + public Lottos merge(Lottos other) { + return new Lottos(Stream.of(this.lottos, other.lottos) + .flatMap(List::stream) + .collect(toList())); } - public int getSize() { - return lottos.size(); + public List getLottos() { + return lottos; } } diff --git a/src/main/java/org/duckstudy/view/InputView.java b/src/main/java/org/duckstudy/view/InputView.java index faf356c7..94cb6681 100644 --- a/src/main/java/org/duckstudy/view/InputView.java +++ b/src/main/java/org/duckstudy/view/InputView.java @@ -50,4 +50,22 @@ public int inputBonusNumber() { throw new NumberFormatException("숫자만 입력 가능합니다."); } } + + public int inputManualLottoCount() { + outputView.printInputManualLottoCount(); + + try { + return Integer.parseInt(bufferedReader.readLine()); + } catch (NumberFormatException | IOException e) { + throw new NumberFormatException("숫자만 입력 가능합니다."); + } + } + + public List inputManualLotto() { + try { + return inputLottoNumber(); + } catch (NumberFormatException | IOException e) { + throw new NumberFormatException("숫자만 입력 가능합니다.\n"); + } + } } diff --git a/src/main/java/org/duckstudy/view/OutputView.java b/src/main/java/org/duckstudy/view/OutputView.java index 460b6960..126c61dc 100644 --- a/src/main/java/org/duckstudy/view/OutputView.java +++ b/src/main/java/org/duckstudy/view/OutputView.java @@ -20,8 +20,8 @@ public void printException(Exception e) { System.out.println(e.getMessage()); } - public void printLottos(Lottos lottos) { - System.out.printf("\n%d개를 구매했습니다.\n", lottos.getSize()); + public void printLottos(int manualLottoCount, int autoLottoCount, Lottos lottos) { + System.out.printf("\n수동으로 %d개, 자동으로 %d개를 구매했습니다.\n", manualLottoCount, autoLottoCount); lottos.getLottos() .forEach(this::printLotto); } @@ -81,7 +81,11 @@ public void printTotalProfit(double totalProfitRate) { System.out.printf("총 수익률은 %.2f입니다.\n", totalProfitRate); } - public void printExceptionForBonusNumber() { - System.out.println("보너스 볼은 당첨 번호와 중복되면 안됩니다."); + public void printInputManualLottoCount() { + System.out.println("\n수동으로 구매할 로또 수를 입력해 주세요."); + } + + public void printInputManualLotto() { + System.out.println("\n수동으로 구매할 번호를 입력해 주세요."); } } From 3f1a41a65e285a30c3ac1d453452c7267bec5398 Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 18:01:00 +0900 Subject: [PATCH 08/27] =?UTF-8?q?feat:=20LottoCount=20=EA=B0=92=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로또 개수를 나타내는 LottoCount 값 객체를 생성하여 원시형 int를 포장하고 로또 개수에 대한 로직을 모은다. --- .../duckstudy/controller/LottoController.java | 30 +++++++----------- .../org/duckstudy/model/lotto/LottoCount.java | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 src/main/java/org/duckstudy/model/lotto/LottoCount.java diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index 918ac51f..e86e9aa1 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -4,6 +4,7 @@ import java.util.stream.IntStream; import org.duckstudy.model.Price; import org.duckstudy.model.lotto.Lotto; +import org.duckstudy.model.lotto.LottoCount; import org.duckstudy.model.lotto.LottoNumber; import org.duckstudy.model.lotto.LottoResult; import org.duckstudy.model.lotto.Lottos; @@ -22,16 +23,16 @@ public LottoController(OutputView outputView, InputView inputView) { public void run() { Price price = createPrice(); - int totalLottoCount = price.calculateLottoCount(); + LottoCount totalLottoCount = price.calculateLottoCount(); - int manualLottoCount = getManualLottoCount(totalLottoCount); - Lottos manualLottos = createManualLottos(manualLottoCount); + LottoCount manualLottoCount = createManualLottoCount(totalLottoCount); + Lottos manualLottos = createManualLottos(manualLottoCount.getCount()); - int autoLottoCount = totalLottoCount - manualLottoCount; - Lottos autoLottos = Lottos.generateLottos(autoLottoCount); + LottoCount autoLottoCount = totalLottoCount.subtract(manualLottoCount); + Lottos autoLottos = Lottos.generateLottos(autoLottoCount.getCount()); Lottos totalLottos = manualLottos.merge(autoLottos); - outputView.printLottos(manualLottoCount, autoLottoCount, totalLottos); + outputView.printLottos(manualLottoCount.getCount(), autoLottoCount.getCount(), totalLottos); Lotto winningLotto = createWinningLotto(); LottoNumber bonusNumber = createBonusNumber(winningLotto); @@ -58,23 +59,14 @@ private Lottos createManualLottos(int manualLottoCount) { .collect(Collectors.toList())); } - private int getManualLottoCount(int lottoCount) { + private LottoCount createManualLottoCount(LottoCount lottoCount) { try { - int manualLottoCount = inputView.inputManualLottoCount(); - validateManualLottoCount(lottoCount, manualLottoCount); + LottoCount manualLottoCount = new LottoCount(inputView.inputManualLottoCount()); + manualLottoCount.validateManualLottoCount(lottoCount.getCount()); return manualLottoCount; } catch (IllegalArgumentException e) { outputView.printException(e); - return getManualLottoCount(lottoCount); - } - } - - private void validateManualLottoCount(int lottoCount, int manualLottoCount) { - if (manualLottoCount < 0) { - throw new IllegalArgumentException("음수로 입력할 수 없습니다.\n"); - } - if (manualLottoCount > lottoCount) { - throw new IllegalArgumentException("수동으로 구매할 로또 수가 전체 로또 수를 초과합니다.\n"); + return createManualLottoCount(lottoCount); } } diff --git a/src/main/java/org/duckstudy/model/lotto/LottoCount.java b/src/main/java/org/duckstudy/model/lotto/LottoCount.java new file mode 100644 index 00000000..3b50a816 --- /dev/null +++ b/src/main/java/org/duckstudy/model/lotto/LottoCount.java @@ -0,0 +1,31 @@ +package org.duckstudy.model.lotto; + +public class LottoCount { + + private final int count; + + public LottoCount(int count) { + validateLottoCount(count); + this.count = count; + } + + private void validateLottoCount(int count) { + if (count < 0) { + throw new IllegalArgumentException("로또 개수는 0개 이상이어야 합니다."); + } + } + + public void validateManualLottoCount(int totalLottoCount) { + if (count > totalLottoCount) { + throw new IllegalArgumentException("수동으로 구매할 로또 수가 전체 로또 수를 초과합니다.\n"); + } + } + + public LottoCount subtract(LottoCount lottoCount) { + return new LottoCount(count - lottoCount.count); + } + + public int getCount() { + return count; + } +} From e3c81917b0379e15d7484858dfc695622d6e8e83 Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 18:11:14 +0900 Subject: [PATCH 09/27] =?UTF-8?q?refactor:=20inputView=EC=97=90=EC=84=9C?= =?UTF-8?q?=20outputView=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - inputView는 입력 받는 역할에 집중하고, controller에서 inputView와 outputView를 모두 의존하도록 조정한다. --- src/main/java/org/duckstudy/Application.java | 2 +- .../org/duckstudy/controller/LottoController.java | 4 ++++ src/main/java/org/duckstudy/view/InputView.java | 11 +---------- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/duckstudy/Application.java b/src/main/java/org/duckstudy/Application.java index 811618ef..53873c44 100644 --- a/src/main/java/org/duckstudy/Application.java +++ b/src/main/java/org/duckstudy/Application.java @@ -10,7 +10,7 @@ public class Application { public static void main(String[] args) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); OutputView outputView = new OutputView(); - InputView inputView = new InputView(bufferedReader, outputView); + InputView inputView = new InputView(bufferedReader); LottoController lottoController = new LottoController(outputView, inputView); lottoController.run(); diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index e86e9aa1..6baca258 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -41,6 +41,7 @@ public void run() { } private Price createPrice() { + outputView.printInputPrice(); try { Price price = new Price(inputView.inputPrice()); price.validateInputPrice(); @@ -60,6 +61,7 @@ private Lottos createManualLottos(int manualLottoCount) { } private LottoCount createManualLottoCount(LottoCount lottoCount) { + outputView.printInputManualLottoCount(); try { LottoCount manualLottoCount = new LottoCount(inputView.inputManualLottoCount()); manualLottoCount.validateManualLottoCount(lottoCount.getCount()); @@ -80,6 +82,7 @@ private Lotto createManualLotto() { } private Lotto createWinningLotto() { + outputView.printInputWinningLotto(); try { return Lotto.from(inputView.inputWinningLotto()); } catch (IllegalArgumentException e) { @@ -89,6 +92,7 @@ private Lotto createWinningLotto() { } private LottoNumber createBonusNumber(Lotto winningLotto) { + outputView.printInputBonusNumber(); try { LottoNumber bonusNumber = LottoNumber.valueOf(inputView.inputBonusNumber()); validateBonusNumber(winningLotto, bonusNumber); diff --git a/src/main/java/org/duckstudy/view/InputView.java b/src/main/java/org/duckstudy/view/InputView.java index 94cb6681..bc241c77 100644 --- a/src/main/java/org/duckstudy/view/InputView.java +++ b/src/main/java/org/duckstudy/view/InputView.java @@ -8,15 +8,12 @@ public class InputView { private final BufferedReader bufferedReader; - private final OutputView outputView; - public InputView(BufferedReader bufferedReader, OutputView outputView) { + public InputView(BufferedReader bufferedReader) { this.bufferedReader = bufferedReader; - this.outputView = outputView; } public int inputPrice() { - outputView.printInputPrice(); try { return Integer.parseInt(bufferedReader.readLine()); } catch (NumberFormatException | IOException e) { @@ -25,8 +22,6 @@ public int inputPrice() { } public List inputWinningLotto() { - outputView.printInputWinningLotto(); - try { return inputLottoNumber(); } catch (NumberFormatException | IOException e) { @@ -42,8 +37,6 @@ private List inputLottoNumber() throws IOException { } public int inputBonusNumber() { - outputView.printInputBonusNumber(); - try { return Integer.parseInt(bufferedReader.readLine()); } catch (NumberFormatException | IOException e) { @@ -52,8 +45,6 @@ public int inputBonusNumber() { } public int inputManualLottoCount() { - outputView.printInputManualLottoCount(); - try { return Integer.parseInt(bufferedReader.readLine()); } catch (NumberFormatException | IOException e) { From f01e1ac6a22c6cbf5d605a5cbffc7543b6d613b1 Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 18:16:12 +0900 Subject: [PATCH 10/27] =?UTF-8?q?fix:=20=EA=B0=80=EA=B2=A9=20=EB=B0=B0?= =?UTF-8?q?=EC=88=98=20=EB=8D=94=ED=95=98=EA=B8=B0=20=EB=B2=84=EA=B7=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - count가 0일 경우 기존 price가 0원이 되므로 더한 다음 곱하는 것이 아닌 곱한 가격을 더하도록 수정한다. --- src/main/java/org/duckstudy/model/Price.java | 8 ++++---- src/test/java/org/duckstudy/model/PriceTest.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/duckstudy/model/Price.java b/src/main/java/org/duckstudy/model/Price.java index d69241f1..f12e02dd 100644 --- a/src/main/java/org/duckstudy/model/Price.java +++ b/src/main/java/org/duckstudy/model/Price.java @@ -1,6 +1,7 @@ package org.duckstudy.model; import java.util.Objects; +import org.duckstudy.model.lotto.LottoCount; import org.duckstudy.model.lotto.LottoResult; import org.duckstudy.model.lotto.constant.WinningRank; @@ -53,9 +54,9 @@ private void checkIfZero(int divisor) { } } - public int calculateLottoCount() { + public LottoCount calculateLottoCount() { checkIfZero(LOTTO_PRICE); - return value / LOTTO_PRICE; + return new LottoCount(value / LOTTO_PRICE); } public double calculateProfitRate(LottoResult result) { @@ -67,8 +68,7 @@ public double calculateProfitRate(LottoResult result) { } private Price accumulateProfit(WinningRank winningRank, int count) { - return this.addPrice(winningRank.getPrice()) - .multiplyTimes(count); + return this.addPrice(winningRank.getPrice() * count); } public int getValue() { diff --git a/src/test/java/org/duckstudy/model/PriceTest.java b/src/test/java/org/duckstudy/model/PriceTest.java index df9875ab..8d2ab347 100644 --- a/src/test/java/org/duckstudy/model/PriceTest.java +++ b/src/test/java/org/duckstudy/model/PriceTest.java @@ -104,10 +104,10 @@ void calculateLottoCountWhenInputPrice() { void calculateProfitRate() { Price purchasePrice = new Price(15000); Map result = new HashMap<>(); - result.put(WinningRank.FIRST.getKey(), 1); + result.put(WinningRank.SECOND.getKey(), 1); LottoResult lottoResult = new LottoResult(result); - assertThat(purchasePrice.calculateProfitRate(lottoResult)).isEqualTo(1.3333333333333334E7); + assertThat(purchasePrice.calculateProfitRate(lottoResult)).isEqualTo(200000.0); } } } From 2b0574270f40656994331cf58396b9859aa0f57d Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 18:19:37 +0900 Subject: [PATCH 11/27] =?UTF-8?q?refactor:=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 인자명 수정 - 테스트 코드 검증값 올바르게 수정 - 테스트 설명 수정 --- src/main/java/org/duckstudy/model/lotto/LottoResult.java | 4 ++-- src/test/java/org/duckstudy/model/PriceTest.java | 2 +- src/test/java/org/duckstudy/model/lotto/LottosTest.java | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/duckstudy/model/lotto/LottoResult.java b/src/main/java/org/duckstudy/model/lotto/LottoResult.java index 8afd3106..6588f1ab 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoResult.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoResult.java @@ -36,8 +36,8 @@ public LottoResult merge(LottoResult other) { ))); } - public int getMatchingCount(int count) { - return result.getOrDefault(count, DEFAULT_VALUE); + public int getMatchingCount(int key) { + return result.getOrDefault(key, DEFAULT_VALUE); } public Map getResult() { diff --git a/src/test/java/org/duckstudy/model/PriceTest.java b/src/test/java/org/duckstudy/model/PriceTest.java index 8d2ab347..f92adb41 100644 --- a/src/test/java/org/duckstudy/model/PriceTest.java +++ b/src/test/java/org/duckstudy/model/PriceTest.java @@ -96,7 +96,7 @@ void calculateLottoCountWhenInputPrice() { Price price = new Price(10000); - assertThat(price.calculateLottoCount()).isEqualTo(10); + assertThat(price.calculateLottoCount().getCount()).isEqualTo(10); } @Test diff --git a/src/test/java/org/duckstudy/model/lotto/LottosTest.java b/src/test/java/org/duckstudy/model/lotto/LottosTest.java index d71b4715..c2c5c477 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottosTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottosTest.java @@ -20,12 +20,14 @@ class LottosTest { class LottosCreationTest { @Test - @DisplayName("구매 가격을 입력하면 가격에 맞는 로또 묶음을 생성한다") + @DisplayName("구매 개수를 입력하면 가격에 맞는 로또 묶음을 생성한다") void createLottosWhenInputPrice() { Price purchasePrice = new Price(10000); + int lottoCount = purchasePrice.calculateLottoCount() + .getCount(); - Lottos lottos = Lottos.generateLottosByPrice(purchasePrice); + Lottos lottos = Lottos.generateLottos(lottoCount); assertThat(lottos.getLottos()).hasSize(10); } From 48e7debc186f98c64674c417eb06165ec9233c3c Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 18:51:35 +0900 Subject: [PATCH 12/27] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=2010=EC=A4=84=20=EC=9D=B4=ED=95=98=EB=A1=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../duckstudy/controller/LottoController.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index 6baca258..3c5f44a3 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -23,21 +23,25 @@ public LottoController(OutputView outputView, InputView inputView) { public void run() { Price price = createPrice(); - LottoCount totalLottoCount = price.calculateLottoCount(); + Lottos totalLottos = createTotalLottos(price); + + Lotto winningLotto = createWinningLotto(); + LottoNumber bonusNumber = createBonusNumber(winningLotto); + getWinningResult(price, totalLottos, winningLotto, bonusNumber); + } + + private Lottos createTotalLottos(Price price) { + LottoCount totalLottoCount = price.calculateLottoCount(); LottoCount manualLottoCount = createManualLottoCount(totalLottoCount); Lottos manualLottos = createManualLottos(manualLottoCount.getCount()); LottoCount autoLottoCount = totalLottoCount.subtract(manualLottoCount); Lottos autoLottos = Lottos.generateLottos(autoLottoCount.getCount()); - Lottos totalLottos = manualLottos.merge(autoLottos); - outputView.printLottos(manualLottoCount.getCount(), autoLottoCount.getCount(), totalLottos); - Lotto winningLotto = createWinningLotto(); - LottoNumber bonusNumber = createBonusNumber(winningLotto); - - getWinningResult(price, totalLottos, winningLotto, bonusNumber); + outputView.printLottos(manualLottoCount.getCount(), autoLottoCount.getCount(), totalLottos); + return totalLottos; } private Price createPrice() { From 0232fab530510a2f8a830c2cc0290f332bb6dcec Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 19:04:42 +0900 Subject: [PATCH 13/27] =?UTF-8?q?test:=20LottoCount=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/duckstudy/model/lotto/LottoCount.java | 19 +++++ .../duckstudy/model/lotto/LottoCountTest.java | 77 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/test/java/org/duckstudy/model/lotto/LottoCountTest.java diff --git a/src/main/java/org/duckstudy/model/lotto/LottoCount.java b/src/main/java/org/duckstudy/model/lotto/LottoCount.java index 3b50a816..c1a6b856 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoCount.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoCount.java @@ -1,5 +1,7 @@ package org.duckstudy.model.lotto; +import java.util.Objects; + public class LottoCount { private final int count; @@ -28,4 +30,21 @@ public LottoCount subtract(LottoCount lottoCount) { public int getCount() { return count; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LottoCount that = (LottoCount) o; + return count == that.count; + } + + @Override + public int hashCode() { + return Objects.hash(count); + } } diff --git a/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java b/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java new file mode 100644 index 00000000..9576f5b1 --- /dev/null +++ b/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java @@ -0,0 +1,77 @@ +package org.duckstudy.model.lotto; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +@DisplayName("로또 개수 테스트") +class LottoCountTest { + + @Nested + @DisplayName("로또 개수 생성 테스트") + class LottoCountCreationTest { + + @Test + @DisplayName("로또 개수를 입력하면 로또 개수를 생성한다") + public void createLottoCountWhenInputCount() { + + assertThatCode(() -> new LottoCount(10)) + .doesNotThrowAnyException(); + } + + @Test + @DisplayName("로또 개수가 음수일 경우 예외가 발생한다") + public void createLottoCountFailWhenCountIsLessThanZero() { + + assertThatThrownBy(() -> new LottoCount(-1)) + .isExactlyInstanceOf(IllegalArgumentException.class) + .hasMessage("로또 개수는 0개 이상이어야 합니다."); + } + } + + @Nested + @DisplayName("수동 로또 개수 검증 테스트") + class ManualLottoCountValidationTest { + + @Test + @DisplayName("수동으로 구매할 로또 수가 전체 로또 수를 초과하지 않으면 성공한다") + public void validateManualLottoCountSuccessWhenManualLottoCountIsNotGreaterThanTotalLottoCount() { + + LottoCount manualLottoCount = new LottoCount(10); + + assertThatCode(() -> manualLottoCount.validateManualLottoCount(10)) + .doesNotThrowAnyException(); + } + + @Test + @DisplayName("수동으로 구매할 로또 수가 전체 로또 수를 초과하면 예외가 발생한다") + public void validateManualLottoCountFailWhenManualLottoCountIsGreaterThanTotalLottoCount() { + + LottoCount lottoCount = new LottoCount(10); + + assertThatThrownBy(() -> lottoCount.validateManualLottoCount(5)) + .isExactlyInstanceOf(IllegalArgumentException.class) + .hasMessage("수동으로 구매할 로또 수가 전체 로또 수를 초과합니다.\n"); + } + } + + @Nested + @DisplayName("로또 개수 계산 테스트") + class LottoCountCalculateTest { + + @Test + @DisplayName("로또 개수를 뺀다") + public void subtractLottoCount() { + + LottoCount lottoCount = new LottoCount(10); + + LottoCount result = lottoCount.subtract(new LottoCount(5)); + + assertThat(result).isEqualTo(new LottoCount(5)); + } + } +} From 49c785c870903a22d5d6bcb89dc413f44964e4d6 Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 19:16:58 +0900 Subject: [PATCH 14/27] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/duckstudy/model/PriceTest.java | 41 +++++++++++++++++++ .../org/duckstudy/model/lotto/LottosTest.java | 22 ++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/test/java/org/duckstudy/model/PriceTest.java b/src/test/java/org/duckstudy/model/PriceTest.java index f92adb41..c03e6716 100644 --- a/src/test/java/org/duckstudy/model/PriceTest.java +++ b/src/test/java/org/duckstudy/model/PriceTest.java @@ -37,6 +37,47 @@ void validateFailWhenPriceIsLessThanZero() { } } + @Nested + @DisplayName("입력 가격 검증 테스트") + class InputPriceValidationTest { + + @Test + @DisplayName("입력 가격이 양수일 경우 성공한다") + void validateInputPriceSuccessWhenPriceIsEqualOrGreaterThanZero() { + + Price price = new Price(1); + + assertThatCode(price::validateInputPrice) + .doesNotThrowAnyException(); + } + + @Test + @DisplayName("입력 가격이 0 이하일 경우 예외가 발생한다") + void validateInputPriceFailWhenPriceIsZero() { + + Price price = new Price(0); + + assertThatThrownBy(price::validateInputPrice) + .isExactlyInstanceOf(IllegalArgumentException.class) + .hasMessage("가격은 0원 이상이어야 합니다.\n"); + } + } + + @Nested + @DisplayName("가격 생성 테스트") + class PriceCreationTest { + + @Test + @DisplayName("0원으로 가격을 생성하면 성공한다") + void createPriceWhenZero() { + + Price price = Price.zero(); + + assertThat(price).isEqualTo(new Price(0)); + } + } + + @Nested @DisplayName("가격 계산 테스트") class PriceCalculateTest { diff --git a/src/test/java/org/duckstudy/model/lotto/LottosTest.java b/src/test/java/org/duckstudy/model/lotto/LottosTest.java index c2c5c477..4a81f6cc 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottosTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottosTest.java @@ -61,4 +61,26 @@ void calculateWinningResultWhenInputWinningLotto() { ); } } + + @Nested + @DisplayName("로또 묶음 병합 테스트") + class LottosMergeTest { + + @Test + @DisplayName("로또 묶음을 병합하면 하나의 로또 묶음으로 생성한다") + void mergeLottos() { + + Lottos lottos1 = new Lottos(List.of( + Lotto.from(List.of(1, 2, 3, 4, 5, 6)) + )); + + Lottos lottos2 = new Lottos(List.of( + Lotto.from(List.of(7, 8, 9, 10, 11, 12)) + )); + + Lottos mergedLottos = lottos1.merge(lottos2); + + assertThat(mergedLottos.getLottos()).hasSize(2); + } + } } From c61bd4927ebd31e243d73e620e19e3f6a790ccdd Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 19:18:00 +0900 Subject: [PATCH 15/27] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/duckstudy/model/Price.java | 4 ---- src/test/java/org/duckstudy/model/PriceTest.java | 11 ----------- 2 files changed, 15 deletions(-) diff --git a/src/main/java/org/duckstudy/model/Price.java b/src/main/java/org/duckstudy/model/Price.java index f12e02dd..8d262a26 100644 --- a/src/main/java/org/duckstudy/model/Price.java +++ b/src/main/java/org/duckstudy/model/Price.java @@ -39,10 +39,6 @@ public Price addPrice(int value) { return new Price(this.value + value); } - public Price multiplyTimes(int times) { - return new Price(value * times); - } - public double divideByPrice(Price divisor) { checkIfZero(divisor.getValue()); return (double) value / divisor.getValue(); diff --git a/src/test/java/org/duckstudy/model/PriceTest.java b/src/test/java/org/duckstudy/model/PriceTest.java index c03e6716..e7fdf5db 100644 --- a/src/test/java/org/duckstudy/model/PriceTest.java +++ b/src/test/java/org/duckstudy/model/PriceTest.java @@ -93,17 +93,6 @@ void addPrice() { assertThat(result).isEqualTo(new Price(3000)); } - @Test - @DisplayName("가격을 곱한다") - void multiplyPrice() { - - Price price = new Price(1000); - - Price result = price.multiplyTimes(3); - - assertThat(result).isEqualTo(new Price(3000)); - } - @Test @DisplayName("가격을 나눈다") void dividePriceByPrice() { From 17e4d696ea3831b8773b7c81c021f40444ac1899 Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 22:05:50 +0900 Subject: [PATCH 16/27] =?UTF-8?q?refactor:=20Lotto=20=EB=82=B4=EB=B6=80=20?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98=20Set=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - List에서 Set으로 변경하면 내부 원소들의 중복 여부를 확인하지 않아도 된다. --- .../java/org/duckstudy/model/lotto/Lotto.java | 31 ++++++-------- .../java/org/duckstudy/view/InputView.java | 12 +++--- .../model/lotto/LottoResultTest.java | 6 +-- .../org/duckstudy/model/lotto/LottoTest.java | 40 ++++++++----------- .../org/duckstudy/model/lotto/LottosTest.java | 15 +++---- 5 files changed, 47 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/duckstudy/model/lotto/Lotto.java b/src/main/java/org/duckstudy/model/lotto/Lotto.java index 4a489d88..e661bb59 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lotto.java +++ b/src/main/java/org/duckstudy/model/lotto/Lotto.java @@ -3,38 +3,39 @@ import static java.util.Collections.shuffle; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; import static org.duckstudy.model.lotto.LottoNumber.END_INCLUSIVE_NUMBER; import static org.duckstudy.model.lotto.LottoNumber.START_INCLUSIVE_NUMBER; import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.stream.Collector; import java.util.stream.IntStream; import java.util.stream.Stream; public class Lotto { - private static final List NUMBERS; + private static final Set NUMBERS; private static final int LOTTO_SIZE = 6; static { NUMBERS = IntStream.range(START_INCLUSIVE_NUMBER, END_INCLUSIVE_NUMBER + 1) .boxed() - .toList(); + .collect(toSet()); } - private final List lotto; + private final Set lotto; - public Lotto(List lotto) { + public Lotto(Set lotto) { validateLottoSize(lotto); - validateDuplicate(lotto); - this.lotto = Collections.unmodifiableList(lotto); + this.lotto = Collections.unmodifiableSet(lotto); } - public static Lotto from(List values) { + public static Lotto from(Set values) { return new Lotto(values.stream() .map(LottoNumber::valueOf) - .collect(toList())); + .collect(toSet())); } public static Lotto createRandomLotto() { @@ -43,7 +44,7 @@ public static Lotto createRandomLotto() { .limit(LOTTO_SIZE) .sorted() .map(LottoNumber::valueOf) - .collect(toList())); + .collect(toSet())); } private static Collector> getCollector() { @@ -65,19 +66,13 @@ public boolean containsNumber(LottoNumber lottoNumber) { return lotto.contains(lottoNumber); } - private void validateLottoSize(List lotto) { + private void validateLottoSize(Set lotto) { if (lotto.size() != LOTTO_SIZE) { - throw new IllegalArgumentException(String.format("로또 번호는 %d개여야 합니다.", LOTTO_SIZE)); + throw new IllegalArgumentException(String.format("로또 번호는 중복되지 않은 %d개여야 합니다.\n", LOTTO_SIZE)); } } - private void validateDuplicate(List lotto) { - if (lotto.stream().distinct().count() != LOTTO_SIZE) { - throw new IllegalArgumentException("로또 번호는 중복되지 않아야 합니다."); - } - } - - public List getLotto() { + public Set getLotto() { return lotto; } } diff --git a/src/main/java/org/duckstudy/view/InputView.java b/src/main/java/org/duckstudy/view/InputView.java index bc241c77..2ca9ed75 100644 --- a/src/main/java/org/duckstudy/view/InputView.java +++ b/src/main/java/org/duckstudy/view/InputView.java @@ -1,9 +1,11 @@ package org.duckstudy.view; +import static java.util.stream.Collectors.toSet; + import java.io.BufferedReader; import java.io.IOException; import java.util.Arrays; -import java.util.List; +import java.util.Set; public class InputView { @@ -21,7 +23,7 @@ public int inputPrice() { } } - public List inputWinningLotto() { + public Set inputWinningLotto() { try { return inputLottoNumber(); } catch (NumberFormatException | IOException e) { @@ -29,11 +31,11 @@ public List inputWinningLotto() { } } - private List inputLottoNumber() throws IOException { + private Set inputLottoNumber() throws IOException { return Arrays.stream(bufferedReader.readLine().split(",")) .map(String::trim) .map(Integer::parseInt) - .toList(); + .collect(toSet()); } public int inputBonusNumber() { @@ -52,7 +54,7 @@ public int inputManualLottoCount() { } } - public List inputManualLotto() { + public Set inputManualLotto() { try { return inputLottoNumber(); } catch (NumberFormatException | IOException e) { diff --git a/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java b/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java index 12624b4b..478b9963 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java @@ -2,8 +2,8 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.util.List; import java.util.Map; +import java.util.Set; import org.duckstudy.model.lotto.constant.WinningRank; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -15,8 +15,8 @@ class LottoResultTest { @DisplayName("로또 결과를 생성한다") void createLottoResult() { - Lotto lotto = Lotto.from(List.of(1, 2, 3, 4, 5, 7)); - Lotto winningLotto = Lotto.from(List.of(1, 2, 3, 4, 5, 6)); + Lotto lotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 7)); + Lotto winningLotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 6)); LottoNumber bonusNumber = LottoNumber.valueOf(7); LottoResult lottoResult = LottoResult.createLottoResult(lotto, winningLotto, bonusNumber); diff --git a/src/test/java/org/duckstudy/model/lotto/LottoTest.java b/src/test/java/org/duckstudy/model/lotto/LottoTest.java index ee561748..745c8926 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottoTest.java @@ -1,10 +1,11 @@ package org.duckstudy.model.lotto; +import static java.util.stream.Collectors.toSet; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import java.util.List; +import java.util.Set; import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -25,9 +26,9 @@ class LottoCreationFromLottoNumberTest { @DisplayName("로또를 생성한다") void createLottoSuccess() { - List lottoNumbers = Stream.of(1, 2, 3, 4, 5, 6) + Set lottoNumbers = Stream.of(1, 2, 3, 4, 5, 6) .map(LottoNumber::valueOf) - .toList(); + .collect(toSet()); assertThatCode(() -> new Lotto(lottoNumbers)) .doesNotThrowAnyException(); @@ -37,26 +38,26 @@ void createLottoSuccess() { @DisplayName("로또를 생성할 때 6개의 로또 번호가 아니면 예외를 발생한다") void createLottoFailWhenSizeIsNotSix() { - List lottoNumbers = List.of( + Set lottoNumbers = Set.of( LottoNumber.valueOf(1) ); assertThatThrownBy(() -> new Lotto(lottoNumbers)) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("로또 번호는 6개여야 합니다."); + .hasMessage("로또 번호는 중복되지 않은 6개여야 합니다.\n"); } @Test @DisplayName("로또를 생성할 때 중복된 로또 번호가 있으면 예외를 발생한다") void createLottoFailWhenDuplicateNumberExists() { - List lottoNumbers = Stream.of(1, 1, 2, 3, 4, 5) + Set lottoNumbers = Stream.of(1, 1, 2, 3, 4, 5) .map(LottoNumber::valueOf) - .toList(); + .collect(toSet()); assertThatThrownBy(() -> new Lotto(lottoNumbers)) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("로또 번호는 중복되지 않아야 합니다."); + .hasMessage("로또 번호는 중복되지 않은 6개여야 합니다.\n"); } } @@ -68,26 +69,17 @@ class LottoCreationFromIntegerTest { @DisplayName("로또를 생성한다") void createLottoSuccess() { - assertThatCode(() -> Lotto.from(List.of(1, 2, 3, 4, 5, 6))) + assertThatCode(() -> Lotto.from(Set.of(1, 2, 3, 4, 5, 6))) .doesNotThrowAnyException(); } @Test - @DisplayName("로또를 생성할 때 6개의 로또 번호가 아니면 예외를 발생한다") + @DisplayName("로또를 생성할 때 중복되지 않은 6개의 로또 번호가 아니면 예외를 발생한다") void createLottoFailWhenSizeIsNotSix() { - assertThatThrownBy(() -> Lotto.from(List.of(1))) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("로또 번호는 6개여야 합니다."); - } - - @Test - @DisplayName("로또를 생성할 때 중복된 로또 번호가 있으면 예외를 발생한다") - void createLottoFailWhenDuplicateNumberExists() { - - assertThatThrownBy(() -> Lotto.from(List.of(1, 1, 3, 4, 5, 6))) + assertThatThrownBy(() -> Lotto.from(Set.of(1))) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("로또 번호는 중복되지 않아야 합니다."); + .hasMessage("로또 번호는 중복되지 않은 6개여야 합니다.\n"); } } @@ -113,8 +105,8 @@ class LottoComparisonTest { @DisplayName("일치하는 로또 번호의 개수를 반환한다") void countMatchingNumber() { - Lotto lotto = Lotto.from(List.of(1, 2, 3, 4, 5, 6)); - Lotto compareLotto = Lotto.from(List.of(1, 2, 3, 7, 8, 9)); + Lotto lotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 6)); + Lotto compareLotto = Lotto.from(Set.of(1, 2, 3, 7, 8, 9)); assertThat(lotto.countMatchingNumber(compareLotto)).isEqualTo(3); } @@ -128,7 +120,7 @@ class LottoContainsTest { @DisplayName("로또 번호가 포함되어 있는지 확인한다") void containsNumber() { - Lotto lotto = Lotto.from(List.of(1, 2, 3, 4, 5, 6)); + Lotto lotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 6)); LottoNumber lottoNumber = LottoNumber.valueOf(3); assertThat(lotto.containsNumber(lottoNumber)).isTrue(); diff --git a/src/test/java/org/duckstudy/model/lotto/LottosTest.java b/src/test/java/org/duckstudy/model/lotto/LottosTest.java index 4a81f6cc..36340a57 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottosTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottosTest.java @@ -7,6 +7,7 @@ import static org.duckstudy.model.lotto.constant.WinningRank.SECOND; import java.util.List; +import java.util.Set; import org.duckstudy.model.Price; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -41,14 +42,14 @@ class LottosWinningTest { @DisplayName("당첨된 로또 번호와 보너스 번호를 입력하면 당첨된 로또 묶음의 결과를 계산한다") void calculateWinningResultWhenInputWinningLotto() { - Lotto winningLotto = Lotto.from(List.of(1, 2, 3, 4, 5, 6)); + Lotto winningLotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 6)); LottoNumber bonusNumber = LottoNumber.valueOf(7); Lottos totalLottos = new Lottos(List.of( winningLotto, - Lotto.from(List.of(1, 2, 3, 4, 5, 7)), - Lotto.from(List.of(8, 9, 10, 11, 12, 13)), - Lotto.from(List.of(14, 15, 16, 17, 18, 19)), - Lotto.from(List.of(20, 21, 22, 23, 24, 25)) + Lotto.from(Set.of(1, 2, 3, 4, 5, 7)), + Lotto.from(Set.of(8, 9, 10, 11, 12, 13)), + Lotto.from(Set.of(14, 15, 16, 17, 18, 19)), + Lotto.from(Set.of(20, 21, 22, 23, 24, 25)) )); LottoResult lottoResult = totalLottos.accumulateLottoResult(winningLotto, bonusNumber); @@ -71,11 +72,11 @@ class LottosMergeTest { void mergeLottos() { Lottos lottos1 = new Lottos(List.of( - Lotto.from(List.of(1, 2, 3, 4, 5, 6)) + Lotto.from(Set.of(1, 2, 3, 4, 5, 6)) )); Lottos lottos2 = new Lottos(List.of( - Lotto.from(List.of(7, 8, 9, 10, 11, 12)) + Lotto.from(Set.of(7, 8, 9, 10, 11, 12)) )); Lottos mergedLottos = lottos1.merge(lottos2); From dda74be7b2081ab2b12528f7f9e115e1b22f4672 Mon Sep 17 00:00:00 2001 From: Mint Date: Sat, 22 Jun 2024 22:21:58 +0900 Subject: [PATCH 17/27] =?UTF-8?q?refactor:=20=EB=A7=A4=EA=B0=9C=EB=B3=80?= =?UTF-8?q?=EC=88=98=EC=97=90=20final=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=EB=B6=88=EB=B3=80=EC=84=B1=20=ED=91=9C=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../duckstudy/controller/LottoController.java | 20 ++++++++++--------- src/main/java/org/duckstudy/model/Price.java | 14 ++++++------- .../java/org/duckstudy/model/lotto/Lotto.java | 10 +++++----- .../org/duckstudy/model/lotto/LottoCount.java | 8 ++++---- .../duckstudy/model/lotto/LottoNumber.java | 6 +++--- .../duckstudy/model/lotto/LottoResult.java | 9 +++++---- .../org/duckstudy/model/lotto/Lottos.java | 8 ++++---- .../model/lotto/constant/WinningRank.java | 7 ++++--- .../java/org/duckstudy/view/OutputView.java | 16 +++++++-------- 9 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index 3c5f44a3..ebbe2812 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -16,7 +16,7 @@ public class LottoController { private final OutputView outputView; private final InputView inputView; - public LottoController(OutputView outputView, InputView inputView) { + public LottoController(final OutputView outputView, final InputView inputView) { this.outputView = outputView; this.inputView = inputView; } @@ -31,7 +31,7 @@ public void run() { getWinningResult(price, totalLottos, winningLotto, bonusNumber); } - private Lottos createTotalLottos(Price price) { + private Lottos createTotalLottos(final Price price) { LottoCount totalLottoCount = price.calculateLottoCount(); LottoCount manualLottoCount = createManualLottoCount(totalLottoCount); Lottos manualLottos = createManualLottos(manualLottoCount.getCount()); @@ -56,7 +56,7 @@ private Price createPrice() { } } - private Lottos createManualLottos(int manualLottoCount) { + private Lottos createManualLottos(final int manualLottoCount) { outputView.printInputManualLotto(); return new Lottos(IntStream.range(0, manualLottoCount) @@ -64,7 +64,7 @@ private Lottos createManualLottos(int manualLottoCount) { .collect(Collectors.toList())); } - private LottoCount createManualLottoCount(LottoCount lottoCount) { + private LottoCount createManualLottoCount(final LottoCount lottoCount) { outputView.printInputManualLottoCount(); try { LottoCount manualLottoCount = new LottoCount(inputView.inputManualLottoCount()); @@ -95,7 +95,7 @@ private Lotto createWinningLotto() { } } - private LottoNumber createBonusNumber(Lotto winningLotto) { + private LottoNumber createBonusNumber(final Lotto winningLotto) { outputView.printInputBonusNumber(); try { LottoNumber bonusNumber = LottoNumber.valueOf(inputView.inputBonusNumber()); @@ -107,24 +107,26 @@ private LottoNumber createBonusNumber(Lotto winningLotto) { } } - private void validateBonusNumber(Lotto winningLotto, LottoNumber bonusNumber) { + private void validateBonusNumber(final Lotto winningLotto, final LottoNumber bonusNumber) { if (winningLotto.containsNumber(bonusNumber)) { throw new IllegalArgumentException("당첨 번호와 중복되는 보너스 볼은 입력할 수 없습니다."); } } - private void getWinningResult(Price price, Lottos lottos, Lotto winningLotto, LottoNumber bonusNumber) { + private void getWinningResult(final Price price, final Lottos lottos, final Lotto winningLotto, + final LottoNumber bonusNumber) { LottoResult result = createLottoResult(lottos, winningLotto, bonusNumber); calculateProfitRate(price, result); } - private LottoResult createLottoResult(Lottos lottos, Lotto winningLotto, LottoNumber bonusNumber) { + private LottoResult createLottoResult(final Lottos lottos, final Lotto winningLotto, + final LottoNumber bonusNumber) { LottoResult result = lottos.accumulateLottoResult(winningLotto, bonusNumber); outputView.printLottoResult(result); return result; } - private void calculateProfitRate(Price price, LottoResult result) { + private void calculateProfitRate(final Price price, final LottoResult result) { double profitRate = price.calculateProfitRate(result); outputView.printTotalProfit(profitRate); } diff --git a/src/main/java/org/duckstudy/model/Price.java b/src/main/java/org/duckstudy/model/Price.java index 8d262a26..72929800 100644 --- a/src/main/java/org/duckstudy/model/Price.java +++ b/src/main/java/org/duckstudy/model/Price.java @@ -14,7 +14,7 @@ public class Price { private final int value; - public Price(int price) { + public Price(final int price) { validatePrice(price); this.value = price; } @@ -23,7 +23,7 @@ public static Price zero() { return new Price(INCLUSIVE_MIN_PRICE); } - private void validatePrice(int price) { + private void validatePrice(final int price) { if (price < INCLUSIVE_MIN_PRICE) { throw new IllegalArgumentException(String.format("가격은 %d원 이상이어야 합니다.", INCLUSIVE_MIN_PRICE)); } @@ -35,16 +35,16 @@ public void validateInputPrice() { } } - public Price addPrice(int value) { + public Price addPrice(final int value) { return new Price(this.value + value); } - public double divideByPrice(Price divisor) { + public double divideByPrice(final Price divisor) { checkIfZero(divisor.getValue()); return (double) value / divisor.getValue(); } - private void checkIfZero(int divisor) { + private void checkIfZero(final int divisor) { if (divisor == ZERO) { throw new IllegalArgumentException(String.format("%d으로 나눌 수 없습니다.", ZERO)); } @@ -55,7 +55,7 @@ public LottoCount calculateLottoCount() { return new LottoCount(value / LOTTO_PRICE); } - public double calculateProfitRate(LottoResult result) { + public double calculateProfitRate(final LottoResult result) { Price profit = Price.zero(); for (WinningRank winningRank : WinningRank.values()) { profit = profit.accumulateProfit(winningRank, result.getMatchingCount(winningRank.getKey())); @@ -63,7 +63,7 @@ public double calculateProfitRate(LottoResult result) { return profit.divideByPrice(this) * PERCENT_BASE; } - private Price accumulateProfit(WinningRank winningRank, int count) { + private Price accumulateProfit(final WinningRank winningRank, final int count) { return this.addPrice(winningRank.getPrice() * count); } diff --git a/src/main/java/org/duckstudy/model/lotto/Lotto.java b/src/main/java/org/duckstudy/model/lotto/Lotto.java index e661bb59..6ce4ce60 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lotto.java +++ b/src/main/java/org/duckstudy/model/lotto/Lotto.java @@ -27,12 +27,12 @@ public class Lotto { private final Set lotto; - public Lotto(Set lotto) { + public Lotto(final Set lotto) { validateLottoSize(lotto); this.lotto = Collections.unmodifiableSet(lotto); } - public static Lotto from(Set values) { + public static Lotto from(final Set values) { return new Lotto(values.stream() .map(LottoNumber::valueOf) .collect(toSet())); @@ -55,18 +55,18 @@ public static Lotto createRandomLotto() { }); } - public int countMatchingNumber(Lotto compareLotto) { + public int countMatchingNumber(final Lotto compareLotto) { return lotto.stream() .filter(lottoNumber -> compareLotto.getLotto().contains(lottoNumber)) .toList() .size(); } - public boolean containsNumber(LottoNumber lottoNumber) { + public boolean containsNumber(final LottoNumber lottoNumber) { return lotto.contains(lottoNumber); } - private void validateLottoSize(Set lotto) { + private void validateLottoSize(final Set lotto) { if (lotto.size() != LOTTO_SIZE) { throw new IllegalArgumentException(String.format("로또 번호는 중복되지 않은 %d개여야 합니다.\n", LOTTO_SIZE)); } diff --git a/src/main/java/org/duckstudy/model/lotto/LottoCount.java b/src/main/java/org/duckstudy/model/lotto/LottoCount.java index c1a6b856..500aa7ad 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoCount.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoCount.java @@ -6,24 +6,24 @@ public class LottoCount { private final int count; - public LottoCount(int count) { + public LottoCount(final int count) { validateLottoCount(count); this.count = count; } - private void validateLottoCount(int count) { + private void validateLottoCount(final int count) { if (count < 0) { throw new IllegalArgumentException("로또 개수는 0개 이상이어야 합니다."); } } - public void validateManualLottoCount(int totalLottoCount) { + public void validateManualLottoCount(final int totalLottoCount) { if (count > totalLottoCount) { throw new IllegalArgumentException("수동으로 구매할 로또 수가 전체 로또 수를 초과합니다.\n"); } } - public LottoCount subtract(LottoCount lottoCount) { + public LottoCount subtract(final LottoCount lottoCount) { return new LottoCount(count - lottoCount.count); } diff --git a/src/main/java/org/duckstudy/model/lotto/LottoNumber.java b/src/main/java/org/duckstudy/model/lotto/LottoNumber.java index a077d3cd..465e61fc 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoNumber.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoNumber.java @@ -18,17 +18,17 @@ public class LottoNumber { private final int value; - private LottoNumber(int number) { + private LottoNumber(final int number) { this.value = number; } - public static LottoNumber valueOf(int number) { + public static LottoNumber valueOf(final int number) { validateNumber(number); return cache.get(number - START_INCLUSIVE_NUMBER); } - private static void validateNumber(int number) { + private static void validateNumber(final int number) { if (number < START_INCLUSIVE_NUMBER || number > END_INCLUSIVE_NUMBER) { throw new IllegalArgumentException( String.format("로또 번호는 %d 이상 %d 이하의 숫자여야 합니다.", START_INCLUSIVE_NUMBER, diff --git a/src/main/java/org/duckstudy/model/lotto/LottoResult.java b/src/main/java/org/duckstudy/model/lotto/LottoResult.java index 6588f1ab..25c5e0a7 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoResult.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoResult.java @@ -13,11 +13,12 @@ public class LottoResult { private final Map result; - public LottoResult(Map result) { + public LottoResult(final Map result) { this.result = Collections.unmodifiableMap(result); } - public static LottoResult createLottoResult(Lotto lotto, Lotto winningLotto, LottoNumber bonusNumber) { + public static LottoResult createLottoResult(final Lotto lotto, final Lotto winningLotto, + final LottoNumber bonusNumber) { int matchingCount = lotto.countMatchingNumber(winningLotto); boolean matchBonus = lotto.containsNumber(bonusNumber); @@ -26,7 +27,7 @@ public static LottoResult createLottoResult(Lotto lotto, Lotto winningLotto, Lot return new LottoResult(Map.of(key, DEFAULT_FREQUENCY)); } - public LottoResult merge(LottoResult other) { + public LottoResult merge(final LottoResult other) { return new LottoResult(Stream.of(this.result, other.result) .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap( @@ -36,7 +37,7 @@ public LottoResult merge(LottoResult other) { ))); } - public int getMatchingCount(int key) { + public int getMatchingCount(final int key) { return result.getOrDefault(key, DEFAULT_VALUE); } diff --git a/src/main/java/org/duckstudy/model/lotto/Lottos.java b/src/main/java/org/duckstudy/model/lotto/Lottos.java index fd69d782..b9e41a82 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lottos.java +++ b/src/main/java/org/duckstudy/model/lotto/Lottos.java @@ -12,23 +12,23 @@ public class Lottos { private final List lottos; - public Lottos(List lottos) { + public Lottos(final List lottos) { this.lottos = Collections.unmodifiableList(lottos); } - public static Lottos generateLottos(int lottoCount) { + public static Lottos generateLottos(final int lottoCount) { return Stream.generate(Lotto::createRandomLotto) .limit(lottoCount) .collect(collectingAndThen(toList(), Lottos::new)); } - public LottoResult accumulateLottoResult(Lotto winningLotto, LottoNumber bonusNumber) { + public LottoResult accumulateLottoResult(final Lotto winningLotto, final LottoNumber bonusNumber) { return lottos.stream() .map(lotto -> LottoResult.createLottoResult(lotto, winningLotto, bonusNumber)) .reduce(new LottoResult(Map.of()), LottoResult::merge); } - public Lottos merge(Lottos other) { + public Lottos merge(final Lottos other) { return new Lottos(Stream.of(this.lottos, other.lottos) .flatMap(List::stream) .collect(toList())); diff --git a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java index 06274171..771d3a73 100644 --- a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java +++ b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java @@ -17,14 +17,15 @@ public enum WinningRank { private final int key; private final BiPredicate isMatchPredicate; - WinningRank(int matchCount, int price, int key, BiPredicate isMatchPredicate) { + WinningRank(final int matchCount, final int price, final int key, + final BiPredicate isMatchPredicate) { this.matchCount = matchCount; this.price = price; this.key = key; this.isMatchPredicate = isMatchPredicate; } - public static int findByMatchCountAndBonus(int matchCount, boolean matchBonus) { + public static int findByMatchCountAndBonus(final int matchCount, final boolean matchBonus) { return Stream.of(values()) .filter(winningLank -> winningLank.isMatch(matchCount, matchBonus)) .findFirst() @@ -32,7 +33,7 @@ public static int findByMatchCountAndBonus(int matchCount, boolean matchBonus) { .orElseThrow(() -> new IllegalArgumentException("적절한 당첨 등수를 찾을 수 없습니다.")); } - private boolean isMatch(int matchCount, boolean matchBonus) { + private boolean isMatch(final int matchCount, final boolean matchBonus) { return isMatchPredicate.test(matchCount, matchBonus); } diff --git a/src/main/java/org/duckstudy/view/OutputView.java b/src/main/java/org/duckstudy/view/OutputView.java index 126c61dc..b2073658 100644 --- a/src/main/java/org/duckstudy/view/OutputView.java +++ b/src/main/java/org/duckstudy/view/OutputView.java @@ -16,17 +16,17 @@ public void printInputPrice() { System.out.println("구입금액을 입력해 주세요."); } - public void printException(Exception e) { + public void printException(final Exception e) { System.out.println(e.getMessage()); } - public void printLottos(int manualLottoCount, int autoLottoCount, Lottos lottos) { + public void printLottos(final int manualLottoCount, final int autoLottoCount, final Lottos lottos) { System.out.printf("\n수동으로 %d개, 자동으로 %d개를 구매했습니다.\n", manualLottoCount, autoLottoCount); lottos.getLottos() .forEach(this::printLotto); } - private void printLotto(Lotto lotto) { + private void printLotto(final Lotto lotto) { System.out.println(lotto.getLotto() .stream() .map(LottoNumber::getValue) @@ -42,20 +42,20 @@ public void printInputBonusNumber() { System.out.println("\n보너스 볼을 입력해 주세요."); } - public void printLottoResult(LottoResult result) { + public void printLottoResult(final LottoResult result) { System.out.println("\n당첨 통계"); System.out.println("---------"); iterateLottoResult(result); } - private void iterateLottoResult(LottoResult result) { + private void iterateLottoResult(final LottoResult result) { for (WinningRank winningRank : WinningRank.values()) { printMatchingResult(result, winningRank); } System.out.println(); } - private void printMatchingResult(LottoResult result, WinningRank winningRank) { + private void printMatchingResult(final LottoResult result, final WinningRank winningRank) { if (winningRank == NONE) { return; } @@ -70,14 +70,14 @@ private void printMatchingResult(LottoResult result, WinningRank winningRank) { System.out.println(matchPriceMessage + matchingCount + "개"); } - private String getMatchPrice(WinningRank winningRank, int cnt, int price) { + private String getMatchPrice(final WinningRank winningRank, final int cnt, final int price) { if (winningRank == SECOND) { return String.format("%d개 일치, 보너스 볼 일치(%d원)- ", cnt, price); } return String.format("%d개 일치 (%d원)- ", cnt, price); } - public void printTotalProfit(double totalProfitRate) { + public void printTotalProfit(final double totalProfitRate) { System.out.printf("총 수익률은 %.2f입니다.\n", totalProfitRate); } From 6a3581da79ee90e181cf668c82b1d3d89c1e5d2f Mon Sep 17 00:00:00 2001 From: Mint Date: Sun, 23 Jun 2024 13:55:18 +0900 Subject: [PATCH 18/27] =?UTF-8?q?rename:=20LottoResult=EB=A5=BC=20LottoSta?= =?UTF-8?q?tistics=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로또 당첨 통계를 더 잘 나타내는 이름으로 변경 --- .../duckstudy/controller/LottoController.java | 12 +++++----- src/main/java/org/duckstudy/model/Price.java | 4 ++-- ...{LottoResult.java => LottoStatistics.java} | 24 +++++++++---------- .../org/duckstudy/model/lotto/Lottos.java | 6 ++--- .../java/org/duckstudy/view/OutputView.java | 8 +++---- .../java/org/duckstudy/model/PriceTest.java | 6 ++--- ...sultTest.java => LottoStatisticsTest.java} | 14 +++++------ .../org/duckstudy/model/lotto/LottosTest.java | 4 ++-- 8 files changed, 39 insertions(+), 39 deletions(-) rename src/main/java/org/duckstudy/model/lotto/{LottoResult.java => LottoStatistics.java} (53%) rename src/test/java/org/duckstudy/model/lotto/{LottoResultTest.java => LottoStatisticsTest.java} (62%) diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index ebbe2812..f2794400 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -6,7 +6,7 @@ import org.duckstudy.model.lotto.Lotto; import org.duckstudy.model.lotto.LottoCount; import org.duckstudy.model.lotto.LottoNumber; -import org.duckstudy.model.lotto.LottoResult; +import org.duckstudy.model.lotto.LottoStatistics; import org.duckstudy.model.lotto.Lottos; import org.duckstudy.view.InputView; import org.duckstudy.view.OutputView; @@ -115,18 +115,18 @@ private void validateBonusNumber(final Lotto winningLotto, final LottoNumber bon private void getWinningResult(final Price price, final Lottos lottos, final Lotto winningLotto, final LottoNumber bonusNumber) { - LottoResult result = createLottoResult(lottos, winningLotto, bonusNumber); + LottoStatistics result = createLottoResult(lottos, winningLotto, bonusNumber); calculateProfitRate(price, result); } - private LottoResult createLottoResult(final Lottos lottos, final Lotto winningLotto, - final LottoNumber bonusNumber) { - LottoResult result = lottos.accumulateLottoResult(winningLotto, bonusNumber); + private LottoStatistics createLottoResult(final Lottos lottos, final Lotto winningLotto, + final LottoNumber bonusNumber) { + LottoStatistics result = lottos.accumulateLottoResult(winningLotto, bonusNumber); outputView.printLottoResult(result); return result; } - private void calculateProfitRate(final Price price, final LottoResult result) { + private void calculateProfitRate(final Price price, final LottoStatistics result) { double profitRate = price.calculateProfitRate(result); outputView.printTotalProfit(profitRate); } diff --git a/src/main/java/org/duckstudy/model/Price.java b/src/main/java/org/duckstudy/model/Price.java index 72929800..0ac93268 100644 --- a/src/main/java/org/duckstudy/model/Price.java +++ b/src/main/java/org/duckstudy/model/Price.java @@ -2,7 +2,7 @@ import java.util.Objects; import org.duckstudy.model.lotto.LottoCount; -import org.duckstudy.model.lotto.LottoResult; +import org.duckstudy.model.lotto.LottoStatistics; import org.duckstudy.model.lotto.constant.WinningRank; public class Price { @@ -55,7 +55,7 @@ public LottoCount calculateLottoCount() { return new LottoCount(value / LOTTO_PRICE); } - public double calculateProfitRate(final LottoResult result) { + public double calculateProfitRate(final LottoStatistics result) { Price profit = Price.zero(); for (WinningRank winningRank : WinningRank.values()) { profit = profit.accumulateProfit(winningRank, result.getMatchingCount(winningRank.getKey())); diff --git a/src/main/java/org/duckstudy/model/lotto/LottoResult.java b/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java similarity index 53% rename from src/main/java/org/duckstudy/model/lotto/LottoResult.java rename to src/main/java/org/duckstudy/model/lotto/LottoStatistics.java index 25c5e0a7..f14f15c6 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoResult.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java @@ -6,29 +6,29 @@ import java.util.stream.Stream; import org.duckstudy.model.lotto.constant.WinningRank; -public class LottoResult { +public class LottoStatistics { public static final int DEFAULT_FREQUENCY = 1; public static final int DEFAULT_VALUE = 0; - private final Map result; + private final Map statistics; - public LottoResult(final Map result) { - this.result = Collections.unmodifiableMap(result); + public LottoStatistics(final Map statistics) { + this.statistics = Collections.unmodifiableMap(statistics); } - public static LottoResult createLottoResult(final Lotto lotto, final Lotto winningLotto, - final LottoNumber bonusNumber) { + public static LottoStatistics createLottoResult(final Lotto lotto, final Lotto winningLotto, + final LottoNumber bonusNumber) { int matchingCount = lotto.countMatchingNumber(winningLotto); boolean matchBonus = lotto.containsNumber(bonusNumber); int key = WinningRank.findByMatchCountAndBonus(matchingCount, matchBonus); - return new LottoResult(Map.of(key, DEFAULT_FREQUENCY)); + return new LottoStatistics(Map.of(key, DEFAULT_FREQUENCY)); } - public LottoResult merge(final LottoResult other) { - return new LottoResult(Stream.of(this.result, other.result) + public LottoStatistics merge(final LottoStatistics other) { + return new LottoStatistics(Stream.of(this.statistics, other.statistics) .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap( Map.Entry::getKey, @@ -38,10 +38,10 @@ public LottoResult merge(final LottoResult other) { } public int getMatchingCount(final int key) { - return result.getOrDefault(key, DEFAULT_VALUE); + return statistics.getOrDefault(key, DEFAULT_VALUE); } - public Map getResult() { - return result; + public Map getStatistics() { + return statistics; } } diff --git a/src/main/java/org/duckstudy/model/lotto/Lottos.java b/src/main/java/org/duckstudy/model/lotto/Lottos.java index b9e41a82..01d07d75 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lottos.java +++ b/src/main/java/org/duckstudy/model/lotto/Lottos.java @@ -22,10 +22,10 @@ public static Lottos generateLottos(final int lottoCount) { .collect(collectingAndThen(toList(), Lottos::new)); } - public LottoResult accumulateLottoResult(final Lotto winningLotto, final LottoNumber bonusNumber) { + public LottoStatistics accumulateLottoResult(final Lotto winningLotto, final LottoNumber bonusNumber) { return lottos.stream() - .map(lotto -> LottoResult.createLottoResult(lotto, winningLotto, bonusNumber)) - .reduce(new LottoResult(Map.of()), LottoResult::merge); + .map(lotto -> LottoStatistics.createLottoResult(lotto, winningLotto, bonusNumber)) + .reduce(new LottoStatistics(Map.of()), LottoStatistics::merge); } public Lottos merge(final Lottos other) { diff --git a/src/main/java/org/duckstudy/view/OutputView.java b/src/main/java/org/duckstudy/view/OutputView.java index b2073658..be84f765 100644 --- a/src/main/java/org/duckstudy/view/OutputView.java +++ b/src/main/java/org/duckstudy/view/OutputView.java @@ -6,7 +6,7 @@ import java.util.stream.Collectors; import org.duckstudy.model.lotto.Lotto; import org.duckstudy.model.lotto.LottoNumber; -import org.duckstudy.model.lotto.LottoResult; +import org.duckstudy.model.lotto.LottoStatistics; import org.duckstudy.model.lotto.Lottos; import org.duckstudy.model.lotto.constant.WinningRank; @@ -42,20 +42,20 @@ public void printInputBonusNumber() { System.out.println("\n보너스 볼을 입력해 주세요."); } - public void printLottoResult(final LottoResult result) { + public void printLottoResult(final LottoStatistics result) { System.out.println("\n당첨 통계"); System.out.println("---------"); iterateLottoResult(result); } - private void iterateLottoResult(final LottoResult result) { + private void iterateLottoResult(final LottoStatistics result) { for (WinningRank winningRank : WinningRank.values()) { printMatchingResult(result, winningRank); } System.out.println(); } - private void printMatchingResult(final LottoResult result, final WinningRank winningRank) { + private void printMatchingResult(final LottoStatistics result, final WinningRank winningRank) { if (winningRank == NONE) { return; } diff --git a/src/test/java/org/duckstudy/model/PriceTest.java b/src/test/java/org/duckstudy/model/PriceTest.java index e7fdf5db..673656e4 100644 --- a/src/test/java/org/duckstudy/model/PriceTest.java +++ b/src/test/java/org/duckstudy/model/PriceTest.java @@ -6,7 +6,7 @@ import java.util.HashMap; import java.util.Map; -import org.duckstudy.model.lotto.LottoResult; +import org.duckstudy.model.lotto.LottoStatistics; import org.duckstudy.model.lotto.constant.WinningRank; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -135,9 +135,9 @@ void calculateProfitRate() { Price purchasePrice = new Price(15000); Map result = new HashMap<>(); result.put(WinningRank.SECOND.getKey(), 1); - LottoResult lottoResult = new LottoResult(result); + LottoStatistics lottoStatistics = new LottoStatistics(result); - assertThat(purchasePrice.calculateProfitRate(lottoResult)).isEqualTo(200000.0); + assertThat(purchasePrice.calculateProfitRate(lottoStatistics)).isEqualTo(200000.0); } } } diff --git a/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java b/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java similarity index 62% rename from src/test/java/org/duckstudy/model/lotto/LottoResultTest.java rename to src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java index 478b9963..c2db62a4 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test; @DisplayName("로또 당첨 결과 테스트") -class LottoResultTest { +class LottoStatisticsTest { @Test @DisplayName("로또 결과를 생성한다") @@ -19,9 +19,9 @@ void createLottoResult() { Lotto winningLotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 6)); LottoNumber bonusNumber = LottoNumber.valueOf(7); - LottoResult lottoResult = LottoResult.createLottoResult(lotto, winningLotto, bonusNumber); + LottoStatistics lottoStatistics = LottoStatistics.createLottoResult(lotto, winningLotto, bonusNumber); - assertThat(lottoResult.getResult()) + assertThat(lottoStatistics.getStatistics()) .containsEntry(WinningRank.SECOND.getKey(), 1); } @@ -29,12 +29,12 @@ void createLottoResult() { @DisplayName("로또 결과를 병합한다") void mergeLottoResult() { - LottoResult lottoResult1 = new LottoResult(Map.of(3, 2)); - LottoResult lottoResult2 = new LottoResult(Map.of(3, 1)); + LottoStatistics lottoStatistics1 = new LottoStatistics(Map.of(3, 2)); + LottoStatistics lottoStatistics2 = new LottoStatistics(Map.of(3, 1)); - LottoResult mergedLottoResult = lottoResult1.merge(lottoResult2); + LottoStatistics mergedLottoStatistics = lottoStatistics1.merge(lottoStatistics2); - assertThat(mergedLottoResult.getResult()) + assertThat(mergedLottoStatistics.getStatistics()) .containsEntry(3, 3); } } diff --git a/src/test/java/org/duckstudy/model/lotto/LottosTest.java b/src/test/java/org/duckstudy/model/lotto/LottosTest.java index 36340a57..691ed107 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottosTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottosTest.java @@ -52,9 +52,9 @@ void calculateWinningResultWhenInputWinningLotto() { Lotto.from(Set.of(20, 21, 22, 23, 24, 25)) )); - LottoResult lottoResult = totalLottos.accumulateLottoResult(winningLotto, bonusNumber); + LottoStatistics lottoStatistics = totalLottos.accumulateLottoResult(winningLotto, bonusNumber); - assertThat(lottoResult.getResult()) + assertThat(lottoStatistics.getStatistics()) .containsExactly( entry(NONE.getKey(), 3), entry(SECOND.getKey(), 1), From 63711c3d59c1499f5e806220c0539f41b108d63f Mon Sep 17 00:00:00 2001 From: Mint Date: Sun, 23 Jun 2024 14:11:33 +0900 Subject: [PATCH 19/27] =?UTF-8?q?refactor:=20=ED=95=84=EC=9A=94=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A0=9C=EA=B1=B0=20?= =?UTF-8?q?=EB=B0=8F=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../duckstudy/controller/LottoController.java | 20 +++++-------------- .../java/org/duckstudy/view/OutputView.java | 2 +- .../model/lotto/LottoStatisticsTest.java | 10 +++++----- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index f2794400..984bbb1f 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -28,7 +28,7 @@ public void run() { Lotto winningLotto = createWinningLotto(); LottoNumber bonusNumber = createBonusNumber(winningLotto); - getWinningResult(price, totalLottos, winningLotto, bonusNumber); + printWinningResult(price, totalLottos, winningLotto, bonusNumber); } private Lottos createTotalLottos(final Price price) { @@ -113,21 +113,11 @@ private void validateBonusNumber(final Lotto winningLotto, final LottoNumber bon } } - private void getWinningResult(final Price price, final Lottos lottos, final Lotto winningLotto, - final LottoNumber bonusNumber) { - LottoStatistics result = createLottoResult(lottos, winningLotto, bonusNumber); - calculateProfitRate(price, result); - } - - private LottoStatistics createLottoResult(final Lottos lottos, final Lotto winningLotto, - final LottoNumber bonusNumber) { - LottoStatistics result = lottos.accumulateLottoResult(winningLotto, bonusNumber); - outputView.printLottoResult(result); - return result; - } + private void printWinningResult(Price price, Lottos totalLottos, Lotto winningLotto, LottoNumber bonusNumber) { + LottoStatistics statistics = totalLottos.accumulateLottoResult(winningLotto, bonusNumber); + outputView.printLottoStatistics(statistics); - private void calculateProfitRate(final Price price, final LottoStatistics result) { - double profitRate = price.calculateProfitRate(result); + double profitRate = price.calculateProfitRate(statistics); outputView.printTotalProfit(profitRate); } } diff --git a/src/main/java/org/duckstudy/view/OutputView.java b/src/main/java/org/duckstudy/view/OutputView.java index be84f765..8d4e9214 100644 --- a/src/main/java/org/duckstudy/view/OutputView.java +++ b/src/main/java/org/duckstudy/view/OutputView.java @@ -42,7 +42,7 @@ public void printInputBonusNumber() { System.out.println("\n보너스 볼을 입력해 주세요."); } - public void printLottoResult(final LottoStatistics result) { + public void printLottoStatistics(final LottoStatistics result) { System.out.println("\n당첨 통계"); System.out.println("---------"); iterateLottoResult(result); diff --git a/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java b/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java index c2db62a4..d99babd8 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java @@ -8,12 +8,12 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -@DisplayName("로또 당첨 결과 테스트") +@DisplayName("로또 당첨 통계 테스트") class LottoStatisticsTest { @Test - @DisplayName("로또 결과를 생성한다") - void createLottoResult() { + @DisplayName("로또 당첨 통계를 생성한다") + void createLottoStatistics() { Lotto lotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 7)); Lotto winningLotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 6)); @@ -26,8 +26,8 @@ void createLottoResult() { } @Test - @DisplayName("로또 결과를 병합한다") - void mergeLottoResult() { + @DisplayName("로또 당첨 통계를 병합한다") + void mergeLottoStatistics() { LottoStatistics lottoStatistics1 = new LottoStatistics(Map.of(3, 2)); LottoStatistics lottoStatistics2 = new LottoStatistics(Map.of(3, 1)); From c4f33e29b9d10f5aed503fbe60ee68fae786c22e Mon Sep 17 00:00:00 2001 From: Mint Date: Sun, 23 Jun 2024 18:51:00 +0900 Subject: [PATCH 20/27] =?UTF-8?q?refactor:=20=EC=9E=85=EB=A0=A5=EC=9D=84?= =?UTF-8?q?=20=EB=B0=9B=EB=8A=94=20=EC=BB=AC=EB=A0=89=EC=85=98=EC=97=90=20?= =?UTF-8?q?=EB=B0=A9=EC=96=B4=EC=A0=81=20=EB=B3=B5=EC=82=AC=20=EC=88=98?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 입력받은 컬렉션을 외부에서 조작할 수 있으므로 방어적 복사를 수행하도록 변경한다. - getter는 컬렉션을 외부에 노출하므로 변경 가능성을 제거하기 위해 불변 컬렉션으로 감싸서 반환한다. --- src/main/java/org/duckstudy/model/lotto/Lotto.java | 5 +++-- src/main/java/org/duckstudy/model/lotto/LottoStatistics.java | 5 +++-- src/main/java/org/duckstudy/model/lotto/Lottos.java | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/duckstudy/model/lotto/Lotto.java b/src/main/java/org/duckstudy/model/lotto/Lotto.java index 6ce4ce60..e1f26c46 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lotto.java +++ b/src/main/java/org/duckstudy/model/lotto/Lotto.java @@ -8,6 +8,7 @@ import static org.duckstudy.model.lotto.LottoNumber.START_INCLUSIVE_NUMBER; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collector; @@ -29,7 +30,7 @@ public class Lotto { public Lotto(final Set lotto) { validateLottoSize(lotto); - this.lotto = Collections.unmodifiableSet(lotto); + this.lotto = new HashSet<>(lotto); } public static Lotto from(final Set values) { @@ -73,6 +74,6 @@ private void validateLottoSize(final Set lotto) { } public Set getLotto() { - return lotto; + return Collections.unmodifiableSet(lotto); } } diff --git a/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java b/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java index f14f15c6..94085781 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java @@ -1,6 +1,7 @@ package org.duckstudy.model.lotto; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -14,7 +15,7 @@ public class LottoStatistics { private final Map statistics; public LottoStatistics(final Map statistics) { - this.statistics = Collections.unmodifiableMap(statistics); + this.statistics = new HashMap<>(statistics); } public static LottoStatistics createLottoResult(final Lotto lotto, final Lotto winningLotto, @@ -42,6 +43,6 @@ public int getMatchingCount(final int key) { } public Map getStatistics() { - return statistics; + return Collections.unmodifiableMap(statistics); } } diff --git a/src/main/java/org/duckstudy/model/lotto/Lottos.java b/src/main/java/org/duckstudy/model/lotto/Lottos.java index 01d07d75..3212a8c2 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lottos.java +++ b/src/main/java/org/duckstudy/model/lotto/Lottos.java @@ -3,6 +3,7 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; @@ -13,7 +14,7 @@ public class Lottos { private final List lottos; public Lottos(final List lottos) { - this.lottos = Collections.unmodifiableList(lottos); + this.lottos = new ArrayList<>(lottos); } public static Lottos generateLottos(final int lottoCount) { @@ -35,6 +36,6 @@ public Lottos merge(final Lottos other) { } public List getLottos() { - return lottos; + return Collections.unmodifiableList(lottos); } } From 20eb06cd0fc37bb086571c7d198a29575878d846 Mon Sep 17 00:00:00 2001 From: Mint Date: Sun, 23 Jun 2024 19:01:15 +0900 Subject: [PATCH 21/27] =?UTF-8?q?docs:=20README=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 32b16cd0..65058266 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,12 @@ - `LottoNumber` : 로또 번호 VO - `Lotto` : 로또 객체 - `Lottos` : 여러 개의 Lotto 객체를 관리하는 일급 컬렉션 -- `LottoResult` : 로또 결과 VO +- `LottoStatistics` : 로또 당첨 통계 객체 +- `LottoCount` : 로또 구매 개수 VO #### constant -- `LottoBoundary` : 로또 경계값 상수 -- `LottoMatch` : 로또 당첨 결과 상수 +- `WinningRank` : 당첨 순위 Enum ## view @@ -132,7 +132,8 @@ - 당첨 통계에 2등을 추가한다. - 2등 당첨 조건은 당첨 번호 5개 일치 + 보너스 볼 일치다. -실행 결과 + +- 실행 결과 위 요구사항에 따라 14000원 어치 로또를 구매하였을 경우 프로그램을 실행한 결과는 다음과 같다. @@ -174,3 +175,61 @@ ### 새로운 프로그래밍 요구사항 Java Enum을 적용한다. + +### 4단계 - 로또 수동 구매 + +- 현재 로또 생성기는 자동 생성 기능만 제공한다. 사용자가 수동으로 추첨 번호를 입력할 수 있도록 해야 한다. +- 입력한 금액, 자동 생성 숫자, 수동 생성 번호를 입력하도록 해야 한다. + + +실행 결과 + +- 위 요구사항에 따라 14000원 어치 중 수동 3개, 자동 11개를 구매한 경우 프로그램을 실행한 결과는 다음과 같다. + +```java +구입금액을 입력해 주세요. +14000 + +수동으로 구매할 로또 수를 입력해 주세요. +3 + +수동으로 구매할 번호를 입력해 주세요. +8, 21, 23, 41, 42, 43 +3, 5, 11, 16, 32, 38 +7, 11, 16, 35, 36, 44 + +수동으로 3장, 자동으로 11개를 구매했습니다. +[8, 21, 23, 41, 42, 43] +[3, 5, 11, 16, 32, 38] +[7, 11, 16, 35, 36, 44] +[1, 8, 11, 31, 41, 42] +[13, 14, 16, 38, 42, 45] +[7, 11, 30, 40, 42, 43] +[2, 13, 22, 32, 38, 45] +[23, 25, 33, 36, 39, 41] +[1, 3, 5, 14, 22, 45] +[5, 9, 38, 41, 43, 44] +[2, 8, 9, 18, 19, 21] +[13, 14, 18, 21, 23, 35] +[17, 21, 29, 37, 42, 45] +[3, 8, 27, 30, 35, 44] + +지난 주 당첨 번호를 입력해 주세요. +1, 2, 3, 4, 5, 6 + +보너스 볼을 입력해 주세요. +7 + +당첨 통계 +--------- +3개 일치 (5000원)- 1개 +4개 일치 (50000원)- 0개 +5개 일치 (1500000원)- 0개 +5개 일치, 보너스 볼 일치(30000000원) - 0개 +6개 일치 (2000000000원)- 0개 +총 수익률은 0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임) +``` + +### 5단계 - 리팩터링 + +- 기존 프로그래밍 요구사항을 다시 한번 확인하고, 학습 테스트를 통해 학습한 내용을 반영한다. From 7b9c6fa1702b5ea0336aa7145a6daa3248f7a77c Mon Sep 17 00:00:00 2001 From: Mint Date: Mon, 1 Jul 2024 17:27:55 +0900 Subject: [PATCH 22/27] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=ED=83=80=EC=9E=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 호출하는 함수에서 모두 getter를 이용해 값으로 접근하기 때문에 메서드 파라미터 타입을 LottoCount로 변경하는 것이 좋다. --- .../java/org/duckstudy/controller/LottoController.java | 2 +- src/main/java/org/duckstudy/model/lotto/LottoCount.java | 4 ++-- .../org/duckstudy/model/lotto/constant/WinningRank.java | 4 ++-- .../java/org/duckstudy/model/lotto/LottoCountTest.java | 8 +++++--- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index 984bbb1f..058da8e0 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -68,7 +68,7 @@ private LottoCount createManualLottoCount(final LottoCount lottoCount) { outputView.printInputManualLottoCount(); try { LottoCount manualLottoCount = new LottoCount(inputView.inputManualLottoCount()); - manualLottoCount.validateManualLottoCount(lottoCount.getCount()); + manualLottoCount.validateManualLottoCount(lottoCount); return manualLottoCount; } catch (IllegalArgumentException e) { outputView.printException(e); diff --git a/src/main/java/org/duckstudy/model/lotto/LottoCount.java b/src/main/java/org/duckstudy/model/lotto/LottoCount.java index 500aa7ad..8ebc449b 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoCount.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoCount.java @@ -17,8 +17,8 @@ private void validateLottoCount(final int count) { } } - public void validateManualLottoCount(final int totalLottoCount) { - if (count > totalLottoCount) { + public void validateManualLottoCount(final LottoCount totalLottoCount) { + if (count > totalLottoCount.getCount()) { throw new IllegalArgumentException("수동으로 구매할 로또 수가 전체 로또 수를 초과합니다.\n"); } } diff --git a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java index 771d3a73..773aeeca 100644 --- a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java +++ b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java @@ -1,7 +1,7 @@ package org.duckstudy.model.lotto.constant; +import java.util.Arrays; import java.util.function.BiPredicate; -import java.util.stream.Stream; public enum WinningRank { @@ -26,7 +26,7 @@ public enum WinningRank { } public static int findByMatchCountAndBonus(final int matchCount, final boolean matchBonus) { - return Stream.of(values()) + return Arrays.stream(values()) .filter(winningLank -> winningLank.isMatch(matchCount, matchBonus)) .findFirst() .map(WinningRank::getKey) diff --git a/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java b/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java index 9576f5b1..9db77fc0 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java @@ -42,8 +42,9 @@ class ManualLottoCountValidationTest { public void validateManualLottoCountSuccessWhenManualLottoCountIsNotGreaterThanTotalLottoCount() { LottoCount manualLottoCount = new LottoCount(10); + LottoCount totalLottoCount = new LottoCount(15); - assertThatCode(() -> manualLottoCount.validateManualLottoCount(10)) + assertThatCode(() -> manualLottoCount.validateManualLottoCount(totalLottoCount)) .doesNotThrowAnyException(); } @@ -51,9 +52,10 @@ public void validateManualLottoCountSuccessWhenManualLottoCountIsNotGreaterThanT @DisplayName("수동으로 구매할 로또 수가 전체 로또 수를 초과하면 예외가 발생한다") public void validateManualLottoCountFailWhenManualLottoCountIsGreaterThanTotalLottoCount() { - LottoCount lottoCount = new LottoCount(10); + LottoCount manualLottoCount = new LottoCount(20); + LottoCount totalLottoCount = new LottoCount(15); - assertThatThrownBy(() -> lottoCount.validateManualLottoCount(5)) + assertThatThrownBy(() -> manualLottoCount.validateManualLottoCount(totalLottoCount)) .isExactlyInstanceOf(IllegalArgumentException.class) .hasMessage("수동으로 구매할 로또 수가 전체 로또 수를 초과합니다.\n"); } From e715e319f8ca9be4b98bc88900373ba204f500f8 Mon Sep 17 00:00:00 2001 From: Mint Date: Mon, 1 Jul 2024 19:27:47 +0900 Subject: [PATCH 23/27] =?UTF-8?q?refactor:=20LottoStatistics=EC=97=90?= =?UTF-8?q?=EC=84=9C=20WinningRank=20=EC=9E=90=EC=B2=B4=EB=A5=BC=20key?= =?UTF-8?q?=EB=A1=9C=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - WinningRank를 key로 사용하여 LottoStatistics에 저장하면, WinningRank에서 key를 별도로 관리할 필요가 없어지고 의미상 명확하다. --- src/main/java/org/duckstudy/model/Price.java | 6 ++-- .../model/lotto/LottoStatistics.java | 14 +++++----- .../model/lotto/constant/WinningRank.java | 28 ++++++++----------- .../java/org/duckstudy/view/OutputView.java | 9 +++--- .../java/org/duckstudy/model/PriceTest.java | 4 +-- .../model/lotto/LottoStatisticsTest.java | 8 +++--- .../org/duckstudy/model/lotto/LottosTest.java | 6 ++-- .../model/lotto/constant/WinningRankTest.java | 14 +++++----- 8 files changed, 42 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/duckstudy/model/Price.java b/src/main/java/org/duckstudy/model/Price.java index 0ac93268..6bb5f0b6 100644 --- a/src/main/java/org/duckstudy/model/Price.java +++ b/src/main/java/org/duckstudy/model/Price.java @@ -58,13 +58,13 @@ public LottoCount calculateLottoCount() { public double calculateProfitRate(final LottoStatistics result) { Price profit = Price.zero(); for (WinningRank winningRank : WinningRank.values()) { - profit = profit.accumulateProfit(winningRank, result.getMatchingCount(winningRank.getKey())); + profit = profit.accumulateProfit(winningRank.getPrice(), result.getMatchingCount(winningRank)); } return profit.divideByPrice(this) * PERCENT_BASE; } - private Price accumulateProfit(final WinningRank winningRank, final int count) { - return this.addPrice(winningRank.getPrice() * count); + private Price accumulateProfit(final int price, final int count) { + return this.addPrice(price * count); } public int getValue() { diff --git a/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java b/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java index 94085781..60907ea8 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java @@ -12,9 +12,9 @@ public class LottoStatistics { public static final int DEFAULT_FREQUENCY = 1; public static final int DEFAULT_VALUE = 0; - private final Map statistics; + private final Map statistics; - public LottoStatistics(final Map statistics) { + public LottoStatistics(final Map statistics) { this.statistics = new HashMap<>(statistics); } @@ -23,9 +23,9 @@ public static LottoStatistics createLottoResult(final Lotto lotto, final Lotto w int matchingCount = lotto.countMatchingNumber(winningLotto); boolean matchBonus = lotto.containsNumber(bonusNumber); - int key = WinningRank.findByMatchCountAndBonus(matchingCount, matchBonus); + WinningRank winningRank = WinningRank.findByMatchCountAndBonus(matchingCount, matchBonus); - return new LottoStatistics(Map.of(key, DEFAULT_FREQUENCY)); + return new LottoStatistics(Map.of(winningRank, DEFAULT_FREQUENCY)); } public LottoStatistics merge(final LottoStatistics other) { @@ -38,11 +38,11 @@ public LottoStatistics merge(final LottoStatistics other) { ))); } - public int getMatchingCount(final int key) { - return statistics.getOrDefault(key, DEFAULT_VALUE); + public int getMatchingCount(final WinningRank winningRank) { + return statistics.getOrDefault(winningRank, DEFAULT_VALUE); } - public Map getStatistics() { + public Map getStatistics() { return Collections.unmodifiableMap(statistics); } } diff --git a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java index 773aeeca..5bcd16ca 100644 --- a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java +++ b/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java @@ -1,35 +1,33 @@ package org.duckstudy.model.lotto.constant; import java.util.Arrays; +import java.util.List; import java.util.function.BiPredicate; public enum WinningRank { - NONE(0, 0, 0, (matchCount, matchBonus) -> matchCount < 3), - FIFTH(3, 5_000, 3, (matchCount, matchBonus) -> matchCount == 3), - FOURTH(4, 50_000, 4, (matchCount, matchBonus) -> matchCount == 4), - THIRD(5, 1_500_000, 5, (matchCount, matchBonus) -> matchCount == 5 && !matchBonus), - SECOND(5, 30_000_000, -5, (matchCount, matchBonus) -> matchCount == 5 && matchBonus), - FIRST(6, 2_000_000_000, 6, (matchCount, matchBonus) -> matchCount == 6); + NONE(List.of(0, 1, 2), 0, (matchCount, matchBonus) -> matchCount < 3), + FIFTH(List.of(3), 5_000, (matchCount, matchBonus) -> matchCount == 3), + FOURTH(List.of(4), 50_000, (matchCount, matchBonus) -> matchCount == 4), + THIRD(List.of(5), 1_500_000, (matchCount, matchBonus) -> matchCount == 5 && !matchBonus), + SECOND(List.of(5), 30_000_000, (matchCount, matchBonus) -> matchCount == 5 && matchBonus), + FIRST(List.of(6), 2_000_000_000, (matchCount, matchBonus) -> matchCount == 6); - private final int matchCount; + private final List matchCount; private final int price; - private final int key; private final BiPredicate isMatchPredicate; - WinningRank(final int matchCount, final int price, final int key, + WinningRank(final List matchCount, final int price, final BiPredicate isMatchPredicate) { this.matchCount = matchCount; this.price = price; - this.key = key; this.isMatchPredicate = isMatchPredicate; } - public static int findByMatchCountAndBonus(final int matchCount, final boolean matchBonus) { + public static WinningRank findByMatchCountAndBonus(final int matchCount, final boolean matchBonus) { return Arrays.stream(values()) .filter(winningLank -> winningLank.isMatch(matchCount, matchBonus)) .findFirst() - .map(WinningRank::getKey) .orElseThrow(() -> new IllegalArgumentException("적절한 당첨 등수를 찾을 수 없습니다.")); } @@ -37,15 +35,11 @@ private boolean isMatch(final int matchCount, final boolean matchBonus) { return isMatchPredicate.test(matchCount, matchBonus); } - public int getMatchCount() { + public List getMatchCount() { return matchCount; } public int getPrice() { return price; } - - public int getKey() { - return key; - } } diff --git a/src/main/java/org/duckstudy/view/OutputView.java b/src/main/java/org/duckstudy/view/OutputView.java index 8d4e9214..2bd0e806 100644 --- a/src/main/java/org/duckstudy/view/OutputView.java +++ b/src/main/java/org/duckstudy/view/OutputView.java @@ -3,6 +3,7 @@ import static org.duckstudy.model.lotto.constant.WinningRank.NONE; import static org.duckstudy.model.lotto.constant.WinningRank.SECOND; +import java.util.List; import java.util.stream.Collectors; import org.duckstudy.model.lotto.Lotto; import org.duckstudy.model.lotto.LottoNumber; @@ -60,12 +61,12 @@ private void printMatchingResult(final LottoStatistics result, final WinningRank return; } - int cnt = winningRank.getMatchCount(); - int key = winningRank.getKey(); + List matchCounts = winningRank.getMatchCount(); +// int key = winningRank.getKey(); int price = winningRank.getPrice(); - String matchPriceMessage = getMatchPrice(winningRank, cnt, price); - int matchingCount = result.getMatchingCount(key); + String matchPriceMessage = getMatchPrice(winningRank, matchCounts.get(0), price); + int matchingCount = result.getMatchingCount(winningRank); System.out.println(matchPriceMessage + matchingCount + "개"); } diff --git a/src/test/java/org/duckstudy/model/PriceTest.java b/src/test/java/org/duckstudy/model/PriceTest.java index 673656e4..295f0d44 100644 --- a/src/test/java/org/duckstudy/model/PriceTest.java +++ b/src/test/java/org/duckstudy/model/PriceTest.java @@ -133,8 +133,8 @@ void calculateLottoCountWhenInputPrice() { @DisplayName("로또 수익률을 계산한다") void calculateProfitRate() { Price purchasePrice = new Price(15000); - Map result = new HashMap<>(); - result.put(WinningRank.SECOND.getKey(), 1); + Map result = new HashMap<>(); + result.put(WinningRank.SECOND, 1); LottoStatistics lottoStatistics = new LottoStatistics(result); assertThat(purchasePrice.calculateProfitRate(lottoStatistics)).isEqualTo(200000.0); diff --git a/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java b/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java index d99babd8..7e139161 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java @@ -22,19 +22,19 @@ void createLottoStatistics() { LottoStatistics lottoStatistics = LottoStatistics.createLottoResult(lotto, winningLotto, bonusNumber); assertThat(lottoStatistics.getStatistics()) - .containsEntry(WinningRank.SECOND.getKey(), 1); + .containsEntry(WinningRank.SECOND, 1); } @Test @DisplayName("로또 당첨 통계를 병합한다") void mergeLottoStatistics() { - LottoStatistics lottoStatistics1 = new LottoStatistics(Map.of(3, 2)); - LottoStatistics lottoStatistics2 = new LottoStatistics(Map.of(3, 1)); + LottoStatistics lottoStatistics1 = new LottoStatistics(Map.of(WinningRank.FIFTH, 2)); + LottoStatistics lottoStatistics2 = new LottoStatistics(Map.of(WinningRank.FIFTH, 1)); LottoStatistics mergedLottoStatistics = lottoStatistics1.merge(lottoStatistics2); assertThat(mergedLottoStatistics.getStatistics()) - .containsEntry(3, 3); + .containsEntry(WinningRank.FIFTH, 3); } } diff --git a/src/test/java/org/duckstudy/model/lotto/LottosTest.java b/src/test/java/org/duckstudy/model/lotto/LottosTest.java index 691ed107..cac0a8af 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottosTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottosTest.java @@ -56,9 +56,9 @@ void calculateWinningResultWhenInputWinningLotto() { assertThat(lottoStatistics.getStatistics()) .containsExactly( - entry(NONE.getKey(), 3), - entry(SECOND.getKey(), 1), - entry(FIRST.getKey(), 1) + entry(NONE, 3), + entry(SECOND, 1), + entry(FIRST, 1) ); } } diff --git a/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java b/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java index dae79097..13ccca00 100644 --- a/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java +++ b/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java @@ -15,19 +15,19 @@ class WinningRankTest { @ParameterizedTest @MethodSource("generateMatchCountAndMatchBonus") void findWinningRankSuccess(int matchCount, boolean matchBonus, - int expected) { + WinningRank expected) { assertThat(WinningRank.findByMatchCountAndBonus(matchCount, matchBonus)) .isEqualTo(expected); } static Stream generateMatchCountAndMatchBonus() { return Stream.of( - Arguments.of(0, false, WinningRank.NONE.getKey()), - Arguments.of(3, false, WinningRank.FIFTH.getKey()), - Arguments.of(4, false, WinningRank.FOURTH.getKey()), - Arguments.of(5, false, WinningRank.THIRD.getKey()), - Arguments.of(5, true, WinningRank.SECOND.getKey()), - Arguments.of(6, false, WinningRank.FIRST.getKey()) + Arguments.of(0, false, WinningRank.NONE), + Arguments.of(3, false, WinningRank.FIFTH), + Arguments.of(4, false, WinningRank.FOURTH), + Arguments.of(5, false, WinningRank.THIRD), + Arguments.of(5, true, WinningRank.SECOND), + Arguments.of(6, false, WinningRank.FIRST) ); } } From 4e486aa2567f26b86422bba2712eb8f24f53de15 Mon Sep 17 00:00:00 2001 From: Mint Date: Mon, 1 Jul 2024 19:44:40 +0900 Subject: [PATCH 24/27] rename: LottoStatistics.java -> LottoResult.java --- README.md | 240 +++++++++--------- .../duckstudy/controller/LottoController.java | 8 +- src/main/java/org/duckstudy/model/Price.java | 4 +- ...{LottoStatistics.java => LottoResult.java} | 24 +- .../org/duckstudy/model/lotto/Lottos.java | 6 +- .../java/org/duckstudy/view/OutputView.java | 9 +- .../java/org/duckstudy/model/PriceTest.java | 6 +- .../model/lotto/LottoResultTest.java | 40 +++ .../model/lotto/LottoStatisticsTest.java | 40 --- .../org/duckstudy/model/lotto/LottosTest.java | 4 +- 10 files changed, 190 insertions(+), 191 deletions(-) rename src/main/java/org/duckstudy/model/lotto/{LottoStatistics.java => LottoResult.java} (53%) create mode 100644 src/test/java/org/duckstudy/model/lotto/LottoResultTest.java delete mode 100644 src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java diff --git a/README.md b/README.md index 65058266..affcbe98 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ - `LottoNumber` : 로또 번호 VO - `Lotto` : 로또 객체 - `Lottos` : 여러 개의 Lotto 객체를 관리하는 일급 컬렉션 -- `LottoStatistics` : 로또 당첨 통계 객체 +- `LottoResult` : 로또 당첨 결과 객체 - `LottoCount` : 로또 구매 개수 VO #### constant @@ -44,23 +44,23 @@ ```java 구입금액을 입력해 주세요. -14000 - -14개를 구매했습니다. -[8,21,23,41,42,43] -[3,5,11,16,32,38] -[7,11,16,35,36,44] -[1,8,11,31,41,42] -[13,14,16,38,42,45] -[7,11,30,40,42,43] -[2,13,22,32,38,45] -[23,25,33,36,39,41] -[1,3,5,14,22,45] -[5,9,38,41,43,44] -[2,8,9,18,19,21] -[13,14,18,21,23,35] -[17,21,29,37,42,45] -[3,8,27,30,35,44] + 14000 + + 14개를 구매했습니다. + [8,21,23,41,42,43] + [3,5,11,16,32,38] + [7,11,16,35,36,44] + [1,8,11,31,41,42] + [13,14,16,38,42,45] + [7,11,30,40,42,43] + [2,13,22,32,38,45] + [23,25,33,36,39,41] + [1,3,5,14,22,45] + [5,9,38,41,43,44] + [2,8,9,18,19,21] + [13,14,18,21,23,35] + [17,21,29,37,42,45] + [3,8,27,30,35,44] ``` ### 새로운 프로그래밍 요구사항 @@ -90,34 +90,34 @@ ```java 구입금액을 입력해 주세요. -14000 - -14개를 구매했습니다. -[8,21,23,41,42,43] -[3,5,11,16,32,38] -[7,11,16,35,36,44] -[1,8,11,31,41,42] -[13,14,16,38,42,45] -[7,11,30,40,42,43] -[2,13,22,32,38,45] -[23,25,33,36,39,41] -[1,3,5,14,22,45] -[5,9,38,41,43,44] -[2,8,9,18,19,21] -[13,14,18,21,23,35] -[17,21,29,37,42,45] -[3,8,27,30,35,44] - -지난 주 당첨 번호를 입력해 주세요. -1,2,3,4,5,6 - -당첨 통계 ---------- -3개 일치(5000원)-1개 -4개 일치(50000원)-0개 -5개 일치(1500000원)-0개 -6개 일치(2000000000원)-0개 -총 수익률은0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임) + 14000 + + 14개를 구매했습니다. + [8,21,23,41,42,43] + [3,5,11,16,32,38] + [7,11,16,35,36,44] + [1,8,11,31,41,42] + [13,14,16,38,42,45] + [7,11,30,40,42,43] + [2,13,22,32,38,45] + [23,25,33,36,39,41] + [1,3,5,14,22,45] + [5,9,38,41,43,44] + [2,8,9,18,19,21] + [13,14,18,21,23,35] + [17,21,29,37,42,45] + [3,8,27,30,35,44] + + 지난 주 당첨 번호를 입력해 주세요. + 1,2,3,4,5,6 + + 당첨 통계 + --------- + 3개 일치(5000원)-1개 + 4개 일치(50000원)-0개 + 5개 일치(1500000원)-0개 + 6개 일치(2000000000원)-0개 + 총 수익률은0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임) ``` @@ -130,7 +130,7 @@ - 2등을 위한 보너스볼을 추첨한다. - 당첨 통계에 2등을 추가한다. - - 2등 당첨 조건은 당첨 번호 5개 일치 + 보너스 볼 일치다. + - 2등 당첨 조건은 당첨 번호 5개 일치 + 보너스 볼 일치다. - 실행 결과 @@ -139,41 +139,42 @@ ```java 구입금액을 입력해 주세요. -14000 - -14개를 구매했습니다. -[8,21,23,41,42,43] -[3,5,11,16,32,38] -[7,11,16,35,36,44] -[1,8,11,31,41,42] -[13,14,16,38,42,45] -[7,11,30,40,42,43] -[2,13,22,32,38,45] -[23,25,33,36,39,41] -[1,3,5,14,22,45] -[5,9,38,41,43,44] -[2,8,9,18,19,21] -[13,14,18,21,23,35] -[17,21,29,37,42,45] -[3,8,27,30,35,44] - -지난 주 당첨 번호를 입력해 주세요. -1,2,3,4,5,6 - -보너스 볼을 입력해 주세요. -7 - -당첨 통계 ---------- -3개 일치(5000원)-1개 -4개 일치(50000원)-0개 -5개 일치(1500000원)-0개 -5개 일치,보너스 볼 일치(30000000원)-0개 -6개 일치(2000000000원)-0개 -총 수익률은0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임) + 14000 + + 14개를 구매했습니다. + [8,21,23,41,42,43] + [3,5,11,16,32,38] + [7,11,16,35,36,44] + [1,8,11,31,41,42] + [13,14,16,38,42,45] + [7,11,30,40,42,43] + [2,13,22,32,38,45] + [23,25,33,36,39,41] + [1,3,5,14,22,45] + [5,9,38,41,43,44] + [2,8,9,18,19,21] + [13,14,18,21,23,35] + [17,21,29,37,42,45] + [3,8,27,30,35,44] + + 지난 주 당첨 번호를 입력해 주세요. + 1,2,3,4,5,6 + + 보너스 볼을 입력해 주세요. + 7 + + 당첨 통계 + --------- + 3개 일치(5000원)-1개 + 4개 일치(50000원)-0개 + 5개 일치(1500000원)-0개 + 5개 일치,보너스 볼 일치(30000000원)-0개 + 6개 일치(2000000000원)-0개 + 총 수익률은0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임) ``` ### 새로운 프로그래밍 요구사항 + Java Enum을 적용한다. ### 4단계 - 로또 수동 구매 @@ -181,53 +182,52 @@ Java Enum을 적용한다. - 현재 로또 생성기는 자동 생성 기능만 제공한다. 사용자가 수동으로 추첨 번호를 입력할 수 있도록 해야 한다. - 입력한 금액, 자동 생성 숫자, 수동 생성 번호를 입력하도록 해야 한다. - 실행 결과 - 위 요구사항에 따라 14000원 어치 중 수동 3개, 자동 11개를 구매한 경우 프로그램을 실행한 결과는 다음과 같다. ```java 구입금액을 입력해 주세요. -14000 - -수동으로 구매할 로또 수를 입력해 주세요. -3 - -수동으로 구매할 번호를 입력해 주세요. -8, 21, 23, 41, 42, 43 -3, 5, 11, 16, 32, 38 -7, 11, 16, 35, 36, 44 - -수동으로 3장, 자동으로 11개를 구매했습니다. -[8, 21, 23, 41, 42, 43] -[3, 5, 11, 16, 32, 38] -[7, 11, 16, 35, 36, 44] -[1, 8, 11, 31, 41, 42] -[13, 14, 16, 38, 42, 45] -[7, 11, 30, 40, 42, 43] -[2, 13, 22, 32, 38, 45] -[23, 25, 33, 36, 39, 41] -[1, 3, 5, 14, 22, 45] -[5, 9, 38, 41, 43, 44] -[2, 8, 9, 18, 19, 21] -[13, 14, 18, 21, 23, 35] -[17, 21, 29, 37, 42, 45] -[3, 8, 27, 30, 35, 44] - -지난 주 당첨 번호를 입력해 주세요. -1, 2, 3, 4, 5, 6 - -보너스 볼을 입력해 주세요. -7 - -당첨 통계 ---------- -3개 일치 (5000원)- 1개 -4개 일치 (50000원)- 0개 -5개 일치 (1500000원)- 0개 -5개 일치, 보너스 볼 일치(30000000원) - 0개 -6개 일치 (2000000000원)- 0개 -총 수익률은 0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임) + 14000 + + 수동으로 구매할 로또 수를 입력해 주세요. + 3 + + 수동으로 구매할 번호를 입력해 주세요. + 8,21,23,41,42,43 + 3,5,11,16,32,38 + 7,11,16,35,36,44 + + 수동으로 3장,자동으로 11개를 구매했습니다. + [8,21,23,41,42,43] + [3,5,11,16,32,38] + [7,11,16,35,36,44] + [1,8,11,31,41,42] + [13,14,16,38,42,45] + [7,11,30,40,42,43] + [2,13,22,32,38,45] + [23,25,33,36,39,41] + [1,3,5,14,22,45] + [5,9,38,41,43,44] + [2,8,9,18,19,21] + [13,14,18,21,23,35] + [17,21,29,37,42,45] + [3,8,27,30,35,44] + + 지난 주 당첨 번호를 입력해 주세요. + 1,2,3,4,5,6 + + 보너스 볼을 입력해 주세요. + 7 + + 당첨 통계 + --------- + 3개 일치(5000원)-1개 + 4개 일치(50000원)-0개 + 5개 일치(1500000원)-0개 + 5개 일치,보너스 볼 일치(30000000원)-0개 + 6개 일치(2000000000원)-0개 + 총 수익률은0.35입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임) ``` ### 5단계 - 리팩터링 diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/controller/LottoController.java index 058da8e0..3dbc0c05 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/controller/LottoController.java @@ -6,7 +6,7 @@ import org.duckstudy.model.lotto.Lotto; import org.duckstudy.model.lotto.LottoCount; import org.duckstudy.model.lotto.LottoNumber; -import org.duckstudy.model.lotto.LottoStatistics; +import org.duckstudy.model.lotto.LottoResult; import org.duckstudy.model.lotto.Lottos; import org.duckstudy.view.InputView; import org.duckstudy.view.OutputView; @@ -114,10 +114,10 @@ private void validateBonusNumber(final Lotto winningLotto, final LottoNumber bon } private void printWinningResult(Price price, Lottos totalLottos, Lotto winningLotto, LottoNumber bonusNumber) { - LottoStatistics statistics = totalLottos.accumulateLottoResult(winningLotto, bonusNumber); - outputView.printLottoStatistics(statistics); + LottoResult lottoResult = totalLottos.accumulateLottoResult(winningLotto, bonusNumber); + outputView.printLottoResult(lottoResult); - double profitRate = price.calculateProfitRate(statistics); + double profitRate = price.calculateProfitRate(lottoResult); outputView.printTotalProfit(profitRate); } } diff --git a/src/main/java/org/duckstudy/model/Price.java b/src/main/java/org/duckstudy/model/Price.java index 6bb5f0b6..1dc67ab5 100644 --- a/src/main/java/org/duckstudy/model/Price.java +++ b/src/main/java/org/duckstudy/model/Price.java @@ -2,7 +2,7 @@ import java.util.Objects; import org.duckstudy.model.lotto.LottoCount; -import org.duckstudy.model.lotto.LottoStatistics; +import org.duckstudy.model.lotto.LottoResult; import org.duckstudy.model.lotto.constant.WinningRank; public class Price { @@ -55,7 +55,7 @@ public LottoCount calculateLottoCount() { return new LottoCount(value / LOTTO_PRICE); } - public double calculateProfitRate(final LottoStatistics result) { + public double calculateProfitRate(final LottoResult result) { Price profit = Price.zero(); for (WinningRank winningRank : WinningRank.values()) { profit = profit.accumulateProfit(winningRank.getPrice(), result.getMatchingCount(winningRank)); diff --git a/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java b/src/main/java/org/duckstudy/model/lotto/LottoResult.java similarity index 53% rename from src/main/java/org/duckstudy/model/lotto/LottoStatistics.java rename to src/main/java/org/duckstudy/model/lotto/LottoResult.java index 60907ea8..1fb6cfb1 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoStatistics.java +++ b/src/main/java/org/duckstudy/model/lotto/LottoResult.java @@ -7,29 +7,29 @@ import java.util.stream.Stream; import org.duckstudy.model.lotto.constant.WinningRank; -public class LottoStatistics { +public class LottoResult { public static final int DEFAULT_FREQUENCY = 1; public static final int DEFAULT_VALUE = 0; - private final Map statistics; + private final Map result; - public LottoStatistics(final Map statistics) { - this.statistics = new HashMap<>(statistics); + public LottoResult(final Map result) { + this.result = new HashMap<>(result); } - public static LottoStatistics createLottoResult(final Lotto lotto, final Lotto winningLotto, - final LottoNumber bonusNumber) { + public static LottoResult createLottoResult(final Lotto lotto, final Lotto winningLotto, + final LottoNumber bonusNumber) { int matchingCount = lotto.countMatchingNumber(winningLotto); boolean matchBonus = lotto.containsNumber(bonusNumber); WinningRank winningRank = WinningRank.findByMatchCountAndBonus(matchingCount, matchBonus); - return new LottoStatistics(Map.of(winningRank, DEFAULT_FREQUENCY)); + return new LottoResult(Map.of(winningRank, DEFAULT_FREQUENCY)); } - public LottoStatistics merge(final LottoStatistics other) { - return new LottoStatistics(Stream.of(this.statistics, other.statistics) + public LottoResult merge(final LottoResult other) { + return new LottoResult(Stream.of(this.result, other.result) .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap( Map.Entry::getKey, @@ -39,10 +39,10 @@ public LottoStatistics merge(final LottoStatistics other) { } public int getMatchingCount(final WinningRank winningRank) { - return statistics.getOrDefault(winningRank, DEFAULT_VALUE); + return result.getOrDefault(winningRank, DEFAULT_VALUE); } - public Map getStatistics() { - return Collections.unmodifiableMap(statistics); + public Map getResult() { + return Collections.unmodifiableMap(result); } } diff --git a/src/main/java/org/duckstudy/model/lotto/Lottos.java b/src/main/java/org/duckstudy/model/lotto/Lottos.java index 3212a8c2..da1f29f8 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lottos.java +++ b/src/main/java/org/duckstudy/model/lotto/Lottos.java @@ -23,10 +23,10 @@ public static Lottos generateLottos(final int lottoCount) { .collect(collectingAndThen(toList(), Lottos::new)); } - public LottoStatistics accumulateLottoResult(final Lotto winningLotto, final LottoNumber bonusNumber) { + public LottoResult accumulateLottoResult(final Lotto winningLotto, final LottoNumber bonusNumber) { return lottos.stream() - .map(lotto -> LottoStatistics.createLottoResult(lotto, winningLotto, bonusNumber)) - .reduce(new LottoStatistics(Map.of()), LottoStatistics::merge); + .map(lotto -> LottoResult.createLottoResult(lotto, winningLotto, bonusNumber)) + .reduce(new LottoResult(Map.of()), LottoResult::merge); } public Lottos merge(final Lottos other) { diff --git a/src/main/java/org/duckstudy/view/OutputView.java b/src/main/java/org/duckstudy/view/OutputView.java index 2bd0e806..cf1db4ad 100644 --- a/src/main/java/org/duckstudy/view/OutputView.java +++ b/src/main/java/org/duckstudy/view/OutputView.java @@ -7,7 +7,7 @@ import java.util.stream.Collectors; import org.duckstudy.model.lotto.Lotto; import org.duckstudy.model.lotto.LottoNumber; -import org.duckstudy.model.lotto.LottoStatistics; +import org.duckstudy.model.lotto.LottoResult; import org.duckstudy.model.lotto.Lottos; import org.duckstudy.model.lotto.constant.WinningRank; @@ -43,26 +43,25 @@ public void printInputBonusNumber() { System.out.println("\n보너스 볼을 입력해 주세요."); } - public void printLottoStatistics(final LottoStatistics result) { + public void printLottoResult(final LottoResult result) { System.out.println("\n당첨 통계"); System.out.println("---------"); iterateLottoResult(result); } - private void iterateLottoResult(final LottoStatistics result) { + private void iterateLottoResult(final LottoResult result) { for (WinningRank winningRank : WinningRank.values()) { printMatchingResult(result, winningRank); } System.out.println(); } - private void printMatchingResult(final LottoStatistics result, final WinningRank winningRank) { + private void printMatchingResult(final LottoResult result, final WinningRank winningRank) { if (winningRank == NONE) { return; } List matchCounts = winningRank.getMatchCount(); -// int key = winningRank.getKey(); int price = winningRank.getPrice(); String matchPriceMessage = getMatchPrice(winningRank, matchCounts.get(0), price); diff --git a/src/test/java/org/duckstudy/model/PriceTest.java b/src/test/java/org/duckstudy/model/PriceTest.java index 295f0d44..be192345 100644 --- a/src/test/java/org/duckstudy/model/PriceTest.java +++ b/src/test/java/org/duckstudy/model/PriceTest.java @@ -6,7 +6,7 @@ import java.util.HashMap; import java.util.Map; -import org.duckstudy.model.lotto.LottoStatistics; +import org.duckstudy.model.lotto.LottoResult; import org.duckstudy.model.lotto.constant.WinningRank; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -135,9 +135,9 @@ void calculateProfitRate() { Price purchasePrice = new Price(15000); Map result = new HashMap<>(); result.put(WinningRank.SECOND, 1); - LottoStatistics lottoStatistics = new LottoStatistics(result); + LottoResult lottoResult = new LottoResult(result); - assertThat(purchasePrice.calculateProfitRate(lottoStatistics)).isEqualTo(200000.0); + assertThat(purchasePrice.calculateProfitRate(lottoResult)).isEqualTo(200000.0); } } } diff --git a/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java b/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java new file mode 100644 index 00000000..cf498be2 --- /dev/null +++ b/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java @@ -0,0 +1,40 @@ +package org.duckstudy.model.lotto; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Map; +import java.util.Set; +import org.duckstudy.model.lotto.constant.WinningRank; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("로또 당첨 결과 테스트") +class LottoResultTest { + + @Test + @DisplayName("로또 당첨 결과를 생성한다") + void createLottoResult() { + + Lotto lotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 7)); + Lotto winningLotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 6)); + LottoNumber bonusNumber = LottoNumber.valueOf(7); + + LottoResult lottoResult = LottoResult.createLottoResult(lotto, winningLotto, bonusNumber); + + assertThat(lottoResult.getResult()) + .containsEntry(WinningRank.SECOND, 1); + } + + @Test + @DisplayName("로또 당첨 결과를 병합한다") + void mergeLottoResult() { + + LottoResult lottoResult1 = new LottoResult(Map.of(WinningRank.FIFTH, 2)); + LottoResult lottoResult2 = new LottoResult(Map.of(WinningRank.FIFTH, 1)); + + LottoResult mergedLottoResult = lottoResult1.merge(lottoResult2); + + assertThat(mergedLottoResult.getResult()) + .containsEntry(WinningRank.FIFTH, 3); + } +} diff --git a/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java b/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java deleted file mode 100644 index 7e139161..00000000 --- a/src/test/java/org/duckstudy/model/lotto/LottoStatisticsTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.duckstudy.model.lotto; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Map; -import java.util.Set; -import org.duckstudy.model.lotto.constant.WinningRank; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -@DisplayName("로또 당첨 통계 테스트") -class LottoStatisticsTest { - - @Test - @DisplayName("로또 당첨 통계를 생성한다") - void createLottoStatistics() { - - Lotto lotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 7)); - Lotto winningLotto = Lotto.from(Set.of(1, 2, 3, 4, 5, 6)); - LottoNumber bonusNumber = LottoNumber.valueOf(7); - - LottoStatistics lottoStatistics = LottoStatistics.createLottoResult(lotto, winningLotto, bonusNumber); - - assertThat(lottoStatistics.getStatistics()) - .containsEntry(WinningRank.SECOND, 1); - } - - @Test - @DisplayName("로또 당첨 통계를 병합한다") - void mergeLottoStatistics() { - - LottoStatistics lottoStatistics1 = new LottoStatistics(Map.of(WinningRank.FIFTH, 2)); - LottoStatistics lottoStatistics2 = new LottoStatistics(Map.of(WinningRank.FIFTH, 1)); - - LottoStatistics mergedLottoStatistics = lottoStatistics1.merge(lottoStatistics2); - - assertThat(mergedLottoStatistics.getStatistics()) - .containsEntry(WinningRank.FIFTH, 3); - } -} diff --git a/src/test/java/org/duckstudy/model/lotto/LottosTest.java b/src/test/java/org/duckstudy/model/lotto/LottosTest.java index cac0a8af..481fdee4 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottosTest.java +++ b/src/test/java/org/duckstudy/model/lotto/LottosTest.java @@ -52,9 +52,9 @@ void calculateWinningResultWhenInputWinningLotto() { Lotto.from(Set.of(20, 21, 22, 23, 24, 25)) )); - LottoStatistics lottoStatistics = totalLottos.accumulateLottoResult(winningLotto, bonusNumber); + LottoResult lottoResult = totalLottos.accumulateLottoResult(winningLotto, bonusNumber); - assertThat(lottoStatistics.getStatistics()) + assertThat(lottoResult.getResult()) .containsExactly( entry(NONE, 3), entry(SECOND, 1), From 2ff823cbfb74d96de823f92b95ff0ea57edf6b14 Mon Sep 17 00:00:00 2001 From: Mint Date: Mon, 1 Jul 2024 19:50:13 +0900 Subject: [PATCH 25/27] =?UTF-8?q?feat:=20lotto=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EC=98=A4=EB=A6=84=EC=B0=A8=EC=88=9C=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/duckstudy/view/OutputView.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/duckstudy/view/OutputView.java b/src/main/java/org/duckstudy/view/OutputView.java index cf1db4ad..32904dbf 100644 --- a/src/main/java/org/duckstudy/view/OutputView.java +++ b/src/main/java/org/duckstudy/view/OutputView.java @@ -31,6 +31,7 @@ private void printLotto(final Lotto lotto) { System.out.println(lotto.getLotto() .stream() .map(LottoNumber::getValue) + .sorted() .map(String::valueOf) .collect(Collectors.joining(", ", "[", "]"))); } From 4188aeca33613e53c216a0e43d8ef665a849727e Mon Sep 17 00:00:00 2001 From: Mint Date: Mon, 1 Jul 2024 20:33:47 +0900 Subject: [PATCH 26/27] =?UTF-8?q?rename:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 콘솔 애플리케이션 구현에 맞게 적절한 패키지명을 부여한다. --- src/main/java/org/duckstudy/Application.java | 10 ++++----- .../duckstudy/{model => domain}/Price.java | 8 +++---- .../{model => domain}/lotto/Lotto.java | 6 ++--- .../{model => domain}/lotto/LottoCount.java | 2 +- .../{model => domain}/lotto/LottoNumber.java | 2 +- .../{model => domain}/lotto/LottoResult.java | 4 ++-- .../{model => domain}/lotto/Lottos.java | 2 +- .../lotto/constant/WinningRank.java | 2 +- .../LottoGame.java} | 22 +++++++++---------- .../org/duckstudy/{view => io}/InputView.java | 2 +- .../duckstudy/{view => io}/OutputView.java | 16 +++++++------- .../{model => domain}/PriceTest.java | 6 ++--- .../lotto/LottoCountTest.java | 2 +- .../lotto/LottoNumberTest.java | 2 +- .../lotto/LottoResultTest.java | 4 ++-- .../{model => domain}/lotto/LottoTest.java | 2 +- .../{model => domain}/lotto/LottosTest.java | 10 ++++----- .../lotto/constant/WinningRankTest.java | 2 +- 18 files changed, 52 insertions(+), 52 deletions(-) rename src/main/java/org/duckstudy/{model => domain}/Price.java (93%) rename src/main/java/org/duckstudy/{model => domain}/lotto/Lotto.java (92%) rename src/main/java/org/duckstudy/{model => domain}/lotto/LottoCount.java (97%) rename src/main/java/org/duckstudy/{model => domain}/lotto/LottoNumber.java (97%) rename src/main/java/org/duckstudy/{model => domain}/lotto/LottoResult.java (94%) rename src/main/java/org/duckstudy/{model => domain}/lotto/Lottos.java (97%) rename src/main/java/org/duckstudy/{model => domain}/lotto/constant/WinningRank.java (97%) rename src/main/java/org/duckstudy/{controller/LottoController.java => game/LottoGame.java} (89%) rename src/main/java/org/duckstudy/{view => io}/InputView.java (98%) rename src/main/java/org/duckstudy/{view => io}/OutputView.java (87%) rename src/test/java/org/duckstudy/{model => domain}/PriceTest.java (96%) rename src/test/java/org/duckstudy/{model => domain}/lotto/LottoCountTest.java (98%) rename src/test/java/org/duckstudy/{model => domain}/lotto/LottoNumberTest.java (97%) rename src/test/java/org/duckstudy/{model => domain}/lotto/LottoResultTest.java (92%) rename src/test/java/org/duckstudy/{model => domain}/lotto/LottoTest.java (99%) rename src/test/java/org/duckstudy/{model => domain}/lotto/LottosTest.java (90%) rename src/test/java/org/duckstudy/{model => domain}/lotto/constant/WinningRankTest.java (96%) diff --git a/src/main/java/org/duckstudy/Application.java b/src/main/java/org/duckstudy/Application.java index 53873c44..6fcc749d 100644 --- a/src/main/java/org/duckstudy/Application.java +++ b/src/main/java/org/duckstudy/Application.java @@ -2,9 +2,9 @@ import java.io.BufferedReader; import java.io.InputStreamReader; -import org.duckstudy.controller.LottoController; -import org.duckstudy.view.InputView; -import org.duckstudy.view.OutputView; +import org.duckstudy.game.LottoGame; +import org.duckstudy.io.InputView; +import org.duckstudy.io.OutputView; public class Application { public static void main(String[] args) { @@ -12,7 +12,7 @@ public static void main(String[] args) { OutputView outputView = new OutputView(); InputView inputView = new InputView(bufferedReader); - LottoController lottoController = new LottoController(outputView, inputView); - lottoController.run(); + LottoGame lottoGame = new LottoGame(outputView, inputView); + lottoGame.run(); } } diff --git a/src/main/java/org/duckstudy/model/Price.java b/src/main/java/org/duckstudy/domain/Price.java similarity index 93% rename from src/main/java/org/duckstudy/model/Price.java rename to src/main/java/org/duckstudy/domain/Price.java index 1dc67ab5..f06c63d5 100644 --- a/src/main/java/org/duckstudy/model/Price.java +++ b/src/main/java/org/duckstudy/domain/Price.java @@ -1,9 +1,9 @@ -package org.duckstudy.model; +package org.duckstudy.domain; import java.util.Objects; -import org.duckstudy.model.lotto.LottoCount; -import org.duckstudy.model.lotto.LottoResult; -import org.duckstudy.model.lotto.constant.WinningRank; +import org.duckstudy.domain.lotto.LottoCount; +import org.duckstudy.domain.lotto.LottoResult; +import org.duckstudy.domain.lotto.constant.WinningRank; public class Price { diff --git a/src/main/java/org/duckstudy/model/lotto/Lotto.java b/src/main/java/org/duckstudy/domain/lotto/Lotto.java similarity index 92% rename from src/main/java/org/duckstudy/model/lotto/Lotto.java rename to src/main/java/org/duckstudy/domain/lotto/Lotto.java index e1f26c46..a8e43748 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lotto.java +++ b/src/main/java/org/duckstudy/domain/lotto/Lotto.java @@ -1,11 +1,11 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import static java.util.Collections.shuffle; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toSet; -import static org.duckstudy.model.lotto.LottoNumber.END_INCLUSIVE_NUMBER; -import static org.duckstudy.model.lotto.LottoNumber.START_INCLUSIVE_NUMBER; +import static org.duckstudy.domain.lotto.LottoNumber.END_INCLUSIVE_NUMBER; +import static org.duckstudy.domain.lotto.LottoNumber.START_INCLUSIVE_NUMBER; import java.util.Collections; import java.util.HashSet; diff --git a/src/main/java/org/duckstudy/model/lotto/LottoCount.java b/src/main/java/org/duckstudy/domain/lotto/LottoCount.java similarity index 97% rename from src/main/java/org/duckstudy/model/lotto/LottoCount.java rename to src/main/java/org/duckstudy/domain/lotto/LottoCount.java index 8ebc449b..7bf975eb 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoCount.java +++ b/src/main/java/org/duckstudy/domain/lotto/LottoCount.java @@ -1,4 +1,4 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import java.util.Objects; diff --git a/src/main/java/org/duckstudy/model/lotto/LottoNumber.java b/src/main/java/org/duckstudy/domain/lotto/LottoNumber.java similarity index 97% rename from src/main/java/org/duckstudy/model/lotto/LottoNumber.java rename to src/main/java/org/duckstudy/domain/lotto/LottoNumber.java index 465e61fc..d51dac4f 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoNumber.java +++ b/src/main/java/org/duckstudy/domain/lotto/LottoNumber.java @@ -1,4 +1,4 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import java.util.List; import java.util.Objects; diff --git a/src/main/java/org/duckstudy/model/lotto/LottoResult.java b/src/main/java/org/duckstudy/domain/lotto/LottoResult.java similarity index 94% rename from src/main/java/org/duckstudy/model/lotto/LottoResult.java rename to src/main/java/org/duckstudy/domain/lotto/LottoResult.java index 1fb6cfb1..a44d7c3e 100644 --- a/src/main/java/org/duckstudy/model/lotto/LottoResult.java +++ b/src/main/java/org/duckstudy/domain/lotto/LottoResult.java @@ -1,11 +1,11 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.duckstudy.model.lotto.constant.WinningRank; +import org.duckstudy.domain.lotto.constant.WinningRank; public class LottoResult { diff --git a/src/main/java/org/duckstudy/model/lotto/Lottos.java b/src/main/java/org/duckstudy/domain/lotto/Lottos.java similarity index 97% rename from src/main/java/org/duckstudy/model/lotto/Lottos.java rename to src/main/java/org/duckstudy/domain/lotto/Lottos.java index da1f29f8..9a40e2b9 100644 --- a/src/main/java/org/duckstudy/model/lotto/Lottos.java +++ b/src/main/java/org/duckstudy/domain/lotto/Lottos.java @@ -1,4 +1,4 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; diff --git a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java b/src/main/java/org/duckstudy/domain/lotto/constant/WinningRank.java similarity index 97% rename from src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java rename to src/main/java/org/duckstudy/domain/lotto/constant/WinningRank.java index 5bcd16ca..0abbac9c 100644 --- a/src/main/java/org/duckstudy/model/lotto/constant/WinningRank.java +++ b/src/main/java/org/duckstudy/domain/lotto/constant/WinningRank.java @@ -1,4 +1,4 @@ -package org.duckstudy.model.lotto.constant; +package org.duckstudy.domain.lotto.constant; import java.util.Arrays; import java.util.List; diff --git a/src/main/java/org/duckstudy/controller/LottoController.java b/src/main/java/org/duckstudy/game/LottoGame.java similarity index 89% rename from src/main/java/org/duckstudy/controller/LottoController.java rename to src/main/java/org/duckstudy/game/LottoGame.java index 3dbc0c05..b06cd180 100644 --- a/src/main/java/org/duckstudy/controller/LottoController.java +++ b/src/main/java/org/duckstudy/game/LottoGame.java @@ -1,22 +1,22 @@ -package org.duckstudy.controller; +package org.duckstudy.game; import java.util.stream.Collectors; import java.util.stream.IntStream; -import org.duckstudy.model.Price; -import org.duckstudy.model.lotto.Lotto; -import org.duckstudy.model.lotto.LottoCount; -import org.duckstudy.model.lotto.LottoNumber; -import org.duckstudy.model.lotto.LottoResult; -import org.duckstudy.model.lotto.Lottos; -import org.duckstudy.view.InputView; -import org.duckstudy.view.OutputView; +import org.duckstudy.domain.Price; +import org.duckstudy.domain.lotto.Lotto; +import org.duckstudy.domain.lotto.LottoCount; +import org.duckstudy.domain.lotto.LottoNumber; +import org.duckstudy.domain.lotto.LottoResult; +import org.duckstudy.domain.lotto.Lottos; +import org.duckstudy.io.InputView; +import org.duckstudy.io.OutputView; -public class LottoController { +public class LottoGame { private final OutputView outputView; private final InputView inputView; - public LottoController(final OutputView outputView, final InputView inputView) { + public LottoGame(final OutputView outputView, final InputView inputView) { this.outputView = outputView; this.inputView = inputView; } diff --git a/src/main/java/org/duckstudy/view/InputView.java b/src/main/java/org/duckstudy/io/InputView.java similarity index 98% rename from src/main/java/org/duckstudy/view/InputView.java rename to src/main/java/org/duckstudy/io/InputView.java index 2ca9ed75..fec048bd 100644 --- a/src/main/java/org/duckstudy/view/InputView.java +++ b/src/main/java/org/duckstudy/io/InputView.java @@ -1,4 +1,4 @@ -package org.duckstudy.view; +package org.duckstudy.io; import static java.util.stream.Collectors.toSet; diff --git a/src/main/java/org/duckstudy/view/OutputView.java b/src/main/java/org/duckstudy/io/OutputView.java similarity index 87% rename from src/main/java/org/duckstudy/view/OutputView.java rename to src/main/java/org/duckstudy/io/OutputView.java index 32904dbf..7beca5ab 100644 --- a/src/main/java/org/duckstudy/view/OutputView.java +++ b/src/main/java/org/duckstudy/io/OutputView.java @@ -1,15 +1,15 @@ -package org.duckstudy.view; +package org.duckstudy.io; -import static org.duckstudy.model.lotto.constant.WinningRank.NONE; -import static org.duckstudy.model.lotto.constant.WinningRank.SECOND; +import static org.duckstudy.domain.lotto.constant.WinningRank.NONE; +import static org.duckstudy.domain.lotto.constant.WinningRank.SECOND; import java.util.List; import java.util.stream.Collectors; -import org.duckstudy.model.lotto.Lotto; -import org.duckstudy.model.lotto.LottoNumber; -import org.duckstudy.model.lotto.LottoResult; -import org.duckstudy.model.lotto.Lottos; -import org.duckstudy.model.lotto.constant.WinningRank; +import org.duckstudy.domain.lotto.Lotto; +import org.duckstudy.domain.lotto.LottoNumber; +import org.duckstudy.domain.lotto.LottoResult; +import org.duckstudy.domain.lotto.Lottos; +import org.duckstudy.domain.lotto.constant.WinningRank; public class OutputView { diff --git a/src/test/java/org/duckstudy/model/PriceTest.java b/src/test/java/org/duckstudy/domain/PriceTest.java similarity index 96% rename from src/test/java/org/duckstudy/model/PriceTest.java rename to src/test/java/org/duckstudy/domain/PriceTest.java index be192345..7fb21d70 100644 --- a/src/test/java/org/duckstudy/model/PriceTest.java +++ b/src/test/java/org/duckstudy/domain/PriceTest.java @@ -1,4 +1,4 @@ -package org.duckstudy.model; +package org.duckstudy.domain; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; @@ -6,8 +6,8 @@ import java.util.HashMap; import java.util.Map; -import org.duckstudy.model.lotto.LottoResult; -import org.duckstudy.model.lotto.constant.WinningRank; +import org.duckstudy.domain.lotto.LottoResult; +import org.duckstudy.domain.lotto.constant.WinningRank; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; diff --git a/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java b/src/test/java/org/duckstudy/domain/lotto/LottoCountTest.java similarity index 98% rename from src/test/java/org/duckstudy/model/lotto/LottoCountTest.java rename to src/test/java/org/duckstudy/domain/lotto/LottoCountTest.java index 9db77fc0..535bdde3 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoCountTest.java +++ b/src/test/java/org/duckstudy/domain/lotto/LottoCountTest.java @@ -1,4 +1,4 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; diff --git a/src/test/java/org/duckstudy/model/lotto/LottoNumberTest.java b/src/test/java/org/duckstudy/domain/lotto/LottoNumberTest.java similarity index 97% rename from src/test/java/org/duckstudy/model/lotto/LottoNumberTest.java rename to src/test/java/org/duckstudy/domain/lotto/LottoNumberTest.java index 88376eb6..311e24fd 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoNumberTest.java +++ b/src/test/java/org/duckstudy/domain/lotto/LottoNumberTest.java @@ -1,4 +1,4 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; diff --git a/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java b/src/test/java/org/duckstudy/domain/lotto/LottoResultTest.java similarity index 92% rename from src/test/java/org/duckstudy/model/lotto/LottoResultTest.java rename to src/test/java/org/duckstudy/domain/lotto/LottoResultTest.java index cf498be2..1be0dbcb 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoResultTest.java +++ b/src/test/java/org/duckstudy/domain/lotto/LottoResultTest.java @@ -1,10 +1,10 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import static org.assertj.core.api.Assertions.assertThat; import java.util.Map; import java.util.Set; -import org.duckstudy.model.lotto.constant.WinningRank; +import org.duckstudy.domain.lotto.constant.WinningRank; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/org/duckstudy/model/lotto/LottoTest.java b/src/test/java/org/duckstudy/domain/lotto/LottoTest.java similarity index 99% rename from src/test/java/org/duckstudy/model/lotto/LottoTest.java rename to src/test/java/org/duckstudy/domain/lotto/LottoTest.java index 745c8926..9c5c69ef 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottoTest.java +++ b/src/test/java/org/duckstudy/domain/lotto/LottoTest.java @@ -1,4 +1,4 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import static java.util.stream.Collectors.toSet; import static org.assertj.core.api.Assertions.assertThat; diff --git a/src/test/java/org/duckstudy/model/lotto/LottosTest.java b/src/test/java/org/duckstudy/domain/lotto/LottosTest.java similarity index 90% rename from src/test/java/org/duckstudy/model/lotto/LottosTest.java rename to src/test/java/org/duckstudy/domain/lotto/LottosTest.java index 481fdee4..f5681802 100644 --- a/src/test/java/org/duckstudy/model/lotto/LottosTest.java +++ b/src/test/java/org/duckstudy/domain/lotto/LottosTest.java @@ -1,14 +1,14 @@ -package org.duckstudy.model.lotto; +package org.duckstudy.domain.lotto; import static java.util.Map.entry; import static org.assertj.core.api.Assertions.assertThat; -import static org.duckstudy.model.lotto.constant.WinningRank.FIRST; -import static org.duckstudy.model.lotto.constant.WinningRank.NONE; -import static org.duckstudy.model.lotto.constant.WinningRank.SECOND; +import static org.duckstudy.domain.lotto.constant.WinningRank.FIRST; +import static org.duckstudy.domain.lotto.constant.WinningRank.NONE; +import static org.duckstudy.domain.lotto.constant.WinningRank.SECOND; import java.util.List; import java.util.Set; -import org.duckstudy.model.Price; +import org.duckstudy.domain.Price; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; diff --git a/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java b/src/test/java/org/duckstudy/domain/lotto/constant/WinningRankTest.java similarity index 96% rename from src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java rename to src/test/java/org/duckstudy/domain/lotto/constant/WinningRankTest.java index 13ccca00..c14997d4 100644 --- a/src/test/java/org/duckstudy/model/lotto/constant/WinningRankTest.java +++ b/src/test/java/org/duckstudy/domain/lotto/constant/WinningRankTest.java @@ -1,4 +1,4 @@ -package org.duckstudy.model.lotto.constant; +package org.duckstudy.domain.lotto.constant; import static org.assertj.core.api.Assertions.assertThat; From 2134c60803715352bb3ae3cc3294198a32f12055 Mon Sep 17 00:00:00 2001 From: Mint Date: Mon, 1 Jul 2024 20:42:17 +0900 Subject: [PATCH 27/27] =?UTF-8?q?docs:=20README.md=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index affcbe98..1fe627a9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - `Application` : 로또 실행을 담당하는 진입점 클래스 -## model +## domain - `Price` : 구매 가격 VO @@ -20,14 +20,14 @@ - `WinningRank` : 당첨 순위 Enum -## view +## io - `InputView` : 입력을 담당하는 클래스 - `OutputView` : 출력을 담당하는 클래스 -## controller +## game -- `LottoController` : 로또 전체 흐름을 제어하는 클래스 +- `LottoGame` : 로또 게임을 담당하는 클래스