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

πŸš€ 4단계 - 둜또(μˆ˜λ™) #3550

Open
wants to merge 6 commits into
base: gkssk4163
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@
- [X] λ³΄λ„ˆμŠ€λ³Ό κΈ°λŠ₯ μΆ”κ°€
- [X] λ³΄λ„ˆμŠ€λ³Ό μ‚¬μš©μž μž…λ ₯ κ΅¬ν˜„
- [X] λ³΄λ„ˆμŠ€λ³Όμ„ ν¬ν•¨ν•œ 둜또 생성
- [X] 당첨 톡계에 2λ“± μΆ”κ°€
- [X] 당첨 톡계에 2λ“± μΆ”κ°€
- [X] μˆ˜λ™ 둜또 ꡬ맀 κ΅¬ν˜„
- [X] κ΅¬μž…κ°œμˆ˜λ³΄λ‹€ 큰 경우 였λ₯˜
- [ ] μˆ˜λ™ 둜또 번호 μž…λ ₯ κ΅¬ν˜„
4 changes: 3 additions & 1 deletion src/main/java/lotto/LottoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import lotto.view.InputView;
import lotto.view.ResultView;

import java.util.List;
import java.util.Map;

public class LottoController {

public static void main(String[] args) {
int purchasePrice = InputView.askPurchasePrice();
List<Lotto> manualLottoList = InputView.askManualLottoCountAndNumber();

LottoGenerator lottoRandomGenerator = new LottoRandomGenerator();
LottoBundle lottoBundle = new LottoBundle(lottoRandomGenerator, purchasePrice);
LottoBundle lottoBundle = new LottoBundle(lottoRandomGenerator, purchasePrice, manualLottoList);
ResultView.printPurchaseInfo(lottoBundle);

Lotto winningLotto = InputView.askLottoWinningNumbers();
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/lotto/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lotto.domain;
Copy link

Choose a reason for hiding this comment

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

LottoNumber 에 변경사항이 μ—†μ–΄ 여기에 λ‚¨κ²¨λ†“μŠ΅λ‹ˆλ‹€.

LottoNumber 의 경우 1~45 의 숫자이기 λ•Œλ¬Έμ— 맀번 μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ§€μ•Šκ³ 
μΊμ‹±μ²˜λ¦¬ν•˜μ—¬ κ°™μ€λ²ˆν˜Έμ— λŒ€ν•΄μ„  같은 μΈμŠ€ν„΄μŠ€λ₯Ό μ‚¬μš©ν•΄λ³Όμˆ˜ μžˆμ„κ²ƒ κ°™μ•„μš”.

둜또 ν”Όλ“œλ°± - α„Œα…₯α†Όα„Œα…₯ᆨ ᄑᅒᆨ토라 메소드, 안스ᄐα…₯ᆫ스 ᄏᅒ상 을 μ°Έκ³ ν•˜μ—¬ μ μš©ν•΄λ³΄λ©΄ 쒋을것 κ°™μŠ΅λ‹ˆλ‹€.


import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
Expand All @@ -12,19 +12,19 @@ public class Lotto {

private final Set<LottoNumber> numbers;

public Lotto(List<Integer> numbers) {
public Lotto(Set<Integer> numbers) {
this.numbers = new TreeSet<>(parseLottoNumber(numbers));
validate();
}

public Lotto(Integer... numbers) {
this(Arrays.asList(numbers));
this(Set.copyOf(Arrays.asList(numbers)));
Copy link

Choose a reason for hiding this comment

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

Suggested change
this(Set.copyOf(Arrays.asList(numbers)));
this(Set.of(numbers));

}

private List<LottoNumber> parseLottoNumber(List<Integer> numbers) {
private Set<LottoNumber> parseLottoNumber(Set<Integer> numbers) {
return numbers.stream()
.map(value -> new LottoNumber(value))
.collect(Collectors.toList());
.collect(Collectors.toSet());
}

private void validate() {
Expand All @@ -33,18 +33,18 @@ private void validate() {
}
}

public List<LottoNumber> numbers() {
return this.numbers.stream().collect(Collectors.toList());
public Set<LottoNumber> numbers() {
return Collections.unmodifiableSet(this.numbers);
}

public boolean hasNumber(LottoNumber lottoNumber) {
return this.numbers().contains(lottoNumber);
return this.numbers.contains(lottoNumber);
}

public int matchCount(Lotto winningLotto) {
return (int) winningLotto.numbers()
.stream()
.filter(value -> this.hasNumber(value))
.filter(this::hasNumber)
.count();
}

Expand Down
63 changes: 47 additions & 16 deletions src/main/java/lotto/domain/LottoBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,82 @@

import lotto.strategy.LottoGenerator;

import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LottoBundle {

private static final int LOTTO_PRICE = 1000;

private final List<Lotto> lottoList;
private final List<Lotto> manualLottoList;
Copy link

Choose a reason for hiding this comment

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

LottoBundle 에선 μˆ˜λ™μΈμ§€ μžλ™μΈμ§€ ꡬ뢄없이 ν•˜λ‚˜μ˜ μ»¬λ ‰μ…˜μœΌλ‘œ κ΄€λ¦¬ν•΄λ„λ˜μ§€ μ•Šμ„κΉŒμš”? πŸ€”
μ •μ νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό ν™œμš©ν•˜μ—¬ μƒμ„±ν•΄μ•Όν•˜λŠ” μžλ™λ‘œλ˜ + μˆ˜λ™μƒμ„±λœ 둜또λ₯Ό λ”ν•΄μ„œ λͺ©λ‘μ„
λ§Œλ“€λ„λ‘ ν•΄λ³Όμˆ˜λ„ μžˆμ„κ²ƒ κ°™μŠ΅λ‹ˆλ‹€.

private final List<Lotto> automaticLottoList;

public LottoBundle(List<Lotto> lottoList) {
this.lottoList = lottoList;
public LottoBundle(List<Lotto> automaticLottoList, List<Lotto> manualLottoList) {
this.manualLottoList = manualLottoList;
this.automaticLottoList = automaticLottoList;
}

public LottoBundle(List<Lotto> automaticLottoList) {
this(automaticLottoList, new ArrayList<>());
}

public LottoBundle(LottoGenerator lottoGenerator, int lottoPurchasedPrice, List<Lotto> manualLottoList) {
this(
IntStream.range(0, getAutomaticCount(lottoPurchasedPrice, manualLottoList.size()))
.mapToObj(i -> lottoGenerator.generate())
.collect(Collectors.toList()),
manualLottoList
);
}

public LottoBundle(LottoGenerator lottoGenerator, int lottoPurchasedPrice) {
this(IntStream.range(0, lottoPurchasedPrice / LOTTO_PRICE)
.mapToObj(i -> lottoGenerator.generate())
.collect(Collectors.toList()));
this(lottoGenerator, lottoPurchasedPrice, new ArrayList<>());
}

private static int getAutomaticCount(int lottoPurchasedPrice, int manualCount) {
int totalCount = lottoPurchasedPrice / LOTTO_PRICE;
if (totalCount < manualCount) {
throw new IllegalArgumentException(
String.format("κ΅¬λ§€ν•œ 둜또 κ°œμˆ˜λŠ” %dκ°œμž…λ‹ˆλ‹€. %d개 μ΄ν•˜λ‘œ μž…λ ₯ν•΄μ£Όμ„Έμš”.", totalCount, totalCount)
);
}

return (lottoPurchasedPrice / LOTTO_PRICE) - manualCount;
}

public Map<LottoResult, Integer> checkWinningResult(WinningLotto winningLotto) {
Map<LottoResult, Integer> lottoResults = Arrays.stream(LottoResult.values())
.collect(Collectors.toMap(key -> key, value -> 0, (x, y) -> y, () -> new EnumMap<>(LottoResult.class)));

for (Lotto lotto : lottoList) {
for (Lotto lotto : lottoList()) {
LottoResult lottoResult = winningLotto.getLottoResult(lotto);
lottoResults.put(lottoResult, lottoResults.get(lottoResult) + 1);
}

return lottoResults;
}

public int lottoCount() {
return lottoList.size();
public List<Lotto> lottoList() {
List<Lotto> totalLottoList = new ArrayList<>();
totalLottoList.addAll(manualLottoList);
totalLottoList.addAll(automaticLottoList);
return totalLottoList;
}

public List<Lotto> lottoList() {
return lottoList;
public int totalLottoCount() {
return manualLottoList.size() + automaticLottoList.size();
}

public int purchasedPrice() {
return lottoCount() * LOTTO_PRICE;
return totalLottoCount() * LOTTO_PRICE;
}

public int manualCount() {
return this.manualLottoList.size();
}

public int automaticCount() {
return automaticLottoList.size();
}
}
4 changes: 2 additions & 2 deletions src/main/java/lotto/domain/WinningLotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public WinningLotto(Lotto winningLotto, LottoNumber bonus) {

private void validate() {
Copy link

Choose a reason for hiding this comment

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

numbers() λ₯Ό κΊΌλ‚΄μ™€μ„œ 순회λ₯Ό ν•˜κ³  μžˆμ§€λ§Œ μ‹€μ œ winningLottoNumber λŠ”
μ‚¬μš©ν•˜κ³  μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

ν•˜λ‹¨κ³Ό 같이 μ²˜λ¦¬ν•΄λ³Όμˆ˜ μžˆμ§€ μ•Šμ„κΉŒμš”?

if (winningLotto.hasNumber(this.bonus)) {
    // μ˜ˆμ™Έ λ°œμƒ
}

this.winningLotto.numbers().stream()
.filter(winningLottoNumber -> winningLottoNumber.equals(this.bonus))
.filter(winningLottoNumber -> winningLotto.hasNumber(this.bonus))
.findAny()
.ifPresent(a -> {
throw new IllegalArgumentException();
throw new IllegalArgumentException("λ³΄λ„ˆμŠ€λ²ˆν˜Έκ°€ λ‹Ήμ²¨λ²ˆν˜Έμ— 이미 ν¬ν•¨λœ λ²ˆν˜Έμž…λ‹ˆλ‹€.");
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/lotto/strategy/LottoRandomGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class LottoRandomGenerator implements LottoGenerator {

Expand All @@ -17,7 +18,7 @@ public Lotto generate() {
}
Collections.shuffle(numbers);

return new Lotto(numbers.subList(0, Lotto.LOTTO_NUMBER_COUNT));
return new Lotto(Set.copyOf(numbers.subList(0, Lotto.LOTTO_NUMBER_COUNT)));
}

}
25 changes: 20 additions & 5 deletions src/main/java/lotto/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import lotto.domain.Lotto;
import lotto.domain.LottoNumber;

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

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.

μš”κ΅¬μ‚¬ν•­

  • μ˜ˆμ™Έ 처리λ₯Ό 톡해 μ—λŸ¬κ°€ λ°œμƒν•˜μ§€ μ•Šλ„λ‘ ν•œλ‹€.

힌트

  • μ‚¬μš©μžκ°€ 잘λͺ»λœ 값을 μž…λ ₯ν–ˆμ„ λ•Œ java exception으둜 μ—λŸ¬ 처리λ₯Ό ν•œλ‹€.

μƒλ‹¨μ˜ μš”κ΅¬μ‚¬ν•­μ— 맞좰 μž…λ ₯λ‹¨κ³„μ—μ„œ μ˜ˆμ™Έκ°€ λ°œμƒν•˜μ§€ μ•Šλ„λ‘
try catch ꡬ문을 ν™œμš©ν•˜μ—¬ μž¬μž…λ ₯을 받도둝 해보면 쒋을것 κ°™μŠ΅λ‹ˆλ‹€.

Expand All @@ -19,6 +17,10 @@ public static int askPurchasePrice() {

public static Lotto askLottoWinningNumbers() {
System.out.println("μ§€λ‚œ μ£Ό 당첨 번호λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”.");
return getLotto();
}

private static Lotto getLotto() {
return new Lotto(parseInt(asList(split(scanner.nextLine()))));
}

Expand All @@ -36,15 +38,28 @@ private static List<String> asList(String[] winningNumbers) {
return Arrays.asList(winningNumbers);
}

private static List<Integer> parseInt(List<String> winningNumbers) {
private static Set<Integer> parseInt(List<String> winningNumbers) {
return winningNumbers.stream()
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList());
.collect(Collectors.toSet());
}

public static LottoNumber askBonusNumber() {
System.out.println("λ³΄λ„ˆμŠ€ 볼을 μž…λ ₯ν•΄ μ£Όμ„Έμš”.");
return new LottoNumber(scanInt());
}

public static List<Lotto> askManualLottoCountAndNumber() {
System.out.println("μˆ˜λ™μœΌλ‘œ ꡬ맀할 둜또 수λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”.");
int manualLottoCount = scanInt();

System.out.println("μˆ˜λ™μœΌλ‘œ ꡬ맀할 번호λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”.");
List<Lotto> lottoList = new ArrayList<>();
for (int index = 0; index < manualLottoCount; index++) {
lottoList.add(getLotto());
}

return lottoList;
}
}
2 changes: 1 addition & 1 deletion src/main/java/lotto/view/ResultView.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class ResultView {

public static void printPurchaseInfo(LottoBundle lottoBundle) {
System.out.printf("%d개λ₯Ό κ΅¬λ§€ν–ˆμŠ΅λ‹ˆλ‹€.", lottoBundle.lottoCount());
System.out.printf("μˆ˜λ™μœΌλ‘œ %dμž₯, μžλ™μœΌλ‘œ %d개λ₯Ό κ΅¬λ§€ν–ˆμŠ΅λ‹ˆλ‹€.\n", lottoBundle.manualCount(), lottoBundle.automaticCount());
printLottoNumbers(lottoBundle.lottoList());
}

Expand Down
49 changes: 48 additions & 1 deletion src/test/java/lotto/domain/LottoBundleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class LottoBundleTest {

Expand Down Expand Up @@ -38,8 +40,53 @@ public static List<Arguments> LottoResultProvider() {
@Test
void 둜또_μ—¬λŸ¬κ°œ_ꡬ맀() {
LottoBundle lottoBundle = new LottoBundle(new LottoRandomGenerator(), 5000);
assertThat(lottoBundle.lottoCount()).isEqualTo(5);
assertThat(lottoBundle.totalLottoCount()).isEqualTo(5);
assertThat(lottoBundle.purchasedPrice()).isEqualTo(5000);
}

@Test
void 둜또_μˆ˜λ™_ꡬ맀() {
List<Lotto> manualLottoList = Arrays.asList(
new Lotto(1, 2, 3, 4, 5, 6),
new Lotto(7, 8, 9, 10, 11, 12),
new Lotto(13, 14, 15, 16, 17, 18)
);
LottoBundle lottoBundle = new LottoBundle(new LottoRandomGenerator(), 5000, manualLottoList);
assertThat(lottoBundle.manualCount()).isEqualTo(3);
assertThat(lottoBundle.automaticCount()).isEqualTo(2);
}

@Test
void 둜또ꡬ맀_κ°œμˆ˜λ³΄λ‹€_μˆ˜λ™κ°œμˆ˜λ₯Ό_더_많이_μž…λ ₯ν•œ_경우_였λ₯˜() {
List<Lotto> manualLottoList = Arrays.asList(
new Lotto(1, 2, 3, 4, 5, 6),
new Lotto(7, 8, 9, 10, 11, 12),
new Lotto(13, 14, 15, 16, 17, 18),
new Lotto(19, 20, 21, 22, 23, 24),
new Lotto(25, 26, 27, 28, 29, 30),
new Lotto(31, 32, 33, 34, 35, 36)
);

assertThatThrownBy(() -> {
new LottoBundle(new LottoRandomGenerator(), 5000, manualLottoList);
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("κ΅¬λ§€ν•œ 둜또 κ°œμˆ˜λŠ” 5κ°œμž…λ‹ˆλ‹€. 5개 μ΄ν•˜λ‘œ μž…λ ₯ν•΄μ£Όμ„Έμš”.");
}

@Test
void μˆ˜λ™λ‘œλ˜_정보확인() {
List<Lotto> manualLottoList = Arrays.asList(
new Lotto(1, 2, 3, 4, 5, 6),
new Lotto(7, 8, 9, 10, 11, 12),
new Lotto(13, 14, 15, 16, 17, 18)
);

LottoBundle lottoBundle = new LottoBundle(new LottoRandomGenerator(), 5000, manualLottoList);

List<Lotto> lottoList = lottoBundle.lottoList();
assertThat(lottoList.get(0)).usingRecursiveComparison().isEqualTo(manualLottoList.get(0));
assertThat(lottoList.get(1)).usingRecursiveComparison().isEqualTo(manualLottoList.get(1));
assertThat(lottoList.get(2)).usingRecursiveComparison().isEqualTo(manualLottoList.get(2));
}

}
19 changes: 9 additions & 10 deletions src/test/java/lotto/domain/LottoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

Expand All @@ -35,21 +34,21 @@ class LottoTest {
@Test
void μˆ˜λ™μƒμ„±ν…ŒμŠ€νŠΈ_둜또숫자_쀑볡일_경우_IllegalArgumentException_였λ₯˜λ°œμƒ() {
assertThatThrownBy(() -> {
new Lotto(Arrays.asList(1, 2, 3, 4, 5, 5));
new Lotto(1, 2, 3, 4, 5, 5);
}).isInstanceOf(IllegalArgumentException.class);
}

@ParameterizedTest
@ValueSource(ints = {0, 46})
void μˆ˜λ™μƒμ„±ν…ŒμŠ€νŠΈ_λ‘œλ˜μˆ«μžλ²”μœ„_λ²—μ–΄λ‚ _경우_IllegalArgumentException_였λ₯˜λ°œμƒ(int invalidNumber) {
assertThatThrownBy(() -> {
new Lotto(Arrays.asList(1, 2, 3, 4, 5, invalidNumber));
new Lotto(1, 2, 3, 4, 5, invalidNumber);
}).isInstanceOf(IllegalArgumentException.class);
}

@ParameterizedTest
@MethodSource("LottoMatchParameterProvider")
void 둜또_일치_확인(List<Integer> lottoList, LottoResult expected) {
void 둜또_일치_확인(Integer[] lottoList, LottoResult expected) {
WinningLotto winningLotto = new WinningLotto(new Lotto(1, 2, 3, 4, 5, 6), new LottoNumber(7));
Lotto lotto = new Lotto(lottoList);

Expand All @@ -60,12 +59,12 @@ class LottoTest {

private static List<Arguments> LottoMatchParameterProvider() {
return List.of(
Arguments.of(Arrays.asList(1, 3, 5, 7, 9, 11), LottoResult.THREE),
Arguments.of(Arrays.asList(1, 2, 3, 4, 9, 11), LottoResult.FOUR),
Arguments.of(Arrays.asList(1, 2, 3, 4, 5, 11), LottoResult.FIVE),
Arguments.of(Arrays.asList(1, 2, 3, 4, 5, 7), LottoResult.BONUS),
Arguments.of(Arrays.asList(1, 2, 3, 4, 5, 6), LottoResult.SIX),
Arguments.of(Arrays.asList(6, 7, 8, 9, 10, 11), LottoResult.FAIL)
Arguments.of(new Integer[]{1, 3, 5, 7, 9, 11}, LottoResult.THREE),
Copy link

Choose a reason for hiding this comment

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

ν•˜λ‹¨κ³Ό 같이 μ£Όμƒμ„±μžλ₯Ό μ΄μš©ν•˜κ²Œ ν•΄λ³Όμˆ˜λ„ μžˆμ„κ²ƒ κ°™μŠ΅λ‹ˆλ‹€!

Suggested change
Arguments.of(new Integer[]{1, 3, 5, 7, 9, 11}, LottoResult.THREE),
Arguments.of(Set.of(1, 3, 5, 7, 9, 11)}, LottoResult.THREE)

Arguments.of(new Integer[]{1, 2, 3, 4, 9, 11}, LottoResult.FOUR),
Arguments.of(new Integer[]{1, 2, 3, 4, 5, 11}, LottoResult.FIVE),
Arguments.of(new Integer[]{1, 2, 3, 4, 5, 7}, LottoResult.BONUS),
Arguments.of(new Integer[]{1, 2, 3, 4, 5, 6}, LottoResult.SIX),
Arguments.of(new Integer[]{6, 7, 8, 9, 10, 11}, LottoResult.FAIL)
);
}

Expand Down
Loading