-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step4 리뷰요청드립니다. #4237
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
Open
qwer920414-ctrl
wants to merge
8
commits into
next-step:qwer920414-ctrl
Choose a base branch
from
qwer920414-ctrl:step4
base: qwer920414-ctrl
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Step4 리뷰요청드립니다. #4237
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d92adbc
refactor: 불필요 LottoGame 필드 삭제
qwer920414-ctrl 5c1af36
feat: Money 클래스 역할 분담을 위한 LottoPurchase - LottoCount 클래스 생성
qwer920414-ctrl 14a0f17
refactor: LottoGame 로직 수정
qwer920414-ctrl 4632815
fix: Rank 로직 수정(2,3위 카운팅 이슈)
qwer920414-ctrl bafdc5c
refactor: LottoPurchase 개선
qwer920414-ctrl 34b962c
refactor: Main, InputView 로직 추가
qwer920414-ctrl 2b7cd46
refactor: Rank from() 로직 수정(stream 재적용)
qwer920414-ctrl ab91c78
refactor: LottoGenerator 도입 - LottoPurchase 번호 생성 로직 위임
qwer920414-ctrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.domain.LottoGame; | ||
| import lotto.domain.LottoResult; | ||
| import lotto.domain.Money; | ||
| import lotto.domain.WinningLotto; | ||
| import lotto.domain.*; | ||
| import lotto.view.InputView; | ||
| import lotto.view.ResultView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| long price = InputView.initLottoPrice(); | ||
| LottoGame lottoGame = new LottoGame(price); | ||
| Money money = new Money(InputView.initLottoPrice()); | ||
| int manualCount = InputView.initManualCount(); | ||
| List<String> manualList = InputView.initManualList(manualCount); | ||
|
|
||
| LottoPurchase lottoPurchase = new LottoPurchase(money, manualList); | ||
|
|
||
| LottoGame lottoGame = new LottoGame(lottoPurchase); | ||
| ResultView.printLottos(lottoGame); | ||
|
|
||
| WinningLotto winningLotto = new WinningLotto(InputView.initWinningLotto(), InputView.initBonusNumber()); | ||
| LottoResult result = lottoGame.findWinner(winningLotto); | ||
| ResultView.printResult(result, new Money(price)); | ||
| ResultView.printResult(result, money); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package lotto.domain; | ||
|
|
||
| public class AutoLottoGenerator implements LottoGenerator { | ||
|
|
||
| private final LottoCount lottoCount; | ||
|
|
||
| public AutoLottoGenerator(LottoCount lottoCount) { | ||
| this.lottoCount = lottoCount; | ||
| } | ||
|
|
||
| @Override | ||
| public Lottos generate() { | ||
| return new Lottos(lottoCount.value()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class CompositeLottoGenerator implements LottoGenerator { | ||
|
|
||
| private final List<LottoGenerator> generators; | ||
|
|
||
| public CompositeLottoGenerator(List<LottoGenerator> generators) { | ||
| this.generators = generators; | ||
| } | ||
|
|
||
| @Override | ||
| public Lottos generate() { | ||
| return generators.stream() | ||
| .map(LottoGenerator::generate) | ||
| .reduce(Lottos::merge) | ||
| .orElseGet(Lottos::empty); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package lotto.domain; | ||
|
|
||
| public class LottoCount { | ||
| private final int value; | ||
|
|
||
| public LottoCount(int value) { | ||
| validate(value); | ||
| this.value = value; | ||
| } | ||
|
|
||
| private static void validate(int value) { | ||
| if (value < 0) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public int value() { | ||
| return value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package lotto.domain; | ||
|
|
||
| public interface LottoGenerator { | ||
| Lottos generate(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class LottoPurchase { | ||
|
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. 이 객체가 수동/자동 로또 생성을 담당하고 있다. 3단계에서 4단계로 요구사항이 변경될 때 로또를 생성하는 부분의 요구사항만 변경됐다. |
||
| private static final long LOTTO_PRICE = 1_000; | ||
|
|
||
| private final LottoCount count; | ||
| private final List<Lotto> manualLottos; | ||
|
|
||
| public LottoPurchase(Money money) { | ||
| this(new LottoCount(calculateCount(money))); | ||
| } | ||
|
|
||
| public LottoPurchase(LottoCount count) { | ||
| this(count, List.of()); | ||
| } | ||
|
|
||
| public LottoPurchase(Money money, List<String> manualList) { | ||
| this(new LottoCount(calculateCount(money)), convert(manualList)); | ||
| } | ||
|
|
||
| public LottoPurchase(LottoCount count, List<Lotto> manualLottos) { | ||
| this.count = count; | ||
| this.manualLottos = manualLottos; | ||
| } | ||
|
|
||
| public Lottos purchase() { | ||
| LottoGenerator compositeGenerator = new CompositeLottoGenerator( | ||
| List.of(new ManualLottoGenerator(manualLottos), new AutoLottoGenerator(new LottoCount(autoCount())))); | ||
|
|
||
| return compositeGenerator.generate(); | ||
| } | ||
|
|
||
| public static List<Lotto> convert(List<String> manualList) { | ||
| return manualList.stream() | ||
| .map(Lotto::new) | ||
| .collect(Collectors.toUnmodifiableList()); | ||
| } | ||
|
|
||
| private static int calculateCount(Money money) { | ||
| return (int)(money.getMoney() / LOTTO_PRICE); | ||
| } | ||
|
|
||
| public int count() { | ||
| return count.value(); | ||
| } | ||
|
|
||
| public int autoCount() { | ||
| return count.value() - manualLottos.size(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class ManualLottoGenerator implements LottoGenerator { | ||
|
|
||
| private final List<Lotto> manualList; | ||
|
|
||
| public ManualLottoGenerator(List<Lotto> manualList) { | ||
| this.manualList = manualList; | ||
| } | ||
|
|
||
| @Override | ||
| public Lottos generate() { | ||
| return new Lottos(manualList); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package lotto.domain; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class AutoLottoGeneratorTest { | ||
|
|
||
| @Test | ||
| void auto_generate_size() { | ||
| AutoLottoGenerator auto = new AutoLottoGenerator(new LottoCount(3)); | ||
| Lottos generate = auto.generate(); | ||
| Assertions.assertThat(generate.size()).isEqualTo(3); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/test/java/lotto/domain/CompositeLottoGeneratorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package lotto.domain; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class CompositeLottoGeneratorTest { | ||
|
|
||
| @Test | ||
| void merge() { | ||
| List<Lotto> manualList = List.of( | ||
| new Lotto(1, 2, 3, 4, 5, 6) | ||
| , new Lotto(2, 3, 4, 5, 6, 7) | ||
| ); | ||
| LottoGenerator manual = new ManualLottoGenerator(manualList); | ||
| LottoGenerator auto = new AutoLottoGenerator(new LottoCount(2)); | ||
|
|
||
| LottoGenerator composite = new CompositeLottoGenerator( | ||
| List.of(manual, auto) | ||
| ); | ||
|
|
||
| Lottos merge = composite.generate(); | ||
| assertThat(merge.size()).isEqualTo(4); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package lotto.domain; | ||
|
|
||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class LottoPurchaseTest { | ||
|
|
||
| @Test | ||
| @DisplayName("정해진 개수로 구매할 수 있다") | ||
| void count() { | ||
| LottoPurchase purchase = new LottoPurchase(new LottoCount(3)); | ||
|
|
||
| assertThat(purchase.count()).isEqualTo(3); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Money로부터 구매 가능한 로또 개수를 계산한다") | ||
| void calculate() { | ||
| Money money = new Money(5000); | ||
|
|
||
| LottoPurchase purchase = new LottoPurchase(money); | ||
|
|
||
| assertThat(purchase.count()).isEqualTo(5); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("수동 생성한 리스트의 사이즈를 확인할 수 있다.") | ||
| void manualList_size() { | ||
| Money money = new Money(5000); | ||
| List<String> list = List.of("1,2,3,4,5,6", "2,3,4,5,6,7", "3,4,5,6,7,8"); | ||
|
|
||
| LottoPurchase lottoPurchase = new LottoPurchase(money, list); | ||
| Lottos lottos = lottoPurchase.purchase(); | ||
|
|
||
| assertThat(lottos.size()).isEqualTo(5); | ||
| assertThat(lottoPurchase.autoCount()).isEqualTo(2); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
두 개의 Lotto를 합치는 객체까지 추가 👍
LottoPurchase의 purchse 메서드 내부를 CompositeLottoGenerator 에서 아래와 같이 구현해도 되지 않을까?
이와 같이 인터페이스를 구현하고, 구현체가 서로 연결되어 있는 경우 아래와 같은 구현체를 통해 구현체를 통합할 수 있음.
보통 디자인 패턴에서 컴포지트 패턴으로 알려져 있음.
아래와 같은 객체 추가에 따른 효과를 느껴 봤으면 하는 바람으로 피드백 남겨봄.