Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 12 additions & 2 deletions src/main/java/lotto/LottoMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
import lotto.view.InputView;
import lotto.view.ResultView;

import java.util.List;

public class LottoMain {
public static void main(String[] args) {
Money money = new Money(InputView.getInputMoney());

LottoGroup lottoGroup = new LottoGroup(money);
int inputManualLottoCount = InputView.getInputManualLottoCount();

LottoCount lottoCount = new LottoCount(money, inputManualLottoCount);

LottoPurChase lottoPurChase = new LottoPurChase(money, lottoCount);

List<Lotto> manualLottos = InputView.getInputManualLottos(inputManualLottoCount);

LottoGroup lottoGroup = new LottoGroup(lottoPurChase, manualLottos);

ResultView.showBuyLottos(lottoGroup);
ResultView.showBuyLottos(lottoGroup, lottoPurChase);

Lotto winLotto = new Lotto(InputView.getInputWinNumber());

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/lotto/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lotto.domain;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

Expand All @@ -16,8 +17,22 @@ public Lotto(String... numbers) {
}

public Lotto(List<LottoNumber> numbers) {
validation(numbers);
this.numbers = numbers;
}
private void validation(List<LottoNumber> numbers) {
if (isDuplication(numbers)) {
throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다.");
}

if (numbers.size() != 6) {
throw new IllegalArgumentException("로또 번호는 6개여야 합니다.");
}
}

private boolean isDuplication(List<LottoNumber> numbers) {
return new HashSet<>(numbers).size() != numbers.size();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

중복 체크를 위해 Set으로 변환하고 있다.
Lotto 필드인 List를 Set로 구현해 보면 어떨까?
Set으로 관리하다 외부에서 이 값을 접근할 때 순서를 보장해야 한다면 정렬해서 반환하는 것은 어떨까?


private static List<LottoNumber> stringToList(String... numbers) {
return Arrays.stream(numbers)
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/lotto/domain/LottoCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package lotto.domain;

public class LottoCount {
private final int manualCount;
private final int autoCount;

public LottoCount(Money money, int manualCount) {
this(money.getBuyableCount() - manualCount, manualCount);
}

public LottoCount(int autoCount,int manualCount) {
this.autoCount = autoCount;
this.manualCount = manualCount;
}

public int getManualCount() {
return this.manualCount;
}

public int getAutoCount() {
return this.autoCount;
}
}
16 changes: 16 additions & 0 deletions src/main/java/lotto/domain/LottoGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@
public class LottoGroup {
Copy link
Contributor

Choose a reason for hiding this comment

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

LottoGroup이 구매한 로또와 관련한 로직을 구현하는 책임과 로또 생성하는 책임을 가지는 것으로 보여진다.
기능이 추가되면서 로또 생성하는 책임의 비율이 높아지고 있는데 로또 생성에 대한 책임을 다른 객체로 분리하는 것은 어떨까?

3단계에서 4단계로 요구사항이 변경될 때 로또를 생성하는 부분의 요구사항만 변경됐다.
로또를 생성하는 부분을 다음과 같은 구조의 인터페이스로 분리해 보는 연습을 해보면 어떨까?
이와 같이 인터페이스로 구현했을 때의 잇점에 대해 고민해 보는 시간을 가져본다.

public interface LottosGenerator {
    Lottos generate();
}

private final List<Lotto> lottos;

public LottoGroup(LottoPurChase lottoPurchase, List<Lotto> manualLottos) {
this(buyLotto(lottoPurchase, manualLottos));
}

public LottoGroup(Money money) {
this(buyLotto(money));
}

public LottoGroup(List<Lotto> lottos) {
this.lottos = lottos;
}

private static List<Lotto> buyLotto(LottoPurChase lottoPurchase, List<Lotto> manualLottos) {
List<Lotto> lottoArray = new ArrayList<>(manualLottos);

for(int i = 0; i < lottoPurchase.getAutoCount(); i++) {
lottoArray.add(new Lotto(LottoMachine.createLottoNumbers()));
}

return lottoArray;
}


private static List<Lotto> buyLotto(Money money) {
int cnt = money.getBuyableCount();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/lotto/domain/LottoNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public LottoNumber(int number) {

public static LottoNumber valueOf(String number){
validation(Integer.parseInt(number));
return CACHE_NUM.get(Integer.parseInt(number));
return valueOf(Integer.parseInt(number));
}

public static LottoNumber valueOf(int number){
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/lotto/domain/LottoPurChase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package lotto.domain;

public class LottoPurChase {
private final Money money;
private final LottoCount lottoCount;


public LottoPurChase(int money, int autoCount, int manualCount) {
this(new Money(money), new LottoCount(autoCount, manualCount));
}
public LottoPurChase(Money money, LottoCount lottoCount) {
this.money = money;
this.lottoCount = lottoCount;
}

public Money getMoney() {
return this.money;
}

public int getAutoCount() {
return this.lottoCount.getAutoCount();
}

public int getManualCount() {
return this.lottoCount.getManualCount();
}
}
4 changes: 4 additions & 0 deletions src/main/java/lotto/domain/LottoWinningNumbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public class LottoWinningNumbers {
private final Lotto winningLotto;
private final LottoNumber bonusNumber;

public LottoWinningNumbers(Lotto winningLotto, int bonusNumber) {
this(winningLotto, new LottoNumber(bonusNumber));
}

public LottoWinningNumbers(Lotto winningLotto, LottoNumber bonusNumber) {
this.winningLotto = winningLotto;
this.bonusNumber = bonusNumber;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/lotto/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lotto.domain.Lotto;

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

public class InputView {
Expand All @@ -12,6 +14,22 @@ public static int getInputMoney() {
return Integer.parseInt(scanner.nextLine());
}

public static int getInputManualLottoCount() {
System.out.println("수동으로 구매할 로또 수를 입력해 주세요.");
return Integer.parseInt(scanner.nextLine());
}

public static List<Lotto> getInputManualLottos(int manualLottoCount) {
System.out.println("수동으로 구매할 번호를 입력해 주세요.");

List<Lotto> manualLottos = new ArrayList<>();
for (int i = 0; i < manualLottoCount; i++) {
manualLottos.add(new Lotto(parse(scanner.nextLine())));
}

return manualLottos;
}

public static String[] getInputWinNumber() {
System.out.println("지난 주 당첨 번호를 입력해 주세요.");

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/lotto/view/ResultView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class ResultView {

public static void showBuyLottos(LottoGroup lottoGroup) {
System.out.printf("%d개를 구매했습니다.%n", lottoGroup.getLottoNumbers().size());
public static void showBuyLottos(LottoGroup lottoGroup, LottoPurChase lottoPurchase) {
System.out.printf("수동으로 %d장, 자동으로 %d개를 구매했습니다.%n", lottoPurchase.getManualCount() ,lottoPurchase.getAutoCount());

for (Lotto lotto : lottoGroup.getLottoNumbers()) {
System.out.println(lotto);
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/lotto/domain/LottoCountTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lotto.domain;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class LottoCountTest {

@Test
void 수동_자동_카운트해서_생성() {
LottoCount lottoCount = new LottoCount(11, 3);

Assertions.assertThat(lottoCount.getAutoCount()).isEqualTo(11);
}
}
13 changes: 13 additions & 0 deletions src/test/java/lotto/domain/LottoPurchaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package lotto.domain;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class LottoPurchaseTest {
@Test
void create() {
LottoPurChase lottoPurChase = new LottoPurChase(14000, 11, 3);

Assertions.assertThat(lottoPurChase.getMoney()).isEqualTo(new Money(14000));
}
}
4 changes: 2 additions & 2 deletions src/test/java/lotto/domain/LottoResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LottoResultTest {
)
);

LottoWinningNumbers lottoWinningNumbers = new LottoWinningNumbers(winLotto, LottoNumber.valueOf(10));
LottoWinningNumbers lottoWinningNumbers = new LottoWinningNumbers(winLotto, 10);
LottoResult lottoResult = lottoGroup.match(lottoWinningNumbers);

Assertions.assertThat(lottoResult.calTotal()).isEqualTo(new Money(10000));
Expand All @@ -35,7 +35,7 @@ public class LottoResultTest {
)
);

LottoWinningNumbers lottoWinningNumbers = new LottoWinningNumbers(winLotto, LottoNumber.valueOf(10));
LottoWinningNumbers lottoWinningNumbers = new LottoWinningNumbers(winLotto, 10);

LottoResult lottoResult = lottoGroup.match(lottoWinningNumbers);

Expand Down
25 changes: 20 additions & 5 deletions src/test/java/lotto/domain/LottoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,46 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class LottoTest {

@Test
void 로또_개수_구하기() {
Lotto lotto = new Lotto(1,2,3,4,5,6);
Lotto lotto = new Lotto(1, 2, 3, 4, 5, 6);

assertThat(lotto.countMatchedNumbers(
new Lotto(1,2,3,11,22,33)
new Lotto(1, 2, 3, 11, 22, 33)
)).isEqualTo(3);
}

@Test
void 포함된_숫자인지() {
Lotto lotto = new Lotto(1,2,3,4,5,6);
Lotto lotto = new Lotto(1, 2, 3, 4, 5, 6);

assertThat(lotto.contains(LottoNumber.valueOf(1))).isTrue();
}

@Test
void 로또_스트링으로_받기() {
Lotto lotto = new Lotto("1","2","3","4","5","6");
Lotto lotto = new Lotto("1", "2", "3", "4", "5", "6");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Lotto lotto = new Lotto("1", "2", "3", "4", "5", "6");
Lotto lotto = new Lotto("1,2,3,4,5,6");

문자열 가변인자보다 위와 같이 구현할 수 있으면 더 좋지 않을까?


assertThat(lotto.countMatchedNumbers(
new Lotto(1,2,3,11,22,33)
new Lotto(1, 2, 3, 11, 22, 33)
)).isEqualTo(3);
}

@Test
void 로또_번호는_중복_될_수_없음() {
assertThatThrownBy(()-> new Lotto("1", "2", "3", "4", "5", "5"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("로또 번호는 중복될 수 없습니다.");
}

@Test
void 로또_번호는_6개_이여야_함() {
assertThatThrownBy(()-> new Lotto("1", "2", "3", "4", "5"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("로또 번호는 6개여야 합니다.");
}
}