-
Notifications
You must be signed in to change notification settings - Fork 39
[스프링 화요일]정수미 미션 제출합니다. #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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> 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<Integer> 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원 단위여야 합니다."); | ||
| } | ||
|
|
||
| } | ||
| } |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 클래스가 누락된 것 같습니다 |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로또 결과를 종합하는 메소드와 결과를 출력하는 메소드를 나누면 메소드가 조금 더 짧아질 것 같습니다. 또, 결과를 return 할 수 있게 되어 else문을 안 쓸 수도 있을 것 같습니다. switch-case문도 안쓰는 방향으로 해보시면 좋을 것 같아요. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package lotto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class LottoResult { | ||
| private final List<LottosReady> lottos; | ||
| private final List<Integer> winningNumbers; | ||
| private final int bonusNumber; | ||
| private final int lottoPrice = 1000; // 로또 1장의 가격 | ||
|
|
||
| public LottoResult(List<LottosReady> lottos, List<Integer> 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); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Integer> 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<Integer> winningNumbers) { | ||
| return (int) numbers.stream() | ||
| .filter(winningNumbers::contains) | ||
| .count(); | ||
| } | ||
|
|
||
| public boolean contains(int bonusNumber) { | ||
| return numbers.contains(bonusNumber); | ||
| } | ||
|
|
||
| // numbers getter | ||
| public List<Integer> getNumbers() { | ||
| return numbers; | ||
| } | ||
|
|
||
| } |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
입출력 시행도 클래스로 따로 분리하여 메인메소드를 실행한다면 조금 더 유지보수가 쉽고 코드가 간결해질 것 같습니다!