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단계 - 둜또(2λ“±) #3559

Open
wants to merge 8 commits into
base: h3yon
Choose a base branch
from
27 changes: 0 additions & 27 deletions src/main/java/auto/AutoLotteryApplication.java

This file was deleted.

33 changes: 33 additions & 0 deletions src/main/java/auto/AutoLottoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package auto;

import auto.application.AutoLottoService;
import auto.application.MatchLottoService;
import auto.application.MatchedAmount;
import auto.domain.Lotto;
import auto.domain.Lottos;
import auto.view.InputView;
import auto.view.ResultView;

import java.util.Map;

public class AutoLottoApplication {
private static final AutoLottoService autoService = new AutoLottoService();
private static final MatchLottoService matchService = new MatchLottoService();

public static void main(String[] args) {
int purchaseAmount = InputView.inputPurchaseAmount();
int lottoCount = autoService.getLottoCount(purchaseAmount);
ResultView.printLottoCounts(lottoCount);

Lottos totalLottoNumbers = Lottos.createLottoNumbersList(lottoCount);
ResultView.printTotalLottoNumbers(totalLottoNumbers);

Lotto winningNumbersLastWeek = InputView.inputWinningNumbersLastWeek();
int bonusBallNumber = InputView.inputBonusBallNumber(winningNumbersLastWeek);

Map<MatchedAmount, Integer> matchedCountMap = matchService.getMatchedCountMap(totalLottoNumbers,
winningNumbersLastWeek,
bonusBallNumber);
ResultView.printLottoStats(matchedCountMap, purchaseAmount);
}
}
25 changes: 0 additions & 25 deletions src/main/java/auto/application/AutoLotteryService.java

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/java/auto/application/AutoLottoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package auto.application;

import java.math.BigDecimal;

public class AutoLottoService {
private static final int LOTTO_PRICE = 1000;

public int getLottoCount(int purchaseAmount) {
return purchaseAmount / LOTTO_PRICE;
}

public static BigDecimal getReturnRate(int totalAmount, int amount) {
return BigDecimal.valueOf(totalAmount)
.divide(BigDecimal.valueOf(amount));
}
}
37 changes: 0 additions & 37 deletions src/main/java/auto/application/MatchLotteryService.java

This file was deleted.

59 changes: 59 additions & 0 deletions src/main/java/auto/application/MatchLottoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package auto.application;

import auto.domain.Lotto;
import auto.domain.Lottos;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static auto.application.MatchedAmount.NONE;
import static auto.application.MatchedAmount.findByCount;

public class MatchLottoService {
private static final Map<MatchedAmount, Integer> matchedCountMap = new HashMap<>();// = MatchedAmount.getMatchedCountMap();
Copy link

Choose a reason for hiding this comment

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

