-
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 all 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) { | ||||||||||
Count manualIssueCount = getManualIssueCount(budget); | ||||||||||
|
||||||||||
budget = budget.spend(Price.LOTTO, manualIssueCount); | ||||||||||
|
||||||||||
Lottos manualLottos = getManualIssuedLottos(manualIssueCount); | ||||||||||
Lottos autoLottos = getAutoIssuedLottos(budget); | ||||||||||
Comment on lines
+26
to
+27
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
표현하는게 한줄인데 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. 분리된 표현으로인해 복잡도가 올라갔고 그에 대해 전체적인 흐름의 그림이 잘 그려지지 않는다면, 저렇게 절차적으로 표현하고 나니, 메소드가 분리된다면, 여기가 좀 애매해지네요.
이후 로직은 result 객체로 코드간의 대화를 이어가면 어떨까요? 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. 위는 저의 관점일 뿐입니다.
최고의 정리방법은 구현하면서 느꼈던 점을 상기시키면서 다시 구현하는거라고 개인적으론 생각합니다 🤔 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. 다시 구현한다면 어떻게 하면 좋을까요? 로또 생성에 대한 정보는 순차적으로 입력받는데 어떻게 객체지향적으로 개발할지 잘 모르겠습니다. |
||||||||||
|
||||||||||
ResultView.showLottoQuantity(manualLottos, autoLottos); | ||||||||||
|
||||||||||
Lottos allLottos = manualLottos.combine(autoLottos); | ||||||||||
ResultView.showGeneratedLottos(allLottos); | ||||||||||
|
||||||||||
return allLottos; | ||||||||||
} | ||||||||||
|
||||||||||
private Count getManualIssueCount(Budget budget) { | ||||||||||
return InputView.retryableInputSelfIssueCount(budget); | ||||||||||
} | ||||||||||
|
||||||||||
private Lottos getManualIssuedLottos(Count 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,36 @@ | ||
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 price.isEnough(budget); | ||
} | ||
|
||
public boolean isEnoughToPay(Price price, int count) { | ||
return price.isEnough(budget, count); | ||
} | ||
|
||
public Budget spend(Price price) { | ||
return new Budget(budget - price.getValue()); | ||
} | ||
|
||
public Budget spend(Price price, Count issueCount) { | ||
return new Budget(budget - price.multiple(issueCount)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package lotto.domain; | ||
|
||
public class Count implements Comparable<Count>{ | ||
|
||
private int count; | ||
|
||
public Count(int count) { | ||
this(count, null); | ||
} | ||
|
||
public Count(int count, Budget budget) { | ||
validPositiveValue(count); | ||
validEnableCount(count, budget); | ||
this.count = count; | ||
} | ||
|
||
private void validEnableCount(int count, Budget budget) { | ||
if(budget == null) { | ||
return; | ||
} | ||
if(!budget.isEnoughToPay(Price.LOTTO, count)){ | ||
throw new IllegalArgumentException("구매 가능 수량을 초과했습니다: " + 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 Count decrease() { | ||
return new Count(count - 1); | ||
} | ||
|
||
@Override | ||
public int compareTo(Count that) { | ||
return this.count - that.count; | ||
} | ||
} |
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 size() { | ||||||
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,32 @@ | ||
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(Count count) { | ||
return count.getCount() * value; | ||
} | ||
|
||
public int divide(int budget) { | ||
return budget / value; | ||
} | ||
|
||
public boolean isEnough(int budget) { | ||
return value <= budget; | ||
} | ||
|
||
public boolean isEnough(int budget, int count) { | ||
return value * count <= budget; | ||
} | ||
} |
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.
결제 금액보다 많이 살수 없지 않을까요~?