From 5265546afe1c6c9c12561b5072589f1fa088b60d Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Thu, 31 Oct 2024 19:06:03 +0900 Subject: [PATCH 01/15] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=20=EC=82=AC=ED=95=AD,=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [기능 요구 사항] 로또 문제에 대한 로직을 기능 별로 분리 및 순차 정리 - 초기 입력: 구입 금액 / 당첨 번호 6개 / 보너스 번호 1개 - 구매 수량에 따른 난수 생성 (1-45 범위 내 중복 없는 6개의 수) - 구매 수량 및 생성된 난수 출력 - 당첨 케이스에 따른 당첨 여부 판별 - 당첨 통계(수익률, 총 당첨 내역) 출력 [예외 케이스] 각 입력에 따라 계산이 되지 않는 수를 구분하여 예외 케이스 작성 기본(모든 입력에 적용) / 구입 금액 / 당첨 번호 / 쉼표 기준 6개 번호 --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index 5fa2560b46..c7f8050d3c 100644 --- a/README.md +++ b/README.md @@ -1 +1,49 @@ # java-lotto-precourse + +## 기능 요구 사항 +1. 로또 구입 금액을 **1,000원 단위**로 입력 받는다. +1-1. 구매 수량은 입력 받은 금액을 1,000으로 나눈 값이다. +2. 당첨 번호를 **쉼표** 기준으로 **6개** 입력 받고, 보너스 번호를 **1개** 입력 받는다. +> **[입력 형식]** +12,5,7,23,40,1 +19 +3. 구매 수량(1-1번)에 따라 로또를 발행한다. +로또 발행 시 1개 당 **1부터 45까지**의 수 중 **6개**의 수를 중복 없이 선택한다. +4. 로또의 **수량**과 그 수량만큼의 **로또 번호**를 *오름차순*으로 정렬하여 출력한다. +> **[출력 형식]** +5개를 구매했습니다. +[8, 21, 23, 41, 42, 43] +[3, 5, 11, 16, 32, 38] +[7, 11, 16, 35, 36, 44] +[1, 8, 11, 31, 41, 42] +[13, 14, 16, 38, 42, 45] +5. 당첨 번호(2번)와 발행한 로또의 번호를 모두 대조하여 당첨 케이스 별로 **당첨 여부**를 판별한다. +5-1. 당첨 케이스는 **3개 / 4개 / 5개 / 5개 및 보너스 볼 / 6개 일치**이다. +6. **총 당첨 금액**을 **구매 수량**으로 나누고, *소수점 둘째 자리*에서 반올림하여 **수익률**을 구한다. +7. **당첨 통계**(수익률, 총 당첨 내역)를 출력한다. +> **[출력 형식]** +3개 일치 (5,000원) - 2개 +4개 일치 (50,000원) - 1개 +5개 일치 (1,500,000원) - 0개 +5개 일치, 보너스 볼 일치 (30,000,000원) - 0개 +6개 일치 (2,000,000,000원) - 0개 +총 수익률은 150.0%입니다. +## 예외 케이스 +사용자가 잘못된 값을 입력할 경우 IllegalArgumentException 발생 +[ERROR]로 시작하는 **에러 메시지** 출력 후 해당 시점부터의 입력을 다시 받음 + +**[기본 예외 케이스]** +- 비어 있는 값 +- 문자, 문자열 등 숫자가 아닌 값 +- 0 또는 음수 + +**[구입 금액 입력]** +- 1,000으로 나누어 떨어지지 않는 숫자 + +**[당첨 번호 입력 - 공통]** +- 1~45 범위 외의 숫자 +- 중복되는 숫자 + +**[당첨 번호 입력 - 쉼표 기준 6개]** +- 쉼표, 숫자 중 하나라도 포함되지 않음 +- 쉼표 기준으로 나누었을 때 6개의 숫자가 되지 않음 \ No newline at end of file From 1b9db5752aa767a09b720c874ea4d1813ae67b96 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:00:58 +0900 Subject: [PATCH 02/15] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EA=B5=AC?= =?UTF-8?q?=EC=9E=85=20=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20=EB=B0=8F?= =?UTF-8?q?=20=EA=B5=AC=EB=A7=A4=20=EC=88=98=EB=9F=89=EC=9D=84=20=EA=B5=AC?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 구입 금액을 입력 받은 다음, 1000으로 나눈 값을 lottoCount(구매 수량) 변수에 저장 - 입력 받은 값이 숫자가 아닌 경우 IllegalArgumentException 반환 --- src/main/java/lotto/Application.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..814a217079 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,14 @@ package lotto; +import camp.nextstep.edu.missionutils.Console; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + System.out.println("구입금액을 입력해 주세요."); + try { + int lottoCount = Integer.parseInt(Console.readLine()) / 1000; + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 구입금액은 숫자로 입력해야 합니다."); + } } } From 87c124fce6637ebdd5593e1a275ab53538ea7dbc Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:05:08 +0900 Subject: [PATCH 03/15] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8,=20=EB=B3=B4=EB=84=88=EC=8A=A4=20=EB=B2=88=ED=98=B8?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 당첨 번호를 입력 받아 lottoNumber 변수에 저장 (parsing을 위해 String으로 입력 받음) - 보너스 번호를 입력 받아 bonusNumber 변수에 저장 - 보너스 번호를 숫자로 입력하지 않을 경우 IllegalArgumentException 반환 --- src/main/java/lotto/Application.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 814a217079..e536bdaa30 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -10,5 +10,15 @@ public static void main(String[] args) { } catch (NumberFormatException e) { throw new IllegalArgumentException("[ERROR] 구입금액은 숫자로 입력해야 합니다."); } + + System.out.println("당첨 번호를 입력해 주세요."); + String lottoNumber = Console.readLine(); + + System.out.println("보너스 번호를 입력해 주세요."); + try { + int bonusNumber = Integer.parseInt(Console.readLine()); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 번호는 숫자로 입력해야 합니다."); + } } } From 0e2e67998a65f1c03d2210332593970919d4c84c Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:29:17 +0900 Subject: [PATCH 04/15] =?UTF-8?q?fix:=20try=20=EB=AC=B8=EC=9D=98=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=20=EC=8A=A4=EC=BD=94=ED=94=84=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=ED=95=B4=EA=B2=B0=EC=9D=84=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=20=EC=84=A0=EC=96=B8=20=EC=9C=84=EC=B9=98=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - lottoCount, bonusNumber 변수의 선언이 try 문 내에 있어 외부에서 접근 불가능한 문제를 try 문 외부로 이동함으로써 해결 --- src/main/java/lotto/Application.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index e536bdaa30..fe8ad7cab7 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -5,8 +5,9 @@ public class Application { public static void main(String[] args) { System.out.println("구입금액을 입력해 주세요."); + int lottoCount = 0; try { - int lottoCount = Integer.parseInt(Console.readLine()) / 1000; + lottoCount = Integer.parseInt(Console.readLine()) / 1000; } catch (NumberFormatException e) { throw new IllegalArgumentException("[ERROR] 구입금액은 숫자로 입력해야 합니다."); } @@ -15,8 +16,9 @@ public static void main(String[] args) { String lottoNumber = Console.readLine(); System.out.println("보너스 번호를 입력해 주세요."); + int bonusNumber = 0; try { - int bonusNumber = Integer.parseInt(Console.readLine()); + bonusNumber = Integer.parseInt(Console.readLine()); } catch (NumberFormatException e) { throw new IllegalArgumentException("[ERROR] 번호는 숫자로 입력해야 합니다."); } From 37d80919b40b5638a5eb7b327b7c299ec10737a3 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:22:47 +0900 Subject: [PATCH 05/15] =?UTF-8?q?feat:=20=EA=B5=AC=EC=9E=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EC=9E=85=EB=A0=A5=20=EC=8B=9C=200=20=EC=9D=B4?= =?UTF-8?q?=ED=95=98=EC=9D=BC=20=EA=B2=BD=EC=9A=B0,=201000=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=82=98=EB=88=84=EC=96=B4=20=EB=96=A8=EC=96=B4?= =?UTF-8?q?=EC=A7=80=EC=A7=80=20=EC=95=8A=EC=9D=84=20=EA=B2=BD=EC=9A=B0?= =?UTF-8?q?=EC=97=90=20=EA=B4=80=ED=95=9C=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=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 - 입력을 받아 바로 1000으로 나누지 않고 money 변수를 추가하여 구입 금액 값의 검사를 진행함 - 검사가 완료된 money의 값만 lottoCount 변수를 1000으로 나눈 값으로 저장함 --- src/main/java/lotto/Application.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index fe8ad7cab7..1f224197c7 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -7,7 +7,14 @@ public static void main(String[] args) { System.out.println("구입금액을 입력해 주세요."); int lottoCount = 0; try { - lottoCount = Integer.parseInt(Console.readLine()) / 1000; + int money = Integer.parseInt(Console.readLine()); + if (money <= 0) { + throw new IllegalArgumentException("[ERROR] 구입금액은 양수여야 합니다."); + } + if (money < 1000 || money % 1000 != 0) { + throw new IllegalArgumentException("[ERROR] 구입금액은 1000원 단위여야 합니다."); + } + lottoCount = money / 1000; } catch (NumberFormatException e) { throw new IllegalArgumentException("[ERROR] 구입금액은 숫자로 입력해야 합니다."); } From 8d42cf38a03951fc1115fe0648e2bd191c958c05 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:53:47 +0900 Subject: [PATCH 06/15] =?UTF-8?q?feat:=20=EA=B5=AC=EB=A7=A4=20=EC=88=98?= =?UTF-8?q?=EB=9F=89=EC=97=90=20=EB=94=B0=EB=9D=BC=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Randoms.pickUniqueNumbersInRange() 메소드를 이용하여 1부터 45까지 무작위로 중복 없이 6개의 값을 생성하고 저장함 - 구입 금액을 입력 받을 때 계산한 구매 수량만큼 반복함 --- src/main/java/lotto/Application.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 1f224197c7..191f7fcfd0 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,6 +1,9 @@ package lotto; import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.List; public class Application { public static void main(String[] args) { @@ -19,6 +22,12 @@ public static void main(String[] args) { throw new IllegalArgumentException("[ERROR] 구입금액은 숫자로 입력해야 합니다."); } + List lottos = new ArrayList<>(); + for (int i = 0; i < lottoCount; i++) { + List numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + lottos.add(new Lotto(numbers)); + } + System.out.println("당첨 번호를 입력해 주세요."); String lottoNumber = Console.readLine(); From c49dc6e3a404af1a62057bafa5cb93a41493a9af Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:06:04 +0900 Subject: [PATCH 07/15] =?UTF-8?q?feat:=20=EA=B5=AC=EB=A7=A4=20=EC=88=98?= =?UTF-8?q?=EB=9F=89=EA=B3=BC=20=EC=83=9D=EC=84=B1=EB=90=9C=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EB=B2=88=ED=98=B8=EB=A5=BC=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 구매 수량과 그 수량만큼 저장한 로또 번호를 6개씩 불러와서 출력함 - 생성 및 저장한 로또 번호를 불러오기 위해 Lotto 클래스의 numbers 컨테이너 값을 반환하는 getNumbers() 메소드 추가 --- src/main/java/lotto/Application.java | 9 +++++++-- src/main/java/lotto/Lotto.java | 5 ++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 191f7fcfd0..840c1f7a7a 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -28,10 +28,15 @@ public static void main(String[] args) { lottos.add(new Lotto(numbers)); } - System.out.println("당첨 번호를 입력해 주세요."); + System.out.println(lottoCount + "개를 구매했습니다."); + for (Lotto lotto : lottos) { + System.out.println(lotto.getNumbers()); + } + + System.out.println("\n당첨 번호를 입력해 주세요."); String lottoNumber = Console.readLine(); - System.out.println("보너스 번호를 입력해 주세요."); + System.out.println("\n보너스 번호를 입력해 주세요."); int bonusNumber = 0; try { bonusNumber = Integer.parseInt(Console.readLine()); diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 88fc5cf12b..96d5cf2c94 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,5 +1,6 @@ package lotto; +import java.util.ArrayList; import java.util.List; public class Lotto { @@ -16,5 +17,7 @@ private void validate(List numbers) { } } - // TODO: 추가 기능 구현 + public List getNumbers() { + return new ArrayList<>(numbers); + } } From 88f2083d8064acd95520b73857e67e6e248e7df8 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:24:06 +0900 Subject: [PATCH 08/15] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5=20=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88=ED=98=B8=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EC=97=B4=EC=9D=84=20=EC=89=BC=ED=91=9C=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=EB=A1=9C=20=EB=82=98=EB=88=84=EC=96=B4=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=ED=95=98=EB=8A=94=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 - 당첨 번호 문자열을 split() 메소드를 통해 쉼표 단위로 나눈 다음, 반복문을 통해 int 타입으로 변환하고 당첨 번호(winningNumbers) 컨테이너에 저장함 --- src/main/java/lotto/Application.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 840c1f7a7a..858fc06e51 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -35,6 +35,12 @@ public static void main(String[] args) { System.out.println("\n당첨 번호를 입력해 주세요."); String lottoNumber = Console.readLine(); + List winningNumbers = new ArrayList<>(); + String[] eachNumbers = lottoNumber.split(","); + + for (String number : eachNumbers) { + winningNumbers.add(Integer.parseInt(number)); + } System.out.println("\n보너스 번호를 입력해 주세요."); int bonusNumber = 0; From c252b4876428ee334dbe167cbed1d84f66e54c16 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:38:50 +0900 Subject: [PATCH 09/15] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=EC=99=80=20=EB=A1=9C=EB=98=90=20=EB=B2=88=ED=98=B8?= =?UTF-8?q?=EB=A5=BC=20=EB=8C=80=EC=A1=B0=ED=95=98=EC=97=AC=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EC=97=AC=EB=B6=80=20=ED=8C=90=EB=B3=84=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 각 로또 번호가 당첨 번호와 일치하면 일치 개수 변수를 1 더하고, 보너스 번호와 일치하면 hasBonus 변수를 true로 변경함 - 각 인덱스를 당첨 번호와 생성된 로또 번호가 몇 개 일치하는지 나타내도록 설정함 (7의 경우 5개 및 보너스 볼 일치) --- src/main/java/lotto/Application.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 858fc06e51..4e3dd4c267 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -49,5 +49,26 @@ public static void main(String[] args) { } catch (NumberFormatException e) { throw new IllegalArgumentException("[ERROR] 번호는 숫자로 입력해야 합니다."); } + + int[] matchCounts = new int[8]; + + for (Lotto lotto : lottos) { + int matches = 0; + boolean hasBonus = false; + for (int number : lotto.getNumbers()) { + if (winningNumbers.contains(number)) { + matches++; + } + if (number == bonusNumber) { + hasBonus = true; + } + } + if (matches == 5 && hasBonus) { + matchCounts[7]++; + } + if (matches >= 3) { + matchCounts[matches]++; + } + } } } From 0597e1a45f3176da2ebf43fb916e67b5cbc7b3e5 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:50:58 +0900 Subject: [PATCH 10/15] =?UTF-8?q?feat:=20=EC=B4=9D=20=EB=8B=B9=EC=B2=A8=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=EA=B3=BC=20=EC=88=98=EC=9D=B5=EB=A5=A0?= =?UTF-8?q?=EC=9D=84=20=EA=B3=84=EC=82=B0=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [총 당첨 금액] - 각 당첨 케이스(3개 / 4개 / 5개 / 5개 및 보너스 볼 / 6개 일치)가 나온 횟수와 당첨 금액을 곱하여 계산함 - 큰 수를 가독성 있게 표현하기 위해 당첨 금액 표기에 언더바를 이용함 [수익률] - 총 당첨 금액을 100으로 곱한 값(백분율)과 구입 금액을 나누어 계산함 --- src/main/java/lotto/Application.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 4e3dd4c267..bafba8fdbd 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -9,8 +9,8 @@ public class Application { public static void main(String[] args) { System.out.println("구입금액을 입력해 주세요."); int lottoCount = 0; + int money = Integer.parseInt(Console.readLine()); try { - int money = Integer.parseInt(Console.readLine()); if (money <= 0) { throw new IllegalArgumentException("[ERROR] 구입금액은 양수여야 합니다."); } @@ -70,5 +70,10 @@ public static void main(String[] args) { matchCounts[matches]++; } } + + long totalMoney = matchCounts[3] * 5_000L + matchCounts[4] * 50_000L + + matchCounts[5] * 1_500_000L + matchCounts[7] * 30_000_000L + matchCounts[6] * 2_000_000_000L; + + double returnRate = totalMoney * 100.0 / money; } } From 244276923ad7e4c83eabb97fa3c03d7517969c05 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:01:42 +0900 Subject: [PATCH 11/15] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=ED=86=B5?= =?UTF-8?q?=EA=B3=84(=EC=88=98=EC=9D=B5=EB=A5=A0,=20=EC=B4=9D=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EB=82=B4=EC=97=AD)=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 당첨 통계: 당첨 결과 배열을 인덱스를 이용해 꺼내어 각 당첨 케이스 별로 출력함 - 수익률: 소수점 둘째 자리에서 반올림하도록 포맷하여 출력함 --- src/main/java/lotto/Application.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index bafba8fdbd..6bfc8c4ec6 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -71,9 +71,18 @@ public static void main(String[] args) { } } + System.out.println("\n당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - " + matchCounts[3] + "개"); + System.out.println("4개 일치 (50,000원) - " + matchCounts[4] + "개"); + System.out.println("5개 일치 (1,500,000원) - " + matchCounts[5] + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + matchCounts[7] + "개"); + System.out.println("6개 일치 (2,000,000,000원) - " + matchCounts[6] + "개"); + long totalMoney = matchCounts[3] * 5_000L + matchCounts[4] * 50_000L + matchCounts[5] * 1_500_000L + matchCounts[7] * 30_000_000L + matchCounts[6] * 2_000_000_000L; double returnRate = totalMoney * 100.0 / money; + System.out.printf("총 수익률은 %.1f%%입니다.\n", returnRate); } } From e8a5fc3cd1682a7013ae46ec39518f63e74e40be Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:04:24 +0900 Subject: [PATCH 12/15] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EB=82=9C=EC=88=98=EB=A5=BC=20=EC=A0=95=EB=A0=AC?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EC=A0=80=EC=9E=A5=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Collections.sort() 메소드를 사용하여 생성된 로또 번호들을 1장(번호 6개) 기준으로 정렬함 --- src/main/java/lotto/Lotto.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 96d5cf2c94..ce02bc77a5 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,6 +1,7 @@ package lotto; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class Lotto { @@ -8,6 +9,7 @@ public class Lotto { public Lotto(List numbers) { validate(numbers); + Collections.sort(numbers); this.numbers = numbers; } From 3a7d4ef8b87aadae0d6af27314767276eaf2f5ca Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:08:17 +0900 Subject: [PATCH 13/15] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EB=A1=9C=EB=98=90=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=9C=A0=ED=9A=A8?= =?UTF-8?q?=EC=84=B1=20=EA=B2=80=EC=82=AC=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 - 중복된 숫자, 1-45 범위를 넘는 숫자에 대한 검사 로직을 추가함 --- src/main/java/lotto/Lotto.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index ce02bc77a5..7917acd2f4 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -17,6 +17,12 @@ private void validate(List numbers) { if (numbers.size() != 6) { throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); } + if (numbers.stream().distinct().count() != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호들 중 중복된 숫자가 있습니다."); + } + if (numbers.stream().anyMatch(n -> n < 1 || n > 45)) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } } public List getNumbers() { From d2c1dec297a0bd6c1a053a9079211d40531ffe54 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:20:36 +0900 Subject: [PATCH 14/15] =?UTF-8?q?test:=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EB=A1=9C=EB=98=90=20=EB=B2=88=ED=98=B8=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BC=80?= =?UTF-8?q?=EC=9D=B4=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 - 빈 문자열, 범위를 넘는 수, 0, 음수에 대한 테스트 케이스를 추가함 --- src/test/java/lotto/LottoTest.java | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 309f4e50ae..d36f086848 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -21,5 +21,31 @@ class LottoTest { .isInstanceOf(IllegalArgumentException.class); } - // TODO: 추가 기능 구현에 따른 테스트 코드 작성 -} + @DisplayName("로또 번호에 빈 문자열이 있으면 예외가 발생한다.") + @Test + void 로또_번호에_빈_문자열이_있으면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, null))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호가 범위를 넘으면 예외가 발생한다.") + @Test + void 로또_번호가_범위를_넘으면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(47, 2, 3, 4, 5, 6))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호가 0이면 예외가 발생한다.") + @Test + void 로또_번호가_0이면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(0, 2, 3, 4, 5, 6))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호에 음수가 있으면 예외가 발생한다.") + @Test + void 로또_번호에_음수가_있으면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(-1, 2, 3, 4, 5, 6))) + .isInstanceOf(IllegalArgumentException.class); + } +} \ No newline at end of file From 468c7cc5bd2e7a6999d63acc33a2b30a7e63f4d9 Mon Sep 17 00:00:00 2001 From: Kim Chaewon <101501925+Dim-chae@users.noreply.github.com> Date: Mon, 4 Nov 2024 23:55:53 +0900 Subject: [PATCH 15/15] =?UTF-8?q?refactor:=20=EA=B0=9D=EC=B2=B4=EC=A7=80?= =?UTF-8?q?=ED=96=A5=20=EC=9B=90=EC=B9=99=EC=97=90=20=EB=94=B0=EB=A5=B8=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Lotto: 로또 번호 관리 및 유효성 검사 - LottoGenerator: 랜덤 로또 번호 생성 - LottoGame: 로또 게임 진행 및 결과 계산 - InputProcessor: 사용자 입력 처리 - InputValidator: 입력값 유효성 검사 - WinningResult: 당첨 결과 관리 및 통계 계산 - LottoRank: 당첨 등수 및 상금 정의 (enum) - Application: 메인 애플리케이션 실행 --- src/main/java/lotto/Application.java | 89 +++---------------------- src/main/java/lotto/InputProcessor.java | 47 +++++++++++++ src/main/java/lotto/InputValidator.java | 60 +++++++++++++++++ src/main/java/lotto/Lotto.java | 6 +- src/main/java/lotto/LottoGame.java | 44 ++++++++++++ src/main/java/lotto/LottoGenerator.java | 11 +++ src/main/java/lotto/LottoRank.java | 41 ++++++++++++ src/main/java/lotto/WinningResult.java | 37 ++++++++++ 8 files changed, 253 insertions(+), 82 deletions(-) create mode 100644 src/main/java/lotto/InputProcessor.java create mode 100644 src/main/java/lotto/InputValidator.java create mode 100644 src/main/java/lotto/LottoGame.java create mode 100644 src/main/java/lotto/LottoGenerator.java create mode 100644 src/main/java/lotto/LottoRank.java create mode 100644 src/main/java/lotto/WinningResult.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 6bfc8c4ec6..f35c7735f5 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,88 +1,19 @@ package lotto; -import camp.nextstep.edu.missionutils.Console; -import camp.nextstep.edu.missionutils.Randoms; -import java.util.ArrayList; -import java.util.List; - public class Application { public static void main(String[] args) { - System.out.println("구입금액을 입력해 주세요."); - int lottoCount = 0; - int money = Integer.parseInt(Console.readLine()); try { - if (money <= 0) { - throw new IllegalArgumentException("[ERROR] 구입금액은 양수여야 합니다."); - } - if (money < 1000 || money % 1000 != 0) { - throw new IllegalArgumentException("[ERROR] 구입금액은 1000원 단위여야 합니다."); - } - lottoCount = money / 1000; - } catch (NumberFormatException e) { - throw new IllegalArgumentException("[ERROR] 구입금액은 숫자로 입력해야 합니다."); - } - - List lottos = new ArrayList<>(); - for (int i = 0; i < lottoCount; i++) { - List numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); - lottos.add(new Lotto(numbers)); - } + int money = InputProcessor.getPurchaseAmount(); + LottoGame game = new LottoGame(money); + game.printLottos(); - System.out.println(lottoCount + "개를 구매했습니다."); - for (Lotto lotto : lottos) { - System.out.println(lotto.getNumbers()); - } - - System.out.println("\n당첨 번호를 입력해 주세요."); - String lottoNumber = Console.readLine(); - List winningNumbers = new ArrayList<>(); - String[] eachNumbers = lottoNumber.split(","); + Lotto winningLotto = InputProcessor.getWinningLotto(); + int bonusNumber = InputProcessor.getBonusNumber(winningLotto.getNumbers()); - for (String number : eachNumbers) { - winningNumbers.add(Integer.parseInt(number)); + game.checkWinningLottos(winningLotto, bonusNumber); + game.printWinningStatistics(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); } - - System.out.println("\n보너스 번호를 입력해 주세요."); - int bonusNumber = 0; - try { - bonusNumber = Integer.parseInt(Console.readLine()); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("[ERROR] 번호는 숫자로 입력해야 합니다."); - } - - int[] matchCounts = new int[8]; - - for (Lotto lotto : lottos) { - int matches = 0; - boolean hasBonus = false; - for (int number : lotto.getNumbers()) { - if (winningNumbers.contains(number)) { - matches++; - } - if (number == bonusNumber) { - hasBonus = true; - } - } - if (matches == 5 && hasBonus) { - matchCounts[7]++; - } - if (matches >= 3) { - matchCounts[matches]++; - } - } - - System.out.println("\n당첨 통계"); - System.out.println("---"); - System.out.println("3개 일치 (5,000원) - " + matchCounts[3] + "개"); - System.out.println("4개 일치 (50,000원) - " + matchCounts[4] + "개"); - System.out.println("5개 일치 (1,500,000원) - " + matchCounts[5] + "개"); - System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + matchCounts[7] + "개"); - System.out.println("6개 일치 (2,000,000,000원) - " + matchCounts[6] + "개"); - - long totalMoney = matchCounts[3] * 5_000L + matchCounts[4] * 50_000L + - matchCounts[5] * 1_500_000L + matchCounts[7] * 30_000_000L + matchCounts[6] * 2_000_000_000L; - - double returnRate = totalMoney * 100.0 / money; - System.out.printf("총 수익률은 %.1f%%입니다.\n", returnRate); } -} +} \ No newline at end of file diff --git a/src/main/java/lotto/InputProcessor.java b/src/main/java/lotto/InputProcessor.java new file mode 100644 index 0000000000..50718dbc5f --- /dev/null +++ b/src/main/java/lotto/InputProcessor.java @@ -0,0 +1,47 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class InputProcessor { + public static int getPurchaseAmount() { + System.out.println("구입금액을 입력해 주세요."); + String input = Console.readLine(); + int money = parseInteger(input); + InputValidator.validatePurchaseAmount(money); + return money; + } + + public static Lotto getWinningLotto() { + System.out.println("\n당첨 번호를 입력해 주세요."); + String input = Console.readLine(); + List numbers = parseIntegerList(input); + InputValidator.validateWinningNumbers(numbers); + return new Lotto(numbers); + } + + public static int getBonusNumber(List winningNumbers) { + System.out.println("\n보너스 번호를 입력해 주세요."); + String input = Console.readLine(); + int bonusNumber = parseInteger(input); + InputValidator.validateBonusNumber(bonusNumber, winningNumbers); + return bonusNumber; + } + + private static int parseInteger(String input) { + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 올바른 숫자를 입력해주세요."); + } + } + + private static List parseIntegerList(String input) { + return Arrays.stream(input.split(",")) + .map(String::trim) + .map(InputProcessor::parseInteger) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/src/main/java/lotto/InputValidator.java b/src/main/java/lotto/InputValidator.java new file mode 100644 index 0000000000..d803ef0075 --- /dev/null +++ b/src/main/java/lotto/InputValidator.java @@ -0,0 +1,60 @@ +package lotto; + +import java.util.List; + +public class InputValidator { + private static final int LOTTO_PRICE = 1000; + private static final int LOTTO_NUMBER_COUNT = 6; + private static final int MIN_LOTTO_NUMBER = 1; + private static final int MAX_LOTTO_NUMBER = 45; + + public static void validatePurchaseAmount(int money) { + if (money <= 0) { + throw new IllegalArgumentException("[ERROR] 구입금액은 양수여야 합니다."); + } + if (money % LOTTO_PRICE != 0) { + throw new IllegalArgumentException("[ERROR] 구입금액은 " + LOTTO_PRICE + "원 단위여야 합니다."); + } + } + + public static void validateWinningNumbers(List numbers) { + validateNumberCount(numbers); + validateNoDuplicates(numbers); + validateNumberRange(numbers); + } + + public static void validateBonusNumber(int bonusNumber, List winningNumbers) { + validateNumberRange(bonusNumber); + if (winningNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException("[ERROR] 보너스 번호는 당첨 번호와 중복될 수 없습니다."); + } + } + + private static void validateNumberCount(List numbers) { + if (numbers.size() != LOTTO_NUMBER_COUNT) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 " + LOTTO_NUMBER_COUNT + "개여야 합니다."); + } + } + + private static void validateNoDuplicates(List numbers) { + if (numbers.stream().distinct().count() != LOTTO_NUMBER_COUNT) { + throw new IllegalArgumentException("[ERROR] 로또 번호들 중 중복된 숫자가 있습니다."); + } + } + + private static void validateNumberRange(List numbers) { + if (numbers.stream().anyMatch(n -> !isValidLottoNumber(n))) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 " + MIN_LOTTO_NUMBER + "부터 " + MAX_LOTTO_NUMBER + " 사이의 숫자여야 합니다."); + } + } + + private static void validateNumberRange(int number) { + if (!isValidLottoNumber(number)) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 " + MIN_LOTTO_NUMBER + "부터 " + MAX_LOTTO_NUMBER + " 사이의 숫자여야 합니다."); + } + } + + private static boolean isValidLottoNumber(int number) { + return number >= MIN_LOTTO_NUMBER && number <= MAX_LOTTO_NUMBER; + } +} \ No newline at end of file diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 7917acd2f4..00aef747ba 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -9,8 +9,8 @@ public class Lotto { public Lotto(List numbers) { validate(numbers); - Collections.sort(numbers); - this.numbers = numbers; + this.numbers = new ArrayList<>(numbers); + Collections.sort(this.numbers); } private void validate(List numbers) { @@ -28,4 +28,4 @@ private void validate(List numbers) { public List getNumbers() { return new ArrayList<>(numbers); } -} +} \ No newline at end of file diff --git a/src/main/java/lotto/LottoGame.java b/src/main/java/lotto/LottoGame.java new file mode 100644 index 0000000000..654d3ae90e --- /dev/null +++ b/src/main/java/lotto/LottoGame.java @@ -0,0 +1,44 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; + +public class LottoGame { + private final List lottos; + private final WinningResult winningResult; + private final int purchaseAmount; + + public LottoGame(int money) { + this.purchaseAmount = money; + this.lottos = new ArrayList<>(); + this.winningResult = new WinningResult(); + buyLottos(money); + } + + private void buyLottos(int money) { + int lottoCount = money / 1000; + for (int i = 0; i < lottoCount; i++) { + lottos.add(LottoGenerator.generate()); + } + } + + public void printLottos() { + System.out.println(lottos.size() + "개를 구매했습니다."); + for (Lotto lotto : lottos) { + System.out.println(lotto.getNumbers()); + } + } + + public void checkWinningLottos(Lotto winningLotto, int bonusNumber) { + for (Lotto lotto : lottos) { + LottoRank rank = LottoRank.getRank(lotto, winningLotto, bonusNumber); + winningResult.addWinningRank(rank); + } + } + + public void printWinningStatistics() { + winningResult.printStatistics(); + double returnRate = winningResult.calculateReturnRate(purchaseAmount); + System.out.printf("총 수익률은 %.1f%%입니다.\n", returnRate); + } +} \ No newline at end of file diff --git a/src/main/java/lotto/LottoGenerator.java b/src/main/java/lotto/LottoGenerator.java new file mode 100644 index 0000000000..4049f900a6 --- /dev/null +++ b/src/main/java/lotto/LottoGenerator.java @@ -0,0 +1,11 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.List; + +public class LottoGenerator { + public static Lotto generate() { + List numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + return new Lotto(numbers); + } +} \ No newline at end of file diff --git a/src/main/java/lotto/LottoRank.java b/src/main/java/lotto/LottoRank.java new file mode 100644 index 0000000000..ff623497b3 --- /dev/null +++ b/src/main/java/lotto/LottoRank.java @@ -0,0 +1,41 @@ +package lotto; + +public enum LottoRank { + FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원)"), + SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원)"), + THIRD(5, 1_500_000, "5개 일치 (1,500,000원)"), + FOURTH(4, 50_000, "4개 일치 (50,000원)"), + FIFTH(3, 5_000, "3개 일치 (5,000원)"), + NONE(0, 0, ""); + + private final int matchCount; + private final int prize; + private final String description; + + LottoRank(int matchCount, int prize, String description) { + this.matchCount = matchCount; + this.prize = prize; + this.description = description; + } + + public static LottoRank getRank(Lotto lotto, Lotto winningLotto, int bonusNumber) { + int matches = (int) lotto.getNumbers().stream() + .filter(winningLotto.getNumbers()::contains) + .count(); + + if (matches == 6) return FIRST; + if (matches == 5 && lotto.getNumbers().contains(bonusNumber)) return SECOND; + if (matches == 5) return THIRD; + if (matches == 4) return FOURTH; + if (matches == 3) return FIFTH; + return NONE; + } + + public int getPrize() { + return prize; + } + + public String getDescription() { + return description; + } +} \ No newline at end of file diff --git a/src/main/java/lotto/WinningResult.java b/src/main/java/lotto/WinningResult.java new file mode 100644 index 0000000000..05cc236382 --- /dev/null +++ b/src/main/java/lotto/WinningResult.java @@ -0,0 +1,37 @@ +package lotto; + +import java.util.EnumMap; +import java.util.Map; + +public class WinningResult { + private final Map rankCounts; + + public WinningResult() { + this.rankCounts = new EnumMap<>(LottoRank.class); + for (LottoRank rank : LottoRank.values()) { + rankCounts.put(rank, 0); + } + } + + public void addWinningRank(LottoRank rank) { + rankCounts.put(rank, rankCounts.get(rank) + 1); + } + + public void printStatistics() { + System.out.println("\n당첨 통계"); + System.out.println("---"); + for (LottoRank rank : LottoRank.values()) { + if (rank != LottoRank.NONE) { + System.out.printf("%s - %d개\n", rank.getDescription(), rankCounts.get(rank)); + } + } + } + + public double calculateReturnRate(int purchaseAmount) { + long totalPrize = 0; + for (LottoRank rank : LottoRank.values()) { + totalPrize += (long) rankCounts.get(rank) * rank.getPrize(); + } + return (double) totalPrize * 100 / purchaseAmount; + } +} \ No newline at end of file