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
5 changes: 4 additions & 1 deletion src/main/java/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import domain.lotto.Lotto;
import domain.lotto.LottoCollection;
import domain.lotto.LottoNumber;
import domain.lotto.WinningLotto;
import domain.money.Money;
import domain.provider.LottoSeller;
import input.InputView;
Expand All @@ -15,11 +17,12 @@ public static void main(String[] args) {
LottoSeller lottoSeller = new LottoSeller(lottoPrice);

Money money = InputView.inputMoney();

LottoCollection myLotto = lottoSeller.sellTo(money);

OutputView.printLotto(myLotto);

Lotto winningLotto = InputView.inputWinningLottoNumber();
WinningLotto winningLotto = InputView.inputWinningLottoNumber();

LottoStatistics lottoStatistics = new LottoStatistics(winningLotto, myLotto, money);
OutputView.showWinningStatistics(lottoStatistics);
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/domain/LottoStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import domain.lotto.Lotto;
import domain.lotto.LottoCollection;
import domain.lotto.WinningLotto;
import domain.money.Money;

import java.util.Iterator;
Expand All @@ -14,7 +15,7 @@ public class LottoStatistics implements Iterable<Rank> {
private Map<Rank, Integer> counter;
private Money invest;

public LottoStatistics(Lotto winningLotto, LottoCollection lottoCollection, Money money) {
public LottoStatistics(WinningLotto winningLotto, LottoCollection lottoCollection, Money money) {
this.counter = getCounter(winningLotto, lottoCollection);
this.invest = money;
}
Expand All @@ -25,13 +26,10 @@ public float getRor() {
sum += rank.getProfit(counter.get(rank));
}

System.out.println(invest.getAmount());
return (float) sum / invest.getAmount();
}

private final static Rank[] PRINT_ORDER_RANK = {Rank.FIFTH, Rank.FOURTH, Rank.THIRD, /*Rank.SECOND, */Rank.FIRST};

private Map<Rank, Integer> getCounter(Lotto winningLotto, LottoCollection lottoCollection) {
private Map<Rank, Integer> getCounter(WinningLotto winningLotto, LottoCollection lottoCollection) {
Copy link
Member

Choose a reason for hiding this comment

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

Map<Rank, Integer>을 추상화 하는 건 어떨까요?
Rank.RANK_COUNTER()안의 로직과 이 메서드의 로직을 책임으로 가질 수 있을 것 같아요.

Map<Rank, Integer> counter = Rank.RANK_COUNTER();
for (Lotto lotto : lottoCollection) {
counter.merge(winningLotto.match(lotto), 1, Integer::sum);
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/domain/Rank.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public enum Rank {
this.winningMoney = winningMoney;
}

public static Rank valueOf(int countOfMatch) {
public static Rank valueOf(int countOfMatch, boolean matchBonus) {
Copy link
Member

Choose a reason for hiding this comment

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

matchBouns와 같은 부분에 대해 전략으로 Refactoring할 수는 없을까요?

if (countOfMatch < WINNING_MIN_COUNT) {
return MISS;
}

if (SECOND.matchCount(countOfMatch)) {
if (SECOND.matchCount(countOfMatch) && matchBonus) {
return SECOND;
}

Expand Down Expand Up @@ -61,6 +61,10 @@ public static Map<Rank, Integer> RANK_COUNTER() {

@Override
public String toString() {
return String.format("%d개 일치 (%,d원)", countOfMatch, winningMoney);
String note = "";
if (this == Rank.SECOND) {
Copy link
Member

Choose a reason for hiding this comment

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

이 부분을 다른 Object로 리팩토링할 수 없을까요?
나중에 특정 조건이 추가되면 확장될 가능성이 높아보여서 그렇습니다ㅠ

Copy link
Author

Choose a reason for hiding this comment

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

저도 출력에 관한 책임을 따로 분리할 계획을 갖고 있었습니다ㅎㅎ 다음 스탭때 구현해보도록 하죠.

note = ", 보너스볼 일치";
}
return String.format("%d개 일치%s (%,d원)", countOfMatch, note, winningMoney);
}
}
18 changes: 4 additions & 14 deletions src/main/java/domain/lotto/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class Lotto {

private static final int PICK_NUM = 6;
public static final int PICK_NUM = 6;
private List<LottoNumber> lotto;

public Lotto(List<LottoNumber> lotto) {
Expand All @@ -17,26 +17,16 @@ public Lotto(List<LottoNumber> lotto) {
this.lotto = lotto;
}

public Rank match(Lotto userLotto) {
int matchCount = getMatchCount(userLotto);
return Rank.valueOf(matchCount);
public int getMatchCount(Lotto userLotto) {
return (int) lotto.stream().filter(userLotto::contains).count();
}

private int getMatchCount(Lotto userLotto) {
int count = 0;
for (LottoNumber n : this.lotto) {
count += userLotto.contains(n) ? 1 : 0;
}
return count;
}

private boolean contains(LottoNumber n) {
public boolean contains(LottoNumber n) {
return lotto.contains(n);
}

@Override
public String toString() {
return lotto.toString();
}

}
24 changes: 24 additions & 0 deletions src/main/java/domain/lotto/WinningLotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package domain.lotto;

import domain.Rank;

public class WinningLotto {
private Lotto lotto;
private LottoNumber bonusNumber;

public WinningLotto(Lotto lotto, LottoNumber bonusNumber) {
Copy link
Member

Choose a reason for hiding this comment

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

WinningLotto와 BonusNumber는 종속적인 관계인가요?

this.lotto = lotto;
this.bonusNumber = bonusNumber;
}

public Rank match(Lotto userLotto) {
int matchCount = lotto.getMatchCount(userLotto);
boolean matchBonus = userLotto.contains(bonusNumber);
return Rank.valueOf(matchCount, matchBonus);
}

@Override
public String toString() {
return String.format("%s + %s", lotto , bonusNumber);
}
}
1 change: 0 additions & 1 deletion src/main/java/domain/provider/LottoSeller.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class LottoSeller {

public LottoSeller(Money lottoPrice) {
this.lotto = lottoPrice;

}

public LottoCollection sellTo(Money money) {
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/input/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import domain.lotto.Lotto;
import domain.lotto.LottoNumber;
import domain.lotto.WinningLotto;
import domain.money.Money;

import java.util.Arrays;
Expand All @@ -12,26 +13,32 @@
public class InputView {
private static final String buyGuideText = "구입금액을 입력해 주세요.";
private static final String winningGuideText = "지난 주 당첨 번호를 입력해 주세요.";
private static final String bonusNumberGuideText = "보너스 볼을 입력해 주세요.";

public static Money inputMoney() {
return buyMock(1400000);
}

private static Money buyMock(int n) {
System.out.println(buyGuideText);
return new Money(n);
return Money.of(n);
}

public static Lotto inputWinningLottoNumber() {
public static WinningLotto inputWinningLottoNumber() {
return winningLottoMock();
}

private static Lotto winningLottoMock() {
private static WinningLotto winningLottoMock() {
System.out.println(winningGuideText);
Lotto lotto = new Lotto(Arrays.asList(LottoNumber.of(1), LottoNumber.of(2), LottoNumber.of(3), LottoNumber.of(4), LottoNumber.of(5), LottoNumber.of(6)));
System.out.println(lotto);
System.out.println("> 입력함");

return lotto;
}
System.out.println(bonusNumberGuideText);
WinningLotto winningLotto = new WinningLotto(lotto, LottoNumber.of(7));
System.out.println("> 입력함");

System.out.println(" 입력 값: " + winningLotto + "\n");

return winningLotto;
}
}
10 changes: 8 additions & 2 deletions src/main/java/output/OutputView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package output;

import domain.Rank;
import domain.lotto.LottoCollection;
import domain.LottoStatistics;

Expand All @@ -14,10 +15,15 @@ public static void printLotto(LottoCollection lottoCollection) {
}

public static void showWinningStatistics(LottoStatistics lottoStatistics) {
final Rank[] PRINT_ORDER_RANK = {Rank.FIFTH, Rank.FOURTH, Rank.THIRD, Rank.SECOND, Rank.FIRST};

System.out.println("당첨 통계" + "\n" + "-------------");
lottoStatistics.forEach(rank -> System.out.println(
/*lottoStatistics.forEach(rank -> System.out.println(
Copy link
Member

Choose a reason for hiding this comment

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

불필요한 주석은 제거해주세요 :)

String.format("- %s %s개", rank, lottoStatistics.getCount(rank)))
);
);*/
for(Rank rank : PRINT_ORDER_RANK) {
System.out.println(String.format("- %s %s개", rank, lottoStatistics.getCount(rank)));
}
System.out.println(String.format("\n총 수익률은 %.2f%%입니다.", lottoStatistics.getRor()));
}
}
59 changes: 59 additions & 0 deletions src/test/java/domain/lotto/WinningLottoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package domain.lotto;

import domain.Rank;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;

class WinningLottoTest {

private final static WinningLotto WINNING_LOTTO = new WinningLotto(
new Lotto(Arrays.asList(LottoNumber.of(1), LottoNumber.of(2), LottoNumber.of(3), LottoNumber.of(4), LottoNumber.of(5), LottoNumber.of(6))),
LottoNumber.of(7)
);

@Test
void noMatch() {
final Lotto userLotto = generateLotto(8, 9, 10, 11, 12, 13, 14);
assertThat(WINNING_LOTTO.match(userLotto)).isEqualTo(Rank.MISS);
Copy link
Member

Choose a reason for hiding this comment

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

모든 케이스에 대해 테스트를 작성하는게 의미가 있을까요?
나중에 로또 넘버가 100가지 되는 경우에 대해 모든 케이스를 테스트 하기 위해선 어떻게 작성해야할까요?

Copy link
Author

Choose a reason for hiding this comment

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

의미있는 경곗값만 테스트하는걸 고려해볼 수 있겠네요

}

@Test
void matchThree() {
final Lotto userLotto = generateLotto(1, 2, 3, 11, 12, 13);
assertThat(WINNING_LOTTO.match(userLotto)).isEqualTo(Rank.FIFTH);
}

@Test
void matchFour() {
final Lotto userLotto = generateLotto(1, 2, 3, 4, 12, 13);
assertThat(WINNING_LOTTO.match(userLotto)).isEqualTo(Rank.FOURTH);
}

@Test
void matchFive() {
final Lotto userLotto = generateLotto(1, 2, 3, 4, 5, 13);
assertThat(WINNING_LOTTO.match(userLotto)).isEqualTo(Rank.THIRD);
}

@Test
void matchSix() {
final Lotto userLotto = generateLotto(1, 2, 3, 4, 5, 6);
assertThat(WINNING_LOTTO.match(userLotto)).isEqualTo(Rank.FIRST);
}

@Test
void matchFiveAndBonus() {
final Lotto userLotto = generateLotto(1, 2, 3, 4, 5, 7, 14);
assertThat(WINNING_LOTTO.match(userLotto)).isEqualTo(Rank.SECOND);
}


private Lotto generateLotto(int... lottoNumbers) {
return new Lotto(Arrays.stream(lottoNumbers).mapToObj(LottoNumber::of).collect(toList()));
}

}