diff --git a/src/main/Application.java b/src/main/Application.java new file mode 100644 index 0000000..1270d01 --- /dev/null +++ b/src/main/Application.java @@ -0,0 +1,51 @@ +package lotto; +import java.util.Arrays; +import java.util.List; +import java.util.ArrayList; +import java.util.Scanner; +import java.util.stream.Collectors; + +import camp.nextstep.edu.missionutils.Console; + + +public class Application { + public static void main(String[] args) { + // TODO: 프로그램 구현 + Scanner sc = new Scanner(System.in); + + System.out.println("구입금액을 입력해 주세요."); + int money = sc.nextInt(); + sc.nextLine(); //왜???ㅠㅠ + System.out.println(); + + if (money % 1000 == 0){ + System.out.println( money/1000 + "개를 구매했습니다."); + List lottosready = new ArrayList<>(); + for (int i = 0; i < money/1000; i++){ + LottosReady lotto = new LottosReady(); + lottosready.add(lotto); + lotto.printNumbers(); + } + + System.out.println(); + System.out.println("당첨 번호를 입력해 주세요."); + String winningNumbersInput = sc.nextLine(); + List winningNumbers = Arrays.stream(winningNumbersInput.split(",")) + .map(String::trim) + .map(Integer::parseInt) + .collect(Collectors.toList()); + + System.out.println(); + System.out.println("보너스 번호를 입력해 주세요."); + int bonusNumber = sc.nextInt(); + + System.out.println(); + LottoResult lottoResult = new LottoResult(lottosready, winningNumbers, bonusNumber); + lottoResult.printResult(); + } + else{ + throw new IllegalArgumentException("금액은 1000원 단위여야 합니다."); + } + + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/Lotto.java similarity index 100% rename from src/main/java/lotto/Lotto.java rename to src/main/Lotto.java diff --git a/src/main/LottoResult.java b/src/main/LottoResult.java new file mode 100644 index 0000000..cffb4b4 --- /dev/null +++ b/src/main/LottoResult.java @@ -0,0 +1,60 @@ +package lotto; + +import java.util.List; + +public class LottoResult { + private final List lottos; + private final List winningNumbers; + private final int bonusNumber; + private final int lottoPrice = 1000; // 로또 1장의 가격 + + public LottoResult(List lottos, List winningNumbers, int bonusNumber) { + this.lottos = lottos; + this.winningNumbers = winningNumbers; + this.bonusNumber = bonusNumber; + } + + public void printResult() { + int[] counts = new int[7]; // 인덱스 0~6까지 일치하는 번호의 개수에 따른 로또 티켓 수를 저장 + long totalPrize = 0; // 총 당첨 상금 + + for (LottosReady lotto : lottos) { + int matchCount = lotto.match(winningNumbers); + if (matchCount == 5 && lotto.contains(bonusNumber)) { + counts[6]++; + totalPrize += 30000000; // 5개 일치 + 보너스 볼 + } else { + switch (matchCount) { + case 3: + counts[3]++; + totalPrize += 5000; + break; + case 4: + counts[4]++; + totalPrize += 50000; + break; + case 5: + counts[5]++; + totalPrize += 1500000; + break; + case 6: + counts[6]++; + totalPrize += 2000000000; + break; + } + } + } + + // 당첨 통계 출력 + System.out.println("당첨 통계\n---"); + System.out.println("3개 일치 (5,000원) - " + counts[3] + "개"); + System.out.println("4개 일치 (50,000원) - " + counts[4] + "개"); + System.out.println("5개 일치 (1,500,000원) - " + counts[5] + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + counts[6] + "개"); + System.out.println("6개 일치 (2,000,000,000원) - " + counts[6] + "개"); + + // 총 수익률 계산 및 출력 + double profitRate = ((double) totalPrize / (lottos.size() * lottoPrice)) * 100; + System.out.printf("총 수익률은 %.2f%%입니다.\n", profitRate); + } +} diff --git a/src/main/LottosReady.java b/src/main/LottosReady.java new file mode 100644 index 0000000..16f6d85 --- /dev/null +++ b/src/main/LottosReady.java @@ -0,0 +1,40 @@ +package lotto; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import camp.nextstep.edu.missionutils.Randoms; +import camp.nextstep.edu.missionutils.Console; + +public class LottosReady { + private List numbers; + + // 생성자에서 로또번호 초기화 + public LottosReady(){ + // 1부터 45까지의 범위에서 중복되지 않는 6개의 숫자를 추출 + numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + // 리스트를 오름차순으로 정렬 + Collections.sort(numbers); + } + + // 로또 번호 출력 메서드 + public void printNumbers(){ + System.out.println(numbers); + } + + public int match(List winningNumbers) { + return (int) numbers.stream() + .filter(winningNumbers::contains) + .count(); + } + + public boolean contains(int bonusNumber) { + return numbers.contains(bonusNumber); + } + + // numbers getter + public List getNumbers() { + return numbers; + } + +} diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java deleted file mode 100644 index d190922..0000000 --- a/src/main/java/lotto/Application.java +++ /dev/null @@ -1,7 +0,0 @@ -package lotto; - -public class Application { - public static void main(String[] args) { - // TODO: 프로그램 구현 - } -}