-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: gkssk4163
Are you sure you want to change the base?
Changes from all commits
17f39bc
163f2e5
aacac3d
39fc959
ccf96e4
b0bc9f8
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 | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||
package lotto.domain; | ||||||
|
||||||
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; | ||||||
|
@@ -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))); | ||||||
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.
Suggested change
|
||||||
} | ||||||
|
||||||
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() { | ||||||
|
@@ -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(); | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
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. 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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,10 @@ public WinningLotto(Lotto winningLotto, LottoNumber bonus) { | |
|
||
private void validate() { | ||
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.
νλ¨κ³Ό κ°μ΄ μ²λ¦¬ν΄λ³Όμ μμ§ μμκΉμ? 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("보λμ€λ²νΈκ° λΉμ²¨λ²νΈμ μ΄λ―Έ ν¬ν¨λ λ²νΈμ λλ€."); | ||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
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. μꡬμ¬ν
ννΈ
μλ¨μ μꡬμ¬νμ λ§μΆ° μ
λ ₯λ¨κ³μμ μμΈκ° λ°μνμ§ μλλ‘ |
||
|
@@ -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())))); | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
|
||||||
|
@@ -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); | ||||||
|
||||||
|
@@ -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), | ||||||
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. νλ¨κ³Ό κ°μ΄ μ£Όμμ±μλ₯Ό μ΄μ©νκ² ν΄λ³Όμλ μμκ² κ°μ΅λλ€!
Suggested change
|
||||||
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) | ||||||
); | ||||||
} | ||||||
|
||||||
|
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.
LottoNumber μ λ³κ²½μ¬νμ΄ μμ΄ μ¬κΈ°μ λ¨κ²¨λμ΅λλ€.
LottoNumber μ κ²½μ° 1~45 μ μ«μμ΄κΈ° λλ¬Έμ λ§€λ² μΈμ€ν΄μ€λ₯Ό μμ±νμ§μκ³
μΊμ±μ²λ¦¬νμ¬ κ°μλ²νΈμ λν΄μ κ°μ μΈμ€ν΄μ€λ₯Ό μ¬μ©ν΄λ³Όμ μμκ² κ°μμ.
λ‘λ νΌλλ°± - αα ₯αΌαα ₯α¨ αα ’α¨αα ©α α ΅ αα ¦αα ©αα ³, αα ΅α«αα ³αα ₯α«αα ³ αα ’αα ΅αΌ
μ μ°Έκ³ νμ¬ μ μ©ν΄λ³΄λ©΄ μ’μκ² κ°μ΅λλ€.