diff --git a/README.md b/README.md index 5fa2560b46..d6a812162f 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ # java-lotto-precourse + +## 로또 \ No newline at end of file diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..a9214f693b 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -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 getWinningNumbers() { + while (true) { + try { + System.out.println("당첨 번호를 입력해 주세요."); + String[] input = Console.readLine().split(","); + List 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 lottos) { + System.out.printf("%d개를 구매했습니다.%n", lottos.size()); + for (Lotto lotto : lottos) { + System.out.println(lotto.getNumbers()); + } + } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 88fc5cf12b..abde5701eb 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -14,7 +14,15 @@ private void validate(List 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 getNumbers() { + return numbers; + } } diff --git a/src/main/java/lotto/LottoMachine.java b/src/main/java/lotto/LottoMachine.java new file mode 100644 index 0000000000..3f4eb0b464 --- /dev/null +++ b/src/main/java/lotto/LottoMachine.java @@ -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 generateLottos(int count) { + List lottos = new ArrayList<>(); + for (int i = 0; i < count; i++) { + List numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + lottos.add(new Lotto(numbers)); + } + return lottos; + } + + public WinningResult checkWinningResults(List 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; + } +} diff --git a/src/main/java/lotto/Rank.java b/src/main/java/lotto/Rank.java new file mode 100644 index 0000000000..de0222d384 --- /dev/null +++ b/src/main/java/lotto/Rank.java @@ -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; + } +} diff --git a/src/main/java/lotto/WinningResult.java b/src/main/java/lotto/WinningResult.java new file mode 100644 index 0000000000..6abfb2c225 --- /dev/null +++ b/src/main/java/lotto/WinningResult.java @@ -0,0 +1,39 @@ +package lotto; + +import java.util.HashMap; +import java.util.Map; + +public class WinningResult { + private final Map 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; + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 309f4e50ae..2ad0174381 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -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 사이의 숫자여야 합니다."); + } }