Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
[![우아한테크코스](https://github.com/user-attachments/assets/488b7a20-5b07-462e-afdb-71aac9e68b64)](https://www.woowacourse.io/)

# java-planetlotto-precourse

## 📌 요구사항 요약

1 ~ 30 범위의 5개 숫자로 이뤄진 로또를 구현한다.

**문제 분해, 설계, TDD를 코드에 녹여내는 게 중요 ⭐**

---

## 👊 도전 과제

### 도전 방향: 리팩터링

- [x] Zero-Trust: 모든 입력 경우의 수에 대비해 최대한의 입력 검증 로직을 만든다.
- [x] 가능한 모든 도메인 로직 유닛 테스트를 작성한다.
- [x] 일급 컬렉션을 사용하고 VO는 record를 사용한다.
- [x] 가능한 예외 상황을 `ErrorMessage`에 모두 정의해두고 여기서 꺼내 사용한다.

---

### 💡 스스로 판단한 규칙

- 건강을 위해 `복권및복권기금법시행령 제3조`에 따라 1회 최대 10만원까지만 허용한다.
- 로또의 취지에 맞게, 구입 금액에서 잔돈이 생길 경우 자동으로 기부된다 ^^
- 생성된 로또 번호가 중복인 경우가 존재할 수 있다.
- [ ] 왜 행성이지?
<br/>

---

## 📝 구현할 기능 목록

### 로또 구입 금액을 입력받는 기능

- [x] 구입 금액 입력 요청 문구를 출력한다.
- [x] 구입 금액을 입력받는다.
- [x] 로또 1장 가격인 500원보다 적을 경우 재입력받는다.
- [x] 10만원 초과일 경우, 법을 안내하고 재입력받는다.
- [x] 로또 1장 가격인 500원으로 떨어지지 않는 경우, 잔돈은 기부된다.

### 로또를 발행하는 기능

- [x] `edu.missionutils.Randoms`의 `pickUniqueNumbersInRange()`를 활용한다.
- [x] 로또의 `최소 숫자`와 `최대 숫자`, `로또 번호 개수`를 로또 정보에 맞게 입력한다.
- [x] 구입 금액에 맞게 로또를 발행하고 `Lottos`로 반환한다.

### 구입한 로또의 발행 내역을 출력하는 기능

- [x] `로또 발행 개수`를 출력한다.
- [x] 발행된 로또들의 번호를 `오름차순`으로 출력한다.

### 당첨 번호와 보너스 번호를 입력받고 당첨 로또 정보를 만드는 기능

- [x] `당첨 번호` 입력 요청 문구를 출력한다.
- [x] `당첨 번호`를 입력받는다.
- [x] `1`부터 `30`까지의 숫자인지 검증한다.
- [x] 중복이 없는지 검증한다.
- [x] 총 `5개`인지 검증한다.
- [x] `, ` 구분자 형식이 맞는지 검증한다.
- [x] `보너스 번호` 입력 요청 문구를 출력한다.
- [x] `보너스 번호`를 입력받는다.
- [x] `1`부터 `30`까지의 숫자인지 검증한다.
- [x] `당첨 번호`와 중복되지 않는지 검증한다.
- [x] `당첨 번호`들과 `보너스 번호`를 가진 당첨 로또 정보 객체를 만든다.

### 당첨 여부를 판별하고, 당첨 통계를 출력하는 기능

- [x] `발행된 로또들`을 `당첨 로또 정보`와 비교해 당첨 결과를 낸다.
- [x] 통계에는 집계되지 않는 범위는 따로 저장한다.
- [x] `1등`부터 `5등`까지, 그리고 `0개 일치`인 경우를 순서대로 출력한다.
- [x] `일치 정보`와 `당첨 금액`, `해당 개수`를 출력한다.
4 changes: 3 additions & 1 deletion src/main/java/planetlotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package planetlotto;

import planetlotto.controller.PlanetLottoController;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
new PlanetLottoController().run();
}
}
74 changes: 74 additions & 0 deletions src/main/java/planetlotto/controller/PlanetLottoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package planetlotto.controller;

import java.util.List;
import planetlotto.domain.BonusNumber;
import planetlotto.domain.Lotto;
import planetlotto.domain.LottoGenerator;
import planetlotto.domain.LottoResult;
import planetlotto.domain.Lottos;
import planetlotto.domain.PurchaseAmount;
import planetlotto.domain.WinningLottoInfo;
import planetlotto.view.InputView;
import planetlotto.view.OutputView;

