diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba44..53f12de469a 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -3,5 +3,10 @@ public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + BuyingLotto buyingLotto = new BuyingLotto(); + LottoCreator lottoCreator = new LottoCreator(buyingLotto.getLottoCount()); + lottoCreator.printLottoList(); + LottoResult lottoResult = new LottoResult(lottoCreator.getLottoList()); + lottoResult.printResult(); } } diff --git a/src/main/java/lotto/BuyingLotto.java b/src/main/java/lotto/BuyingLotto.java new file mode 100644 index 00000000000..c306217310f --- /dev/null +++ b/src/main/java/lotto/BuyingLotto.java @@ -0,0 +1,39 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +public class BuyingLotto { + static int lottoCount; + + public BuyingLotto() { + buyLotto(); + } + + private void buyLotto() { + boolean validInput = false; + + while (!validInput) { + try { + System.out.println("구입금액을 입력해 주세요."); + String input = Console.readLine(); + int money = Integer.parseInt(input); + validateInput(money); + lottoCount = money / 1000; + System.out.println(lottoCount + "개를 구매했습니다."); + validInput = true; + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] 금액은 1000원 단위여야 합니다. 다시 입력해 주세요."); + } + } + } + + public int getLottoCount() { + return lottoCount; + } + + private void validateInput(int money) { + if (money % 1000 == 0) return; + throw new IllegalArgumentException(); + } + +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d1f73..00c77eaf88a 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,20 +1,38 @@ package lotto; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class Lotto { private final List numbers; public Lotto(List numbers) { validate(numbers); + checkForDuplicates(numbers); this.numbers = numbers; } private void validate(List numbers) { if (numbers.size() != 6) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("[Error] 6자리가 아님"); } } + private void checkForDuplicates(List numbers) { + Set numberSet = new HashSet<>(numbers); + if (numberSet.size() != numbers.size()) { + throw new IllegalArgumentException("[Error]번호 중복"); + } + } + + public List getNumbers() { + return numbers; + } + + public void printLotto() { + System.out.println(numbers); + } + // TODO: 추가 기능 구현 } diff --git a/src/main/java/lotto/LottoCreator.java b/src/main/java/lotto/LottoCreator.java new file mode 100644 index 00000000000..1c49e060510 --- /dev/null +++ b/src/main/java/lotto/LottoCreator.java @@ -0,0 +1,32 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class LottoCreator { + private List lottoList; + + public LottoCreator(int count) { + createLotto(count); + } + + private void createLotto(int count) { + lottoList = IntStream.range(0, count) + .mapToObj(i -> new Lotto(LottoNumberGenerator.generateLottoNumber())) + .collect(Collectors.toList()); + + } + + public List getLottoList() { + return lottoList; + } + + public void printLottoList() { + for (Lotto lotto : lottoList) { + lotto.printLotto(); + } + } +} diff --git a/src/main/java/lotto/LottoNumberGenerator.java b/src/main/java/lotto/LottoNumberGenerator.java new file mode 100644 index 00000000000..600868d7da8 --- /dev/null +++ b/src/main/java/lotto/LottoNumberGenerator.java @@ -0,0 +1,22 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.*; + +public class LottoNumberGenerator { + private static final int MAX_NUM = 45; + private static final int MIN_NUM = 1; + private static final int LOTTO_SIZE = 6; + + private LottoNumberGenerator() { + throw new UnsupportedOperationException("Utility class"); + } + + public static List generateLottoNumber() { + List numbers = new ArrayList<>(Randoms.pickUniqueNumbersInRange(MIN_NUM, MAX_NUM, LOTTO_SIZE)); + Collections.sort(numbers); + return numbers; + } + +} diff --git a/src/main/java/lotto/LottoResult.java b/src/main/java/lotto/LottoResult.java new file mode 100644 index 00000000000..65e3993d811 --- /dev/null +++ b/src/main/java/lotto/LottoResult.java @@ -0,0 +1,59 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; + +public class LottoResult { + public int[] resultArray = {0, 0, 0, 0, 0}; + private int attempts; + + public LottoResult(List lottoList) { + attempts = lottoList.size(); + checkLotto(lottoList); + } + + private void checkLotto(List lottoList) { + WinningNumberInput winningNumberInput = new WinningNumberInput(); + + for (Lotto lotto : lottoList) { + List common = new ArrayList<>(winningNumberInput.getWinningNumbers()); + common.retainAll(lotto.getNumbers()); + checkCondition(common.size(), lotto.getNumbers(), winningNumberInput); + } + } + + private void checkCondition(int commonSize, List lottoNumbers, WinningNumberInput winningNumberInput) { + if (commonSize == 6) { + resultArray[4]++; + return; + } + if (commonSize == 5 && lottoNumbers.contains(winningNumberInput.getBonusNumber())) { + resultArray[3]++; + return; + } + if (commonSize >= 3 && commonSize <= 5) { + resultArray[commonSize - 3]++; + return; + } + } + + private double CalculateReturn() { + int allResult = resultArray[0] * 5000 + resultArray[1] * 50000 + resultArray[2] * 1500000 + resultArray[3] * 30000000 + resultArray[4] * 200000000; + double result = (double) allResult / (attempts * 10); + + // 소수점 둘째자리에서 반올림 + return Math.round(result * 100) / 100.0; + } + + public void printResult() { + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - " + resultArray[0] + "개"); + System.out.println("4개 일치 (50,000원) - " + resultArray[1] + "개"); + System.out.println("5개 일치 (1,500,000원) - " + resultArray[2] + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + resultArray[3] + "개"); + System.out.println("6개 일치 (2,000,000,000원) - " + resultArray[4] + "개"); + System.out.println("총 수익률은 " + CalculateReturn() + "%입니다."); + } + +} diff --git a/src/main/java/lotto/WinningNumberInput.java b/src/main/java/lotto/WinningNumberInput.java new file mode 100644 index 00000000000..6339b76c7c9 --- /dev/null +++ b/src/main/java/lotto/WinningNumberInput.java @@ -0,0 +1,33 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.List; + +public class WinningNumberInput { + + private List winningNumbers; + private int bonusNumber; + + public WinningNumberInput() { + setWinningNumber(); + } + + private void setWinningNumber() { + System.out.println("당첨 번호를 입력해 주세요."); + String input = Console.readLine(); + winningNumbers = WinningNumberValidator.parseAndValidateWinningNumbers(input); + + System.out.println("보너스 번호를 입력해 주세요."); + String bonusInput = Console.readLine(); + bonusNumber = WinningNumberValidator.validateBonusNumber(bonusInput, winningNumbers); + } + + public List getWinningNumbers() { + return winningNumbers; + } + + public int getBonusNumber() { + return bonusNumber; + } +} diff --git a/src/main/java/lotto/WinningNumberValidator.java b/src/main/java/lotto/WinningNumberValidator.java new file mode 100644 index 00000000000..681d540bd6f --- /dev/null +++ b/src/main/java/lotto/WinningNumberValidator.java @@ -0,0 +1,35 @@ +package lotto; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class WinningNumberValidator { + private static final int MAX_NUM = 45; + private static final int MIN_NUM = 1; + private static final int LOTTO_SIZE = 6; + public static List parseAndValidateWinningNumbers(String input) { + List numbers = Arrays.stream(input.split(",")) + .map(String::trim) + .map(Integer::parseInt) + .collect(Collectors.toList()); + + if (numbers.size() != LOTTO_SIZE || !numbers.stream().allMatch(WinningNumberValidator::isValidNumber)) { + throw new IllegalArgumentException("[ERROR] 당첨 번호 오류"); + } + + return numbers; + } + + public static int validateBonusNumber(String input, List winningNumbers) { + int number = Integer.parseInt(input); + if (!isValidNumber(number) || winningNumbers.contains(number)) { + throw new IllegalArgumentException("[ERROR] 보너스 번호 오류"); + } + return number; + } + + private static boolean isValidNumber(int number) { + return number >= MIN_NUM && number <= MAX_NUM; + } +}