Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0f61e75
docs(README.md): update README.md
jumining Jul 9, 2023
6f52b9f
fix(build.gradle): update java language version
jumining Jul 9, 2023
932f89e
feat(Message): add a constant message file
jumining Jul 9, 2023
dfd587f
feat(Number): add a constant number file
jumining Jul 9, 2023
a4c0ede
feat(Lotto): add a Lotto class
jumining Jul 9, 2023
380bcaa
feat(MyLottoData): add a MyLottoData class
jumining Jul 9, 2023
4ec9285
feat(WinnerLottoData): add a WinnerLottoData class
jumining Jul 9, 2023
64cf7ba
feat(ResultLottoData): add a ResultLottoData class
jumining Jul 9, 2023
58157ea
feat(LottoManager): add a LottoManager class
jumining Jul 9, 2023
d65a39e
feat(LottoManager): add a function that input purchase money
jumining Jul 9, 2023
cc47138
feat(LottoManager): add two functions that issue lottery
jumining Jul 9, 2023
25fa5d9
feat(LottoManager): add a function that print issued lottery
jumining Jul 9, 2023
d500744
feat(LottoManager): add two functions that input winner and bonus number
jumining Jul 9, 2023
8118e3f
feat(LottoManager): add a function that calculate my profit and rate
jumining Jul 9, 2023
e7bb169
feat(LottoManager): add two functions that used for calculate lottery…
jumining Jul 9, 2023
561627e
feat(LottoManager): add a function that print lottery result
jumining Jul 9, 2023
94feba0
feat(LottoManager): add a play function to operate the full functions
jumining Jul 9, 2023
3796ee4
feat(Application): add codes to operate LottoManager class
jumining Jul 9, 2023
6d0df31
feat(LottoManager): delete a issueLottoOneSet function
jumining Jul 9, 2023
9de6ade
docs(README.md): update README.md
jumining Jul 9, 2023
acecbd0
feat(ResultLottoData): update code at calculateRate function
jumining Jul 9, 2023
d5e9c7d
feat(Message): update code about output rate format
jumining Jul 9, 2023
5dc0977
docs(README.md): update README.md
jumining Jul 9, 2023
7cd2e40
feat(LottoManager): add validation codes
jumining Jul 9, 2023
dd04ac0
docs(README.md): update README.md
jumining Jul 9, 2023
0183b37
feat (LottoData) : delete LottoData
jumining Jul 18, 2023
6854524
feat (GameController) : add GameController
jumining Jul 18, 2023
b8937d2
feat (LottoManager) : move code to GameController
jumining Jul 18, 2023
c66fc00
feat (Score) : add Score
jumining Jul 18, 2023
92d4d50
feat (Message) : update Message function
jumining Jul 18, 2023
285e4b9
feat (Application) : make gamecontroller object in application file
jumining Jul 18, 2023
71e93ca
chore(lotto) : update code newline
jumining Jul 18, 2023
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
242 changes: 64 additions & 178 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,207 +1,93 @@
# 과제 - 로또

## 🔍 진행 방식

- 과제는 **기능 요구 사항, 프로그래밍 요구 사항, 과제 진행 요구 사항** 세 가지로 구성되어 있다.
- 세 개의 요구 사항을 만족하기 위해 노력한다. 특히 기능을 구현하기 전에 기능 목록을 만들고, 기능 단위로 커밋 하는 방식으로 진행한다.
- 기능 요구 사항에 기재되지 않은 내용은 스스로 판단하여 구현한다.

---

## 📈 과제 진행 및 제출 방법

