diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java deleted file mode 100644 index d190922ba44..00000000000 --- a/src/main/java/lotto/Application.java +++ /dev/null @@ -1,7 +0,0 @@ -package lotto; - -public class Application { - public static void main(String[] args) { - // TODO: 프로그램 구현 - } -} diff --git a/src/main/java/lotto/Controller/Application.java b/src/main/java/lotto/Controller/Application.java new file mode 100644 index 00000000000..64395a87adb --- /dev/null +++ b/src/main/java/lotto/Controller/Application.java @@ -0,0 +1,28 @@ +package lotto.Controller; + +import lotto.Model.Lotto; +import lotto.Model.LottoNumGenerater; +import lotto.Model.LottoResultChecker; +import lotto.View.LottoInput; +import lotto.View.LottoResultPrinter; + +import java.util.List; + +public class Application { + public static void main(String[] args) { + LottoInput input = new LottoInput(); + LottoNumGenerater lottoGenerate = new LottoNumGenerater(); + LottoResultChecker lottoResultCheck = new LottoResultChecker(); + LottoResultPrinter lottoResultPrint = new LottoResultPrinter(); + + int lottoPrice = input.getLottoPrice(); + int lottoNum = lottoPrice / 1000; + + List lottos = lottoGenerate.generateLottos(lottoNum); + List correctNum = input.getCorrectNum(); + int bonusNum = input.getBonusNum(); + + int[] prizeCounts = lottoResultCheck.checkCorrectNum(lottos, correctNum, bonusNum); + lottoResultPrint.printResult(prizeCounts, lottoPrice); + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Model/Lotto.java similarity index 61% rename from src/main/java/lotto/Lotto.java rename to src/main/java/lotto/Model/Lotto.java index 519793d1f73..8f32ed4cd2c 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Model/Lotto.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.Model; import java.util.List; @@ -12,9 +12,11 @@ public Lotto(List numbers) { private void validate(List numbers) { if (numbers.size() != 6) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); } } - // TODO: 추가 기능 구현 + public List getNumbers() { + return numbers; + } } diff --git a/src/main/java/lotto/Model/LottoNumGenerater.java b/src/main/java/lotto/Model/LottoNumGenerater.java new file mode 100644 index 00000000000..12563db91bb --- /dev/null +++ b/src/main/java/lotto/Model/LottoNumGenerater.java @@ -0,0 +1,21 @@ +package lotto.Model; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class LottoNumGenerater { + + public List generateLottos(int count) { + List lottoNumList = new ArrayList<>(); + for (int i = 0; i < count; i++) { + List numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + Collections.sort(numbers); + lottoNumList.add(new Lotto(numbers)); + System.out.println(numbers); + } + return lottoNumList; + } +} diff --git a/src/main/java/lotto/Model/LottoResultChecker.java b/src/main/java/lotto/Model/LottoResultChecker.java new file mode 100644 index 00000000000..32ed3439c7b --- /dev/null +++ b/src/main/java/lotto/Model/LottoResultChecker.java @@ -0,0 +1,44 @@ +package lotto.Model; + +import java.util.List; + +public class LottoResultChecker { + public int[] checkCorrectNum(List lottos, List winningNumbers, int bonusNumber) { + int[] prizeCnt = new int[5]; + for (Lotto lotto : lottos) { + int correctCnt = 0; + boolean bonusCorrect = false; + + for (Integer number : lotto.getNumbers()) { + if (winningNumbers.contains(number)) { + correctCnt++; + } + } + + if (lotto.getNumbers().contains(bonusNumber)) { + bonusCorrect = true; + } + + if (correctCnt == 6) { + prizeCnt[4]++; + } + + if (correctCnt == 5 && bonusCorrect) { + prizeCnt[3]++; + } + + if (correctCnt == 5 && !bonusCorrect) { + prizeCnt[2]++; + } + + if (correctCnt == 4) { + prizeCnt[1]++; + } + + if (correctCnt == 3) { + prizeCnt[0]++; + } + } + return prizeCnt; + } +} diff --git a/src/main/java/lotto/View/LottoInput.java b/src/main/java/lotto/View/LottoInput.java new file mode 100644 index 00000000000..1540225dd01 --- /dev/null +++ b/src/main/java/lotto/View/LottoInput.java @@ -0,0 +1,119 @@ +package lotto.View; + +import java.io.Console; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class LottoInput { + private final Console console = System.console(); + private final Scanner scanner; + + public LottoInput() { + if (console == null) { + scanner = new Scanner(System.in); + } else { + scanner = null; + } + } + + static final int MIN_LOTTO_NUMBER = LottoNumberRange.MIN_NUMBER.getValue(); + static final int MAX_LOTTO_NUMBER = LottoNumberRange.MAX_NUMBER.getValue(); + static final int LOTTO_NUMBER_COUNT = 6; + static final int LOTTO_PRICE_UNIT = 1000; + + public enum LottoNumberRange { + MIN_NUMBER(1), + MAX_NUMBER(45); + + final int value; + + LottoNumberRange(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + } + + public int getLottoPrice() { + while (true) { + try { + System.out.println("구입금액을 입력해주세요."); + String input; + if (console != null) { + input = console.readLine(); + } else { + input = scanner.nextLine(); + } + int lottoPrice = Integer.parseInt(input); + if (lottoPrice % LOTTO_PRICE_UNIT != 0) { + throw new IllegalArgumentException("[ERROR] 구입 금액은 " + LOTTO_PRICE_UNIT + "원 단위로 입력해야 합니다."); + } + return lottoPrice; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public List getCorrectNum() { + while (true) { + try { + System.out.println("당첨 번호를 입력해 주세요."); + String input; + if (console != null) { + input = console.readLine(); + } else { + input = scanner.nextLine(); + } + return splitLottoNum(input); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public int getBonusNum() { + while (true) { + try { + System.out.println("보너스 번호를 입력해주세요."); + String input; + if (console != null) { + input = console.readLine(); + } else { + input = scanner.nextLine(); + } + int bonusNumber = Integer.parseInt(input); + if (bonusNumber < MIN_LOTTO_NUMBER || bonusNumber > MAX_LOTTO_NUMBER) { + throw new IllegalArgumentException("[ERROR] 보너스 번호는 " + MIN_LOTTO_NUMBER + "부터 " + MAX_LOTTO_NUMBER + " 사이의 숫자여야 합니다."); + } + return bonusNumber; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + private List splitLottoNum(String input) { + String[] splitLottoNumList = input.split(","); + List lottoNum = new ArrayList<>(); + + for (String splitLottoNum : splitLottoNumList) { + try { + int num = Integer.parseInt(splitLottoNum.trim()); + if (num < MIN_LOTTO_NUMBER || num > MAX_LOTTO_NUMBER) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 " + MIN_LOTTO_NUMBER + "부터 " + MAX_LOTTO_NUMBER + " 사이의 숫자여야 합니다."); + } + lottoNum.add(num); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 잘못된 형식의 숫자가 입력되었습니다."); + } + } + if (lottoNum.size() != LOTTO_NUMBER_COUNT) { + throw new IllegalArgumentException("[ERROR] 당첨 번호는 " + LOTTO_NUMBER_COUNT + "개여야 합니다."); + } + return lottoNum; + } +} diff --git a/src/main/java/lotto/View/LottoResultPrinter.java b/src/main/java/lotto/View/LottoResultPrinter.java new file mode 100644 index 00000000000..8f6bfe63d29 --- /dev/null +++ b/src/main/java/lotto/View/LottoResultPrinter.java @@ -0,0 +1,24 @@ +package lotto.View; + +public class LottoResultPrinter { + private final int[] prizeMoney = { 5000, 50000, 1500000, 30000000, 2000000000 }; + + public void printResult(int[] prizeCnt, int totalCost) { + int totalMoney = 0; + + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - " + prizeCnt[0] + "개"); + System.out.println("4개 일치 (50,000원) - " + prizeCnt[1] + "개"); + System.out.println("5개 일치 (1,500,000원) - " + prizeCnt[2] + "개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - " + prizeCnt[3] + "개"); + System.out.println("6개 일치 (2,000,000,000원) - " + prizeCnt[4] + "개"); + + for (int i = 0; i < prizeCnt.length; i++) { + totalMoney += prizeCnt[i] * prizeMoney[i]; + } + + double benefitRate = ((double) totalMoney / totalCost) * 100; + System.out.printf("총 수익률은 %.1f%%입니다.\n", benefitRate); + } +} diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index a15c7d1f522..5ba15edcee0 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -1,6 +1,7 @@ package lotto; import camp.nextstep.edu.missionutils.test.NsTest; +import lotto.Controller.Application; import org.junit.jupiter.api.Test; import java.util.List; diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 9f5dfe7eb83..f91c11b1cf2 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,5 +1,6 @@ package lotto; +import lotto.Model.Lotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test;