-
Notifications
You must be signed in to change notification settings - Fork 6
로또 1단계 미션 제출 #6
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: chae-heechan
Are you sure you want to change the base?
로또 1단계 미션 제출 #6
Changes from 34 commits
1756707
db1647d
673ac2e
5e62790
73db742
b74d509
5863fc8
3406e4a
1318d7a
7fa1b41
380b00e
6aea022
2e761cf
40543c8
983c002
e1efbaf
b0db442
f5c9577
4b04a8b
c0d6281
60fc2a0
a01c323
77cddd7
f2068d1
1e16e0d
f13151d
0f8bc09
09c6fb4
eef113d
f9638c7
13f3aac
ac4f5f2
a87480f
7cfa859
2e40f10
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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ apply plugin: 'java' | |
| apply plugin: 'eclipse' | ||
|
|
||
| version = '1.0.0' | ||
| sourceCompatibility = 1.8 | ||
| sourceCompatibility = 14 | ||
|
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. 프로젝트의 자바 버전을 올린 이유가 있을까요? |
||
|
|
||
| repositories { | ||
| mavenCentral() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package lotto; | ||
|
|
||
| import lotto.domain.*; | ||
| import lotto.domain.factory.Issuer; | ||
| import lotto.domain.lotto.LottoTickets; | ||
| import lotto.domain.lotto.WinningNumber; | ||
| import lotto.domain.result.GameResults; | ||
| import lotto.ui.Printer; | ||
| import lotto.ui.Receiver; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class LottoApplication { | ||
| private static final String WINNING_NUMBER_INPUT_DELIMITER = ", "; | ||
|
|
||
| private final Printer printer; | ||
| private final Receiver receiver; | ||
|
|
||
| public LottoApplication(Printer printer, Receiver receiver) { | ||
| this.printer = printer; | ||
| this.receiver = receiver; | ||
| } | ||
|
|
||
| public void run() { | ||
| int purchaseAmount = receivePurchaseAmount(); | ||
| LottoTickets lottoTickets = Issuer.issueLottoTickets(purchaseAmount); | ||
| printer.printIssuedTickets(lottoTickets); | ||
|
|
||
| WinningNumber winningNumber = new WinningNumber(receiveWinningNumber(), receiveBonusNumber()); | ||
| GameResults gameResults = lottoTickets.matchNumbers(winningNumber); | ||
| Statistics statistics = new Statistics(gameResults, purchaseAmount); | ||
| printer.printStatistics(statistics); | ||
| } | ||
|
|
||
| private int receivePurchaseAmount() { | ||
| printer.requestPurchaseAmount(); | ||
| String purchaseAmount = receiver.receiveLine(); | ||
| return Integer.parseInt(purchaseAmount); | ||
| } | ||
|
|
||
| private List<Integer> receiveWinningNumber() { | ||
| printer.requestWinningNumber(); | ||
| String winningNumber = receiver.receiveLine(); | ||
|
|
||
| return Arrays.stream(winningNumber.split(WINNING_NUMBER_INPUT_DELIMITER)) | ||
| .map(Integer::valueOf) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private int receiveBonusNumber() { | ||
| printer.requestBonusNumber(); | ||
| String bonusNumber = receiver.receiveLine(); | ||
| return Integer.parseInt(bonusNumber); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| LottoApplication app = new LottoApplication(new Printer(), new Receiver(System.in)); | ||
| app.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package lotto.domain; | ||
| public class MatchCount { | ||
| private final int matchCount; | ||
| private final boolean isBonusMatch; | ||
| public MatchCount(int matchCount, boolean isBonusMatch) { | ||
| this.matchCount = matchCount; | ||
| this.isBonusMatch = isBonusMatch; | ||
| } | ||
| public boolean isUnderThree() { | ||
| return matchCount < 3; | ||
|
Member
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. 전역상수로 빼는 방향은 어떠신가요??? 예를들어 로또의 규칙이 바뀌거나 범위가 1~45에서 바뀔 경우 상수로 빼는것도 좋을 것 같아요!!! |
||
| } | ||
| public boolean isMatchThree() { | ||
| return matchCount == 3; | ||
| } | ||
| public boolean isMatchFour() { | ||
| return matchCount == 4; | ||
| } | ||
| public boolean isMatchFiveWithoutBonus() { | ||
| return matchCount == 5 && !isBonusMatch; | ||
| } | ||
| public boolean isMatchFiveWithBonus() { | ||
| return matchCount == 5 && isBonusMatch; | ||
| } | ||
| public boolean isAllMatch() { | ||
| return matchCount == 6; | ||
|
Member
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. isAllMatch 와 matchCount == 6 으로 했을 때 나중에 수정을 할 상황이 올 것 같아요!!! |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package lotto.domain; | ||
|
|
||
| import lotto.domain.dto.WinningResult; | ||
| import lotto.domain.result.GameResult; | ||
| import lotto.domain.result.GameResults; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Statistics { | ||
| private static final int IS_EQUAL_RATE = 1; | ||
|
|
||
| private final GameResults gameResults; | ||
| private final int purchaseAmount; | ||
|
|
||
| public Statistics(GameResults gameResults, int purchaseAmount) { | ||
| this.gameResults = gameResults; | ||
| this.purchaseAmount = purchaseAmount; | ||
| } | ||
|
|
||
| public List<WinningResult> winningResults() { | ||
| return GameResult.valuesWithoutUnderThreeMatched() | ||
| .stream() | ||
| .map(gameResult -> new WinningResult(gameResults.count(gameResult), gameResult)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public double calculateEarningRate() { | ||
| return gameResults.calculatePrize() / purchaseAmount; | ||
| } | ||
|
|
||
| public boolean isProfit(double earningRate) { | ||
| return earningRate > IS_EQUAL_RATE; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package lotto.domain.dto; | ||
|
|
||
| import lotto.domain.result.GameResult; | ||
|
|
||
| public class WinningResult { | ||
| private final int numberOfWinners; | ||
| private final GameResult gameResult; | ||
| public WinningResult(int numberOfWinners, GameResult gameResult) { | ||
|
Member
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. numberOfWinners 라고 하니까 살짝 이해가 안될 수도 있을 것 같아요!!! 읽었을 때 우승자의 수?? 이렇게 해석이 될 수도 있지 않을까 하는 조심스러운 리뷰 달겠습니다!!! 저의 지극히 개인적인 생각이옵니다! |
||
| this.numberOfWinners = numberOfWinners; | ||
| this.gameResult = gameResult; | ||
| } | ||
| public int getNumberOfWinners() { | ||
| return numberOfWinners; | ||
| } | ||
| public GameResult getGameResult() { | ||
| return gameResult; | ||
| } | ||
| } | ||
|
Member
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. 개행을 추가하면 될 거에요!! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package lotto.domain.factory; | ||
| import lotto.domain.lotto.LottoTicket; | ||
| import lotto.domain.lotto.LottoTickets; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| public class Issuer { | ||
| private static final int TICKET_PRICE = 1000; | ||
| private static final int MINIMUM_NUMBER_OF_TICKETS = 1; | ||
| private static final String MONEY_AMOUNT_EXCEPTION_MESSAGE = "금액은 " + TICKET_PRICE + "원 이상으로 입력해주세요."; | ||
| public static LottoTickets issueLottoTickets(int money) { | ||
| int numberOfTickets = money / TICKET_PRICE; | ||
| validate(numberOfTickets); | ||
| List<LottoTicket> lottoTickets = new ArrayList<>(); | ||
| for (int i = 0; i < numberOfTickets; i++) { | ||
| lottoTickets.add(new LottoTicket()); | ||
| } | ||
| return new LottoTickets(lottoTickets); | ||
| } | ||
| private static void validate(int numberOfTickets) { | ||
| if (numberOfTickets < MINIMUM_NUMBER_OF_TICKETS) { | ||
| throw new IllegalArgumentException(MONEY_AMOUNT_EXCEPTION_MESSAGE); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package lotto.domain.lotto; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| public class LottoNumber { | ||
| private static final int LOTTO_NUMBER_LOWER_BOUND = 1; | ||
| private static final int LOTTO_NUMBER_UPPER_BOUND = 45; | ||
| private static final String LOTTO_NUMBER_RANGE_EXCEPTION_MESSAGE = | ||
| "로또 번호는 " + LOTTO_NUMBER_LOWER_BOUND + " 이상, " + LOTTO_NUMBER_UPPER_BOUND + " 이하여야 합니다."; | ||
| private final int lottoNumber; | ||
| public LottoNumber(int lottoNumber) { | ||
| validate(lottoNumber); | ||
| this.lottoNumber = lottoNumber; | ||
| } | ||
| private void validate(int lottoNumber) { | ||
| if (!isInRange(lottoNumber)) { | ||
| throw new IllegalArgumentException(LOTTO_NUMBER_RANGE_EXCEPTION_MESSAGE); | ||
| } | ||
| } | ||
| private boolean isInRange(int lottoNumber) { | ||
| return lottoNumber >= LOTTO_NUMBER_LOWER_BOUND && lottoNumber <= LOTTO_NUMBER_UPPER_BOUND; | ||
| } | ||
| public static List<LottoNumber> range() { | ||
| return IntStream.range(LOTTO_NUMBER_LOWER_BOUND, LOTTO_NUMBER_UPPER_BOUND + 1) | ||
| .boxed() | ||
| .map(LottoNumber::new) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| public int getLottoNumber() { | ||
| return lottoNumber; | ||
| } | ||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| LottoNumber that = (LottoNumber) o; | ||
| return lottoNumber == that.lottoNumber; | ||
| } | ||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(lottoNumber); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package lotto.domain.lotto; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| public class LottoTicket { | ||
|
Member
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 NUMBER_OF_LOTTO_NUMBERS = 6; | ||
| private final List<LottoNumber> lottoNumbers; | ||
| public LottoTicket() { | ||
| List<LottoNumber> lottoNumbers = LottoNumber.range(); | ||
| lottoNumbers = generateLottoNumbers(lottoNumbers); | ||
| this.lottoNumbers = new ArrayList<>(lottoNumbers); | ||
| } | ||
| private List<LottoNumber> generateLottoNumbers(List<LottoNumber> lottoNumbers) { | ||
| Collections.shuffle(lottoNumbers); | ||
| lottoNumbers = lottoNumbers.stream() | ||
| .limit(NUMBER_OF_LOTTO_NUMBERS) | ||
| .sorted(Comparator.comparing(LottoNumber::getLottoNumber)) | ||
| .collect(Collectors.toList()); | ||
| return lottoNumbers; | ||
| } | ||
| public boolean contains(LottoNumber lottoNumber) { | ||
|
Member
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. 이건 제가 궁금해서 그런 부분인데 contains 라는 method가 기존에 있는데 같은 이름의 method 를 구현하는게 괜찮은 방향인지 모르겠어요!!! |
||
| return lottoNumbers.contains(lottoNumber); | ||
| } | ||
| public List<LottoNumber> getLottoNumbers() { | ||
| return lottoNumbers; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package lotto.domain.lotto; | ||
| import lotto.domain.result.GameResult; | ||
| import lotto.domain.result.GameResults; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| public class LottoTickets { | ||
| private final List<LottoTicket> lottoTickets; | ||
| public LottoTickets(List<LottoTicket> lottoTickets) { | ||
| this.lottoTickets = new ArrayList<>(lottoTickets); | ||
| } | ||
| public GameResults matchNumbers(WinningNumber winningNumber) { | ||
| List<GameResult> results = lottoTickets.stream() | ||
| .map(winningNumber::matchNumbers) | ||
| .map(GameResult::evaluate) | ||
| .collect(Collectors.toList()); | ||
| return new GameResults(results); | ||
| } | ||
| public List<LottoTicket> getLottoTickets() { | ||
| return lottoTickets; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package lotto.domain.lotto; | ||
| import lotto.domain.MatchCount; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| public class WinningNumber { | ||
| private static final String BONUS_NUMBER_EXCEPTION_MESSAGE = "보너스 번호는 당첨 번호와 다른 번호여야 합니다."; | ||
| private final List<LottoNumber> winningNumbers; | ||
| private final LottoNumber bonusNumber; | ||
| public WinningNumber(List<Integer> winningNumbers, int bonusNumber) { | ||
| validate(winningNumbers, bonusNumber); | ||
| this.winningNumbers = winningNumbers.stream() | ||
| .map(LottoNumber::new) | ||
| .collect(Collectors.toUnmodifiableList()); | ||
| this.bonusNumber = new LottoNumber(bonusNumber); | ||
| } | ||
| private void validate(List<Integer> winningNumbers, int bonusNumber) { | ||
| if (winningNumbers.contains(bonusNumber)) { | ||
| throw new IllegalArgumentException(BONUS_NUMBER_EXCEPTION_MESSAGE); | ||
| } | ||
| } | ||
| public MatchCount matchNumbers(LottoTicket lottoTicket) { | ||
| int matchCount = Long.valueOf( | ||
| winningNumbers.stream() | ||
| .filter(lottoTicket::contains) | ||
| .count() | ||
| ).intValue(); | ||
| boolean isBonusMatch = lottoTicket.contains(bonusNumber); | ||
| return new MatchCount(matchCount, isBonusMatch); | ||
| } | ||
| } |
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.
기능 목록 👍🏻