From a2e08722668b9d6c51b4bb930146b69f707732d9 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Thu, 26 Sep 2024 23:09:19 +0900 Subject: [PATCH 01/16] =?UTF-8?q?refactor:=20Lotto=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20domain=EC=9C=BC=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/{ => domain}/Lotto.java | 2 +- src/test/java/lotto/LottoTest.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) rename src/main/java/lotto/{ => domain}/Lotto.java (94%) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/domain/Lotto.java similarity index 94% rename from src/main/java/lotto/Lotto.java rename to src/main/java/lotto/domain/Lotto.java index 519793d1f73..366657953a2 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.domain; import java.util.List; diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 9f5dfe7eb83..0e4d4f73896 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,5 +1,6 @@ package lotto; +import lotto.domain.Lotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From c20a49ce5fb3e0bbb1b95fdc964efecabd770387 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Fri, 27 Sep 2024 00:04:41 +0900 Subject: [PATCH 02/16] =?UTF-8?q?feat:=20InputView,=20OutputView=20?= =?UTF-8?q?=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/view/input/InputView.java | 9 ++++++++ .../java/lotto/view/output/OutputMessage.java | 22 +++++++++++++++++++ .../java/lotto/view/output/OutputView.java | 11 ++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/main/java/lotto/view/input/InputView.java create mode 100644 src/main/java/lotto/view/output/OutputMessage.java create mode 100644 src/main/java/lotto/view/output/OutputView.java diff --git a/src/main/java/lotto/view/input/InputView.java b/src/main/java/lotto/view/input/InputView.java new file mode 100644 index 00000000000..d1ca62d9d85 --- /dev/null +++ b/src/main/java/lotto/view/input/InputView.java @@ -0,0 +1,9 @@ +package lotto.view.input; + +import static camp.nextstep.edu.missionutils.Console.readLine; + +public class InputView { + public static String Input() { + return readLine(); + } +} diff --git a/src/main/java/lotto/view/output/OutputMessage.java b/src/main/java/lotto/view/output/OutputMessage.java new file mode 100644 index 00000000000..bd81d18be2d --- /dev/null +++ b/src/main/java/lotto/view/output/OutputMessage.java @@ -0,0 +1,22 @@ +package lotto.view.output; + +public enum OutputMessage { + ASK_PURCHASE_PRICE_MESSAGE("구입금액을 입력해 주세요."), + PURCHASE_INFORMATION_MESSAGE("\n%d개를 구매했습니다."), + ASK_WINNING_NUMBER_MESSAGE("\n당첨 번호를 입력해 주세요."), + ASK_BONUS_NUMBER_MESSAGE("\n보너스 번호를 입력해 주세요."), + WINNING_STATISTICS_MESSAGE("\n당첨 통계\n---"), + WINNING_STATISTICS_DETAILS_MESSAGE("%d개 일치 (%s원) - %d개"), + SECOND_WINNING_DETAILS_MESSAGE("%d개 일치, 보너스 볼 일치 (%s원) - %d개"), + RATE_OF_RETURN_MESSAGE("총 수익률은 %f%입니다."); + + private final String message; + + OutputMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/lotto/view/output/OutputView.java b/src/main/java/lotto/view/output/OutputView.java new file mode 100644 index 00000000000..9939989cdb1 --- /dev/null +++ b/src/main/java/lotto/view/output/OutputView.java @@ -0,0 +1,11 @@ +package lotto.view.output; + +public class OutputView { + public static void PrintStaticMessage(OutputMessage message) { + System.out.println(message.getMessage()); + } + + public static void PrintDynamicMessage(String message) { + System.out.println(message); + } +} From f8ab6cf69b9daf7ab6b39c5da1cb044622e3d3dd Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Fri, 27 Sep 2024 00:20:32 +0900 Subject: [PATCH 03/16] =?UTF-8?q?feat:=20global=20config=20=EA=B5=AC?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/global/config/LottoConfig.java | 18 ++++++++++++++++++ .../java/lotto/global/config/PrizeConfig.java | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/main/java/lotto/global/config/LottoConfig.java create mode 100644 src/main/java/lotto/global/config/PrizeConfig.java diff --git a/src/main/java/lotto/global/config/LottoConfig.java b/src/main/java/lotto/global/config/LottoConfig.java new file mode 100644 index 00000000000..52b3685803f --- /dev/null +++ b/src/main/java/lotto/global/config/LottoConfig.java @@ -0,0 +1,18 @@ +package lotto.global.config; + +public enum LottoConfig { + LOTTO_COST(1000), + + MIN_LOTTO_NUM(1), + MAX_LOTTO_NUM(45), + + NUMBER_OF_PICKS(6), + NUMBER_OF_BONUS(1); + + private final int value; + + LottoConfig(int value) { + this.value = value; + } + +} diff --git a/src/main/java/lotto/global/config/PrizeConfig.java b/src/main/java/lotto/global/config/PrizeConfig.java new file mode 100644 index 00000000000..7d214d48737 --- /dev/null +++ b/src/main/java/lotto/global/config/PrizeConfig.java @@ -0,0 +1,17 @@ +package lotto.global.config; + +public enum PrizeConfig { + NUMBER_OF_WINNER(5), + + FIRST_WINNING_PRIZE(2000000000), + SECOND_WINNING_PRIZE(30000000), + THIRD_WINNING_PRIZE(1500000), + FOURTH_WINNING_PRIZE(50000), + FIFTH_WINNING_PRIZE(5000); + + private final int value; + + PrizeConfig(int value) { + this.value = value; + } +} From 0c71de785832a4934eb5ee646eb0a6ad16b39d69 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Fri, 27 Sep 2024 21:28:51 +0900 Subject: [PATCH 04/16] =?UTF-8?q?refactor:=20enum=20=EA=B0=92=20=EB=B0=9B?= =?UTF-8?q?=EC=95=84=EC=98=A4=EA=B8=B0=20=EC=9C=84=ED=95=B4=20getValue()?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/global/config/LottoConfig.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/lotto/global/config/LottoConfig.java b/src/main/java/lotto/global/config/LottoConfig.java index 52b3685803f..020265ebebd 100644 --- a/src/main/java/lotto/global/config/LottoConfig.java +++ b/src/main/java/lotto/global/config/LottoConfig.java @@ -9,6 +9,10 @@ public enum LottoConfig { NUMBER_OF_PICKS(6), NUMBER_OF_BONUS(1); + public int getValue() { + return value; + } + private final int value; LottoConfig(int value) { From 0b390ad8c103e614b875f8683cd3a0a8fb095da4 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Fri, 27 Sep 2024 21:29:22 +0900 Subject: [PATCH 05/16] =?UTF-8?q?feat:=20=EA=B5=AC=EC=9E=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/controller/BuyerController.java | 16 ++++++++++++++ .../java/lotto/controller/MainController.java | 9 ++++++++ src/main/java/lotto/domain/Buyer.java | 22 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 src/main/java/lotto/controller/BuyerController.java create mode 100644 src/main/java/lotto/controller/MainController.java create mode 100644 src/main/java/lotto/domain/Buyer.java diff --git a/src/main/java/lotto/controller/BuyerController.java b/src/main/java/lotto/controller/BuyerController.java new file mode 100644 index 00000000000..7162dd14ad5 --- /dev/null +++ b/src/main/java/lotto/controller/BuyerController.java @@ -0,0 +1,16 @@ +package lotto.controller; + +import lotto.domain.Buyer; +import lotto.view.input.InputView; + +import static lotto.view.output.OutputMessage.ASK_PURCHASE_PRICE_MESSAGE; +import static lotto.view.output.OutputView.PrintStaticMessage; + +public class BuyerController { + public static Buyer payment() { + PrintStaticMessage(ASK_PURCHASE_PRICE_MESSAGE); + + int cost = Integer.getInteger(InputView.Input()); + return Buyer.create(cost); + } +} diff --git a/src/main/java/lotto/controller/MainController.java b/src/main/java/lotto/controller/MainController.java new file mode 100644 index 00000000000..99cd529ea5e --- /dev/null +++ b/src/main/java/lotto/controller/MainController.java @@ -0,0 +1,9 @@ +package lotto.controller; + +import lotto.domain.Buyer; + +public class MainController { + public static void start() { + Buyer buyer = BuyerController.payment(); + } +} diff --git a/src/main/java/lotto/domain/Buyer.java b/src/main/java/lotto/domain/Buyer.java new file mode 100644 index 00000000000..8d529b939aa --- /dev/null +++ b/src/main/java/lotto/domain/Buyer.java @@ -0,0 +1,22 @@ +package lotto.domain; + +import static lotto.global.config.LottoConfig.LOTTO_COST; + +public class Buyer { + private int purchaseCost; + private int purchaseCount; + + private Buyer(int purchaseCost) { + getPurchaseCount(); + + this.purchaseCost = purchaseCost; + } + + public static Buyer create(int purchaseCost) { + return new Buyer(purchaseCost); + } + + private int getPurchaseCount() { + return purchaseCost / LOTTO_COST.getValue(); + } +} From c6ce4f5343a389b157ffd3068be090c159e3f05e Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Fri, 27 Sep 2024 21:58:51 +0900 Subject: [PATCH 06/16] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EC=A7=81=EA=B4=80=EC=84=B1=EC=9D=84=20=EC=9C=84=ED=95=B4=20?= =?UTF-8?q?createBuyer()=EB=A1=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85?= =?UTF-8?q?=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/lotto/controller/BuyerController.java | 2 +- src/main/java/lotto/domain/Buyer.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/controller/BuyerController.java b/src/main/java/lotto/controller/BuyerController.java index 7162dd14ad5..329ff7f260b 100644 --- a/src/main/java/lotto/controller/BuyerController.java +++ b/src/main/java/lotto/controller/BuyerController.java @@ -11,6 +11,6 @@ public static Buyer payment() { PrintStaticMessage(ASK_PURCHASE_PRICE_MESSAGE); int cost = Integer.getInteger(InputView.Input()); - return Buyer.create(cost); + return Buyer.createBuyer(cost); } } diff --git a/src/main/java/lotto/domain/Buyer.java b/src/main/java/lotto/domain/Buyer.java index 8d529b939aa..85907c9754c 100644 --- a/src/main/java/lotto/domain/Buyer.java +++ b/src/main/java/lotto/domain/Buyer.java @@ -12,7 +12,7 @@ private Buyer(int purchaseCost) { this.purchaseCost = purchaseCost; } - public static Buyer create(int purchaseCost) { + public static Buyer createBuyer(int purchaseCost) { return new Buyer(purchaseCost); } From b6a0b08dcc771a57de26eb7e7f24aa7f6e1b8208 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Fri, 27 Sep 2024 22:11:00 +0900 Subject: [PATCH 07/16] =?UTF-8?q?refactor:=20purchaseCount=20=EC=A0=91?= =?UTF-8?q?=EA=B7=BC=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=B4=20public?= =?UTF-8?q?=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 --- src/main/java/lotto/domain/Buyer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/lotto/domain/Buyer.java b/src/main/java/lotto/domain/Buyer.java index 85907c9754c..6ba7ae0596b 100644 --- a/src/main/java/lotto/domain/Buyer.java +++ b/src/main/java/lotto/domain/Buyer.java @@ -4,7 +4,7 @@ public class Buyer { private int purchaseCost; - private int purchaseCount; + public int purchaseCount; private Buyer(int purchaseCost) { getPurchaseCount(); From db05c85da1dc382bb7429fe6423b44e5364ebdb6 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Fri, 27 Sep 2024 22:13:12 +0900 Subject: [PATCH 08/16] =?UTF-8?q?feat:=20PrintNewLine()=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80,=20=EA=B7=B8=EC=97=90=20=EB=94=B0=EB=9D=BC=20enum?= =?UTF-8?q?=EC=9D=98=20=EA=B0=9C=ED=96=89=EB=AC=B8=EC=9E=90=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/view/output/OutputMessage.java | 8 ++++---- src/main/java/lotto/view/output/OutputView.java | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/lotto/view/output/OutputMessage.java b/src/main/java/lotto/view/output/OutputMessage.java index bd81d18be2d..d607c1d2d29 100644 --- a/src/main/java/lotto/view/output/OutputMessage.java +++ b/src/main/java/lotto/view/output/OutputMessage.java @@ -2,10 +2,10 @@ public enum OutputMessage { ASK_PURCHASE_PRICE_MESSAGE("구입금액을 입력해 주세요."), - PURCHASE_INFORMATION_MESSAGE("\n%d개를 구매했습니다."), - ASK_WINNING_NUMBER_MESSAGE("\n당첨 번호를 입력해 주세요."), - ASK_BONUS_NUMBER_MESSAGE("\n보너스 번호를 입력해 주세요."), - WINNING_STATISTICS_MESSAGE("\n당첨 통계\n---"), + PURCHASE_INFORMATION_MESSAGE("%d개를 구매했습니다."), + ASK_WINNING_NUMBER_MESSAGE("당첨 번호를 입력해 주세요."), + ASK_BONUS_NUMBER_MESSAGE("보너스 번호를 입력해 주세요."), + WINNING_STATISTICS_MESSAGE("당첨 통계\n---"), WINNING_STATISTICS_DETAILS_MESSAGE("%d개 일치 (%s원) - %d개"), SECOND_WINNING_DETAILS_MESSAGE("%d개 일치, 보너스 볼 일치 (%s원) - %d개"), RATE_OF_RETURN_MESSAGE("총 수익률은 %f%입니다."); diff --git a/src/main/java/lotto/view/output/OutputView.java b/src/main/java/lotto/view/output/OutputView.java index 9939989cdb1..44c739f1559 100644 --- a/src/main/java/lotto/view/output/OutputView.java +++ b/src/main/java/lotto/view/output/OutputView.java @@ -8,4 +8,9 @@ public static void PrintStaticMessage(OutputMessage message) { public static void PrintDynamicMessage(String message) { System.out.println(message); } + + public static void PrintNewLine() { + System.out.println(); + } + } From c835a434548b802019ff643b4665f5744ebc4fe0 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Sat, 28 Sep 2024 00:11:27 +0900 Subject: [PATCH 09/16] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=20=EB=B2=88=ED=98=B8=20=EC=83=9D=EC=84=B1=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 --- src/main/java/lotto/Application.java | 4 ++- .../lotto/controller/BuyerController.java | 14 ++++++++-- .../lotto/controller/LottoController.java | 28 +++++++++++++++++++ .../java/lotto/controller/MainController.java | 3 ++ src/main/java/lotto/domain/Buyer.java | 6 ++-- src/main/java/lotto/domain/Lottos.java | 15 ++++++++++ src/main/java/lotto/service/LottoService.java | 28 +++++++++++++++++++ .../java/lotto/view/output/OutputView.java | 6 ++++ 8 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/main/java/lotto/controller/LottoController.java create mode 100644 src/main/java/lotto/domain/Lottos.java create mode 100644 src/main/java/lotto/service/LottoService.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba44..667d409ce4b 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,9 @@ package lotto; +import lotto.controller.MainController; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + MainController.start(); } } diff --git a/src/main/java/lotto/controller/BuyerController.java b/src/main/java/lotto/controller/BuyerController.java index 329ff7f260b..c151487068a 100644 --- a/src/main/java/lotto/controller/BuyerController.java +++ b/src/main/java/lotto/controller/BuyerController.java @@ -3,14 +3,24 @@ import lotto.domain.Buyer; import lotto.view.input.InputView; +import static java.lang.String.format; import static lotto.view.output.OutputMessage.ASK_PURCHASE_PRICE_MESSAGE; -import static lotto.view.output.OutputView.PrintStaticMessage; +import static lotto.view.output.OutputMessage.PURCHASE_INFORMATION_MESSAGE; +import static lotto.view.output.OutputView.*; public class BuyerController { public static Buyer payment() { PrintStaticMessage(ASK_PURCHASE_PRICE_MESSAGE); - int cost = Integer.getInteger(InputView.Input()); + int cost = Integer.parseInt(InputView.Input()); return Buyer.createBuyer(cost); } + public static int purchase(Buyer buyer) { + int purchaseCount = buyer.purchaseCount; + + PrintNewLine(); + PrintDynamicMessage(format(PURCHASE_INFORMATION_MESSAGE.getMessage(), purchaseCount)); + + return purchaseCount; + } } diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java new file mode 100644 index 00000000000..8c44364d6a9 --- /dev/null +++ b/src/main/java/lotto/controller/LottoController.java @@ -0,0 +1,28 @@ +package lotto.controller; + +import lotto.domain.Lottos; +import lotto.service.LottoService; + +import java.util.ArrayList; +import java.util.List; + +import static lotto.view.output.OutputView.PrintLottoNumbers; +import static lotto.view.output.OutputView.PrintNewLine; + +public class LottoController { + public static Lottos generateLottos(int purchaseCount) { + List> numbersList = new ArrayList<>(); + + for (int i = 0; i < purchaseCount; i++) { + List numbers = LottoService.generateRandomNumbers(); + numbersList.add(numbers); + PrintLottoNumbers(numbers); + } + + PrintNewLine(); + + return LottoService.convertToLottos(numbersList); + } + + +} diff --git a/src/main/java/lotto/controller/MainController.java b/src/main/java/lotto/controller/MainController.java index 99cd529ea5e..58612b43475 100644 --- a/src/main/java/lotto/controller/MainController.java +++ b/src/main/java/lotto/controller/MainController.java @@ -1,9 +1,12 @@ package lotto.controller; import lotto.domain.Buyer; +import lotto.domain.Lottos; public class MainController { public static void start() { Buyer buyer = BuyerController.payment(); + int purchaseCount = BuyerController.purchase(buyer); + Lottos lottos = LottoController.generateLottos(purchaseCount); } } diff --git a/src/main/java/lotto/domain/Buyer.java b/src/main/java/lotto/domain/Buyer.java index 6ba7ae0596b..66a8e3f72e9 100644 --- a/src/main/java/lotto/domain/Buyer.java +++ b/src/main/java/lotto/domain/Buyer.java @@ -7,7 +7,7 @@ public class Buyer { public int purchaseCount; private Buyer(int purchaseCost) { - getPurchaseCount(); + purchaseCount = getPurchaseCount(purchaseCost); this.purchaseCost = purchaseCost; } @@ -16,7 +16,7 @@ public static Buyer createBuyer(int purchaseCost) { return new Buyer(purchaseCost); } - private int getPurchaseCount() { - return purchaseCost / LOTTO_COST.getValue(); + private int getPurchaseCount(int payment) { + return payment / LOTTO_COST.getValue(); } } diff --git a/src/main/java/lotto/domain/Lottos.java b/src/main/java/lotto/domain/Lottos.java new file mode 100644 index 00000000000..76026da3d8d --- /dev/null +++ b/src/main/java/lotto/domain/Lottos.java @@ -0,0 +1,15 @@ +package lotto.domain; + +import java.util.List; + +public class Lottos { + private final List lottos; + + private Lottos(List lottos) { + this.lottos = lottos; + } + + public static Lottos createLottos(List lottos) { + return new Lottos(lottos); + } +} diff --git a/src/main/java/lotto/service/LottoService.java b/src/main/java/lotto/service/LottoService.java new file mode 100644 index 00000000000..9fb26b9eaf2 --- /dev/null +++ b/src/main/java/lotto/service/LottoService.java @@ -0,0 +1,28 @@ +package lotto.service; + +import camp.nextstep.edu.missionutils.Randoms; +import lotto.domain.Lotto; +import lotto.domain.Lottos; + +import java.util.List; +import java.util.stream.Collectors; + +import static lotto.global.config.LottoConfig.*; + +public class LottoService { + public static List generateRandomNumbers() { + return Randoms.pickUniqueNumbersInRange(MIN_LOTTO_NUM.getValue(), MAX_LOTTO_NUM.getValue(), NUMBER_OF_PICKS.getValue()); + } + + public static Lottos convertToLottos(List> randomNumbersList) { + List lottoList = convertToLottoList(randomNumbersList); + return Lottos.createLottos(lottoList); + } + + private static List convertToLottoList(List> randomNumbersList) { + return randomNumbersList.stream() + .map(Lotto::new) + .collect(Collectors.toList()); + } + +} diff --git a/src/main/java/lotto/view/output/OutputView.java b/src/main/java/lotto/view/output/OutputView.java index 44c739f1559..66ca1cab80e 100644 --- a/src/main/java/lotto/view/output/OutputView.java +++ b/src/main/java/lotto/view/output/OutputView.java @@ -1,5 +1,7 @@ package lotto.view.output; +import java.util.List; + public class OutputView { public static void PrintStaticMessage(OutputMessage message) { System.out.println(message.getMessage()); @@ -13,4 +15,8 @@ public static void PrintNewLine() { System.out.println(); } + public static void PrintLottoNumbers(List numbers) { + System.out.println(numbers); + } + } From 70bbf42837f95447fa65b0e750c613ca52832e3e Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Sat, 28 Sep 2024 13:53:05 +0900 Subject: [PATCH 10/16] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/controller/MainController.java | 5 ++++ .../lotto/controller/PrizeController.java | 30 +++++++++++++++++++ src/main/java/lotto/domain/Prize.java | 17 +++++++++++ src/main/java/lotto/service/PrizeService.java | 14 +++++++++ 4 files changed, 66 insertions(+) create mode 100644 src/main/java/lotto/controller/PrizeController.java create mode 100644 src/main/java/lotto/domain/Prize.java create mode 100644 src/main/java/lotto/service/PrizeService.java diff --git a/src/main/java/lotto/controller/MainController.java b/src/main/java/lotto/controller/MainController.java index 58612b43475..692d4e60dc9 100644 --- a/src/main/java/lotto/controller/MainController.java +++ b/src/main/java/lotto/controller/MainController.java @@ -2,11 +2,16 @@ import lotto.domain.Buyer; import lotto.domain.Lottos; +import lotto.domain.Prize; public class MainController { public static void start() { Buyer buyer = BuyerController.payment(); int purchaseCount = BuyerController.purchase(buyer); + Lottos lottos = LottoController.generateLottos(purchaseCount); + + Prize prize = PrizeController.setWinningNumber(); + } } diff --git a/src/main/java/lotto/controller/PrizeController.java b/src/main/java/lotto/controller/PrizeController.java new file mode 100644 index 00000000000..2fa3471a229 --- /dev/null +++ b/src/main/java/lotto/controller/PrizeController.java @@ -0,0 +1,30 @@ +package lotto.controller; + +import lotto.domain.Prize; +import lotto.service.PrizeService; +import lotto.view.input.InputView; + +import java.util.List; + +import static lotto.view.output.OutputMessage.ASK_BONUS_NUMBER_MESSAGE; +import static lotto.view.output.OutputMessage.ASK_WINNING_NUMBER_MESSAGE; +import static lotto.view.output.OutputView.PrintNewLine; +import static lotto.view.output.OutputView.PrintStaticMessage; + +public class PrizeController { + public static Prize setWinningNumber() { + PrintStaticMessage(ASK_WINNING_NUMBER_MESSAGE); + List numbers = PrizeService.parsingNumbers(InputView.Input()); + + int bonusNumber = setBonusNumber(); + + return Prize.createPrize(numbers, bonusNumber); + } + + private static int setBonusNumber() { + PrintNewLine(); + + PrintStaticMessage(ASK_BONUS_NUMBER_MESSAGE); + return Integer.parseInt(InputView.Input()); + } +} diff --git a/src/main/java/lotto/domain/Prize.java b/src/main/java/lotto/domain/Prize.java new file mode 100644 index 00000000000..4666201aa22 --- /dev/null +++ b/src/main/java/lotto/domain/Prize.java @@ -0,0 +1,17 @@ +package lotto.domain; + +import java.util.List; + +public class Prize { + private List numbers; + private int bonusNumber; + + private Prize(List numbers, int bonusNumber) { + this.numbers = numbers; + this.bonusNumber = bonusNumber; + } + + public static Prize createPrize(List numbers, int bonusNumber) { + return new Prize(numbers, bonusNumber); + } +} diff --git a/src/main/java/lotto/service/PrizeService.java b/src/main/java/lotto/service/PrizeService.java new file mode 100644 index 00000000000..eaf089f5eb0 --- /dev/null +++ b/src/main/java/lotto/service/PrizeService.java @@ -0,0 +1,14 @@ +package lotto.service; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class PrizeService { + public static List parsingNumbers(String input) { + return Arrays.stream(input.split(",")) + .map(String::trim) + .map(Integer::parseInt) + .collect(Collectors.toList()); + } +} From 59b432ae25f00e7b0bd0a52e920d497ecd34c1a8 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Sat, 28 Sep 2024 16:26:38 +0900 Subject: [PATCH 11/16] =?UTF-8?q?feat:=20=ED=86=B5=EA=B3=84=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/controller/MainController.java | 1 + .../lotto/controller/PrizeController.java | 2 + .../controller/StatisticsController.java | 25 +++++ src/main/java/lotto/domain/Buyer.java | 4 + src/main/java/lotto/domain/Lotto.java | 4 +- src/main/java/lotto/domain/Lottos.java | 4 + src/main/java/lotto/domain/Prize.java | 9 ++ src/main/java/lotto/domain/Statistics.java | 25 +++++ .../java/lotto/global/config/LottoConfig.java | 4 +- .../java/lotto/global/config/PrizeConfig.java | 40 +++++-- .../java/lotto/service/StatisticsService.java | 104 ++++++++++++++++++ .../java/lotto/view/output/OutputMessage.java | 5 +- .../java/lotto/view/output/OutputView.java | 6 + 13 files changed, 219 insertions(+), 14 deletions(-) create mode 100644 src/main/java/lotto/controller/StatisticsController.java create mode 100644 src/main/java/lotto/domain/Statistics.java create mode 100644 src/main/java/lotto/service/StatisticsService.java diff --git a/src/main/java/lotto/controller/MainController.java b/src/main/java/lotto/controller/MainController.java index 692d4e60dc9..732c3224f34 100644 --- a/src/main/java/lotto/controller/MainController.java +++ b/src/main/java/lotto/controller/MainController.java @@ -13,5 +13,6 @@ public static void start() { Prize prize = PrizeController.setWinningNumber(); + StatisticsController.getStatistics(lottos, prize, buyer); } } diff --git a/src/main/java/lotto/controller/PrizeController.java b/src/main/java/lotto/controller/PrizeController.java index 2fa3471a229..a6b95f2e8ae 100644 --- a/src/main/java/lotto/controller/PrizeController.java +++ b/src/main/java/lotto/controller/PrizeController.java @@ -18,6 +18,8 @@ public static Prize setWinningNumber() { int bonusNumber = setBonusNumber(); + PrintNewLine(); + return Prize.createPrize(numbers, bonusNumber); } diff --git a/src/main/java/lotto/controller/StatisticsController.java b/src/main/java/lotto/controller/StatisticsController.java new file mode 100644 index 00000000000..1c3239f891d --- /dev/null +++ b/src/main/java/lotto/controller/StatisticsController.java @@ -0,0 +1,25 @@ +package lotto.controller; + +import lotto.domain.Buyer; +import lotto.domain.Lottos; +import lotto.domain.Prize; +import lotto.domain.Statistics; +import lotto.service.StatisticsService; + +import java.util.List; + +import static lotto.view.output.OutputMessage.WINNING_STATISTICS_MESSAGE; +import static lotto.view.output.OutputView.PrintStaticMessage; +import static lotto.view.output.OutputView.PrintStatisticsMessage; + +public class StatisticsController { + public static void getStatistics(Lottos lottos, Prize prize, Buyer buyer) { + PrintStaticMessage(WINNING_STATISTICS_MESSAGE); + + Statistics statistics = StatisticsService.computeStatistics(lottos, prize, buyer); + + List result = StatisticsService.createStatisticsString(statistics); + + PrintStatisticsMessage(result); + } +} diff --git a/src/main/java/lotto/domain/Buyer.java b/src/main/java/lotto/domain/Buyer.java index 66a8e3f72e9..e01fd603782 100644 --- a/src/main/java/lotto/domain/Buyer.java +++ b/src/main/java/lotto/domain/Buyer.java @@ -19,4 +19,8 @@ public static Buyer createBuyer(int purchaseCost) { private int getPurchaseCount(int payment) { return payment / LOTTO_COST.getValue(); } + + public int getPurchaseCost() { + return purchaseCost; + } } diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 366657953a2..e58cf2275e3 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -16,5 +16,7 @@ private void validate(List numbers) { } } - // TODO: 추가 기능 구현 + public List getNumbers() { + return numbers; + } } diff --git a/src/main/java/lotto/domain/Lottos.java b/src/main/java/lotto/domain/Lottos.java index 76026da3d8d..d5bb78b09e3 100644 --- a/src/main/java/lotto/domain/Lottos.java +++ b/src/main/java/lotto/domain/Lottos.java @@ -12,4 +12,8 @@ private Lottos(List lottos) { public static Lottos createLottos(List lottos) { return new Lottos(lottos); } + + public List getLottos() { + return lottos; + } } diff --git a/src/main/java/lotto/domain/Prize.java b/src/main/java/lotto/domain/Prize.java index 4666201aa22..9f22387f377 100644 --- a/src/main/java/lotto/domain/Prize.java +++ b/src/main/java/lotto/domain/Prize.java @@ -14,4 +14,13 @@ private Prize(List numbers, int bonusNumber) { public static Prize createPrize(List numbers, int bonusNumber) { return new Prize(numbers, bonusNumber); } + + public List getNumbers() { + return numbers; + } + + public int getBonusNumber() { + return bonusNumber; + } + } diff --git a/src/main/java/lotto/domain/Statistics.java b/src/main/java/lotto/domain/Statistics.java new file mode 100644 index 00000000000..17bc44dc0cf --- /dev/null +++ b/src/main/java/lotto/domain/Statistics.java @@ -0,0 +1,25 @@ +package lotto.domain; + +import java.util.List; + +public class Statistics { + private static List rank; + private static float profitability; + + private Statistics(List rank, float profitability) { + this.rank = rank; + this.profitability = profitability; + } + + public static Statistics createStatistics(List rank, float profitability) { + return new Statistics(rank, profitability); + } + + public static List getRank() { + return rank; + } + + public static float getProfitability() { + return profitability; + } +} diff --git a/src/main/java/lotto/global/config/LottoConfig.java b/src/main/java/lotto/global/config/LottoConfig.java index 020265ebebd..0d6101d8782 100644 --- a/src/main/java/lotto/global/config/LottoConfig.java +++ b/src/main/java/lotto/global/config/LottoConfig.java @@ -7,7 +7,9 @@ public enum LottoConfig { MAX_LOTTO_NUM(45), NUMBER_OF_PICKS(6), - NUMBER_OF_BONUS(1); + NUMBER_OF_BONUS(1), + + NUMBER_OF_WINNER(5); public int getValue() { return value; diff --git a/src/main/java/lotto/global/config/PrizeConfig.java b/src/main/java/lotto/global/config/PrizeConfig.java index 7d214d48737..6eebf3f428c 100644 --- a/src/main/java/lotto/global/config/PrizeConfig.java +++ b/src/main/java/lotto/global/config/PrizeConfig.java @@ -1,17 +1,39 @@ package lotto.global.config; public enum PrizeConfig { - NUMBER_OF_WINNER(5), + FIRST_WINNING_PRIZE(1, 6,2000000000), + SECOND_WINNING_PRIZE(2,5,30000000), + THIRD_WINNING_PRIZE(3,5,1500000), + FOURTH_WINNING_PRIZE(4,4,50000), + FIFTH_WINNING_PRIZE(5,3,5000), + NON_WINNING(-1,-1,0); - FIRST_WINNING_PRIZE(2000000000), - SECOND_WINNING_PRIZE(30000000), - THIRD_WINNING_PRIZE(1500000), - FOURTH_WINNING_PRIZE(50000), - FIFTH_WINNING_PRIZE(5000); + private final int price; + private final int rank; - private final int value; + private final int count; - PrizeConfig(int value) { - this.value = value; + public static int getPriceByRank(int rank) { + for (PrizeConfig prizeConfig : PrizeConfig.values()) { + if (prizeConfig.rank == rank) { + return prizeConfig.price; + } + } + return -1; + } + + public static int getCountByRank(int rank) { + for (PrizeConfig prizeConfig : PrizeConfig.values()) { + if (prizeConfig.rank == rank) { + return prizeConfig.count; + } + } + return -1; + } + + PrizeConfig(int rank, int count, int price) { + this.rank = rank; + this.count = count; + this.price = price; } } diff --git a/src/main/java/lotto/service/StatisticsService.java b/src/main/java/lotto/service/StatisticsService.java new file mode 100644 index 00000000000..4791612eb42 --- /dev/null +++ b/src/main/java/lotto/service/StatisticsService.java @@ -0,0 +1,104 @@ +package lotto.service; + +import lotto.domain.*; +import lotto.global.config.PrizeConfig; + +import java.util.ArrayList; +import java.util.List; + +import static java.lang.String.format; +import static lotto.global.config.LottoConfig.NUMBER_OF_WINNER; +import static lotto.view.output.OutputMessage.PROFITABILITY_MESSAGE; +import static lotto.view.output.OutputMessage.WINNING_STATISTICS_DETAILS_MESSAGE; + +public class StatisticsService { + public static List createStatisticsString(Statistics statistics) { + List result = new ArrayList<>(); + List rank = statistics.getRank(); + float profitability = statistics.getProfitability(); + + for(int i=NUMBER_OF_WINNER.getValue();i>=1;i--) { + int matchCount = PrizeConfig.getCountByRank(i); + int winningCount = getWinningCount(rank, i); + String price = convertMoneyFormat(i); + String message = format(WINNING_STATISTICS_DETAILS_MESSAGE.getMessage(), matchCount, price, winningCount); + result.add(message); + } + + String profitabilityMessage = format(PROFITABILITY_MESSAGE.getMessage(), profitability); + result.add(profitabilityMessage); + + return result; + } + + public static Statistics computeStatistics(Lottos lottos, Prize prize, Buyer buyer) { + List rank = new ArrayList<>(); + float profitability; + + List lottoList = lottos.getLottos(); + + for (Lotto lotto : lottoList) { + + List numbers = lotto.getNumbers(); + + int count = countMatches(numbers, prize); + boolean bonus = isBonusNumberMatched(numbers, prize); + rank.add(findRank(count, bonus)); + } + + profitability = calculateProfitability(buyer, rank); + + return Statistics.createStatistics(rank, profitability); + } + + private static String convertMoneyFormat(int rank) { + int price = PrizeConfig.getPriceByRank(rank); + String str = String.format("%,d", price); + + if(rank == 2) return String.format(", 보너스 볼 일치 (%s원)", str); + return String.format(" (%s원)", str); + } + + private static int getWinningCount(List rankList, int rank) { + return (int) rankList.stream() + .filter(num -> num == rank) + .count(); + } + + private static float calculateProfitability(Buyer buyer, List rank) { + int purchaseCost = buyer.getPurchaseCost(); + int totalProfit = getTotalProfit(rank); + + float profitability = (float) totalProfit / purchaseCost * 100; + + return profitability; + } + + private static int getTotalProfit(List rank) { + return rank.stream().mapToInt(i -> PrizeConfig.getPriceByRank(i)).sum(); + } + + private static int findRank(int count, boolean bonus) { + if (count == 6) return 1; + if (count == 5 && bonus) return 2; + if (count == 5) return 3; + if (count == 4) return 4; + if (count == 3) return 5; + return -1; + } + + private static int countMatches(List numbers, Prize prize) { + List winningNumbers = prize.getNumbers(); + + return (int) numbers.stream() + .filter(winningNumbers::contains) + .count(); + } + + private static boolean isBonusNumberMatched(List numbers, Prize prize) { + int bonusNumber = prize.getBonusNumber(); + + return numbers.stream() + .anyMatch(number -> number == bonusNumber); + } +} diff --git a/src/main/java/lotto/view/output/OutputMessage.java b/src/main/java/lotto/view/output/OutputMessage.java index d607c1d2d29..e576413143c 100644 --- a/src/main/java/lotto/view/output/OutputMessage.java +++ b/src/main/java/lotto/view/output/OutputMessage.java @@ -6,9 +6,8 @@ public enum OutputMessage { ASK_WINNING_NUMBER_MESSAGE("당첨 번호를 입력해 주세요."), ASK_BONUS_NUMBER_MESSAGE("보너스 번호를 입력해 주세요."), WINNING_STATISTICS_MESSAGE("당첨 통계\n---"), - WINNING_STATISTICS_DETAILS_MESSAGE("%d개 일치 (%s원) - %d개"), - SECOND_WINNING_DETAILS_MESSAGE("%d개 일치, 보너스 볼 일치 (%s원) - %d개"), - RATE_OF_RETURN_MESSAGE("총 수익률은 %f%입니다."); + WINNING_STATISTICS_DETAILS_MESSAGE("%d개 일치%s - %d개"), + PROFITABILITY_MESSAGE("총 수익률은 %.1f%%입니다."); private final String message; diff --git a/src/main/java/lotto/view/output/OutputView.java b/src/main/java/lotto/view/output/OutputView.java index 66ca1cab80e..42cf36f8f22 100644 --- a/src/main/java/lotto/view/output/OutputView.java +++ b/src/main/java/lotto/view/output/OutputView.java @@ -19,4 +19,10 @@ public static void PrintLottoNumbers(List numbers) { System.out.println(numbers); } + public static void PrintStatisticsMessage(List messages) { + for(String message : messages) { + System.out.println(message); + } + } + } From bcd1241c081dcb623e6e768b13c37c8255777369 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Sat, 28 Sep 2024 16:55:25 +0900 Subject: [PATCH 12/16] =?UTF-8?q?feat:=20LottoException,=20ErrorMessage=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/global/config/PrizeConfig.java | 1 - .../lotto/global/exception/ErrorMessage.java | 24 +++++++++++++++++++ .../global/exception/LottoException.java | 9 +++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/main/java/lotto/global/exception/ErrorMessage.java create mode 100644 src/main/java/lotto/global/exception/LottoException.java diff --git a/src/main/java/lotto/global/config/PrizeConfig.java b/src/main/java/lotto/global/config/PrizeConfig.java index 6eebf3f428c..0925cd2f477 100644 --- a/src/main/java/lotto/global/config/PrizeConfig.java +++ b/src/main/java/lotto/global/config/PrizeConfig.java @@ -10,7 +10,6 @@ public enum PrizeConfig { private final int price; private final int rank; - private final int count; public static int getPriceByRank(int rank) { diff --git a/src/main/java/lotto/global/exception/ErrorMessage.java b/src/main/java/lotto/global/exception/ErrorMessage.java new file mode 100644 index 00000000000..7543199899f --- /dev/null +++ b/src/main/java/lotto/global/exception/ErrorMessage.java @@ -0,0 +1,24 @@ +package lotto.global.exception; + +public enum ErrorMessage { + PRICE_ERROR_MESSAGE("구입 금액은 로또 1장 가격(%d)으로 나누어 떨어져야합니다."), + + INVALID_WINNING_NUMBER_MESSAGE("로또 번호는 1부터 45 사이의 숫자여야 합니다."), + NON_DIGIT_ERROR_MESSAGE("로또 번호는 숫자만 입력할 수 있습니다."), + + DELIMITER_ERROR_MESSAGE("당첨 번호는 '',''로 구분되어 입력되어야 합니다."), + INVALID_PICK_NUMBER_MESSAGE("%d개의 숫자만 입력할 수 있습니다."), + DUPLICATED_MESSAGE("중복된 당첨 번호는 입력할 수 없습니다."), + + DUPLICATED_BONUS_NUMBER_MESSAGE("당첨 번호와 중복되는 보너스 번호는 입력할 수 없습니다."), + INVALID_PICK_BONUS_MESSAGE("보너스 번호는 1개만 입력할 수 있습니다."); + + + + + private final String message; + + ErrorMessage(String message) { + this.message = message; + } +} diff --git a/src/main/java/lotto/global/exception/LottoException.java b/src/main/java/lotto/global/exception/LottoException.java new file mode 100644 index 00000000000..bd7f4bd9e0a --- /dev/null +++ b/src/main/java/lotto/global/exception/LottoException.java @@ -0,0 +1,9 @@ +package lotto.global.exception; + +public class LottoException extends IllegalArgumentException { + private static final String PREFIX = "[ERROR]"; + + public LottoException(String message) { + super(String.format("%s %s", PREFIX, message)); + } +} From 9d462a2d2d1d0076da185cb27d0d98939a94527a Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Sun, 29 Sep 2024 14:25:48 +0900 Subject: [PATCH 13/16] =?UTF-8?q?feat:=20Buyer=20exception=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/controller/BuyerController.java | 35 +++++++++++++++---- .../java/lotto/controller/MainController.java | 5 ++- src/main/java/lotto/domain/Buyer.java | 8 ++--- .../lotto/global/exception/ErrorMessage.java | 18 ++++++---- .../global/exception/LottoException.java | 4 +-- .../global/handler/ExceptionHandler.java | 20 +++++++++++ .../java/lotto/view/output/OutputView.java | 4 +++ 7 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 src/main/java/lotto/global/handler/ExceptionHandler.java diff --git a/src/main/java/lotto/controller/BuyerController.java b/src/main/java/lotto/controller/BuyerController.java index c151487068a..3ef7ca1e274 100644 --- a/src/main/java/lotto/controller/BuyerController.java +++ b/src/main/java/lotto/controller/BuyerController.java @@ -1,26 +1,47 @@ package lotto.controller; import lotto.domain.Buyer; +import lotto.global.exception.LottoException; +import lotto.global.handler.ExceptionHandler; import lotto.view.input.InputView; import static java.lang.String.format; +import static lotto.global.config.LottoConfig.LOTTO_COST; +import static lotto.global.exception.ErrorMessage.COST_ERROR_MESSAGE; +import static lotto.global.exception.ErrorMessage.NON_DIGIT_COST_MESSAGE; import static lotto.view.output.OutputMessage.ASK_PURCHASE_PRICE_MESSAGE; import static lotto.view.output.OutputMessage.PURCHASE_INFORMATION_MESSAGE; import static lotto.view.output.OutputView.*; public class BuyerController { - public static Buyer payment() { + public static Buyer requestCost() { PrintStaticMessage(ASK_PURCHASE_PRICE_MESSAGE); - int cost = Integer.parseInt(InputView.Input()); - return Buyer.createBuyer(cost); + return ExceptionHandler.execute(BuyerController::purchase); } - public static int purchase(Buyer buyer) { - int purchaseCount = buyer.purchaseCount; + + private static Buyer purchase() { + String input = InputView.Input(); + int cost; + + if(!isDigit(input)) throw new LottoException(NON_DIGIT_COST_MESSAGE); + + cost = Integer.parseInt(input); + + if (!isValidCost(cost)) throw new LottoException(COST_ERROR_MESSAGE); PrintNewLine(); - PrintDynamicMessage(format(PURCHASE_INFORMATION_MESSAGE.getMessage(), purchaseCount)); + PrintDynamicMessage(format(PURCHASE_INFORMATION_MESSAGE.getMessage(), cost / LOTTO_COST.getValue())); - return purchaseCount; + return Buyer.createBuyer(cost); } + + private static boolean isValidCost(int cost) { + return cost % LOTTO_COST.getValue() == 0; + } + + private static boolean isDigit(String input) { + return input.matches("[0-9]*"); + } + } diff --git a/src/main/java/lotto/controller/MainController.java b/src/main/java/lotto/controller/MainController.java index 732c3224f34..20b01403d7e 100644 --- a/src/main/java/lotto/controller/MainController.java +++ b/src/main/java/lotto/controller/MainController.java @@ -6,10 +6,9 @@ public class MainController { public static void start() { - Buyer buyer = BuyerController.payment(); - int purchaseCount = BuyerController.purchase(buyer); + Buyer buyer = BuyerController.requestCost(); - Lottos lottos = LottoController.generateLottos(purchaseCount); + Lottos lottos = LottoController.generateLottos(buyer.purchaseCount); Prize prize = PrizeController.setWinningNumber(); diff --git a/src/main/java/lotto/domain/Buyer.java b/src/main/java/lotto/domain/Buyer.java index e01fd603782..ed2c86be507 100644 --- a/src/main/java/lotto/domain/Buyer.java +++ b/src/main/java/lotto/domain/Buyer.java @@ -16,11 +16,11 @@ public static Buyer createBuyer(int purchaseCost) { return new Buyer(purchaseCost); } - private int getPurchaseCount(int payment) { - return payment / LOTTO_COST.getValue(); - } - public int getPurchaseCost() { return purchaseCost; } + + private int getPurchaseCount(int payment) { + return payment / LOTTO_COST.getValue(); + } } diff --git a/src/main/java/lotto/global/exception/ErrorMessage.java b/src/main/java/lotto/global/exception/ErrorMessage.java index 7543199899f..90c9a61fc73 100644 --- a/src/main/java/lotto/global/exception/ErrorMessage.java +++ b/src/main/java/lotto/global/exception/ErrorMessage.java @@ -1,20 +1,26 @@ package lotto.global.exception; +import static java.lang.String.format; +import static lotto.global.config.LottoConfig.*; + public enum ErrorMessage { - PRICE_ERROR_MESSAGE("구입 금액은 로또 1장 가격(%d)으로 나누어 떨어져야합니다."), + COST_ERROR_MESSAGE(format("구입 금액은 로또 1장 가격(%d)으로 나누어 떨어져야합니다.", LOTTO_COST.getValue())), + NON_DIGIT_COST_MESSAGE("구입 금액은 숫자만 입력할 수 있습니다."), INVALID_WINNING_NUMBER_MESSAGE("로또 번호는 1부터 45 사이의 숫자여야 합니다."), NON_DIGIT_ERROR_MESSAGE("로또 번호는 숫자만 입력할 수 있습니다."), - DELIMITER_ERROR_MESSAGE("당첨 번호는 '',''로 구분되어 입력되어야 합니다."), - INVALID_PICK_NUMBER_MESSAGE("%d개의 숫자만 입력할 수 있습니다."), + INVALID_DELIMITER_MESSAGE("당첨 번호는 '',''로 구분되어 입력되어야 합니다."), + DELIMITER_ERROR_MESSAGE("구분자('','')는 숫자 사이에 여러번 입력할 수 없습니다."), + INVALID_PICK_NUMBER_MESSAGE(format("%d개의 숫자만 입력할 수 있습니다.", NUMBER_OF_PICKS.getValue())), DUPLICATED_MESSAGE("중복된 당첨 번호는 입력할 수 없습니다."), DUPLICATED_BONUS_NUMBER_MESSAGE("당첨 번호와 중복되는 보너스 번호는 입력할 수 없습니다."), - INVALID_PICK_BONUS_MESSAGE("보너스 번호는 1개만 입력할 수 있습니다."); - - + INVALID_PICK_BONUS_MESSAGE(format("보너스 번호는 %d개만 입력할 수 있습니다.", NUMBER_OF_BONUS.getValue())); + public String getMessage() { + return message; + } private final String message; diff --git a/src/main/java/lotto/global/exception/LottoException.java b/src/main/java/lotto/global/exception/LottoException.java index bd7f4bd9e0a..004a50a7876 100644 --- a/src/main/java/lotto/global/exception/LottoException.java +++ b/src/main/java/lotto/global/exception/LottoException.java @@ -3,7 +3,7 @@ public class LottoException extends IllegalArgumentException { private static final String PREFIX = "[ERROR]"; - public LottoException(String message) { - super(String.format("%s %s", PREFIX, message)); + public LottoException(ErrorMessage message) { + super(String.format("%s %s", PREFIX, message.getMessage())); } } diff --git a/src/main/java/lotto/global/handler/ExceptionHandler.java b/src/main/java/lotto/global/handler/ExceptionHandler.java new file mode 100644 index 00000000000..66aeac43c66 --- /dev/null +++ b/src/main/java/lotto/global/handler/ExceptionHandler.java @@ -0,0 +1,20 @@ +package lotto.global.handler; + +import lotto.global.exception.LottoException; + +import java.util.function.Supplier; + +import static lotto.view.output.OutputView.PrintErrorMessage; + +public class ExceptionHandler { + public static T execute(Supplier supplier) { + while (true) { + try { + return supplier.get(); + } catch (LottoException exception) { + PrintErrorMessage(exception.getMessage()); + } + } + } + +} diff --git a/src/main/java/lotto/view/output/OutputView.java b/src/main/java/lotto/view/output/OutputView.java index 42cf36f8f22..a94bfbc60a3 100644 --- a/src/main/java/lotto/view/output/OutputView.java +++ b/src/main/java/lotto/view/output/OutputView.java @@ -25,4 +25,8 @@ public static void PrintStatisticsMessage(List messages) { } } + public static void PrintErrorMessage(String message) { + System.out.println(message); + } + } From 1cb9cf880aab6aa69cdb2363ac7b4e8a7301d999 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Sun, 29 Sep 2024 14:48:50 +0900 Subject: [PATCH 14/16] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=20=ED=8E=B8=EC=9D=98=EC=84=B1=20=EA=B3=A0=EB=A0=A4=ED=95=98?= =?UTF-8?q?=EC=97=AC=20error=EC=8B=9C=EC=97=90=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=20=EB=A9=94=EC=84=B8=EC=A7=80=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=ED=95=98=EB=8F=84=EB=A1=9D=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/lotto/controller/BuyerController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/controller/BuyerController.java b/src/main/java/lotto/controller/BuyerController.java index 3ef7ca1e274..d22b46232c0 100644 --- a/src/main/java/lotto/controller/BuyerController.java +++ b/src/main/java/lotto/controller/BuyerController.java @@ -15,12 +15,12 @@ public class BuyerController { public static Buyer requestCost() { - PrintStaticMessage(ASK_PURCHASE_PRICE_MESSAGE); - return ExceptionHandler.execute(BuyerController::purchase); } private static Buyer purchase() { + PrintStaticMessage(ASK_PURCHASE_PRICE_MESSAGE); + String input = InputView.Input(); int cost; From 7be29933836aabd323e4aa867b9d5aecbf5bc961 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Sun, 29 Sep 2024 15:34:51 +0900 Subject: [PATCH 15/16] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EC=8B=9C=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/controller/MainController.java | 2 +- .../lotto/controller/PrizeController.java | 12 ++++- .../lotto/global/exception/ErrorMessage.java | 6 +-- src/main/java/lotto/service/PrizeService.java | 46 ++++++++++++++++++- .../java/lotto/view/output/OutputView.java | 1 + 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/main/java/lotto/controller/MainController.java b/src/main/java/lotto/controller/MainController.java index 20b01403d7e..eaf89a657e1 100644 --- a/src/main/java/lotto/controller/MainController.java +++ b/src/main/java/lotto/controller/MainController.java @@ -10,7 +10,7 @@ public static void start() { Lottos lottos = LottoController.generateLottos(buyer.purchaseCount); - Prize prize = PrizeController.setWinningNumber(); + Prize prize = PrizeController.pickWinningNumber(); StatisticsController.getStatistics(lottos, prize, buyer); } diff --git a/src/main/java/lotto/controller/PrizeController.java b/src/main/java/lotto/controller/PrizeController.java index a6b95f2e8ae..27fc91beec1 100644 --- a/src/main/java/lotto/controller/PrizeController.java +++ b/src/main/java/lotto/controller/PrizeController.java @@ -1,6 +1,7 @@ package lotto.controller; import lotto.domain.Prize; +import lotto.global.handler.ExceptionHandler; import lotto.service.PrizeService; import lotto.view.input.InputView; @@ -12,7 +13,11 @@ import static lotto.view.output.OutputView.PrintStaticMessage; public class PrizeController { - public static Prize setWinningNumber() { + public static Prize pickWinningNumber() { + return ExceptionHandler.execute(PrizeController::setWinningNumber); + } + + private static Prize setWinningNumber() { PrintStaticMessage(ASK_WINNING_NUMBER_MESSAGE); List numbers = PrizeService.parsingNumbers(InputView.Input()); @@ -24,9 +29,12 @@ public static Prize setWinningNumber() { } private static int setBonusNumber() { - PrintNewLine(); + String input; + PrintNewLine(); PrintStaticMessage(ASK_BONUS_NUMBER_MESSAGE); + + input = InputView.Input(); return Integer.parseInt(InputView.Input()); } } diff --git a/src/main/java/lotto/global/exception/ErrorMessage.java b/src/main/java/lotto/global/exception/ErrorMessage.java index 90c9a61fc73..c0eb5183495 100644 --- a/src/main/java/lotto/global/exception/ErrorMessage.java +++ b/src/main/java/lotto/global/exception/ErrorMessage.java @@ -10,9 +10,9 @@ public enum ErrorMessage { INVALID_WINNING_NUMBER_MESSAGE("로또 번호는 1부터 45 사이의 숫자여야 합니다."), NON_DIGIT_ERROR_MESSAGE("로또 번호는 숫자만 입력할 수 있습니다."), - INVALID_DELIMITER_MESSAGE("당첨 번호는 '',''로 구분되어 입력되어야 합니다."), - DELIMITER_ERROR_MESSAGE("구분자('','')는 숫자 사이에 여러번 입력할 수 없습니다."), - INVALID_PICK_NUMBER_MESSAGE(format("%d개의 숫자만 입력할 수 있습니다.", NUMBER_OF_PICKS.getValue())), + EMPTY_CONTAINS_ERROR_MESSAGE("공백 문자 없이 입력되어야 합니다."), + INVALID_DELIMITER_MESSAGE("당첨 번호는 ','로 구분되어야 합니다."), + INVALID_PICK_NUMBER_MESSAGE(format("총 %d개의 숫자가 입력되어야 합니다.", NUMBER_OF_PICKS.getValue())), DUPLICATED_MESSAGE("중복된 당첨 번호는 입력할 수 없습니다."), DUPLICATED_BONUS_NUMBER_MESSAGE("당첨 번호와 중복되는 보너스 번호는 입력할 수 없습니다."), diff --git a/src/main/java/lotto/service/PrizeService.java b/src/main/java/lotto/service/PrizeService.java index eaf089f5eb0..c1227d1440d 100644 --- a/src/main/java/lotto/service/PrizeService.java +++ b/src/main/java/lotto/service/PrizeService.java @@ -1,14 +1,56 @@ package lotto.service; +import lotto.global.exception.LottoException; + import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import static lotto.global.config.LottoConfig.NUMBER_OF_PICKS; +import static lotto.global.exception.ErrorMessage.*; + public class PrizeService { public static List parsingNumbers(String input) { - return Arrays.stream(input.split(",")) - .map(String::trim) + if (isContainedEmpty(input)) throw new LottoException(EMPTY_CONTAINS_ERROR_MESSAGE); + if (isContainedLetter(input)) throw new LottoException(NON_DIGIT_ERROR_MESSAGE); + if (!isValidDelimiter(input)) throw new LottoException(INVALID_DELIMITER_MESSAGE); + + List numbers = Arrays.stream(input.split(",")) .map(Integer::parseInt) + .map(num -> { + if(!isValidNumber(num)) throw new LottoException(INVALID_WINNING_NUMBER_MESSAGE); + return num; + }) .collect(Collectors.toList()); + + if (!isValidPicks(numbers.size())) throw new LottoException(INVALID_PICK_NUMBER_MESSAGE); + if (isDuplicated(numbers)) throw new LottoException(DUPLICATED_MESSAGE); + + return numbers; + } + + private static boolean isContainedEmpty(String input) { + return input.contains(" "); + } + + private static boolean isContainedLetter(String input) { + return input.matches(".*[a-zA-Z]+.*"); } + + private static boolean isValidDelimiter(String input) { + return input.matches("^[0-9]+(,[0-9]+)*$"); + } + + private static boolean isValidNumber(int number) { + return number >= 1 && number <= 45; + } + + private static boolean isValidPicks(int picks) { + return picks == NUMBER_OF_PICKS.getValue(); + } + + private static boolean isDuplicated(List numbers) { + return numbers.stream().distinct().count() != NUMBER_OF_PICKS.getValue(); + } + } diff --git a/src/main/java/lotto/view/output/OutputView.java b/src/main/java/lotto/view/output/OutputView.java index a94bfbc60a3..e15a7e719a1 100644 --- a/src/main/java/lotto/view/output/OutputView.java +++ b/src/main/java/lotto/view/output/OutputView.java @@ -27,6 +27,7 @@ public static void PrintStatisticsMessage(List messages) { public static void PrintErrorMessage(String message) { System.out.println(message); + PrintNewLine(); } } From 31e4f95b7c52a307801729a5ce554a6561561524 Mon Sep 17 00:00:00 2001 From: StoneCAU Date: Sun, 29 Sep 2024 22:04:53 +0900 Subject: [PATCH 16/16] =?UTF-8?q?feat:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/controller/PrizeController.java | 16 +++++++++------- .../lotto/global/exception/ErrorMessage.java | 3 +-- src/main/java/lotto/service/PrizeService.java | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main/java/lotto/controller/PrizeController.java b/src/main/java/lotto/controller/PrizeController.java index 27fc91beec1..f4abe6e0d3c 100644 --- a/src/main/java/lotto/controller/PrizeController.java +++ b/src/main/java/lotto/controller/PrizeController.java @@ -18,23 +18,25 @@ public static Prize pickWinningNumber() { } private static Prize setWinningNumber() { - PrintStaticMessage(ASK_WINNING_NUMBER_MESSAGE); - List numbers = PrizeService.parsingNumbers(InputView.Input()); + List numbers; - int bonusNumber = setBonusNumber(); + PrintStaticMessage(ASK_WINNING_NUMBER_MESSAGE); + numbers = PrizeService.parsingNumbers(InputView.Input()); PrintNewLine(); - return Prize.createPrize(numbers, bonusNumber); + return ExceptionHandler.execute(() -> setBonusNumber(numbers)); } - private static int setBonusNumber() { + private static Prize setBonusNumber(List numbers) { String input; + int bonusNumber; - PrintNewLine(); PrintStaticMessage(ASK_BONUS_NUMBER_MESSAGE); input = InputView.Input(); - return Integer.parseInt(InputView.Input()); + bonusNumber = PrizeService.validateBonusNumber(numbers, input); + + return Prize.createPrize(numbers, bonusNumber); } } diff --git a/src/main/java/lotto/global/exception/ErrorMessage.java b/src/main/java/lotto/global/exception/ErrorMessage.java index c0eb5183495..31710311e20 100644 --- a/src/main/java/lotto/global/exception/ErrorMessage.java +++ b/src/main/java/lotto/global/exception/ErrorMessage.java @@ -15,8 +15,7 @@ public enum ErrorMessage { INVALID_PICK_NUMBER_MESSAGE(format("총 %d개의 숫자가 입력되어야 합니다.", NUMBER_OF_PICKS.getValue())), DUPLICATED_MESSAGE("중복된 당첨 번호는 입력할 수 없습니다."), - DUPLICATED_BONUS_NUMBER_MESSAGE("당첨 번호와 중복되는 보너스 번호는 입력할 수 없습니다."), - INVALID_PICK_BONUS_MESSAGE(format("보너스 번호는 %d개만 입력할 수 있습니다.", NUMBER_OF_BONUS.getValue())); + DUPLICATED_BONUS_NUMBER_MESSAGE("당첨 번호와 중복되는 보너스 번호는 입력할 수 없습니다."); public String getMessage() { return message; diff --git a/src/main/java/lotto/service/PrizeService.java b/src/main/java/lotto/service/PrizeService.java index c1227d1440d..60b1e525557 100644 --- a/src/main/java/lotto/service/PrizeService.java +++ b/src/main/java/lotto/service/PrizeService.java @@ -29,6 +29,25 @@ public static List parsingNumbers(String input) { return numbers; } + public static int validateBonusNumber(List winningNumbers, String input) { + int bonusNumber; + + if (isContainedLetter(input)) throw new LottoException(NON_DIGIT_ERROR_MESSAGE); + if (isContainedEmpty(input)) throw new LottoException(EMPTY_CONTAINS_ERROR_MESSAGE); + + bonusNumber = Integer.parseInt(input); + + if (!isValidNumber(bonusNumber)) throw new LottoException(INVALID_WINNING_NUMBER_MESSAGE); + if (isDuplicatedBonus(winningNumbers, bonusNumber)) throw new LottoException(DUPLICATED_BONUS_NUMBER_MESSAGE); + + return bonusNumber; + } + + private static boolean isDuplicatedBonus(List winningNumbers, int bonusNumber) { + return winningNumbers.stream() + .anyMatch(winningNumber -> winningNumber.equals(bonusNumber)); + } + private static boolean isContainedEmpty(String input) { return input.contains(" "); }