-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step4 - 로또(수동) #4234
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
mins1031
wants to merge
4
commits into
next-step:mins1031
Choose a base branch
from
mins1031:step4
base: mins1031
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 - 로또(수동) #4234
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions
22
src/main/java/lottogame/controller/LottoPurchaseRequest.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,22 @@ | ||
| package lottogame.controller; | ||
|
|
||
| import java.util.Set; | ||
| import lottogame.model.lotto.ManualLottos; | ||
| import lottogame.model.price.LottoPurchasePrice; | ||
| import lottogame.model.winner.WinnerResult; | ||
|
|
||
| public record LottoPurchaseRequest( | ||
| LottoPurchasePrice lottoPurchasePrice, | ||
| ManualLottos manualLottos | ||
| ) { | ||
| public int calculateAutoLottoCount(int perLottoPrice) { | ||
| int totalLottoCount = this.lottoPurchasePrice.calculateLottoCount(perLottoPrice); | ||
| int manualLottoCount = this.manualLottos.size(); | ||
|
|
||
| return totalLottoCount - manualLottoCount; | ||
| } | ||
|
|
||
| public double getRateOfReturn(WinnerResult winnerResult) { | ||
| return this.lottoPurchasePrice.calculateRateOfReturn(winnerResult.sumTotalWinReturn()); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,34 +1,55 @@ | ||
| package lottogame.model.lotto; | ||
|
|
||
| import static java.util.stream.IntStream.range; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| public class LottoNum implements Comparable<LottoNum>{ | ||
| public class LottoNum implements Comparable<LottoNum> { | ||
|
|
||
| public static final int MIN_NUM = 1; | ||
| public static final int MAX_NUM = 45; | ||
|
|
||
| private static final Map<Integer, LottoNum> CACHE = new HashMap<>(); | ||
|
|
||
| private final int num; | ||
|
|
||
| public LottoNum(int num) { | ||
| static { | ||
| range(MIN_NUM, MAX_NUM + 1).forEach( | ||
| num -> CACHE.put(num, new LottoNum(num)) | ||
| ); | ||
| } | ||
|
|
||
| private LottoNum(int num) { | ||
| if (isOutOfLottoRange(num)) { | ||
| throw new IllegalArgumentException("1부터 45사이의 숫자만 입력해주세요."); | ||
| } | ||
|
|
||
| this.num = num; | ||
| } | ||
|
|
||
| private boolean isOutOfLottoRange(int num) { | ||
| return num < MIN_NUM || num > MAX_NUM; | ||
| public static LottoNum getInstance(int num) { | ||
|
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. effective java 책을 보면 정적 팩터리 메서드 이름에 대한 컨벤션 제안을 하고 있다. |
||
| if (isOutOfLottoRange(num)) { | ||
| throw new IllegalArgumentException("1부터 45사이의 숫자만 입력해주세요."); | ||
| } | ||
|
|
||
| return CACHE.get(num); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(LottoNum o) { | ||
| return Integer.compare(this.num, o.num); | ||
| private static boolean isOutOfLottoRange(int num) { | ||
| return num < MIN_NUM || num > MAX_NUM; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return String.valueOf(num); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(LottoNum o) { | ||
| return Integer.compare(num, o.num); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) { | ||
|
|
||
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,35 @@ | ||||||
| package lottogame.model.lotto; | ||||||
|
|
||||||
| import java.util.ArrayList; | ||||||
| import java.util.List; | ||||||
| import java.util.Set; | ||||||
|
|
||||||
| public class ManualLottos { | ||||||
| private final List<Set<Integer>> manualLottos; | ||||||
|
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.
Suggested change
Lottos와 다른 점이 있나? |
||||||
|
|
||||||
| public ManualLottos(List<Set<Integer>> ManualLottos) { | ||||||
| this.manualLottos = ManualLottos; | ||||||
| } | ||||||
|
|
||||||
| public int size() { | ||||||
| return manualLottos.size(); | ||||||
| } | ||||||
|
|
||||||
| public Set<Integer> findManualLottoByIdx(int idx) { | ||||||
| if (idx < 0 || idx >= this.manualLottos.size()) { | ||||||
| return Set.of(); | ||||||
| } | ||||||
|
|
||||||
| return manualLottos.get(idx); | ||||||
| } | ||||||
|
|
||||||
| public Lottos convertToLottos() { | ||||||
| List<Lotto> tempLottos = new ArrayList<>(size()); | ||||||
| for (int i = 0; i < size(); i++) { | ||||||
| Set<LottoNum> lottoNums = LottoMachine.createLottoNums(findManualLottoByIdx(i)); | ||||||
| tempLottos.add(new Lotto(lottoNums)); | ||||||
| } | ||||||
|
|
||||||
| return new Lottos(tempLottos); | ||||||
| } | ||||||
| } | ||||||
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
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.
이 객체의 책임 중 로또를 생성하는 책임을 다음과 같은 인터페이스를 추가한 후 분리해 보는 도전을 해보면 어떨까?
코드를 보면 로또를 생성하는 부분이 여러 객체에 분산되어 있는 느낌이 든다.
인터페이스를 추가하고 자동/수동에 대한 구현체를 만들어 로또 생성 부분을 합쳐보면 어떨까?
3단계에서 4단계로 요구사항이 변경될 때 로또를 생성하는 부분의 요구사항만 변경됐다.
로또를 생성하는 부분을 다음과 같은 구조의 인터페이스로 분리해 보는 연습을 해보면 어떨까?
이와 같이 인터페이스로 구현했을 때의 잇점에 대해 고민해 보는 시간을 가져본다.