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

[로또 게임] 김지웅 2차 과제 제출합니다. #18

Open
wants to merge 6 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
7 changes: 0 additions & 7 deletions src/main/java/lotto/Application.java

This file was deleted.

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

import lotto.Model.Lotto;
import lotto.Model.LottoNumGenerater;
import lotto.Model.LottoResultChecker;
import lotto.View.LottoInput;
import lotto.View.LottoResultPrinter;

import java.util.List;

public class Application {
public static void main(String[] args) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

main 함수에 모든 흐름이 다적혀 있네요. controller는 단순히 실행시키는 역할을 하는 객체가 아닙니다.

LottoInput input = new LottoInput();
LottoNumGenerater lottoGenerate = new LottoNumGenerater();
LottoResultChecker lottoResultCheck = new LottoResultChecker();
LottoResultPrinter lottoResultPrint = new LottoResultPrinter();

int lottoPrice = input.getLottoPrice();
int lottoNum = lottoPrice / 1000;

List<Lotto> lottos = lottoGenerate.generateLottos(lottoNum);
List<Integer> correctNum = input.getCorrectNum();
int bonusNum = input.getBonusNum();

int[] prizeCounts = lottoResultCheck.checkCorrectNum(lottos, correctNum, bonusNum);
lottoResultPrint.printResult(prizeCounts, lottoPrice);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lotto;
package lotto.Model;

import java.util.List;

Expand All @@ -12,9 +12,11 @@ public Lotto(List<Integer> numbers) {

private void validate(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException();
throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다.");
}
}

// TODO: 추가 기능 구현
public List<Integer> getNumbers() {
return numbers;
}
}
21 changes: 21 additions & 0 deletions src/main/java/lotto/Model/LottoNumGenerater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package lotto.Model;

import camp.nextstep.edu.missionutils.Randoms;

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

public class LottoNumGenerater {

public List<Lotto> generateLottos(int count) {
List<Lotto> lottoNumList = new ArrayList<>();
for (int i = 0; i < count; i++) {
List<Integer> numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6);
Collections.sort(numbers);
lottoNumList.add(new Lotto(numbers));
System.out.println(numbers);
}
return lottoNumList;
}
}
44 changes: 44 additions & 0 deletions src/main/java/lotto/Model/LottoResultChecker.java
Copy link
Collaborator

Choose a reason for hiding this comment

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

mvc에서 비즈니스 로직을 수행하는 역할은 controller가 합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package lotto.Model;

import java.util.List;

public class LottoResultChecker {
Copy link
Collaborator

Choose a reason for hiding this comment

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

객체라기보다는 함수를 실행시키는 역할만 하는 것으로 보이네요.

public int[] checkCorrectNum(List<Lotto> lottos, List<Integer> winningNumbers, int bonusNumber) {
int[] prizeCnt = new int[5];
for (Lotto lotto : lottos) {
int correctCnt = 0;
boolean bonusCorrect = false;

for (Integer number : lotto.getNumbers()) {
if (winningNumbers.contains(number)) {
correctCnt++;
}
}

if (lotto.getNumbers().contains(bonusNumber)) {
bonusCorrect = true;
}

if (correctCnt == 6) {
prizeCnt[4]++;
}

if (correctCnt == 5 && bonusCorrect) {
prizeCnt[3]++;
}

if (correctCnt == 5 && !bonusCorrect) {
prizeCnt[2]++;
}

if (correctCnt == 4) {
prizeCnt[1]++;
}

if (correctCnt == 3) {
prizeCnt[0]++;
}
}
return prizeCnt;
}
}
119 changes: 119 additions & 0 deletions src/main/java/lotto/View/LottoInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package lotto.View;

import java.io.Console;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LottoInput {
private final Console console = System.console();
private final Scanner scanner;

public LottoInput() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

이건 무슨 의도죠? console은 주어진 요구사항입니다. 굳이 예외처리 하지 않아도 됩니다.

if (console == null) {
scanner = new Scanner(System.in);
} else {
scanner = null;
}
}

