Skip to content

[로또 게임] Step 2 리뷰 요청드립니다 #16

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

Open
wants to merge 4 commits into
base: eoeh04
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ dependencies {

test {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package controller;
package com.study.controller;

import domain.Lotto;
import domain.LottoMachine;
import enums.Rank;
import view.InputView;
import view.OutputView;
import com.study.domain.LottoMachine;
import com.study.domain.Lottos;
import com.study.enums.Rank;
import com.study.view.InputView;
import com.study.view.OutputView;

import java.util.List;
import java.util.Map;
Expand All @@ -16,14 +16,16 @@ public class LottoController {

public static void main(String[] args) {

LottoMachine lottoMachine = new LottoMachine(inputView.inputMoney());
List<Lotto> lottoTickets = lottoMachine.getLottoTickets();
outputView.printLottoTickets(lottoTickets);
LottoMachine lottoMachine = new LottoMachine(inputView.inputMoney(), inputView.manualTicketCount());
List<List<Integer>> manulLottoNumbers = inputView.manualLottlNumbers(lottoMachine.getLottoManualTicketCount());

Lottos lottos = new Lottos(lottoMachine.getLottoTotalTickets(manulLottoNumbers));
outputView.printLottoTickets(lottos);

List<Integer> winningNumbers = lottoMachine.getWinningNumber(inputView.inputWinningNumbers());
int bonusNumber = lottoMachine.getBonusBall(inputView.inputBonusBall(), winningNumbers);

Map<Rank, Integer> rankResult = lottoMachine.getRankResult(lottoTickets, winningNumbers, bonusNumber);
Map<Rank, Integer> rankResult = lottoMachine.getRankResult(lottos, winningNumbers, bonusNumber);
outputView.printRankResult(rankResult);
outputView.printStatistics(lottoMachine.getProfitRate(rankResult));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package domain;
package com.study.domain;

import enums.Rank;
import com.study.enums.Rank;

import java.util.List;

public class Lotto {

private final List<Integer> lottoNumbers;
private final LottoValication lottoValication = new LottoValication();

public Lotto(List<Integer> randomNumber) {
lottoValication.lottoNumberValidate(randomNumber);
this.lottoNumbers = randomNumber;
}

Expand All @@ -17,8 +19,6 @@ public List<Integer> getLottoNumbers() {
}

public Rank getRank(List<Integer> winningNumber, int bonusNumber) {
countOfMatches(winningNumber);
countOfBonusMatch(bonusNumber);
return Rank.getRank(countOfMatches(winningNumber), countOfBonusMatch(bonusNumber));
}

Expand Down
105 changes: 105 additions & 0 deletions src/main/java/com/study/domain/LottoMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.study.domain;

import com.study.enums.Rank;
import com.study.enums.RankMap;
import com.study.utils.InputValidation;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LottoMachine {

Choose a reason for hiding this comment

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

LottoMachine이 너무 많은 역할을 담당하고 있는 것 같아요.
기능과 역할에 맞게 객체를 조금 더 분리해보는 것은 어떤가요?


private final int lottoTotalTicketCount;
private final int lottoAutoTicketCount;
private final int lottoManualTicketCount;
private final int money;
private static InputValidation inputValidation = new InputValidation();
private final static LottoValication LOTTO_VALIDATION = new LottoValication();

public LottoMachine(String inputMoney) {
this.money = LOTTO_VALIDATION.checkGivenMoney(inputMoney);
this.lottoTotalTicketCount = this.money / LOTTO_VALIDATION.LOTTO_PRICE;
lottoManualTicketCount = lottoTotalTicketCount;
lottoAutoTicketCount = 0;
}

public LottoMachine(String inputMoney, int manualTicketCount) {
this.money = LOTTO_VALIDATION.checkGivenMoney(inputMoney);
LOTTO_VALIDATION.canbyAllTickets(money, manualTicketCount);
this.lottoTotalTicketCount = this.money / LOTTO_VALIDATION.LOTTO_PRICE;
this.lottoManualTicketCount = manualTicketCount;
this.lottoAutoTicketCount = lottoTotalTicketCount - lottoManualTicketCount;
}

public int getLottoTicketCount() {
return this.lottoTotalTicketCount;
}

public int getLottoManualTicketCount() {
return lottoManualTicketCount;
}

public List<Integer> createRandomNumber() {
List<Integer> balls = IntStream.range(1, 45)
.boxed()
.collect(Collectors.toList());
Collections.shuffle(balls);
List<Integer> lottoNumbers = balls.subList(0, 6);
Collections.sort(lottoNumbers);
return lottoNumbers;
}

public List<Lotto> getLottoTotalTickets(List<List<Integer>> manualLottos) {
List<Lotto> lottoTickets = new ArrayList<>();
lottoTickets.addAll(getLottoManulTickets(manualLottos));
lottoTickets.addAll(getAutoLottoTickets());
return lottoTickets;
}

public List<Lotto> getAutoLottoTickets() {
List<Lotto> lottoTicket = new ArrayList<>();
for (int i = 0; i < lottoAutoTicketCount; i++) {
lottoTicket.add(new Lotto(createRandomNumber()));
}
return lottoTicket;
}

public List<Lotto> getLottoManulTickets(List<List<Integer>> manualLottos) {
List<Lotto> lottoTicket = new ArrayList<>();
for (int i = 0; i < lottoManualTicketCount; i++) {
lottoTicket.add(new Lotto(manualLottos.get(i)));
}
return lottoTicket;
}

public int getBonusBall(String bonusInput, List<Integer> winningNumber) {
int bonusInputNumber = Integer.parseInt(bonusInput);
LOTTO_VALIDATION.checkBound(bonusInputNumber);
LOTTO_VALIDATION.checkBonusDuplicate(bonusInputNumber, winningNumber);
return bonusInputNumber;
}

public RankMap getRankResult(Lottos lottos, List<Integer> winningNumber, int bonusNumber) {
RankMap rankMap = new RankMap(Rank.class);
for (Lotto lotto : lottos.getLottos()) {

Choose a reason for hiding this comment

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

Lottos 일급컬렉션에서 로또를 꺼내서, 각각의 Lotto로부터 Rank를 만들어 RankMap에 넣고 있군요.
일급컬렉션을 만드신 이유를 한 번 더 고민해보시면 좋을 것 같아요. java Collection을 한 번 감싸고, 다른 이름을 붙여주기만 하면 일급컬렉션으로 생각해도 좋을까요? java Collection을 감싸는 이유는 무엇인가요?
Lottos로 감싼 collection을 되도록 밖으로 꺼내지 않고 해결할 수 있는 방법은 없나요?

rankMap.put(lotto.getRank(winningNumber, bonusNumber), rankMap.get(lotto.getRank(winningNumber, bonusNumber)) + 1);
}
return rankMap;
}

public double getProfitRate(Map<Rank, Integer> rankResult) {
double totalPrize = rankResult.entrySet()
.stream()
.mapToDouble(rank -> rank.getKey().prize() * rank.getValue())
.sum();
return totalPrize / this.money;
}

public List<Integer> getWinningNumber(String winningNumber) {
List<Integer> winningNumbers = inputValidation.toIntegers(inputValidation.splitByComma(winningNumber));
LOTTO_VALIDATION.lottoNumberValidate(winningNumbers);
return winningNumbers;
}

}
75 changes: 75 additions & 0 deletions src/main/java/com/study/domain/LottoValication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.study.domain;

import java.util.List;
import java.util.regex.Pattern;

public class LottoValication {

public static final int LOTTO_PRICE = 1000;
public static final int BOUND_MIN = 1;
public static final int BOUND_MAX = 45;
public static final int LOTTO_LENGTH = 6;

private static final String LOTTO_PRICE_PATTERN = "\\d*000";
private static final String ALERT_CHECK_BOUND = String.format("당첨번호는 %d - %d 사이값을 입력해주세요", BOUND_MIN, BOUND_MAX);
private static final String ALERT_CHECK_LENGTH = String.format("당첨번호는 %d개 이어야 합니다.", LOTTO_LENGTH);
private static final String ALERT_CHECK_DUPLICATION = "중복되는 숫자가 포함되어 있는지 확인해주세요.";
private static final String ALERT_CHECK_BONUS_DUPLICATE = "보너스볼이 당첨 번호와 중복되는지 확인해주세요.";
private static final String ALERT_CHECK_PAYMENT_MANUAL= "구매 금액 이상의 수동 로또를 살 수 없습니다.";
private static final String ALERT_CHECK_PAYMENT= "1000원 단위의 금액만 투입할 수 있습니다.";

public int checkGivenMoney(String givenMoney) {
if (!Pattern.matches(LOTTO_PRICE_PATTERN, givenMoney)) {
throw new IllegalArgumentException(ALERT_CHECK_PAYMENT);
}
return Integer.parseInt(givenMoney);
}

public void lottoNumberValidate(List<Integer> winningNumbers) {
checkListBound(winningNumbers);
checkLength(winningNumbers);
checkDuplicate(winningNumbers);
}

public void checkBound(int number) {
if (BOUND_MIN > number || number > BOUND_MAX) {
throw new IllegalArgumentException(ALERT_CHECK_BOUND);
}
}

public void checkBonusDuplicate(int bonusInputNumber, List<Integer> winningNumber) {
boolean isDuplicate = winningNumber.stream()
.anyMatch(number -> bonusInputNumber == number);

if (isDuplicate) {
throw new IllegalArgumentException(ALERT_CHECK_BONUS_DUPLICATE);
}
}

private void checkListBound(List<Integer> winningNumbers) {
for (Integer winningNumber : winningNumbers) {
checkBound(winningNumber);
}
}
private void checkLength(List<Integer> winningNumbers) {
if (winningNumbers.size() != LOTTO_LENGTH) {
throw new IllegalArgumentException(ALERT_CHECK_LENGTH);

}
}
private void checkDuplicate(List<Integer> winningNumbers) {
int countOfDeDuplication = (int) winningNumbers.stream()
.distinct()
.count();

if (countOfDeDuplication != winningNumbers.size()) {
throw new IllegalArgumentException(ALERT_CHECK_DUPLICATION);
}
}

public void canbyAllTickets(int totalCount, int manulCount) {
if(totalCount<manulCount) {
throw new IllegalArgumentException(ALERT_CHECK_PAYMENT_MANUAL);
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/study/domain/Lottos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.study.domain;

import java.util.List;

public class Lottos {

private final List<Lotto> lottos;

public Lottos(List<Lotto> lottos) {
this.lottos = lottos;
}

public List<Lotto> getLottos() {
return lottos;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package enums;
package com.study.enums;

import java.util.Arrays;

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/study/enums/RankMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.study.enums;

import java.util.EnumMap;

public class RankMap extends EnumMap<Rank, Integer> {

Choose a reason for hiding this comment

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

Java Collection을 상속받고 있군요. RankMap은 EnumMap과 무슨 차이가 있나요?
이렇게 상속 받은 이유가 무엇인가요?


{
put(Rank.FIRST_PLACE, 0);
put(Rank.SECOND_PLACE, 0);
put(Rank.THIRD_PLACE, 0);
put(Rank.FOURTH_PLACE, 0);
put(Rank.FIFTH_PLACE, 0);
put(Rank.ETC, 0);
}
public RankMap(Class<Rank> keyType) {
super(keyType);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package utils;
package com.study.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class InputValidation {
public class InputValidation {

private static final String COMMA = ",";
private static final String ALERT_CHECK_COMMA = String.format("구분자를 \"%s\"로 입력하셨는지 확인해주세요.", COMMA);
private static final String ALERT_CHECK_NULL_OR_EMPTY = String.format("\"%s\"로 구분한 지난 주 당첨번호를 입력해주세요.", COMMA);




public static List<Integer> toIntegers(List<String> input) {
return new ArrayList<>(Collections.unmodifiableList(input.stream()
.mapToInt(Integer::parseInt)
Expand All @@ -40,5 +36,7 @@ public void checkNullOrEmpty(String input) {
}
}


public String removeBlank(String input) {
return input.replace(" ", "");
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/study/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.study.view;

import com.study.utils.InputValidation;

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

public class InputView {

private static Scanner scanner = new Scanner(System.in);
private static final InputValidation INPUT_VALICATION = new InputValidation();

public String inputMoney() {
System.out.println("구입금액을 입력해 주세요.");
String money = scanner.nextLine();
INPUT_VALICATION.checkNullOrEmpty(money);
return money;
}

public int manualTicketCount() {
System.out.println("수동으로 구매할 로또 수를 입력해 주세요.");
String manulTicketCount = scanner.nextLine();
INPUT_VALICATION.checkNullOrEmpty(manulTicketCount);
return Integer.parseInt(manulTicketCount);
}

public List<List<Integer>> manualLottlNumbers(int manulTicketCount) {
System.out.println("수동으로 구매할 번호를 입력해 주세요.");
List<List<Integer>> lottos = new ArrayList<>();
for (int i = 0; i < manulTicketCount; i++) {
List<Integer> lottoNumbers = manualLottlNumber();
lottos.add(lottoNumbers);
}
return lottos;
}

public String inputWinningNumbers() {
System.out.println("지난 주 당첨 번호를 입력해 주세요.");
String winningNumber = scanner.nextLine();
INPUT_VALICATION.checkNullOrEmpty(winningNumber);
return winningNumber;
}

public String inputBonusBall() {
System.out.println("보너스 볼을 입력해 주세요.");
return scanner.nextLine();
}

private List<Integer> manualLottlNumber() {
String manulTicketCount = INPUT_VALICATION.removeBlank(scanner.nextLine());
INPUT_VALICATION.checkNullOrEmpty(manulTicketCount);
List<String> manulLottoNumbers = InputValidation.splitByComma(manulTicketCount);
return InputValidation.toIntegers(manulLottoNumbers);
}

}
Loading