From 6c247e2c56053f6f1012514ba3c7e646ac19bee9 Mon Sep 17 00:00:00 2001 From: nahyunKoo Date: Wed, 20 Mar 2024 00:23:56 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=EB=8B=A4=EB=A5=B8=20=EB=A0=88=ED=8F=AC?= =?UTF-8?q?=EC=A7=80=ED=86=A0=EB=A6=AC=EB=A5=BC=20fork=20=ED=95=9C=20?= =?UTF-8?q?=EC=9D=B4=EC=8A=88=EB=A1=9C=20=EB=AA=B0=EC=95=84=EC=84=9C=20com?= =?UTF-8?q?mit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. inputHelper 클래스를 새로 생성하여 메소드를 사용해 코드의 재사용성 증가 2. 로또 생성 출력 완료 3. 당첨번호 입력 받는 부분 구현 완료 --- src/main/java/lotto/Application.java | 37 ++++++++++++++++- src/main/java/lotto/InputHelper.java | 32 ++++++++++++++ src/main/java/lotto/Lotto.java | 62 +++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 src/main/java/lotto/InputHelper.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..d7ab1de 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,42 @@ package lotto; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + + int purchaseAmount, numLotto; + List purchasedLottos = new ArrayList<>(); + + purchaseAmount = InputHelper.getIntInput("구입금액을 입력해 주세요."); + + //구매한 로또 개수 구하고 출력 + numLotto = Lotto.getNumberOfLotto(purchaseAmount); + System.out.println(numLotto + "개를 구매했습니다."); + + //로또 생성 + Lotto.generateLotto(purchasedLottos, numLotto); + //구매한 로또 출력 + for(int i = 0; i winningNumbers = InputHelper.getListInput("당첨 번호를 입력해 주세요."); + if(Lotto.isWinningNumberValuable(winningNumbers) == false){ + //보너스 번호 입력받기 + throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); + } + + int n = InputHelper.getIntInput("보너스 번호를 입력해 주세요."); + //보너스 번호 범위 확인 + if(n < 1 || n > 45){ + throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); + } + + winningNumbers.add(n); + + } } diff --git a/src/main/java/lotto/InputHelper.java b/src/main/java/lotto/InputHelper.java new file mode 100644 index 0000000..832a675 --- /dev/null +++ b/src/main/java/lotto/InputHelper.java @@ -0,0 +1,32 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import static java.lang.Integer.parseInt; + +public class InputHelper { + private static final Scanner scanner = new Scanner(System.in); + + public static int getIntInput(String prompt){ + System.out.println(prompt); + int num = scanner.nextInt(); + scanner.nextLine(); //개행 처리 + return num; + } + + public static List getListInput(String prompt){ + List list = new ArrayList<>(); + System.out.println(prompt); + String line = scanner.nextLine(); + String[] str = line.split(","); + if(str.length == 6){ + for(int i = 0; i < str.length; i++){ + list.add(Integer.parseInt(str[i])); + } + return list; + } + throw new IllegalArgumentException("[ERROR]당첨숫자의 개수를 잘못 입력했습니다."); + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d..6fcdc1e 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,9 +1,14 @@ package lotto; -import java.util.List; +import org.kokodak.Randoms; + +import java.util.random.*; + +import java.util.*; public class Lotto { private final List numbers; + public static final int LOTTO_PRICE = 1000; public Lotto(List numbers) { validate(numbers); @@ -16,5 +21,60 @@ private void validate(List numbers) { } } + public List getLottoNum(){ + return numbers; + } + + //구매한 로또 개수 구하기 + public static int getNumberOfLotto(int price){ + if(price % Lotto.LOTTO_PRICE == 0) + return price / Lotto.LOTTO_PRICE; + + throw new IllegalArgumentException("[ERROR]구입금액이 정수개로 나누어 떨어지지 않습니다!"); + } + //로또 개수만큼 로또 생성하기 + public static void generateLotto(List lottoList, int num){ + for(int i = 0; i numberList = Randoms.pickUniqueNumbersInRange(1, 45, 6); + Collections.sort(numberList); + Lotto lotto = new Lotto(numberList); + lottoList.add(lotto); + } + } + + public static boolean isWinningNumberValuable(List numbers){ + for(int i = 0; i 45){ + return false; + } + } + return true; + } + + public static void printWinningResults(List winningNums, List lottos, int purchaseP) { + int first, second, third, fourth, fifth; + double earningRate; + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - " + fifth + "개"); + System.out.println("4개 일치 (50,000원) - " + fourth + "개"); + System.out.println("5개 일치 (1,500,000원) - " + third + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + second + "개"); + System.out.println("6개 일치 (2,000,000,000원) - " + first + "개"); + earningRate = (fifth * 5 + fourth * 50 + third * 1500 + second * 30000 + first * 2000000) / (purchaseP / 1000) * 100; + System.out.println("총 수익률은 " + earningRate + "입니다."); + + } + + public static int checkWinningResults(List winningNums, List lottos, int num){ + int numOfWinnings = 0; + for(int i = 0; i Date: Wed, 20 Mar 2024 01:27:02 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=EB=B2=88=ED=98=B8=EB=9E=91=20=EB=B9=84=EA=B5=90=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 19 ++++++++++------ src/main/java/lotto/Lotto.java | 34 ++++++++++------------------ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d7ab1de..40b5ad1 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,5 @@ package lotto; -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; +import java.util.*; public class Application { public static void main(String[] args) { @@ -9,6 +7,8 @@ public static void main(String[] args) { int purchaseAmount, numLotto; List purchasedLottos = new ArrayList<>(); + int[] result = {0, 0, 0, 0, 0, 0}; + purchaseAmount = InputHelper.getIntInput("구입금액을 입력해 주세요."); //구매한 로또 개수 구하고 출력 @@ -28,15 +28,20 @@ public static void main(String[] args) { //보너스 번호 입력받기 throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); } + Collections.sort(winningNumbers); - int n = InputHelper.getIntInput("보너스 번호를 입력해 주세요."); + //보너스 번호 입력받기 + int bonus = InputHelper.getIntInput("보너스 번호를 입력해 주세요."); //보너스 번호 범위 확인 - if(n < 1 || n > 45){ + if(bonus < 1 || bonus > 45){ throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); } - winningNumbers.add(n); - + for(int i = 0; i numbers){ return true; } - public static void printWinningResults(List winningNums, List lottos, int purchaseP) { - int first, second, third, fourth, fifth; - double earningRate; - System.out.println("당첨 통계"); - System.out.println("---"); - System.out.println("3개 일치 (5,000원) - " + fifth + "개"); - System.out.println("4개 일치 (50,000원) - " + fourth + "개"); - System.out.println("5개 일치 (1,500,000원) - " + third + "개"); - System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + second + "개"); - System.out.println("6개 일치 (2,000,000,000원) - " + first + "개"); - earningRate = (fifth * 5 + fourth * 50 + third * 1500 + second * 30000 + first * 2000000) / (purchaseP / 1000) * 100; - System.out.println("총 수익률은 " + earningRate + "입니다."); - } - - public static int checkWinningResults(List winningNums, List lottos, int num){ + public static int checkWinningResults(List winningNums, Lotto lottos, int bonusN){ int numOfWinnings = 0; - for(int i = 0; i Date: Wed, 20 Mar 2024 01:27:48 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B2=B0=EA=B3=BC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8A=94=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=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 | 4 +++- src/main/java/lotto/Lotto.java | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 40b5ad1..7419b5b 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -42,6 +42,8 @@ public static void main(String[] args) { int rank = Lotto.checkWinningResults(winningNumbers, purchasedLottos.get(i), bonus); result[rank] += 1; } - + + Lotto.printWinningResults(result, purchaseAmount); + } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 12938d8..3db9e0b 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -50,8 +50,6 @@ public static boolean isWinningNumberValuable(List numbers){ } return true; } - - public static int checkWinningResults(List winningNums, Lotto lottos, int bonusN){ int numOfWinnings = 0; for (int number : lottos.numbers) { @@ -67,4 +65,19 @@ public static int checkWinningResults(List winningNums, Lotto lottos, i } return 1; } + + public static void printWinningResults(int[] result, int purchaseP) { + int first, second, third, fourth, fifth; + double earningRate; + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - " + result[5] + "개"); + System.out.println("4개 일치 (50,000원) - " + result[4] + "개"); + System.out.println("5개 일치 (1,500,000원) - " + result[3] + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + result[2] + "개"); + System.out.println("6개 일치 (2,000,000,000원) - " + result[1] + "개"); + earningRate = (result[5] * 5 + result[4] * 50 + result[3] * 1500 + result[2] * 30000 + result[1] * 2000000) / (purchaseP / 1000) * 100; + System.out.println("총 수익률은 " + earningRate + "입니다."); + } + } From 2e7e081115eb0f2b6cbee13fb94f39cc1e4a9b13 Mon Sep 17 00:00:00 2001 From: nahyunKoo Date: Wed, 20 Mar 2024 01:53:05 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=EB=A9=94=EC=9D=B8=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EA=B0=84=EA=B2=B0=ED=95=98=EA=B2=8C=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 66 +++++++++++++++++----------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 7419b5b..954aa7b 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -3,47 +3,61 @@ public class Application { public static void main(String[] args) { - - int purchaseAmount, numLotto; List purchasedLottos = new ArrayList<>(); + int[] result; - int[] result = {0, 0, 0, 0, 0, 0}; - - purchaseAmount = InputHelper.getIntInput("구입금액을 입력해 주세요."); - - //구매한 로또 개수 구하고 출력 - numLotto = Lotto.getNumberOfLotto(purchaseAmount); + //구입 금액 입력 받은 후 로또 개수 구하기 + int purchaseAmount = InputHelper.getIntInput("구입금액을 입력해 주세요."); + int numLotto = Lotto.getNumberOfLotto(purchaseAmount); System.out.println(numLotto + "개를 구매했습니다."); - //로또 생성 + //로또 생성 후 생성한 로또들 출력 Lotto.generateLotto(purchasedLottos, numLotto); - //구매한 로또 출력 - for(int i = 0; i winningNumbers = getWinningNumbers(); + + //보너스 번호 입력받기 + int bonus = getBonusNumber(); + + //로또 비교하기 + result = getResult(purchasedLottos, winningNumbers, bonus); + + //결과 출력하기 + Lotto.printWinningResults(result, purchaseAmount); + } + + + + private static void printPurchasedLottos(List purchased){ + for(int i = 0; i getWinningNumbers() { List winningNumbers = InputHelper.getListInput("당첨 번호를 입력해 주세요."); - if(Lotto.isWinningNumberValuable(winningNumbers) == false){ - //보너스 번호 입력받기 + if (!Lotto.isWinningNumberValuable(winningNumbers)) { throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); } Collections.sort(winningNumbers); + return winningNumbers; + } - //보너스 번호 입력받기 - int bonus = InputHelper.getIntInput("보너스 번호를 입력해 주세요."); - //보너스 번호 범위 확인 - if(bonus < 1 || bonus > 45){ + private static int getBonusNumber(){ + int bonusN = InputHelper.getIntInput("보너스 번호를 입력해 주세요."); + if(bonusN < 1 || bonusN > 45){ throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); } + return bonusN; + } - - for(int i = 0; i purchased, List winningNumbers, int bonus){ + int[] result = {0, 0, 0, 0, 0, 0}; + for(int i = 0; i Date: Wed, 20 Mar 2024 01:56:06 +0900 Subject: [PATCH 5/7] commit test --- src/main/java/lotto/Application.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 954aa7b..63185e2 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -14,6 +14,7 @@ public static void main(String[] args) { //로또 생성 후 생성한 로또들 출력 Lotto.generateLotto(purchasedLottos, numLotto); Application.printPurchasedLottos(purchasedLottos); + System.out.println(1); //당첨번호 입력받기 List winningNumbers = getWinningNumbers(); From c72e07756109330dbfbe02ab987548b520c4bf42 Mon Sep 17 00:00:00 2001 From: nahyunKoo Date: Wed, 20 Mar 2024 11:15:35 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EB=8B=AC=EA=B3=A0=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=9C=84=EC=B9=98=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 24 +++++++++++++++++++++--- src/main/java/lotto/InputHelper.java | 3 +++ src/main/java/lotto/Lotto.java | 24 +++++++----------------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 63185e2..b2a30a7 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -14,7 +14,6 @@ public static void main(String[] args) { //로또 생성 후 생성한 로또들 출력 Lotto.generateLotto(purchasedLottos, numLotto); Application.printPurchasedLottos(purchasedLottos); - System.out.println(1); //당첨번호 입력받기 List winningNumbers = getWinningNumbers(); @@ -26,16 +25,18 @@ public static void main(String[] args) { result = getResult(purchasedLottos, winningNumbers, bonus); //결과 출력하기 - Lotto.printWinningResults(result, purchaseAmount); + Application.printWinningResults(result, purchaseAmount); } - + //구매한 로또 목록 출력하는 메소드 private static void printPurchasedLottos(List purchased){ for(int i = 0; i getWinningNumbers() { List winningNumbers = InputHelper.getListInput("당첨 번호를 입력해 주세요."); if (!Lotto.isWinningNumberValuable(winningNumbers)) { @@ -45,6 +46,7 @@ private static List getWinningNumbers() { return winningNumbers; } + //보너스 번호 입력 받는 메소드 private static int getBonusNumber(){ int bonusN = InputHelper.getIntInput("보너스 번호를 입력해 주세요."); if(bonusN < 1 || bonusN > 45){ @@ -53,6 +55,7 @@ private static int getBonusNumber(){ return bonusN; } + //구매한 모든 로또에 대해 반복문을 돌면서 당첨 확인한 후 결과를 저장하는 메소드 private static int[] getResult(List purchased, List winningNumbers, int bonus){ int[] result = {0, 0, 0, 0, 0, 0}; for(int i = 0; i purchased, List winningNumbe } return result; } + + //결과 출력하는 메소드 + private static void printWinningResults(int[] result, int purchaseP) { + int first, second, third, fourth, fifth; + double earningRate; + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - " + result[5] + "개"); + System.out.println("4개 일치 (50,000원) - " + result[4] + "개"); + System.out.println("5개 일치 (1,500,000원) - " + result[3] + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + result[2] + "개"); + System.out.println("6개 일치 (2,000,000,000원) - " + result[1] + "개"); + earningRate = (result[5] * 5 + result[4] * 50 + result[3] * 1500 + result[2] * 30000 + result[1] * 2000000) / (purchaseP / 1000) * 100; + System.out.println("총 수익률은 " + earningRate + "입니다."); + } } diff --git a/src/main/java/lotto/InputHelper.java b/src/main/java/lotto/InputHelper.java index 832a675..7cf2cd0 100644 --- a/src/main/java/lotto/InputHelper.java +++ b/src/main/java/lotto/InputHelper.java @@ -9,6 +9,8 @@ public class InputHelper { private static final Scanner scanner = new Scanner(System.in); + + //정수를 입력받는 메소드 public static int getIntInput(String prompt){ System.out.println(prompt); int num = scanner.nextInt(); @@ -16,6 +18,7 @@ public static int getIntInput(String prompt){ return num; } + //리스트를 입력받는 메소드 public static List getListInput(String prompt){ List list = new ArrayList<>(); System.out.println(prompt); diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 3db9e0b..19d0cab 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,9 +1,7 @@ package lotto; import org.kokodak.Randoms; - import java.util.random.*; - import java.util.*; public class Lotto { @@ -20,7 +18,8 @@ private void validate(List numbers) { throw new IllegalArgumentException(); } } - + + //로또 넘버 반환하는 메소드 public List getLottoNum(){ return numbers; } @@ -29,9 +28,9 @@ public List getLottoNum(){ public static int getNumberOfLotto(int price){ if(price % Lotto.LOTTO_PRICE == 0) return price / Lotto.LOTTO_PRICE; - throw new IllegalArgumentException("[ERROR]구입금액이 정수개로 나누어 떨어지지 않습니다!"); } + //로또 개수만큼 로또 생성하기 public static void generateLotto(List lottoList, int num){ for(int i = 0; i lottoList, int num){ } } + //입력받은 당첨번호의 범위 확인하기 public static boolean isWinningNumberValuable(List numbers){ for(int i = 0; i 45){ @@ -50,6 +50,9 @@ public static boolean isWinningNumberValuable(List numbers){ } return true; } + + + //로또 번호 비교하는 메소드 public static int checkWinningResults(List winningNums, Lotto lottos, int bonusN){ int numOfWinnings = 0; for (int number : lottos.numbers) { @@ -66,18 +69,5 @@ public static int checkWinningResults(List winningNums, Lotto lottos, i return 1; } - public static void printWinningResults(int[] result, int purchaseP) { - int first, second, third, fourth, fifth; - double earningRate; - System.out.println("당첨 통계"); - System.out.println("---"); - System.out.println("3개 일치 (5,000원) - " + result[5] + "개"); - System.out.println("4개 일치 (50,000원) - " + result[4] + "개"); - System.out.println("5개 일치 (1,500,000원) - " + result[3] + "개"); - System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + result[2] + "개"); - System.out.println("6개 일치 (2,000,000,000원) - " + result[1] + "개"); - earningRate = (result[5] * 5 + result[4] * 50 + result[3] * 1500 + result[2] * 30000 + result[1] * 2000000) / (purchaseP / 1000) * 100; - System.out.println("총 수익률은 " + earningRate + "입니다."); - } } From e6f66635ee7f9a8469a3bc25d7bbb9328b43f2a2 Mon Sep 17 00:00:00 2001 From: nahyunKoo Date: Thu, 21 Mar 2024 16:13:19 +0900 Subject: [PATCH 7/7] =?UTF-8?q?TEST=203=EA=B0=9C=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 60 +++++++++++++++++----------- src/main/java/lotto/InputHelper.java | 22 +++++----- src/main/java/lotto/Lotto.java | 27 +++++++++++-- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index b2a30a7..c9fae81 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -3,29 +3,38 @@ public class Application { public static void main(String[] args) { - List purchasedLottos = new ArrayList<>(); - int[] result; - //구입 금액 입력 받은 후 로또 개수 구하기 - int purchaseAmount = InputHelper.getIntInput("구입금액을 입력해 주세요."); - int numLotto = Lotto.getNumberOfLotto(purchaseAmount); - System.out.println(numLotto + "개를 구매했습니다."); + try{ + List purchasedLottos = new ArrayList<>(); + int[] result; - //로또 생성 후 생성한 로또들 출력 - Lotto.generateLotto(purchasedLottos, numLotto); - Application.printPurchasedLottos(purchasedLottos); + //구입 금액 입력 받은 후 로또 개수 구하기 + int purchaseAmount = InputHelper.getIntInput("구입금액을 입력해 주세요."); + int numLotto = Lotto.getNumberOfLotto(purchaseAmount); + System.out.println(numLotto + "개를 구매했습니다."); - //당첨번호 입력받기 - List winningNumbers = getWinningNumbers(); + //로또 생성 후 생성한 로또들 출력 + Lotto.generateLotto(purchasedLottos, numLotto); + Application.printPurchasedLottos(purchasedLottos); - //보너스 번호 입력받기 - int bonus = getBonusNumber(); + //당첨번호 입력받기 + List winningNumbers = new ArrayList<>(getWinningNumbers()); - //로또 비교하기 - result = getResult(purchasedLottos, winningNumbers, bonus); + //보너스 번호 입력받기 + int bonus = getBonusNumber(); + Lotto.checkWinningNumberBonus(winningNumbers, bonus); + + + //로또 비교하기 + result = getResult(purchasedLottos, winningNumbers, bonus); + + //결과 출력하기 + Application.printWinningResults(result, purchaseAmount); + + } catch(IllegalArgumentException e){ + System.out.println(e.getMessage()); + } - //결과 출력하기 - Application.printWinningResults(result, purchaseAmount); } @@ -38,11 +47,17 @@ private static void printPurchasedLottos(List purchased){ //당첨 번호 입력받는 메소드 private static List getWinningNumbers() { - List winningNumbers = InputHelper.getListInput("당첨 번호를 입력해 주세요."); - if (!Lotto.isWinningNumberValuable(winningNumbers)) { + List winningNumbers = new ArrayList<>(InputHelper.getListInput("당첨 번호를 입력해 주세요.")); + Collections.sort(winningNumbers); + if(!Lotto.checkWinningNumbersRange(winningNumbers)){ + throw new IllegalArgumentException("[ERROR]로또 번호 개수는 6개입니다."); + } + if (!Lotto.checkWinningNumbersCount(winningNumbers)) { throw new IllegalArgumentException("[ERROR]번호의 범위는 1~45입니다."); } - Collections.sort(winningNumbers); + if(!Lotto.checkWinningNumbersDuplication(winningNumbers)){ + throw new IllegalArgumentException("[ERROR]로또 번호에 중복이 있습니다."); + } return winningNumbers; } @@ -67,7 +82,6 @@ private static int[] getResult(List purchased, List winningNumbe //결과 출력하는 메소드 private static void printWinningResults(int[] result, int purchaseP) { - int first, second, third, fourth, fifth; double earningRate; System.out.println("당첨 통계"); System.out.println("---"); @@ -76,7 +90,7 @@ private static void printWinningResults(int[] result, int purchaseP) { System.out.println("5개 일치 (1,500,000원) - " + result[3] + "개"); System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + result[2] + "개"); System.out.println("6개 일치 (2,000,000,000원) - " + result[1] + "개"); - earningRate = (result[5] * 5 + result[4] * 50 + result[3] * 1500 + result[2] * 30000 + result[1] * 2000000) / (purchaseP / 1000) * 100; - System.out.println("총 수익률은 " + earningRate + "입니다."); + earningRate = (double)((result[5] * 5 + result[4] * 50 + result[3] * 1500 + result[2] * 30000 + result[1] * 2000000)) / (purchaseP / 1000) * 100; + System.out.println("총 수익률은 " + earningRate + "%입니다."); } } diff --git a/src/main/java/lotto/InputHelper.java b/src/main/java/lotto/InputHelper.java index 7cf2cd0..ca847c8 100644 --- a/src/main/java/lotto/InputHelper.java +++ b/src/main/java/lotto/InputHelper.java @@ -1,28 +1,28 @@ package lotto; + import java.util.ArrayList; import java.util.List; -import java.util.Scanner; - -import static java.lang.Integer.parseInt; +import org.kokodak.Console; public class InputHelper { - private static final Scanner scanner = new Scanner(System.in); - //정수를 입력받는 메소드 - public static int getIntInput(String prompt){ - System.out.println(prompt); - int num = scanner.nextInt(); - scanner.nextLine(); //개행 처리 - return num; + public static int getIntInput(String prompt) { + try{ + System.out.println(prompt); + int num = Integer.parseInt(Console.readLine()); + return num; + } catch (IllegalArgumentException e){ + throw new IllegalArgumentException("[ERROR]유효한 숫자를 입력해주세요"); + } } //리스트를 입력받는 메소드 public static List getListInput(String prompt){ List list = new ArrayList<>(); System.out.println(prompt); - String line = scanner.nextLine(); + String line = Console.readLine(); String[] str = line.split(","); if(str.length == 6){ for(int i = 0; i < str.length; i++){ diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 19d0cab..efa2439 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -34,7 +34,7 @@ public static int getNumberOfLotto(int price){ //로또 개수만큼 로또 생성하기 public static void generateLotto(List lottoList, int num){ for(int i = 0; i numberList = Randoms.pickUniqueNumbersInRange(1, 45, 6); + List numberList = new ArrayList<>(Randoms.pickUniqueNumbersInRange(1, 45, 6)); Collections.sort(numberList); Lotto lotto = new Lotto(numberList); lottoList.add(lotto); @@ -42,8 +42,12 @@ public static void generateLotto(List lottoList, int num){ } //입력받은 당첨번호의 범위 확인하기 - public static boolean isWinningNumberValuable(List numbers){ - for(int i = 0; i numbers){ + return numbers.size() == 6; + } + + public static boolean checkWinningNumbersRange(List numbers){ + for(int i = 0; i < numbers.size(); i++){ if(numbers.get(i) < 1 || numbers.get(i) > 45){ return false; } @@ -51,6 +55,23 @@ public static boolean isWinningNumberValuable(List numbers){ return true; } + public static boolean checkWinningNumbersDuplication(List numbers){ + Set set = new HashSet<>(numbers); + if(set.size() != numbers.size()) + return false; + return true; + } + + public static void checkWinningNumberBonus(List numbers, int bonus){ + Set s = new HashSet<>(numbers); + List list = new ArrayList<>(numbers); + s.add(bonus); + list.add(bonus); + if(s.size() != list.size()){ + throw new IllegalArgumentException("[ERROR]로또 번호에 중복이 있습니다."); + } + } + //로또 번호 비교하는 메소드 public static int checkWinningResults(List winningNums, Lotto lottos, int bonusN){