Skip to content
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
139 changes: 138 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,138 @@
# java-lotto-precourse
## 🚀 기능 요구 사항

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

```
- 로또 번호의 숫자 범위는 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원
```

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

- `Exception`이 아닌 `IllegalArgumentException`, `IllegalStateException` 등과 같은 명확한 유형을 처리한다.

### 입출력 요구 사항

### 입력

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

```
14000
```

- 당첨 번호를 입력 받는다. 번호는 쉼표(,)를 기준으로 구분한다.

```
1,2,3,4,5,6
```

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

```
7
```

### 출력
발행한 로또 수량 및 번호를 출력한다. 로또 번호는 오름차순으로 정렬하여 보여준다.
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개 일치 (5,000원) - 1개
4개 일치 (50,000원) - 0개
5개 일치 (1,500,000원) - 0개
5개 일치, 보너스 볼 일치 (30,000,000원) - 0개
6개 일치 (2,000,000,000원) - 0개
수익률은 소수점 둘째 자리에서 반올림한다. (ex. 100.0%, 51.5%, 1,000,000.0%)
총 수익률은 62.5%입니다.
예외 상황 시 에러 문구를 출력해야 한다. 단, 에러 문구는 "[ERROR]"로 시작해야 한다.
[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.

실행 결과 예시
```
구입금액을 입력해 주세요.
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%입니다.
```


## 🎯 프로그래밍 요구 사항
- 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](http://system.in/), Scanner) 로직은 제외한다.
- 핵심 로직을 구현하는 코드와 UI를 담당하는 로직을 분리해 구현한다.
- 단위 테스트 작성이 익숙하지 않다면 `test/java/lottoList/LottoTest`를 참고하여 학습한 후 테스트를 구현한다.

### 구현할 기능
- 구입 금액 입력
- 번호 6개와 보너스 번호 입력
- 번호 일치하는지 판단
- 몇등인지 판단
- 당첨 내역 및 수익률을 출력

### 비지니스 로직
- 로또 구입해서 번호 입력하기
- 당첨 번호 입력하기
- 보너스 번호 입력하기
- 번호 맞춰보기
- 등수 판단하기
- 결과 출력하기
52 changes: 52 additions & 0 deletions src/main/java/lotto/AppConfig.java
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체적으로 객체를 여러번 생성하는 코드가 보이네요 AppConfig를 만드는 이유는 객체를 한번만 생성하고 이를 다른곳에서 재사용하기위해서라고 알고있어요! 그러려면 필드에 객체를 저장하고 한번만 초기화하도록 변경해야할 것 같아요!

Copy link
Copy Markdown

@seulnan seulnan Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import lotto.controller.LottoController;
import lotto.service.AdjustmentLotto;
import lotto.service.CalculateTotalBenefit;
import lotto.service.CountCorrectNumber;
import lotto.service.LottoList;
import lotto.util.Validator;
import lotto.view.UserInput;
import lotto.view.UserOutput;

public class AppConfig {
    private final UserInput userInput = new UserInput();
    private final UserOutput userOutput = new UserOutput();
    private final Validator validator = new Validator();
    private final LottoList lottoList = new LottoList(userInput.getNumberOfLotto()); // 한 번만 생성
    private final CountCorrectNumber countCorrectNumber = new CountCorrectNumber(lottoList.getLottoContainer());
    private final AdjustmentLotto adjustmentLotto = new AdjustmentLotto(countCorrectNumber.getLottoList(), userInput.getBonusNumber());
    private final CalculateTotalBenefit calculateTotalBenefit = new CalculateTotalBenefit(adjustmentLotto.getLotto(), userInput.getBudget());

    public UserInput userInput() {
        return userInput; 
    }

    public UserOutput userOutput() {
        return userOutput; 
    }

    public Validator validator() {
        return validator;
    }

    public LottoList makeRandomLotto() {
        return lottoList; 
    }

    public CountCorrectNumber countCorrectNumber() {
        return countCorrectNumber;
    }

    public AdjustmentLotto adjustmentLotto() {
        return adjustmentLotto; 
    }

    public CalculateTotalBenefit calculateTotalBenefit() {
        return calculateTotalBenefit; 
    }

    public LottoController lottoController() {
        return new LottoController(
                userInput(),
                userOutput(),
                validator(),
                makeRandomLotto(),
                countCorrectNumber(),
                adjustmentLotto(),
                calculateTotalBenefit()
        );
    }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package lotto;


import lotto.controller.LottoController;
import lotto.service.AdjustmentLotto;
import lotto.service.CalculateTotalBenefit;
import lotto.service.CountCorrectNumber;
import lotto.service.LottoList;
import lotto.util.Validator;
import lotto.view.UserInput;
import lotto.view.UserOutput;

public class AppConfig {
public UserInput userInput(){
return new UserInput();
};

public UserOutput userOutput(){
return new UserOutput();
}

public Validator validator(){
return new Validator();
}

public LottoList makeRandomLotto(){
return new LottoList(userInput().getNumberOfLotto());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 코드를 다른 곳에서 실행하면
userInput이 호출되면서 새로운 userInput 객체가 계속 생성되어서 사용자 입력값이 유지되지 않는 문제가 발생할 수도 있어요

}

public CountCorrectNumber countCorrectNumber(){
return new CountCorrectNumber(makeRandomLotto().getLottoContainer());
}

public AdjustmentLotto adjustmentLotto(){
return new AdjustmentLotto(countCorrectNumber().getLottoList(), userInput().getBonusNumber());
}

public CalculateTotalBenefit calculateTotalBenefit(){
return new CalculateTotalBenefit(adjustmentLotto().getLotto(), userInput().getBudget());
}
public LottoController lottoController(){
return new LottoController(
userInput(),
userOutput(),
validator(),
makeRandomLotto(),
countCorrectNumber(),
adjustmentLotto(),
calculateTotalBenefit()
);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

피드백 요사항에 적어두실 필요 없었던 것 같은데요??ㅎㅎ 깔끔하게 쓰셨어요
그런데 util에 들어있는 validator같은 경우는 여기 의존성 주입에서 빼시고 utility 클래스로 만드시는게 좋을 것 같아용
모든 메서드에 static을 붙이면 굳이 instance를 생성하지 않고도 내부 메서드를 불러올 수 있습니다!!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appConfig의 역할을 쉽게 설명하면 객체를 생성하는 역할과 실행하는 역할을 분리하는 것이라 validator같은 유틸리티클래스는 객체를 생성하지않고 그냥 개별클래스로 관리하는게 더 좋을듯합니다!!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 changes: 8 additions & 0 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package lotto;

import lotto.controller.LottoController;

public class Application {
static AppConfig appConfig;
static LottoController lottoController;
public static void main(String[] args) {
// TODO: 프로그램 구현
appConfig = new AppConfig();
lottoController = appConfig.lottoController();

lottoController.lottoPlaying();
}
}
20 changes: 0 additions & 20 deletions src/main/java/lotto/Lotto.java

This file was deleted.

78 changes: 78 additions & 0 deletions src/main/java/lotto/controller/LottoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package lotto.controller;

import lotto.model.Lotto;
import lotto.service.AdjustmentLotto;
import lotto.service.CalculateTotalBenefit;
import lotto.service.CountCorrectNumber;
import lotto.service.LottoList;
import lotto.view.UserInput;
import lotto.view.UserOutput;
import lotto.util.Validator;

public class LottoController {
UserInput userInput;
UserOutput userOutput;
Validator validator;
LottoList lottoList;
CountCorrectNumber countCorrectNumber;
AdjustmentLotto adjustmentLotto;
CalculateTotalBenefit calculateTotalBenefit;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금 로또 게임 프로그램 상에서는 인스턴스를 프로그램 실행 중 재생성해야할 필요가 없으니 final를 앞에 붙여주시는게 더 좋은 시도일 것 같습니다!

public LottoController(
UserInput userInput,
UserOutput userOutput,
Validator validator,
LottoList lottoList,
CountCorrectNumber countCorrectNumber,
AdjustmentLotto adjustmentLotto,
CalculateTotalBenefit calculateTotalBenefit) {
this.userInput = userInput;
this.userOutput = userOutput;
this.validator = validator;
this.lottoList = lottoList;
this.countCorrectNumber = countCorrectNumber;
this.adjustmentLotto = adjustmentLotto;
this.calculateTotalBenefit = calculateTotalBenefit;
}

public void lottoPlaying(){
buyLotto();
executeLotto();
winningPriceLotto();
}

public void buyLotto(){
userInput.setBudget();
lottoList = new LottoList(userInput.getNumberOfLotto());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기에서 AppConfig에서 생성한 LottoList가 아니라 새로 만들어진 LottoList를 사용하고있네요 이러면 데이터 일관성이 깨질 위험이 있지않을까요?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

물론 모든 객체생성을 AppConfig에서 해야하는건아니지만 LottoList 같은 핵심객체는 한번만 생성해서 공유하는게 AppConfig를 제대로 활용하는 것 같습니다!


userOutput.printNumberOfLotto(userInput.getNumberOfLotto());
userOutput.printNumberOfLottoNumbers(lottoList.getLottoContainer());
}

public void executeLotto(){
userInput.setWinningNumbers();
Lotto winningLotto = new Lotto(userInput.getWinningLotto().getNumbers());
validator.numberCountValidator(winningLotto.getNumbers());

userInput.setBonusNumber();
validator.numberDuplicateWithBonusNumberValidator(winningLotto, userInput.getBonusNumber());

countCorrectNumber.listCorrectNumber(lottoList.getLottoContainer(), winningLotto);

}

public void winningPriceLotto(){
adjustmentLotto.countCorrectNumber();
userOutput.printWinningLotto(adjustmentLotto.getLotto());

calculateTotalBenefit.sumLotto();
calculateTotalBenefit.calculateTotalBenefit();
}







}
30 changes: 30 additions & 0 deletions src/main/java/lotto/model/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lotto.model;

import java.util.List;


public class Lotto {
private final List<Integer> numbers;
private int correctNumbers;

public Lotto(List<Integer> lotto) {
correctNumbers = 0;
numbers = lotto;
}

public List<Integer> getNumbers() {
return numbers;
}

public int getCorrectNumbers() {
return correctNumbers;
}

public void setNumbers(int correctNumbers) {
this.correctNumbers = correctNumbers;
}

public void plusCorrectNumbers() {
correctNumbers++;
}
}
43 changes: 43 additions & 0 deletions src/main/java/lotto/service/AdjustmentLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lotto.service;

import lotto.model.Lotto;

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


public class AdjustmentLotto {
private int[] lotto = new int[5];
private List<Lotto> lottoList = new ArrayList<Lotto>();
private int bonus;

public AdjustmentLotto(List<Lotto> lottoList, int bonus) {
this.lottoList = lottoList;
this.bonus = bonus;
for (int l : lotto) { lotto[l] = 0; }
}

public int[] getLotto() {
return lotto;
}

public void countCorrectNumber(){
for (Lotto l : lottoList){
countCorrectNumber(l);
}
}

public void countCorrectNumber(Lotto winningLotto) {
if (winningLotto.getCorrectNumbers() == 3) { lotto[0]++; }
if (winningLotto.getCorrectNumbers() == 4) { lotto[1]++; }
if (winningLotto.getCorrectNumbers() == 5) {
CountCorrectNumber countCorrectNumber = new CountCorrectNumber(lottoList);
if (countCorrectNumber.sameNumber(winningLotto, bonus)){
lotto[3]++;
return;
}
lotto[4]++;
}
if (winningLotto.getCorrectNumbers() == 6) { lotto[4]++; }
}
}
Loading