From 63ea69f8312ec27348e8130d57dd2fb6a2323fb0 Mon Sep 17 00:00:00 2001 From: 20011730 Date: Mon, 18 Mar 2024 18:10:31 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B5=AC=EC=9E=85=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20(=EB=8B=A8=EC=9C=84?= =?UTF-8?q?=EB=8A=94=201,000=EC=9B=90=20=EB=8B=A8=EC=9C=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- src/main/java/lotto/Application.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 99385f2..50995bf 100644 --- a/build.gradle +++ b/build.gradle @@ -19,7 +19,7 @@ dependencies { java { toolchain { - languageVersion = JavaLanguageVersion.of(17) + languageVersion = JavaLanguageVersion.of(21) } } diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..e8ebaf6 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,15 @@ package lotto; +import java.util.*; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + + int lotto_price = 1000; + + Scanner scanner = new Scanner(System.in); + int money = scanner.nextInt(); + } } From d4991bcd179e144931b2b8b405f9abbf5c8dcb6f Mon Sep 17 00:00:00 2001 From: 20011730 Date: Thu, 21 Mar 2024 14:45:11 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B5=AC=EC=9E=85=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20-=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=EB=8A=94=201,000=EC=9B=90=20=EB=8B=A8=EC=9C=84=20-=20?= =?UTF-8?q?1000=EC=9B=90=EC=9C=BC=EB=A1=9C=20=EB=82=98=EB=88=84=EC=96=B4?= =?UTF-8?q?=20=EB=96=A8=EC=96=B4=EC=A7=80=EC=A7=80=20=EC=95=8A=EC=9D=84?= =?UTF-8?q?=EC=8B=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= 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 e8ebaf6..c2c3ba5 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -9,7 +9,16 @@ public static void main(String[] args) { int lotto_price = 1000; Scanner scanner = new Scanner(System.in); + + //System.out.println("로또 구입 금액을 입력해주세요."); + System.out.println("Please enter the amount of lotto purchase."); int money = scanner.nextInt(); + if (money % 1000 != 0){ + throw new IllegalArgumentException("This is not an appropriate value. Please check the amount again."); + } + + List numbers = new ArrayList<>(); + } } From b7e760a941964978a66673a4ac79910053f0e003 Mon Sep 17 00:00:00 2001 From: 20011730 Date: Thu, 21 Mar 2024 15:13:06 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B5=AC=EC=9E=85=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20-=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=EB=8A=94=201,000=EC=9B=90=20=EB=8B=A8=EC=9C=84=20-=20?= =?UTF-8?q?1000=EC=9B=90=EC=9C=BC=EB=A1=9C=20=EB=82=98=EB=88=84=EC=96=B4?= =?UTF-8?q?=20=EB=96=A8=EC=96=B4=EC=A7=80=EC=A7=80=20=EC=95=8A=EC=9D=84?= =?UTF-8?q?=EC=8B=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 당첨 번호 입력 --- src/main/java/lotto/Application.java | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index c2c3ba5..2b7c7be 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,24 +1,45 @@ package lotto; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.*; public class Application { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { // TODO: 프로그램 구현 int lotto_price = 1000; Scanner scanner = new Scanner(System.in); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //System.out.println("로또 구입 금액을 입력해주세요."); System.out.println("Please enter the amount of lotto purchase."); - int money = scanner.nextInt(); + + // int money = scanner.nextInt(); + int money = Integer.parseInt(br.readLine()); + if (money % 1000 != 0){ - throw new IllegalArgumentException("This is not an appropriate value. Please check the amount again."); + throw new IllegalArgumentException("[ERROR] This is not an appropriate value. Please check the amount again."); } List numbers = new ArrayList<>(); + System.out.println("Please enter winning number."); + + String str = br.readLine(); + List list = Arrays.asList(str.split(",")); + + if (list.size() > 6){ + throw new IllegalArgumentException("[ERROR] There are too many winning numbers."); + } + + for (String s : list){ + numbers.add(Integer.parseInt(s)); + } + + System.out.println(numbers); } } From 3654d46d8c44ab13c8c5858d20bd3d9b439745a2 Mon Sep 17 00:00:00 2001 From: 20011730 Date: Thu, 21 Mar 2024 15:18:02 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B5=AC=EC=9E=85=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20-=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=EB=8A=94=201,000=EC=9B=90=20=EB=8B=A8=EC=9C=84=20-=20?= =?UTF-8?q?1000=EC=9B=90=EC=9C=BC=EB=A1=9C=20=EB=82=98=EB=88=84=EC=96=B4?= =?UTF-8?q?=20=EB=96=A8=EC=96=B4=EC=A7=80=EC=A7=80=20=EC=95=8A=EC=9D=84?= =?UTF-8?q?=EC=8B=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 당첨 번호 입력 보너스 번호 입력 --- src/main/java/lotto/Application.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 2b7c7be..126735c 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) throws IOException { Scanner scanner = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + // 로또 구입 금액 입력 //System.out.println("로또 구입 금액을 입력해주세요."); System.out.println("Please enter the amount of lotto purchase."); @@ -26,6 +27,7 @@ public static void main(String[] args) throws IOException { List numbers = new ArrayList<>(); + // 당첨 번호 입력 System.out.println("Please enter winning number."); String str = br.readLine(); @@ -39,7 +41,11 @@ public static void main(String[] args) throws IOException { numbers.add(Integer.parseInt(s)); } - System.out.println(numbers); + // 보너스 번호 입력 + System.out.println("Please enter bonus number."); + str = br.readLine(); + numbers.add(Integer.parseInt(str)); + //System.out.println(numbers); } } From 81562d7643ca0c9d39646b39bcbc12713db1bec6 Mon Sep 17 00:00:00 2001 From: 20011730 Date: Thu, 21 Mar 2024 15:27:23 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B5=AC=EC=9E=85=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20-=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=EB=8A=94=201,000=EC=9B=90=20=EB=8B=A8=EC=9C=84=20-=20?= =?UTF-8?q?1000=EC=9B=90=EC=9C=BC=EB=A1=9C=20=EB=82=98=EB=88=84=EC=96=B4?= =?UTF-8?q?=20=EB=96=A8=EC=96=B4=EC=A7=80=EC=A7=80=20=EC=95=8A=EC=9D=84?= =?UTF-8?q?=EC=8B=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 당첨 번호 입력 - 당첨 번호를 7개 이상 입력 시 예외 처리 - 당첨 번호가 중복될 시 예외 처리 보너스 번호 입력 - 보너스 번호가 당첨 번호와 중복될 시 예외 처리 --- src/main/java/lotto/Application.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 126735c..a688e81 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -21,6 +21,7 @@ public static void main(String[] args) throws IOException { // int money = scanner.nextInt(); int money = Integer.parseInt(br.readLine()); + // 로또 구입 금액이 1000원 단위가 아닐 때 예외 처리 if (money % 1000 != 0){ throw new IllegalArgumentException("[ERROR] This is not an appropriate value. Please check the amount again."); } @@ -33,10 +34,19 @@ public static void main(String[] args) throws IOException { String str = br.readLine(); List list = Arrays.asList(str.split(",")); + // 당첨번호 7개 이상 입력 시 예외 처리 if (list.size() > 6){ throw new IllegalArgumentException("[ERROR] There are too many winning numbers."); } + // 당첨번호 중복 입력 시 예외 처리 + Set listSet = new HashSet<>(list); + + if (list.size() != listSet.size()){ + throw new IllegalArgumentException("[ERROR] There is a duplicate number."); + } + + // 문자열 리스트 -> 정수형 리스트로 변환 for (String s : list){ numbers.add(Integer.parseInt(s)); } @@ -46,6 +56,13 @@ public static void main(String[] args) throws IOException { str = br.readLine(); numbers.add(Integer.parseInt(str)); + // 보너스 번호 중복 시 예외 처리 + Set numbersSet = new HashSet<>(numbers); + + if (list.size() != listSet.size()){ + throw new IllegalArgumentException("[ERROR] There is a duplicate number."); + } + //System.out.println(numbers); } } From 0b7253f34c839a608779eff7f6eb5c35aa70458d Mon Sep 17 00:00:00 2001 From: 20011730 Date: Thu, 21 Mar 2024 16:06:09 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B5=AC=EC=9E=85=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20-=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=EB=8A=94=201,000=EC=9B=90=20=EB=8B=A8=EC=9C=84=20-=20?= =?UTF-8?q?1000=EC=9B=90=EC=9C=BC=EB=A1=9C=20=EB=82=98=EB=88=84=EC=96=B4?= =?UTF-8?q?=20=EB=96=A8=EC=96=B4=EC=A7=80=EC=A7=80=20=EC=95=8A=EC=9D=84?= =?UTF-8?q?=EC=8B=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 당첨 번호 입력 - 당첨 번호를 7개 이상 입력 시 예외 처리 - 당첨 번호가 중복될 시 예외 처리 보너스 번호 입력 - 보너스 번호가 당첨 번호와 중복될 시 예외 처리 구입된 복권 번호 출력 --- src/main/java/lotto/Application.java | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index a688e81..619012e 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,5 +1,7 @@ package lotto; +import org.kokodak.Randoms; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -26,16 +28,33 @@ public static void main(String[] args) throws IOException { throw new IllegalArgumentException("[ERROR] This is not an appropriate value. Please check the amount again."); } - List numbers = new ArrayList<>(); + // 로또 구입 결과 출력 + int lotto_amount = money / 1000; + System.out.println("Purchased " + lotto_amount + " lotto"); + + List> purchased_lotto = new ArrayList<>(); + + for (int i = 0; i < lotto_amount; i++){ + List RandomNumbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + Collections.sort(RandomNumbers); + + purchased_lotto.add(RandomNumbers); + } + + for (int i = 0; i < lotto_amount; i++){ + System.out.println(purchased_lotto.get(i)); + } // 당첨 번호 입력 + List numbers = new ArrayList<>(); + System.out.println("Please enter winning number."); String str = br.readLine(); List list = Arrays.asList(str.split(",")); // 당첨번호 7개 이상 입력 시 예외 처리 - if (list.size() > 6){ + if (list.size() != 6){ throw new IllegalArgumentException("[ERROR] There are too many winning numbers."); } @@ -59,7 +78,7 @@ public static void main(String[] args) throws IOException { // 보너스 번호 중복 시 예외 처리 Set numbersSet = new HashSet<>(numbers); - if (list.size() != listSet.size()){ + if (numbers.size() != numbersSet.size()){ throw new IllegalArgumentException("[ERROR] There is a duplicate number."); } From 7b71acb8ce12aaa9837138d3c9513c85e5170d81 Mon Sep 17 00:00:00 2001 From: 20011730 Date: Thu, 21 Mar 2024 17:12:47 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EA=B5=AC=EC=9E=85=20?= =?UTF-8?q?=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5=20-=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=EB=8A=94=201,000=EC=9B=90=20=EB=8B=A8=EC=9C=84=20-=20?= =?UTF-8?q?1000=EC=9B=90=EC=9C=BC=EB=A1=9C=20=EB=82=98=EB=88=84=EC=96=B4?= =?UTF-8?q?=20=EB=96=A8=EC=96=B4=EC=A7=80=EC=A7=80=20=EC=95=8A=EC=9D=84?= =?UTF-8?q?=EC=8B=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 당첨 번호 입력 - 당첨 번호를 7개 이상 입력 시 예외 처리 - 당첨 번호가 중복될 시 예외 처리 - 당첨 번호가 1~45의 범위를 넘어갈 때 예외 처리 보너스 번호 입력 - 보너스 번호가 당첨 번호와 중복될 시 예외 처리 구입된 복권 번호 출력 담청 결과 계산 당첨 결과 출력 --- src/main/java/lotto/Application.java | 64 +++++++++++++++++++++--- src/test/java/lotto/ApplicationTest.java | 7 ++- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 619012e..51d21a8 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.*; +import java.util.function.Predicate; +import java.util.stream.Collectors; public class Application { public static void main(String[] args) throws IOException { @@ -61,7 +63,7 @@ public static void main(String[] args) throws IOException { // 당첨번호 중복 입력 시 예외 처리 Set listSet = new HashSet<>(list); - if (list.size() != listSet.size()){ + if (list.size() != listSet.size()) { throw new IllegalArgumentException("[ERROR] There is a duplicate number."); } @@ -70,18 +72,68 @@ public static void main(String[] args) throws IOException { numbers.add(Integer.parseInt(s)); } + // 입력된 당첨번호가 1~45 범위가 아닐 때 예외처리 + for (Integer num : numbers){ + if (num < 1 || num > 45){ + throw new IllegalArgumentException("[ERROR] Invalid value."); + } + } + // 보너스 번호 입력 System.out.println("Please enter bonus number."); str = br.readLine(); - numbers.add(Integer.parseInt(str)); + int bonus = Integer.parseInt(str); // 보너스 번호 중복 시 예외 처리 - Set numbersSet = new HashSet<>(numbers); + for (int i = 0; i < numbers.size(); i++){ + if (numbers.get(i) == bonus){ + throw new IllegalArgumentException("[ERROR] There is a duplicate number."); + } + } - if (numbers.size() != numbersSet.size()){ - throw new IllegalArgumentException("[ERROR] There is a duplicate number."); + // 당첨 결과 계산 + int income = 0; + int[] result = new int[5]; + for (int i = 0; i < 5; i++) + result[i] = 0; + + for (List purchased_list : purchased_lotto){ + List matchList = purchased_list.stream().filter(o -> + numbers.stream().anyMatch(Predicate.isEqual(o))).toList(); + + if (matchList.size() == 6) { + income += 2000000000; + result[0]++; + + } + else if (matchList.size() == 5 && purchased_list.contains(bonus)) { + income += 30000000; + result[1]++; + } + else if (matchList.size() == 5) { + income += 1500000; + result[2]++; + } + else if (matchList.size() == 4) { + income += 50000; + result[3]++; + } + else if (matchList.size() == 3) { + income += 5000; + result[4]++; + } } - //System.out.println(numbers); + // 당첨 결과 출력 + System.out.println("winning statistics"); + System.out.println("---"); + System.out.println("Correct 3 (5,000) - " + result[4]); + System.out.println("Correct 4 (50,000) - " + result[3]); + System.out.println("Correct 5 (1,500,000) - " + result[2]); + System.out.println("Correct 5, Bonus (30,000,000) - " + result[1]); + System.out.println("Correct 6 (2,000,000,000) - " + result[0]); + + float total_return = (float)income / (float)money * 100.0f; + System.out.println("Total Return : " + total_return + "%"); } } diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index 404cbe1..d98258f 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -4,6 +4,7 @@ import static org.kokodak.test.Assertions.assertRandomUniqueNumbersInRangeTest; import static org.kokodak.test.Assertions.assertSimpleTest; +import java.io.IOException; import java.util.List; import org.junit.jupiter.api.Test; import org.kokodak.test.NsTest; @@ -55,6 +56,10 @@ class ApplicationTest extends NsTest { @Override public void runMain() { - Application.main(new String[]{}); + try { + Application.main(new String[]{}); + } catch (IOException e) { + throw new RuntimeException(e); + } } }