-
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 all 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,27 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class AutoLottoGenerator implements LottoGenerator { | ||
| private final Money money; | ||
|
|
||
| public AutoLottoGenerator(int money) { | ||
| this(new Money(money)); | ||
| } | ||
|
|
||
| public AutoLottoGenerator(Money money) { | ||
| this.money = money; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Lotto> generate() { | ||
| List<Lotto> lottoArray = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < money.getBuyableCount(); i++) { | ||
| lottoArray.add(new Lotto(LottoMachine.createLottoNumbers())); | ||
| } | ||
|
|
||
| return lottoArray; | ||
| } | ||
| } | ||
| 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,42 @@ public Lotto(String... numbers) { | |
| this(stringToList(numbers)); | ||
| } | ||
|
|
||
| public Lotto(List<LottoNumber> numbers) { | ||
| public Lotto(String numbers) { | ||
| this(stringToList(parse(numbers))); | ||
| } | ||
|
|
||
| private static String[] parse(String s) { | ||
| String[] strs = s.split(","); | ||
|
|
||
| for (int i = 0; i < strs.length; i++) { | ||
| strs[i] = strs[i].trim(); | ||
| } | ||
|
|
||
| return strs; | ||
| } | ||
|
|
||
| public Lotto(Set<LottoNumber> numbers) { | ||
| System.out.println(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,31 @@ | ||
| 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 deductManualLottoCost() { | ||
| return this.money.deductManualLottoCost(lottoCount.getManualCount()); | ||
| } | ||
|
|
||
| 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,26 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class LottosBundleGenerator implements LottoGenerator{ | ||
| private final LottoPurChase lottoPurChase; | ||
| private final List<String> manualLottoText; | ||
|
|
||
| public LottosBundleGenerator(LottoPurChase lottoPurChase, List<String> manualLottoText) { | ||
| this.lottoPurChase = lottoPurChase; | ||
| this.manualLottoText = manualLottoText; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Lotto> generate() { | ||
| // ManualLottosGenerator 활용해 수동 로또 생성 | ||
| List<Lotto> manualLottos = new ManualLottosGenerator(manualLottoText).generate(); | ||
|
|
||
| // AutoLottosGenerator 활용해 자동 로또 생성(수동 로또 수 만큼 Money 차감) | ||
| List<Lotto> autoLottos = new AutoLottoGenerator(lottoPurChase.deductManualLottoCost()).generate(); | ||
|
|
||
| return Stream.concat(manualLottos.stream(), autoLottos.stream()).collect(Collectors.toList()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ManualLottosGenerator implements LottoGenerator { | ||
| private final List<String> manualLottos; | ||
|
|
||
| public ManualLottosGenerator(List<String> manualLottos) { | ||
| this.manualLottos = manualLottos; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Lotto> generate() { | ||
| return manualLottos.stream() | ||
| .map(Lotto::new) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } |
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.
👍