From 02f98781a907e91e02f833ab4e1aea4693c8bb9b Mon Sep 17 00:00:00 2001 From: wldnd7145 <80200734+wldnd7145@users.noreply.github.com> Date: Mon, 15 Jul 2024 03:12:51 +0900 Subject: [PATCH 1/6] Delete src directory --- src/main/java/lotto/Application.java | 7 --- src/main/java/lotto/Lotto.java | 20 -------- src/test/java/lotto/ApplicationTest.java | 61 ------------------------ src/test/java/lotto/LottoTest.java | 27 ----------- 4 files changed, 115 deletions(-) delete mode 100644 src/main/java/lotto/Application.java delete mode 100644 src/main/java/lotto/Lotto.java delete mode 100644 src/test/java/lotto/ApplicationTest.java delete mode 100644 src/test/java/lotto/LottoTest.java 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/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 519793d1f73..00000000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,20 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException(); - } - } - - // TODO: 추가 기능 구현 -} diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java deleted file mode 100644 index a15c7d1f522..00000000000 --- a/src/test/java/lotto/ApplicationTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package lotto; - -import camp.nextstep.edu.missionutils.test.NsTest; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; -import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; -import static org.assertj.core.api.Assertions.assertThat; - -class ApplicationTest extends NsTest { - private static final String ERROR_MESSAGE = "[ERROR]"; - - @Test - void 기능_테스트() { - assertRandomUniqueNumbersInRangeTest( - () -> { - run("8000", "1,2,3,4,5,6", "7"); - assertThat(output()).contains( - "8개를 구매했습니다.", - "[8, 21, 23, 41, 42, 43]", - "[3, 5, 11, 16, 32, 38]", - "[7, 11, 16, 35, 36, 44]", - "[1, 8, 11, 31, 41, 42]", - "[13, 14, 16, 38, 42, 45]", - "[7, 11, 30, 40, 42, 43]", - "[2, 13, 22, 32, 38, 45]", - "[1, 3, 5, 14, 22, 45]", - "3개 일치 (5,000원) - 1개", - "4개 일치 (50,000원) - 0개", - "5개 일치 (1,500,000원) - 0개", - "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", - "6개 일치 (2,000,000,000원) - 0개", - "총 수익률은 62.5%입니다." - ); - }, - List.of(8, 21, 23, 41, 42, 43), - List.of(3, 5, 11, 16, 32, 38), - List.of(7, 11, 16, 35, 36, 44), - List.of(1, 8, 11, 31, 41, 42), - List.of(13, 14, 16, 38, 42, 45), - List.of(7, 11, 30, 40, 42, 43), - List.of(2, 13, 22, 32, 38, 45), - List.of(1, 3, 5, 14, 22, 45) - ); - } - - @Test - void 예외_테스트() { - assertSimpleTest(() -> { - runException("1000j"); - assertThat(output()).contains(ERROR_MESSAGE); - }); - } - - @Override - public void runMain() { - Application.main(new String[]{}); - } -} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java deleted file mode 100644 index 9f5dfe7eb83..00000000000 --- a/src/test/java/lotto/LottoTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package lotto; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -class LottoTest { - @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") - @Test - void createLottoByOverSize() { - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) - .isInstanceOf(IllegalArgumentException.class); - } - - @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") - @Test - void createLottoByDuplicatedNumber() { - // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) - .isInstanceOf(IllegalArgumentException.class); - } - - // 아래에 추가 테스트 작성 가능 -} \ No newline at end of file From 32b377668f9599cbea8a94813e2e62ec5cf77e4e Mon Sep 17 00:00:00 2001 From: wldnd7145 <80200734+wldnd7145@users.noreply.github.com> Date: Mon, 15 Jul 2024 03:13:35 +0900 Subject: [PATCH 2/6] Add files via upload --- src/main/java/lotto/Application.java | 22 +++++++ src/main/java/lotto/Lotto.java | 22 +++++++ src/main/java/lotto/LottoGenerate.java | 19 ++++++ src/main/java/lotto/LottoInput.java | 72 +++++++++++++++++++++++ src/main/java/lotto/LottoNumGenerate.java | 21 +++++++ src/main/java/lotto/LottoResultCheck.java | 36 ++++++++++++ src/main/java/lotto/LottoResultPrint.java | 24 ++++++++ src/test/java/lotto/ApplicationTest.java | 61 +++++++++++++++++++ src/test/java/lotto/LottoTest.java | 27 +++++++++ 9 files changed, 304 insertions(+) create mode 100644 src/main/java/lotto/Application.java create mode 100644 src/main/java/lotto/Lotto.java create mode 100644 src/main/java/lotto/LottoGenerate.java create mode 100644 src/main/java/lotto/LottoInput.java create mode 100644 src/main/java/lotto/LottoNumGenerate.java create mode 100644 src/main/java/lotto/LottoResultCheck.java create mode 100644 src/main/java/lotto/LottoResultPrint.java create mode 100644 src/test/java/lotto/ApplicationTest.java create mode 100644 src/test/java/lotto/LottoTest.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java new file mode 100644 index 00000000000..3ee68c3e99e --- /dev/null +++ b/src/main/java/lotto/Application.java @@ -0,0 +1,22 @@ +package lotto; + +import java.util.List; + +public class Application { + public static void main(String[] args) { + LottoInput input = new LottoInput(); + LottoGenerate lottoGenerate = new LottoGenerate(); + LottoResultCheck lottoResultCheck = new LottoResultCheck(); + LottoResultPrint lottoResultPrint = new LottoResultPrint(); + + int lottoPrice = input.getLottoPrice(); + int lottoNum = lottoPrice / 1000; + + List lottos = lottoGenerate.generateLottos(lottoNum); + List collectNum = input.getCollectNum(); + int bonusNum = input.getBonusNum(); + + int[] prizeCounts = lottoResultCheck.checkCollectNum(lottos, collectNum, bonusNum); + lottoResultPrint.printResult(prizeCounts, lottoPrice); + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java new file mode 100644 index 00000000000..79758ea9c9c --- /dev/null +++ b/src/main/java/lotto/Lotto.java @@ -0,0 +1,22 @@ +package lotto; + +import java.util.List; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validate(numbers); + this.numbers = numbers; + } + + private void validate(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } + + public List getNumbers() { + return numbers; + } +} diff --git a/src/main/java/lotto/LottoGenerate.java b/src/main/java/lotto/LottoGenerate.java new file mode 100644 index 00000000000..afd462f2990 --- /dev/null +++ b/src/main/java/lotto/LottoGenerate.java @@ -0,0 +1,19 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class LottoGenerate { + 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/LottoInput.java b/src/main/java/lotto/LottoInput.java new file mode 100644 index 00000000000..939308ebea9 --- /dev/null +++ b/src/main/java/lotto/LottoInput.java @@ -0,0 +1,72 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class LottoInput { + private final Scanner scan = new Scanner(System.in); + + public int getLottoPrice() { + while (true) { + try { + System.out.println("구입금액을 입력해주세요"); + int lottoPrice = Integer.parseInt(scan.nextLine()); + if (lottoPrice % 1000 != 0) { + throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원 단위로 입력해야 합니다."); + } + return lottoPrice; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public List getCollectNum() { + while (true) { + try { + System.out.println("당첨 번호를 입력해 주세요."); + String input = scan.nextLine(); + return splitLottoNum(input); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public int getBonusNum() { + while (true) { + try { + System.out.println("보너스 번호를 입력해주세요."); + int bonusNumber = Integer.parseInt(scan.nextLine()); + if (bonusNumber < 1 || bonusNumber > 45) { + throw new IllegalArgumentException("[ERROR] 보너스 번호는 1부터 45 사이의 숫자여야 합니다."); + } + return bonusNumber; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + private List splitLottoNum(String input) { + String[] subLottoNum = input.split(","); + List lottoNum = new ArrayList<>(); + + for (String element : subLottoNum) { + try { + int num = Integer.parseInt(element.trim()); + if (num < 1 || num > 45) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + lottoNum.add(num); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 잘못된 형식의 숫자가 입력되었습니다."); + } + } + if (lottoNum.size() != 6) { + throw new IllegalArgumentException("[ERROR] 당첨 번호는 6개여야 합니다."); + } + return lottoNum; + } +} diff --git a/src/main/java/lotto/LottoNumGenerate.java b/src/main/java/lotto/LottoNumGenerate.java new file mode 100644 index 00000000000..ca04f4eb415 --- /dev/null +++ b/src/main/java/lotto/LottoNumGenerate.java @@ -0,0 +1,21 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class LottoNumGenerate { + + 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/LottoResultCheck.java b/src/main/java/lotto/LottoResultCheck.java new file mode 100644 index 00000000000..eada397338a --- /dev/null +++ b/src/main/java/lotto/LottoResultCheck.java @@ -0,0 +1,36 @@ +package lotto; + +import java.util.List; + +public class LottoResultCheck { + public int[] checkCollectNum(List lottos, List winningNumbers, int bonusNumber) { + int[] prizeCnt = new int[5]; + for (Lotto lotto : lottos) { + int collectCnt = 0; + boolean bonusCollect = false; + + for (Integer number : lotto.getNumbers()) { + if (winningNumbers.contains(number)) { + collectCnt++; + } + } + + if (lotto.getNumbers().contains(bonusNumber)) { + bonusCollect = true; + } + + if (collectCnt == 6) { + prizeCnt[4]++; + } else if (collectCnt == 5 && bonusCollect) { + prizeCnt[3]++; + } else if (collectCnt == 5) { + prizeCnt[2]++; + } else if (collectCnt == 4) { + prizeCnt[1]++; + } else if (collectCnt == 3) { + prizeCnt[0]++; + } + } + return prizeCnt; + } +} diff --git a/src/main/java/lotto/LottoResultPrint.java b/src/main/java/lotto/LottoResultPrint.java new file mode 100644 index 00000000000..8ca3076ac32 --- /dev/null +++ b/src/main/java/lotto/LottoResultPrint.java @@ -0,0 +1,24 @@ +package lotto; + +public class LottoResultPrint { + 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 = (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 new file mode 100644 index 00000000000..84daa4964bc --- /dev/null +++ b/src/test/java/lotto/ApplicationTest.java @@ -0,0 +1,61 @@ +package lotto; + +import camp.nextstep.edu.missionutils.test.NsTest; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; +import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; +import static org.assertj.core.api.Assertions.assertThat; + +class ApplicationTest extends NsTest { + private static final String ERROR_MESSAGE = "[ERROR]"; + + @Test + void 기능_테스트() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("8000", "1,2,3,4,5,6", "7"); + assertThat(output()).contains( + "8개를 구매했습니다.", + "[8, 21, 23, 41, 42, 43]", + "[3, 5, 11, 16, 32, 38]", + "[7, 11, 16, 35, 36, 44]", + "[1, 8, 11, 31, 41, 42]", + "[13, 14, 16, 38, 42, 45]", + "[7, 11, 30, 40, 42, 43]", + "[2, 13, 22, 32, 38, 45]", + "[1, 3, 5, 14, 22, 45]", + "3개 일치 (5,000원) - 1개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 0개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", + "6개 일치 (2,000,000,000원) - 0개", + "총 수익률은 62.5%입니다." + ); + }, + List.of(8, 21, 23, 41, 42, 43), + List.of(3, 5, 11, 16, 32, 38), + List.of(7, 11, 16, 35, 36, 44), + List.of(1, 8, 11, 31, 41, 42), + List.of(13, 14, 16, 38, 42, 45), + List.of(7, 11, 30, 40, 42, 43), + List.of(2, 13, 22, 32, 38, 45), + List.of(1, 3, 5, 14, 22, 45) + ); + } + + @Test + void 예외_테스트() { + assertSimpleTest(() -> { + runException("1000j"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + + @Override + public void runMain() { + Application.main(new String[]{}); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java new file mode 100644 index 00000000000..6c090d75406 --- /dev/null +++ b/src/test/java/lotto/LottoTest.java @@ -0,0 +1,27 @@ +package lotto; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class LottoTest { + @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") + @Test + void createLottoByOverSize() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") + @Test + void createLottoByDuplicatedNumber() { + // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) + .isInstanceOf(IllegalArgumentException.class); + } + + // 아래에 추가 테스트 작성 가능 +} \ No newline at end of file From d9043799da4ab2bea689771258d22f953de71a36 Mon Sep 17 00:00:00 2001 From: wldnd7145 <80200734+wldnd7145@users.noreply.github.com> Date: Mon, 15 Jul 2024 03:22:30 +0900 Subject: [PATCH 3/6] Delete src directory --- src/main/java/lotto/Application.java | 22 ------- src/main/java/lotto/Lotto.java | 22 ------- src/main/java/lotto/LottoGenerate.java | 19 ------ src/main/java/lotto/LottoInput.java | 72 ----------------------- src/main/java/lotto/LottoNumGenerate.java | 21 ------- src/main/java/lotto/LottoResultCheck.java | 36 ------------ src/main/java/lotto/LottoResultPrint.java | 24 -------- src/test/java/lotto/ApplicationTest.java | 61 ------------------- src/test/java/lotto/LottoTest.java | 27 --------- 9 files changed, 304 deletions(-) delete mode 100644 src/main/java/lotto/Application.java delete mode 100644 src/main/java/lotto/Lotto.java delete mode 100644 src/main/java/lotto/LottoGenerate.java delete mode 100644 src/main/java/lotto/LottoInput.java delete mode 100644 src/main/java/lotto/LottoNumGenerate.java delete mode 100644 src/main/java/lotto/LottoResultCheck.java delete mode 100644 src/main/java/lotto/LottoResultPrint.java delete mode 100644 src/test/java/lotto/ApplicationTest.java delete mode 100644 src/test/java/lotto/LottoTest.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java deleted file mode 100644 index 3ee68c3e99e..00000000000 --- a/src/main/java/lotto/Application.java +++ /dev/null @@ -1,22 +0,0 @@ -package lotto; - -import java.util.List; - -public class Application { - public static void main(String[] args) { - LottoInput input = new LottoInput(); - LottoGenerate lottoGenerate = new LottoGenerate(); - LottoResultCheck lottoResultCheck = new LottoResultCheck(); - LottoResultPrint lottoResultPrint = new LottoResultPrint(); - - int lottoPrice = input.getLottoPrice(); - int lottoNum = lottoPrice / 1000; - - List lottos = lottoGenerate.generateLottos(lottoNum); - List collectNum = input.getCollectNum(); - int bonusNum = input.getBonusNum(); - - int[] prizeCounts = lottoResultCheck.checkCollectNum(lottos, collectNum, bonusNum); - lottoResultPrint.printResult(prizeCounts, lottoPrice); - } -} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 79758ea9c9c..00000000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,22 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); - } - } - - public List getNumbers() { - return numbers; - } -} diff --git a/src/main/java/lotto/LottoGenerate.java b/src/main/java/lotto/LottoGenerate.java deleted file mode 100644 index afd462f2990..00000000000 --- a/src/main/java/lotto/LottoGenerate.java +++ /dev/null @@ -1,19 +0,0 @@ -package lotto; - -import camp.nextstep.edu.missionutils.Randoms; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class LottoGenerate { - 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/LottoInput.java b/src/main/java/lotto/LottoInput.java deleted file mode 100644 index 939308ebea9..00000000000 --- a/src/main/java/lotto/LottoInput.java +++ /dev/null @@ -1,72 +0,0 @@ -package lotto; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class LottoInput { - private final Scanner scan = new Scanner(System.in); - - public int getLottoPrice() { - while (true) { - try { - System.out.println("구입금액을 입력해주세요"); - int lottoPrice = Integer.parseInt(scan.nextLine()); - if (lottoPrice % 1000 != 0) { - throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원 단위로 입력해야 합니다."); - } - return lottoPrice; - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - } - - public List getCollectNum() { - while (true) { - try { - System.out.println("당첨 번호를 입력해 주세요."); - String input = scan.nextLine(); - return splitLottoNum(input); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - } - - public int getBonusNum() { - while (true) { - try { - System.out.println("보너스 번호를 입력해주세요."); - int bonusNumber = Integer.parseInt(scan.nextLine()); - if (bonusNumber < 1 || bonusNumber > 45) { - throw new IllegalArgumentException("[ERROR] 보너스 번호는 1부터 45 사이의 숫자여야 합니다."); - } - return bonusNumber; - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - } - - private List splitLottoNum(String input) { - String[] subLottoNum = input.split(","); - List lottoNum = new ArrayList<>(); - - for (String element : subLottoNum) { - try { - int num = Integer.parseInt(element.trim()); - if (num < 1 || num > 45) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - lottoNum.add(num); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("[ERROR] 잘못된 형식의 숫자가 입력되었습니다."); - } - } - if (lottoNum.size() != 6) { - throw new IllegalArgumentException("[ERROR] 당첨 번호는 6개여야 합니다."); - } - return lottoNum; - } -} diff --git a/src/main/java/lotto/LottoNumGenerate.java b/src/main/java/lotto/LottoNumGenerate.java deleted file mode 100644 index ca04f4eb415..00000000000 --- a/src/main/java/lotto/LottoNumGenerate.java +++ /dev/null @@ -1,21 +0,0 @@ -package lotto; - -import camp.nextstep.edu.missionutils.Randoms; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class LottoNumGenerate { - - 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/LottoResultCheck.java b/src/main/java/lotto/LottoResultCheck.java deleted file mode 100644 index eada397338a..00000000000 --- a/src/main/java/lotto/LottoResultCheck.java +++ /dev/null @@ -1,36 +0,0 @@ -package lotto; - -import java.util.List; - -public class LottoResultCheck { - public int[] checkCollectNum(List lottos, List winningNumbers, int bonusNumber) { - int[] prizeCnt = new int[5]; - for (Lotto lotto : lottos) { - int collectCnt = 0; - boolean bonusCollect = false; - - for (Integer number : lotto.getNumbers()) { - if (winningNumbers.contains(number)) { - collectCnt++; - } - } - - if (lotto.getNumbers().contains(bonusNumber)) { - bonusCollect = true; - } - - if (collectCnt == 6) { - prizeCnt[4]++; - } else if (collectCnt == 5 && bonusCollect) { - prizeCnt[3]++; - } else if (collectCnt == 5) { - prizeCnt[2]++; - } else if (collectCnt == 4) { - prizeCnt[1]++; - } else if (collectCnt == 3) { - prizeCnt[0]++; - } - } - return prizeCnt; - } -} diff --git a/src/main/java/lotto/LottoResultPrint.java b/src/main/java/lotto/LottoResultPrint.java deleted file mode 100644 index 8ca3076ac32..00000000000 --- a/src/main/java/lotto/LottoResultPrint.java +++ /dev/null @@ -1,24 +0,0 @@ -package lotto; - -public class LottoResultPrint { - 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 = (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 deleted file mode 100644 index 84daa4964bc..00000000000 --- a/src/test/java/lotto/ApplicationTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package lotto; - -import camp.nextstep.edu.missionutils.test.NsTest; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; -import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; -import static org.assertj.core.api.Assertions.assertThat; - -class ApplicationTest extends NsTest { - private static final String ERROR_MESSAGE = "[ERROR]"; - - @Test - void 기능_테스트() { - assertRandomUniqueNumbersInRangeTest( - () -> { - run("8000", "1,2,3,4,5,6", "7"); - assertThat(output()).contains( - "8개를 구매했습니다.", - "[8, 21, 23, 41, 42, 43]", - "[3, 5, 11, 16, 32, 38]", - "[7, 11, 16, 35, 36, 44]", - "[1, 8, 11, 31, 41, 42]", - "[13, 14, 16, 38, 42, 45]", - "[7, 11, 30, 40, 42, 43]", - "[2, 13, 22, 32, 38, 45]", - "[1, 3, 5, 14, 22, 45]", - "3개 일치 (5,000원) - 1개", - "4개 일치 (50,000원) - 0개", - "5개 일치 (1,500,000원) - 0개", - "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", - "6개 일치 (2,000,000,000원) - 0개", - "총 수익률은 62.5%입니다." - ); - }, - List.of(8, 21, 23, 41, 42, 43), - List.of(3, 5, 11, 16, 32, 38), - List.of(7, 11, 16, 35, 36, 44), - List.of(1, 8, 11, 31, 41, 42), - List.of(13, 14, 16, 38, 42, 45), - List.of(7, 11, 30, 40, 42, 43), - List.of(2, 13, 22, 32, 38, 45), - List.of(1, 3, 5, 14, 22, 45) - ); - } - - @Test - void 예외_테스트() { - assertSimpleTest(() -> { - runException("1000j"); - assertThat(output()).contains(ERROR_MESSAGE); - }); - } - - @Override - public void runMain() { - Application.main(new String[]{}); - } -} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java deleted file mode 100644 index 6c090d75406..00000000000 --- a/src/test/java/lotto/LottoTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package lotto; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -class LottoTest { - @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") - @Test - void createLottoByOverSize() { - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) - .isInstanceOf(IllegalArgumentException.class); - } - - @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") - @Test - void createLottoByDuplicatedNumber() { - // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) - .isInstanceOf(IllegalArgumentException.class); - } - - // 아래에 추가 테스트 작성 가능 -} \ No newline at end of file From 74a612e99bccc442abc20d42dac781a57eaef891 Mon Sep 17 00:00:00 2001 From: wldnd7145 <80200734+wldnd7145@users.noreply.github.com> Date: Mon, 15 Jul 2024 03:23:12 +0900 Subject: [PATCH 4/6] Add files via upload --- src/main/java/lotto/Application.java | 22 +++++++ src/main/java/lotto/Lotto.java | 22 +++++++ src/main/java/lotto/LottoInput.java | 72 +++++++++++++++++++++++ src/main/java/lotto/LottoNumGenerate.java | 21 +++++++ src/main/java/lotto/LottoResultCheck.java | 36 ++++++++++++ src/main/java/lotto/LottoResultPrint.java | 24 ++++++++ src/test/java/lotto/ApplicationTest.java | 61 +++++++++++++++++++ src/test/java/lotto/LottoTest.java | 27 +++++++++ 8 files changed, 285 insertions(+) create mode 100644 src/main/java/lotto/Application.java create mode 100644 src/main/java/lotto/Lotto.java create mode 100644 src/main/java/lotto/LottoInput.java create mode 100644 src/main/java/lotto/LottoNumGenerate.java create mode 100644 src/main/java/lotto/LottoResultCheck.java create mode 100644 src/main/java/lotto/LottoResultPrint.java create mode 100644 src/test/java/lotto/ApplicationTest.java create mode 100644 src/test/java/lotto/LottoTest.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java new file mode 100644 index 00000000000..ea154f6fac8 --- /dev/null +++ b/src/main/java/lotto/Application.java @@ -0,0 +1,22 @@ +package lotto; + +import java.util.List; + +public class Application { + public static void main(String[] args) { + LottoInput input = new LottoInput(); + LottoNumGenerate lottoGenerate = new LottoNumGenerate(); + LottoResultCheck lottoResultCheck = new LottoResultCheck(); + LottoResultPrint lottoResultPrint = new LottoResultPrint(); + + int lottoPrice = input.getLottoPrice(); + int lottoNum = lottoPrice / 1000; + + List lottos = lottoGenerate.generateLottos(lottoNum); + List collectNum = input.getCollectNum(); + int bonusNum = input.getBonusNum(); + + int[] prizeCounts = lottoResultCheck.checkCollectNum(lottos, collectNum, bonusNum); + lottoResultPrint.printResult(prizeCounts, lottoPrice); + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java new file mode 100644 index 00000000000..79758ea9c9c --- /dev/null +++ b/src/main/java/lotto/Lotto.java @@ -0,0 +1,22 @@ +package lotto; + +import java.util.List; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validate(numbers); + this.numbers = numbers; + } + + private void validate(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } + + public List getNumbers() { + return numbers; + } +} diff --git a/src/main/java/lotto/LottoInput.java b/src/main/java/lotto/LottoInput.java new file mode 100644 index 00000000000..939308ebea9 --- /dev/null +++ b/src/main/java/lotto/LottoInput.java @@ -0,0 +1,72 @@ +package lotto; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class LottoInput { + private final Scanner scan = new Scanner(System.in); + + public int getLottoPrice() { + while (true) { + try { + System.out.println("구입금액을 입력해주세요"); + int lottoPrice = Integer.parseInt(scan.nextLine()); + if (lottoPrice % 1000 != 0) { + throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원 단위로 입력해야 합니다."); + } + return lottoPrice; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public List getCollectNum() { + while (true) { + try { + System.out.println("당첨 번호를 입력해 주세요."); + String input = scan.nextLine(); + return splitLottoNum(input); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + public int getBonusNum() { + while (true) { + try { + System.out.println("보너스 번호를 입력해주세요."); + int bonusNumber = Integer.parseInt(scan.nextLine()); + if (bonusNumber < 1 || bonusNumber > 45) { + throw new IllegalArgumentException("[ERROR] 보너스 번호는 1부터 45 사이의 숫자여야 합니다."); + } + return bonusNumber; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + + private List splitLottoNum(String input) { + String[] subLottoNum = input.split(","); + List lottoNum = new ArrayList<>(); + + for (String element : subLottoNum) { + try { + int num = Integer.parseInt(element.trim()); + if (num < 1 || num > 45) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); + } + lottoNum.add(num); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 잘못된 형식의 숫자가 입력되었습니다."); + } + } + if (lottoNum.size() != 6) { + throw new IllegalArgumentException("[ERROR] 당첨 번호는 6개여야 합니다."); + } + return lottoNum; + } +} diff --git a/src/main/java/lotto/LottoNumGenerate.java b/src/main/java/lotto/LottoNumGenerate.java new file mode 100644 index 00000000000..ca04f4eb415 --- /dev/null +++ b/src/main/java/lotto/LottoNumGenerate.java @@ -0,0 +1,21 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class LottoNumGenerate { + + 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/LottoResultCheck.java b/src/main/java/lotto/LottoResultCheck.java new file mode 100644 index 00000000000..eada397338a --- /dev/null +++ b/src/main/java/lotto/LottoResultCheck.java @@ -0,0 +1,36 @@ +package lotto; + +import java.util.List; + +public class LottoResultCheck { + public int[] checkCollectNum(List lottos, List winningNumbers, int bonusNumber) { + int[] prizeCnt = new int[5]; + for (Lotto lotto : lottos) { + int collectCnt = 0; + boolean bonusCollect = false; + + for (Integer number : lotto.getNumbers()) { + if (winningNumbers.contains(number)) { + collectCnt++; + } + } + + if (lotto.getNumbers().contains(bonusNumber)) { + bonusCollect = true; + } + + if (collectCnt == 6) { + prizeCnt[4]++; + } else if (collectCnt == 5 && bonusCollect) { + prizeCnt[3]++; + } else if (collectCnt == 5) { + prizeCnt[2]++; + } else if (collectCnt == 4) { + prizeCnt[1]++; + } else if (collectCnt == 3) { + prizeCnt[0]++; + } + } + return prizeCnt; + } +} diff --git a/src/main/java/lotto/LottoResultPrint.java b/src/main/java/lotto/LottoResultPrint.java new file mode 100644 index 00000000000..8ca3076ac32 --- /dev/null +++ b/src/main/java/lotto/LottoResultPrint.java @@ -0,0 +1,24 @@ +package lotto; + +public class LottoResultPrint { + 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 = (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 new file mode 100644 index 00000000000..84daa4964bc --- /dev/null +++ b/src/test/java/lotto/ApplicationTest.java @@ -0,0 +1,61 @@ +package lotto; + +import camp.nextstep.edu.missionutils.test.NsTest; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; +import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; +import static org.assertj.core.api.Assertions.assertThat; + +class ApplicationTest extends NsTest { + private static final String ERROR_MESSAGE = "[ERROR]"; + + @Test + void 기능_테스트() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("8000", "1,2,3,4,5,6", "7"); + assertThat(output()).contains( + "8개를 구매했습니다.", + "[8, 21, 23, 41, 42, 43]", + "[3, 5, 11, 16, 32, 38]", + "[7, 11, 16, 35, 36, 44]", + "[1, 8, 11, 31, 41, 42]", + "[13, 14, 16, 38, 42, 45]", + "[7, 11, 30, 40, 42, 43]", + "[2, 13, 22, 32, 38, 45]", + "[1, 3, 5, 14, 22, 45]", + "3개 일치 (5,000원) - 1개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 0개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", + "6개 일치 (2,000,000,000원) - 0개", + "총 수익률은 62.5%입니다." + ); + }, + List.of(8, 21, 23, 41, 42, 43), + List.of(3, 5, 11, 16, 32, 38), + List.of(7, 11, 16, 35, 36, 44), + List.of(1, 8, 11, 31, 41, 42), + List.of(13, 14, 16, 38, 42, 45), + List.of(7, 11, 30, 40, 42, 43), + List.of(2, 13, 22, 32, 38, 45), + List.of(1, 3, 5, 14, 22, 45) + ); + } + + @Test + void 예외_테스트() { + assertSimpleTest(() -> { + runException("1000j"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + + @Override + public void runMain() { + Application.main(new String[]{}); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java new file mode 100644 index 00000000000..6c090d75406 --- /dev/null +++ b/src/test/java/lotto/LottoTest.java @@ -0,0 +1,27 @@ +package lotto; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class LottoTest { + @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") + @Test + void createLottoByOverSize() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") + @Test + void createLottoByDuplicatedNumber() { + // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) + .isInstanceOf(IllegalArgumentException.class); + } + + // 아래에 추가 테스트 작성 가능 +} \ No newline at end of file From c488a2362b16b5073c85c391a0539a5f49012ab0 Mon Sep 17 00:00:00 2001 From: wldnd7145 <80200734+wldnd7145@users.noreply.github.com> Date: Thu, 18 Jul 2024 19:54:36 +0900 Subject: [PATCH 5/6] Delete src directory --- src/main/java/lotto/Application.java | 22 ------- src/main/java/lotto/Lotto.java | 22 ------- src/main/java/lotto/LottoInput.java | 72 ----------------------- src/main/java/lotto/LottoNumGenerate.java | 21 ------- src/main/java/lotto/LottoResultCheck.java | 36 ------------ src/main/java/lotto/LottoResultPrint.java | 24 -------- src/test/java/lotto/ApplicationTest.java | 61 ------------------- src/test/java/lotto/LottoTest.java | 27 --------- 8 files changed, 285 deletions(-) delete mode 100644 src/main/java/lotto/Application.java delete mode 100644 src/main/java/lotto/Lotto.java delete mode 100644 src/main/java/lotto/LottoInput.java delete mode 100644 src/main/java/lotto/LottoNumGenerate.java delete mode 100644 src/main/java/lotto/LottoResultCheck.java delete mode 100644 src/main/java/lotto/LottoResultPrint.java delete mode 100644 src/test/java/lotto/ApplicationTest.java delete mode 100644 src/test/java/lotto/LottoTest.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java deleted file mode 100644 index ea154f6fac8..00000000000 --- a/src/main/java/lotto/Application.java +++ /dev/null @@ -1,22 +0,0 @@ -package lotto; - -import java.util.List; - -public class Application { - public static void main(String[] args) { - LottoInput input = new LottoInput(); - LottoNumGenerate lottoGenerate = new LottoNumGenerate(); - LottoResultCheck lottoResultCheck = new LottoResultCheck(); - LottoResultPrint lottoResultPrint = new LottoResultPrint(); - - int lottoPrice = input.getLottoPrice(); - int lottoNum = lottoPrice / 1000; - - List lottos = lottoGenerate.generateLottos(lottoNum); - List collectNum = input.getCollectNum(); - int bonusNum = input.getBonusNum(); - - int[] prizeCounts = lottoResultCheck.checkCollectNum(lottos, collectNum, bonusNum); - lottoResultPrint.printResult(prizeCounts, lottoPrice); - } -} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 79758ea9c9c..00000000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,22 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); - } - } - - public List getNumbers() { - return numbers; - } -} diff --git a/src/main/java/lotto/LottoInput.java b/src/main/java/lotto/LottoInput.java deleted file mode 100644 index 939308ebea9..00000000000 --- a/src/main/java/lotto/LottoInput.java +++ /dev/null @@ -1,72 +0,0 @@ -package lotto; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class LottoInput { - private final Scanner scan = new Scanner(System.in); - - public int getLottoPrice() { - while (true) { - try { - System.out.println("구입금액을 입력해주세요"); - int lottoPrice = Integer.parseInt(scan.nextLine()); - if (lottoPrice % 1000 != 0) { - throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원 단위로 입력해야 합니다."); - } - return lottoPrice; - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - } - - public List getCollectNum() { - while (true) { - try { - System.out.println("당첨 번호를 입력해 주세요."); - String input = scan.nextLine(); - return splitLottoNum(input); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - } - - public int getBonusNum() { - while (true) { - try { - System.out.println("보너스 번호를 입력해주세요."); - int bonusNumber = Integer.parseInt(scan.nextLine()); - if (bonusNumber < 1 || bonusNumber > 45) { - throw new IllegalArgumentException("[ERROR] 보너스 번호는 1부터 45 사이의 숫자여야 합니다."); - } - return bonusNumber; - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - } - - private List splitLottoNum(String input) { - String[] subLottoNum = input.split(","); - List lottoNum = new ArrayList<>(); - - for (String element : subLottoNum) { - try { - int num = Integer.parseInt(element.trim()); - if (num < 1 || num > 45) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자여야 합니다."); - } - lottoNum.add(num); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("[ERROR] 잘못된 형식의 숫자가 입력되었습니다."); - } - } - if (lottoNum.size() != 6) { - throw new IllegalArgumentException("[ERROR] 당첨 번호는 6개여야 합니다."); - } - return lottoNum; - } -} diff --git a/src/main/java/lotto/LottoNumGenerate.java b/src/main/java/lotto/LottoNumGenerate.java deleted file mode 100644 index ca04f4eb415..00000000000 --- a/src/main/java/lotto/LottoNumGenerate.java +++ /dev/null @@ -1,21 +0,0 @@ -package lotto; - -import camp.nextstep.edu.missionutils.Randoms; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class LottoNumGenerate { - - 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/LottoResultCheck.java b/src/main/java/lotto/LottoResultCheck.java deleted file mode 100644 index eada397338a..00000000000 --- a/src/main/java/lotto/LottoResultCheck.java +++ /dev/null @@ -1,36 +0,0 @@ -package lotto; - -import java.util.List; - -public class LottoResultCheck { - public int[] checkCollectNum(List lottos, List winningNumbers, int bonusNumber) { - int[] prizeCnt = new int[5]; - for (Lotto lotto : lottos) { - int collectCnt = 0; - boolean bonusCollect = false; - - for (Integer number : lotto.getNumbers()) { - if (winningNumbers.contains(number)) { - collectCnt++; - } - } - - if (lotto.getNumbers().contains(bonusNumber)) { - bonusCollect = true; - } - - if (collectCnt == 6) { - prizeCnt[4]++; - } else if (collectCnt == 5 && bonusCollect) { - prizeCnt[3]++; - } else if (collectCnt == 5) { - prizeCnt[2]++; - } else if (collectCnt == 4) { - prizeCnt[1]++; - } else if (collectCnt == 3) { - prizeCnt[0]++; - } - } - return prizeCnt; - } -} diff --git a/src/main/java/lotto/LottoResultPrint.java b/src/main/java/lotto/LottoResultPrint.java deleted file mode 100644 index 8ca3076ac32..00000000000 --- a/src/main/java/lotto/LottoResultPrint.java +++ /dev/null @@ -1,24 +0,0 @@ -package lotto; - -public class LottoResultPrint { - 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 = (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 deleted file mode 100644 index 84daa4964bc..00000000000 --- a/src/test/java/lotto/ApplicationTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package lotto; - -import camp.nextstep.edu.missionutils.test.NsTest; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; -import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; -import static org.assertj.core.api.Assertions.assertThat; - -class ApplicationTest extends NsTest { - private static final String ERROR_MESSAGE = "[ERROR]"; - - @Test - void 기능_테스트() { - assertRandomUniqueNumbersInRangeTest( - () -> { - run("8000", "1,2,3,4,5,6", "7"); - assertThat(output()).contains( - "8개를 구매했습니다.", - "[8, 21, 23, 41, 42, 43]", - "[3, 5, 11, 16, 32, 38]", - "[7, 11, 16, 35, 36, 44]", - "[1, 8, 11, 31, 41, 42]", - "[13, 14, 16, 38, 42, 45]", - "[7, 11, 30, 40, 42, 43]", - "[2, 13, 22, 32, 38, 45]", - "[1, 3, 5, 14, 22, 45]", - "3개 일치 (5,000원) - 1개", - "4개 일치 (50,000원) - 0개", - "5개 일치 (1,500,000원) - 0개", - "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", - "6개 일치 (2,000,000,000원) - 0개", - "총 수익률은 62.5%입니다." - ); - }, - List.of(8, 21, 23, 41, 42, 43), - List.of(3, 5, 11, 16, 32, 38), - List.of(7, 11, 16, 35, 36, 44), - List.of(1, 8, 11, 31, 41, 42), - List.of(13, 14, 16, 38, 42, 45), - List.of(7, 11, 30, 40, 42, 43), - List.of(2, 13, 22, 32, 38, 45), - List.of(1, 3, 5, 14, 22, 45) - ); - } - - @Test - void 예외_테스트() { - assertSimpleTest(() -> { - runException("1000j"); - assertThat(output()).contains(ERROR_MESSAGE); - }); - } - - @Override - public void runMain() { - Application.main(new String[]{}); - } -} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java deleted file mode 100644 index 6c090d75406..00000000000 --- a/src/test/java/lotto/LottoTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package lotto; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -class LottoTest { - @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") - @Test - void createLottoByOverSize() { - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) - .isInstanceOf(IllegalArgumentException.class); - } - - @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") - @Test - void createLottoByDuplicatedNumber() { - // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) - .isInstanceOf(IllegalArgumentException.class); - } - - // 아래에 추가 테스트 작성 가능 -} \ No newline at end of file From 8c318fce551bc9318ca89296e332af3cd8457f00 Mon Sep 17 00:00:00 2001 From: wldnd7145 <80200734+wldnd7145@users.noreply.github.com> Date: Thu, 18 Jul 2024 19:55:30 +0900 Subject: [PATCH 6/6] Add files via upload --- .../java/lotto/Controller/Application.java | 28 +++++ src/main/java/lotto/Model/Lotto.java | 22 ++++ .../java/lotto/Model/LottoNumGenerater.java | 21 ++++ .../java/lotto/Model/LottoResultChecker.java | 44 +++++++ src/main/java/lotto/View/LottoInput.java | 119 ++++++++++++++++++ .../java/lotto/View/LottoResultPrinter.java | 24 ++++ src/test/java/lotto/ApplicationTest.java | 62 +++++++++ src/test/java/lotto/LottoTest.java | 28 +++++ 8 files changed, 348 insertions(+) create mode 100644 src/main/java/lotto/Controller/Application.java create mode 100644 src/main/java/lotto/Model/Lotto.java create mode 100644 src/main/java/lotto/Model/LottoNumGenerater.java create mode 100644 src/main/java/lotto/Model/LottoResultChecker.java create mode 100644 src/main/java/lotto/View/LottoInput.java create mode 100644 src/main/java/lotto/View/LottoResultPrinter.java create mode 100644 src/test/java/lotto/ApplicationTest.java create mode 100644 src/test/java/lotto/LottoTest.java 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/Model/Lotto.java b/src/main/java/lotto/Model/Lotto.java new file mode 100644 index 00000000000..8f32ed4cd2c --- /dev/null +++ b/src/main/java/lotto/Model/Lotto.java @@ -0,0 +1,22 @@ +package lotto.Model; + +import java.util.List; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validate(numbers); + this.numbers = numbers; + } + + private void validate(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } + + 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 new file mode 100644 index 00000000000..5ba15edcee0 --- /dev/null +++ b/src/test/java/lotto/ApplicationTest.java @@ -0,0 +1,62 @@ +package lotto; + +import camp.nextstep.edu.missionutils.test.NsTest; +import lotto.Controller.Application; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; +import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; +import static org.assertj.core.api.Assertions.assertThat; + +class ApplicationTest extends NsTest { + private static final String ERROR_MESSAGE = "[ERROR]"; + + @Test + void 기능_테스트() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("8000", "1,2,3,4,5,6", "7"); + assertThat(output()).contains( + "8개를 구매했습니다.", + "[8, 21, 23, 41, 42, 43]", + "[3, 5, 11, 16, 32, 38]", + "[7, 11, 16, 35, 36, 44]", + "[1, 8, 11, 31, 41, 42]", + "[13, 14, 16, 38, 42, 45]", + "[7, 11, 30, 40, 42, 43]", + "[2, 13, 22, 32, 38, 45]", + "[1, 3, 5, 14, 22, 45]", + "3개 일치 (5,000원) - 1개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 0개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", + "6개 일치 (2,000,000,000원) - 0개", + "총 수익률은 62.5%입니다." + ); + }, + List.of(8, 21, 23, 41, 42, 43), + List.of(3, 5, 11, 16, 32, 38), + List.of(7, 11, 16, 35, 36, 44), + List.of(1, 8, 11, 31, 41, 42), + List.of(13, 14, 16, 38, 42, 45), + List.of(7, 11, 30, 40, 42, 43), + List.of(2, 13, 22, 32, 38, 45), + List.of(1, 3, 5, 14, 22, 45) + ); + } + + @Test + void 예외_테스트() { + assertSimpleTest(() -> { + runException("1000j"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + + @Override + public void runMain() { + Application.main(new String[]{}); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java new file mode 100644 index 00000000000..f91c11b1cf2 --- /dev/null +++ b/src/test/java/lotto/LottoTest.java @@ -0,0 +1,28 @@ +package lotto; + +import lotto.Model.Lotto; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class LottoTest { + @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") + @Test + void createLottoByOverSize() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") + @Test + void createLottoByDuplicatedNumber() { + // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) + .isInstanceOf(IllegalArgumentException.class); + } + + // 아래에 추가 테스트 작성 가능 +} \ No newline at end of file