Skip to content
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

[로또] 기윤호 미션 제출합니다. #1360

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
# java-lotto-precourse

## 간단한 로또 발매기를 구현한다.

> 이 문서의 작성 양식은 [프리코스 과제 가이드](https://github.com/woowacourse/woowacourse-docs/blob/e2f102b97f6b65f5ba8da09944ee8cb9b33b696a/precourse/README.md)를 참고했습니다.

* 취소선 처리한 내용을 검색하면 다른 항목에서 찾을 수 있습니다.

---

사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시키고, "[ERROR]"로 시작하는 에러 메시지를 출력 후 그 부분부터 입력을 다시 받는다.

### 1. 로또 구입 금액을 입력 받는다.

사용자가 입력하는 값은 `camp.nextstep.edu.missionutils.Console`의 `readLine()`을 활용한다.

> 예:
>
> 8000

* 1-1. 구입 금액은 1,000원으로 나누어 떨어지지 않는 경우 IllegalArgumentException 처리한다.

### 2. 구입 금액에 해당하는 만큼 로또를 발행해야 한다.

로또 1장의 가격은 1,000원이다.

1개의 로또를 발행할 때 중복되지 않는 6개의 숫자를 뽑는다.

Random 값 추출은 `camp.nextstep.edu.missionutils.Randoms`의 `pickUniqueNumbersInRange()`를 활용한다.

로또 번호의 숫자 범위는 1~45까지이다.

> 예:
>
> 8개를 구매했습니다.
> [8, 21, 23, 41, 42, 43]
> [3, 5, 11, 16, 32, 38]
> [7, 11, 16, 35, 36, 44]
> [1, 8, 11, 31, 41, 42]
> [13, 14, 16, 38, 42, 45]
> [7, 11, 30, 40, 42, 43]
> [2, 13, 22, 32, 38, 45]
> [1, 3, 5, 14, 22, 45]

* 2-1. 발행한 로또 수량 및 번호를 출력한다.

* 2-2. 로또 번호는 오름차순으로 정렬하여 보여준다.

### 3. 당첨 번호를 입력 받는다.

번호는 쉼표(,)를 기준으로 구분한다.

> 예:
>
> 1,2,3,4,5,6

* 3-1. 당첨 번호의 숫자 범위는 1~45까지가 아니면 IllegalArgumentException 처리한다.

* 3-2. 당첨 번호는 중복되면 IllegalArgumentException 처리한다.

* 3-3. 당첨 번호는 6개가 아니면 IllegalArgumentException 처리한다.

### 4. 보너스 번호를 입력 받는다.

> 예:
>
> 7

* 3-1. 보너스 번호의 숫자 범위는 1~45까지가 아니면 IllegalArgumentException 처리한다.

* 3-2. 보너스 번호는 당첨 번호와 중복되면 IllegalArgumentException 처리한다.

### 5. 사용자가 구매한 로또 번호와 당첨 번호를 비교하여 당첨 내역을 출력한다.

> 예:
>
> 3개 일치 (5,000원) - 1개
> 4개 일치 (50,000원) - 0개
> 5개 일치 (1,500,000원) - 0개
> 5개 일치, 보너스 볼 일치 (30,000,000원) - 0개
> 6개 일치 (2,000,000,000원) - 0개

### 6. 수익률을 출력하고 로또 게임을 종료한다.

> 예:
>
> 총 수익률은 62.5%입니다.

* 6-1. 수익률은 소수점 둘째 자리에서 반올림한다.
51 changes: 50 additions & 1 deletion src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
package lotto;

import static camp.nextstep.edu.missionutils.Console.readLine;
import static camp.nextstep.edu.missionutils.Randoms.pickUniqueNumbersInRange;

import java.util.ArrayList;
import java.util.List;

public class Application {

private static final int LOTTO_PRICE = 1000;
private static final int MIN_LOTTO_NO = 1;
private static final int MAX_LOTTO_NO = 45;
private static final int LOTTO_COUNT = 6;

public static void main(String[] args) {
// TODO: 프로그램 구현
int budget;
while (true) {
try {
budget = readBudget();
break;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}

final List<Lotto> lottos = new ArrayList<>();
for (int i = budget / 1000; i != 0; i--) {
lottos.add(new Lotto(pickUniqueNumbersInRange(MIN_LOTTO_NO, MAX_LOTTO_NO, LOTTO_COUNT)));
}
System.out.println(lottos.size() + "개를 구매했습니다.");
for (final Lotto lotto : lottos) {
System.out.println('[' + lotto.toString() + ']');
}
System.out.println("3개 일치 (5,000원) - 1개");
System.out.println("4개 일치 (50,000원) - 0개");
System.out.println("5개 일치 (1,500,000원) - 0개");
System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - 0개");
System.out.println("6개 일치 (2,000,000,000원) - 0개");
System.out.println("총 수익률은 62.5%입니다.");
}

private static int readBudget() throws IllegalArgumentException {
int budget;
try {
budget = Integer.parseInt(readLine());
assert budget >= 0;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[ERROR] 구입 금액은 0 이상의 자연수여야 합니다.");
}
if (budget % 1000 != 0) {
throw new IllegalArgumentException("[ERROR] 구입 금액은 1,000원으로 나누어 떨어져야 합니다.");
}
return budget;
}
}
8 changes: 7 additions & 1 deletion src/main/java/lotto/Lotto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lotto;

import java.util.List;
import java.util.stream.Collectors;

public class Lotto {
private final List<Integer> numbers;
Expand All @@ -16,5 +17,10 @@ private void validate(List<Integer> numbers) {
}
}

// TODO: 추가 기능 구현
@Override
public String toString() {
return numbers.stream()
.map(x -> x.toString())
.collect(Collectors.joining(", "));
}
}