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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# java-lotto-precourse

## 로또
64 changes: 64 additions & 0 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,71 @@
package lotto;

import camp.nextstep.edu.missionutils.Console;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
}

private static int getPurchaseAmount() {
while (true) {
try {
System.out.println("구입금액을 입력해 주세요.");
int amount = Integer.parseInt(Console.readLine());
if (amount % 1000 != 0) {
throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원 단위여야 합니다.");
}
return amount;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

private static List<Integer> getWinningNumbers() {
while (true) {
try {
System.out.println("당첨 번호를 입력해 주세요.");
String[] input = Console.readLine().split(",");
List<Integer> numbers = Arrays.stream(input)
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toList());
if (numbers.size() != 6) {
throw new IllegalArgumentException("[ERROR] 당첨 번호는 6개여야 합니다.");
}
if (numbers.stream().anyMatch(n -> n < 1 || n > 45)) {
throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.");
}
return numbers;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

private static int getBonusNumber() {
while (true) {
try {
System.out.println("보너스 번호를 입력해 주세요.");
int bonusNumber = Integer.parseInt(Console.readLine());
if (bonusNumber < 1 || bonusNumber > 45) {
throw new IllegalArgumentException("[ERROR] 보너스 번호는 1부터 45 사이의 숫자여야 합니다.");
}
return bonusNumber;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

private static void printLottos(List<Lotto> lottos) {
System.out.printf("%d개를 구매했습니다.%n", lottos.size());
for (Lotto lotto : lottos) {
System.out.println(lotto.getNumbers());
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/lotto/Lotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ private void validate(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다.");
}
if (numbers.stream().anyMatch(num -> num < 1 || num > 45)) {
throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.");
}
if (numbers.stream().distinct().count() != 6) {
throw new IllegalArgumentException("[ERROR] 로또 번호는 중복되지 않아야 합니다.");
}
}

// TODO: 추가 기능 구현
public List<Integer> getNumbers() {
return numbers;
}
}
31 changes: 31 additions & 0 deletions src/main/java/lotto/LottoMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lotto;

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

public class LottoMachine {
public static final int LOTTO_PRICE = 1000;

public List<Lotto> generateLottos(int count) {
List<Lotto> lottos = new ArrayList<>();
for (int i = 0; i < count; i++) {
List<Integer> numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6);
lottos.add(new Lotto(numbers));
}
return lottos;
}

public WinningResult checkWinningResults(List<Lotto> userLottos, Lotto winningLotto, int bonusNumber) {
WinningResult result = new WinningResult();
for (Lotto userLotto : userLottos) {
int matchCount = (int) userLotto.getNumbers().stream()
.filter(winningLotto.getNumbers()::contains)
.count();
boolean bonusMatch = userLotto.getNumbers().contains(bonusNumber);
Rank rank = Rank.valueOf(matchCount, bonusMatch);
result.addResult(rank);
}
return result;
}
}
35 changes: 35 additions & 0 deletions src/main/java/lotto/Rank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lotto;

public enum Rank {
FIRST(6, 2_000_000_000),
SECOND(5, 30_000_000),
THIRD(5, 1_500_000),
FOURTH(4, 50_000),
FIFTH(3, 5_000),
MISS(0, 0);

private final int matchCount;
private final int prize;

Rank(int matchCount, int prize) {
this.matchCount = matchCount;
this.prize = prize;
}

public int getMatchCount() {
return matchCount;
}

public int getPrize() {
return prize;
}

public static Rank valueOf(int matchCount, boolean bonusMatch) {
if (matchCount == 6) return FIRST;
if (matchCount == 5 && bonusMatch) return SECOND;
if (matchCount == 5) return THIRD;
if (matchCount == 4) return FOURTH;
if (matchCount == 3) return FIFTH;
return MISS;
}
}
39 changes: 39 additions & 0 deletions src/main/java/lotto/WinningResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lotto;

import java.util.HashMap;
import java.util.Map;

public class WinningResult {
private final Map<Rank, Integer> resultMap = new HashMap<>();

public WinningResult() {
for (Rank rank : Rank.values()) {
resultMap.put(rank, 0);
}
}

public void addResult(Rank rank) {
resultMap.put(rank, resultMap.get(rank) + 1);
}

public void printResult() {
System.out.println("당첨 통계");
System.out.println("---");
for (Rank rank : Rank.values()) {
if (rank.getMatchCount() >= 3) { // 5등 이상 당첨만 출력
System.out.printf("%d개 일치", rank.getMatchCount());
if (rank == Rank.SECOND) {
System.out.print(", 보너스 볼 일치");
}
System.out.printf(" (%d원) - %d개%n", rank.getPrize(), resultMap.get(rank));
}
}
}

public double calculateProfitRate(int purchaseAmount) {
long totalPrize = resultMap.entrySet().stream()
.mapToLong(entry -> entry.getKey().getPrize() * entry.getValue())
.sum();
return (double) totalPrize / purchaseAmount * 100;
}
}
8 changes: 7 additions & 1 deletion src/test/java/lotto/LottoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ class LottoTest {
.isInstanceOf(IllegalArgumentException.class);
}

// TODO: 추가 기능 구현에 따른 테스트 코드 작성
@DisplayName("로또 번호가 1~45 범위를 벗어나면 예외가 발생한다.")
@Test
void 로또_번호가_1_에서_45_사이를_벗어나면_예외가_발생한다() {
assertThatThrownBy(() -> new Lotto(List.of(0, 46, 3, 4, 5, 6))) // 0과 46이 범위를 벗어남
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다.");
}
}