public class PlanetLottoController {

public void run() {
Lottos lottos = generateLotto(getPurchaseAmount());
WinningLottoInfo winningLottoInfo = getWinningLottoInfo();
showWinningStatistics(lottos, winningLottoInfo);
}

private PurchaseAmount getPurchaseAmount() {
while (true) {
try {
int amount = InputView.askAmount();
return PurchaseAmount.of(amount);
} catch (IllegalArgumentException e) {
OutputView.printErrorMessage(e.getMessage());
}
}
}


private Lottos generateLotto(PurchaseAmount purchaseAmount) {
Lottos lottos = LottoGenerator.generateLottos(purchaseAmount);
OutputView.printPurchasedLottos(lottos.getLottos());
return lottos;
}


private WinningLottoInfo getWinningLottoInfo() {
Lotto winningLotto = getWinningLotto();
BonusNumber bonusNumber = getBonusNumber(winningLotto);
return new WinningLottoInfo(winningLotto, bonusNumber);
}

private Lotto getWinningLotto() {
while (true) {
try {
List<Integer> numbers = InputView.askWinningLotto();
return Lotto.of(numbers);
} catch (IllegalArgumentException e) {
OutputView.printErrorMessage(e.getMessage());
}
}
}

private BonusNumber getBonusNumber(Lotto winningLotto) {
while (true) {
try {
int number = InputView.askBonusNumber();
return BonusNumber.of(winningLotto, number);
} catch (IllegalArgumentException e) {
OutputView.printErrorMessage(e.getMessage());
}
}
}


private void showWinningStatistics(Lottos lottos, WinningLottoInfo winningLottoInfo) {
LottoResult lottoResult = lottos.getLottoResult(winningLottoInfo);
OutputView.printResult(lottoResult.countsByRank());
}
}
27 changes: 27 additions & 0 deletions src/main/java/planetlotto/domain/BonusNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package planetlotto.domain;

import planetlotto.exception.ErrorMessage;

public class BonusNumber {
private final int bonusNumber;

private BonusNumber(int bonusNumber) {
this.bonusNumber = bonusNumber;
}

public static BonusNumber of(Lotto winningLotto, int number) {
if (number < Lotto.MIN_NUMBER || Lotto.MAX_NUMBER < number) {
throw new IllegalArgumentException(ErrorMessage.OUT_Of_LOTTO_BOUNDS.getMessage());
}

if (winningLotto.isContain(number)) {
throw new IllegalArgumentException(ErrorMessage.DUPLICATE_WINNING_NUMBER.getMessage());
}

return new BonusNumber(number);
}

public int getBonusNumber() {
return bonusNumber;
}
}
59 changes: 59 additions & 0 deletions src/main/java/planetlotto/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package planetlotto.domain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import planetlotto.exception.ErrorMessage;

