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

3단계 구현 #3973

Open
wants to merge 1 commit into
base: byh0923
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
4 changes: 3 additions & 1 deletion src/main/java/lotto/LottoDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lotto.entity.LottoMoney;
import lotto.controller.LottoMachine;
import lotto.entity.LottoNumber;
import lotto.entity.Lottos;
import lotto.entity.PrizePolicy;
import lotto.view.InputView;
Expand All @@ -27,7 +28,8 @@ public static void main(String[] args) {
resultView.printLottoTickets(totalLottoTickets);

List<Integer> matchingCountResult = lottoMachine.checkMatchingNumber(inputView.inputWinningNumbers());
Map<PrizePolicy, Integer> matchingResult = lottoMachine.winningResult(matchingCountResult);
LottoNumber bonusBall = inputView.inputBonusBall();
Map<PrizePolicy, Integer> matchingResult = lottoMachine.winningResult(matchingCountResult, bonusBall);

int totalPrizeMoney = resultView.printTotalResult(matchingResult);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/lotto/controller/LottoMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public List<Integer> checkMatchingNumber(List<LottoNumber> inputWinningNumbers)
return totalLottoTicket.getMatchingCounts(new Lotto(inputWinningNumbers));
}

public Map<PrizePolicy, Integer> winningResult(List<Integer> matchingLottoTickets) {
return totalLottoTicket.calculateWinLottoTicket(matchingLottoTickets);
public Map<PrizePolicy, Integer> winningResult(List<Integer> matchingLottoTickets, LottoNumber bonusBall) {
Copy link

Choose a reason for hiding this comment

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

이전에 드린 코멘트와 같이 컨트롤러는 도메인을 상태로 관리하지 않아야합니다.
현재 LottoDisplay 가 컨트롤러의 역할처럼 보이네요!
현재 Lottos 를 통해 로직이 모두 처리되므로 옮겨보시는것도 좋을것 같아요.

추가로 checkMatchingNumber 의 반환값이 winningResult 로 반드시 전달 된다면
메서드를 분리하지 않는것이 명확하지 않을까요?

return totalLottoTicket.calculateWinLottoTicket(matchingLottoTickets, bonusBall);
}

public double rateOfReturnResult(int totalMoney, int totalPrizeMoney) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/lotto/entity/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public int matchCount(Lotto winningLotto) {
.filter(winningLotto.getLottoNumbers()::contains)
.count();
}

public boolean containsBonusBall(LottoNumber bonusBall) {
return lottoNumbers.contains(bonusBall);
}
}
9 changes: 6 additions & 3 deletions src/main/java/lotto/entity/Lottos.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ public List<Integer> getMatchingCounts(Lotto winningLotto) {
.collect(Collectors.toList());
}

public Map<PrizePolicy, Integer> calculateWinLottoTicket(List<Integer> matchingLottoTickets) {
public Map<PrizePolicy, Integer> calculateWinLottoTicket(List<Integer> matchingLottoTickets, LottoNumber bonusBall) {
Copy link

Choose a reason for hiding this comment

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

List<Integer> matchingLottoTickets, LottoNumber bonusBall

뒤의 인자는 입력한 보너스볼인데 앞의 인자는 입력한 로또 숫자가아니네요!
위의 드린 코멘트 참고하셔서 고민해보시면 좋을것 같습니다.

Map<PrizePolicy, Integer> winLottoTicket = new HashMap<>();
for (Integer matchCount : matchingLottoTickets) {
inputWinLottoTicket(winLottoTicket, matchCount);
for (int i = 0; i < matchingLottoTickets.size(); i++) {
int matchCount = matchingLottoTickets.get(i);
Lotto lottoTicket = values.get(i);
PrizePolicy prizePolicy = PrizePolicy.fromMatchCountAndBonus(matchCount, lottoTicket.containsBonusBall(bonusBall));
winLottoTicket.put(prizePolicy, winLottoTicket.getOrDefault(prizePolicy, 0) + 1);
}
return winLottoTicket;
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/lotto/entity/PrizePolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import java.util.Arrays;

public enum PrizePolicy {
ZERO(0, 0),
Copy link

Choose a reason for hiding this comment

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

당첨이 아닌 0,1,2 개 에 대해서 관리할필요가 있을까요? 🤔

ONE(1, 0),
TWO(2, 0),
THREE(3, 5_000),
FOUR(4, 50_000),
FIVE(5, 1_500_000),
FIVE_BONUS(5, 30_000_000),
SIX(6, 2_000_000_000);

private final int matchCount;
Expand Down Expand Up @@ -34,4 +38,11 @@ public static PrizePolicy fromMatchCount(int matchCount) {
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("유효하지 않은 매칭 숫자: " + matchCount));
}

public static PrizePolicy fromMatchCountAndBonus(int matchCount, boolean bonusMatch) {
if (matchCount == 5 && bonusMatch) {
return FIVE_BONUS;
}
return fromMatchCount(matchCount);
}
}
15 changes: 11 additions & 4 deletions src/main/java/lotto/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ public class InputView {

public int inputTotalAmount() {
System.out.println("구입금액을 입력해 주세요.");
return SCANNER.nextInt();
int totalAmount = Integer.parseInt(SCANNER.nextLine()); // 금액을 입력 받음
return totalAmount;
}

public List<LottoNumber> inputWinningNumbers() {
System.out.println("지난 주 당첨 번호를 입력해 주세요.");
String winningNumbers = SCANNER.next();
return inputNumberChangeArray(winningNumbers)
String winningNumbers = SCANNER.nextLine();
return inputNumberChangeArray(winningNumbers.trim())
.map(LottoNumber::new)
.collect(Collectors.toList());
}

public static Stream<Integer> inputNumberChangeArray(String winningNumbers) {
return Arrays.stream(winningNumbers.replace(" ","").split(","))
return Arrays.stream(winningNumbers.split(",\\s*"))
.map(Integer::parseInt);
}

public LottoNumber inputBonusBall() {
System.out.println("보너스 볼을 입력해 주세요.");
int bonusBall = Integer.parseInt(SCANNER.nextLine()); // 여기서 nextLine()으로 보너스 볼을 받음
return new LottoNumber(bonusBall);
}
}
13 changes: 11 additions & 2 deletions src/main/java/lotto/view/ResultView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.Map;

public class ResultView {

public static final int MIN_MATCH_COUNT = 3;

public void printTotalLottoTicketCount(int totalLottoTicketCount) {
System.out.println(totalLottoTicketCount + "개를 구매했습니다.");
}
Expand All @@ -21,8 +24,14 @@ public int printTotalResult(Map<PrizePolicy, Integer> matchingResult) {
int matchingTicketCount = matchingResult.getOrDefault(prizePolicy, 0);
int prize = prizePolicy.getPrize();
int matchCount = prizePolicy.getMatchCount();

System.out.println(matchCount + "개 일치 - (" + prize + "원) - " + matchingTicketCount + "개");
if (matchCount >= MIN_MATCH_COUNT) {
String bonusMessage = (prizePolicy == PrizePolicy.FIVE_BONUS) ? ", 보너스 볼 일치" : "";
System.out.println(String.format("%d개 일치%s - (%d원) - %d개",
matchCount,
bonusMessage,
prize,
matchingTicketCount));
}

totalPrizeMoney += prize * matchingTicketCount;
}
Expand Down