Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
19 changes: 19 additions & 0 deletions src/main/java/com/javabom/lotto/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
package com.javabom.lotto;

import com.javabom.lotto.domain.results.LottoBasicLuckyNumbers;
import com.javabom.lotto.domain.results.LottoLuckyNumbers;
import com.javabom.lotto.domain.results.LottoResults;
import com.javabom.lotto.domain.ticket.*;
import com.javabom.lotto.view.InputView;
import com.javabom.lotto.view.OutputView;

public class LottoApplication {
public static void main(String[] args) {
Money inputMoney = new Money(InputView.getMoneyToBuyTicket());

LottoTicketDispenser lottoTicketDispenser = new LottoTicketDispenser(new RandomLottoNumberGenerator());
LottoTickets lottoTickets = lottoTicketDispenser.getAutoTickets(inputMoney);
OutputView.printLottoTickets(lottoTickets);

LottoBasicLuckyNumbers basicLuckyNumbers = new LottoBasicLuckyNumbers(InputView.getLottoBasicLuckyNumbers());
LottoNumber bonusNumber = new LottoNumber(InputView.getBonusNumber());
LottoLuckyNumbers LuckyNumbers = new LottoLuckyNumbers(basicLuckyNumbers, bonusNumber);

LottoResults lottoResults = LuckyNumbers.getLottoResults(lottoTickets);
OutputView.printLottoResults(lottoResults);
OutputView.printEarningRate(inputMoney, lottoResults.getTotalPrizeMoney());
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/javabom/lotto/domain/LottoNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.javabom.lotto.domain;

import com.javabom.lotto.domain.ticket.LottoNumber;

import java.util.List;

abstract public class LottoNumbers {

protected final List<LottoNumber> numbers;

public LottoNumbers(List<LottoNumber> numbers) {
validateLottoNumbers(numbers);
this.numbers = numbers;
}

protected abstract void validateLottoNumbers(List<LottoNumber> numbers);

public int countMatchingNumbers(LottoNumbers lottoNumbers) {
return (int) lottoNumbers.numbers.stream()
.map(this.numbers::contains)
.filter(isContain -> isContain)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

.filter(this.numbers::contains)

하나로 할 수 있어보여!

.count();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.javabom.lotto.domain.results;

import com.javabom.lotto.domain.LottoNumbers;
import com.javabom.lotto.domain.ticket.LottoNumber;

import java.util.List;
import java.util.stream.Collectors;

public class LottoBasicLuckyNumbers extends LottoNumbers {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

당첨 번호 중복 검사도 필요할것같아!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

맞어 이거 안햇엇어..

private static final int LUCKY_LOTTO_NUM_SIZE = 6;

public LottoBasicLuckyNumbers(List<Integer> numbers) {
super(convertToLottoNumbers(numbers));
}

private static List<LottoNumber> convertToLottoNumbers(List<Integer> numbers) {
return numbers.stream()
.map(LottoNumber::new)
.collect(Collectors.toList());
}

@Override
protected void validateLottoNumbers(List<LottoNumber> numbers) {
if (numbers.size() != LUCKY_LOTTO_NUM_SIZE) {
throw new IllegalArgumentException("당첨 번호는 반드시 6개입니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.javabom.lotto.domain.results;

import com.javabom.lotto.domain.ticket.LottoNumber;
import com.javabom.lotto.domain.ticket.LottoTicket;
import com.javabom.lotto.domain.ticket.LottoTickets;

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

public class LottoLuckyNumbers {

private final LottoBasicLuckyNumbers basicLuckyNumbers;
private final LottoNumber bonusNumber;

public LottoLuckyNumbers(LottoBasicLuckyNumbers basicLuckyNumbers, LottoNumber bonusNumber) {
this.basicLuckyNumbers = basicLuckyNumbers;
this.bonusNumber = bonusNumber;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

당첨 번호와 보너스 번호가 중복되는지 유효성 검사 필요해보여!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

또 BasicLuckNumbers나 LottoTicket 같이 LottoNumbers 추상클래스를 상속하는 객체들을 생성하는 새로운 객체를 하나 만들어서 거기에 validation 로직을 두는건 어떨까??? 지금 로또 번호 개수 상수가 여러군데에서 중복되서 쓰이고 있어서 이런식으로 하면 어느정도 해결할 수 있을것으로 보여!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

밑에 말이 무슨말인지 잘 모르겠어.
일단은 이 상속관계에 대해서 좋지않다는 리뷰를 많이 들어서 상속을 걷어낼생각이구 BasicLuckyNumbers내부가 LottoTicket과 너무 유사해서 그냥 LottoTicket으로 통일해버릴 생각이야


public LottoResults getLottoResults(LottoTickets lottoTickets) {
List<LottoResult> lottoResults = new ArrayList<>();
for (LottoTicket ticket : lottoTickets.get()) {
lottoResults.add(getLottoResult(ticket));
}
return new LottoResults(lottoResults);
}

private LottoResult getLottoResult(LottoTicket lottoTicket) {
int matchCount = basicLuckyNumbers.countMatchingNumbers(lottoTicket);
boolean isBonusMatched = lottoTicket.isContain(bonusNumber);
return LottoResult.find(matchCount, isBonusMatched);
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/javabom/lotto/domain/results/LottoResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.javabom.lotto.domain.results;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public enum LottoResult {

FIRST_PRIZE(6, 2_000_000_000),
SECOND_PRIZE(5, 30_000_000),
THIRD_PRIZE(5, 1_500_000),
FOURTH_PRIZE(4, 50_000),
FIFTH_PRIZE(3, 5_000),
LOSE(-1, 0);

private final int matchCount;
private final long price;

LottoResult(int matchCount, long price) {
this.matchCount = matchCount;
this.price = price;
}

public static LottoResult find(int matchCount, boolean isBonusMatched) {
validateMatchCount(matchCount);
if (matchCount == SECOND_PRIZE.matchCount && isBonusMatched) {
return SECOND_PRIZE;
}
return valuesWithoutSecond().stream()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Second 빼고 List만들어주는 과정없이 해결할 수 있지않을까?

.filter(result -> result.matchCount == matchCount)
.findFirst()
.orElse(LOSE);
}

private static void validateMatchCount(int matchCount) {
if (matchCount < 0 || matchCount > 6) {
throw new IllegalArgumentException("로또번호 매치 수는 0~6 이외일 수 없습니다.");
}
}

private static List<LottoResult> valuesWithoutSecond() {
return Arrays.stream(values())
.filter(result -> result != SECOND_PRIZE)
.collect(Collectors.toList());
}

public long getPrice() {
return price;
}

public int getMatchCount() {
return matchCount;
}

}
42 changes: 42 additions & 0 deletions src/main/java/com/javabom/lotto/domain/results/LottoResults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.javabom.lotto.domain.results;

import com.javabom.lotto.domain.ticket.Money;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class LottoResults {

private final List<LottoResult> lottoResults;

public LottoResults(List<LottoResult> lottoResults) {
this.lottoResults = lottoResults;
}

public List<LottoResult> findResultsOf(LottoResult lottoResult) {
return lottoResults.stream()
.filter(result -> result == lottoResult)
.collect(Collectors.toList());
}

public Money getTotalPrizeMoney() {
long sum = lottoResults.stream()
.mapToLong(LottoResult::getPrice)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

나도 실수했던건데 Prize랑 Price!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

?? 나도 지금 실수하고있는거야??
저기에 잇는 price는 오타인거같네 첨부터 prize를 생각하고쓴거였는데

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

오타 나도 했었거든

.sum();
return new Money(sum);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LottoResults that = (LottoResults) o;
return Objects.equals(lottoResults, that.lottoResults);
}

@Override
public int hashCode() {
return Objects.hash(lottoResults);
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/javabom/lotto/domain/ticket/LottoNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.javabom.lotto.domain.ticket;

import java.util.Objects;

public class LottoNumber {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

감싼거 아주 좋습니다b

public static final int MIN_LOTTO_NUM = 1;
public static final int MAX_LOTTO_NUM = 45;

private final int number;

public LottoNumber(int number) {
validateNumber(number);
this.number = number;
}

private void validateNumber(int number) {
if (number < MIN_LOTTO_NUM || number > MAX_LOTTO_NUM) {
throw new IllegalArgumentException("로또 번호는 1~45이외 일 수 없습니다.");
}
}

public int get() {
return number;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LottoNumber that = (LottoNumber) o;
return number == that.number;
}

@Override
public int hashCode() {
return Objects.hash(number);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.javabom.lotto.domain.ticket;

import java.util.List;

public interface LottoNumberGenerator {

List<LottoNumber> getLottoNumbers();

}
47 changes: 47 additions & 0 deletions src/main/java/com/javabom/lotto/domain/ticket/LottoTicket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.javabom.lotto.domain.ticket;

import com.javabom.lotto.domain.LottoNumbers;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class LottoTicket extends LottoNumbers {

private static final int LOTTO_NUM_PICK_SIZE = 6;

public LottoTicket(List<LottoNumber> numbers) {
super(numbers);
}

@Override
protected void validateLottoNumbers(List<LottoNumber> numbers) {
if (numbers.size() != LOTTO_NUM_PICK_SIZE) {
throw new IllegalArgumentException("로또 티켓에 6개 숫자를 넣어야 합니다.");
}
}

public List<Integer> getNumbers() {
return numbers.stream()
.map(LottoNumber::get)
.collect(Collectors.toList());
}

public boolean isContain(LottoNumber number) {
return numbers.contains(number);
}

@Override
public boolean equals(Object o) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

이 LottoTicket의 equals는 어디에서 사용하는 거야?? 일단 실제 코드에는 사용하지않는 것 같고 사용한다면 테스트 코드에서 사용할 것 같은데 내 생각은 equals는 조심해서 사용해야 한다고 생각해.

LottoTicket이 같은 경우가 코드대로라면은 티켓이 들고있는 LottoNumber들이 같을 때 같다고 하게 되는데, 그렇게 되면 똑같은 번호로 산 로또 2개는 같은 Ticket 이라는 의미가 될것같아.

Set은 중복을 허용하지 않는데, Ticket을 Set에 넣는다고 가정했을때 같은 번호를 가지고있는 Ticket 2개를 넣게되면 Set에는 1개만 들어가게 될것같아.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

내생각에는 같은 번호의 로또 티켓이라면 같은 티켓이 맞다고 봐.
Set은 형말대로 "중복"으로 같은 숫자들을 가진 티켓들을 제거 하려고 쓰일텐데, 그렇다면 오히려 Equals를 상속받아서 중복을 제거하는게 맞다고봐. 그러니 같은 숫자들을 가지고있는 티켓을 2개 넣는다면 1개가 들어가는게 맞는거지.
같은 번호들을 갖는 티켓을 서로 다르게 취급할거면 애초에 List로 들고있는게 맞다고봐.

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LottoTicket that = (LottoTicket) o;
return Objects.equals(numbers, that.numbers);
}

@Override
public int hashCode() {
return Objects.hash(numbers);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.javabom.lotto.domain.ticket;

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

public class LottoTicketDispenser {

private static final int LOTTO_TICKET_PRICE = 1_000;

private final LottoNumberGenerator lottoNumberGenerator;

public LottoTicketDispenser(LottoNumberGenerator lottoNumberGenerator) {
this.lottoNumberGenerator = lottoNumberGenerator;
}

public LottoTickets getAutoTickets(Money inputMoney) {
int quantity = inputMoney.calculateQuantityPer(LOTTO_TICKET_PRICE);
List<LottoTicket> tickets = new ArrayList<>();
for (int i = 0; i < quantity; i++) {
tickets.add(new LottoTicket(lottoNumberGenerator.getLottoNumbers()));
}
return new LottoTickets(tickets);
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/javabom/lotto/domain/ticket/LottoTickets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.javabom.lotto.domain.ticket;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class LottoTickets {

private final List<LottoTicket> tickets;

public LottoTickets(List<LottoTicket> tickets) {
this.tickets = tickets;
}

public List<LottoTicket> get() {
return Collections.unmodifiableList(tickets);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LottoTickets that = (LottoTickets) o;
return Objects.equals(tickets, that.tickets);
}

@Override
public int hashCode() {
return Objects.hash(tickets);
}
}
Loading