-
Notifications
You must be signed in to change notification settings - Fork 3
[혁] 로또게임 #8
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
base: master
Are you sure you want to change the base?
[혁] 로또게임 #8
Changes from 8 commits
c7c6eff
f61cab6
339f14c
41e7fa3
dd38b73
6eee120
bfb1a47
40b2369
efe0fa1
97bf6f1
1cd3d44
9fb8981
a8c5835
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()); | ||
| } | ||
| } |
| 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) | ||
| .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 { | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 당첨 번호 중복 검사도 필요할것같아!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 당첨 번호와 보너스 번호가 중복되는지 유효성 검사 필요해보여! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 또 BasicLuckNumbers나 LottoTicket 같이 LottoNumbers 추상클래스를 상속하는 객체들을 생성하는 새로운 객체를 하나 만들어서 거기에 validation 로직을 두는건 어떨까??? 지금 로또 번호 개수 상수가 여러군데에서 중복되서 쓰이고 있어서 이런식으로 하면 어느정도 해결할 수 있을것으로 보여!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 밑에 말이 무슨말인지 잘 모르겠어. |
||
|
|
||
| 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); | ||
| } | ||
| } | ||
| 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() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
| } | ||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 나도 실수했던건데 Prize랑 Price!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?? 나도 지금 실수하고있는거야?? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
|
||
| } |
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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개만 들어가게 될것같아.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 내생각에는 같은 번호의 로또 티켓이라면 같은 티켓이 맞다고 봐. |
||
| 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); | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
하나로 할 수 있어보여!