Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 22 additions & 6 deletions src/main/java/lotto/LottoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lotto.domain.*;
import lotto.view.LottoView;

import java.util.ArrayList;
import java.util.List;

public class LottoApplication {
Expand All @@ -11,17 +12,32 @@ public static void main(String[] args) {
LottoView lottoView = new LottoView();

Money money = new Money(lottoView.inputMoney());
Lottos lottos = lottoSystem.buyLottos(money);

int numberOfManualLotto = lottoView.inputNumberOfManualLotto();
lottoSystem.validateNumberOfManualLotto(numberOfManualLotto);
money.buyLotto(numberOfManualLotto);

List<Lotto> allLottos = new ArrayList<>();

lottoView.printManualLottosDescription();
for (int i = 0; i < numberOfManualLotto; i++) {
List<Integer> manualNumbers = lottoView.inputManualLottos();
Lotto lotto = lottoSystem.convertNumbersToLotto(manualNumbers);
allLottos.add(lotto);
Comment on lines +25 to +26

Choose a reason for hiding this comment

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

lottoSystem의 역할은 무엇인가요? 메서드들을 보니 책임이 하나인 것 같지는 않아서요.

Copy link
Author

@riroan riroan Apr 15, 2024

Choose a reason for hiding this comment

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

로또 플레이할 때 필요한 기능들을 모아둔 클래스입니다. 어떻게 쪼개야할 지 몰라서 한 클래스에 모두 들어간 느낌이네요..

}

List<Lotto> autoLottos = lottoSystem.buyAutoLotto(money);
allLottos.addAll(autoLottos);

Lottos lottos = new Lottos(allLottos);
lottoView.printLottos(lottos);

List<Integer> answerAndBonusNumber = lottoView.inputAnswer();
WinningNumber winningNumber = lottoSystem.convertToAnswer(answerAndBonusNumber);
lottoSystem.scoreLottos(lottos, winningNumber);
WinningNumber winningNumber = lottoView.inputAnswer();
Result result = lottoSystem.scoreLottos(lottos, winningNumber);

Result result = lottoSystem.getResult();
lottoView.printResult(result);

Profit profit = lottoSystem.calculateProfit();
Profit profit = lottoSystem.calculateProfit(result, lottos);
lottoView.printProfit(profit);
}
}
65 changes: 0 additions & 65 deletions src/main/java/lotto/LottoSystem.java

This file was deleted.