public class Lotto {
public static final int LOTTO_SIZE = 5;
public static final int MIN_NUMBER = 1;
public static final int MAX_NUMBER = 30;
public static final int LOTTO_PRICE = 500;
public static final int MAX_PURCHASE_AMOUNT = 100_000;

private final Set<Integer> numbers;

private Lotto(Set<Integer> numbers) {
this.numbers = numbers;
}

public static Lotto of(List<Integer> numbers) {
validateLottoBounds(numbers);
validateLottoSize(numbers);
Set<Integer> numbersNoDuplicate = new HashSet<>(numbers);
validateDuplicated(numbersNoDuplicate);
return new Lotto(numbersNoDuplicate);
}

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

public List<Integer> getNumbers() {
List<Integer> numberList = new ArrayList<>(numbers);
numberList.sort(null);
return numberList;
}

private static void validateLottoBounds(List<Integer> numbers) {
for (Integer number : numbers) {
if (number < MIN_NUMBER || MAX_NUMBER < number) {
throw new IllegalArgumentException(ErrorMessage.OUT_Of_LOTTO_BOUNDS.getMessage());
}
}
}

private static void validateLottoSize(List<Integer> numbers) {
if (numbers.size() != LOTTO_SIZE) {
throw new IllegalArgumentException(ErrorMessage.INVALID_LOTTO_COUNT.getMessage());
}
}

private static void validateDuplicated(Set<Integer> numbersNoDuplicate) {
if (numbersNoDuplicate.size() != LOTTO_SIZE) {
throw new IllegalArgumentException(ErrorMessage.DUPLICATE_LOTTO_NUMBER.getMessage());
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/planetlotto/domain/LottoGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package planetlotto.domain;

import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.List;

public class LottoGenerator {

public static Lottos generateLottos(PurchaseAmount amount) {
int lottoCount = amount.getAmount() / Lotto.LOTTO_PRICE;

List<Lotto> lottos = new ArrayList<>();
for (int i = 0; i < lottoCount; i++) {
List<Integer> randomNumbers = Randoms.pickUniqueNumbersInRange(Lotto.MIN_NUMBER, Lotto.MAX_NUMBER,
Lotto.LOTTO_SIZE);
lottos.add(Lotto.of(randomNumbers));
}
return new Lottos(lottos);
}
}
35 changes: 35 additions & 0 deletions src/main/java/planetlotto/domain/LottoRank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package planetlotto.domain;

public enum LottoRank {
FIRST(1, 5, false),
SECOND(2, 4, true),
THIRD(3, 4, false),
FOURTH(4, 3, true),
FIFTH(5, 2, true),
MISS_ALL(0, 0, false),
MISS_SOME(-1, -1, false);

private final int rankKey;
private final int matchWinningCount;
private final boolean matchBonus;

LottoRank(int rankKey, int matchWinningCount, boolean matchBonus) {
this.rankKey = rankKey;
this.matchWinningCount = matchWinningCount;
this.matchBonus = matchBonus;
}

public static LottoRank of(int matchWinningCount, boolean matchBonus) {
for (LottoRank rank : LottoRank.values()) {
if (rank.matchWinningCount == matchWinningCount
&& rank.matchBonus == matchBonus) {
return rank;
}
}
return MISS_SOME;
}

public int getRankKey() {
return rankKey;
}
}
8 changes: 8 additions & 0 deletions src/main/java/planetlotto/domain/LottoResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package planetlotto.domain;

import java.util.Map;

public record LottoResult(
Map<Integer, Integer> countsByRank
) {
}
42 changes: 42 additions & 0 deletions src/main/java/planetlotto/domain/Lottos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package planetlotto.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Lottos {
private final List<Lotto> lottos;

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

public List<List<Integer>> getLottos() {
List<List<Integer>> lottosNumbers = new ArrayList<>();
for (Lotto lotto : lottos) {
lottosNumbers.add(lotto.getNumbers());
}
return lottosNumbers;
}

public LottoResult getLottoResult(WinningLottoInfo winningLottoInfo) {
List<Integer> winningNumbers = winningLottoInfo.winningLotto().getNumbers();
int bonusNumber = winningLottoInfo.bonusNumber().getBonusNumber();

Map<Integer, Integer> countsByRank = new HashMap<>();
for (Lotto lotto : lottos) {
boolean matchBonus = lotto.isContain(bonusNumber);
LottoRank lottoRank = getLottoRank(lotto, winningNumbers, matchBonus);
int previousRankCount = countsByRank.getOrDefault(lottoRank.getRankKey(), 0);
countsByRank.put(lottoRank.getRankKey(), previousRankCount + 1);
}
return new LottoResult(countsByRank);
}

private static LottoRank getLottoRank(Lotto lotto, List<Integer> winningNumbers, boolean matchBonus) {
List<Integer> lottoNumbers = lotto.getNumbers();
lottoNumbers.retainAll(winningNumbers);
return LottoRank.of(lottoNumbers.size(), matchBonus);
}
}
33 changes: 33 additions & 0 deletions src/main/java/planetlotto/domain/PurchaseAmount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package planetlotto.domain;

import planetlotto.exception.ErrorMessage;

public class PurchaseAmount {
private final int amount;

private PurchaseAmount(int amount) {
this.amount = amount;
}

public static PurchaseAmount of(int amount) {
validateMinimumAmount(amount);
validateOver10K(amount);
return new PurchaseAmount(amount);
}

public int getAmount() {
return amount;
}

private static void validateOver10K(int amount) {
if (Lotto.MAX_PURCHASE_AMOUNT < amount) {
throw new IllegalArgumentException(ErrorMessage.OVER_THE_100K.getMessage());
}
}

private static void validateMinimumAmount(int amount) {
if (amount < Lotto.LOTTO_PRICE) {
throw new IllegalArgumentException(ErrorMessage.LESS_THAN_LOTTO_PRICE.getMessage());
}
}
}
Loading