-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step4 - 로또(수동) #4231
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
Step4 - 로또(수동) #4231
Changes from 6 commits
072cd19
ffb12e3
9744613
490fe65
dd72b07
3f9d019
f8de414
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||||||||
| package lotto.domain; | ||||||||||||||||
|
|
||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||
| import java.util.List; | ||||||||||||||||
|
|
||||||||||||||||
| public class AutoLottoGenerator implements LottoGenerator { | ||||||||||||||||
| private final Money money; | ||||||||||||||||
|
|
||||||||||||||||
| public AutoLottoGenerator(int number) { | ||||||||||||||||
| this(new Money(number)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public AutoLottoGenerator(Money money) { | ||||||||||||||||
| this.money = money; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| public List<Lotto> generate() { | ||||||||||||||||
| int cnt = money.getBuyableCount(); | ||||||||||||||||
|
|
||||||||||||||||
| List<Lotto> lottoArray = new ArrayList<>(); | ||||||||||||||||
|
|
||||||||||||||||
| for (int i = 0; i < cnt; i++) { | ||||||||||||||||
|
||||||||||||||||
| int cnt = money.getBuyableCount(); | |
| List<Lotto> lottoArray = new ArrayList<>(); | |
| for (int i = 0; i < cnt; i++) { | |
| List<Lotto> lottoArray = new ArrayList<>(); | |
| for (int i = 0; i < money.getBuyableCount(); i++) { |
저는 굳이 불필요한 로컬 변수 사용하지 않는 방식을 선호함
한번 고려해 보면 어떨까?
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,10 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Lotto { | ||
| private final List<LottoNumber> numbers; | ||
| private final Set<LottoNumber> numbers; | ||
|
Contributor
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. 👍 |
||
|
|
||
| public Lotto(int... numbers) { | ||
| this(intToList(numbers)); | ||
|
|
@@ -15,20 +14,31 @@ public Lotto(String... numbers) { | |
| this(stringToList(numbers)); | ||
| } | ||
|
|
||
| public Lotto(List<LottoNumber> numbers) { | ||
| public Lotto(String numbers) { | ||
| this(stringToList(numbers.split(","))); | ||
| } | ||
|
|
||
| public Lotto(Set<LottoNumber> numbers) { | ||
| validation(numbers); | ||
| this.numbers = numbers; | ||
| } | ||
|
|
||
| private static List<LottoNumber> stringToList(String... numbers) { | ||
| private void validation(Set<LottoNumber> numbers) { | ||
| if (numbers.size() != 6) { | ||
| throw new IllegalArgumentException("로또 번호는 6개여야 합니다."); | ||
| } | ||
| } | ||
|
|
||
| private static Set<LottoNumber> stringToList(String... numbers) { | ||
| return Arrays.stream(numbers) | ||
| .map(LottoNumber::valueOf) | ||
| .toList(); | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| private static List<LottoNumber> intToList(int... numbers) { | ||
| private static Set<LottoNumber> intToList(int... numbers) { | ||
| return Arrays.stream(numbers) | ||
| .mapToObj(LottoNumber::valueOf) | ||
| .toList(); | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| public int countMatchedNumbers(Lotto winningLotto) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package lotto.domain; | ||
|
|
||
| public class LottoCount { | ||
| private final int manualCount; | ||
| private final int autoCount; | ||
|
|
||
| public LottoCount(Money money, int manualCount) { | ||
| this(money.getBuyableCount() - manualCount, manualCount); | ||
| } | ||
|
|
||
| public LottoCount(int autoCount, int manualCount) { | ||
| this.autoCount = autoCount; | ||
| this.manualCount = manualCount; | ||
| } | ||
|
|
||
| public int getManualCount() { | ||
| return this.manualCount; | ||
| } | ||
|
|
||
| public int getAutoCount() { | ||
| return this.autoCount; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface LottoGenerator { | ||
| List<Lotto> generate(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,24 +6,13 @@ | |
| public class LottoGroup { | ||
|
Contributor
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. LottoGroup이 구매한 로또와 관련한 로직을 구현하는 책임과 로또 생성하는 책임을 가지는 것으로 보여진다. 3단계에서 4단계로 요구사항이 변경될 때 로또를 생성하는 부분의 요구사항만 변경됐다. |
||
| private final List<Lotto> lottos; | ||
|
|
||
| public LottoGroup(Money money) { | ||
| this(buyLotto(money)); | ||
| public LottoGroup(LottoGenerator lottoGenerator) { | ||
| this(lottoGenerator.generate()); | ||
| } | ||
|
|
||
| public LottoGroup(List<Lotto> lottos) { | ||
| this.lottos = lottos; | ||
| } | ||
| private static List<Lotto> buyLotto(Money money) { | ||
| int cnt = money.getBuyableCount(); | ||
|
|
||
| List<Lotto> lottoArray = new ArrayList<>(); | ||
|
|
||
| for(int i = 0; i < cnt; i++) { | ||
| lottoArray.add(new Lotto(LottoMachine.createLottoNumbers())); | ||
| } | ||
|
|
||
| return lottoArray; | ||
| } | ||
|
|
||
| public List<Lotto> getLottoNumbers() { | ||
| return lottos; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package lotto.domain; | ||
|
|
||
| public class LottoPurChase { | ||
| private final Money money; | ||
| private final LottoCount lottoCount; | ||
|
|
||
|
|
||
| public LottoPurChase(int money, int autoCount, int manualCount) { | ||
| this(new Money(money), new LottoCount(autoCount, manualCount)); | ||
| } | ||
|
|
||
| public LottoPurChase(Money money, LottoCount lottoCount) { | ||
| this.money = money; | ||
| this.lottoCount = lottoCount; | ||
| } | ||
|
|
||
| public Money getMoney() { | ||
| return this.money; | ||
| } | ||
|
|
||
| public int getAutoCount() { | ||
| return this.lottoCount.getAutoCount(); | ||
| } | ||
|
|
||
| public int getManualCount() { | ||
| return this.lottoCount.getManualCount(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class MixedLottoGenerator implements LottoGenerator { | ||
|
||
| private final LottoPurChase lottoPurchase; | ||
| private final List<Lotto> manualLottos; | ||
|
|
||
| public MixedLottoGenerator(LottoPurChase lottoPurchase, List<Lotto> manualLottos) { | ||
| this.lottoPurchase = lottoPurchase; | ||
| this.manualLottos = manualLottos; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Lotto> generate() { | ||
| List<Lotto> lottoArray = new ArrayList<>(manualLottos); | ||
|
|
||
| for (int i = 0; i < lottoPurchase.getAutoCount(); i++) { | ||
| lottoArray.add(new Lotto(LottoMachine.createLottoNumbers())); | ||
| } | ||
|
|
||
| return lottoArray; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package lotto.domain; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class LottoCountTest { | ||
|
|
||
| @Test | ||
| void 수동_자동_카운트해서_생성() { | ||
| LottoCount lottoCount = new LottoCount(11, 3); | ||
|
|
||
| Assertions.assertThat(lottoCount.getAutoCount()).isEqualTo(11); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package lotto.domain; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class LottoGeneratorTest { | ||
|
|
||
| @Test | ||
| @DisplayName("돈을 입력하여 자동으로 몇장 살 수 있는지") | ||
| void AutoGenerator() { | ||
| LottoGenerator autoLottoGenerator = new AutoLottoGenerator(14000); | ||
|
|
||
| Assertions.assertThat(autoLottoGenerator.generate()).hasSize(14); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("돈을 입력하여 자동과 수동으로 몇장 살 수 있는지") | ||
| void mixGenerator() { | ||
| LottoGenerator mixedLottoGenerator = new MixedLottoGenerator( | ||
| new LottoPurChase(3000, 2, 1), List.of(new Lotto(1,2,3,4,5,6)) | ||
| ); | ||
|
|
||
| Assertions.assertThat(mixedLottoGenerator.generate()).hasSize(3); | ||
| } | ||
| } |
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.
👍