diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..6f230d9 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,129 @@ package lotto; +import org.kokodak.Console; +import org.kokodak.Randoms; + +import java.lang.reflect.Array; +import java.util.*; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + + //로또 가격 + int lotto_price = 1000; + + //로또 구입 금액 입력 + 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]"); + } + + //로또 개수 + 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(); + + //당첨 번호 입력 + 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){ + System.out.println("[ERROR]"); + throw new IllegalArgumentException("[ERROR]"); + } + //중복된 번호가 있는 경우 예외 처리 + if(ns.size() != ns.stream().distinct().count()){ + System.out.println("[ERROR]"); + throw new IllegalArgumentException("[ERROR]"); + } + + Lotto winLotto = new Lotto(ns); + System.out.println(); + + //보너스 번호 입력 + System.out.println("보너스 번호를 입력해 주세요."); + 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(); + + //당첨 통계 + 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); + double earning_rate = (double)profit / (double)purchase * 100.0d; + 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 519793d..c2ffddc 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,5 +1,8 @@ package lotto; +import java.net.Inet4Address; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class Lotto { @@ -10,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(); @@ -17,4 +24,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; + } + }