  1. μ„œλΉ„μŠ€ 계측이면 μ΅œλŒ€ν•œ λ¬΄μƒνƒœ(stateless)계측 섀계λ₯Ό 지양해보면 μ–΄λ–¨κΉŒμš”? μž¬μ‚¬μš©μ„± μ΄μŠˆκ°€ μžˆμœΌλ‹ˆκΉŒμš”
  2. 이 경우 Enumνƒ€μž…μ„ key둜 μ‚¬μš©ν•œλ‹€λ©΄ HashMap보닀 EnumMap을 μ‚¬μš©ν•˜λŠ”κ±΄ μ–΄λ–¨κΉŒμš”


private static void setUpWinningStatisticsMap() {
EnumSet.allOf(MatchedAmount.class)
.stream()
.filter(matchedAmount -> matchedAmount != NONE)
.sorted()
.forEach(matchedAmount -> matchedCountMap.put(matchedAmount, 0));
}

static boolean isMatchedBonusNumber(List<Integer> lottoNumbers,
List<Integer> winningNumbersLastWeek,
int bonusBallNumber) {
List<Integer> copyLottoNumbers = new ArrayList<>(lottoNumbers);
copyLottoNumbers.removeAll(winningNumbersLastWeek);
return copyLottoNumbers.contains(bonusBallNumber);
Copy link

Choose a reason for hiding this comment

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

bonusNumberκ°€ μ²˜μŒλΆ€ν„° 당첨 λ²ˆν˜Έμ™€ μ€‘λ³΅λ˜μ§€ μ•ŠλŠ” 번호라면, removeAll을 μˆ˜ν–‰ν•˜λ©° 맀번 번호λ₯Ό μ œκ±°ν•  ν•„μš”κ°€ 없을 것 κ°™λ„€μš”

}

public Map<MatchedAmount, Integer> getMatchedCountMap(Lottos lottoNumbersList,
Lotto winningNumbersLastWeek,
int bonusBallNumber) {
setUpWinningStatisticsMap();
for (Lotto lotto : lottoNumbersList.getLottoList()) {
setMatchedCountMap(lotto.getNumbers(),
winningNumbersLastWeek.getNumbers(),
isMatchedBonusNumber(lotto.getNumbers(),
winningNumbersLastWeek.getNumbers(),
bonusBallNumber));
}
return matchedCountMap;
}

private void setMatchedCountMap(List<Integer> lottoNumbers,
List<Integer> winningNumbersLastWeek,
boolean isMatchedBonusNumber) {
int matchedCountSum = (int) lottoNumbers.stream()
.filter(winningNumbersLastWeek::contains)
.count();
MatchedAmount matchedAmount = findByCount(matchedCountSum, isMatchedBonusNumber);
if (matchedAmount.getCount() > 0) {
matchedCountMap.put(matchedAmount, matchedCountMap.get(matchedAmount) + 1);
}
}
Comment on lines +51 to +58
Copy link

Choose a reason for hiding this comment

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

Stream API의 groupByλ₯Ό ν™œμš©ν•œλ‹€λ©΄ μ’€ 더 κ°„λ‹¨ν•˜κ²Œλ„ κ°€λŠ₯ν•œλ°, 고민해보면 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€.

}
10 changes: 9 additions & 1 deletion src/main/java/auto/application/MatchedAmount.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum MatchedAmount {
THREE(3, 5_000),
FOUR(4, 50_000),
FIVE(5, 1_500_000),
FIVE_AND_BONUS(5, 30_000_000),
SIX(6, 2_000_000_000),
NONE(0, 0);

Expand All @@ -28,7 +29,10 @@ public enum MatchedAmount {
this.amount = amount;
}

public static MatchedAmount findByCount(int count) {
public static MatchedAmount findByCount(int count, boolean isMatchedBonus) { // findByNumberWithMatchedBonus
if (count == FIVE.getCount()) {
Copy link

Choose a reason for hiding this comment

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

μ§€κΈˆμ€ λ³΄λ„ˆμŠ€ 번호 맀칭 μ—¬λΆ€λ§Œ 가지고 λ“±κΈ‰ νŒλ‹¨μ΄λ˜μ—ˆμ§€λ§Œ, 이런 쑰건듀이 κ³„μ†ν•΄μ„œ μΆ”κ°€λœλ‹€λ©΄ ν•΄λ‹Ή λ‘œμ§μ— κ³„μ†ν•΄μ„œ νŒλ‹¨ 뢄기문이 μΆ”κ°€λ˜μ•Όν• κΉŒμš”? λžŒλ‹€ν‘œν˜„μ‹μ„ ν™œμš©ν•΄μ„œ μ—΄κ±°νƒ€μž…μ˜ 각 μΈμŠ€ν„΄μŠ€κ°€ μžμ‹ μ΄ λ§žλŠ”μ§€ νŒŒμ•…ν•  수 μžˆλ„λ‘ ν•  수 μ—†μ„κΉŒμš”?

return isMatchedBonus ? FIVE_AND_BONUS : FIVE;
}
if (!MATCHED_AMOUNT_MAP.containsKey(count)) {
return NONE;
}
Expand All @@ -42,6 +46,10 @@ public static ConcurrentMap<Integer, Integer> getMatchedCountMap() {
.collect(Collectors.toConcurrentMap(MatchedAmount::getCount, u -> 0, (u, v) -> u));
}

public static boolean isMatchedNumberFiveAndBonus(MatchedAmount matchedAmount) {
return FIVE_AND_BONUS == matchedAmount;
}

public int getCount() {
return count;
}
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/auto/domain/AutoLotteryRepositoryImpl.java

This file was deleted.

38 changes: 38 additions & 0 deletions src/main/java/auto/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package auto.domain;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Lotto {
public final static int LOTTO_MIN_NUMBER = 1;
public final static int LOTTO_MAX_NUMBER = 45;
private static final String LOTTO_NUMBER_SIZE_ERROR_TEXT = "둜또 λ²ˆν˜ΈλŠ” 6개의 숫자둜 이루어져야 ν•©λ‹ˆλ‹€.";
private static final int MAXIMUM_LOTTO_NUMBER_SIZE = 6;
private final List<Integer> numbers;
Comment on lines +12 to +13
Copy link

Choose a reason for hiding this comment

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

  1. μƒμˆ˜μ™€ λ³€μˆ˜κ°„μ˜ κ°œν–‰μœΌλ‘œ 가독성을 λ†’ν˜€λ³΄λ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€.
  2. collection νƒ€μž…μ€ ν•„λ“œμ΄ˆκΈ°ν™”λ„ ꢌμž₯λ“œλ¦½λ‹ˆλ‹€.


public Lotto(List<Integer> numbers) {
if (numbers == null || numbers.size() != MAXIMUM_LOTTO_NUMBER_SIZE) {
throw new IllegalArgumentException(LOTTO_NUMBER_SIZE_ERROR_TEXT);
}
this.numbers = numbers;
}

public List<Integer> getNumbers() {
return Collections.unmodifiableList(numbers);
}

@Override
public String toString() {
return "" + numbers;
}

public static Lotto createLottoNumbers() {
List<Integer> numbers = IntStream.range(LOTTO_MIN_NUMBER, LOTTO_MAX_NUMBER + 1)
.boxed()
.collect(Collectors.toList());
Comment on lines +32 to +34
Copy link

Choose a reason for hiding this comment

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

  • 맀번 둜또번호λ₯Ό 1~45κΉŒμ§€ μƒμ„±ν•΄μ„œ μ»¬λ ‰μ…˜μ„ λ§Œλ“€κ³  , μ„žκ³ , 6개λ₯Ό κΊΌλ‚΄μ„œ μƒˆλ‘œμš΄ 리슀트λ₯Ό λ§Œλ“€μ–΄μ•Όν• κΉŒμš”?
  • 일급 객체λ₯Ό ν™œμš©ν•΄λ³΄λ©΄ μ–΄λ–¨κΉŒμš”?(이전 ν”Όλ“œλ°± 반볡)
  • 캐싱을 ν™œμš©ν•΄λ³΄λ©΄ μ–΄λ–¨κΉŒμš”?

Collections.shuffle(numbers);
return new Lotto(numbers.subList(0, 6));
}
}
24 changes: 24 additions & 0 deletions src/main/java/auto/domain/Lottos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package auto.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Lottos {
private final List<Lotto> lottoList;

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

public static Lottos createLottoNumbersList(int lottoCount) {
return new Lottos(IntStream.range(0, lottoCount)
.mapToObj(i -> Lotto.createLottoNumbers())
.collect(Collectors.toCollection(ArrayList::new)));
}

public List<Lotto> getLottoList() {
return lottoList;
}
}
7 changes: 0 additions & 7 deletions src/main/java/auto/infrastructure/AutoLotteryRepository.java

This file was deleted.

32 changes: 27 additions & 5 deletions src/main/java/auto/view/InputView.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
package auto.view;

import auto.domain.Lotto;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

import static auto.domain.Lotto.LOTTO_MAX_NUMBER;
import static auto.domain.Lotto.LOTTO_MIN_NUMBER;

public class InputView {
Copy link

Choose a reason for hiding this comment

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

λͺ¨λ“  ν΄λž˜μŠ€κ°€ 정적이라면, μœ ν‹Έλ¦¬ν‹° 클래슀둜 λ³Ό 수 μžˆλŠ”λ°, 객체 생성이 ν•„μš” μ—†λ‹€λ©΄ 객체 생성을λͺ»ν•˜λ„둝 막을 순 μ—†μ„κΉŒμš”?

private static final Scanner scanner = new Scanner(System.in);
private static final String EMPTY_BONUS_BALL_MESSAGE = "λ³΄λ„ˆμŠ€ 볼이 λΉ„μ–΄μžˆμŠ΅λ‹ˆλ‹€.";
private static final String IN_LOTTO_BONUS_BALL_MESSAGE = "둜또 λ²ˆν˜Έμ— ν¬ν•¨λœ λ³΄λ„ˆμŠ€ λ³Όμž…λ‹ˆλ‹€.";
private static final String NOT_RANGE_LOTTO_NUMBER_MESSAGE = "λ²ˆν˜ΈλŠ” 1 - 45κΉŒμ§€ μž…λ ₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.";

public static int inputPurchaseAmount() {
System.out.println("κ΅¬μž…κΈˆμ•‘μ„ μž…λ ₯ν•΄ μ£Όμ„Έμš”.");
return Integer.parseInt(scanner.nextLine());
}

public static List<Integer> inputWinningNumbersLastWeek() {
public static Lotto inputWinningNumbersLastWeek() {
System.out.println("μ§€λ‚œ μ£Ό 당첨 번호λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”.");
String winningNumbersLastWeek = scanner.nextLine();
return Arrays.stream(winningNumbersLastWeek.split(", "))
.map(Integer::parseInt)
.collect(Collectors.toList());
return new Lotto(Arrays.stream(winningNumbersLastWeek.split(", "))
.map(Integer::parseInt)
.collect(Collectors.toList()));
}

public static int inputBonusBallNumber(Lotto lastWeekWinningNumber) {
System.out.println(EMPTY_BONUS_BALL_MESSAGE);
int bonusNumber = Integer.parseInt(scanner.nextLine());
if (lastWeekWinningNumber.getNumbers()
.contains(bonusNumber))
throw new IllegalArgumentException(IN_LOTTO_BONUS_BALL_MESSAGE);
if (isNotInLottoNumberRange(bonusNumber))
throw new IllegalArgumentException(NOT_RANGE_LOTTO_NUMBER_MESSAGE);
return bonusNumber;
}

private static boolean isNotInLottoNumberRange(int bonusNumber) {
return !(bonusNumber >= LOTTO_MIN_NUMBER && bonusNumber <= LOTTO_MAX_NUMBER);
}
}
Loading