-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 #3768
base: boy0516
Are you sure you want to change the base?
Step4 #3768
Changes from 14 commits
c7eb2b2
6ba48a0
bb73a39
5222306
fb92f92
a94b228
2c6954d
27d8ab8
2554f16
f74f912
960df5d
c4d7025
1cf832e
dac372e
e550f17
ae86d04
319c518
e56d638
89f197c
11a38da
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -11,22 +11,43 @@ public void simulateLotto() { | |||||||
} | ||||||||
|
||||||||
private Statistics getStatistics() { | ||||||||
return getLottos().computeStatistic(getWinLotto()); | ||||||||
return getLottos(getBudget()).computeStatistic(getWinLotto()); | ||||||||
} | ||||||||
|
||||||||
private WinLotto getWinLotto() { | ||||||||
return InputView.inputLastWinLotto(); | ||||||||
return InputView.retryableInputLastWinLotto(); | ||||||||
} | ||||||||
|
||||||||
private Lottos getLottos() { | ||||||||
Lottos lottos = new Lottos(getBudget()); | ||||||||
ResultView.showGeneratedLottos(lottos); | ||||||||
private Lottos getLottos(Budget budget) { | ||||||||
SelfIssueCount count = getManualIssueCount(); | ||||||||
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.
|
||||||||
|
||||||||
budget = budget.spend(Price.LOTTO, count); | ||||||||
|
||||||||
Lottos manualLottos = getManualIssuedLottos(count); | ||||||||
Lottos autoLottos = getAutoIssuedLottos(budget); | ||||||||
|
||||||||
ResultView.showLottoQuantity(manualLottos, autoLottos); | ||||||||
|
||||||||
Lottos allLottos = manualLottos.combine(autoLottos); | ||||||||
ResultView.showGeneratedLottos(allLottos); | ||||||||
|
||||||||
return allLottos; | ||||||||
} | ||||||||
|
||||||||
private SelfIssueCount getManualIssueCount() { | ||||||||
return InputView.retryableInputSelfIssueCount(); | ||||||||
} | ||||||||
|
||||||||
private Lottos getManualIssuedLottos(SelfIssueCount count) { | ||||||||
return InputView.inputSelfIssueLottos(count); | ||||||||
} | ||||||||
private Lottos getAutoIssuedLottos(Budget budget) { | ||||||||
Lottos lottos = new Lottos(budget); | ||||||||
return lottos; | ||||||||
Comment on lines
+45
to
46
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
불필요한 변수 선언은 하지 않는게 복잡도를 덜어낼수 있을것 같아요~ |
||||||||
} | ||||||||
|
||||||||
private Budget getBudget() { | ||||||||
Budget budget = InputView.inputBuyBudget(); | ||||||||
ResultView.showLottoQuantity(budget.divide(Lotto.LOTTO_PRICE)); | ||||||||
Budget budget = InputView.retryableInputBuyBudget(); | ||||||||
return budget; | ||||||||
} | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,18 +1,32 @@ | ||||||
package lotto.domain; | ||||||
|
||||||
public class Budget { | ||||||
public class Budget{ | ||||||
|
||||||
private int value; | ||||||
private int budget; | ||||||
|
||||||
public Budget(int budget) { | ||||||
this.value = budget; | ||||||
validMinValue(budget); | ||||||
this.budget = budget; | ||||||
} | ||||||
|
||||||
public int getValue() { | ||||||
return value; | ||||||
private void validMinValue(int budget) { | ||||||
if(budget % 1000 != 0) | ||||||
throw new IllegalArgumentException("100원 단위는 입력할 수 없습니다: " + budget); | ||||||
} | ||||||
|
||||||
public int divide(int lottoPrice) { | ||||||
return value / lottoPrice; | ||||||
public int purchasableQuantity(Price price) { | ||||||
return price.divide(budget); | ||||||
} | ||||||
|
||||||
public boolean isEnoughToPay(Price price) { | ||||||
return budget > price.getValue(); | ||||||
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
값을 꺼내기 보다는 상태를 가진 객체에게 물어볼수 있지 않을까요? |
||||||
} | ||||||
|
||||||
public Budget spend(Price price) { | ||||||
return new Budget(budget - price.getValue()); | ||||||
} | ||||||
|
||||||
public Budget spend(Price price, SelfIssueCount issueCount) { | ||||||
return new Budget(budget - price.multiple(issueCount)); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ public Lotto() { | |
|
||
public Lotto(List<LottoNumber> lotto) { | ||
validEmpty(lotto); | ||
validLottoSize(lotto); | ||
this.lotto = lotto; | ||
} | ||
|
||
|
@@ -29,6 +30,12 @@ private static void validEmpty(List<?> lotto) { | |
} | ||
} | ||
|
||
private static void validLottoSize(List<?> lotto) { | ||
if(lotto.size() != 6) { | ||
throw new IllegalArgumentException("유효한 Lotto size가 아닙니다: " + lotto.size()); | ||
} | ||
} | ||
|
||
public List<LottoNumber> getLotto() { | ||
return lotto; | ||
} | ||
|
@@ -39,8 +46,12 @@ public int size() { | |
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Lotto lotto1 = (Lotto) o; | ||
return Objects.equals(lotto, lotto1.lotto); | ||
} | ||
|
@@ -49,4 +60,8 @@ public boolean equals(Object o) { | |
public int hashCode() { | ||
return Objects.hash(lotto); | ||
} | ||
|
||
protected boolean isContainBonus(LottoNumber bonus) { | ||
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. equals, hashCode 보다는 위에 있는게 일반적인것 같습니다. 🤔 |
||
return lotto.contains(bonus); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,8 @@ | |||||
import java.util.List; | ||||||
import java.util.stream.Collectors; | ||||||
|
||||||
import static lotto.domain.Price.LOTTO; | ||||||
|
||||||
public class Lottos { | ||||||
|
||||||
private List<Lotto> lottos; | ||||||
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
재할당을 하지말라고 final을 붙여주는건 어떨까요? |
||||||
|
@@ -12,15 +14,21 @@ public Lottos(Budget budget) { | |||||
this(generateLottosBy(budget)); | ||||||
} | ||||||
|
||||||
public Lottos(Lotto lotto) { | ||||||
this(List.of(lotto)); | ||||||
} | ||||||
|
||||||
public Lottos(List<Lotto> lottos) { | ||||||
this.lottos = lottos; | ||||||
} | ||||||
|
||||||
private static List<Lotto> generateLottosBy(Budget budget) { | ||||||
List<Lotto> lottos = new ArrayList<>(); | ||||||
|
||||||
for (int i = 0; i < budget.divide(Lotto.LOTTO_PRICE); i++) { | ||||||
if (budget.isEnoughToPay(LOTTO)) { | ||||||
lottos.addAll(generateLottosBy(budget.spend(LOTTO))); | ||||||
lottos.add(new Lotto()); | ||||||
return lottos; | ||||||
} | ||||||
return lottos; | ||||||
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.
리턴해준 상태를 수정 불가로 해주면 어떨까요? |
||||||
} | ||||||
|
@@ -29,6 +37,10 @@ public List<Lotto> getLottos() { | |||||
return lottos; | ||||||
} | ||||||
|
||||||
public int getLottoSize() { | ||||||
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
어쩌면 우리는 무의식적으로 |
||||||
return lottos.size(); | ||||||
} | ||||||
|
||||||
public Statistics computeStatistic(WinLotto winLotto) { | ||||||
return new Statistics(checkStatisticNumberMatch(winLotto)); | ||||||
} | ||||||
|
@@ -45,4 +57,11 @@ private static Statistic getStatistic(WinLotto winLotto, Lotto lotto) { | |||||
winLotto.isContainBonus(lotto) | ||||||
); | ||||||
} | ||||||
|
||||||
public Lottos combine(Lottos lottos) { | ||||||
List<Lotto> newLottos = new ArrayList<>(this.lottos); | ||||||
List<Lotto> combineLottos = lottos.getLottos(); | ||||||
newLottos.addAll(combineLottos); | ||||||
return new Lottos(newLottos); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package lotto.domain; | ||
|
||
public enum Price { | ||
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. enum을 사용한 이유가 궁금해요 🤔 |
||
|
||
LOTTO(1000); | ||
|
||
private final int value; | ||
|
||
Price(int value) { | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
public int multiple(SelfIssueCount count) { | ||
return count.getCount() * value; | ||
} | ||
|
||
public int divide(int budget) { | ||
return budget / value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package lotto.domain; | ||
|
||
public class SelfIssueCount implements Comparable<SelfIssueCount>{ | ||
|
||
private int count; | ||
|
||
public SelfIssueCount(int count) { | ||
validPositiveValue(count); | ||
this.count = count; | ||
} | ||
|
||
private void validPositiveValue(int count) { | ||
if (count < 0) | ||
throw new IllegalArgumentException("양수 값만 입력할 수 있습니다: " + count); | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
|
||
public boolean isPositive() { | ||
return count > 0; | ||
} | ||
|
||
public SelfIssueCount decrease() { | ||
return new SelfIssueCount(count - 1); | ||
} | ||
|
||
@Override | ||
public int compareTo(SelfIssueCount that) { | ||
return this.count - that.count; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,63 +4,46 @@ | |
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class WinLotto { | ||
public class WinLotto extends Lotto { | ||
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.
[OOP] 코드의 재사용, 상속(Inheritance)보다 합성(Composition)을 사용해야 하는 이유 |
||
|
||
private List<LottoNumber> winLotto; | ||
private LottoNumber bonus; | ||
|
||
public WinLotto(List<LottoNumber> winLotto, LottoNumber bonus) { | ||
validEmpty(winLotto); | ||
this.winLotto = winLotto; | ||
this.bonus = bonus; | ||
public WinLotto(List<Integer> lottoNumbers, Integer bonus) { | ||
this(lottoNumbers.stream().map(LottoNumber::new).collect(Collectors.toList())); | ||
this.bonus = new LottoNumber(bonus); | ||
} | ||
|
||
public static WinLotto ofNumbers(List<Integer> lottoNumbers, int bonus) { | ||
validEmpty(lottoNumbers); | ||
return new WinLotto(createLottoNumber(lottoNumbers), createLottoNumber(bonus)); | ||
} | ||
|
||
private static List<LottoNumber> createLottoNumber(List<Integer> lottoNumbers) { | ||
return lottoNumbers | ||
.stream() | ||
.map(LottoNumber::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static LottoNumber createLottoNumber(int bonus) { | ||
return new LottoNumber(bonus); | ||
} | ||
|
||
private static void validEmpty(List<?> lotto) { | ||
if (lotto == null || lotto.isEmpty()) { | ||
throw new IllegalArgumentException("입력값이 없습니다"); | ||
} | ||
} | ||
|
||
public int size() { | ||
return winLotto.size(); | ||
private WinLotto(List<LottoNumber> lottoNumbers) { | ||
super(lottoNumbers); | ||
} | ||
|
||
public int getFeatNumberCount(Lotto lotto) { | ||
return (int) lotto.getLotto().stream() | ||
.filter(value -> this.winLotto.contains(value)) | ||
.count(); | ||
.filter(value -> super.getLotto().contains(value)) | ||
.count(); | ||
} | ||
|
||
public boolean isContainBonus(Lotto lotto) { | ||
return lotto.getLotto().contains(bonus); | ||
return lotto.isContainBonus(bonus); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
WinLotto lotto1 = (WinLotto) o; | ||
return Objects.equals(winLotto, lotto1.winLotto); | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
if (!super.equals(o)) { | ||
return false; | ||
} | ||
WinLotto winLotto = (WinLotto) o; | ||
return Objects.equals(bonus, winLotto.bonus); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(winLotto); | ||
return Objects.hash(super.hashCode(), bonus); | ||
} | ||
} |
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.
결제 금액보다 많이 살수 없지 않을까요~?