-
Notifications
You must be signed in to change notification settings - Fork 12
[로또 게임] 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
base: eoeh04
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ dependencies { | |
|
||
test { | ||
useJUnitPlatform() | ||
} | ||
} |
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 { | ||
|
||
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lottos 일급컬렉션에서 로또를 꺼내서, 각각의 Lotto로부터 Rank를 만들어 RankMap에 넣고 있군요. |
||
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; | ||
} | ||
|
||
} |
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); | ||
} | ||
} | ||
} |
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; | ||
|
||
|
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LottoMachine이 너무 많은 역할을 담당하고 있는 것 같아요.
기능과 역할에 맞게 객체를 조금 더 분리해보는 것은 어떤가요?