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
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
# java-lotto-precourse

## 기능의 분리

### 1. 입력부

- 로또 구입 금액 입력 → 구현
- 당첨 번호 입력 → 구현
- 보너스 번호 입력 → 구현

### 2. 연산부

- 로또 구입 개수 연산 → 구현
- 랜덤한 로또 발행 연산 → 구현
- 발행 로또의 당첨 내역 연산
- 수익률 연산

### 3. 출력부

- 로또 구입 개수 출력 **→ 문제 발생**
- 발행 로또 출력
- 당첨 내역 출력
- 수익률 출력

### 4. 예외부

- 로또 구입 금액의 오류

- 숫자 이외의 입력 → 구현
- 1000원으로 나누어 떨어지지 않는 입력 → 구현

- 당첨 번호의 오류

- 숫자 이외의 입력 → 구현
- 잘못된 구분기호의 입력 → 구현
- 6개의 숫자가 아닌 입력 → 구현
- 가능 숫자 범위를 벗어난 입력 → 구현

- 보너스 번호의 오류

- 숫자 이외의 입력 → 구현
- 가능 숫자 범위를 벗어난 입력 → 구현
9 changes: 8 additions & 1 deletion src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package lotto;

import lotto.view.InputMoney;
import lotto.view.OutputView;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
InputMoney money = new InputMoney();
OutputView output = new OutputView();

money.getMoney();
output.showLottoCount();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

view들만 사용하는것보단 view와 모델 사이의 상호작용을 관리할 controller를 만드시는게 더 좋을것 같습니다~~

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package lotto;
package lotto.model;

import camp.nextstep.edu.missionutils.Randoms;

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

public class Lotto {
Expand All @@ -16,5 +19,9 @@ private void validate(List<Integer> numbers) {
}
}

// TODO: 추가 기능 구현
// 랜덤한 로또 발행 연산
public List<Integer> setLotto() {
List<Integer> lottoNums = Randoms.pickUniqueNumbersInRange(1,45,6);
return lottoNums;
}
}
16 changes: 16 additions & 0 deletions src/main/java/lotto/model/LottoCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package lotto.model;

import lotto.view.InputMoney;

public class LottoCount {

// 로또 구입 개수 연산
public int setPurchaseLotto() {
InputMoney tmpMoney = new InputMoney();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

여기서 오류가 나는것 같아요!! 메인 어플리케이션에서 inputMoney를 하나 생성하셨는데 여기서 또 생성하면 입력이 두번 호출되니까용 여기서 new 를 통해 새 instance를 생성하는것보단 함수 파라미터로 기존 생성한 inputMoney를 받아서 사용하시고 inputMoney내부나 다른 코드로 한번 얻은 money값을 저장하는 변수를 두셔야할것 같아용 그 변수값만 불러오는 방식으로 바꾸시면 해결될 것 같아요!


int money = tmpMoney.getMoney();
int lotto = money % 1000;

return lotto;
}
}
41 changes: 41 additions & 0 deletions src/main/java/lotto/view/InputBonus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lotto.view;

import camp.nextstep.edu.missionutils.Console;

public class InputBonus {

// 보너스 번호 입력
public int getBonus() {
while(true) {
try {
System.out.println("Please enter a bonus numbers.");
String strBonus = Console.readLine();
int bonus = checkBonus(strBonus);
return bonus;
}

catch (NumberFormatException e) {
System.out.println("[ERROR] It is not a number.");
}

// 숫자 범위를 벗어나는 입력
catch (IllegalArgumentException e) {
System.out.println("[ERROR] It is out of range.");
}
}
}

// 올바른 입력 체크 합수
public int checkBonus(String strBonus) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

view마다 check~~() 함수를 두는것보다 util 디렉토리 아래에 Validator 파일을 하나 만들어서 전체 코드에서 여러 검증만을 담당하게 코드를 짜도 좋을 것 같아요!

// 숫자인지 체크
int bonus = Integer.parseInt(strBonus);

// 숫자 범위 확인
if(bonus < 1 || bonus > 45) {
throw new IllegalArgumentException();
}

return bonus;
}
}
58 changes: 58 additions & 0 deletions src/main/java/lotto/view/InputLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package lotto.view;

import camp.nextstep.edu.missionutils.Console;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;

public class InputLotto {

// 당첨 번호 입력
public List<Integer> getLotto() {
while(true) { // 예외 발생 시 반복적인 입력을 받기 위한 반복문
try {
System.out.println("Please enter a lotto numbers.");
String strLotto = Console.readLine();
List<Integer> lotto = checkLotto(strLotto);
return lotto;
}

// 숫자가 아니거나 구분자가 이상한 입력
catch (NumberFormatException e) {
System.out.println("[ERROR] It is not a valid lotto.");
}

// 숫자 범위를 벗어나는 입력
catch (IllegalArgumentException e) {
System.out.println("[ERROR] It is out of range.");
}
}
}

// 올바른 입력 체크 함수
public List<Integer> checkLotto(String lotto) {
List<Integer> arrLotto = new ArrayList<Integer>();

// 구분자(,)를 기준으로 문자열 자르기
String[] strLotto = lotto.split(",");

// 문자를 숫자로 변환
for (int i = 0; i < strLotto.length; i++) {
arrLotto.add(Integer.parseInt(strLotto[i]));
}

// 숫자 개수 확인
if (arrLotto.size() != 6) {
throw new IllegalArgumentException();
}

// 숫자 범위 확인
for (int i = 0; i < arrLotto.size(); i++) {
if(arrLotto.get(i) < 1 || arrLotto.get(i) > 45) {
throw new IllegalArgumentException();
}
}

return arrLotto;
}
}
36 changes: 36 additions & 0 deletions src/main/java/lotto/view/InputMoney.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lotto.view;

import camp.nextstep.edu.missionutils.Console;

public class InputMoney {

// 로또 구입 금액 입력
public int getMoney() {
while(true) { // 예외 발생 시 반복적인 입력을 받기 위한 반복문
try {
System.out.println("Please enter a money.");
int money = Integer.parseInt(Console.readLine());
checkMoney(money);
return money;
}

// 숫자 이외의 입력
catch (NumberFormatException e) {
System.out.println("[ERROR] It is not a number.");
}

// 1000원으로 나누어 떨어지지 않는 입력
catch (IllegalArgumentException e) {
System.out.println("[ERROR] It is not a valid money.");
}
}
}

// 1000원으로 나누어 떨어지지 않는 입력 체크 함수
public void checkMoney(int money) {
int value = money % 1000;
if(value != 0) {
throw new IllegalArgumentException();
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/lotto/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lotto.view;

import lotto.model.LottoCount;

public class OutputView {
public void showLottoCount() {
LottoCount lottoCount = new LottoCount();
System.out.println(lottoCount.setPurchaseLotto() + " purchase lotto.");
}
}
1 change: 1 addition & 0 deletions src/test/java/lotto/LottoTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lotto;

import lotto.model.Lotto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down