Skip to content
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

Open
wants to merge 20 commits into
base: boy0516
Choose a base branch
from
Open

Step4 #3768

Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- [x] 로또 래퍼 클래스 구현
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스크린샷 2024-04-06 오후 11 41 00

결제 금액보다 많이 살수 없지 않을까요~?

- [x] 로또 생성자 빈값 입력 예외 처리 검증
- [x] 구입금액 입력 기능
- [ ] 구입금액 최소 단위 제한(백원 이하 오류 처리)
- [X] 구입금액 최소 단위 제한(백원 이하 오류 처리)
- [X] 랜덤 로또 번호 발급 기능
- [x] 로또 래퍼 클래스 반환
- [x] 로또 번호 6자리 생성
Expand All @@ -28,3 +28,5 @@
- [x] 수익률 산출 기능
- [X] 보너스 번호 입력 기능
- [X] 2등 상금 추가
- [X] 수동 발급 추가
- [x] 예산 지불 추가
35 changes: 28 additions & 7 deletions src/main/java/lotto/controller/LottoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SelfIssueCount 일반적으로 이해하기 쉬운 표현인가요?
Count 그냥 카운트 아닌가요?

Count 수동금액갯수
불리어지는 곳에 따라 이름은 달라질 수 있지 않을까요?


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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Lottos lottos = new Lottos(budget);
return lottos;
return new Lottos(budget);

불필요한 변수 선언은 하지 않는게 복잡도를 덜어낼수 있을것 같아요~

}

private Budget getBudget() {
Budget budget = InputView.inputBuyBudget();
ResultView.showLottoQuantity(budget.divide(Lotto.LOTTO_PRICE));
Budget budget = InputView.retryableInputBuyBudget();
return budget;
}

Expand Down
28 changes: 21 additions & 7 deletions src/main/java/lotto/domain/Budget.java
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();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return budget > price.getValue();
return price.이것보다_작아?(price);

값을 꺼내기 보다는 상태를 가진 객체에게 물어볼수 있지 않을까요?

}

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));
}
}
19 changes: 17 additions & 2 deletions src/main/java/lotto/domain/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public Lotto() {

public Lotto(List<LottoNumber> lotto) {
validEmpty(lotto);
validLottoSize(lotto);
this.lotto = lotto;
}

Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -49,4 +60,8 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(lotto);
}

protected boolean isContainBonus(LottoNumber bonus) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equals, hashCode 보다는 위에 있는게 일반적인것 같습니다. 🤔

return lotto.contains(bonus);
}
}
21 changes: 20 additions & 1 deletion src/main/java/lotto/domain/Lottos.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private List<Lotto> lottos;
private final List<Lotto> lottos;

재할당을 하지말라고 final을 붙여주는건 어떨까요?

Expand All @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collections.unmodifiableList

리턴해준 상태를 수정 불가로 해주면 어떨까요?

}
Expand All @@ -29,6 +37,10 @@ public List<Lotto> getLottos() {
return lottos;
}

public int getLottoSize() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public int getLottoSize() {
public int size() {

어쩌면 우리는 무의식적으로 get-을 붙이고 있는게 아닐까요?
일급콜렉션은 하나의 상태를 관리하는 객체이므로 size() 라고 해도 충분한 의미전달이 된다고 생각해요 😄

return lottos.size();
}

public Statistics computeStatistic(WinLotto winLotto) {
return new Statistics(checkStatisticNumberMatch(winLotto));
}
Expand All @@ -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);
}
}
24 changes: 24 additions & 0 deletions src/main/java/lotto/domain/Price.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lotto.domain;

public enum Price {
Copy link

Choose a reason for hiding this comment

The 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;
}
}
33 changes: 33 additions & 0 deletions src/main/java/lotto/domain/SelfIssueCount.java
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;
}
}
59 changes: 21 additions & 38 deletions src/main/java/lotto/domain/WinLotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,46 @@
import java.util.Objects;
import java.util.stream.Collectors;

public class WinLotto {
public class WinLotto extends Lotto {
Copy link

Choose a reason for hiding this comment

The 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);
}
}
Loading