- 과제는 [java-lotto](https://github.com/LandvibeDev/java-lotto) 저장소를 Fork/Clone해 시작한다.
- **기능을 구현하기 전에 java-baseball-precourse/README.md 파일에 구현할 기능 목록을 정리**해 추가한다.
- **Git의 커밋 단위는 앞 단계에서 README.md 파일에 정리한 기능 목록 단위**로 추가한다.
- [AngularJS Commit Message Conventions](https://gist.github.com/stephenparish/9941e89d80e2bc58a153) 참고해 commit log를 남긴다.
- 과제 진행 및 제출 방법은 [우아한코스 과제 제출 문서](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) 를 참고한다.
- base repository를 `LandvibeDev/java-lotto`로 지정해서 PR 생성하면됨
# 🎱 로또 미션 진행

<br>

### 테스트 실행 가이드
## 🗒 구현 기능 목록

- 터미널에서 `java -version`을 실행하여 Java 버전이 14인지 확인한다. 또는 Eclipse 또는 IntelliJ IDEA와 같은 IDE에서 Java 14로 실행되는지 확인한다.
- 터미널에서 Mac 또는 Linux 사용자의 경우 `./gradlew clean test` 명령을 실행 하고,
Windows 사용자의 경우 `gradlew.bat clean test` 명령을 실행할 때 동작 하는지 만 확인(테스트는 실패).
### 1️⃣ [입력] 구입할 로또 금액 입력받기

---

## 🚀 기능 요구 사항

로또 게임 기능을 구현해야 한다. 로또 게임은 아래와 같은 규칙으로 진행된다.

```
- 로또 번호의 숫자 범위는 1~45까지이다.
- 1개의 로또를 발행할 때 중복되지 않는 6개의 숫자를 뽑는다.
- 당첨 번호 추첨 시 중복되지 않는 숫자 6개와 보너스 번호 1개를 뽑는다.
- 당첨은 1등부터 5등까지 있다. 당첨 기준과 금액은 아래와 같다.
- 1등: 6개 번호 일치 / 2,000,000,000원
- 2등: 5개 번호 + 보너스 번호 일치 / 30,000,000원
- 3등: 5개 번호 일치 / 1,500,000원
- 4등: 4개 번호 일치 / 50,000원
- 5등: 3개 번호 일치 / 5,000원
```
- 천원 단위로 입력받아 *MyLottoData*의 *money*에 금액을 저장합니다.
- 예외처리
- **inputPurchasingMoney()**: void

- 로또 구입 금액을 입력하면 구입 금액에 해당하는 만큼 로또를 발행해야 한다.
- 로또 1장의 가격은 1,000원이다.
- 당첨 번호와 보너스 번호를 입력받는다.
- 사용자가 구매한 로또 번호와 당첨 번호를 비교하여 당첨 내역 및 수익률을 출력하고 로또 게임을 종료한다.
- 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`를 발생시키고, "[ERROR]"로 시작하는 에러 메시지를 출력 후 종료한다.

## ✍🏻 입출력 요구사항

### ⌨️ 입력

- 로또 구입 금액을 입력 받는다. 구입 금액은 1,000원 단위로 입력 받으며 1,000원으로 나누어 떨어지지 않는 경우 예외 처리한다.

```
14000
```
<br>

- 당첨 번호를 입력 받는다. 번호는 쉼표(,)를 기준으로 구분한다.
### 2️⃣ [계산] 로또 발행하기

```
1,2,3,4,5,6
```
- 구입할 로또 금액으로부터 로또 개수를 계산해서 개수를 *MyLottoData*의 *quantity*에 저장합니다.
- 개수만큼 로또를 여러개 발행해서 *MyLottoData*의 *lottoSet*에 저장합니다.
- **issueLottoMultiSet()**: void

- 보너스 번호를 입력 받는다.
<br>

```
7
```
### 2️⃣ [출력] 구입한 로또 출력하기

### 🖥 출력
- *MyLottoData*의 *lottoSet*에 저장된 구입한 로또의 번호들을 출력합니다.
- **printPurchasedLotto()**: void

- 발행한 로또 수량 및 번호를 출력한다. 로또 번호는 오름차순으로 정렬하여 보여준다.
<br>

```
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]
```
### 3️⃣ [입력] 당첨 번호와 보너스 번호 입력받기

- 당첨 내역을 출력한다.
- 당첨 번호와 보너스 번호를 입력받아 *WinnerLottoData*에 저장합니다.
- 예외처리
- **inputWinnerNumber()**: void
- **inputBonusNumber()**: void

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

- 수익률은 소수점 둘째 자리에서 반올림한다. (ex. 100.0%, 51.5%, 1,000,000.0%)
### 4️⃣ [계산] 로또 당첨 결과 계산하기

```
총 수익률은 62.5%입니다.
```
- 발행한 로또와 당첨 번호, 보너스 번호를 비교하여 *ResultLottoData*에 저장합니다.
- **calculateResultLotto()**: void
- **countNumberOfLottoMatches(**Lotto**)**: int
- **isMatchWithBonusNumber(**Lotto**)**: boolean

- 예외 상황 시 에러 문구를 출력해야 한다. 단, 에러 문구는 "[ERROR]"로 시작해야 한다.

```
[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.
```
<br>

### 💻 실행 결과 예시
### 5️⃣ [출력] 로또 당첨 결과 출력하기

```
구입금액을 입력해 주세요.
8000

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]

당첨 번호를 입력해 주세요.
1,2,3,4,5,6

보너스 번호를 입력해 주세요.
7

당첨 통계
---
3개 일치 (5,000원) - 1개
4개 일치 (50,000원) - 0개
5개 일치 (1,500,000원) - 0개
5개 일치, 보너스 볼 일치 (30,000,000원) - 0개
6개 일치 (2,000,000,000원) - 0개
총 수익률은 62.5%입니다.
```
- *ResultLottoData*에 저장된 데이터를 바탕으로 당첨 결과와 수익률을 출력합니다.
- **printResultLotto()**: void

---

## 🎯 프로그래밍 요구 사항

- JDK 14 버전에서 실행 가능해야 한다.
- 프로그램 실행의 시작점은 `Application`의 `main()`이다.
- `build.gradle` 파일을 변경할 수 없고, 외부 라이브러리를 사용하지 않는다.
- [Java 코드 컨벤션](https://naver.github.io/hackday-conventions-java/) 가이드를 준수하며 프로그래밍한다.
- 프로그램 종료 시 `System.exit()`를 호출하지 않는다.
- 프로그램 구현이 완료되면 `ApplicationTest`의 모든 테스트가 성공해야 한다.
- 프로그래밍 요구 사항에서 달리 명시하지 않는 한 파일, 패키지 이름을 수정하거나 이동하지 않는다.
- indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용한다.
- 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다.
- 힌트: indent(인덴트, 들여쓰기) depth를 줄이는 좋은 방법은 함수(또는 메서드)를 분리하면 된다.
- 3항 연산자를 쓰지 않는다.
- 함수(또는 메서드)가 한 가지 일만 하도록 최대한 작게 만들어라.
- JUnit 5와 AssertJ를 이용하여 본인이 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인한다.
- 함수(또는 메서드)의 길이가 15라인을 넘어가지 않도록 구현한다.
- 함수(또는 메서드)가 한 가지 일만 잘 하도록 구현한다.
- else 예약어를 쓰지 않는다.
- 힌트: if 조건절에서 값을 return하는 방식으로 구현하면 else를 사용하지 않아도 된다.
- else를 쓰지 말라고 하니 switch/case로 구현하는 경우가 있는데 switch/case도 허용하지 않는다.
- Java Enum을 적용한다.
- 도메인 로직에 단위 테스트를 구현해야 한다. 단, UI(System.out, System.in, Scanner) 로직은 제외한다.
- 핵심 로직을 구현하는 코드와 UI를 담당하는 로직을 분리해 구현한다.

### 라이브러리

- [`camp.nextstep.edu.missionutils`](https://github.com/woowacourse-projects/mission-utils)에서 제공하는 `Randoms` 및 `Console` API를 사용하여 구현해야 한다.
- Random 값 추출은 `camp.nextstep.edu.missionutils.Randoms`의 `pickUniqueNumbersInRange()`를 활용한다.
- 사용자가 입력하는 값은 `camp.nextstep.edu.missionutils.Console`의 `readLine()`을 활용한다.

#### 사용 예시

```java
List<Integer> numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6);
```
<br>

### Lotto 클래스
------

- 제공된 `Lotto` 클래스를 활용해 구현해야 한다.
- `Lotto`에 매개 변수가 없는 생성자를 추가할 수 없다.
- `numbers`의 접근 제어자인 private을 변경할 수 없다.
- `Lotto`에 필드(인스턴스 변수)를 추가할 수 없다.
- `Lotto`의 패키지 변경은 가능하다.
<br>

```java
public class Lotto {
private final List<Integer> numbers;

public Lotto(List<Integer> numbers) {
validate(numbers);
this.numbers = numbers;
}

private void validate(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException();
}
}
## 🗂 폴더링