static final int MIN_LOTTO_NUMBER = LottoNumberRange.MIN_NUMBER.getValue();
static final int MAX_LOTTO_NUMBER = LottoNumberRange.MAX_NUMBER.getValue();
static final int LOTTO_NUMBER_COUNT = 6;
static final int LOTTO_PRICE_UNIT = 1000;

public enum LottoNumberRange {
MIN_NUMBER(1),
MAX_NUMBER(45);

final int value;

LottoNumberRange(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

public int getLottoPrice() {
while (true) {
try {
System.out.println("구입금액을 입력해주세요.");
String input;
if (console != null) {
input = console.readLine();
} else {
input = scanner.nextLine();
Copy link
Collaborator

Choose a reason for hiding this comment

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

굳이 왜 scanner 사용을 고집하는걸까요? 가독성이 떨어집니다.

}
int lottoPrice = Integer.parseInt(input);
if (lottoPrice % LOTTO_PRICE_UNIT != 0) {
throw new IllegalArgumentException("[ERROR] 구입 금액은 " + LOTTO_PRICE_UNIT + "원 단위로 입력해야 합니다.");
}
return lottoPrice;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

public List<Integer> getCorrectNum() {
while (true) {
try {
System.out.println("당첨 번호를 입력해 주세요.");
String input;
if (console != null) {
input = console.readLine();
} else {
input = scanner.nextLine();
}
return splitLottoNum(input);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

public int getBonusNum() {
while (true) {
try {
System.out.println("보너스 번호를 입력해주세요.");
String input;
if (console != null) {
input = console.readLine();
} else {
input = scanner.nextLine();
}
int bonusNumber = Integer.parseInt(input);
if (bonusNumber < MIN_LOTTO_NUMBER || bonusNumber > MAX_LOTTO_NUMBER) {
throw new IllegalArgumentException("[ERROR] 보너스 번호는 " + MIN_LOTTO_NUMBER + "부터 " + MAX_LOTTO_NUMBER + " 사이의 숫자여야 합니다.");
}
return bonusNumber;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

private List<Integer> splitLottoNum(String input) {
String[] splitLottoNumList = input.split(",");
List<Integer> lottoNum = new ArrayList<>();

for (String splitLottoNum : splitLottoNumList) {
try {
int num = Integer.parseInt(splitLottoNum.trim());
if (num < MIN_LOTTO_NUMBER || num > MAX_LOTTO_NUMBER) {
throw new IllegalArgumentException("[ERROR] 로또 번호는 " + MIN_LOTTO_NUMBER + "부터 " + MAX_LOTTO_NUMBER + " 사이의 숫자여야 합니다.");
}
lottoNum.add(num);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[ERROR] 잘못된 형식의 숫자가 입력되었습니다.");
}
}
if (lottoNum.size() != LOTTO_NUMBER_COUNT) {
throw new IllegalArgumentException("[ERROR] 당첨 번호는 " + LOTTO_NUMBER_COUNT + "개여야 합니다.");
}
return lottoNum;
}
}
24 changes: 24 additions & 0 deletions src/main/java/lotto/View/LottoResultPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lotto.View;

public class LottoResultPrinter {
private final int[] prizeMoney = { 5000, 50000, 1500000, 30000000, 2000000000 };

public void printResult(int[] prizeCnt, int totalCost) {
int totalMoney = 0;

System.out.println("당첨 통계");
System.out.println("---");
System.out.println("3개 일치 (5,000원) - " + prizeCnt[0] + "개");
System.out.println("4개 일치 (50,000원) - " + prizeCnt[1] + "개");
System.out.println("5개 일치 (1,500,000원) - " + prizeCnt[2] + "개");
System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + prizeCnt[3] + "개");
System.out.println("6개 일치 (2,000,000,000원) - " + prizeCnt[4] + "개");

for (int i = 0; i < prizeCnt.length; i++) {
totalMoney += prizeCnt[i] * prizeMoney[i];
}

double benefitRate = ((double) totalMoney / totalCost) * 100;
System.out.printf("총 수익률은 %.1f%%입니다.\n", benefitRate);
}
}
1 change: 1 addition & 0 deletions src/test/java/lotto/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lotto;

import camp.nextstep.edu.missionutils.test.NsTest;
import lotto.Controller.Application;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand Down
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