-
Notifications
You must be signed in to change notification settings - Fork 12
[로또 게임 1단계] 리뷰 부탁드립니다 #11
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: jiwoo-kimm
Are you sure you want to change the base?
Changes from all commits
33a7d0d
e9d2001
05fbeba
538e6b8
96e6d63
fd24a8d
862c243
27424d2
39be046
c983156
f89c53e
27e69ac
9680ee3
8bd4e7f
3ee2d11
0f0a9aa
6cb70d4
dcf4842
c512869
a6ef7de
078bf69
4a83ad0
f8da3c5
a84a57e
016863a
3b93395
ddff79a
e0d1880
f4d2c40
eaac136
674ae96
a0173ea
1bf5325
ffed907
c48c8e1
2c6da43
3e67178
8d185da
60c4c70
2119239
41d2147
36c204b
1084404
2056c66
a44450b
fb57be6
a4809aa
4357c37
d1e6740
0f0371e
42ddc76
b65a98a
0f2c076
eda9666
ca56354
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 +1,30 @@ | ||
# java-lotto 게임 | ||
# java-Lotto 게임 | ||
|
||
# 구현 기능 목록 | ||
|
||
- [x] 구입금액 입력받기 | ||
- [x] 1000원 미만 입력 시 처리 | ||
- [x] 1000의 배수 아닌 값 입력 시 처리 | ||
- [x] 숫자가 아닌 값 (문자열) | ||
- [x] 정상값 처리 | ||
- [x] 공백 처리 | ||
- [x] 구매 개수 구하기 | ||
- [x] 랜덤 로또번호 구매 개수만큼 만들기 | ||
- [x] defaultNumberSet 1번만 생성되도록 변경 | ||
- [x] RandomLottoTest 상수 리팩토링 | ||
- [x] PurchaseCount의 1000 접근 | ||
- [x] 지난 주 당첨 번호 입력받기 | ||
- [x] WinningNumbers 멤버변수 ArrayList 클래스 확인 | ||
- [x] 숫자 개수 6개 확인 | ||
- [x] 숫자가 아닌 값 포함 | ||
- [x] 범위 (1~45) 확인 | ||
- [x] 공백 처리 | ||
- [x] 보너스 볼 입력받기 | ||
- [x] 당첨 통계 | ||
- [x] 당첨 조건을 enum 처리 | ||
- [x] 일치 개수 찾기 | ||
- [x] 5개 일치 시 보너스 볼 일치 여부 확인 | ||
- [x] 로또 당첨 개수 구하기 | ||
- [x] 당첨값의 합 구하기 | ||
- [x] 수익률 구하기 | ||
- [x] 결과 출력 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,13 @@ repositories { | |
dependencies { | ||
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0') | ||
testImplementation('org.assertj:assertj-core:3.15.0') | ||
|
||
compileOnly 'org.projectlombok:lombok:1.18.20' | ||
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. 넵! |
||
annotationProcessor 'org.projectlombok:lombok:1.18.20' | ||
|
||
testCompileOnly 'org.projectlombok:lombok:1.18.20' | ||
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20' | ||
|
||
} | ||
|
||
test { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package lotto; | ||
|
||
import lotto.controller.LottoController; | ||
import lotto.domain.dto.LottoResult; | ||
import lotto.domain.dto.PurchaseInput; | ||
import lotto.domain.dto.PurchaseResult; | ||
import lotto.domain.dto.WinningLottoInput; | ||
import lotto.exception.ExceptionMessage; | ||
import lotto.view.ConsoleInputView; | ||
import lotto.view.ConsoleOutputView; | ||
import lotto.view.View; | ||
|
||
public class LottoApplication { | ||
|
||
public static void main(String[] args) { | ||
View view = new View(new ConsoleInputView(), new ConsoleOutputView()); | ||
LottoController lottoController = new LottoController(); | ||
|
||
try { | ||
PurchaseInput purchaseInput = view.getPurchasePrice(); | ||
PurchaseResult purchaseResult = lottoController.purchase(purchaseInput); | ||
view.printLottoPurchaseResult(purchaseResult); | ||
|
||
WinningLottoInput winningLottoInput = view.getWinningLottoAndBonus(); | ||
LottoResult lottoResult = lottoController.calculateResult(purchaseResult, winningLottoInput); | ||
view.printLottoStatistics(lottoResult); | ||
} catch (NumberFormatException e) { | ||
view.printException(ExceptionMessage.NON_NUMBER_INPUT.getMessage()); | ||
} catch (IllegalArgumentException e) { | ||
view.printException(e.getMessage()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package lotto.controller; | ||
|
||
import lotto.domain.dto.LottoResult; | ||
import lotto.domain.dto.PurchaseInput; | ||
import lotto.domain.dto.PurchaseResult; | ||
import lotto.domain.dto.WinningLottoInput; | ||
import lotto.service.LottoService; | ||
|
||
public class LottoController { | ||
|
||
private final LottoService lottoService; | ||
|
||
public LottoController() { | ||
this.lottoService = new LottoService(); | ||
} | ||
|
||
public PurchaseResult purchase(PurchaseInput purchaseInput) throws IllegalArgumentException { | ||
return lottoService.purchase(purchaseInput); | ||
} | ||
|
||
public LottoResult calculateResult(PurchaseResult purchaseResult, WinningLottoInput winningLottoInput) throws IllegalArgumentException { | ||
return lottoService.calculateResult(purchaseResult, winningLottoInput); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package lotto.domain; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import static lotto.exception.ExceptionMessage.DUPLICATE_LOTTO_NUMBER_INPUT_FOR_LOTTO; | ||
import static lotto.exception.ExceptionMessage.INVALID_LENGTH_INPUT_FOR_LOTTO; | ||
|
||
public class Lotto { | ||
|
||
public static final int PRICE = 1000; | ||
public static final int LOTTO_NUMBER_SIZE = 6; | ||
|
||
@Getter | ||
private final List<LottoNumber> lottoNumbers; | ||
|
||
public Lotto(List<Integer> numbers) throws IllegalArgumentException { | ||
validate(numbers); | ||
|
||
numbers.sort(Integer::compare); | ||
this.lottoNumbers = Collections.unmodifiableList(numbers.stream() | ||
.map(LottoNumber::new) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
private void validate(List<Integer> numbers) throws IllegalArgumentException { | ||
if (numbers.size() != Lotto.LOTTO_NUMBER_SIZE) { | ||
throw new IllegalArgumentException(INVALID_LENGTH_INPUT_FOR_LOTTO.getMessage()); | ||
} | ||
if (numbers.size() != numbers.stream().distinct().count()) { | ||
throw new IllegalArgumentException(DUPLICATE_LOTTO_NUMBER_INPUT_FOR_LOTTO.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Lotto lotto = (Lotto) o; | ||
return Objects.equals(lottoNumbers, lotto.lottoNumbers); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(lottoNumbers); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package lotto.domain; | ||
|
||
public class LottoMatcher { | ||
|
||
private final WinningLotto winningLotto; | ||
private final LottoSet lottoSet; | ||
|
||
public LottoMatcher(WinningLotto winningLotto, LottoSet lottoSet) { | ||
this.winningLotto = winningLotto; | ||
this.lottoSet = lottoSet; | ||
} | ||
|
||
public PrizeCount countPrizes() { | ||
PrizeCount prizeCount = new PrizeCount(); | ||
for (Lotto lotto : lottoSet.getLottoSet()) { | ||
Prize prize = Prize.getMatchPrize(getMatchNumbersCount(lotto), isBonusMatch(lotto)); | ||
prizeCount.addPrize(prize); | ||
} | ||
return prizeCount; | ||
} | ||
|
||
private int getMatchNumbersCount(Lotto targetLotto) { | ||
return (int) targetLotto.getLottoNumbers().stream() | ||
.filter(lottoNumber -> winningLotto.getLottoNumbers().contains(lottoNumber)) | ||
.count(); | ||
} | ||
|
||
private boolean isBonusMatch(Lotto targetLotto) { | ||
return targetLotto.getLottoNumbers().contains(winningLotto.getBonusNumber()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package lotto.domain; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.Objects; | ||
|
||
import static lotto.exception.ExceptionMessage.OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER; | ||
|
||
public class LottoNumber { | ||
|
||
public static final int LOWER_BOUND = 1; | ||
public static final int UPPER_BOUND = 45; | ||
|
||
@Getter | ||
private final int lottoNumber; | ||
|
||
public LottoNumber(int lottoNumber) throws IllegalArgumentException { | ||
validate(lottoNumber); | ||
|
||
this.lottoNumber = lottoNumber; | ||
} | ||
|
||
private void validate(int lottoNumber) throws IllegalArgumentException { | ||
if (isOutOfBound(lottoNumber)) { | ||
throw new IllegalArgumentException(OUT_OF_BOUND_INPUT_FOR_LOTTO_NUMBER.getMessage()); | ||
} | ||
} | ||
|
||
private boolean isOutOfBound(int lottoNumber) { | ||
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. 👍 |
||
return lottoNumber < LOWER_BOUND || lottoNumber > UPPER_BOUND; | ||
} | ||
|
||
public boolean isGreaterThan(LottoNumber winningNumber) { | ||
return this.getLottoNumber() > winningNumber.getLottoNumber(); | ||
} | ||
|
||
@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; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class LottoSet { | ||
|
||
@Getter | ||
private final Set<Lotto> lottoSet; | ||
|
||
public LottoSet(Set<Lotto> lottoSet) { | ||
this.lottoSet = Collections.unmodifiableSet(lottoSet); | ||
} | ||
|
||
public LottoSet(PurchaseCount purchaseCount) { | ||
this(generateRandomLottoSet(purchaseCount)); | ||
} | ||
|
||
private static Set<Lotto> generateRandomLottoSet(PurchaseCount purchaseCount) { | ||
Set<Lotto> lottoSet = new HashSet<>(); | ||
while (lottoSet.size() < purchaseCount.getPurchaseCount()) { | ||
lottoSet.add(new RandomLotto()); | ||
} | ||
return lottoSet; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package lotto.domain; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
public class LottoStatistics { | ||
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. 통계를 담는 객체를 만든 것 아주 좋습니다 👍 |
||
|
||
@Getter | ||
private final PrizeCount prizeCount; | ||
private final PurchaseCount purchaseCount; | ||
|
||
@Builder | ||
public LottoStatistics(PrizeCount prizeCount, PurchaseCount purchaseCount) { | ||
this.prizeCount = prizeCount; | ||
this.purchaseCount = purchaseCount; | ||
} | ||
|
||
public double calculateProfitRate() { | ||
return (double) Prize.sumOfPrizeMoney(prizeCount) | ||
/ (purchaseCount.getPurchaseCount() * Lotto.PRICE); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package lotto.domain; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum Prize { | ||
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. Enum 활용 👏 |
||
|
||
FIRST(6, false, 2000000000), | ||
SECOND(5, true, 30000000), | ||
THIRD(5, false, 1500000), | ||
FOURTH(4, false, 50000), | ||
FIFTH(3, false, 5000), | ||
LOSE(2, false, 0); | ||
|
||
private final int matchNumbersCount; | ||
private final boolean isBonus; | ||
private final long prizeMoney; | ||
|
||
Prize(int matchNumbersCount, boolean isBonus, long prizeMoney) { | ||
this.matchNumbersCount = matchNumbersCount; | ||
this.isBonus = isBonus; | ||
this.prizeMoney = prizeMoney; | ||
} | ||
|
||
public static Prize getMatchPrize(int matchNumbersCount, boolean isBonus) { | ||
if (matchNumbersCount <= LOSE.matchNumbersCount) { | ||
return LOSE; | ||
} | ||
if (matchNumbersCount == FIFTH.matchNumbersCount) { | ||
return FIFTH; | ||
} | ||
if (matchNumbersCount == FOURTH.matchNumbersCount) { | ||
return FOURTH; | ||
} | ||
if (matchNumbersCount == FIRST.matchNumbersCount) { | ||
return FIRST; | ||
} | ||
return dissolveSecondOrThird(isBonus); | ||
} | ||
|
||
private static Prize dissolveSecondOrThird(boolean isBonus) { | ||
if (isBonus) { | ||
return Prize.SECOND; | ||
} | ||
return Prize.THIRD; | ||
} | ||
|
||
public static long sumOfPrizeMoney(PrizeCount prizeCount) { | ||
return prizeCount.getFirst() * FIRST.prizeMoney | ||
+ prizeCount.getSecond() * SECOND.prizeMoney | ||
+ prizeCount.getThird() * THIRD.prizeMoney | ||
+ prizeCount.getFourth() * FOURTH.prizeMoney | ||
+ prizeCount.getFifth() * FIFTH.prizeMoney; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package lotto.domain; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PrizeCount { | ||
|
||
private int first; | ||
private int second; | ||
private int third; | ||
private int fourth; | ||
private int fifth; | ||
|
||
public void addPrize(Prize prize) { | ||
if (prize == Prize.FIRST) { | ||
first++; | ||
return; | ||
} | ||
if (prize == Prize.SECOND) { | ||
second++; | ||
return; | ||
} | ||
if (prize == Prize.THIRD) { | ||
third++; | ||
return; | ||
} | ||
if (prize == Prize.FOURTH) { | ||
fourth++; | ||
return; | ||
} | ||
if (prize == Prize.FIFTH) { | ||
fifth++; | ||
} | ||
} | ||
} |
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.
구현 기능 목록 작성 👍