```
📦 src/main/java/
|
+ 🗂 lotto/
+-------🅲 Application
| : main() 존재, LottoManager 호출
|
+-------🅲 LottoManager
| : 로또가 진행되는 클래스
|
+-------🅲 Lotto
| : 6개의 로또 숫자가 저장되는 클래스
|
+-------🅲 MyLottoData
| : 구입 금액과 발행된 로또 수량과 번호가 저장되는 클래스
|
+-------🅲 WinnerLottoData
| : 당첨 번호와 보너스 번호가 저장되는 클래스
|
+-------🅲 ResultLottoData
| : 로또 당첨 결과와 수익률이 저장되는 클래스
|
+-------🅲 Message
| : 상수 메세지들을 모아놓은 클래스
|
+-------🅲 Number
| : 상수 숫자들을 모아놓은 클래스

// TODO: 추가 기능 구현
}
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(14)
languageVersion = JavaLanguageVersion.of(15)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
LottoManager lottoManager = new LottoManager();
GameController game = new GameController(lottoManager);
game.play();
}
}
90 changes: 90 additions & 0 deletions src/main/java/lotto/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package lotto;

import camp.nextstep.edu.missionutils.Console;

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

public class GameController {

private final LottoManager lottoManager;

public GameController(LottoManager lottoManager) {
this.lottoManager = lottoManager;
}

public void play() {
System.out.println(Message.Input.PURCHASE_MONEY);
int money = readMoney();

int quantity = money / Number.Rule.PRICE.toNumber();
List<Lotto> myLottoSet = new ArrayList<>();
for (int i = 0; i < quantity; i++) {
Lotto lottoOneSet = lottoManager.generate(Number.Rule.MIN.toNumber(),
Number.Rule.MAX.toNumber(), Number.Rule.SIZE.toNumber());
myLottoSet.add(lottoOneSet);
}

System.out.println(Message.Output.PURCHASE_QUANTITY.toQuantityFormat(quantity));
for (Lotto lotto : myLottoSet) {
System.out.println(lotto.getNumbers());
}

System.out.println(Message.Input.WINNER_NUMBER);
Lotto winnerNumber = readNumber();
int bonusNumber = readBonus();

Score score = lottoManager.calculate(myLottoSet, winnerNumber, bonusNumber);
double rate = score.calculateRate(money);
System.out.println(Message.Output.RESULT_STAT);
System.out.println(score);
System.out.println(Message.Output.RESULT_RATE.toRateFormat(rate));
}

private int readMoney() {
String input = Console.readLine();
for (char c : input.toCharArray()) {
if (c < '0' || c > '9') {
throw new IllegalArgumentException(Message.Error.PURCHASE_AMOUNT.toString());
}
}
int purchaseMoney = Integer.parseInt(input);
if (purchaseMoney % Number.Rule.PRICE.toNumber() != 0) {
throw new IllegalArgumentException(Message.Error.PURCHASE_AMOUNT.toString());
}
return Integer.parseInt(input);
}

private Lotto readNumber() {
String input = Console.readLine();
try {
String[] stringArray = input.split(",");
for (String str : stringArray) {
int num = Integer.parseInt(str);
}
} catch (Exception e) {
throw new IllegalArgumentException(Message.Error.LOTTO_NUMBER.toString());
}
String[] stringArray = input.split(",");
List<Integer> integerList = new ArrayList<>();
for (String str : stringArray) {
int num = Integer.parseInt(str);
integerList.add(num);
}
return new Lotto(integerList);
}

private int readBonus() {
String input = Console.readLine();
try {
Integer.parseInt(input);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(Message.Error.BONUS_NUMBER.toString());
}
int bonusNumber = Integer.parseInt(input);
if (bonusNumber < Number.Rule.MIN.toNumber() || bonusNumber > Number.Rule.MAX.toNumber()) {
throw new IllegalArgumentException(Message.Error.BONUS_NUMBER.toString());
}
return bonusNumber;
}
}
Loading