-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Step3 리뷰요청드립니다 #4230
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
Step3 리뷰요청드립니다 #4230
Changes from all commits
44aff33
8ea7835
03abc72
e03eafc
e913f9d
234d0e7
c6846fc
d2b7dd3
da1c943
1485e32
04820e9
8f4643c
84a3030
121b387
0076168
6981cba
19e4582
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,20 +1,20 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.domain.Lotto; | ||
| import lotto.domain.LottoGame; | ||
| import lotto.domain.LottoResult; | ||
| import lotto.domain.Money; | ||
| import lotto.domain.WinningLotto; | ||
| import lotto.view.InputView; | ||
| import lotto.view.ResultView; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| long price = InputView.initLottoPrice(); | ||
| LottoGame lottoGame = new LottoGame(price); | ||
| ResultView.printLottos(lottoGame.lottos()); | ||
|
|
||
| Lotto winningLotto = new Lotto(InputView.initWinningLotto()); | ||
| LottoResult result = lottoGame.check(winningLotto); | ||
| ResultView.printResult(result, lottoGame); | ||
| ResultView.printLottos(lottoGame); | ||
|
|
||
| WinningLotto winningLotto = new WinningLotto(InputView.initWinningLotto(), InputView.initBonusNumber()); | ||
| LottoResult result = lottoGame.findWinner(winningLotto); | ||
| ResultView.printResult(result, new Money(price)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package lotto.domain; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class LottoNumber { | ||
|
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. 원시값 포장 👍 상황
위 요구사항을 서버에서 생성되는 LottoNumber의 인스턴스의 갯수는? 동시에 생성되는 인스턴스 갯수가 너무 많다. 힌트 : Map과 같은 곳에 인스턴스를 생성한 후 재사용하는 방법을 찾아본다. |
||
| public static final int MIN_NUMBER = 1; | ||
| public static final int MAX_NUMBER = 45; | ||
|
|
||
| private static final Map<Integer, LottoNumber> CACHE = new HashMap<>(); | ||
|
|
||
| static { | ||
| IntStream.rangeClosed(MIN_NUMBER, MAX_NUMBER) | ||
| .forEach(i -> CACHE.put(i, new LottoNumber(i))); | ||
| } | ||
|
Comment on lines
+12
to
+17
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. 캐싱 적용 👍 |
||
|
|
||
| private final int lottoNumber; | ||
|
|
||
| private LottoNumber(int lottoNumber) { | ||
|
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. private 으로 구현 👍 |
||
| this.lottoNumber = lottoNumber; | ||
| } | ||
|
|
||
| public static LottoNumber from(int lottoNumber) { | ||
| validate(lottoNumber); | ||
| return CACHE.get(lottoNumber); | ||
| } | ||
|
|
||
| private static void validate(int lottoNumber) { | ||
| if (lottoNumber < MIN_NUMBER || lottoNumber > MAX_NUMBER) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object object) { | ||
| if (this == object) return true; | ||
| if (object == null || getClass() != object.getClass()) return false; | ||
| LottoNumber that = (LottoNumber) object; | ||
| return lottoNumber == that.lottoNumber; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(lottoNumber); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.valueOf(lottoNumber); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package lotto.domain; | ||
|
|
||
| public class WinningLotto { | ||
|
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. 새로운 객체로 분리 👍 |
||
| private final Lotto winningLotto; | ||
| private final LottoNumber bonusNumber; | ||
|
|
||
| public WinningLotto(String lotto, int bonusNumber) { | ||
| this(new Lotto(lotto), bonusNumber); | ||
| } | ||
|
|
||
| public WinningLotto(Lotto lotto, int bonusNumber) { | ||
|
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. 주 생성자는 생성자의 마지막에 구현하는 것이 관례 |
||
| this(lotto, LottoNumber.from(bonusNumber)); | ||
| } | ||
|
|
||
| public WinningLotto(Lotto lotto, LottoNumber bonusNumber) { | ||
| validateBonusNumber(lotto, bonusNumber); | ||
| this.winningLotto = lotto; | ||
| this.bonusNumber = bonusNumber; | ||
| } | ||
|
|
||
| private void validateBonusNumber(Lotto lotto, LottoNumber bonusNumber) { | ||
| if (lotto.contains(bonusNumber)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public Rank match(Lotto lotto) { | ||
| return Rank.from(winningLotto.match(lotto), lotto.contains(bonusNumber)); | ||
| } | ||
| } | ||
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.
기존 생성자를 유지함으로써 외부 코드엥 영향을 미치지 않도록 리팩터링한 점 👍