From 6e33a4e5950db99a2a4b8998974eb90726d0673c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 15:34:17 +0900 Subject: [PATCH 01/26] =?UTF-8?q?docs(README):=20=EB=A6=AC=EB=93=9C?= =?UTF-8?q?=EB=AF=B8=EC=97=90=20=EA=B8=B0=EB=8A=A5=20=EB=B0=8F=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EB=AA=A9=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 리드미 컨텐츠 추가 - 구현할 기능 목록 - 구현할 클래스 목록 - enum 목록 --- docs/README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29bb2..c813885750 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,36 @@ +# 구현할 기능 목록 +1. 구입 금액 입력 기능 +2. 랜덤 번호 생성 및 저장 기능 +3. 로또 발행 입력 기능 +4. 로또 발행 입력값 검증 기능 +5. 로또 발행 기능 +6. 발행한 로또 수량 및 번호 출력 기능 +7. 당첨 번호 입력 기능 +8. 당첨 번호 입력값 검증 기능 +9. 번호 일치 여부 확인 및 당첨 여부 확인 기능 +10. 당첨 내역 출력 기능 +11. 수익률 계산 및 반올림 기능 +12. 수익률 출력 기능 +13. 예외 처리 및 에러 문구 출력 기능 + +# 구현할 클래스 목록 +## LottoTicket - 발행한 로또 티켓 + +## LottoController - 로또 컨트롤러 + +## LottoService - 로또 서비스 + +## LottoRepository - 로또 레포지토리 + +## InputManager - 입력 받기 + +## InputValidator - 입력 검증 + +## OutputManager - 출력하기 + +## ComponentFactory - 컴포넌트들의 생성 + +# Enum 목록 +## LottoMessage - 로또 진행 메시지 + +## ErrorMessage - 에러 메시지 From 95d2b3329562bdb2fd69f6a3ab4d865a02f2d9e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 15:45:55 +0900 Subject: [PATCH 02/26] =?UTF-8?q?feat:=20=EB=82=9C=EC=88=98=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9D=84=20=ED=86=B5=ED=95=9C=20Lotto=20=EA=B0=9D?= =?UTF-8?q?=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 기능 추가 - 난수 생성 - Lotto 객체 생성 --- docs/README.md | 27 ++++++++++++----------- src/main/java/lotto/Application.java | 4 +++- src/main/java/lotto/ComponentFactory.java | 12 ++++++++++ src/main/java/lotto/LottoController.java | 13 +++++++++++ src/main/java/lotto/LottoService.java | 16 ++++++++++++++ 5 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 src/main/java/lotto/ComponentFactory.java create mode 100644 src/main/java/lotto/LottoController.java create mode 100644 src/main/java/lotto/LottoService.java diff --git a/docs/README.md b/docs/README.md index c813885750..7481e180e0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,24 +1,25 @@ # 구현할 기능 목록 -1. 구입 금액 입력 기능 -2. 랜덤 번호 생성 및 저장 기능 -3. 로또 발행 입력 기능 -4. 로또 발행 입력값 검증 기능 -5. 로또 발행 기능 -6. 발행한 로또 수량 및 번호 출력 기능 -7. 당첨 번호 입력 기능 -8. 당첨 번호 입력값 검증 기능 -9. 번호 일치 여부 확인 및 당첨 여부 확인 기능 -10. 당첨 내역 출력 기능 -11. 수익률 계산 및 반올림 기능 -12. 수익률 출력 기능 -13. 예외 처리 및 에러 문구 출력 기능 +1. 랜덤 번호 생성 및 저장 기능 +2. 구입 금액 입력 기능 +3. 구입 금액 입력값 검증 기능 +4. 로또 발행 기능 +5. 발행한 로또 수량 및 번호 출력 기능 +6. 당첨 번호 입력 기능 +7. 당첨 번호 입력값 검증 기능 +8. 번호 일치 여부 확인 및 당첨 여부 확인 기능 +9. 당첨 내역 출력 기능 +10. 수익률 계산 및 반올림 기능 +11. 수익률 출력 기능 +12. 예외 처리 및 에러 문구 출력 기능 # 구현할 클래스 목록 ## LottoTicket - 발행한 로또 티켓 ## LottoController - 로또 컨트롤러 +- startLotto() 로또 시작 ## LottoService - 로또 서비스 +- saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 ## LottoRepository - 로또 레포지토리 diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..c9d396717f 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -2,6 +2,8 @@ public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + final ComponentFactory componentFactory = new ComponentFactory(); + final LottoController lottoController = componentFactory.lottoController(); + lottoController.startLotto(); } } diff --git a/src/main/java/lotto/ComponentFactory.java b/src/main/java/lotto/ComponentFactory.java new file mode 100644 index 0000000000..689e3a3549 --- /dev/null +++ b/src/main/java/lotto/ComponentFactory.java @@ -0,0 +1,12 @@ +package lotto; + +public class ComponentFactory { + + public LottoController lottoController() { + return new LottoController(lottoService()); + } + + private LottoService lottoService() { + return new LottoService(); + } +} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java new file mode 100644 index 0000000000..75b8b4effa --- /dev/null +++ b/src/main/java/lotto/LottoController.java @@ -0,0 +1,13 @@ +package lotto; + +public class LottoController { + private final LottoService lottoService; + + public LottoController(LottoService lottoService) { + this.lottoService = lottoService; + } + + public void startLotto() { + lottoService.saveLotto(); + } +} diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java new file mode 100644 index 0000000000..147aa6e178 --- /dev/null +++ b/src/main/java/lotto/LottoService.java @@ -0,0 +1,16 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.List; + +public class LottoService { + + public void saveLotto() { + final Lotto lotto = new Lotto(createRandomNumbers()); + } + + private List createRandomNumbers() { + return Randoms.pickUniqueNumbersInRange(1, 45, 6); + } +} From 2e8060efe8fb5bff2b3f3ba485f4ccd08bff10ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 15:48:33 +0900 Subject: [PATCH 03/26] =?UTF-8?q?feat(LottoRepository):=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20repository=20=EC=A0=80=EC=9E=A5=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - LottoRepository 생성 - Lotto 객체 저장 기능 --- docs/README.md | 1 + src/main/java/lotto/ComponentFactory.java | 6 +++++- src/main/java/lotto/LottoRepository.java | 9 +++++++++ src/main/java/lotto/LottoService.java | 5 +++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/main/java/lotto/LottoRepository.java diff --git a/docs/README.md b/docs/README.md index 7481e180e0..815dad8436 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,6 +22,7 @@ - saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 ## LottoRepository - 로또 레포지토리 +- saveLotto() Lotto 객체 저장 ## InputManager - 입력 받기 diff --git a/src/main/java/lotto/ComponentFactory.java b/src/main/java/lotto/ComponentFactory.java index 689e3a3549..cf1260f763 100644 --- a/src/main/java/lotto/ComponentFactory.java +++ b/src/main/java/lotto/ComponentFactory.java @@ -7,6 +7,10 @@ public LottoController lottoController() { } private LottoService lottoService() { - return new LottoService(); + return new LottoService(lottoRepository()); + } + + private LottoRepository lottoRepository() { + return new LottoRepository(); } } diff --git a/src/main/java/lotto/LottoRepository.java b/src/main/java/lotto/LottoRepository.java new file mode 100644 index 0000000000..81729b9f78 --- /dev/null +++ b/src/main/java/lotto/LottoRepository.java @@ -0,0 +1,9 @@ +package lotto; + +public class LottoRepository { + private Lotto lotto; + + public void saveLotto(Lotto lotto) { + this.lotto = lotto; + } +} diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 147aa6e178..3a37437a81 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -5,6 +5,11 @@ import java.util.List; public class LottoService { + private final LottoRepository lottoRepository; + + public LottoService(LottoRepository lottoRepository) { + this.lottoRepository = lottoRepository; + } public void saveLotto() { final Lotto lotto = new Lotto(createRandomNumbers()); From 7af6ea45d2304f6f7f052772d8e10ff774945627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 15:56:29 +0900 Subject: [PATCH 04/26] =?UTF-8?q?feat(InputManager):=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=20=EB=8B=B4=EB=8B=B9=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=B0=8F?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B=EB=8A=94=20=ED=95=A8=EC=88=98?= =?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 기능 추가 - InputManager 추가 - 구매 금액 입력 받는 함수 --- docs/README.md | 1 + src/main/java/lotto/ComponentFactory.java | 6 +++++- src/main/java/lotto/InputManager.java | 11 +++++++++++ src/main/java/lotto/LottoController.java | 5 ++++- src/main/java/lotto/LottoRepository.java | 2 +- src/main/java/lotto/LottoService.java | 2 +- 6 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 src/main/java/lotto/InputManager.java diff --git a/docs/README.md b/docs/README.md index 815dad8436..f2b0fcc1cf 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,6 +25,7 @@ - saveLotto() Lotto 객체 저장 ## InputManager - 입력 받기 +- buyAmountInput() 구매 금액 입력 ## InputValidator - 입력 검증 diff --git a/src/main/java/lotto/ComponentFactory.java b/src/main/java/lotto/ComponentFactory.java index cf1260f763..68117f14df 100644 --- a/src/main/java/lotto/ComponentFactory.java +++ b/src/main/java/lotto/ComponentFactory.java @@ -3,7 +3,7 @@ public class ComponentFactory { public LottoController lottoController() { - return new LottoController(lottoService()); + return new LottoController(lottoService(), inputManager()); } private LottoService lottoService() { @@ -13,4 +13,8 @@ private LottoService lottoService() { private LottoRepository lottoRepository() { return new LottoRepository(); } + + private InputManager inputManager() { + return new InputManager(); + } } diff --git a/src/main/java/lotto/InputManager.java b/src/main/java/lotto/InputManager.java new file mode 100644 index 0000000000..38083a7ee3 --- /dev/null +++ b/src/main/java/lotto/InputManager.java @@ -0,0 +1,11 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +public class InputManager { + + public Integer buyAmountInput() { + final String input = Console.readLine(); + return Integer.valueOf(input); + } +} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 75b8b4effa..283bbd2f91 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -2,12 +2,15 @@ public class LottoController { private final LottoService lottoService; + private final InputManager inputManager; - public LottoController(LottoService lottoService) { + public LottoController(final LottoService lottoService, final InputManager inputManager) { this.lottoService = lottoService; + this.inputManager = inputManager; } public void startLotto() { lottoService.saveLotto(); + final Integer buyAmount = inputManager.buyAmountInput(); } } diff --git a/src/main/java/lotto/LottoRepository.java b/src/main/java/lotto/LottoRepository.java index 81729b9f78..6a19fe8768 100644 --- a/src/main/java/lotto/LottoRepository.java +++ b/src/main/java/lotto/LottoRepository.java @@ -3,7 +3,7 @@ public class LottoRepository { private Lotto lotto; - public void saveLotto(Lotto lotto) { + public void saveLotto(final Lotto lotto) { this.lotto = lotto; } } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 3a37437a81..f08b6286f3 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -7,7 +7,7 @@ public class LottoService { private final LottoRepository lottoRepository; - public LottoService(LottoRepository lottoRepository) { + public LottoService(final LottoRepository lottoRepository) { this.lottoRepository = lottoRepository; } From 09c870aee28b1e47516f9ad6914fd3332ca549bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 16:14:03 +0900 Subject: [PATCH 05/26] =?UTF-8?q?feat:=20=EC=B6=9C=EB=A0=A5=20=EB=8B=B4?= =?UTF-8?q?=EB=8B=B9=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=B0=8F=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EA=B2=80=EC=A6=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - OutputManager를 통한 출력 - InputValidator를 통한 입력값 검증 - enum LottoMessage - enum ErrorMessage --- docs/README.md | 3 +++ src/main/java/lotto/ComponentFactory.java | 12 ++++++++++-- src/main/java/lotto/ErrorMessage.java | 12 ++++++++++++ src/main/java/lotto/InputManager.java | 12 +++++++++++- src/main/java/lotto/InputValidator.java | 17 +++++++++++++++++ src/main/java/lotto/LottoController.java | 5 ++++- src/main/java/lotto/LottoMessage.java | 15 +++++++++++++++ src/main/java/lotto/OutputManager.java | 8 ++++++++ 8 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 src/main/java/lotto/ErrorMessage.java create mode 100644 src/main/java/lotto/InputValidator.java create mode 100644 src/main/java/lotto/LottoMessage.java create mode 100644 src/main/java/lotto/OutputManager.java diff --git a/docs/README.md b/docs/README.md index f2b0fcc1cf..c473abfd8d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,8 +28,11 @@ - buyAmountInput() 구매 금액 입력 ## InputValidator - 입력 검증 +- validateIsNumeric() 입력값 숫자인지 검증 +- validateMultiplyOfThousand() 1000의 배수인지 검증 ## OutputManager - 출력하기 +- printStartMessage() 로또 시작 메시지 출력 ## ComponentFactory - 컴포넌트들의 생성 diff --git a/src/main/java/lotto/ComponentFactory.java b/src/main/java/lotto/ComponentFactory.java index 68117f14df..ad64b2957e 100644 --- a/src/main/java/lotto/ComponentFactory.java +++ b/src/main/java/lotto/ComponentFactory.java @@ -3,7 +3,7 @@ public class ComponentFactory { public LottoController lottoController() { - return new LottoController(lottoService(), inputManager()); + return new LottoController(lottoService(), outputManager(), inputManager()); } private LottoService lottoService() { @@ -14,7 +14,15 @@ private LottoRepository lottoRepository() { return new LottoRepository(); } + private OutputManager outputManager() { + return new OutputManager(); + } + private InputManager inputManager() { - return new InputManager(); + return new InputManager(inputValidator()); + } + + private InputValidator inputValidator() { + return new InputValidator(); } } diff --git a/src/main/java/lotto/ErrorMessage.java b/src/main/java/lotto/ErrorMessage.java new file mode 100644 index 0000000000..c8645cc71f --- /dev/null +++ b/src/main/java/lotto/ErrorMessage.java @@ -0,0 +1,12 @@ +package lotto; + +public enum ErrorMessage { + NON_NUMERIC_ERROR("[ERROR] 숫자값만 입력 가능합니다."), + NOT_MULTIPLY_OF_THOUSAND_ERROR("[ERROR] 1000의 배수만 입력 가능합니다."); + + private final String message; + + ErrorMessage(final String message) { + this.message = message; + } +} diff --git a/src/main/java/lotto/InputManager.java b/src/main/java/lotto/InputManager.java index 38083a7ee3..cf1f03011f 100644 --- a/src/main/java/lotto/InputManager.java +++ b/src/main/java/lotto/InputManager.java @@ -3,9 +3,19 @@ import camp.nextstep.edu.missionutils.Console; public class InputManager { + private final InputValidator inputValidator; + + public InputManager(final InputValidator inputValidator) { + this.inputValidator = inputValidator; + } public Integer buyAmountInput() { final String input = Console.readLine(); - return Integer.valueOf(input); + inputValidator.validateIsNumeric(input); + + final Integer inputNumber = Integer.valueOf(input); + inputValidator.validateMultiplyOfThousand(inputNumber); + + return inputNumber; } } diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java new file mode 100644 index 0000000000..4cbcaaea7e --- /dev/null +++ b/src/main/java/lotto/InputValidator.java @@ -0,0 +1,17 @@ +package lotto; + +public class InputValidator { + private static final String NUMERIC_MATCHER = "-?\\d+"; + + public void validateIsNumeric(final String buyAmountInput) { + if (!buyAmountInput.matches(NUMERIC_MATCHER)) { + throw new IllegalArgumentException(ErrorMessage.NON_NUMERIC_ERROR.toString()); + } + } + + public void validateMultiplyOfThousand(final Integer buyAmountInput) { + if (buyAmountInput % 1000 != 0) { + throw new IllegalArgumentException(ErrorMessage.NOT_MULTIPLY_OF_THOUSAND_ERROR.toString()); + } + } +} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 283bbd2f91..227905d96d 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -2,15 +2,18 @@ public class LottoController { private final LottoService lottoService; + private final OutputManager outputManager; private final InputManager inputManager; - public LottoController(final LottoService lottoService, final InputManager inputManager) { + public LottoController(final LottoService lottoService, final OutputManager outputManager, final InputManager inputManager) { this.lottoService = lottoService; + this.outputManager = outputManager; this.inputManager = inputManager; } public void startLotto() { lottoService.saveLotto(); + outputManager.printStartMessage(); final Integer buyAmount = inputManager.buyAmountInput(); } } diff --git a/src/main/java/lotto/LottoMessage.java b/src/main/java/lotto/LottoMessage.java new file mode 100644 index 0000000000..bf1a786b7c --- /dev/null +++ b/src/main/java/lotto/LottoMessage.java @@ -0,0 +1,15 @@ +package lotto; + +public enum LottoMessage { + START_MESSAGE("구입금액을 입력해 주세요."); + private final String message; + + LottoMessage(final String message) { + this.message = message; + } + + @Override + public String toString() { + return this.message; + } +} diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java new file mode 100644 index 0000000000..413d01e166 --- /dev/null +++ b/src/main/java/lotto/OutputManager.java @@ -0,0 +1,8 @@ +package lotto; + +public class OutputManager { + + public void printStartMessage() { + System.out.println(LottoMessage.START_MESSAGE); + } +} From 94d063de4582b713d4c32820304fbb0afd384c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 16:24:28 +0900 Subject: [PATCH 06/26] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=B0=9C?= =?UTF-8?q?=ED=96=89=EB=9F=89=20=EA=B3=84=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 로또 발행 개수 계산 - 로또 발행 개수 메시지 출력 --- docs/README.md | 21 ++++++++++++--------- src/main/java/lotto/LottoController.java | 2 ++ src/main/java/lotto/LottoMessage.java | 3 ++- src/main/java/lotto/LottoService.java | 9 +++++++++ src/main/java/lotto/OutputManager.java | 4 ++++ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/docs/README.md b/docs/README.md index c473abfd8d..baff10d35a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,15 +2,16 @@ 1. 랜덤 번호 생성 및 저장 기능 2. 구입 금액 입력 기능 3. 구입 금액 입력값 검증 기능 -4. 로또 발행 기능 -5. 발행한 로또 수량 및 번호 출력 기능 -6. 당첨 번호 입력 기능 -7. 당첨 번호 입력값 검증 기능 -8. 번호 일치 여부 확인 및 당첨 여부 확인 기능 -9. 당첨 내역 출력 기능 -10. 수익률 계산 및 반올림 기능 -11. 수익률 출력 기능 -12. 예외 처리 및 에러 문구 출력 기능 +4. 로또 발행 개수 계산 기능 +5. 로또 발행 기능 +6. 발행한 로또 수량 및 번호 출력 기능 +7. 당첨 번호 입력 기능 +8. 당첨 번호 입력값 검증 기능 +9. 번호 일치 여부 확인 및 당첨 여부 확인 기능 +10. 당첨 내역 출력 기능 +11. 수익률 계산 및 반올림 기능 +12. 수익률 출력 기능 +13. 예외 처리 및 에러 문구 출력 기능 # 구현할 클래스 목록 ## LottoTicket - 발행한 로또 티켓 @@ -20,6 +21,7 @@ ## LottoService - 로또 서비스 - saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 +- getPublishNum() 로또 발행량 계산 ## LottoRepository - 로또 레포지토리 - saveLotto() Lotto 객체 저장 @@ -33,6 +35,7 @@ ## OutputManager - 출력하기 - printStartMessage() 로또 시작 메시지 출력 +- printPublishNum() 로또 발행 개수 메시지 출력 ## ComponentFactory - 컴포넌트들의 생성 diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 227905d96d..c4b3c6f2db 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -15,5 +15,7 @@ public void startLotto() { lottoService.saveLotto(); outputManager.printStartMessage(); final Integer buyAmount = inputManager.buyAmountInput(); + final Integer publishNum = lottoService.getPublishNum(buyAmount); + outputManager.printPublishNum(publishNum); } } diff --git a/src/main/java/lotto/LottoMessage.java b/src/main/java/lotto/LottoMessage.java index bf1a786b7c..2bb1229275 100644 --- a/src/main/java/lotto/LottoMessage.java +++ b/src/main/java/lotto/LottoMessage.java @@ -1,7 +1,8 @@ package lotto; public enum LottoMessage { - START_MESSAGE("구입금액을 입력해 주세요."); + START_MESSAGE("구입금액을 입력해 주세요."), + PUBLISH_NUM_MESSAGE("%d개를 구매했습니다."); private final String message; LottoMessage(final String message) { diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index f08b6286f3..297c571838 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -13,9 +13,18 @@ public LottoService(final LottoRepository lottoRepository) { public void saveLotto() { final Lotto lotto = new Lotto(createRandomNumbers()); + lottoRepository.saveLotto(lotto); } private List createRandomNumbers() { return Randoms.pickUniqueNumbersInRange(1, 45, 6); } + + public Integer getPublishNum(final Integer buyAmount) { + return divideByThousand(buyAmount); + } + + private Integer divideByThousand(final Integer dividend) { + return dividend / 1000; + } } diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index 413d01e166..b74b2d9afa 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -5,4 +5,8 @@ public class OutputManager { public void printStartMessage() { System.out.println(LottoMessage.START_MESSAGE); } + + public void printPublishNum(final Integer publishNum) { + System.out.println(String.format(LottoMessage.PUBLISH_NUM_MESSAGE.toString(), publishNum)); + } } From abbcead20b1d2c94c9735d2ad0460ee4c9625635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 16:32:55 +0900 Subject: [PATCH 07/26] =?UTF-8?q?feat(FrontController):=20=ED=94=84?= =?UTF-8?q?=EB=A1=A0=ED=8A=B8=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=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 클래스 추가 - FrontController --- docs/README.md | 6 +++++- src/main/java/lotto/Application.java | 4 ++-- src/main/java/lotto/ComponentFactory.java | 6 +++++- src/main/java/lotto/FrontController.java | 14 ++++++++++++++ src/main/java/lotto/LottoController.java | 6 +++++- 5 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 src/main/java/lotto/FrontController.java diff --git a/docs/README.md b/docs/README.md index baff10d35a..51db2a8779 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,8 +16,12 @@ # 구현할 클래스 목록 ## LottoTicket - 발행한 로또 티켓 +## FrontController - 프론트 컨트롤러 +- playLotto() 로또 플레이 + ## LottoController - 로또 컨트롤러 -- startLotto() 로또 시작 +- saveLotto() 로또 저장 +- getPublishNum() 로또 발행량 계산 ## LottoService - 로또 서비스 - saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index c9d396717f..2be7e5eb86 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -3,7 +3,7 @@ public class Application { public static void main(String[] args) { final ComponentFactory componentFactory = new ComponentFactory(); - final LottoController lottoController = componentFactory.lottoController(); - lottoController.startLotto(); + final FrontController frontController = componentFactory.frontController(); + frontController.playLotto(); } } diff --git a/src/main/java/lotto/ComponentFactory.java b/src/main/java/lotto/ComponentFactory.java index ad64b2957e..88bf47a6ab 100644 --- a/src/main/java/lotto/ComponentFactory.java +++ b/src/main/java/lotto/ComponentFactory.java @@ -2,7 +2,11 @@ public class ComponentFactory { - public LottoController lottoController() { + public FrontController frontController() { + return new FrontController(lottoController()); + } + + private LottoController lottoController() { return new LottoController(lottoService(), outputManager(), inputManager()); } diff --git a/src/main/java/lotto/FrontController.java b/src/main/java/lotto/FrontController.java new file mode 100644 index 0000000000..b49fe9364a --- /dev/null +++ b/src/main/java/lotto/FrontController.java @@ -0,0 +1,14 @@ +package lotto; + +public class FrontController { + private final LottoController lottoController; + + public FrontController(final LottoController lottoController) { + this.lottoController = lottoController; + } + + public void playLotto() { + lottoController.saveLotto(); + final Integer publishNum = lottoController.getPublishNum(); + } +} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index c4b3c6f2db..76bd60b688 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -11,11 +11,15 @@ public LottoController(final LottoService lottoService, final OutputManager outp this.inputManager = inputManager; } - public void startLotto() { + public void saveLotto() { lottoService.saveLotto(); + } + + public Integer getPublishNum() { outputManager.printStartMessage(); final Integer buyAmount = inputManager.buyAmountInput(); final Integer publishNum = lottoService.getPublishNum(buyAmount); outputManager.printPublishNum(publishNum); + return publishNum; } } From b7400f8553a35e2e777d2d65eccacd8d64d524bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 16:49:06 +0900 Subject: [PATCH 08/26] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=ED=8B=B0?= =?UTF-8?q?=EC=BC=93=20=EB=B0=9C=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 및 클래스 추가 - 로또 티켓 발행 기능 추가 - LottoTicket 클래스 추가 - enum Number 추가 --- docs/README.md | 3 +++ src/main/java/lotto/FrontController.java | 1 + src/main/java/lotto/InputValidator.java | 2 +- src/main/java/lotto/Lotto.java | 6 +++--- src/main/java/lotto/LottoController.java | 6 ++++++ src/main/java/lotto/LottoRepository.java | 7 +++++++ src/main/java/lotto/LottoService.java | 16 ++++++++++++++-- src/main/java/lotto/LottoTicket.java | 12 ++++++++++++ src/main/java/lotto/Number.java | 20 ++++++++++++++++++++ src/main/java/lotto/OutputManager.java | 2 ++ 10 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/main/java/lotto/LottoTicket.java create mode 100644 src/main/java/lotto/Number.java diff --git a/docs/README.md b/docs/README.md index 51db2a8779..d32cde312b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,13 +22,16 @@ ## LottoController - 로또 컨트롤러 - saveLotto() 로또 저장 - getPublishNum() 로또 발행량 계산 +- publishLottoTickets() 로또 티켓 발행 ## LottoService - 로또 서비스 - saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 - getPublishNum() 로또 발행량 계산 +- publishLottoTickets() 로또 티켓 발행 ## LottoRepository - 로또 레포지토리 - saveLotto() Lotto 객체 저장 +- saveLottoTicket() LottoTicket 객체 저장 ## InputManager - 입력 받기 - buyAmountInput() 구매 금액 입력 diff --git a/src/main/java/lotto/FrontController.java b/src/main/java/lotto/FrontController.java index b49fe9364a..3f49ed0800 100644 --- a/src/main/java/lotto/FrontController.java +++ b/src/main/java/lotto/FrontController.java @@ -10,5 +10,6 @@ public FrontController(final LottoController lottoController) { public void playLotto() { lottoController.saveLotto(); final Integer publishNum = lottoController.getPublishNum(); + lottoController.publishLottoTickets(publishNum); } } diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java index 4cbcaaea7e..7fafb980d0 100644 --- a/src/main/java/lotto/InputValidator.java +++ b/src/main/java/lotto/InputValidator.java @@ -10,7 +10,7 @@ public void validateIsNumeric(final String buyAmountInput) { } public void validateMultiplyOfThousand(final Integer buyAmountInput) { - if (buyAmountInput % 1000 != 0) { + if (buyAmountInput % Number.THOUSAND.toValue() != Number.ZERO.toValue()) { throw new IllegalArgumentException(ErrorMessage.NOT_MULTIPLY_OF_THOUSAND_ERROR.toString()); } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d1f7..ad35225cc8 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -5,13 +5,13 @@ public class Lotto { private final List numbers; - public Lotto(List numbers) { + public Lotto(final List numbers) { validate(numbers); this.numbers = numbers; } - private void validate(List numbers) { - if (numbers.size() != 6) { + private void validate(final List numbers) { + if (numbers.size() != Number.NUMBER_NUM.toValue()) { throw new IllegalArgumentException(); } } diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 76bd60b688..0a1ebb93bd 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -1,5 +1,7 @@ package lotto; +import java.util.List; + public class LottoController { private final LottoService lottoService; private final OutputManager outputManager; @@ -22,4 +24,8 @@ public Integer getPublishNum() { outputManager.printPublishNum(publishNum); return publishNum; } + + public void publishLottoTickets(final Integer publishNum) { + final List lottoTickets = lottoService.publishLottoTickets(publishNum); + } } diff --git a/src/main/java/lotto/LottoRepository.java b/src/main/java/lotto/LottoRepository.java index 6a19fe8768..bd03dc1b5f 100644 --- a/src/main/java/lotto/LottoRepository.java +++ b/src/main/java/lotto/LottoRepository.java @@ -1,9 +1,16 @@ package lotto; +import java.util.List; + public class LottoRepository { private Lotto lotto; + private List lottoTicket; public void saveLotto(final Lotto lotto) { this.lotto = lotto; } + + public void saveLottoTicket(final List lottoTicket) { + this.lottoTicket = lottoTicket; + } } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 297c571838..816bb3bbf5 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -3,6 +3,8 @@ import camp.nextstep.edu.missionutils.Randoms; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class LottoService { private final LottoRepository lottoRepository; @@ -17,7 +19,7 @@ public void saveLotto() { } private List createRandomNumbers() { - return Randoms.pickUniqueNumbersInRange(1, 45, 6); + return Randoms.pickUniqueNumbersInRange(Number.MIN_RANGE.toValue(), Number.MAX_RANGE.toValue(), Number.NUMBER_NUM.toValue()); } public Integer getPublishNum(final Integer buyAmount) { @@ -25,6 +27,16 @@ public Integer getPublishNum(final Integer buyAmount) { } private Integer divideByThousand(final Integer dividend) { - return dividend / 1000; + return dividend / Number.THOUSAND.toValue(); + } + + public List publishLottoTickets(final Integer publishNum) { + final List lottoTickets = IntStream.range(Number.ZERO.toValue(), publishNum) + .mapToObj(i -> createRandomNumbers()) + .map(LottoTicket::new) + .collect(Collectors.toList()); + + lottoRepository.saveLottoTicket(lottoTickets); + return lottoTickets; } } diff --git a/src/main/java/lotto/LottoTicket.java b/src/main/java/lotto/LottoTicket.java new file mode 100644 index 0000000000..4f6ea30fd4 --- /dev/null +++ b/src/main/java/lotto/LottoTicket.java @@ -0,0 +1,12 @@ +package lotto; + +import java.util.Collections; +import java.util.List; + +public class LottoTicket { + private final List numbers; + + public LottoTicket(final List numbers) { + this.numbers = Collections.unmodifiableList(numbers); + } +} diff --git a/src/main/java/lotto/Number.java b/src/main/java/lotto/Number.java new file mode 100644 index 0000000000..3c52e12fba --- /dev/null +++ b/src/main/java/lotto/Number.java @@ -0,0 +1,20 @@ +package lotto; + +public enum Number { + ZERO(0), + + THOUSAND(1000), + MIN_RANGE(1), + MAX_RANGE(45), + NUMBER_NUM(6); + + private final int value; + + Number(int value) { + this.value = value; + } + + public int toValue() { + return value; + } +} diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index b74b2d9afa..cae2b81a6a 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -1,5 +1,7 @@ package lotto; +import java.util.List; + public class OutputManager { public void printStartMessage() { From adb9f4894abcf892a84d5802adb9ceab632fd57d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 16:53:41 +0900 Subject: [PATCH 09/26] =?UTF-8?q?feat:=20=EB=B0=9C=ED=96=89=ED=95=9C=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=ED=8B=B0=EC=BC=93=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 로또 티켓 출력 기능 --- src/main/java/lotto/LottoController.java | 1 + src/main/java/lotto/LottoTicket.java | 5 +++++ src/main/java/lotto/OutputManager.java | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 0a1ebb93bd..63f715a441 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -27,5 +27,6 @@ public Integer getPublishNum() { public void publishLottoTickets(final Integer publishNum) { final List lottoTickets = lottoService.publishLottoTickets(publishNum); + outputManager.printLottoTickets(lottoTickets); } } diff --git a/src/main/java/lotto/LottoTicket.java b/src/main/java/lotto/LottoTicket.java index 4f6ea30fd4..f501d174f1 100644 --- a/src/main/java/lotto/LottoTicket.java +++ b/src/main/java/lotto/LottoTicket.java @@ -9,4 +9,9 @@ public class LottoTicket { public LottoTicket(final List numbers) { this.numbers = Collections.unmodifiableList(numbers); } + + @Override + public String toString() { + return this.numbers.toString(); + } } diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index cae2b81a6a..226f0650c6 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -11,4 +11,8 @@ public void printStartMessage() { public void printPublishNum(final Integer publishNum) { System.out.println(String.format(LottoMessage.PUBLISH_NUM_MESSAGE.toString(), publishNum)); } + + public void printLottoTickets(final List lottoTickets) { + lottoTickets.forEach(System.out::println); + } } From c9ff2dcb67140dc1551c723465152f1dd837b35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 16:56:59 +0900 Subject: [PATCH 10/26] =?UTF-8?q?docs(README):=20=EC=97=B4=EA=B1=B0?= =?UTF-8?q?=ED=98=95=20=EB=B0=8F=20=ED=95=A8=EC=88=98=20=EC=84=A4=EB=AA=85?= =?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 리드미 컨텐츠 추가 - enum Number - OutputManager의 printLottoTickets() --- docs/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/README.md b/docs/README.md index d32cde312b..173d0ba9c6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -43,6 +43,7 @@ ## OutputManager - 출력하기 - printStartMessage() 로또 시작 메시지 출력 - printPublishNum() 로또 발행 개수 메시지 출력 +- printLottoTickets() 발행한 로또 티켓 출력 ## ComponentFactory - 컴포넌트들의 생성 @@ -50,3 +51,5 @@ ## LottoMessage - 로또 진행 메시지 ## ErrorMessage - 에러 메시지 + +## Number - 로또 생성 범위 및 로또 실행에 필요한 숫자 \ No newline at end of file From b0e4bb92115e3fb3a3fd903cf7984f72fe0e0b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 17:06:55 +0900 Subject: [PATCH 11/26] =?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=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 당첨 번호 입력 기능 --- src/main/java/lotto/FrontController.java | 1 + src/main/java/lotto/InputManager.java | 16 ++++++++++++++++ src/main/java/lotto/LottoController.java | 5 +++++ src/main/java/lotto/LottoMessage.java | 4 +++- src/main/java/lotto/OutputManager.java | 4 ++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/FrontController.java b/src/main/java/lotto/FrontController.java index 3f49ed0800..06aa182b53 100644 --- a/src/main/java/lotto/FrontController.java +++ b/src/main/java/lotto/FrontController.java @@ -11,5 +11,6 @@ public void playLotto() { lottoController.saveLotto(); final Integer publishNum = lottoController.getPublishNum(); lottoController.publishLottoTickets(publishNum); + lottoController.getWinLotteryDetails(); } } diff --git a/src/main/java/lotto/InputManager.java b/src/main/java/lotto/InputManager.java index cf1f03011f..9d46e77d57 100644 --- a/src/main/java/lotto/InputManager.java +++ b/src/main/java/lotto/InputManager.java @@ -2,6 +2,10 @@ import camp.nextstep.edu.missionutils.Console; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + public class InputManager { private final InputValidator inputValidator; @@ -18,4 +22,16 @@ public Integer buyAmountInput() { return inputNumber; } + + public List winningNumberInput() { + final String input = Console.readLine(); + final List winningNumber = convertToList(input); + return winningNumber; + } + + private List convertToList(final String input) { + return Arrays.stream(input.split(",")) + .map(Integer::valueOf) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 63f715a441..a144ca562b 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -29,4 +29,9 @@ public void publishLottoTickets(final Integer publishNum) { final List lottoTickets = lottoService.publishLottoTickets(publishNum); outputManager.printLottoTickets(lottoTickets); } + + public void getWinLotteryDetails() { + outputManager.printCreateWinningNumber(); + inputManager.winningNumberInput(); + } } diff --git a/src/main/java/lotto/LottoMessage.java b/src/main/java/lotto/LottoMessage.java index 2bb1229275..b3ad0bb701 100644 --- a/src/main/java/lotto/LottoMessage.java +++ b/src/main/java/lotto/LottoMessage.java @@ -2,7 +2,9 @@ public enum LottoMessage { START_MESSAGE("구입금액을 입력해 주세요."), - PUBLISH_NUM_MESSAGE("%d개를 구매했습니다."); + PUBLISH_NUM_MESSAGE("%d개를 구매했습니다."), + CREATE_WINNING_NUMBER_MESSAGE("당첨 번호를 입력해 주세요."); + private final String message; LottoMessage(final String message) { diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index 226f0650c6..e042500c6e 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -15,4 +15,8 @@ public void printPublishNum(final Integer publishNum) { public void printLottoTickets(final List lottoTickets) { lottoTickets.forEach(System.out::println); } + + public void printCreateWinningNumber() { + System.out.println(LottoMessage.CREATE_WINNING_NUMBER_MESSAGE); + } } From c494bfb565d9b18a08924bd232ed887f522e215a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 17:21:32 +0900 Subject: [PATCH 12/26] =?UTF-8?q?feat(WinningNumber):=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=20=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 당첨 번호 입력 기능 - 보너스 번호 입력 기능 - 당첨 번호 및 보너스 번호 저장 기능 --- docs/README.md | 9 +++++++++ src/main/java/lotto/FrontController.java | 2 +- src/main/java/lotto/InputManager.java | 8 ++++++++ src/main/java/lotto/LottoController.java | 9 +++++++-- src/main/java/lotto/LottoMessage.java | 3 ++- src/main/java/lotto/LottoRepository.java | 5 +++++ src/main/java/lotto/LottoService.java | 6 ++++++ src/main/java/lotto/OutputManager.java | 4 ++++ src/main/java/lotto/WinningNumber.java | 13 +++++++++++++ 9 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/main/java/lotto/WinningNumber.java diff --git a/docs/README.md b/docs/README.md index 173d0ba9c6..3ee2a86b32 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,6 +16,8 @@ # 구현할 클래스 목록 ## LottoTicket - 발행한 로또 티켓 +## WinningNumber - 당첨 번호 + ## FrontController - 프론트 컨트롤러 - playLotto() 로또 플레이 @@ -23,18 +25,23 @@ - saveLotto() 로또 저장 - getPublishNum() 로또 발행량 계산 - publishLottoTickets() 로또 티켓 발행 +- getWinningNumber() 당첨 번호 및 보너스 번호 저장후 조회 ## LottoService - 로또 서비스 - saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 - getPublishNum() 로또 발행량 계산 - publishLottoTickets() 로또 티켓 발행 +- saveWinningNumber() 당첨 번호 저장 ## LottoRepository - 로또 레포지토리 - saveLotto() Lotto 객체 저장 - saveLottoTicket() LottoTicket 객체 저장 +- saveWinningNumber() WinningNumber 객체 저장 ## InputManager - 입력 받기 - buyAmountInput() 구매 금액 입력 +- winningNumberInput() 당첨 번호 입력 +- bonusNumberInput() 보너스 번호 입력 ## InputValidator - 입력 검증 - validateIsNumeric() 입력값 숫자인지 검증 @@ -44,6 +51,8 @@ - printStartMessage() 로또 시작 메시지 출력 - printPublishNum() 로또 발행 개수 메시지 출력 - printLottoTickets() 발행한 로또 티켓 출력 +- printCreateWinningNumber() 당첨 번호 입력 메시지 출력 +- printCreateBonusNumber() 보너스 번호 입력 메시지 출력 ## ComponentFactory - 컴포넌트들의 생성 diff --git a/src/main/java/lotto/FrontController.java b/src/main/java/lotto/FrontController.java index 06aa182b53..35bef06032 100644 --- a/src/main/java/lotto/FrontController.java +++ b/src/main/java/lotto/FrontController.java @@ -11,6 +11,6 @@ public void playLotto() { lottoController.saveLotto(); final Integer publishNum = lottoController.getPublishNum(); lottoController.publishLottoTickets(publishNum); - lottoController.getWinLotteryDetails(); + final WinningNumber winningNumber = lottoController.getWinningNumber(); } } diff --git a/src/main/java/lotto/InputManager.java b/src/main/java/lotto/InputManager.java index 9d46e77d57..287db10a55 100644 --- a/src/main/java/lotto/InputManager.java +++ b/src/main/java/lotto/InputManager.java @@ -34,4 +34,12 @@ private List convertToList(final String input) { .map(Integer::valueOf) .collect(Collectors.toList()); } + + public Integer bonusNumberInput() { + final String input = Console.readLine(); + inputValidator.validateIsNumeric(input); + + final Integer inputNumber = Integer.valueOf(input); + return inputNumber; + } } diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index a144ca562b..ed1f96174e 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -30,8 +30,13 @@ public void publishLottoTickets(final Integer publishNum) { outputManager.printLottoTickets(lottoTickets); } - public void getWinLotteryDetails() { + public WinningNumber getWinningNumber() { outputManager.printCreateWinningNumber(); - inputManager.winningNumberInput(); + final List winningNumberInput = inputManager.winningNumberInput(); + + outputManager.printCreateBonusNumber(); + final Integer bonusNumberInput = inputManager.bonusNumberInput(); + + return lottoService.saveWinningNumber(winningNumberInput, bonusNumberInput); } } diff --git a/src/main/java/lotto/LottoMessage.java b/src/main/java/lotto/LottoMessage.java index b3ad0bb701..862afe5fae 100644 --- a/src/main/java/lotto/LottoMessage.java +++ b/src/main/java/lotto/LottoMessage.java @@ -3,7 +3,8 @@ public enum LottoMessage { START_MESSAGE("구입금액을 입력해 주세요."), PUBLISH_NUM_MESSAGE("%d개를 구매했습니다."), - CREATE_WINNING_NUMBER_MESSAGE("당첨 번호를 입력해 주세요."); + CREATE_WINNING_NUMBER_MESSAGE("당첨 번호를 입력해 주세요."), + CREATE_BONUS_NUMBER_MESSAGE("보너스 번호를 입력해 주세요."); private final String message; diff --git a/src/main/java/lotto/LottoRepository.java b/src/main/java/lotto/LottoRepository.java index bd03dc1b5f..e4bff19dfb 100644 --- a/src/main/java/lotto/LottoRepository.java +++ b/src/main/java/lotto/LottoRepository.java @@ -5,6 +5,7 @@ public class LottoRepository { private Lotto lotto; private List lottoTicket; + private WinningNumber winningNumber; public void saveLotto(final Lotto lotto) { this.lotto = lotto; @@ -13,4 +14,8 @@ public void saveLotto(final Lotto lotto) { public void saveLottoTicket(final List lottoTicket) { this.lottoTicket = lottoTicket; } + + public void saveWinningNumber(final WinningNumber winningNumber) { + this.winningNumber = winningNumber; + } } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 816bb3bbf5..52aa7221de 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -39,4 +39,10 @@ public List publishLottoTickets(final Integer publishNum) { lottoRepository.saveLottoTicket(lottoTickets); return lottoTickets; } + + public WinningNumber saveWinningNumber(final List winningNumberInput, final Integer bonusNumberInput) { + final WinningNumber winningNumber = new WinningNumber(winningNumberInput, bonusNumberInput); + lottoRepository.saveWinningNumber(winningNumber); + return winningNumber; + } } diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index e042500c6e..eaef331c2a 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -19,4 +19,8 @@ public void printLottoTickets(final List lottoTickets) { public void printCreateWinningNumber() { System.out.println(LottoMessage.CREATE_WINNING_NUMBER_MESSAGE); } + + public void printCreateBonusNumber() { + System.out.println(LottoMessage.CREATE_BONUS_NUMBER_MESSAGE); + } } diff --git a/src/main/java/lotto/WinningNumber.java b/src/main/java/lotto/WinningNumber.java new file mode 100644 index 0000000000..23a38d16b4 --- /dev/null +++ b/src/main/java/lotto/WinningNumber.java @@ -0,0 +1,13 @@ +package lotto; + +import java.util.List; + +public class WinningNumber { + private final List winningNumbers; + private final Integer bonusNumber; + + public WinningNumber(final List winningNumbers, final Integer bonusNumber) { + this.winningNumbers = winningNumbers; + this.bonusNumber = bonusNumber; + } +} From 80aac325e3b338128868dc1d4016242151e960c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 17:32:45 +0900 Subject: [PATCH 13/26] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9E=85=EB=A0=A5=EA=B0=92=20=EA=B2=80=EC=A6=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 당첨 번호 입력값 검증 --- src/main/java/lotto/ErrorMessage.java | 11 +++++++++-- src/main/java/lotto/InputManager.java | 1 + src/main/java/lotto/InputValidator.java | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/ErrorMessage.java b/src/main/java/lotto/ErrorMessage.java index c8645cc71f..46beb0c008 100644 --- a/src/main/java/lotto/ErrorMessage.java +++ b/src/main/java/lotto/ErrorMessage.java @@ -1,12 +1,19 @@ package lotto; public enum ErrorMessage { - NON_NUMERIC_ERROR("[ERROR] 숫자값만 입력 가능합니다."), - NOT_MULTIPLY_OF_THOUSAND_ERROR("[ERROR] 1000의 배수만 입력 가능합니다."); + ERROR_PREFIX("[ERROR] "), + NON_NUMERIC_ERROR("숫자값만 입력 가능합니다."), + NOT_MULTIPLY_OF_THOUSAND_ERROR("1000의 배수만 입력 가능합니다."), + NOT_LIST_WITH_COMMA_ERROR("콤마로 구분된 6개의 숫자 형태로만 입력 가능합니다."); private final String message; ErrorMessage(final String message) { this.message = message; } + + @Override + public String toString() { + return ERROR_PREFIX.message + message; + } } diff --git a/src/main/java/lotto/InputManager.java b/src/main/java/lotto/InputManager.java index 287db10a55..e57e7716fe 100644 --- a/src/main/java/lotto/InputManager.java +++ b/src/main/java/lotto/InputManager.java @@ -25,6 +25,7 @@ public Integer buyAmountInput() { public List winningNumberInput() { final String input = Console.readLine(); + inputValidator.validateIsListWithComma(input); final List winningNumber = convertToList(input); return winningNumber; } diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java index 7fafb980d0..efe66919fa 100644 --- a/src/main/java/lotto/InputValidator.java +++ b/src/main/java/lotto/InputValidator.java @@ -1,7 +1,10 @@ package lotto; +import java.util.regex.Pattern; + public class InputValidator { private static final String NUMERIC_MATCHER = "-?\\d+"; + private static final String LIST_WITH_COMMA_MATCHER = "\\d{1,},\\d{1,},\\d{1,},\\d{1,},\\d{1,},\\d{1,}"; public void validateIsNumeric(final String buyAmountInput) { if (!buyAmountInput.matches(NUMERIC_MATCHER)) { @@ -14,4 +17,10 @@ public void validateMultiplyOfThousand(final Integer buyAmountInput) { throw new IllegalArgumentException(ErrorMessage.NOT_MULTIPLY_OF_THOUSAND_ERROR.toString()); } } + + public void validateIsListWithComma(final String input) { + if (!Pattern.matches(LIST_WITH_COMMA_MATCHER, input)) { + throw new IllegalArgumentException(ErrorMessage.NOT_LIST_WITH_COMMA_ERROR.toString()); + } + } } From c685be4f7870150ff26c65908a259084ebbd3b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 17:34:47 +0900 Subject: [PATCH 14/26] =?UTF-8?q?docs(README):=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EA=B0=92=20=EA=B2=80=EC=A6=9D=20=EA=B4=80=EB=A0=A8=20=EB=A6=AC?= =?UTF-8?q?=EB=93=9C=EB=AF=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 리드미 컨텐츠 추가 - validateIsListWithComma() --- docs/README.md | 1 + src/main/java/lotto/InputValidator.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 3ee2a86b32..0ace48db5c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -46,6 +46,7 @@ ## InputValidator - 입력 검증 - validateIsNumeric() 입력값 숫자인지 검증 - validateMultiplyOfThousand() 1000의 배수인지 검증 +- validateIsListWithComma() 콤마로 구분된 숫자들의 나열인지 검증 ## OutputManager - 출력하기 - printStartMessage() 로또 시작 메시지 출력 diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java index efe66919fa..512db32d8b 100644 --- a/src/main/java/lotto/InputValidator.java +++ b/src/main/java/lotto/InputValidator.java @@ -4,7 +4,7 @@ public class InputValidator { private static final String NUMERIC_MATCHER = "-?\\d+"; - private static final String LIST_WITH_COMMA_MATCHER = "\\d{1,},\\d{1,},\\d{1,},\\d{1,},\\d{1,},\\d{1,}"; + private static final String LIST_WITH_COMMA_MATCHER = "\\d+,\\d+,\\d+,\\d+,\\d+,\\d+"; public void validateIsNumeric(final String buyAmountInput) { if (!buyAmountInput.matches(NUMERIC_MATCHER)) { From a725d88584b012daaefa333606c8cbd4b898ed74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 18:25:25 +0900 Subject: [PATCH 15/26] =?UTF-8?q?test:=20=EC=84=9C=EB=B9=84=EC=8A=A4?= =?UTF-8?q?=EC=99=80=20=EC=9E=85=EB=A0=A5=EA=B0=92=20=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=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 테스트 추가 - LottoService 테스트 - InputValidator 테스트 --- src/main/java/lotto/ErrorMessage.java | 6 +- src/main/java/lotto/InputValidator.java | 4 +- src/main/java/lotto/Lotto.java | 15 ++++- src/main/java/lotto/LottoService.java | 21 ++++-- src/main/java/lotto/LottoTicket.java | 20 ++++++ src/main/java/lotto/WinningNumber.java | 20 ++++++ src/test/java/lotto/InputValidatorTest.java | 70 ++++++++++++++++++++ src/test/java/lotto/LottoRepositoryTest.java | 47 +++++++++++++ src/test/java/lotto/LottoServiceTest.java | 62 +++++++++++++++++ 9 files changed, 254 insertions(+), 11 deletions(-) create mode 100644 src/test/java/lotto/InputValidatorTest.java create mode 100644 src/test/java/lotto/LottoRepositoryTest.java create mode 100644 src/test/java/lotto/LottoServiceTest.java diff --git a/src/main/java/lotto/ErrorMessage.java b/src/main/java/lotto/ErrorMessage.java index 46beb0c008..0fcc706e97 100644 --- a/src/main/java/lotto/ErrorMessage.java +++ b/src/main/java/lotto/ErrorMessage.java @@ -3,8 +3,10 @@ public enum ErrorMessage { ERROR_PREFIX("[ERROR] "), NON_NUMERIC_ERROR("숫자값만 입력 가능합니다."), - NOT_MULTIPLY_OF_THOUSAND_ERROR("1000의 배수만 입력 가능합니다."), - NOT_LIST_WITH_COMMA_ERROR("콤마로 구분된 6개의 숫자 형태로만 입력 가능합니다."); + NOT_MULTIPLY_ERROR("%d의 배수만 입력 가능합니다."), + NOT_LIST_WITH_COMMA_ERROR("콤마로 구분된 %d개의 숫자 형태로만 입력 가능합니다."), + NOT_VALID_ELEMENTS_NUM("리스트의 원소 개수는 %d개여야 합니다."), + NOT_UNIQUE_ELEMENTS("리스트에 중복 원소가 존재합니다."); private final String message; diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java index 512db32d8b..d09e58feac 100644 --- a/src/main/java/lotto/InputValidator.java +++ b/src/main/java/lotto/InputValidator.java @@ -14,13 +14,13 @@ public void validateIsNumeric(final String buyAmountInput) { public void validateMultiplyOfThousand(final Integer buyAmountInput) { if (buyAmountInput % Number.THOUSAND.toValue() != Number.ZERO.toValue()) { - throw new IllegalArgumentException(ErrorMessage.NOT_MULTIPLY_OF_THOUSAND_ERROR.toString()); + throw new IllegalArgumentException(String.format(ErrorMessage.NOT_MULTIPLY_ERROR.toString(), Number.THOUSAND)); } } public void validateIsListWithComma(final String input) { if (!Pattern.matches(LIST_WITH_COMMA_MATCHER, input)) { - throw new IllegalArgumentException(ErrorMessage.NOT_LIST_WITH_COMMA_ERROR.toString()); + throw new IllegalArgumentException(String.format(ErrorMessage.NOT_LIST_WITH_COMMA_ERROR.toString(), Number.NUMBER_NUM)); } } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index ad35225cc8..85bf925731 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,5 +1,6 @@ package lotto; +import java.util.HashSet; import java.util.List; public class Lotto { @@ -11,8 +12,20 @@ public Lotto(final List numbers) { } private void validate(final List numbers) { + validateSize(numbers); + validateUnique(numbers); + } + + private void validateUnique(final List numbers) { + if (numbers.size() != new HashSet<>(numbers).size()) { + throw new IllegalArgumentException(ErrorMessage.NOT_UNIQUE_ELEMENTS.toString()); + } + } + + private void validateSize(final List numbers) { if (numbers.size() != Number.NUMBER_NUM.toValue()) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException( + String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), Number.NUMBER_NUM.toValue())); } } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 52aa7221de..2bb384e7ff 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -19,7 +19,10 @@ public void saveLotto() { } private List createRandomNumbers() { - return Randoms.pickUniqueNumbersInRange(Number.MIN_RANGE.toValue(), Number.MAX_RANGE.toValue(), Number.NUMBER_NUM.toValue()); + return Randoms.pickUniqueNumbersInRange( + Number.MIN_RANGE.toValue(), + Number.MAX_RANGE.toValue(), + Number.NUMBER_NUM.toValue()); } public Integer getPublishNum(final Integer buyAmount) { @@ -31,16 +34,22 @@ private Integer divideByThousand(final Integer dividend) { } public List publishLottoTickets(final Integer publishNum) { - final List lottoTickets = IntStream.range(Number.ZERO.toValue(), publishNum) + final List lottoTickets = createLottoTickets(publishNum); + lottoRepository.saveLottoTicket(lottoTickets); + return lottoTickets; + } + + private List createLottoTickets(final Integer publishNum) { + return IntStream.range(Number.ZERO.toValue(), publishNum) .mapToObj(i -> createRandomNumbers()) .map(LottoTicket::new) .collect(Collectors.toList()); - - lottoRepository.saveLottoTicket(lottoTickets); - return lottoTickets; } - public WinningNumber saveWinningNumber(final List winningNumberInput, final Integer bonusNumberInput) { + public WinningNumber saveWinningNumber( + final List winningNumberInput, + final Integer bonusNumberInput) { + final WinningNumber winningNumber = new WinningNumber(winningNumberInput, bonusNumberInput); lottoRepository.saveWinningNumber(winningNumber); return winningNumber; diff --git a/src/main/java/lotto/LottoTicket.java b/src/main/java/lotto/LottoTicket.java index f501d174f1..02ed44f829 100644 --- a/src/main/java/lotto/LottoTicket.java +++ b/src/main/java/lotto/LottoTicket.java @@ -1,15 +1,35 @@ package lotto; import java.util.Collections; +import java.util.HashSet; import java.util.List; public class LottoTicket { private final List numbers; public LottoTicket(final List numbers) { + validate(numbers); this.numbers = Collections.unmodifiableList(numbers); } + private void validate(final List numbers) { + validateSize(numbers); + validateUnique(numbers); + } + + private void validateUnique(final List numbers) { + if (numbers.size() != new HashSet<>(numbers).size()) { + throw new IllegalArgumentException(ErrorMessage.NOT_UNIQUE_ELEMENTS.toString()); + } + } + + private void validateSize(final List numbers) { + if (numbers.size() != Number.NUMBER_NUM.toValue()) { + throw new IllegalArgumentException( + String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), Number.NUMBER_NUM.toValue())); + } + } + @Override public String toString() { return this.numbers.toString(); diff --git a/src/main/java/lotto/WinningNumber.java b/src/main/java/lotto/WinningNumber.java index 23a38d16b4..8dbc2fb365 100644 --- a/src/main/java/lotto/WinningNumber.java +++ b/src/main/java/lotto/WinningNumber.java @@ -1,5 +1,6 @@ package lotto; +import java.util.HashSet; import java.util.List; public class WinningNumber { @@ -7,7 +8,26 @@ public class WinningNumber { private final Integer bonusNumber; public WinningNumber(final List winningNumbers, final Integer bonusNumber) { + validate(winningNumbers); this.winningNumbers = winningNumbers; this.bonusNumber = bonusNumber; } + + private void validate(final List numbers) { + validateSize(numbers); + validateUnique(numbers); + } + + private void validateUnique(final List numbers) { + if (numbers.size() != new HashSet<>(numbers).size()) { + throw new IllegalArgumentException(ErrorMessage.NOT_UNIQUE_ELEMENTS.toString()); + } + } + + private void validateSize(final List numbers) { + if (numbers.size() != Number.NUMBER_NUM.toValue()) { + throw new IllegalArgumentException( + String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), Number.NUMBER_NUM.toValue())); + } + } } diff --git a/src/test/java/lotto/InputValidatorTest.java b/src/test/java/lotto/InputValidatorTest.java new file mode 100644 index 0000000000..b6ab0e2091 --- /dev/null +++ b/src/test/java/lotto/InputValidatorTest.java @@ -0,0 +1,70 @@ +package lotto; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@DisplayName("InputValidator의 검증 기능 중") +class InputValidatorTest { + private InputValidator inputValidator = new InputValidator(); + + @Nested + @DisplayName("숫자 검증에서") + class ValidateIsNumeric { + @Test + @DisplayName("숫자를 입력했을때가 검증되는가") + void validateNumber() { + inputValidator.validateIsNumeric("123"); + } + + @Test + @DisplayName("일반 문자열을 입력했을때가 검증되는가") + void validateNonNumber() { + assertThatThrownBy(() -> inputValidator.validateIsNumeric("abc")) + .isInstanceOf(IllegalArgumentException.class); + } + } + + @Nested + @DisplayName("1000의 배수 검증에서") + class ValidateMultiplyOfThousand { + @Test + @DisplayName("1000의 배수를 입력했을떄가 검증되는가") + void validateMultiplyOfThousand() { + inputValidator.validateMultiplyOfThousand(1000); + } + + @Test + @DisplayName("1000의 배수가 아닌 수를 입력했을떄가 검증되는가") + void validateAnyNumber() { + assertThatThrownBy(() -> inputValidator.validateMultiplyOfThousand(1001)) + .isInstanceOf(IllegalArgumentException.class); + } + } + + @Nested + @DisplayName("당첨 번호 입력값 검증에서") + class ValidateIsListWithComma { + @Test + @DisplayName("정상 입력이 검증되는가") + void validateRightInput() { + inputValidator.validateIsListWithComma("1,2,3,4,5,6"); + } + + @Test + @DisplayName("비정상 입력이 검증되는가") + void validateWrongInputWithMissingNumber() { + assertThatThrownBy(() -> inputValidator.validateIsListWithComma("1,2,3,4,5,")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("비정상 입력이 검증되는가") + void validateWrongInput() { + assertThatThrownBy(() -> inputValidator.validateIsListWithComma("1,2,3,4,5 ,6")) + .isInstanceOf(IllegalArgumentException.class); + } + } +} diff --git a/src/test/java/lotto/LottoRepositoryTest.java b/src/test/java/lotto/LottoRepositoryTest.java new file mode 100644 index 0000000000..ba9956c8de --- /dev/null +++ b/src/test/java/lotto/LottoRepositoryTest.java @@ -0,0 +1,47 @@ +package lotto; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +@DisplayName("LottoRepository 기능중") +class LottoRepositoryTest { + private LottoRepository lottoRepository = new LottoRepository(); + + @Test + @DisplayName("Lotto 저장 기능이 수행되는가") + void saveLotto() { + //given + + //when + lottoRepository.saveLotto(new Lotto(List.of(1, 2, 3, 4, 5, 6))); + + //then + + } + + @Test + @DisplayName("LottoTicket 저장 기능이 수행되는가") + void saveLottoTicket() { + //given + + //when + lottoRepository.saveLottoTicket(List.of(new LottoTicket(List.of(1, 2, 3, 4, 5, 6)))); + + //then + + } + + @Test + @DisplayName("WinningNumber 저장 기능이 수행되는가") + void saveWinningNumber() { + //given + + //when + lottoRepository.saveWinningNumber(new WinningNumber(List.of(1, 2, 3, 4, 5, 6), 7)); + + //then + + } +} diff --git a/src/test/java/lotto/LottoServiceTest.java b/src/test/java/lotto/LottoServiceTest.java new file mode 100644 index 0000000000..2d702e0e4f --- /dev/null +++ b/src/test/java/lotto/LottoServiceTest.java @@ -0,0 +1,62 @@ +package lotto; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.*; + +@DisplayName("LottoService의 서비스 로직 중") +class LottoServiceTest { + private LottoService lottoService = new LottoService(new LottoRepository()); + + + @Test + @DisplayName("로또 저장 로직이 수행되는가") + void saveLotto() { + //given + + //when + lottoService.saveLotto(); + + //then + + } + + @Test + @DisplayName("발행 번호 조회가 수행되는가") + void getPublishNum() { + //given + + //when + final Integer publishNum = lottoService.getPublishNum(1000); + + //then + assertThat(publishNum).isEqualTo(1); + } + + @Test + @DisplayName("로또 티켓 발행이 수행되는가") + void publishLottoTickets() { + //given + + //when + final List lottoTickets = lottoService.publishLottoTickets(1); + + //then + assertThat(lottoTickets).hasSize(1); + } + + @Test + @DisplayName("당첨 번호 저장이 수행되는가") + void saveWinningNumber() { + //given + + //when + final WinningNumber winningNumber = lottoService.saveWinningNumber(List.of(1, 2, 3, 4, 5, 6), 7); + + //then + + } +} From 393fc6bdbed14c890797c09ee8c1dbddc82d66cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Sun, 6 Aug 2023 19:16:12 +0900 Subject: [PATCH 16/26] =?UTF-8?q?refactor:=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20=EB=B0=8F=20enum=20=EC=9D=B4=EB=A6=84?= =?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 리팩토링 - LottoTicket 삭제 - Number 이름 LottoNumberRange로 수정 --- docs/README.md | 37 +++++++++---------- src/main/java/lotto/FrontController.java | 6 +-- src/main/java/lotto/InputValidator.java | 8 ++-- src/main/java/lotto/Lotto.java | 9 +++-- src/main/java/lotto/LottoController.java | 20 +++++----- src/main/java/lotto/LottoMessage.java | 4 +- .../{Number.java => LottoNumberRange.java} | 8 +--- src/main/java/lotto/LottoRepository.java | 9 +---- src/main/java/lotto/LottoService.java | 36 ++++++++---------- src/main/java/lotto/LottoTicket.java | 37 ------------------- src/main/java/lotto/OutputManager.java | 8 +++- src/main/java/lotto/WinningNumber.java | 4 +- src/test/java/lotto/LottoRepositoryTest.java | 14 +------ src/test/java/lotto/LottoServiceTest.java | 21 ++--------- 14 files changed, 78 insertions(+), 143 deletions(-) rename src/main/java/lotto/{Number.java => LottoNumberRange.java} (70%) delete mode 100644 src/main/java/lotto/LottoTicket.java diff --git a/docs/README.md b/docs/README.md index 0ace48db5c..10970118a3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,21 +1,19 @@ # 구현할 기능 목록 -1. 랜덤 번호 생성 및 저장 기능 -2. 구입 금액 입력 기능 -3. 구입 금액 입력값 검증 기능 -4. 로또 발행 개수 계산 기능 -5. 로또 발행 기능 -6. 발행한 로또 수량 및 번호 출력 기능 -7. 당첨 번호 입력 기능 -8. 당첨 번호 입력값 검증 기능 -9. 번호 일치 여부 확인 및 당첨 여부 확인 기능 -10. 당첨 내역 출력 기능 -11. 수익률 계산 및 반올림 기능 -12. 수익률 출력 기능 -13. 예외 처리 및 에러 문구 출력 기능 +[x] 랜덤 번호 생성 및 저장 기능 +[x] 구입 금액 입력 기능 +[x] 구입 금액 입력값 검증 기능 +[x] 로또 발행 개수 계산 기능 +[x] 로또 발행 기능 +[x] 발행한 로또 수량 및 번호 출력 기능 +[x] 당첨 번호 입력 기능 +[x] 당첨 번호 입력값 검증 기능 +[ ] 번호 일치 여부 확인 및 당첨 여부 확인 기능 +[ ] 당첨 내역 출력 기능 +[ ] 수익률 계산 및 반올림 기능 +[ ] 수익률 출력 기능 +[ ] 예외 처리 및 에러 문구 출력 기능 # 구현할 클래스 목록 -## LottoTicket - 발행한 로또 티켓 - ## WinningNumber - 당첨 번호 ## FrontController - 프론트 컨트롤러 @@ -24,18 +22,17 @@ ## LottoController - 로또 컨트롤러 - saveLotto() 로또 저장 - getPublishNum() 로또 발행량 계산 -- publishLottoTickets() 로또 티켓 발행 +- publishLotto() 로또 티켓 발행 - getWinningNumber() 당첨 번호 및 보너스 번호 저장후 조회 ## LottoService - 로또 서비스 - saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 - getPublishNum() 로또 발행량 계산 -- publishLottoTickets() 로또 티켓 발행 +- publishLotto() 로또 티켓 발행 - saveWinningNumber() 당첨 번호 저장 ## LottoRepository - 로또 레포지토리 - saveLotto() Lotto 객체 저장 -- saveLottoTicket() LottoTicket 객체 저장 - saveWinningNumber() WinningNumber 객체 저장 ## InputManager - 입력 받기 @@ -51,7 +48,7 @@ ## OutputManager - 출력하기 - printStartMessage() 로또 시작 메시지 출력 - printPublishNum() 로또 발행 개수 메시지 출력 -- printLottoTickets() 발행한 로또 티켓 출력 +- printLottoList() 발행한 로또 티켓 출력 - printCreateWinningNumber() 당첨 번호 입력 메시지 출력 - printCreateBonusNumber() 보너스 번호 입력 메시지 출력 @@ -62,4 +59,4 @@ ## ErrorMessage - 에러 메시지 -## Number - 로또 생성 범위 및 로또 실행에 필요한 숫자 \ No newline at end of file +## LottoNumberRange - 로또 생성 범위 diff --git a/src/main/java/lotto/FrontController.java b/src/main/java/lotto/FrontController.java index 35bef06032..f7613c9585 100644 --- a/src/main/java/lotto/FrontController.java +++ b/src/main/java/lotto/FrontController.java @@ -8,9 +8,9 @@ public FrontController(final LottoController lottoController) { } public void playLotto() { - lottoController.saveLotto(); final Integer publishNum = lottoController.getPublishNum(); - lottoController.publishLottoTickets(publishNum); - final WinningNumber winningNumber = lottoController.getWinningNumber(); + lottoController.publishLotto(publishNum); + lottoController.getWinningNumber(); + lottoController.checkWinningNumber(); } } diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java index d09e58feac..3c6df6a08b 100644 --- a/src/main/java/lotto/InputValidator.java +++ b/src/main/java/lotto/InputValidator.java @@ -5,6 +5,8 @@ public class InputValidator { private static final String NUMERIC_MATCHER = "-?\\d+"; private static final String LIST_WITH_COMMA_MATCHER = "\\d+,\\d+,\\d+,\\d+,\\d+,\\d+"; + private static final Integer MONEY_UNIT = 1000; + private static final Integer NONE = 0; public void validateIsNumeric(final String buyAmountInput) { if (!buyAmountInput.matches(NUMERIC_MATCHER)) { @@ -13,14 +15,14 @@ public void validateIsNumeric(final String buyAmountInput) { } public void validateMultiplyOfThousand(final Integer buyAmountInput) { - if (buyAmountInput % Number.THOUSAND.toValue() != Number.ZERO.toValue()) { - throw new IllegalArgumentException(String.format(ErrorMessage.NOT_MULTIPLY_ERROR.toString(), Number.THOUSAND)); + if (buyAmountInput % MONEY_UNIT != NONE) { + throw new IllegalArgumentException(String.format(ErrorMessage.NOT_MULTIPLY_ERROR.toString(), MONEY_UNIT)); } } public void validateIsListWithComma(final String input) { if (!Pattern.matches(LIST_WITH_COMMA_MATCHER, input)) { - throw new IllegalArgumentException(String.format(ErrorMessage.NOT_LIST_WITH_COMMA_ERROR.toString(), Number.NUMBER_NUM)); + throw new IllegalArgumentException(String.format(ErrorMessage.NOT_LIST_WITH_COMMA_ERROR.toString(), LottoNumberRange.NUMBER_NUM)); } } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 85bf925731..d4b6e41c8f 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -23,11 +23,14 @@ private void validateUnique(final List numbers) { } private void validateSize(final List numbers) { - if (numbers.size() != Number.NUMBER_NUM.toValue()) { + if (numbers.size() != LottoNumberRange.NUMBER_NUM.toValue()) { throw new IllegalArgumentException( - String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), Number.NUMBER_NUM.toValue())); + String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), LottoNumberRange.NUMBER_NUM.toValue())); } } - // TODO: 추가 기능 구현 + @Override + public String toString() { + return this.numbers.toString(); + } } diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index ed1f96174e..013a094949 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -13,30 +13,32 @@ public LottoController(final LottoService lottoService, final OutputManager outp this.inputManager = inputManager; } - public void saveLotto() { - lottoService.saveLotto(); - } - public Integer getPublishNum() { outputManager.printStartMessage(); + final Integer buyAmount = inputManager.buyAmountInput(); final Integer publishNum = lottoService.getPublishNum(buyAmount); + outputManager.printPublishNum(publishNum); return publishNum; } - public void publishLottoTickets(final Integer publishNum) { - final List lottoTickets = lottoService.publishLottoTickets(publishNum); - outputManager.printLottoTickets(lottoTickets); + public void publishLotto(final Integer publishNum) { + final List lottoList = lottoService.publishLotto(publishNum); + outputManager.printLottoList(lottoList); } - public WinningNumber getWinningNumber() { + public void getWinningNumber() { outputManager.printCreateWinningNumber(); final List winningNumberInput = inputManager.winningNumberInput(); outputManager.printCreateBonusNumber(); final Integer bonusNumberInput = inputManager.bonusNumberInput(); - return lottoService.saveWinningNumber(winningNumberInput, bonusNumberInput); + lottoService.saveWinningNumber(winningNumberInput, bonusNumberInput); + } + + public void checkWinningNumber() { + outputManager.printWinningStatistics(); } } diff --git a/src/main/java/lotto/LottoMessage.java b/src/main/java/lotto/LottoMessage.java index 862afe5fae..42d274dbc6 100644 --- a/src/main/java/lotto/LottoMessage.java +++ b/src/main/java/lotto/LottoMessage.java @@ -4,7 +4,9 @@ public enum LottoMessage { START_MESSAGE("구입금액을 입력해 주세요."), PUBLISH_NUM_MESSAGE("%d개를 구매했습니다."), CREATE_WINNING_NUMBER_MESSAGE("당첨 번호를 입력해 주세요."), - CREATE_BONUS_NUMBER_MESSAGE("보너스 번호를 입력해 주세요."); + CREATE_BONUS_NUMBER_MESSAGE("보너스 번호를 입력해 주세요."), + WINNING_STATISTICS("당첨 통계\n" + "---"), + NUM_OF_MATCH("%d개 일치 (%d원) - %d개"); private final String message; diff --git a/src/main/java/lotto/Number.java b/src/main/java/lotto/LottoNumberRange.java similarity index 70% rename from src/main/java/lotto/Number.java rename to src/main/java/lotto/LottoNumberRange.java index 3c52e12fba..ad64002b36 100644 --- a/src/main/java/lotto/Number.java +++ b/src/main/java/lotto/LottoNumberRange.java @@ -1,16 +1,12 @@ package lotto; -public enum Number { - ZERO(0), - - THOUSAND(1000), +public enum LottoNumberRange { MIN_RANGE(1), MAX_RANGE(45), NUMBER_NUM(6); - private final int value; - Number(int value) { + LottoNumberRange(int value) { this.value = value; } diff --git a/src/main/java/lotto/LottoRepository.java b/src/main/java/lotto/LottoRepository.java index e4bff19dfb..6774d3168e 100644 --- a/src/main/java/lotto/LottoRepository.java +++ b/src/main/java/lotto/LottoRepository.java @@ -3,18 +3,13 @@ import java.util.List; public class LottoRepository { - private Lotto lotto; - private List lottoTicket; + private List lotto; private WinningNumber winningNumber; - public void saveLotto(final Lotto lotto) { + public void saveAllLotto(final List lotto) { this.lotto = lotto; } - public void saveLottoTicket(final List lottoTicket) { - this.lottoTicket = lottoTicket; - } - public void saveWinningNumber(final WinningNumber winningNumber) { this.winningNumber = winningNumber; } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 2bb384e7ff..258cec6a83 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -7,51 +7,47 @@ import java.util.stream.IntStream; public class LottoService { + private static final Integer MONEY_UNIT = 1000; + private static final Integer ZERO = 0; private final LottoRepository lottoRepository; public LottoService(final LottoRepository lottoRepository) { this.lottoRepository = lottoRepository; } - public void saveLotto() { - final Lotto lotto = new Lotto(createRandomNumbers()); - lottoRepository.saveLotto(lotto); - } - private List createRandomNumbers() { return Randoms.pickUniqueNumbersInRange( - Number.MIN_RANGE.toValue(), - Number.MAX_RANGE.toValue(), - Number.NUMBER_NUM.toValue()); + LottoNumberRange.MIN_RANGE.toValue(), + LottoNumberRange.MAX_RANGE.toValue(), + LottoNumberRange.NUMBER_NUM.toValue()); } public Integer getPublishNum(final Integer buyAmount) { - return divideByThousand(buyAmount); + return divideByMoneyUnit(buyAmount); } - private Integer divideByThousand(final Integer dividend) { - return dividend / Number.THOUSAND.toValue(); + private Integer divideByMoneyUnit(final Integer dividend) { + return dividend / MONEY_UNIT; } - public List publishLottoTickets(final Integer publishNum) { - final List lottoTickets = createLottoTickets(publishNum); - lottoRepository.saveLottoTicket(lottoTickets); - return lottoTickets; + public List publishLotto(final Integer publishNum) { + final List lottoList = createLottoList(publishNum); + lottoRepository.saveAllLotto(lottoList); + return lottoList; } - private List createLottoTickets(final Integer publishNum) { - return IntStream.range(Number.ZERO.toValue(), publishNum) + private List createLottoList(final Integer publishNum) { + return IntStream.range(ZERO, publishNum) .mapToObj(i -> createRandomNumbers()) - .map(LottoTicket::new) + .map(Lotto::new) .collect(Collectors.toList()); } - public WinningNumber saveWinningNumber( + public void saveWinningNumber( final List winningNumberInput, final Integer bonusNumberInput) { final WinningNumber winningNumber = new WinningNumber(winningNumberInput, bonusNumberInput); lottoRepository.saveWinningNumber(winningNumber); - return winningNumber; } } diff --git a/src/main/java/lotto/LottoTicket.java b/src/main/java/lotto/LottoTicket.java deleted file mode 100644 index 02ed44f829..0000000000 --- a/src/main/java/lotto/LottoTicket.java +++ /dev/null @@ -1,37 +0,0 @@ -package lotto; - -import java.util.Collections; -import java.util.HashSet; -import java.util.List; - -public class LottoTicket { - private final List numbers; - - public LottoTicket(final List numbers) { - validate(numbers); - this.numbers = Collections.unmodifiableList(numbers); - } - - private void validate(final List numbers) { - validateSize(numbers); - validateUnique(numbers); - } - - private void validateUnique(final List numbers) { - if (numbers.size() != new HashSet<>(numbers).size()) { - throw new IllegalArgumentException(ErrorMessage.NOT_UNIQUE_ELEMENTS.toString()); - } - } - - private void validateSize(final List numbers) { - if (numbers.size() != Number.NUMBER_NUM.toValue()) { - throw new IllegalArgumentException( - String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), Number.NUMBER_NUM.toValue())); - } - } - - @Override - public String toString() { - return this.numbers.toString(); - } -} diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index eaef331c2a..57786bff17 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -12,8 +12,8 @@ public void printPublishNum(final Integer publishNum) { System.out.println(String.format(LottoMessage.PUBLISH_NUM_MESSAGE.toString(), publishNum)); } - public void printLottoTickets(final List lottoTickets) { - lottoTickets.forEach(System.out::println); + public void printLottoList(final List lottoList) { + lottoList.forEach(System.out::println); } public void printCreateWinningNumber() { @@ -23,4 +23,8 @@ public void printCreateWinningNumber() { public void printCreateBonusNumber() { System.out.println(LottoMessage.CREATE_BONUS_NUMBER_MESSAGE); } + + public void printWinningStatistics() { + System.out.println(LottoMessage.WINNING_STATISTICS); + } } diff --git a/src/main/java/lotto/WinningNumber.java b/src/main/java/lotto/WinningNumber.java index 8dbc2fb365..b84c76529d 100644 --- a/src/main/java/lotto/WinningNumber.java +++ b/src/main/java/lotto/WinningNumber.java @@ -25,9 +25,9 @@ private void validateUnique(final List numbers) { } private void validateSize(final List numbers) { - if (numbers.size() != Number.NUMBER_NUM.toValue()) { + if (numbers.size() != LottoNumberRange.NUMBER_NUM.toValue()) { throw new IllegalArgumentException( - String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), Number.NUMBER_NUM.toValue())); + String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), LottoNumberRange.NUMBER_NUM.toValue())); } } } diff --git a/src/test/java/lotto/LottoRepositoryTest.java b/src/test/java/lotto/LottoRepositoryTest.java index ba9956c8de..e49fc4d4ec 100644 --- a/src/test/java/lotto/LottoRepositoryTest.java +++ b/src/test/java/lotto/LottoRepositoryTest.java @@ -15,19 +15,7 @@ void saveLotto() { //given //when - lottoRepository.saveLotto(new Lotto(List.of(1, 2, 3, 4, 5, 6))); - - //then - - } - - @Test - @DisplayName("LottoTicket 저장 기능이 수행되는가") - void saveLottoTicket() { - //given - - //when - lottoRepository.saveLottoTicket(List.of(new LottoTicket(List.of(1, 2, 3, 4, 5, 6)))); + lottoRepository.saveAllLotto(List.of(new Lotto(List.of(1, 2, 3, 4, 5, 6)))); //then diff --git a/src/test/java/lotto/LottoServiceTest.java b/src/test/java/lotto/LottoServiceTest.java index 2d702e0e4f..6e7b021631 100644 --- a/src/test/java/lotto/LottoServiceTest.java +++ b/src/test/java/lotto/LottoServiceTest.java @@ -11,19 +11,6 @@ class LottoServiceTest { private LottoService lottoService = new LottoService(new LottoRepository()); - - @Test - @DisplayName("로또 저장 로직이 수행되는가") - void saveLotto() { - //given - - //when - lottoService.saveLotto(); - - //then - - } - @Test @DisplayName("발행 번호 조회가 수행되는가") void getPublishNum() { @@ -38,14 +25,14 @@ void getPublishNum() { @Test @DisplayName("로또 티켓 발행이 수행되는가") - void publishLottoTickets() { + void publishLotto() { //given //when - final List lottoTickets = lottoService.publishLottoTickets(1); + final List lottoList = lottoService.publishLotto(1); //then - assertThat(lottoTickets).hasSize(1); + assertThat(lottoList).hasSize(1); } @Test @@ -54,7 +41,7 @@ void saveWinningNumber() { //given //when - final WinningNumber winningNumber = lottoService.saveWinningNumber(List.of(1, 2, 3, 4, 5, 6), 7); + lottoService.saveWinningNumber(List.of(1, 2, 3, 4, 5, 6), 7); //then From 344755a3b55c150dd5da85cba398109a0c0d6c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 00:30:01 +0900 Subject: [PATCH 17/26] =?UTF-8?q?feat:=20=EA=B2=B0=EA=B3=BC=20=EB=8F=84?= =?UTF-8?q?=EC=B6=9C=20=EA=B8=B0=EB=8A=A5=20=EB=B0=8F=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 계산을 통한 결과 도출 - 결과 출력 --- docs/README.md | 20 +++++++++ src/main/java/lotto/ErrorMessage.java | 3 +- src/main/java/lotto/InputManager.java | 7 +-- src/main/java/lotto/InputValidator.java | 2 +- src/main/java/lotto/Lotto.java | 4 ++ src/main/java/lotto/LottoController.java | 2 + src/main/java/lotto/LottoMessage.java | 3 +- src/main/java/lotto/LottoRepository.java | 8 ++++ src/main/java/lotto/LottoService.java | 8 +++- src/main/java/lotto/OutputManager.java | 28 ++++++++++++ src/main/java/lotto/Result.java | 32 ++++++++++++++ src/main/java/lotto/ResultMap.java | 35 +++++++++++++++ src/main/java/lotto/RewardMoney.java | 20 +++++++++ src/main/java/lotto/RewardMoneyMap.java | 56 ++++++++++++++++++++++++ src/main/java/lotto/WinningNumber.java | 44 +++++++++++++------ 15 files changed, 249 insertions(+), 23 deletions(-) create mode 100644 src/main/java/lotto/Result.java create mode 100644 src/main/java/lotto/ResultMap.java create mode 100644 src/main/java/lotto/RewardMoney.java create mode 100644 src/main/java/lotto/RewardMoneyMap.java diff --git a/docs/README.md b/docs/README.md index 10970118a3..eb0ca8cee6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -15,6 +15,10 @@ # 구현할 클래스 목록 ## WinningNumber - 당첨 번호 +- compareWithLottoList() 당첨 번호와 로또 번호 비교 + +## Lotto - 로또 정보 +- getNumbers() 로또 번호 조회 ## FrontController - 프론트 컨트롤러 - playLotto() 로또 플레이 @@ -30,10 +34,13 @@ - getPublishNum() 로또 발행량 계산 - publishLotto() 로또 티켓 발행 - saveWinningNumber() 당첨 번호 저장 +- checkWinningNumber() 당점 정보 비교 및 결과 계산 ## LottoRepository - 로또 레포지토리 - saveLotto() Lotto 객체 저장 - saveWinningNumber() WinningNumber 객체 저장 +- findAllLotto() 로또 전체 조회 +- findWinningNumber() 당첨 번호 조회 ## InputManager - 입력 받기 - buyAmountInput() 구매 금액 입력 @@ -51,6 +58,17 @@ - printLottoList() 발행한 로또 티켓 출력 - printCreateWinningNumber() 당첨 번호 입력 메시지 출력 - printCreateBonusNumber() 보너스 번호 입력 메시지 출력 +- printResult() 결과 정보 출력 + +## ResultMap - 결과 종류 마다의 개수 저장 +- getResultMap() 결과 관련 map 조회 + +## Result - 결과 단건에 대한 정보 저장 +- getRewardMoney() RewardMoney 정보 조회 + +## RewardMoneyMap - 당첨금 관련 매핑 정보 저장 +- getRewardMoney() 개수에 따른 RewardMoney 조회 +- getMatchNum() RewardMoney에 따른 개수 조회 ## ComponentFactory - 컴포넌트들의 생성 @@ -60,3 +78,5 @@ ## ErrorMessage - 에러 메시지 ## LottoNumberRange - 로또 생성 범위 + +## RewardMoney - 당첨금 정보 diff --git a/src/main/java/lotto/ErrorMessage.java b/src/main/java/lotto/ErrorMessage.java index 0fcc706e97..95aa390358 100644 --- a/src/main/java/lotto/ErrorMessage.java +++ b/src/main/java/lotto/ErrorMessage.java @@ -6,7 +6,8 @@ public enum ErrorMessage { NOT_MULTIPLY_ERROR("%d의 배수만 입력 가능합니다."), NOT_LIST_WITH_COMMA_ERROR("콤마로 구분된 %d개의 숫자 형태로만 입력 가능합니다."), NOT_VALID_ELEMENTS_NUM("리스트의 원소 개수는 %d개여야 합니다."), - NOT_UNIQUE_ELEMENTS("리스트에 중복 원소가 존재합니다."); + NOT_UNIQUE_ELEMENTS("리스트에 중복 원소가 존재합니다."), + CANNOT_FIND_RESULT("결과를 찾을 수 없습니다."); private final String message; diff --git a/src/main/java/lotto/InputManager.java b/src/main/java/lotto/InputManager.java index e57e7716fe..b48c315165 100644 --- a/src/main/java/lotto/InputManager.java +++ b/src/main/java/lotto/InputManager.java @@ -26,8 +26,7 @@ public Integer buyAmountInput() { public List winningNumberInput() { final String input = Console.readLine(); inputValidator.validateIsListWithComma(input); - final List winningNumber = convertToList(input); - return winningNumber; + return convertToList(input); } private List convertToList(final String input) { @@ -39,8 +38,6 @@ private List convertToList(final String input) { public Integer bonusNumberInput() { final String input = Console.readLine(); inputValidator.validateIsNumeric(input); - - final Integer inputNumber = Integer.valueOf(input); - return inputNumber; + return Integer.valueOf(input); } } diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java index 3c6df6a08b..f0fce0a261 100644 --- a/src/main/java/lotto/InputValidator.java +++ b/src/main/java/lotto/InputValidator.java @@ -5,8 +5,8 @@ public class InputValidator { private static final String NUMERIC_MATCHER = "-?\\d+"; private static final String LIST_WITH_COMMA_MATCHER = "\\d+,\\d+,\\d+,\\d+,\\d+,\\d+"; - private static final Integer MONEY_UNIT = 1000; private static final Integer NONE = 0; + private static final Integer MONEY_UNIT = 1000; public void validateIsNumeric(final String buyAmountInput) { if (!buyAmountInput.matches(NUMERIC_MATCHER)) { diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index d4b6e41c8f..3c3068aba4 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -29,6 +29,10 @@ private void validateSize(final List numbers) { } } + public List getNumbers() { + return this.numbers; + } + @Override public String toString() { return this.numbers.toString(); diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 013a094949..a437082287 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -40,5 +40,7 @@ public void getWinningNumber() { public void checkWinningNumber() { outputManager.printWinningStatistics(); + final ResultMap resultMap = lottoService.checkWinningNumber(); + outputManager.printResult(resultMap); } } diff --git a/src/main/java/lotto/LottoMessage.java b/src/main/java/lotto/LottoMessage.java index 42d274dbc6..f3af2ffe88 100644 --- a/src/main/java/lotto/LottoMessage.java +++ b/src/main/java/lotto/LottoMessage.java @@ -6,7 +6,8 @@ public enum LottoMessage { CREATE_WINNING_NUMBER_MESSAGE("당첨 번호를 입력해 주세요."), CREATE_BONUS_NUMBER_MESSAGE("보너스 번호를 입력해 주세요."), WINNING_STATISTICS("당첨 통계\n" + "---"), - NUM_OF_MATCH("%d개 일치 (%d원) - %d개"); + NUM_OF_MATCH("%d개 일치 (%s원) - %d개"), + BONUS_MATCH("%d개 일치, 보너스 볼 일치 (%s원) - %d개"); private final String message; diff --git a/src/main/java/lotto/LottoRepository.java b/src/main/java/lotto/LottoRepository.java index 6774d3168e..ef77b1550b 100644 --- a/src/main/java/lotto/LottoRepository.java +++ b/src/main/java/lotto/LottoRepository.java @@ -13,4 +13,12 @@ public void saveAllLotto(final List lotto) { public void saveWinningNumber(final WinningNumber winningNumber) { this.winningNumber = winningNumber; } + + public List findAllLotto() { + return this.lotto; + } + + public WinningNumber findWinningNumber() { + return this.winningNumber; + } } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 258cec6a83..dff9134e28 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -7,8 +7,8 @@ import java.util.stream.IntStream; public class LottoService { - private static final Integer MONEY_UNIT = 1000; private static final Integer ZERO = 0; + private static final Integer MONEY_UNIT = 1000; private final LottoRepository lottoRepository; public LottoService(final LottoRepository lottoRepository) { @@ -50,4 +50,10 @@ public void saveWinningNumber( final WinningNumber winningNumber = new WinningNumber(winningNumberInput, bonusNumberInput); lottoRepository.saveWinningNumber(winningNumber); } + + public ResultMap checkWinningNumber() { + final List lottoList = lottoRepository.findAllLotto(); + final WinningNumber winningNumber = lottoRepository.findWinningNumber(); + return winningNumber.compareWithLottoList(lottoList); + } } diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index 57786bff17..7f2a914de0 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -1,8 +1,14 @@ package lotto; +import java.text.NumberFormat; import java.util.List; public class OutputManager { + private final NumberFormat numberFormat; + + public OutputManager() { + this.numberFormat = NumberFormat.getNumberInstance(); + } public void printStartMessage() { System.out.println(LottoMessage.START_MESSAGE); @@ -27,4 +33,26 @@ public void printCreateBonusNumber() { public void printWinningStatistics() { System.out.println(LottoMessage.WINNING_STATISTICS); } + + public void printResult(final ResultMap resultMap) { + resultMap.getResultMap().forEach(((result, numOfResult) ->{ + if (!result.getRewardMoney().equals(RewardMoney.NONE)) { + printResultByMoney(result, numOfResult); + } + })); + } + + private void printResultByMoney(final Result result, final Integer numOfResult) { + RewardMoneyMap rewardMoneyMap = RewardMoneyMap.getInstance(); + + if (result.getRewardMoney().equals(RewardMoney.MATCH_WITH_BONUS)) { + System.out.println(String.format( + LottoMessage.BONUS_MATCH.toString(), + rewardMoneyMap.getMatchNum(result.getRewardMoney()), numberFormat.format(result.getRewardMoney().toValue()), numOfResult)); + return; + } + System.out.println(String.format( + LottoMessage.NUM_OF_MATCH.toString(), + rewardMoneyMap.getMatchNum(result.getRewardMoney()), numberFormat.format(result.getRewardMoney().toValue()), numOfResult)); + } } diff --git a/src/main/java/lotto/Result.java b/src/main/java/lotto/Result.java new file mode 100644 index 0000000000..19b3709b5a --- /dev/null +++ b/src/main/java/lotto/Result.java @@ -0,0 +1,32 @@ +package lotto; + +import java.util.Objects; + +public class Result { + private final RewardMoney rewardMoney; + private final Integer matchNum; + private final boolean isBonusMatch; + + public Result(final RewardMoney rewardMoney, final Integer matchNum, final boolean isBonusMatch) { + this.rewardMoney = rewardMoney; + this.matchNum = matchNum; + this.isBonusMatch = isBonusMatch; + } + + public RewardMoney getRewardMoney() { + return this.rewardMoney; + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Result result = (Result) o; + return isBonusMatch == result.isBonusMatch && rewardMoney == result.rewardMoney; + } + + @Override + public int hashCode() { + return Objects.hash(rewardMoney, isBonusMatch); + } +} diff --git a/src/main/java/lotto/ResultMap.java b/src/main/java/lotto/ResultMap.java new file mode 100644 index 0000000000..dbd150a421 --- /dev/null +++ b/src/main/java/lotto/ResultMap.java @@ -0,0 +1,35 @@ +package lotto; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class ResultMap { + private static final Integer NONE = 0; + private static final Integer ONE = 1; + private final Map resultMap = new LinkedHashMap<>(); + + public ResultMap(final List resultList) { + initializeResultMap(); + putData(resultList); + } + + private void initializeResultMap() { + Arrays.stream(RewardMoney.values()).forEach(rewardMoney -> { + if (!rewardMoney.equals(RewardMoney.MATCH_WITH_BONUS)) { + this.resultMap.put(new Result(rewardMoney, NONE, false), NONE); + return; + } + this.resultMap.put(new Result(rewardMoney, NONE, true), NONE); + }); + } + + private void putData(final List resultList) { + resultList.forEach(result -> this.resultMap.put(result, this.resultMap.get(result) + ONE)); + } + + public Map getResultMap() { + return this.resultMap; + } +} diff --git a/src/main/java/lotto/RewardMoney.java b/src/main/java/lotto/RewardMoney.java new file mode 100644 index 0000000000..afeaf1b136 --- /dev/null +++ b/src/main/java/lotto/RewardMoney.java @@ -0,0 +1,20 @@ +package lotto; + +public enum RewardMoney { + THREE_MATCH(5_000L), + FOUR_MATCH(50_000L), + FIVE_MATCH(1_500_000L), + MATCH_WITH_BONUS(30_000_000L), + SIX_MATCH(20_000_000_000L), + NONE(0L); + + private final Long money; + + RewardMoney(final Long money) { + this.money = money; + } + + public Long toValue() { + return this.money; + } +} diff --git a/src/main/java/lotto/RewardMoneyMap.java b/src/main/java/lotto/RewardMoneyMap.java new file mode 100644 index 0000000000..c93469779d --- /dev/null +++ b/src/main/java/lotto/RewardMoneyMap.java @@ -0,0 +1,56 @@ +package lotto; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class RewardMoneyMap { + private static final Integer DEFAULT_INDEX = 0; + private static final Integer BONUS_INDEX = 1; + private Map> moneyMap = new HashMap<>(); + private static RewardMoneyMap rewardMoneyMap; + + public static RewardMoneyMap getInstance() { + if (rewardMoneyMap == null) { + rewardMoneyMap = new RewardMoneyMap(); + } + return rewardMoneyMap; + } + + private RewardMoneyMap() { + initializeMoneyMap(); + } + + private void initializeMoneyMap() { + moneyMap.put(0, List.of(RewardMoney.NONE)); + moneyMap.put(1, List.of(RewardMoney.NONE)); + moneyMap.put(2, List.of(RewardMoney.NONE)); + moneyMap.put(3, List.of(RewardMoney.THREE_MATCH)); + moneyMap.put(4, List.of(RewardMoney.FOUR_MATCH)); + moneyMap.put(5, List.of(RewardMoney.FIVE_MATCH, RewardMoney.MATCH_WITH_BONUS)); + moneyMap.put(6, List.of(RewardMoney.SIX_MATCH)); + } + + public RewardMoney getRewardMoney(final int matchNum, final boolean isBonusMatch) { + final List rewardMoneyList = moneyMap.get(matchNum); + if (matchNum != 5) { + return rewardMoneyList.get(DEFAULT_INDEX); + } + return checkBonus(rewardMoneyList, isBonusMatch); + } + + public Integer getMatchNum(final RewardMoney rewardMoney) { + return moneyMap.entrySet().stream() + .filter(entry -> entry.getValue().contains(rewardMoney)) + .map(Map.Entry::getKey) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(ErrorMessage.CANNOT_FIND_RESULT.toString())); + } + + private RewardMoney checkBonus(final List rewardMoneyList, final boolean isBonusMatch) { + if (isBonusMatch) { + return rewardMoneyList.get(BONUS_INDEX); + } + return rewardMoneyList.get(DEFAULT_INDEX); + } +} diff --git a/src/main/java/lotto/WinningNumber.java b/src/main/java/lotto/WinningNumber.java index b84c76529d..8c6f452cc0 100644 --- a/src/main/java/lotto/WinningNumber.java +++ b/src/main/java/lotto/WinningNumber.java @@ -1,33 +1,49 @@ package lotto; -import java.util.HashSet; import java.util.List; +import java.util.stream.Collectors; public class WinningNumber { - private final List winningNumbers; + private static final Integer BONUS_AVAILABLE = 5; + private final Lotto winningNumbers; private final Integer bonusNumber; public WinningNumber(final List winningNumbers, final Integer bonusNumber) { - validate(winningNumbers); - this.winningNumbers = winningNumbers; + this.winningNumbers = new Lotto(winningNumbers); this.bonusNumber = bonusNumber; } - private void validate(final List numbers) { - validateSize(numbers); - validateUnique(numbers); + public ResultMap compareWithLottoList(final List lottoList) { + final List winningNumbers = this.winningNumbers.getNumbers(); + winningNumbers.add(bonusNumber); + + final List resultList = lottoList.stream() + .map(Lotto::getNumbers) + .peek(lotto -> lotto.retainAll(winningNumbers)) + .map(this::getResult) + .collect(Collectors.toList()); + + return new ResultMap(resultList); + } + + private Result getResult(final List lotto) { + final boolean isBonusMatch = lotto.contains(bonusNumber); + final int matchNum = getMatchNum(isBonusMatch, lotto.size()); + return getResultWithMatchNum(matchNum, isBonusMatch); } - private void validateUnique(final List numbers) { - if (numbers.size() != new HashSet<>(numbers).size()) { - throw new IllegalArgumentException(ErrorMessage.NOT_UNIQUE_ELEMENTS.toString()); + private Result getResultWithMatchNum(final int matchNum, final boolean isBonusMatch) { + final RewardMoneyMap rewardMoneyMap = RewardMoneyMap.getInstance(); + if (matchNum != BONUS_AVAILABLE) { + return new Result(rewardMoneyMap.getRewardMoney(matchNum, false), matchNum, false); } + return new Result(rewardMoneyMap.getRewardMoney(matchNum, isBonusMatch), matchNum, isBonusMatch); } - private void validateSize(final List numbers) { - if (numbers.size() != LottoNumberRange.NUMBER_NUM.toValue()) { - throw new IllegalArgumentException( - String.format(ErrorMessage.NOT_VALID_ELEMENTS_NUM.toString(), LottoNumberRange.NUMBER_NUM.toValue())); + private int getMatchNum(final boolean isBonusMatch, final Integer size) { + if (isBonusMatch) { + return size - 1; } + return size; } } From 764d6a9827a1db04985501448ed7dc74ed109558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 00:37:30 +0900 Subject: [PATCH 18/26] =?UTF-8?q?feat:=20=EB=B0=9C=ED=96=89=EB=9F=89=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 발행량 저장, 조회 기능 --- docs/README.md | 4 +++- src/main/java/lotto/FrontController.java | 4 ++-- src/main/java/lotto/LottoController.java | 9 ++++----- src/main/java/lotto/LottoRepository.java | 9 +++++++++ src/main/java/lotto/LottoService.java | 11 +++++++---- src/test/java/lotto/LottoServiceTest.java | 4 ++-- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/docs/README.md b/docs/README.md index eb0ca8cee6..f838581ee7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -31,7 +31,7 @@ ## LottoService - 로또 서비스 - saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 -- getPublishNum() 로또 발행량 계산 +- savePublishNum() 로또 발행량 계산 및 저장 - publishLotto() 로또 티켓 발행 - saveWinningNumber() 당첨 번호 저장 - checkWinningNumber() 당점 정보 비교 및 결과 계산 @@ -41,6 +41,8 @@ - saveWinningNumber() WinningNumber 객체 저장 - findAllLotto() 로또 전체 조회 - findWinningNumber() 당첨 번호 조회 +- savePublishNumber() 발행량 저장 +- findPublishNumber() 발행량 조회 ## InputManager - 입력 받기 - buyAmountInput() 구매 금액 입력 diff --git a/src/main/java/lotto/FrontController.java b/src/main/java/lotto/FrontController.java index f7613c9585..95da5252fb 100644 --- a/src/main/java/lotto/FrontController.java +++ b/src/main/java/lotto/FrontController.java @@ -8,8 +8,8 @@ public FrontController(final LottoController lottoController) { } public void playLotto() { - final Integer publishNum = lottoController.getPublishNum(); - lottoController.publishLotto(publishNum); + lottoController.getPublishNum(); + lottoController.publishLotto(); lottoController.getWinningNumber(); lottoController.checkWinningNumber(); } diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index a437082287..a13d6c0fe9 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -13,18 +13,17 @@ public LottoController(final LottoService lottoService, final OutputManager outp this.inputManager = inputManager; } - public Integer getPublishNum() { + public void getPublishNum() { outputManager.printStartMessage(); final Integer buyAmount = inputManager.buyAmountInput(); - final Integer publishNum = lottoService.getPublishNum(buyAmount); + final Integer publishNum = lottoService.savePublishNum(buyAmount); outputManager.printPublishNum(publishNum); - return publishNum; } - public void publishLotto(final Integer publishNum) { - final List lottoList = lottoService.publishLotto(publishNum); + public void publishLotto() { + final List lottoList = lottoService.publishLotto(); outputManager.printLottoList(lottoList); } diff --git a/src/main/java/lotto/LottoRepository.java b/src/main/java/lotto/LottoRepository.java index ef77b1550b..228db07b5e 100644 --- a/src/main/java/lotto/LottoRepository.java +++ b/src/main/java/lotto/LottoRepository.java @@ -5,6 +5,15 @@ public class LottoRepository { private List lotto; private WinningNumber winningNumber; + private Integer publishNumber; + + public void savePublishNumber(final Integer publishNumber) { + this.publishNumber = publishNumber; + } + + public Integer findPublishNumber() { + return this.publishNumber; + } public void saveAllLotto(final List lotto) { this.lotto = lotto; diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index dff9134e28..ebe63964b0 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -22,16 +22,19 @@ private List createRandomNumbers() { LottoNumberRange.NUMBER_NUM.toValue()); } - public Integer getPublishNum(final Integer buyAmount) { - return divideByMoneyUnit(buyAmount); + public Integer savePublishNum(final Integer buyAmount) { + final Integer publishNumber = divideByMoneyUnit(buyAmount); + lottoRepository.savePublishNumber(publishNumber); + return publishNumber; } private Integer divideByMoneyUnit(final Integer dividend) { return dividend / MONEY_UNIT; } - public List publishLotto(final Integer publishNum) { - final List lottoList = createLottoList(publishNum); + public List publishLotto() { + final Integer publishNumber = lottoRepository.findPublishNumber(); + final List lottoList = createLottoList(publishNumber); lottoRepository.saveAllLotto(lottoList); return lottoList; } diff --git a/src/test/java/lotto/LottoServiceTest.java b/src/test/java/lotto/LottoServiceTest.java index 6e7b021631..cb0631ba73 100644 --- a/src/test/java/lotto/LottoServiceTest.java +++ b/src/test/java/lotto/LottoServiceTest.java @@ -17,7 +17,7 @@ void getPublishNum() { //given //when - final Integer publishNum = lottoService.getPublishNum(1000); + final Integer publishNum = lottoService.savePublishNum(1000); //then assertThat(publishNum).isEqualTo(1); @@ -29,7 +29,7 @@ void publishLotto() { //given //when - final List lottoList = lottoService.publishLotto(1); + final List lottoList = lottoService.publishLotto(); //then assertThat(lottoList).hasSize(1); From 3bb834ef56d564777baf1e66a143c84b7ff3e22c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 01:14:07 +0900 Subject: [PATCH 19/26] =?UTF-8?q?feat:=20=EC=88=98=EC=9D=B5=EB=A5=A0=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 수익률 조회 - 수익률 출력 --- docs/README.md | 6 ++++++ src/main/java/lotto/ErrorMessage.java | 3 ++- src/main/java/lotto/FrontController.java | 1 + src/main/java/lotto/InputManager.java | 9 ++++++--- src/main/java/lotto/InputValidator.java | 6 ++++++ src/main/java/lotto/LottoController.java | 7 ++++++- src/main/java/lotto/LottoMessage.java | 11 ++++++----- src/main/java/lotto/LottoRepository.java | 9 +++++++++ src/main/java/lotto/LottoService.java | 21 +++++++++++++++++++-- src/main/java/lotto/OutputManager.java | 12 ++++++++---- src/main/java/lotto/ResultMap.java | 6 ++++++ src/test/java/lotto/LottoServiceTest.java | 2 +- 12 files changed, 76 insertions(+), 17 deletions(-) diff --git a/docs/README.md b/docs/README.md index f838581ee7..9ae5c40519 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,6 +28,7 @@ - getPublishNum() 로또 발행량 계산 - publishLotto() 로또 티켓 발행 - getWinningNumber() 당첨 번호 및 보너스 번호 저장후 조회 +- checkEarningRate() 수익률 조회 ## LottoService - 로또 서비스 - saveLotto() 랜덤한 숫자 생성을 통한 Lotto 생성 및 저장 @@ -35,6 +36,7 @@ - publishLotto() 로또 티켓 발행 - saveWinningNumber() 당첨 번호 저장 - checkWinningNumber() 당점 정보 비교 및 결과 계산 +- checkEarningRate() 수익률 조회 ## LottoRepository - 로또 레포지토리 - saveLotto() Lotto 객체 저장 @@ -43,6 +45,8 @@ - findWinningNumber() 당첨 번호 조회 - savePublishNumber() 발행량 저장 - findPublishNumber() 발행량 조회 +- saveRewardAmount() 당첨금 저장 +- findRewardAmount() 당첨금 조회 ## InputManager - 입력 받기 - buyAmountInput() 구매 금액 입력 @@ -61,9 +65,11 @@ - printCreateWinningNumber() 당첨 번호 입력 메시지 출력 - printCreateBonusNumber() 보너스 번호 입력 메시지 출력 - printResult() 결과 정보 출력 +- printEarningRate() 수익률 출력 ## ResultMap - 결과 종류 마다의 개수 저장 - getResultMap() 결과 관련 map 조회 +- getRewardAmount() 최종 얻은 수익 계산 ## Result - 결과 단건에 대한 정보 저장 - getRewardMoney() RewardMoney 정보 조회 diff --git a/src/main/java/lotto/ErrorMessage.java b/src/main/java/lotto/ErrorMessage.java index 95aa390358..94ace08286 100644 --- a/src/main/java/lotto/ErrorMessage.java +++ b/src/main/java/lotto/ErrorMessage.java @@ -7,7 +7,8 @@ public enum ErrorMessage { NOT_LIST_WITH_COMMA_ERROR("콤마로 구분된 %d개의 숫자 형태로만 입력 가능합니다."), NOT_VALID_ELEMENTS_NUM("리스트의 원소 개수는 %d개여야 합니다."), NOT_UNIQUE_ELEMENTS("리스트에 중복 원소가 존재합니다."), - CANNOT_FIND_RESULT("결과를 찾을 수 없습니다."); + CANNOT_FIND_RESULT("결과를 찾을 수 없습니다."), + NOT_VALID_LOTTO_NUMBER_ERROR("로또 번호의 범위는 최소 1, 최대 45입니다."); private final String message; diff --git a/src/main/java/lotto/FrontController.java b/src/main/java/lotto/FrontController.java index 95da5252fb..04a28b451a 100644 --- a/src/main/java/lotto/FrontController.java +++ b/src/main/java/lotto/FrontController.java @@ -12,5 +12,6 @@ public void playLotto() { lottoController.publishLotto(); lottoController.getWinningNumber(); lottoController.checkWinningNumber(); + lottoController.checkEarningRate(); } } diff --git a/src/main/java/lotto/InputManager.java b/src/main/java/lotto/InputManager.java index b48c315165..fa3a58a607 100644 --- a/src/main/java/lotto/InputManager.java +++ b/src/main/java/lotto/InputManager.java @@ -7,6 +7,7 @@ import java.util.stream.Collectors; public class InputManager { + private static final String COMMA_SPLITTER = ","; private final InputValidator inputValidator; public InputManager(final InputValidator inputValidator) { @@ -19,7 +20,6 @@ public Integer buyAmountInput() { final Integer inputNumber = Integer.valueOf(input); inputValidator.validateMultiplyOfThousand(inputNumber); - return inputNumber; } @@ -30,14 +30,17 @@ public List winningNumberInput() { } private List convertToList(final String input) { - return Arrays.stream(input.split(",")) + return Arrays.stream(input.split(COMMA_SPLITTER)) .map(Integer::valueOf) + .peek(inputValidator::validateLottoNumber) .collect(Collectors.toList()); } public Integer bonusNumberInput() { final String input = Console.readLine(); inputValidator.validateIsNumeric(input); - return Integer.valueOf(input); + final Integer bonusNumber = Integer.valueOf(input); + inputValidator.validateLottoNumber(bonusNumber); + return bonusNumber; } } diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java index f0fce0a261..08f59e6ec7 100644 --- a/src/main/java/lotto/InputValidator.java +++ b/src/main/java/lotto/InputValidator.java @@ -25,4 +25,10 @@ public void validateIsListWithComma(final String input) { throw new IllegalArgumentException(String.format(ErrorMessage.NOT_LIST_WITH_COMMA_ERROR.toString(), LottoNumberRange.NUMBER_NUM)); } } + + public void validateLottoNumber(final Integer lottoNumberInput) { + if (lottoNumberInput < LottoNumberRange.MIN_RANGE.toValue() || lottoNumberInput > LottoNumberRange.MAX_RANGE.toValue()) { + throw new IllegalArgumentException(String.format(ErrorMessage.NOT_VALID_LOTTO_NUMBER_ERROR.toString())); + } + } } diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index a13d6c0fe9..6dfe5c4934 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -17,7 +17,7 @@ public void getPublishNum() { outputManager.printStartMessage(); final Integer buyAmount = inputManager.buyAmountInput(); - final Integer publishNum = lottoService.savePublishNum(buyAmount); + final Integer publishNum = lottoService.savePublishNumber(buyAmount); outputManager.printPublishNum(publishNum); } @@ -42,4 +42,9 @@ public void checkWinningNumber() { final ResultMap resultMap = lottoService.checkWinningNumber(); outputManager.printResult(resultMap); } + + public void checkEarningRate() { + final Double earningRate = lottoService.checkEarningRate(); + outputManager.printEarningRate(earningRate); + } } diff --git a/src/main/java/lotto/LottoMessage.java b/src/main/java/lotto/LottoMessage.java index f3af2ffe88..3e8b097fa5 100644 --- a/src/main/java/lotto/LottoMessage.java +++ b/src/main/java/lotto/LottoMessage.java @@ -1,13 +1,14 @@ package lotto; public enum LottoMessage { - START_MESSAGE("구입금액을 입력해 주세요."), - PUBLISH_NUM_MESSAGE("%d개를 구매했습니다."), - CREATE_WINNING_NUMBER_MESSAGE("당첨 번호를 입력해 주세요."), - CREATE_BONUS_NUMBER_MESSAGE("보너스 번호를 입력해 주세요."), + BUY_TO_START("구입금액을 입력해 주세요."), + PUBLISH_NUM("%d개를 구매했습니다."), + CREATE_WINNING_NUMBER("당첨 번호를 입력해 주세요."), + CREATE_BONUS_NUMBER("보너스 번호를 입력해 주세요."), WINNING_STATISTICS("당첨 통계\n" + "---"), NUM_OF_MATCH("%d개 일치 (%s원) - %d개"), - BONUS_MATCH("%d개 일치, 보너스 볼 일치 (%s원) - %d개"); + BONUS_MATCH("%d개 일치, 보너스 볼 일치 (%s원) - %d개"), + EARNING_RATE("총 수익률은 %.1f%%입니다."); private final String message; diff --git a/src/main/java/lotto/LottoRepository.java b/src/main/java/lotto/LottoRepository.java index 228db07b5e..34c4cccd0f 100644 --- a/src/main/java/lotto/LottoRepository.java +++ b/src/main/java/lotto/LottoRepository.java @@ -6,6 +6,7 @@ public class LottoRepository { private List lotto; private WinningNumber winningNumber; private Integer publishNumber; + private Long rewardAmount; public void savePublishNumber(final Integer publishNumber) { this.publishNumber = publishNumber; @@ -30,4 +31,12 @@ public List findAllLotto() { public WinningNumber findWinningNumber() { return this.winningNumber; } + + public void saveRewardAmount(final Long rewardAmount) { + this.rewardAmount = rewardAmount; + } + + public Long findRewardAmount() { + return this.rewardAmount; + } } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index ebe63964b0..76d7bed894 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -9,6 +9,7 @@ public class LottoService { private static final Integer ZERO = 0; private static final Integer MONEY_UNIT = 1000; + private static final Double PERCENTAGE = 100.0; private final LottoRepository lottoRepository; public LottoService(final LottoRepository lottoRepository) { @@ -22,7 +23,7 @@ private List createRandomNumbers() { LottoNumberRange.NUMBER_NUM.toValue()); } - public Integer savePublishNum(final Integer buyAmount) { + public Integer savePublishNumber(final Integer buyAmount) { final Integer publishNumber = divideByMoneyUnit(buyAmount); lottoRepository.savePublishNumber(publishNumber); return publishNumber; @@ -57,6 +58,22 @@ public void saveWinningNumber( public ResultMap checkWinningNumber() { final List lottoList = lottoRepository.findAllLotto(); final WinningNumber winningNumber = lottoRepository.findWinningNumber(); - return winningNumber.compareWithLottoList(lottoList); + final ResultMap resultMap = winningNumber.compareWithLottoList(lottoList); + lottoRepository.saveRewardAmount(resultMap.getRewardAmount()); + return resultMap; + } + + public Double checkEarningRate() { + final long earnedAmount = lottoRepository.findRewardAmount(); + final int investmentAmount = getInvestment(); + return getEarningRate(earnedAmount, investmentAmount); + } + + private double getEarningRate(final long earnedAmount, final int investmentAmount) { + return ((double)earnedAmount / (double)investmentAmount) * PERCENTAGE; + } + + private int getInvestment() { + return lottoRepository.findPublishNumber() * MONEY_UNIT; } } diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index 7f2a914de0..9049dafd9f 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -11,11 +11,11 @@ public OutputManager() { } public void printStartMessage() { - System.out.println(LottoMessage.START_MESSAGE); + System.out.println(LottoMessage.BUY_TO_START); } public void printPublishNum(final Integer publishNum) { - System.out.println(String.format(LottoMessage.PUBLISH_NUM_MESSAGE.toString(), publishNum)); + System.out.println(String.format(LottoMessage.PUBLISH_NUM.toString(), publishNum)); } public void printLottoList(final List lottoList) { @@ -23,11 +23,11 @@ public void printLottoList(final List lottoList) { } public void printCreateWinningNumber() { - System.out.println(LottoMessage.CREATE_WINNING_NUMBER_MESSAGE); + System.out.println(LottoMessage.CREATE_WINNING_NUMBER); } public void printCreateBonusNumber() { - System.out.println(LottoMessage.CREATE_BONUS_NUMBER_MESSAGE); + System.out.println(LottoMessage.CREATE_BONUS_NUMBER); } public void printWinningStatistics() { @@ -55,4 +55,8 @@ private void printResultByMoney(final Result result, final Integer numOfResult) LottoMessage.NUM_OF_MATCH.toString(), rewardMoneyMap.getMatchNum(result.getRewardMoney()), numberFormat.format(result.getRewardMoney().toValue()), numOfResult)); } + + public void printEarningRate(final Double earningRate) { + System.out.println(String.format(LottoMessage.EARNING_RATE.toString(), earningRate)); + } } diff --git a/src/main/java/lotto/ResultMap.java b/src/main/java/lotto/ResultMap.java index dbd150a421..b437b51223 100644 --- a/src/main/java/lotto/ResultMap.java +++ b/src/main/java/lotto/ResultMap.java @@ -32,4 +32,10 @@ private void putData(final List resultList) { public Map getResultMap() { return this.resultMap; } + + public Long getRewardAmount() { + return this.resultMap.keySet().stream() + .map(result -> result.getRewardMoney().toValue() * resultMap.get(result)) + .reduce(0L, Long::sum); + } } diff --git a/src/test/java/lotto/LottoServiceTest.java b/src/test/java/lotto/LottoServiceTest.java index cb0631ba73..5b10911c74 100644 --- a/src/test/java/lotto/LottoServiceTest.java +++ b/src/test/java/lotto/LottoServiceTest.java @@ -17,7 +17,7 @@ void getPublishNum() { //given //when - final Integer publishNum = lottoService.savePublishNum(1000); + final Integer publishNum = lottoService.savePublishNumber(1000); //then assertThat(publishNum).isEqualTo(1); From 20d47c6e33262bdba51e7fb99ae22f379970bd36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 01:37:18 +0900 Subject: [PATCH 20/26] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 테스트 오류 수정 - 1위 숫자 자릿수 수정 - ArrayList 생성 추가 --- src/main/java/lotto/ErrorMessage.java | 2 +- src/main/java/lotto/RewardMoney.java | 2 +- src/main/java/lotto/WinningNumber.java | 2 ++ src/test/java/lotto/LottoServiceTest.java | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/ErrorMessage.java b/src/main/java/lotto/ErrorMessage.java index 94ace08286..92cc75ecab 100644 --- a/src/main/java/lotto/ErrorMessage.java +++ b/src/main/java/lotto/ErrorMessage.java @@ -18,6 +18,6 @@ public enum ErrorMessage { @Override public String toString() { - return ERROR_PREFIX.message + message; + return ERROR_PREFIX.message + this.message; } } diff --git a/src/main/java/lotto/RewardMoney.java b/src/main/java/lotto/RewardMoney.java index afeaf1b136..0d7500f05e 100644 --- a/src/main/java/lotto/RewardMoney.java +++ b/src/main/java/lotto/RewardMoney.java @@ -5,7 +5,7 @@ public enum RewardMoney { FOUR_MATCH(50_000L), FIVE_MATCH(1_500_000L), MATCH_WITH_BONUS(30_000_000L), - SIX_MATCH(20_000_000_000L), + SIX_MATCH(2_000_000_000L), NONE(0L); private final Long money; diff --git a/src/main/java/lotto/WinningNumber.java b/src/main/java/lotto/WinningNumber.java index 8c6f452cc0..ad9e98381d 100644 --- a/src/main/java/lotto/WinningNumber.java +++ b/src/main/java/lotto/WinningNumber.java @@ -1,5 +1,6 @@ package lotto; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -19,6 +20,7 @@ public ResultMap compareWithLottoList(final List lottoList) { final List resultList = lottoList.stream() .map(Lotto::getNumbers) + .map(ArrayList::new) .peek(lotto -> lotto.retainAll(winningNumbers)) .map(this::getResult) .collect(Collectors.toList()); diff --git a/src/test/java/lotto/LottoServiceTest.java b/src/test/java/lotto/LottoServiceTest.java index 5b10911c74..62f538ed20 100644 --- a/src/test/java/lotto/LottoServiceTest.java +++ b/src/test/java/lotto/LottoServiceTest.java @@ -27,6 +27,7 @@ void getPublishNum() { @DisplayName("로또 티켓 발행이 수행되는가") void publishLotto() { //given + lottoService.savePublishNumber(1000); //when final List lottoList = lottoService.publishLotto(); From b748063abe1e7bcecce8c7c43ded0195935a6bbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 01:48:21 +0900 Subject: [PATCH 21/26] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 리팩토링 - Result 클래스 삭제 --- docs/README.md | 3 -- src/main/java/lotto/OutputManager.java | 10 +++--- src/main/java/lotto/Result.java | 32 ------------------ src/main/java/lotto/ResultMap.java | 21 +++++------- src/main/java/lotto/WinningNumber.java | 16 ++++----- src/test/java/lotto/LottoRepositoryTest.java | 35 -------------------- src/test/java/lotto/LottoServiceTest.java | 15 ++------- 7 files changed, 24 insertions(+), 108 deletions(-) delete mode 100644 src/main/java/lotto/Result.java delete mode 100644 src/test/java/lotto/LottoRepositoryTest.java diff --git a/docs/README.md b/docs/README.md index 9ae5c40519..5f43d9285e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -71,9 +71,6 @@ - getResultMap() 결과 관련 map 조회 - getRewardAmount() 최종 얻은 수익 계산 -## Result - 결과 단건에 대한 정보 저장 -- getRewardMoney() RewardMoney 정보 조회 - ## RewardMoneyMap - 당첨금 관련 매핑 정보 저장 - getRewardMoney() 개수에 따른 RewardMoney 조회 - getMatchNum() RewardMoney에 따른 개수 조회 diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index 9049dafd9f..bf1764c6f5 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -36,24 +36,24 @@ public void printWinningStatistics() { public void printResult(final ResultMap resultMap) { resultMap.getResultMap().forEach(((result, numOfResult) ->{ - if (!result.getRewardMoney().equals(RewardMoney.NONE)) { + if (!result.equals(RewardMoney.NONE)) { printResultByMoney(result, numOfResult); } })); } - private void printResultByMoney(final Result result, final Integer numOfResult) { + private void printResultByMoney(final RewardMoney rewardMoney, final Integer numOfResult) { RewardMoneyMap rewardMoneyMap = RewardMoneyMap.getInstance(); - if (result.getRewardMoney().equals(RewardMoney.MATCH_WITH_BONUS)) { + if (rewardMoney.equals(RewardMoney.MATCH_WITH_BONUS)) { System.out.println(String.format( LottoMessage.BONUS_MATCH.toString(), - rewardMoneyMap.getMatchNum(result.getRewardMoney()), numberFormat.format(result.getRewardMoney().toValue()), numOfResult)); + rewardMoneyMap.getMatchNum(rewardMoney), numberFormat.format(rewardMoney.toValue()), numOfResult)); return; } System.out.println(String.format( LottoMessage.NUM_OF_MATCH.toString(), - rewardMoneyMap.getMatchNum(result.getRewardMoney()), numberFormat.format(result.getRewardMoney().toValue()), numOfResult)); + rewardMoneyMap.getMatchNum(rewardMoney), numberFormat.format(rewardMoney.toValue()), numOfResult)); } public void printEarningRate(final Double earningRate) { diff --git a/src/main/java/lotto/Result.java b/src/main/java/lotto/Result.java deleted file mode 100644 index 19b3709b5a..0000000000 --- a/src/main/java/lotto/Result.java +++ /dev/null @@ -1,32 +0,0 @@ -package lotto; - -import java.util.Objects; - -public class Result { - private final RewardMoney rewardMoney; - private final Integer matchNum; - private final boolean isBonusMatch; - - public Result(final RewardMoney rewardMoney, final Integer matchNum, final boolean isBonusMatch) { - this.rewardMoney = rewardMoney; - this.matchNum = matchNum; - this.isBonusMatch = isBonusMatch; - } - - public RewardMoney getRewardMoney() { - return this.rewardMoney; - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - final Result result = (Result) o; - return isBonusMatch == result.isBonusMatch && rewardMoney == result.rewardMoney; - } - - @Override - public int hashCode() { - return Objects.hash(rewardMoney, isBonusMatch); - } -} diff --git a/src/main/java/lotto/ResultMap.java b/src/main/java/lotto/ResultMap.java index b437b51223..276a713672 100644 --- a/src/main/java/lotto/ResultMap.java +++ b/src/main/java/lotto/ResultMap.java @@ -1,16 +1,13 @@ package lotto; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class ResultMap { private static final Integer NONE = 0; private static final Integer ONE = 1; - private final Map resultMap = new LinkedHashMap<>(); + private final Map resultMap = new EnumMap<>(RewardMoney.class); - public ResultMap(final List resultList) { + public ResultMap(final List resultList) { initializeResultMap(); putData(resultList); } @@ -18,24 +15,24 @@ public ResultMap(final List resultList) { private void initializeResultMap() { Arrays.stream(RewardMoney.values()).forEach(rewardMoney -> { if (!rewardMoney.equals(RewardMoney.MATCH_WITH_BONUS)) { - this.resultMap.put(new Result(rewardMoney, NONE, false), NONE); + this.resultMap.put(rewardMoney, NONE); return; } - this.resultMap.put(new Result(rewardMoney, NONE, true), NONE); + this.resultMap.put(rewardMoney, NONE); }); } - private void putData(final List resultList) { - resultList.forEach(result -> this.resultMap.put(result, this.resultMap.get(result) + ONE)); + private void putData(final List rewardMoneyList) { + rewardMoneyList.forEach(result -> this.resultMap.put(result, this.resultMap.get(result) + ONE)); } - public Map getResultMap() { + public Map getResultMap() { return this.resultMap; } public Long getRewardAmount() { return this.resultMap.keySet().stream() - .map(result -> result.getRewardMoney().toValue() * resultMap.get(result)) + .map(rewardMoney -> rewardMoney.toValue() * resultMap.get(rewardMoney)) .reduce(0L, Long::sum); } } diff --git a/src/main/java/lotto/WinningNumber.java b/src/main/java/lotto/WinningNumber.java index ad9e98381d..5bfe87591c 100644 --- a/src/main/java/lotto/WinningNumber.java +++ b/src/main/java/lotto/WinningNumber.java @@ -15,31 +15,31 @@ public WinningNumber(final List winningNumbers, final Integer bonusNumb } public ResultMap compareWithLottoList(final List lottoList) { - final List winningNumbers = this.winningNumbers.getNumbers(); - winningNumbers.add(bonusNumber); + final List numbers = this.winningNumbers.getNumbers(); + numbers.add(bonusNumber); - final List resultList = lottoList.stream() + final List resultList = lottoList.stream() .map(Lotto::getNumbers) .map(ArrayList::new) - .peek(lotto -> lotto.retainAll(winningNumbers)) + .peek(lotto -> lotto.retainAll(numbers)) .map(this::getResult) .collect(Collectors.toList()); return new ResultMap(resultList); } - private Result getResult(final List lotto) { + private RewardMoney getResult(final List lotto) { final boolean isBonusMatch = lotto.contains(bonusNumber); final int matchNum = getMatchNum(isBonusMatch, lotto.size()); return getResultWithMatchNum(matchNum, isBonusMatch); } - private Result getResultWithMatchNum(final int matchNum, final boolean isBonusMatch) { + private RewardMoney getResultWithMatchNum(final int matchNum, final boolean isBonusMatch) { final RewardMoneyMap rewardMoneyMap = RewardMoneyMap.getInstance(); if (matchNum != BONUS_AVAILABLE) { - return new Result(rewardMoneyMap.getRewardMoney(matchNum, false), matchNum, false); + return rewardMoneyMap.getRewardMoney(matchNum, false); } - return new Result(rewardMoneyMap.getRewardMoney(matchNum, isBonusMatch), matchNum, isBonusMatch); + return rewardMoneyMap.getRewardMoney(matchNum, isBonusMatch); } private int getMatchNum(final boolean isBonusMatch, final Integer size) { diff --git a/src/test/java/lotto/LottoRepositoryTest.java b/src/test/java/lotto/LottoRepositoryTest.java deleted file mode 100644 index e49fc4d4ec..0000000000 --- a/src/test/java/lotto/LottoRepositoryTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package lotto; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -@DisplayName("LottoRepository 기능중") -class LottoRepositoryTest { - private LottoRepository lottoRepository = new LottoRepository(); - - @Test - @DisplayName("Lotto 저장 기능이 수행되는가") - void saveLotto() { - //given - - //when - lottoRepository.saveAllLotto(List.of(new Lotto(List.of(1, 2, 3, 4, 5, 6)))); - - //then - - } - - @Test - @DisplayName("WinningNumber 저장 기능이 수행되는가") - void saveWinningNumber() { - //given - - //when - lottoRepository.saveWinningNumber(new WinningNumber(List.of(1, 2, 3, 4, 5, 6), 7)); - - //then - - } -} diff --git a/src/test/java/lotto/LottoServiceTest.java b/src/test/java/lotto/LottoServiceTest.java index 62f538ed20..21c2745206 100644 --- a/src/test/java/lotto/LottoServiceTest.java +++ b/src/test/java/lotto/LottoServiceTest.java @@ -9,7 +9,8 @@ @DisplayName("LottoService의 서비스 로직 중") class LottoServiceTest { - private LottoService lottoService = new LottoService(new LottoRepository()); + private LottoRepository lottoRepository = new LottoRepository(); + private LottoService lottoService = new LottoService(lottoRepository); @Test @DisplayName("발행 번호 조회가 수행되는가") @@ -35,16 +36,4 @@ void publishLotto() { //then assertThat(lottoList).hasSize(1); } - - @Test - @DisplayName("당첨 번호 저장이 수행되는가") - void saveWinningNumber() { - //given - - //when - lottoService.saveWinningNumber(List.of(1, 2, 3, 4, 5, 6), 7); - - //then - - } } From e322f898821de8553f9147f02202b8e9215235e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 01:51:39 +0900 Subject: [PATCH 22/26] =?UTF-8?q?refactor:=20=EC=83=81=EC=88=98=20?= =?UTF-8?q?=EB=AF=B8=EC=A0=81=EC=9A=A9=20=EB=B6=80=EB=B6=84=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 리팩토링 - 숫자 하드코딩 대신 상수 적용 --- src/main/java/lotto/ResultMap.java | 8 ++++---- src/main/java/lotto/WinningNumber.java | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/lotto/ResultMap.java b/src/main/java/lotto/ResultMap.java index 276a713672..c4d478904b 100644 --- a/src/main/java/lotto/ResultMap.java +++ b/src/main/java/lotto/ResultMap.java @@ -3,7 +3,7 @@ import java.util.*; public class ResultMap { - private static final Integer NONE = 0; + private static final Long NONE = 0L; private static final Integer ONE = 1; private final Map resultMap = new EnumMap<>(RewardMoney.class); @@ -15,10 +15,10 @@ public ResultMap(final List resultList) { private void initializeResultMap() { Arrays.stream(RewardMoney.values()).forEach(rewardMoney -> { if (!rewardMoney.equals(RewardMoney.MATCH_WITH_BONUS)) { - this.resultMap.put(rewardMoney, NONE); + this.resultMap.put(rewardMoney, NONE.intValue()); return; } - this.resultMap.put(rewardMoney, NONE); + this.resultMap.put(rewardMoney, NONE.intValue()); }); } @@ -33,6 +33,6 @@ public Map getResultMap() { public Long getRewardAmount() { return this.resultMap.keySet().stream() .map(rewardMoney -> rewardMoney.toValue() * resultMap.get(rewardMoney)) - .reduce(0L, Long::sum); + .reduce(NONE, Long::sum); } } diff --git a/src/main/java/lotto/WinningNumber.java b/src/main/java/lotto/WinningNumber.java index 5bfe87591c..a9c005d2cd 100644 --- a/src/main/java/lotto/WinningNumber.java +++ b/src/main/java/lotto/WinningNumber.java @@ -5,6 +5,7 @@ import java.util.stream.Collectors; public class WinningNumber { + private static final Integer ONE = 1; private static final Integer BONUS_AVAILABLE = 5; private final Lotto winningNumbers; private final Integer bonusNumber; @@ -44,7 +45,7 @@ private RewardMoney getResultWithMatchNum(final int matchNum, final boolean isBo private int getMatchNum(final boolean isBonusMatch, final Integer size) { if (isBonusMatch) { - return size - 1; + return size - ONE; } return size; } From ad3915840c3aa825150cf2271f121201f1603b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 02:12:46 +0900 Subject: [PATCH 23/26] =?UTF-8?q?feat(OutputFormatter):=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EB=AC=B8=EC=9E=90=EC=97=B4=20=EB=B3=80=ED=99=98=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - OutputFormatter 추가 --- docs/README.md | 5 ++++ src/main/java/lotto/ComponentFactory.java | 6 ++++- src/main/java/lotto/ErrorMessage.java | 2 +- src/main/java/lotto/LottoMessage.java | 2 +- src/main/java/lotto/OutputFormatter.java | 32 +++++++++++++++++++++++ src/main/java/lotto/OutputManager.java | 31 ++++++---------------- src/main/java/lotto/RewardMoneyMap.java | 2 +- 7 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 src/main/java/lotto/OutputFormatter.java diff --git a/docs/README.md b/docs/README.md index 5f43d9285e..01a9c901c9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -75,6 +75,11 @@ - getRewardMoney() 개수에 따른 RewardMoney 조회 - getMatchNum() RewardMoney에 따른 개수 조회 +## OutputFormatter - 출력 String 포매팅 +- formatResultString() 결과 string 포맷 +- formatEarningRateString() 수익률 string 포맷 +- formatPublishNum() 발행 개수 string 포맷 + ## ComponentFactory - 컴포넌트들의 생성 # Enum 목록 diff --git a/src/main/java/lotto/ComponentFactory.java b/src/main/java/lotto/ComponentFactory.java index 88bf47a6ab..87be62b44e 100644 --- a/src/main/java/lotto/ComponentFactory.java +++ b/src/main/java/lotto/ComponentFactory.java @@ -19,7 +19,11 @@ private LottoRepository lottoRepository() { } private OutputManager outputManager() { - return new OutputManager(); + return new OutputManager(outputFormatter()); + } + + private OutputFormatter outputFormatter() { + return new OutputFormatter(); } private InputManager inputManager() { diff --git a/src/main/java/lotto/ErrorMessage.java b/src/main/java/lotto/ErrorMessage.java index 92cc75ecab..790322d881 100644 --- a/src/main/java/lotto/ErrorMessage.java +++ b/src/main/java/lotto/ErrorMessage.java @@ -8,7 +8,7 @@ public enum ErrorMessage { NOT_VALID_ELEMENTS_NUM("리스트의 원소 개수는 %d개여야 합니다."), NOT_UNIQUE_ELEMENTS("리스트에 중복 원소가 존재합니다."), CANNOT_FIND_RESULT("결과를 찾을 수 없습니다."), - NOT_VALID_LOTTO_NUMBER_ERROR("로또 번호의 범위는 최소 1, 최대 45입니다."); + NOT_VALID_LOTTO_NUMBER_ERROR("로또 번호는 1부터 45 사이의 숫자여야 합니다."); private final String message; diff --git a/src/main/java/lotto/LottoMessage.java b/src/main/java/lotto/LottoMessage.java index 3e8b097fa5..a90a16d47d 100644 --- a/src/main/java/lotto/LottoMessage.java +++ b/src/main/java/lotto/LottoMessage.java @@ -5,7 +5,7 @@ public enum LottoMessage { PUBLISH_NUM("%d개를 구매했습니다."), CREATE_WINNING_NUMBER("당첨 번호를 입력해 주세요."), CREATE_BONUS_NUMBER("보너스 번호를 입력해 주세요."), - WINNING_STATISTICS("당첨 통계\n" + "---"), + WINNING_STATISTICS("당첨 통계\n---"), NUM_OF_MATCH("%d개 일치 (%s원) - %d개"), BONUS_MATCH("%d개 일치, 보너스 볼 일치 (%s원) - %d개"), EARNING_RATE("총 수익률은 %.1f%%입니다."); diff --git a/src/main/java/lotto/OutputFormatter.java b/src/main/java/lotto/OutputFormatter.java new file mode 100644 index 0000000000..ee1998f4a7 --- /dev/null +++ b/src/main/java/lotto/OutputFormatter.java @@ -0,0 +1,32 @@ +package lotto; + +import java.text.NumberFormat; + +public class OutputFormatter { + private final NumberFormat numberFormat; + + public OutputFormatter() { + this.numberFormat = NumberFormat.getInstance(); + } + + public String formatResultString(final RewardMoney rewardMoney, final Integer numOfResult) { + RewardMoneyMap rewardMoneyMap = RewardMoneyMap.getInstance(); + + if (rewardMoney.equals(RewardMoney.MATCH_WITH_BONUS)) { + return String.format( + LottoMessage.BONUS_MATCH.toString(), + rewardMoneyMap.getMatchNum(rewardMoney), numberFormat.format(rewardMoney.toValue()), numOfResult); + } + return String.format( + LottoMessage.NUM_OF_MATCH.toString(), + rewardMoneyMap.getMatchNum(rewardMoney), numberFormat.format(rewardMoney.toValue()), numOfResult); + } + + public String formatEarningRateString(final Double earningRate) { + return String.format(LottoMessage.EARNING_RATE.toString(), earningRate); + } + + public String formatPublishNum(final Integer publishNum) { + return String.format(LottoMessage.PUBLISH_NUM.toString(), publishNum); + } +} diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index bf1764c6f5..d1a37dfdb3 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -1,13 +1,12 @@ package lotto; -import java.text.NumberFormat; import java.util.List; public class OutputManager { - private final NumberFormat numberFormat; + private final OutputFormatter outputFormatter; - public OutputManager() { - this.numberFormat = NumberFormat.getNumberInstance(); + public OutputManager(final OutputFormatter outputFormatter) { + this.outputFormatter = outputFormatter; } public void printStartMessage() { @@ -15,7 +14,7 @@ public void printStartMessage() { } public void printPublishNum(final Integer publishNum) { - System.out.println(String.format(LottoMessage.PUBLISH_NUM.toString(), publishNum)); + System.out.println(outputFormatter.formatPublishNum(publishNum)); } public void printLottoList(final List lottoList) { @@ -35,28 +34,14 @@ public void printWinningStatistics() { } public void printResult(final ResultMap resultMap) { - resultMap.getResultMap().forEach(((result, numOfResult) ->{ - if (!result.equals(RewardMoney.NONE)) { - printResultByMoney(result, numOfResult); + resultMap.getResultMap().forEach(((rewardMoney, numOfResult) ->{ + if (!rewardMoney.equals(RewardMoney.NONE)) { + System.out.println(outputFormatter.formatResultString(rewardMoney, numOfResult)); } })); } - private void printResultByMoney(final RewardMoney rewardMoney, final Integer numOfResult) { - RewardMoneyMap rewardMoneyMap = RewardMoneyMap.getInstance(); - - if (rewardMoney.equals(RewardMoney.MATCH_WITH_BONUS)) { - System.out.println(String.format( - LottoMessage.BONUS_MATCH.toString(), - rewardMoneyMap.getMatchNum(rewardMoney), numberFormat.format(rewardMoney.toValue()), numOfResult)); - return; - } - System.out.println(String.format( - LottoMessage.NUM_OF_MATCH.toString(), - rewardMoneyMap.getMatchNum(rewardMoney), numberFormat.format(rewardMoney.toValue()), numOfResult)); - } - public void printEarningRate(final Double earningRate) { - System.out.println(String.format(LottoMessage.EARNING_RATE.toString(), earningRate)); + System.out.println(outputFormatter.formatEarningRateString(earningRate)); } } diff --git a/src/main/java/lotto/RewardMoneyMap.java b/src/main/java/lotto/RewardMoneyMap.java index c93469779d..6e33cccc29 100644 --- a/src/main/java/lotto/RewardMoneyMap.java +++ b/src/main/java/lotto/RewardMoneyMap.java @@ -7,7 +7,7 @@ public class RewardMoneyMap { private static final Integer DEFAULT_INDEX = 0; private static final Integer BONUS_INDEX = 1; - private Map> moneyMap = new HashMap<>(); + private final Map> moneyMap = new HashMap<>(); private static RewardMoneyMap rewardMoneyMap; public static RewardMoneyMap getInstance() { From bca2c44139d304c156173f504f3025ec1c7bcb68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 02:16:57 +0900 Subject: [PATCH 24/26] =?UTF-8?q?docs:=20=EC=99=84=EB=A3=8C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/README.md b/docs/README.md index 01a9c901c9..92c9ac63b1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,11 +7,11 @@ [x] 발행한 로또 수량 및 번호 출력 기능 [x] 당첨 번호 입력 기능 [x] 당첨 번호 입력값 검증 기능 -[ ] 번호 일치 여부 확인 및 당첨 여부 확인 기능 -[ ] 당첨 내역 출력 기능 -[ ] 수익률 계산 및 반올림 기능 -[ ] 수익률 출력 기능 -[ ] 예외 처리 및 에러 문구 출력 기능 +[x] 번호 일치 여부 확인 및 당첨 여부 확인 기능 +[x] 당첨 내역 출력 기능 +[x] 수익률 계산 및 반올림 기능 +[x] 수익률 출력 기능 +[x] 예외 처리 및 에러 문구 출력 기능 # 구현할 클래스 목록 ## WinningNumber - 당첨 번호 From dd7ecb6282ae93326f7fd53142f31341189d8bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 12:34:06 +0900 Subject: [PATCH 25/26] =?UTF-8?q?feat:=20=EC=97=90=EB=9F=AC=20=ED=95=B8?= =?UTF-8?q?=EB=93=A4=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능추가 - 에러 try catch --- src/main/java/lotto/Application.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 2be7e5eb86..252c6c5292 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -4,6 +4,10 @@ public class Application { public static void main(String[] args) { final ComponentFactory componentFactory = new ComponentFactory(); final FrontController frontController = componentFactory.frontController(); - frontController.playLotto(); + try{ + frontController.playLotto(); + } catch(final IllegalArgumentException e) { + System.out.println(e.getMessage()); + } } } From 9760d80e542d5b5eaf16c546f0dd4dad1219aae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Mon, 7 Aug 2023 13:50:48 +0900 Subject: [PATCH 26/26] =?UTF-8?q?feat:=20=EC=97=90=EB=9F=AC=20=ED=95=B8?= =?UTF-8?q?=EB=93=A4=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 추가 - 에러 메시지 출력 --- docs/README.md | 1 + src/main/java/lotto/Application.java | 6 +----- src/main/java/lotto/ComponentFactory.java | 2 +- src/main/java/lotto/FrontController.java | 18 ++++++++++++------ src/main/java/lotto/OutputManager.java | 4 ++++ 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index 92c9ac63b1..923e897001 100644 --- a/docs/README.md +++ b/docs/README.md @@ -66,6 +66,7 @@ - printCreateBonusNumber() 보너스 번호 입력 메시지 출력 - printResult() 결과 정보 출력 - printEarningRate() 수익률 출력 +- printErrorMessage() 에러 메시지 출력 ## ResultMap - 결과 종류 마다의 개수 저장 - getResultMap() 결과 관련 map 조회 diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 252c6c5292..2be7e5eb86 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -4,10 +4,6 @@ public class Application { public static void main(String[] args) { final ComponentFactory componentFactory = new ComponentFactory(); final FrontController frontController = componentFactory.frontController(); - try{ - frontController.playLotto(); - } catch(final IllegalArgumentException e) { - System.out.println(e.getMessage()); - } + frontController.playLotto(); } } diff --git a/src/main/java/lotto/ComponentFactory.java b/src/main/java/lotto/ComponentFactory.java index 87be62b44e..e8ff937cfe 100644 --- a/src/main/java/lotto/ComponentFactory.java +++ b/src/main/java/lotto/ComponentFactory.java @@ -3,7 +3,7 @@ public class ComponentFactory { public FrontController frontController() { - return new FrontController(lottoController()); + return new FrontController(lottoController(), outputManager()); } private LottoController lottoController() { diff --git a/src/main/java/lotto/FrontController.java b/src/main/java/lotto/FrontController.java index 04a28b451a..3bc14daa86 100644 --- a/src/main/java/lotto/FrontController.java +++ b/src/main/java/lotto/FrontController.java @@ -2,16 +2,22 @@ public class FrontController { private final LottoController lottoController; + private final OutputManager outputManager; - public FrontController(final LottoController lottoController) { + public FrontController(final LottoController lottoController, final OutputManager outputManager) { this.lottoController = lottoController; + this.outputManager = outputManager; } public void playLotto() { - lottoController.getPublishNum(); - lottoController.publishLotto(); - lottoController.getWinningNumber(); - lottoController.checkWinningNumber(); - lottoController.checkEarningRate(); + try { + lottoController.getPublishNum(); + lottoController.publishLotto(); + lottoController.getWinningNumber(); + lottoController.checkWinningNumber(); + lottoController.checkEarningRate(); + } catch (final IllegalArgumentException e) { + outputManager.printErrorMessage(e.getMessage()); + } } } diff --git a/src/main/java/lotto/OutputManager.java b/src/main/java/lotto/OutputManager.java index d1a37dfdb3..b2f2406f41 100644 --- a/src/main/java/lotto/OutputManager.java +++ b/src/main/java/lotto/OutputManager.java @@ -44,4 +44,8 @@ public void printResult(final ResultMap resultMap) { public void printEarningRate(final Double earningRate) { System.out.println(outputFormatter.formatEarningRateString(earningRate)); } + + public void printErrorMessage(final String errorMessage) { + System.out.println(errorMessage); + } }