6 changes: 5 additions & 1 deletion src/main/java/lotto/domain/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private void validateBalls(List<Ball> balls) {

private void validateNumberOfBalls(List<Ball> balls) {
if (balls.size() != LOTTO_SIZE) {
throw new RuntimeException();
throw new IllegalStateException("로또 볼의 개수는 6개여야합니다!");
}
}

Expand All @@ -49,6 +49,10 @@ public List<Ball> getBalls() {
return this.balls;
}

public long countCorrectNumbers(Lotto other) {
return balls.stream().filter(other::contain).count();
}

@Override
public String toString() {
List<String> numbers = balls.stream()
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/lotto/domain/LottoSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lotto.domain;

import lotto.generator.ManualLottoGenerator;
import lotto.generator.RandomLottoGenerator;

import java.util.List;

public class LottoSystem {
private static final Money LOTTO_PRICE = new Money(1000);

public List<Lotto> buyAutoLotto(Money money) {
return generateAutoLottos(money.canBuyCount());
}

public void validateNumberOfManualLotto(int numberOfManualLotto) {
if (numberOfManualLotto < 0) {
throw new IllegalArgumentException("유효하지 않은 수동 구매 개수입니다!");
}
}

public WinningNumber convertToAnswer(List<Integer> answerAndBonusNumber) {
Lotto lotto = convertNumbersToLotto(answerAndBonusNumber.subList(0, 6));
Ball bonusBall = new Ball(answerAndBonusNumber.get(6));

return new WinningNumber(lotto, bonusBall);
}

public Lotto convertNumbersToLotto(List<Integer> numbers) {
ManualLottoGenerator generator = new ManualLottoGenerator(numbers);
return generator.generateLotto();
}

private List<Lotto> generateAutoLottos(long count) {
RandomLottoGenerator generator = RandomLottoGenerator.getInstance();
return generator.generateAutoLottos(count);
}
Comment on lines +28 to +36

Choose a reason for hiding this comment

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

이 부분은 제가 의도한거랑은 조금 차이가 있는데요. 🙄

List<LottoGenerator> lottoGenerators = List.of(
    new RandomLottoGenerator(),
    new RandomLottoGenerator(),
    new ManualLottoGenerator(List.of(1, 2, 3, 4, 5, 6)),
    new ManualLottoGenerator(List.of(1, 2, 3, 4, 5, 6))
);
        
new Lottos(
    lottoGenerators.stream()
        .map(it -> it.generate())
        .collect(Collectors.toList())
);
        

이런 느낌을 기대하긴 했어요. 작성해 주신 구조라면 다형성이 크게 의미는 없을 것 같아서요.


public Result scoreLottos(Lottos lottos, WinningNumber winningNumber) {
return lottos.scoreLottos(winningNumber);
}

public Profit calculateProfit(Result result, Lottos lottos) {
long reward = result.calculateReward();
long seed = lottos.getSeed();

return new Profit(reward, seed);
}
}
15 changes: 13 additions & 2 deletions src/main/java/lotto/domain/Lottos.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package lotto.domain;

import lotto.domain.Lotto;

import java.util.List;

public class Lottos {
private static final Money LOTTO_PRICE = new Money(1000);
private final List<Lotto> values;

public Lottos(List<Lotto> values) {
Expand All @@ -18,4 +17,16 @@ public int getSize() {
public Lotto get(int ix) {
return values.get(ix);
}

Comment on lines 17 to +20

Choose a reason for hiding this comment

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

이 메서드는 view에서 사용하고 있는데요. 웹 환경이라면 해당 메서드는 호출 가능한가요?
getter를 사용하지 말라는 말은 도메인 모델끼리 비즈니스 요구 사항을 풀어내는 경우를 말합니다.
view에서는 getter를 사용해 데이터를 출력하는 하면 됩니다.

Copy link
Author

Choose a reason for hiding this comment

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

이 코멘트는 무슨 의미인지 이해하지 못했습니다.. 🤔
get메소드를 view에서만 사용하기 때문에 웹에서는 사용하지 않게 되어 사라질 메소드라 지양하는 의미로 받아들였는데 마지막 줄의 getter를 사용하여 데이터를 출력하라는게 무슨 의미인지 모르겠습니다.

Choose a reason for hiding this comment

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

https://edu.nextstep.camp/s/MCLQmhAp/ls/DO2AJZZW

젤 아래에 getter 메소드 없이 구현 가능한가? 를 읽어보시면 좋을 거 같아요.
출력을 위해 DTO를 만드는 경우 getter를 사용하고 혹은 view에 도메인 객체가 나간다면 getter를 통해서 데이터를 그대로 꺼내면 된다는 의미 입니다.

public Result scoreLottos(WinningNumber winningNumber) {
Result result = new Result();
for (Lotto lotto : values) {
result.scoreLotto(lotto, winningNumber);
}
return result;
}
Comment on lines +21 to +27

Choose a reason for hiding this comment

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

    public Result scoreLottos(WinningNumber winningNumber) {
        return new Result(
            values.stream()
                .map(it -> winningNumber.match(it))
                .collect(Collectors.groupingBy(it -> it, Collectors.counting()))
        );
    }

이런 방법도 있을 것 같아요.

이 경우 테스트는 더 유연해 지지 않을까요? 테스트 코드는 어떻게 변경될 수 있을까요?

Choose a reason for hiding this comment

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

Result.reward는 필드에 꼭 필요할까요? 내부 Map 에서 계산할 수 있지 않을까요?


public long getSeed() {
return values.size() * LOTTO_PRICE.getValue();
}
}
15 changes: 14 additions & 1 deletion src/main/java/lotto/domain/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Objects;

public class Money {
private static final long LOTTO_PRICE = 1000;
private final long value;

public Money(long value) {
Expand All @@ -13,10 +14,22 @@ public long getValue() {
return this.value;
}

public long divide (Money money) {
public long divide(Money money) {
return this.value / money.getValue();
}

public Money buyLotto(int count) {
long need = count * LOTTO_PRICE;
if (need > value) {
throw new IllegalArgumentException("돈이 부족합니다.");
}
return new Money(value - need);
}

public long canBuyCount() {
return value / LOTTO_PRICE;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
34 changes: 3 additions & 31 deletions src/main/java/lotto/domain/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public Result() {
}

public void scoreLotto(Lotto lotto, WinningNumber winningNumber) {
long answerCount = winningNumber.getWinningNumbers().getBalls().stream().filter(lotto::contain).count();
boolean isCorrectBonusBall = lotto.contain(winningNumber.getBonusNumber());

Ranking rank = getRank(answerCount, isCorrectBonusBall);
Ranking rank = winningNumber.match(lotto);
addScore(rank);
}

Expand All @@ -32,38 +29,13 @@ private void addScore(Ranking rank) {
score.put(rank, rankScore + 1);
}


private Ranking getRank(long answerCount, boolean isCorrectBonusBall) {
if (answerCount == 6) {
return Ranking.FIRST;
}

if (answerCount == 5 && isCorrectBonusBall) {
return Ranking.SECOND;
}

if (answerCount == 5) {
return Ranking.THIRD;
}

if (answerCount == 4) {
return Ranking.FOURTH;
}

if (answerCount == 3) {
return Ranking.FIFTH;
}

return Ranking.FAIL;
}

public long calculateReward() {
reward = 0L;

for (Entry<Ranking, Integer> entry: score.entrySet()) {
for (Entry<Ranking, Integer> entry : score.entrySet()) {
Ranking rank = entry.getKey();
int count = entry.getValue();
reward += rank.getReward() * count;
reward += rank.getValue() * count;
}

return reward;
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/lotto/domain/WinningNumber.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lotto.domain;

import lotto.enums.Ranking;

public class WinningNumber {
private final Lotto winningNumbers;
private final Ball bonusNumber;
Expand All @@ -10,18 +12,25 @@ public WinningNumber(Lotto winningNumbers, Ball bonusNumber) {
this.bonusNumber = bonusNumber;
}

private void validateBonusNumberNotInWinningNumbers(Lotto winningNumbers, Ball bonusNumber){
private void validateBonusNumberNotInWinningNumbers(Lotto winningNumbers, Ball bonusNumber) {
boolean isContain = winningNumbers.contain(bonusNumber);
if (isContain) {
throw new IllegalStateException("중복된 숫자가 존재합니다!");
}
}

public Lotto getWinningNumbers() {
return this.winningNumbers;
public long countCorrectNumbers(Lotto lotto) {
return winningNumbers.countCorrectNumbers(lotto);
}

public Ranking match(Lotto lotto) {
long answerCount = countCorrectNumbers(lotto);
boolean isCorrectBonusBall = hasBonusNumber(lotto);

return Ranking.getRank(answerCount, isCorrectBonusBall);
}

public Ball getBonusNumber() {
return this.bonusNumber;
public boolean hasBonusNumber(Lotto lotto) {
return lotto.contain(bonusNumber);
}
}
36 changes: 21 additions & 15 deletions src/main/java/lotto/enums/Ranking.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
package lotto.enums;

public enum Ranking {
FAIL(0), FIFTH(1), FOURTH(2), THIRD(3), SECOND(4), FIRST(5);
FAIL(0, 0), FIFTH(1, 5000), FOURTH(2, 50000), THIRD(3, 1500000), SECOND(4, 30000000), FIRST(5, 2000000000);

final int value;
private final int ix;
private final long value;

Ranking(int value) {
Ranking(int ix, long value) {
this.ix = ix;
this.value = value;
}

public long getReward() {
if (value == Ranking.FIRST.value) {
return 2000000000;
public static Ranking getRank(long answerCount, boolean isCorrectBonusBall) {
if (answerCount == 6) {
return Ranking.FIRST;
}

if (value == Ranking.SECOND.value) {
return 30000000;
if (answerCount == 5 && isCorrectBonusBall) {
return Ranking.SECOND;
}

if (value == Ranking.THIRD.value) {
return 1500000;
if (answerCount == 5) {
return Ranking.THIRD;
}

if (value == Ranking.FOURTH.value) {
return 50000;
if (answerCount == 4) {
return Ranking.FOURTH;
}

if (value == Ranking.FIFTH.value) {
return 5000;
if (answerCount == 3) {
return Ranking.FIFTH;
}

return 0;
return Ranking.FAIL;
}

public long getValue() {
return value;
}
}
7 changes: 7 additions & 0 deletions src/main/java/lotto/generator/LottoGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lotto.generator;

import lotto.domain.Lotto;

public interface LottoGenerator {
Lotto generateLotto();
}
Loading