From 67e2ca487216e2a20ec88dd49f6de0c38c97cf24 Mon Sep 17 00:00:00 2001 From: swissmissed Date: Sat, 23 Mar 2024 22:39:37 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=AF=B8=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 112 +++++++++++++++++++++++++++ src/main/java/lotto/Lotto.java | 52 +++++++++++++ 2 files changed, 164 insertions(+) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..3bafc44 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,119 @@ package lotto; +import org.kokodak.Console; +import org.kokodak.Randoms; + +import java.util.*; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + + //로또 가격 + int lotto_price = 1000; + + //로또 구입 금액 입력 + System.out.println("구입금액을 입력해 주세요."); + int purchase = Integer.parseInt(Console.readLine()); + System.out.println(); + + //잘못된 구입 금액 예외처리 + if(purchase % lotto_price != 0){ + throw new IllegalArgumentException("[ERROR]"); + } + + //로또 개수 + int lotto_num = purchase / lotto_price; + + //로또 구입 + System.out.println(lotto_num + "개를 구매했습니다."); + ArrayList myLotto = new ArrayList<>(); + for(int i=0;i ns = Randoms.pickUniqueNumbersInRange(1,45,6); + Collections.sort(ns); + Lotto a = new Lotto(ns); + myLotto.add(a); + } + System.out.println(); + + //구입한 로또 출력 + for(Lotto i : myLotto) + i.printLotto(); + + //당첨 번호 입력 + System.out.println("당첨 번호를 입력해 주세요."); + List ns = new ArrayList<>(); + String s = Console.readLine(); + String[] n = s.split(","); + for(int i=0;i<6;i++){ + ns.add(Integer.parseInt(n[i])); + } + Collections.sort(ns); + + //예외처리 + //1미만, 45초과인 경우 예외처리 + if(ns.get(0) < 1 || ns.get(5) > 45){ + throw new IllegalArgumentException("[ERROR]"); + } + //중복된 번호가 있는 경우 예외 처리 + if(ns.size() != ns.stream().distinct().count()){ + throw new IllegalArgumentException("[ERROR]"); + } + + Lotto winLotto = new Lotto(ns); + System.out.println(); + + //보너스 번호 입력 + System.out.println("보너스 번호를 입력해 주세요."); + String bn = Console.readLine(); + int bonus_number = Integer.parseInt(bn); + + //예외 처리 + if(bonus_number < 1 || bonus_number > 45){ + throw new IllegalArgumentException("[ERROR]"); + } + System.out.println(); + + //당첨 통계 + System.out.println("당첨 통계"); + System.out.println("---"); + + //3 ~ 6개 일치한 개수 저장 + ArrayList prize = new ArrayList<>(){{ + for(int i=0;i<8;i++) + add(0); + }}; + + for(Lotto i : myLotto){ + int idx = i.winCheck(winLotto, bonus_number); + if(idx<3) + continue; + prize.set(idx, prize.get(idx) + 1); + } + + //통계 출력 + System.out.println(Lotto.accord.THREE.num + + "개 일치 (" + Lotto.accord.THREE.price + + ") - " + prize.get(Lotto.accord.THREE.num) + "개"); + System.out.println(Lotto.accord.FOUR.num + + "개 일치 (" + Lotto.accord.FOUR.price + + ") - " + prize.get(Lotto.accord.FOUR.num) + "개"); + System.out.println(Lotto.accord.FIVE.num + + "개 일치 (" + Lotto.accord.FIVE.price + + ") - " + prize.get(Lotto.accord.FIVE.num) + "개"); + System.out.println(Lotto.accord.FIVEBONUS.num - 1 + + "개 일치, 보너스 볼 일치 (" + Lotto.accord.FIVEBONUS.price + + ") - " + prize.get(Lotto.accord.FIVEBONUS.num) + "개"); + System.out.println(Lotto.accord.SIX.num - 1 + + "개 일치 (" + Lotto.accord.SIX.price + + ") - " + prize.get(Lotto.accord.SIX.num) + "개"); + + //수익률 계산 + int profit = Lotto.getProfit(prize); + System.out.println(profit); + double earning_rate = (double)profit / (double)purchase * 100.0d; + String str = String.format("총 수익률은 %.1f%%입니다", earning_rate); + System.out.println(str); + } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d..651075b 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,5 +1,7 @@ package lotto; +import java.net.Inet4Address; +import java.util.ArrayList; import java.util.List; public class Lotto { @@ -17,4 +19,54 @@ private void validate(List numbers) { } // TODO: 추가 기능 구현 + + public enum accord{ + THREE(3, "5,000원"), + FOUR(4, "50,000원"), + FIVE(5, "1,500,000원"), + FIVEBONUS(6, "30,000,000원"), + SIX(7, "2,000,000,000원"); + + public int num; + public String price; + accord(int i, String s){ + this.num = i; + this.price = s; + } + } + //로또 번호 출력 + public void printLotto(){ + System.out.print("["); + for(int i=0;i ns = new ArrayList<>(); + ns.addAll(numbers); + + int bonus = 0; + if(ns.contains(bonusnumber)) + bonus = 1; + + ns.retainAll(winLotto.numbers); + + if(ns.size() == 5) + return ns.size() + bonus; + if(ns.size() == 6) + return ns.size() + 1; + return ns.size(); + } + + public static int getProfit(ArrayList prize){ + return prize.get(3) * 5000 + prize.get(4) * 50000 + + prize.get(5) * 1500000 + prize.get(6) * 30000000 + + prize.get(7) * 2000000000; + } + } From 391f57b7bec5d65f7aede19cbe0be2eef9382d3c Mon Sep 17 00:00:00 2001 From: swissmissed Date: Tue, 26 Mar 2024 18:52:45 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=EC=A0=95=EB=A0=AC=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=EC=A0=9C=EC=99=B8=ED=95=98=EA=B3=A0=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 28 +++++++++++++++++++--------- src/main/java/lotto/Lotto.java | 5 +++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 3bafc44..20b68f1 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -3,6 +3,7 @@ import org.kokodak.Console; import org.kokodak.Randoms; +import java.lang.reflect.Array; import java.util.*; public class Application { @@ -14,10 +15,15 @@ public static void main(String[] args) { //로또 구입 금액 입력 System.out.println("구입금액을 입력해 주세요."); - int purchase = Integer.parseInt(Console.readLine()); - System.out.println(); + String str = Console.readLine(); + int purchase = 0; + try { + purchase = Integer.parseInt(str); + } catch (Exception e) { + System.out.println("[ERROR]"); + return; + } - //잘못된 구입 금액 예외처리 if(purchase % lotto_price != 0){ throw new IllegalArgumentException("[ERROR]"); } @@ -28,9 +34,10 @@ public static void main(String[] args) { //로또 구입 System.out.println(lotto_num + "개를 구매했습니다."); ArrayList myLotto = new ArrayList<>(); + for(int i=0;i ns = Randoms.pickUniqueNumbersInRange(1,45,6); - Collections.sort(ns); + //Collections.sort(ns); Lotto a = new Lotto(ns); myLotto.add(a); } @@ -39,6 +46,7 @@ public static void main(String[] args) { //구입한 로또 출력 for(Lotto i : myLotto) i.printLotto(); + System.out.println(); //당첨 번호 입력 System.out.println("당첨 번호를 입력해 주세요."); @@ -53,10 +61,12 @@ public static void main(String[] args) { //예외처리 //1미만, 45초과인 경우 예외처리 if(ns.get(0) < 1 || ns.get(5) > 45){ + System.out.println("[ERROR]"); throw new IllegalArgumentException("[ERROR]"); } //중복된 번호가 있는 경우 예외 처리 if(ns.size() != ns.stream().distinct().count()){ + System.out.println("[ERROR]"); throw new IllegalArgumentException("[ERROR]"); } @@ -65,11 +75,11 @@ public static void main(String[] args) { //보너스 번호 입력 System.out.println("보너스 번호를 입력해 주세요."); - String bn = Console.readLine(); - int bonus_number = Integer.parseInt(bn); + int bonus_number = Integer.parseInt(Console.readLine()); //예외 처리 if(bonus_number < 1 || bonus_number > 45){ + System.out.println("[ERROR]"); throw new IllegalArgumentException("[ERROR]"); } System.out.println(); @@ -110,10 +120,10 @@ public static void main(String[] args) { //수익률 계산 int profit = Lotto.getProfit(prize); - System.out.println(profit); double earning_rate = (double)profit / (double)purchase * 100.0d; - String str = String.format("총 수익률은 %.1f%%입니다", earning_rate); - System.out.println(str); + String str1 = String.format("총 수익률은 %.1f%%입니다.", earning_rate); + System.out.println(str1); } + } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 651075b..c2ffddc 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -2,6 +2,7 @@ import java.net.Inet4Address; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class Lotto { @@ -12,6 +13,10 @@ public Lotto(List numbers) { this.numbers = numbers; } + public void sort() { + Collections.sort(numbers); + } + private void validate(List numbers) { if (numbers.size() != 6) { throw new IllegalArgumentException(); From 129b1560debc5e63692d30b7a7808ea4b3c05615 Mon Sep 17 00:00:00 2001 From: swissmissed Date: Thu, 28 Mar 2024 19:01:14 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EC=A0=95=EB=A0=AC=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=EC=A0=9C=EC=99=B8=ED=95=98=EA=B3=A0=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 20b68f1..6f230d9 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -37,16 +37,16 @@ public static void main(String[] args) { for(int i=0;i ns = Randoms.pickUniqueNumbersInRange(1,45,6); - //Collections.sort(ns); + Collections.sort(ns); Lotto a = new Lotto(ns); myLotto.add(a); } - System.out.println(); + //System.out.println(); //구입한 로또 출력 for(Lotto i : myLotto) i.printLotto(); - System.out.println(); + //System.out.println(); //당첨 번호 입력 System.out.println("당첨 번호를 입력해 주세요.");