From c08e1a13ded80b72183de70940bf508fe2f8932b Mon Sep 17 00:00:00 2001 From: rndnjsgud <00kwh@naver.com> Date: Sun, 3 Nov 2024 23:36:04 +0900 Subject: [PATCH 1/8] =?UTF-8?q?docx:=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=B0=8F=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=EC=82=AC=ED=95=AD=20=EB=AA=85=EC=84=B8=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=EC=A0=9D=ED=8A=B8=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD?= =?UTF-8?q?=EC=9D=84=20=ED=86=B5=ED=95=B4=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=B0=8F=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC,=20=EB=93=B1=EC=9D=84=20?= =?UTF-8?q?=EB=AA=85=EC=84=B8=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 5fa2560b46..6155bac21a 100644 --- a/README.md +++ b/README.md @@ -1 +1,36 @@ # java-lotto-precourse + +## 기능 요구 사항 +### 간단한 로또 발매기를 구현한다. +- 로또 번호의 숫자 범위는 1~45까지이다. +- 1개의 로또를 발행할 때 중복되지 않는 6개의 숫자를 뽑는다. +- 당첨 번호 추첨 시 중복되지 않는 숫자 6개와 보너스 번호 1개를 뽑는다. +- 당첨은 1등부터 5등까지 있다. 당첨 기준과 금액은 아래와 같다. + - 1등: 6개 번호 일치 / 2,000,000,000원 + - 2등: 5개 번호 + 보너스 번호 일치 / 30,000,000원 + - 3등: 5개 번호 일치 / 1,500,000원 + - 4등: 4개 번호 일치 / 50,000원 + - 5등: 3개 번호 일치 / 5,000원 +- 로또 구입 금액을 입력하면 구입 금액에 해당하는 만큼 로또를 발행해야 한다. +- 로또 1장의 가격은 1,000원이다. +- 당첨 번호와 보너스 번호를 입력받는다. +- 사용자가 구매한 로또 번호와 당첨 번호를 비교하여 당첨 내역 및 수익률을 출력하고 로또 게임을 종료한다. +- 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시키고, "[ERROR]"로 시작하는 에러 메시지를 출력 후 그 부분부터 입력을 다시 받는다. +- Exception이 아닌 IllegalArgumentException, IllegalStateException 등과 같은 명확한 유형을 처리한다. + +## 기능 구현 사항 +- 입력 관련 기능 + - 사용자로부터 로또 구입 금액을 입력받는다. + - 1000원으로 나누어 떨어지지 않는 경우 예외처리 + - 당첨번호 6자리를 ','를 기준으로 구분하여 입력받는다 + - 당첨번호가 6자리 미만이거나 초과할 경우 예외처리 + - 당첨번호가 1 ~ 45를 벗어나는 경우 예외처리 + - 숫자 이외의 문자를 입력받거나, 입력이 없을 시 예외처리 + - 중복되는 숫자를 입력받았을 시 예외처리 +- 1 ~ 45 사이의 로또 번호를 생성하는 기능 + - 생성 후 오름차순으로 저장 + - 마찬가지로 숫자의 중복이 불가함 +- 각 생성된 로또번호를 통해 당첨 내역을 확인하는 기능 + - 각 당첨 순위는 Enum으로 명세해둠 +- 당첨금을 기준으로 수익률을 계산하는 기능 +- 요구사항에 맞게 결과를 출력하는 기능 \ No newline at end of file From e1ef2a0c65c68bc006a378029c23d7419783e1f5 Mon Sep 17 00:00:00 2001 From: rndnjsgud <00kwh@naver.com> Date: Mon, 4 Nov 2024 18:15:23 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20=EA=B5=AC=EB=A7=A4=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=EA=B3=BC=20=EB=8B=B9=EC=B2=A8=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?=EA=B0=81=20=EC=9E=85=EB=A0=A5=EC=9D=80=20README.md=EC=97=90=20?= =?UTF-8?q?=EB=AA=85=EC=84=B8=ED=95=9C=20=EB=8C=80=EB=A1=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 20 ---------- src/main/java/lotto/domain/Lotto.java | 56 +++++++++++++++++++++++++++ src/main/java/lotto/view/Input.java | 37 ++++++++++++++++++ src/test/java/lotto/LottoTest.java | 1 + 4 files changed, 94 insertions(+), 20 deletions(-) delete mode 100644 src/main/java/lotto/Lotto.java create mode 100644 src/main/java/lotto/domain/Lotto.java create mode 100644 src/main/java/lotto/view/Input.java diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 88fc5cf12b..0000000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,20 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); - } - } - - // TODO: 추가 기능 구현 -} diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java new file mode 100644 index 0000000000..ab1da78ce3 --- /dev/null +++ b/src/main/java/lotto/domain/Lotto.java @@ -0,0 +1,56 @@ +package lotto.domain; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validateLottoNumber(numbers); + this.numbers = numbers; + } + + private void validateLottoSize(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } + + // TODO: 추가 기능 구현 + private void validateLottoNumberRange(List numbers){ + numbers.stream().forEach(element->{ + if(element < 1 || element > 45){ + throw new IllegalArgumentException("[ERROR] 로또 번호는 1 ~ 45 내의 숫자여야 합니다."); + } + }); + } + + private void validateLottoNumberIsInteger(List numbers){ + numbers.stream().forEach(element->{ + if(!(element instanceof Integer)){ + throw new IllegalArgumentException("[ERROR] 로또 번호는 정수만 입력 가능합니다."); + } + }); + } + + private void validateLottoNumberIsDuplicated(List numbers){ + Set numbersSet = new HashSet<>(numbers); + if(numbersSet.size() < numbers.size()){ + throw new IllegalArgumentException("[ERROR] 중복된 로또 번호는 입력이 불가합니다."); + } + } + + private void validateLottoNumber(List numbers){ + try{ + validateLottoSize(numbers); + validateLottoNumberRange(numbers); + validateLottoNumberIsInteger(numbers); + validateLottoNumberIsDuplicated(numbers); + } + catch(IllegalArgumentException e){ + System.out.println(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/main/java/lotto/view/Input.java b/src/main/java/lotto/view/Input.java new file mode 100644 index 0000000000..26bf25368c --- /dev/null +++ b/src/main/java/lotto/view/Input.java @@ -0,0 +1,37 @@ +package lotto.view; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Input { + + public static int inputPurchaseAmount() throws IllegalArgumentException { + int puschaseAmount; + System.out.println("구입금액을 입력해 주세요."); + try { + puschaseAmount = Integer.parseInt(Console.readLine()); + }catch (IllegalArgumentException e){ + throw new IllegalArgumentException("[ERROR] 구입 금액은 정수로 입력해야 합니다."); + } + validatePurchaseAmount(puschaseAmount); + return puschaseAmount; + } + + public static void validatePurchaseAmount(int purchaseAmount){ + if((purchaseAmount % 1000) != 0 && (purchaseAmount / 1000) < 1){ + throw new IllegalArgumentException("[ERROR] 구입 금액은 1000 이상의, 1000단위 정수로 입력하여야 합니다"); + } + } + public static List inputWinnerNumbers(){ + System.out.println("당첨 번호를 입력해 주세요."); + try { + String winningNumbersInString = Console.readLine(); + return Arrays.stream(winningNumbersInString.split(",")).map(element -> Integer.parseInt(element)).collect(Collectors.toList()); + }catch (IllegalArgumentException e){ + throw new IllegalArgumentException("[ERROR] 숫자를 입력해야 합니다."); + } + } +} \ No newline at end of file diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 309f4e50ae..c74fb4b676 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,5 +1,6 @@ package lotto; +import lotto.domain.Lotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From aeb789cfb02936bd450cdf4c11dff3abf3028302 Mon Sep 17 00:00:00 2001 From: rndnjsgud <00kwh@naver.com> Date: Mon, 4 Nov 2024 18:17:23 +0900 Subject: [PATCH 3/8] =?UTF-8?q?docx:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EA=B4=80=EB=A0=A8=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EB=AA=85=EC=84=B8=20=EC=B6=94=EA=B0=80=20README.md=EC=97=90=20?= =?UTF-8?q?=EB=B3=B4=EB=84=88=EC=8A=A4=20=EB=B2=88=ED=98=B8=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EA=B8=B0=EB=8A=A5=EC=9D=B4=20=EB=88=84=EB=9D=BD?= =?UTF-8?q?=EB=90=98=EC=96=B4=20=EC=9E=88=EC=96=B4=20=ED=95=B4=EB=8B=B9=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EB=B0=8F=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B2=BD=EC=9A=B0=20=EC=B6=94=EA=B0=80=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=EB=AA=85=EC=84=B8=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 6155bac21a..5038abfe8d 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,10 @@ - 당첨번호가 1 ~ 45를 벗어나는 경우 예외처리 - 숫자 이외의 문자를 입력받거나, 입력이 없을 시 예외처리 - 중복되는 숫자를 입력받았을 시 예외처리 + - 보너스 번호를 입력받는다 + - 보너스 번호가 1 ~ 45를 벗어나는 경우 예외 처리 + - 숫자 이외의 문자를 입력받거나, 입력이 없을 시 예외처리 + - 입력받은 당첨 번호와 중복되는 숫자를 입력받았을 시 예외처리 - 1 ~ 45 사이의 로또 번호를 생성하는 기능 - 생성 후 오름차순으로 저장 - 마찬가지로 숫자의 중복이 불가함 From 67c32b557342b4f3efbfd7fed8eda8595283d6ea Mon Sep 17 00:00:00 2001 From: rndnjsgud <00kwh@naver.com> Date: Mon, 4 Nov 2024 18:23:43 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EB=B3=B4=EB=84=88?= =?UTF-8?q?=EC=8A=A4=20=EB=B2=88=ED=98=B8=EB=A5=BC=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EB=B0=9B=EA=B3=A0,=20=EB=AA=85=EC=84=B8=ED=95=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/view/Input.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/view/Input.java b/src/main/java/lotto/view/Input.java index 26bf25368c..9b02054eb7 100644 --- a/src/main/java/lotto/view/Input.java +++ b/src/main/java/lotto/view/Input.java @@ -22,7 +22,7 @@ public static int inputPurchaseAmount() throws IllegalArgumentException { public static void validatePurchaseAmount(int purchaseAmount){ if((purchaseAmount % 1000) != 0 && (purchaseAmount / 1000) < 1){ - throw new IllegalArgumentException("[ERROR] 구입 금액은 1000 이상의, 1000단위 정수로 입력하여야 합니다"); + throw new IllegalArgumentException("[ERROR] 구입 금액은 1000 이상의, 1000단위 정수로 입력해야 합니다"); } } public static List inputWinnerNumbers(){ @@ -34,4 +34,25 @@ public static List inputWinnerNumbers(){ throw new IllegalArgumentException("[ERROR] 숫자를 입력해야 합니다."); } } + + public static int inputBonusNumber(List winningNumbers) throws IllegalArgumentException{ + int bonusNumber; + try{ + bonusNumber = Integer.parseInt(Console.readLine()); + } + catch (IllegalArgumentException e){ + throw new IllegalArgumentException("[ERROR] 보너스 번호는 정수로 입력해야 합니다."); + } + validateBonusNumber(bonusNumber, winningNumbers); + return bonusNumber; + } + + public static void validateBonusNumber(int bonusNumber, List winningNumbers) throws IllegalArgumentException{ + if(bonusNumber < 1 && bonusNumber > 45){ + throw new IllegalArgumentException("[ERROR] 보너스 번호는 1 ~ 45 내에 있는 정수입니다."); + } + if(winningNumbers.contains(bonusNumber)){ + throw new IllegalArgumentException("[ERROR] 보너스 번호는 기존 당첨번호와 중복될 수 없습니다."); + } + } } \ No newline at end of file From 4813e9b513c90d25d0eb61212aad08175813747f Mon Sep 17 00:00:00 2001 From: rndnjsgud <00kwh@naver.com> Date: Mon, 4 Nov 2024 18:37:21 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20=EA=B5=AC=EC=9E=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=EC=9D=84=20=ED=86=B5=ED=95=9C=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EA=B5=AC=EB=A7=A4=20=EA=B0=AF=EC=88=98=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=EB=90=9C=20=EA=B5=AC=EC=9E=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=EC=9D=84=20=ED=86=B5=ED=95=B4=20=EA=B5=AC=EB=A7=A4?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EC=88=98=EB=A5=BC=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/view/Input.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/lotto/view/Input.java b/src/main/java/lotto/view/Input.java index 9b02054eb7..fff4906231 100644 --- a/src/main/java/lotto/view/Input.java +++ b/src/main/java/lotto/view/Input.java @@ -25,6 +25,11 @@ public static void validatePurchaseAmount(int purchaseAmount){ throw new IllegalArgumentException("[ERROR] 구입 금액은 1000 이상의, 1000단위 정수로 입력해야 합니다"); } } + + public static int calculateLottoAmount(int purchaseAmount){ + return purchaseAmount/1000; + } + public static List inputWinnerNumbers(){ System.out.println("당첨 번호를 입력해 주세요."); try { From e609a50999e6e3ff6bca00cff6604524c8bdc6e8 Mon Sep 17 00:00:00 2001 From: rndnjsgud <00kwh@naver.com> Date: Mon, 4 Nov 2024 18:42:34 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20=EA=B5=AC=EC=9E=85=20=EA=B0=AF?= =?UTF-8?q?=EC=88=98=EB=A5=BC=20=ED=86=B5=ED=95=9C=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?=EC=A3=BC=EC=96=B4=EC=A7=84=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=EB=8C=80=EB=A1=9C=20Randoms.pickUniqueNumbersInRange?= =?UTF-8?q?=EB=A5=BC=20=ED=86=B5=ED=95=B4=206=EA=B0=9C=EC=9D=98=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EB=B2=88=ED=98=B8=EB=A5=BC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=95=98=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EA=B5=AC=ED=98=84=ED=95=98=EA=B3=A0,=20=EC=9D=B4?= =?UTF-8?q?=20=EB=A1=9C=EB=98=90=EB=A5=BC=20=EA=B5=AC=EB=A7=A4=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EC=88=98=20=EB=A7=8C=ED=81=BC=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/domain/LottoNumberGenerator.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/lotto/domain/LottoNumberGenerator.java diff --git a/src/main/java/lotto/domain/LottoNumberGenerator.java b/src/main/java/lotto/domain/LottoNumberGenerator.java new file mode 100644 index 0000000000..8fe1b1361f --- /dev/null +++ b/src/main/java/lotto/domain/LottoNumberGenerator.java @@ -0,0 +1,26 @@ +package lotto.domain; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class LottoNumberGenerator { + private Lotto generateLotto(){ + List lottoNumbers = Randoms.pickUniqueNumbersInRange(1,45,6); + Collections.sort(lottoNumbers); + + Lotto lotto = new Lotto(lottoNumbers); + return lotto; + } + + private List generateLottoNumbers(int amountOfLotto){ + List lottos = new ArrayList<>(); + for(int i = 0 ; i < amountOfLotto ; i++){ + lottos.add(generateLotto()); + } + return lottos; + } +} From bc3cc7e46d25eecb55a1e565236fdf1e36e46efb Mon Sep 17 00:00:00 2001 From: rndnjsgud <00kwh@naver.com> Date: Mon, 4 Nov 2024 20:14:34 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=EA=B5=AC=EB=A7=A4=ED=95=9C=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=EC=99=80=20=EB=8B=B9=EC=B2=A8=EB=B2=88=ED=98=B8?= =?UTF-8?q?=EB=A5=BC=20=ED=86=B5=ED=95=B4=20=EB=93=B1=EC=88=98=EB=A5=BC=20?= =?UTF-8?q?=EA=B3=84=EC=8B=BC=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=20Enum=EC=9C=BC=EB=A1=9C=20=EA=B0=81=20?= =?UTF-8?q?=EB=93=B1=EC=88=98=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=B4?= =?UTF-8?q?=EB=91=90=EA=B3=A0=20=EB=93=B1=EC=88=98=EB=A5=BC=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=ED=95=98=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EA=B0=81=20=EB=A1=9C=EB=98=90=EC=9D=98=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=EC=99=80=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=9D=BC=EC=B9=98=EB=A5=BC=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=ED=95=9C=20=ED=9B=84=20=EC=9D=B4=EB=A5=BC=20Map?= =?UTF-8?q?=EC=9E=90=EB=A3=8C=ED=98=95=EC=9C=BC=EB=A1=9C=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=20=ED=95=B4=EB=8B=B9=20=EB=A1=9C=EB=98=90=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=B4=EB=91=94=20Enu?= =?UTF-8?q?m=EC=9D=84=20=EC=9D=B4=EC=9A=A9=ED=95=98=EC=97=AC=20=EB=93=B1?= =?UTF-8?q?=EC=88=98=EB=A5=BC=20=EC=A0=95=ED=95=9C=20=ED=9B=84=20=EA=B0=81?= =?UTF-8?q?=20=EB=93=B1=EC=88=98=EB=A5=BC=20List=ED=98=95=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EA=B4=80=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/Lotto.java | 4 ++ src/main/java/lotto/domain/WinningLotto.java | 16 +++++++ src/main/java/lotto/domain/WinningRank.java | 35 +++++++++++++++ .../java/lotto/service/CalculateResult.java | 23 ++++++++++ src/main/java/lotto/service/CheckLotto.java | 45 +++++++++++++++++++ src/main/java/lotto/view/Input.java | 1 + src/main/java/lotto/view/Output.java | 4 ++ 7 files changed, 128 insertions(+) create mode 100644 src/main/java/lotto/domain/WinningLotto.java create mode 100644 src/main/java/lotto/domain/WinningRank.java create mode 100644 src/main/java/lotto/service/CalculateResult.java create mode 100644 src/main/java/lotto/service/CheckLotto.java create mode 100644 src/main/java/lotto/view/Output.java diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index ab1da78ce3..d15156074d 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -53,4 +53,8 @@ private void validateLottoNumber(List numbers){ System.out.println(e.getMessage()); } } + + public List getLottoNumbers(){ + return numbers; + } } \ No newline at end of file diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java new file mode 100644 index 0000000000..76d6d781ba --- /dev/null +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -0,0 +1,16 @@ +package lotto.domain; + +import java.util.HashMap; +import java.util.Map; + +public class WinningLotto { + Map winningLottoWithBonusNumber = new HashMap<>(); + + public Map getWinningLotto() { + return winningLottoWithBonusNumber; + } + + public void setWinningLotto(Map winningLotto) { + this.winningLottoWithBonusNumber = winningLotto; + } +} diff --git a/src/main/java/lotto/domain/WinningRank.java b/src/main/java/lotto/domain/WinningRank.java new file mode 100644 index 0000000000..0ec38e8638 --- /dev/null +++ b/src/main/java/lotto/domain/WinningRank.java @@ -0,0 +1,35 @@ +package lotto.domain; + +public enum WinningRank { + LOSE(0,0,false), + FIFTHPLACE(3,5000,false), + FOURTHPLACE(4,50000,false), + THIRDPLACE(5,1500000,false), + SECONDPLACE(5,30000000,true), + FIRSTPLACE(6,2000000000,false); + + private final int numberOfCorrectNumber; + private final int prizeMoneyAmount; + private final boolean isBonusNumberCorrect; + + WinningRank(int numberOfCorrectNumber, int prizeMoneyAmount, boolean isBonusNumberCorrect){ + this.numberOfCorrectNumber = numberOfCorrectNumber; + this.prizeMoneyAmount = prizeMoneyAmount; + this.isBonusNumberCorrect = isBonusNumberCorrect; + } + + public static WinningRank calculateLottoRank(int numberOfCorrectNumber, boolean isBonusNumberCorrect){ + WinningRank lottoWinningRank = LOSE; + for(WinningRank winningRank: WinningRank.values()){ + if(winningRank.isBonusNumberCorrect == isBonusNumberCorrect && winningRank.numberOfCorrectNumber == numberOfCorrectNumber){ + lottoWinningRank = winningRank; + } + } + return lottoWinningRank; + } + + + public int getWinningAmount() { + return prizeMoneyAmount; + } +} diff --git a/src/main/java/lotto/service/CalculateResult.java b/src/main/java/lotto/service/CalculateResult.java new file mode 100644 index 0000000000..5e88aa48c1 --- /dev/null +++ b/src/main/java/lotto/service/CalculateResult.java @@ -0,0 +1,23 @@ +package lotto.service; + +import lotto.domain.Lotto; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static lotto.domain.WinningRank.calculateLottoRank; + +public class CalculateResult { + public static List calculateResult(Map matchingResult){ + List resultList = new ArrayList<>(); + for (Map.Entry entry : matchingResult.entrySet()) { + int matchingNumber = entry.getKey(); + boolean isBonusNumberCorrect = entry.getValue(); + + Enum winningRank = calculateLottoRank(matchingNumber, isBonusNumberCorrect); + resultList.add(winningRank); + } + return resultList; + } +} diff --git a/src/main/java/lotto/service/CheckLotto.java b/src/main/java/lotto/service/CheckLotto.java new file mode 100644 index 0000000000..abc6a21ac1 --- /dev/null +++ b/src/main/java/lotto/service/CheckLotto.java @@ -0,0 +1,45 @@ +package lotto.service; + +import lotto.domain.Lotto; +import lotto.domain.WinningLotto; + +import java.util.List; +import java.util.Map; + +public class CheckLotto { + private Map checkLottoRank(WinningLotto lotto, List purchasedLottoNumbers, int bonusNumber){ + Map matchingResult = null; + + List winningLottoNumber = null; + int winningBonusNumber = 0; + int numberOfMatching = 0; + boolean isBonusNumberCorrect = false; + for (Map.Entry entry : lotto.getWinningLotto().entrySet()) { + winningLottoNumber = entry.getKey().getLottoNumbers(); + winningBonusNumber = entry.getValue(); + } + + for(Lotto purchasedLotto : purchasedLottoNumbers){ + List purchasedLottoNumber = purchasedLotto.getLottoNumbers(); + + numberOfMatching = checkLottoNumbers(winningLottoNumber, purchasedLottoNumber); + isBonusNumberCorrect = checkBonusNumber(winningBonusNumber, bonusNumber); + + matchingResult.put(numberOfMatching, isBonusNumberCorrect); + } + + return matchingResult; + } + + public static boolean checkBonusNumber(int winningBonusNumber, int bonusNumber){ + return winningBonusNumber == bonusNumber; + } + + private int checkLottoNumbers(List winningNumber, List purchasedLottoNumber){ + long numberOfMatchNumber = purchasedLottoNumber.stream() + .filter(winningNumber::contains) + .count(); + + return (int)numberOfMatchNumber; + } +} diff --git a/src/main/java/lotto/view/Input.java b/src/main/java/lotto/view/Input.java index fff4906231..4146c572d9 100644 --- a/src/main/java/lotto/view/Input.java +++ b/src/main/java/lotto/view/Input.java @@ -41,6 +41,7 @@ public static List inputWinnerNumbers(){ } public static int inputBonusNumber(List winningNumbers) throws IllegalArgumentException{ + System.out.println("보너스 번호를 입력해 주세요."); int bonusNumber; try{ bonusNumber = Integer.parseInt(Console.readLine()); diff --git a/src/main/java/lotto/view/Output.java b/src/main/java/lotto/view/Output.java new file mode 100644 index 0000000000..df86694dee --- /dev/null +++ b/src/main/java/lotto/view/Output.java @@ -0,0 +1,4 @@ +package lotto.view; + +public class Output { +} From ca5c087ddb915668f937b7fdeb812a3f00da9878 Mon Sep 17 00:00:00 2001 From: rndnjsgud <00kwh@naver.com> Date: Mon, 4 Nov 2024 23:58:18 +0900 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EC=9A=94=EA=B5=AC=EC=A1=B0?= =?UTF-8?q?=EA=B1=B4=EC=9D=84=20=EC=99=84=EB=B2=BD=ED=95=98=EA=B2=8C=20?= =?UTF-8?q?=EC=B6=A9=EC=A1=B1=ED=95=98=EC=A7=80=20=EB=AA=BB=ED=96=88?= =?UTF-8?q?=EC=8A=B5=EB=8B=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 3 + .../lotto/Controller/lottoController.java | 33 +++++++++++ .../lotto/domain/LottoNumberGenerator.java | 4 +- src/main/java/lotto/domain/ResultOfLotto.java | 27 +++++++++ src/main/java/lotto/domain/WinningLotto.java | 4 ++ src/main/java/lotto/domain/WinningRank.java | 18 +++++- .../java/lotto/service/CalculateResult.java | 16 +++--- src/main/java/lotto/service/CheckLotto.java | 23 ++++---- src/main/java/lotto/view/Input.java | 11 ++-- src/main/java/lotto/view/Output.java | 55 +++++++++++++++++++ 10 files changed, 165 insertions(+), 29 deletions(-) create mode 100644 src/main/java/lotto/Controller/lottoController.java create mode 100644 src/main/java/lotto/domain/ResultOfLotto.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..7fc3521c92 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,10 @@ package lotto; +import static lotto.Controller.lottoController.start; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + start(); } } diff --git a/src/main/java/lotto/Controller/lottoController.java b/src/main/java/lotto/Controller/lottoController.java new file mode 100644 index 0000000000..b1a91d507c --- /dev/null +++ b/src/main/java/lotto/Controller/lottoController.java @@ -0,0 +1,33 @@ +package lotto.Controller; + +import lotto.domain.*; +import lotto.service.CheckLotto; +import lotto.view.Input; + +import java.util.List; +import java.util.Map; + +import static lotto.service.CalculateResult.calculateResult; +import static lotto.service.CheckLotto.checkLottoRank; +import static lotto.view.Output.*; + +public class lottoController { + public static void start(){ + try { + int putchaseAmount = Input.inputPurchaseAmount(); + List winningNumber = Input.inputWinnerNumbers(); + Lotto winningLotto = new Lotto(winningNumber); + int bonusNumber = Input.inputBonusNumber(winningNumber); + int numberOfPurchase = Input.calculateLottoAmount(putchaseAmount); + List lottos = LottoNumberGenerator.generateLottoNumbers(numberOfPurchase); + WinningLotto winningLottoWithBonusNumber = new WinningLotto(winningLotto, bonusNumber); + List resultOfLottos = checkLottoRank(winningLottoWithBonusNumber, lottos, bonusNumber); + List resultList = calculateResult(resultOfLottos); + Map resultOfLottoAsEnum = collectResult(resultList); + printPurchasedLottoNumbers(lottos); + printResult(resultOfLottoAsEnum, putchaseAmount); + }catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } +} diff --git a/src/main/java/lotto/domain/LottoNumberGenerator.java b/src/main/java/lotto/domain/LottoNumberGenerator.java index 8fe1b1361f..40e0afedf8 100644 --- a/src/main/java/lotto/domain/LottoNumberGenerator.java +++ b/src/main/java/lotto/domain/LottoNumberGenerator.java @@ -8,7 +8,7 @@ import java.util.List; public class LottoNumberGenerator { - private Lotto generateLotto(){ + public static Lotto generateLotto(){ List lottoNumbers = Randoms.pickUniqueNumbersInRange(1,45,6); Collections.sort(lottoNumbers); @@ -16,7 +16,7 @@ private Lotto generateLotto(){ return lotto; } - private List generateLottoNumbers(int amountOfLotto){ + public static List generateLottoNumbers(int amountOfLotto){ List lottos = new ArrayList<>(); for(int i = 0 ; i < amountOfLotto ; i++){ lottos.add(generateLotto()); diff --git a/src/main/java/lotto/domain/ResultOfLotto.java b/src/main/java/lotto/domain/ResultOfLotto.java new file mode 100644 index 0000000000..7902a6b252 --- /dev/null +++ b/src/main/java/lotto/domain/ResultOfLotto.java @@ -0,0 +1,27 @@ +package lotto.domain; + +public class ResultOfLotto { + private int numberOfMatching; + private boolean isBonusNumberCorrect; + + public ResultOfLotto(int numberOfMatching, boolean isBonusNumberCorrect) { + this.numberOfMatching = numberOfMatching; + this.isBonusNumberCorrect = isBonusNumberCorrect; + } + + public int getNumberOfMatching() { + return numberOfMatching; + } + + public void setNumberOfMatching(int numberOfMatching) { + this.numberOfMatching = numberOfMatching; + } + + public boolean isBonusNumberCorrect() { + return isBonusNumberCorrect; + } + + public void setBonusNumberCorrect(boolean bonusNumberCorrect) { + isBonusNumberCorrect = bonusNumberCorrect; + } +} diff --git a/src/main/java/lotto/domain/WinningLotto.java b/src/main/java/lotto/domain/WinningLotto.java index 76d6d781ba..b4fe988516 100644 --- a/src/main/java/lotto/domain/WinningLotto.java +++ b/src/main/java/lotto/domain/WinningLotto.java @@ -6,6 +6,10 @@ public class WinningLotto { Map winningLottoWithBonusNumber = new HashMap<>(); + public WinningLotto(Lotto lotto, int bonusNumber) { + winningLottoWithBonusNumber.put(lotto,bonusNumber); + } + public Map getWinningLotto() { return winningLottoWithBonusNumber; } diff --git a/src/main/java/lotto/domain/WinningRank.java b/src/main/java/lotto/domain/WinningRank.java index 0ec38e8638..afd5be7d23 100644 --- a/src/main/java/lotto/domain/WinningRank.java +++ b/src/main/java/lotto/domain/WinningRank.java @@ -10,6 +10,11 @@ public enum WinningRank { private final int numberOfCorrectNumber; private final int prizeMoneyAmount; + + public boolean isBonusNumberCorrect() { + return isBonusNumberCorrect; + } + private final boolean isBonusNumberCorrect; WinningRank(int numberOfCorrectNumber, int prizeMoneyAmount, boolean isBonusNumberCorrect){ @@ -21,15 +26,22 @@ public enum WinningRank { public static WinningRank calculateLottoRank(int numberOfCorrectNumber, boolean isBonusNumberCorrect){ WinningRank lottoWinningRank = LOSE; for(WinningRank winningRank: WinningRank.values()){ - if(winningRank.isBonusNumberCorrect == isBonusNumberCorrect && winningRank.numberOfCorrectNumber == numberOfCorrectNumber){ - lottoWinningRank = winningRank; + if(winningRank.getnumberOfCorrectNumber() == numberOfCorrectNumber){ + if(winningRank.getnumberOfCorrectNumber() == 5 && winningRank.isBonusNumberCorrect() == isBonusNumberCorrect) + lottoWinningRank = winningRank; + else + lottoWinningRank = winningRank; } } return lottoWinningRank; } - public int getWinningAmount() { + public int getnumberOfCorrectNumber() { + return numberOfCorrectNumber; + } + + public int getPrizeMoneyAmount(){ return prizeMoneyAmount; } } diff --git a/src/main/java/lotto/service/CalculateResult.java b/src/main/java/lotto/service/CalculateResult.java index 5e88aa48c1..806cd1a7bb 100644 --- a/src/main/java/lotto/service/CalculateResult.java +++ b/src/main/java/lotto/service/CalculateResult.java @@ -1,6 +1,8 @@ package lotto.service; import lotto.domain.Lotto; +import lotto.domain.ResultOfLotto; +import lotto.domain.WinningRank; import java.util.ArrayList; import java.util.List; @@ -9,13 +11,13 @@ import static lotto.domain.WinningRank.calculateLottoRank; public class CalculateResult { - public static List calculateResult(Map matchingResult){ - List resultList = new ArrayList<>(); - for (Map.Entry entry : matchingResult.entrySet()) { - int matchingNumber = entry.getKey(); - boolean isBonusNumberCorrect = entry.getValue(); - - Enum winningRank = calculateLottoRank(matchingNumber, isBonusNumberCorrect); + public static List calculateResult(List resultOfLottos){ + List resultList = new ArrayList<>(); + for (ResultOfLotto resultOfLotto: resultOfLottos) { + int matchingNumber = resultOfLotto.getNumberOfMatching(); + boolean isBonusNumberCorrect = resultOfLotto.isBonusNumberCorrect(); + WinningRank winningRank = calculateLottoRank(matchingNumber, isBonusNumberCorrect); + System.out.println(winningRank); resultList.add(winningRank); } return resultList; diff --git a/src/main/java/lotto/service/CheckLotto.java b/src/main/java/lotto/service/CheckLotto.java index abc6a21ac1..1a0e970e9b 100644 --- a/src/main/java/lotto/service/CheckLotto.java +++ b/src/main/java/lotto/service/CheckLotto.java @@ -1,43 +1,44 @@ package lotto.service; import lotto.domain.Lotto; +import lotto.domain.ResultOfLotto; import lotto.domain.WinningLotto; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; public class CheckLotto { - private Map checkLottoRank(WinningLotto lotto, List purchasedLottoNumbers, int bonusNumber){ - Map matchingResult = null; + public static List checkLottoRank(WinningLotto winningLotto, List purchasedLottoNumbers, int bonusNumber){ + List resultOfLottos = new ArrayList<>(); - List winningLottoNumber = null; + List winningLottoNumber = new ArrayList<>(); int winningBonusNumber = 0; int numberOfMatching = 0; boolean isBonusNumberCorrect = false; - for (Map.Entry entry : lotto.getWinningLotto().entrySet()) { + for (Map.Entry entry : winningLotto.getWinningLotto().entrySet()) { winningLottoNumber = entry.getKey().getLottoNumbers(); winningBonusNumber = entry.getValue(); } - for(Lotto purchasedLotto : purchasedLottoNumbers){ List purchasedLottoNumber = purchasedLotto.getLottoNumbers(); - numberOfMatching = checkLottoNumbers(winningLottoNumber, purchasedLottoNumber); isBonusNumberCorrect = checkBonusNumber(winningBonusNumber, bonusNumber); - - matchingResult.put(numberOfMatching, isBonusNumberCorrect); + ResultOfLotto resultOfLotto = new ResultOfLotto(numberOfMatching, isBonusNumberCorrect); + resultOfLottos.add(resultOfLotto); } - return matchingResult; + return resultOfLottos; } public static boolean checkBonusNumber(int winningBonusNumber, int bonusNumber){ return winningBonusNumber == bonusNumber; } - private int checkLottoNumbers(List winningNumber, List purchasedLottoNumber){ + public static int checkLottoNumbers(List winningLottoNumber, List purchasedLottoNumber){ long numberOfMatchNumber = purchasedLottoNumber.stream() - .filter(winningNumber::contains) + .filter(winningLottoNumber::contains) .count(); return (int)numberOfMatchNumber; diff --git a/src/main/java/lotto/view/Input.java b/src/main/java/lotto/view/Input.java index 4146c572d9..f5da7d5a39 100644 --- a/src/main/java/lotto/view/Input.java +++ b/src/main/java/lotto/view/Input.java @@ -7,17 +7,16 @@ import java.util.stream.Collectors; public class Input { - public static int inputPurchaseAmount() throws IllegalArgumentException { - int puschaseAmount; + int purchaseAmount; System.out.println("구입금액을 입력해 주세요."); try { - puschaseAmount = Integer.parseInt(Console.readLine()); + purchaseAmount = Integer.parseInt(Console.readLine()); }catch (IllegalArgumentException e){ throw new IllegalArgumentException("[ERROR] 구입 금액은 정수로 입력해야 합니다."); } - validatePurchaseAmount(puschaseAmount); - return puschaseAmount; + validatePurchaseAmount(purchaseAmount); + return purchaseAmount; } public static void validatePurchaseAmount(int purchaseAmount){ @@ -26,7 +25,7 @@ public static void validatePurchaseAmount(int purchaseAmount){ } } - public static int calculateLottoAmount(int purchaseAmount){ + public static int calculateLottoAmount(int purchaseAmount){ return purchaseAmount/1000; } diff --git a/src/main/java/lotto/view/Output.java b/src/main/java/lotto/view/Output.java index df86694dee..ef56e16d23 100644 --- a/src/main/java/lotto/view/Output.java +++ b/src/main/java/lotto/view/Output.java @@ -1,4 +1,59 @@ package lotto.view; +import lotto.domain.Lotto; +import lotto.domain.WinningLotto; +import lotto.domain.WinningRank; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class Output { + public static void printPurchasedLottoNumbers(List lottos){ + System.out.println(lottos.size() + "개를 구매했습니다."); + for(Lotto lotto: lottos){ + List lottoNumbers = lotto.getLottoNumbers(); + System.out.println(lottoNumbers); + } + } + + public static Map initializeEnumList(){ + Map resultOfLottoAsEnum = new HashMap<>(); + for(WinningRank result: WinningRank.values()){ + resultOfLottoAsEnum.put(result, 0); + } + return resultOfLottoAsEnum; + } + + public static Map collectResult(List resultList){ + Map resultOfLottoAsEnum = initializeEnumList(); + + for(WinningRank result: resultList){ + resultOfLottoAsEnum.put(result, resultOfLottoAsEnum.get(result) + 1); + } + return resultOfLottoAsEnum; + } + + public static int calculateEarningRate(Map result, int purchaseAmount){ + int totalPrizeMoney = 0; + for (Map.Entry entry : result.entrySet()) { + totalPrizeMoney += entry.getValue() * entry.getKey().getPrizeMoneyAmount(); + } + return totalPrizeMoney; + } + + public static void printResult(Map result, int purchaseAmount){ + int totalPrizeMoney = calculateEarningRate(result, purchaseAmount); + + double earningRate = totalPrizeMoney/purchaseAmount; + + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println(WinningRank.FIFTHPLACE.getnumberOfCorrectNumber() + "개 일치 (" + WinningRank.FIFTHPLACE.getPrizeMoneyAmount() +") - "+result.get(WinningRank.FIFTHPLACE)+"개 일치"); + System.out.println(WinningRank.FOURTHPLACE.getnumberOfCorrectNumber() + "개 일치 (" + WinningRank.FOURTHPLACE.getPrizeMoneyAmount() +") - "+result.get(WinningRank.FOURTHPLACE)+"개 일치"); + System.out.println(WinningRank.THIRDPLACE.getnumberOfCorrectNumber() + "개 일치 (" + WinningRank.THIRDPLACE.getPrizeMoneyAmount() +") - "+result.get(WinningRank.THIRDPLACE)+"개 일치"); + System.out.println(WinningRank.SECONDPLACE.getnumberOfCorrectNumber() + "개 일치 (" + WinningRank.SECONDPLACE.getPrizeMoneyAmount() +") - "+result.get(WinningRank.SECONDPLACE)+"개 일치"); + System.out.println(WinningRank.FIRSTPLACE.getnumberOfCorrectNumber() + "개 일치 (" + WinningRank.FIRSTPLACE.getPrizeMoneyAmount() +") - "+result.get(WinningRank.FIRSTPLACE)+"개 일치"); + System.out.println("총 수익률은" + earningRate + "% 입니다."); + } }