From 1587436ba505522729fcdcb03f20173b1b1e43a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 00:38:36 +0900 Subject: [PATCH 01/10] =?UTF-8?q?=EB=8B=A4=EC=96=91=ED=95=9C=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EC=9D=84=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Input.java | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/lotto/Input.java diff --git a/src/main/java/lotto/Input.java b/src/main/java/lotto/Input.java new file mode 100644 index 0000000..aeecdae --- /dev/null +++ b/src/main/java/lotto/Input.java @@ -0,0 +1,48 @@ +package lotto; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + + + +public class Input { + + public static int userBuyIn() { + Scanner sc1 = new Scanner(System.in); + int money = sc1.nextInt(); + + if(money % 1000 != 0) { + UserHelp.error(); + return -1; + } + + return money / 1000; + } + + public static List luckNumberIn() { + + Scanner sc = new Scanner(System.in); + String str = sc.next(); + + String intStr = str.replaceAll(",", ""); + String[] strArr = intStr.split(""); + List intList = new ArrayList<>(Arrays.asList(strArr)); + + List luckyNumber = new ArrayList<>(); + for (String s : intList) { + luckyNumber.add(Integer.parseInt(s)); + } + + return luckyNumber; + + } + + public static int plusNumberIn() { + Scanner sc = new Scanner(System.in); + return sc.nextInt(); + } + + +} From 469ce7ae78677e64ef051447d555d4b1309ae542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 00:38:46 +0900 Subject: [PATCH 02/10] =?UTF-8?q?=EB=8B=A4=EC=96=91=ED=95=9C=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=EC=9D=84=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Output.java | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/lotto/Output.java diff --git a/src/main/java/lotto/Output.java b/src/main/java/lotto/Output.java new file mode 100644 index 0000000..e9a3f87 --- /dev/null +++ b/src/main/java/lotto/Output.java @@ -0,0 +1,58 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; + +public class Output { + + public static int userBuyOut() { + System.out.println("구입금액을 입력해 주세요."); + + int count = Input.userBuyIn(); + if(count != -1) { + System.out.println(" "); + System.out.println(count +"개를 구매했습니다."); + } + + return count; + } + + public static void usernumber(ArrayList> numbers, int count) { + int i; + for(i = 0; i < count; ++i) { + System.out.println(numbers.get(i)); + } + + } + + public static List luckyNumberOut() { + System.out.println(" "); + System.out.println("당첨 번호를 입력해 주세요."); + + List luckyNumber = new ArrayList<>(); + luckyNumber = Input.luckNumberIn(); + + return luckyNumber; + } + + public static int plusNumberOut() { + System.out.println(" "); + System.out.println("보너스 번호를 입력해 주세요."); + + return Input.plusNumberIn(); + } + + public static void luckyResult(int[] win, double avg) { + System.out.println(" "); + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - "+ win[0] +"개"); + System.out.println("4개 일치 (50,000원) - "+ win[1] +"개"); + System.out.println("5개 일치 (1,500,000원) - "+ win[2] +"개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - "+ win[3] +"개"); + System.out.println("6개 일치 (2,000,000,000원) - "+ win[4] +"개"); + System.out.println("총 수익률은 " + avg + "%입니다."); + } + + +} From 537e67727130089ce2b08f6e87e575d8a3438f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 00:39:13 +0900 Subject: [PATCH 03/10] =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EB=B0=9C=EC=83=9D?= =?UTF-8?q?=20=EC=8B=9C=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8A=94=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/UserHelp.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/lotto/UserHelp.java diff --git a/src/main/java/lotto/UserHelp.java b/src/main/java/lotto/UserHelp.java new file mode 100644 index 0000000..906609c --- /dev/null +++ b/src/main/java/lotto/UserHelp.java @@ -0,0 +1,9 @@ +package lotto; + +public class UserHelp { + + public static void error() { + System.out.println("[ERROR]"); + } + +} From 8b619d09cb907c3a1d63db520113fa9fb12b81b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 00:40:30 +0900 Subject: [PATCH 04/10] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=EA=B0=80=20?= =?UTF-8?q?=EA=B5=AC=EB=A7=A4=ED=95=9C=20=EB=A1=9C=EB=98=90=EC=9D=98=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=EB=A5=BC=20=ED=95=98=EB=82=98=EC=9D=98=20?= =?UTF-8?q?=EB=A6=AC=EC=8A=A4=ED=8A=B8=EB=A1=9C=20=EB=A7=8C=EB=93=9C?= =?UTF-8?q?=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4,=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=20=EA=B0=AF=EC=88=98=EB=A5=BC=20=EC=84=B8=EB=8A=94=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4,=20=EC=88=98=EC=9D=B5=EB=A5=A0=EC=9D=84=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=ED=95=B4=EC=A3=BC=EB=8A=94=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 53 +++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d..36c5264 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,12 +1,17 @@ package lotto; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.Comparator; import java.util.List; + public class Lotto { private final List numbers; public Lotto(List numbers) { - validate(numbers); + validate(numbers); //String 검증용 this.numbers = numbers; } @@ -17,4 +22,50 @@ private void validate(List numbers) { } // TODO: 추가 기능 구현 + + public static ArrayList> LottoNumber(int count) { + + int i; + ArrayList> numberU = new ArrayList<>(); + + for (i = 0;i < count; ++i) { + List numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + numbers.sort(Comparator.naturalOrder()); + numberU.add(i, numbers); + } + + return numberU; + } + + public static int[] LottoWinning(ArrayList> numberU, List luckyNumber, int plusNumber, int count) { + int[] win = new int[5]; + int i; + + for(i = 0; i < count; ++i) { + int same = 0, p = 0; + boolean plus = numberU.get(i).contains(plusNumber); + if(plus) p = 1; + + numberU.get(i).retainAll(luckyNumber); + same = numberU.get(i).size(); + + if(same == 3) win[0] += 1; + if(same == 4) win[1] += 1; + if(same == 5) win[2] += 1; + if(same == 5 && p == 1) win[3] += 1; + if(same == 6) win[4] += 1; + } + + return win; + } + + public static double LottoReturnRate (int[] win, int count) { + int sum; + double avg; + + sum = win[0] * 5000 + win[1] * 50000 + win[2] * 1500000 + win[3] * 30000000 + win[4] * 2000000000; + avg = sum / (double)count; + + return avg; + } } From 0930d8a7c3444b22d8206d6290271be33032c197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 00:41:32 +0900 Subject: [PATCH 05/10] =?UTF-8?q?=EC=A3=BC=EB=A1=9C=20Output=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=A5=BC=20=ED=98=B8=EC=B6=9C=ED=95=B4?= =?UTF-8?q?=EC=84=9C=20=EC=BD=94=EB=93=9C=EB=A5=BC=20=EC=9E=91=EB=8F=99?= =?UTF-8?q?=EC=8B=9C=ED=82=A4=EB=8A=94=20main=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=EA=B0=80=20=EC=9E=88=EB=8A=94=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..40aec0c 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,29 @@ package lotto; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + + int count = Output.userBuyOut(); + + ArrayList> numberU = new ArrayList<>(); + numberU = lotto.Lotto.LottoNumber(count); + + Output.usernumber(numberU, count); + + List luckyNumber = new ArrayList<>(); + luckyNumber = Output.luckyNumberOut(); + + int plusNumber = Output.plusNumberOut(); + + int[] win = lotto.Lotto.LottoWinning(numberU, luckyNumber, plusNumber, count); + Output.luckyResult(win, lotto.Lotto.LottoReturnRate(win, count)); + } } + + From 92f9d18f417a2732a485b5b974cd261ea2ce8cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 12:06:12 +0900 Subject: [PATCH 06/10] =?UTF-8?q?main=20=EB=A9=94=EC=86=8C=EB=93=9C?= =?UTF-8?q?=EA=B0=80=20=EC=9E=88=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 40aec0c..c8b1f3e 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,5 @@ package lotto; -import camp.nextstep.edu.missionutils.Randoms; - import java.util.ArrayList; import java.util.List; @@ -18,11 +16,12 @@ public static void main(String[] args) { List luckyNumber = new ArrayList<>(); luckyNumber = Output.luckyNumberOut(); - int plusNumber = Output.plusNumberOut(); + int plusNumber = Output.plusNumberOut(luckyNumber); int[] win = lotto.Lotto.LottoWinning(numberU, luckyNumber, plusNumber, count); Output.luckyResult(win, lotto.Lotto.LottoReturnRate(win, count)); + } } From d50df9b81bf1ff7f246705ef395abdf6aa9d0e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 12:06:52 +0900 Subject: [PATCH 07/10] =?UTF-8?q?=EC=98=A4=EB=A5=98=EA=B0=80=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=ED=96=88=EC=9D=84=20=EB=95=8C=20=EB=AC=B8=EA=B5=AC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=ED=9B=84=20=EC=BD=94=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=EC=A2=85=EB=A3=8C=EC=8B=9C=ED=82=A4=EB=8A=94=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/UserHelp.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/UserHelp.java b/src/main/java/lotto/UserHelp.java index 906609c..ee06bd3 100644 --- a/src/main/java/lotto/UserHelp.java +++ b/src/main/java/lotto/UserHelp.java @@ -2,8 +2,24 @@ public class UserHelp { - public static void error() { - System.out.println("[ERROR]"); + public static void errorUserBuy() { + System.out.println("[ERROR] 지불한 금액이 많거나 적습니다."); + throw new IllegalArgumentException(); + } + + public static void errorLuckyNumber_size() { + System.out.println("[ERROR] 로또 번호의 개수는 6개입니다."); + throw new IllegalArgumentException(); + } + + public static void errorLuckyNumber_number() { + System.out.println("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); + throw new IllegalArgumentException(); + } + + public static void errorPlusNumber() { + System.out.println("[ERROR] 보너스 번호는 로또 당첨 번호에 없는 숫자여야 합니다."); + throw new IllegalArgumentException(); } } From 6575e3a54dcf7fd57697a8b9e33daccdbbb99492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 12:07:31 +0900 Subject: [PATCH 08/10] =?UTF-8?q?main=EC=97=90=EC=84=9C=20=EC=A3=BC?= =?UTF-8?q?=EB=A1=9C=20=ED=98=B8=EC=B6=9C=EB=90=98=EA=B3=A0=20Input?= =?UTF-8?q?=EC=9D=84=20=ED=98=B8=EC=B6=9C=ED=95=B4=20=EA=B0=92=EC=9D=84=20?= =?UTF-8?q?=EC=A0=84=EB=8B=AC=ED=95=98=EB=8A=94=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Output.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Output.java b/src/main/java/lotto/Output.java index e9a3f87..02decc2 100644 --- a/src/main/java/lotto/Output.java +++ b/src/main/java/lotto/Output.java @@ -30,16 +30,16 @@ public static List luckyNumberOut() { System.out.println("당첨 번호를 입력해 주세요."); List luckyNumber = new ArrayList<>(); - luckyNumber = Input.luckNumberIn(); + luckyNumber = Input.luckyNumberIn(); return luckyNumber; } - public static int plusNumberOut() { + public static int plusNumberOut(List luckyNumber) { System.out.println(" "); System.out.println("보너스 번호를 입력해 주세요."); - return Input.plusNumberIn(); + return Input.plusNumberIn(luckyNumber); } public static void luckyResult(int[] win, double avg) { From 04e352c20bad608183c604815f760c1bfacdd3a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 12:08:02 +0900 Subject: [PATCH 09/10] =?UTF-8?q?Output=EC=97=90=EC=84=9C=20=EC=A3=BC?= =?UTF-8?q?=EB=A1=9C=20=ED=98=B8=EC=B6=9C=EB=90=98=EA=B3=A0=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9E=90=EC=97=90=EA=B2=8C=20=EA=B0=92=EC=9D=84=20?= =?UTF-8?q?=EB=B0=9B=EC=95=84=20=EB=B0=98=ED=99=98=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Input.java | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/lotto/Input.java b/src/main/java/lotto/Input.java index aeecdae..3add272 100644 --- a/src/main/java/lotto/Input.java +++ b/src/main/java/lotto/Input.java @@ -14,20 +14,18 @@ public static int userBuyIn() { int money = sc1.nextInt(); if(money % 1000 != 0) { - UserHelp.error(); - return -1; + UserHelp.errorUserBuy(); } return money / 1000; } - public static List luckNumberIn() { + public static List luckyNumberIn() { Scanner sc = new Scanner(System.in); String str = sc.next(); - String intStr = str.replaceAll(",", ""); - String[] strArr = intStr.split(""); + String[] strArr = str.split(","); List intList = new ArrayList<>(Arrays.asList(strArr)); List luckyNumber = new ArrayList<>(); @@ -35,13 +33,28 @@ public static List luckNumberIn() { luckyNumber.add(Integer.parseInt(s)); } + if(luckyNumber.size() != 6) { + UserHelp.errorLuckyNumber_size(); + } + for (int i = 0; i < 6; ++i) { + if(luckyNumber.get(i) > 45 || luckyNumber.get(i) < 0) { + UserHelp.errorLuckyNumber_number(); + } + } + return luckyNumber; } - public static int plusNumberIn() { + public static int plusNumberIn(List luckyNumber) { Scanner sc = new Scanner(System.in); - return sc.nextInt(); + int plusNumber = sc.nextInt(); + boolean plus = luckyNumber.contains(plusNumber); + if(plus) { + UserHelp.errorPlusNumber(); + } + + return plusNumber; } From de5eab9ee72d17ccbcb79a25fc4f07346e884574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=A7=80=EC=9C=A4?= <23011595@sju.ac.kr> Date: Sat, 16 Mar 2024 12:08:56 +0900 Subject: [PATCH 10/10] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=EC=9D=98=20=EB=8C=80=EB=B6=80=EB=B6=84=EC=9D=84=20=EB=A7=A1?= =?UTF-8?q?=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 36c5264..e18a6a0 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -21,8 +21,6 @@ private void validate(List numbers) { } } - // TODO: 추가 기능 구현 - public static ArrayList> LottoNumber(int count) { int i; @@ -38,7 +36,7 @@ public static ArrayList> LottoNumber(int count) { } public static int[] LottoWinning(ArrayList> numberU, List luckyNumber, int plusNumber, int count) { - int[] win = new int[5]; + int[] win = {0, 0, 0, 0, 0}; int i; for(i = 0; i < count; ++i) { @@ -46,8 +44,8 @@ public static int[] LottoWinning(ArrayList> numberU, List boolean plus = numberU.get(i).contains(plusNumber); if(plus) p = 1; - numberU.get(i).retainAll(luckyNumber); - same = numberU.get(i).size(); + (numberU.get(i)).retainAll(luckyNumber); + same = (numberU.get(i)).size(); if(same == 3) win[0] += 1; if(same == 4) win[1] += 1; @@ -64,8 +62,10 @@ public static double LottoReturnRate (int[] win, int count) { double avg; sum = win[0] * 5000 + win[1] * 50000 + win[2] * 1500000 + win[3] * 30000000 + win[4] * 2000000000; - avg = sum / (double)count; + avg = sum / ((double)(count * 1000)) * 100; + + avg = Math.round(avg * 10); - return avg; + return avg / 10; } }