From 33367b19954d27710acd0ea1f6be1b85aff27362 Mon Sep 17 00:00:00 2001 From: genNam Date: Sun, 2 Nov 2025 11:58:16 +0900 Subject: [PATCH 1/4] =?UTF-8?q?docs:=20README=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 5fa2560b46..68771faa21 100644 --- a/README.md +++ b/README.md @@ -1 +1,32 @@ # java-lotto-precourse + +## 기능 요구사항 + +1. **로또 번호 규칙** + - 1~45 사이의 숫자로 구성 + - 1장의 로또는 중복 없는 6개의 숫자로 이루어짐 + +2. **당첨 번호 추첨** + - 6개의 일반 번호 + 1개의 보너스 번호 + +3. **구매 및 발행** + - 로또 1장의 가격: 1,000원 + - 사용자가 입력한 금액에 맞춰 발행 수 계산 + - 발행한 로또 번호는 오름차순으로 출력 + +4. **당첨 기준 및 상금** + - 1등: 6개 번호 일치 / 2,000,000,000원 + - 2등: 5개 번호 + 보너스 번호 일치 / 30,000,000원 + - 3등: 5개 번호 일치 / 1,500,000원 + - 4등: 4개 번호 일치 / 50,000원 + - 5등: 3개 번호 일치 / 5,000원 + +5. **결과 출력** + - 사용자가 구매한 로또와 당첨 번호를 비교해 당첨 내역 계산 + - 총 수익률을 소수점 둘째 자리에서 반올림하여 표시 + +6. **예외 처리** + - 잘못된 입력 시 `IllegalArgumentException` 발생 + - 메시지는 `[ERROR]`로 시작 + - 입력 오류 발생 시 해당 부분부터 재입력 + From a11c64c877a953c7e2608009c2cb451a482de0fb Mon Sep 17 00:00:00 2001 From: genNam Date: Mon, 3 Nov 2025 23:27:24 +0900 Subject: [PATCH 2/4] =?UTF-8?q?docs:=20README=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 68771faa21..5c505657fb 100644 --- a/README.md +++ b/README.md @@ -2,31 +2,36 @@ ## 기능 요구사항 -1. **로또 번호 규칙** - - 1~45 사이의 숫자로 구성 - - 1장의 로또는 중복 없는 6개의 숫자로 이루어짐 - -2. **당첨 번호 추첨** - - 6개의 일반 번호 + 1개의 보너스 번호 - -3. **구매 및 발행** - - 로또 1장의 가격: 1,000원 - - 사용자가 입력한 금액에 맞춰 발행 수 계산 - - 발행한 로또 번호는 오름차순으로 출력 - -4. **당첨 기준 및 상금** +1. **첫번째 입력** + - 로또 구입금액을 입력 받는다. + - 1000원 단위로 입력 받는다. + +2. **구매내역 출력** + - 발행할 로또의 수량을 계산한다. + - 로또를 발행한다. + -번호 범위는 1~45 + -오름차순으로 정렬 + - 로또 수량 과 생성한 로또 리스트를 출력한다. + +3. **두번째 입력** + - 당첨번호를 입력받는다. + - 쉼표 기준으로 번호를 구분한다. + - 보너스 번호도 입력받는다. + +4. **당첨내역 출력** + - 일치하는 개수를 확인하고 + - 구매한 로또 중에 몇개가 맞았는지 출력한다. + - 수익률을 계산한다. + +5. **당첨 기준 및 상금** - 1등: 6개 번호 일치 / 2,000,000,000원 - 2등: 5개 번호 + 보너스 번호 일치 / 30,000,000원 - 3등: 5개 번호 일치 / 1,500,000원 - 4등: 4개 번호 일치 / 50,000원 - 5등: 3개 번호 일치 / 5,000원 -5. **결과 출력** - - 사용자가 구매한 로또와 당첨 번호를 비교해 당첨 내역 계산 - - 총 수익률을 소수점 둘째 자리에서 반올림하여 표시 - 6. **예외 처리** - 잘못된 입력 시 `IllegalArgumentException` 발생 - 메시지는 `[ERROR]`로 시작 - - 입력 오류 발생 시 해당 부분부터 재입력 + - 입력 오류 발생 시 해당 부분부터 재입력 받는다. From 1aaa23ff0164cf357d161ce59aed0469e557357d Mon Sep 17 00:00:00 2001 From: genNam Date: Mon, 3 Nov 2025 23:48:58 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=EB=8F=88=EC=9D=84=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EB=B0=9B=EA=B3=A0=20=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EC=9D=80=20=EC=88=98=EB=9F=89=EB=8C=80=EB=A1=9C=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=ED=95=9C=20=EB=A1=9C=EB=98=90=20=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 10 +++++++++- src/main/java/lotto/Lotto.java | 8 +++++--- src/main/java/lotto/LottoStart.java | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/main/java/lotto/LottoStart.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..0a24ecd6dd 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,15 @@ package lotto; public class Application { + public static void main(String[] args) { - // TODO: 프로그램 구현 + + var Input = new Input(); + + int inputMoney = Input.inputMoney(); + + LottoStart lottoStart = new LottoStart(inputMoney); + lottoStart.printRandomLottos(); + } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 88fc5cf12b..f50e0768f3 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,20 +1,22 @@ package lotto; + import java.util.List; public class Lotto { private final List numbers; public Lotto(List numbers) { - validate(numbers); + validateSix(numbers); + this.numbers = numbers; } - private void validate(List numbers) { + private void validateSix(List numbers) { if (numbers.size() != 6) { throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); } } - // TODO: 추가 기능 구현 } + diff --git a/src/main/java/lotto/LottoStart.java b/src/main/java/lotto/LottoStart.java new file mode 100644 index 0000000000..75ad1e3cb2 --- /dev/null +++ b/src/main/java/lotto/LottoStart.java @@ -0,0 +1,23 @@ +package lotto; + +public class LottoStart { + + private final int inputMoney; + private LottoGenerator lottoGenerator; + + public LottoStart(int inputMoney) { + this.inputMoney = inputMoney; + this.lottoGenerator = new LottoGenerator(inputMoney); + } + + public void printRandomLottos(){ + + System.out.println(inputMoney/1000+"개를 구매했습니다."); + + for(Lotto randomLotto : lottoGenerator.getRandomLotto()){ + System.out.println(randomLotto); + } + + } + +} From db4e38443ff7da72a76d2db8cc236f192b87e264 Mon Sep 17 00:00:00 2001 From: genNam Date: Mon, 3 Nov 2025 23:54:41 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=EB=B2=88?= =?UTF-8?q?=ED=98=B8=EB=A5=BC=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EC=95=84=20?= =?UTF-8?q?=EB=AA=87=EA=B0=9C=20=EB=A7=9E=EC=95=98=EB=8A=94=EC=A7=80=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=ED=95=98=EA=B3=A0=20=EC=88=98=EC=9D=B5?= =?UTF-8?q?=EB=A5=A0=EC=9D=84=20=EA=B3=84=EC=82=B0=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 4 ++ src/main/java/lotto/Input.java | 60 ++++++++++++++++++ src/main/java/lotto/Lotto.java | 21 ++++++- src/main/java/lotto/LottoGenerator.java | 34 ++++++++++ src/main/java/lotto/LottoMatch.java | 82 +++++++++++++++++++++++++ src/main/java/lotto/Prize.java | 20 ++++++ src/main/java/lotto/Validator.java | 66 ++++++++++++++++++++ 7 files changed, 285 insertions(+), 2 deletions(-) create mode 100644 src/main/java/lotto/Input.java create mode 100644 src/main/java/lotto/LottoGenerator.java create mode 100644 src/main/java/lotto/LottoMatch.java create mode 100644 src/main/java/lotto/Prize.java create mode 100644 src/main/java/lotto/Validator.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 0a24ecd6dd..838ff736d0 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -11,5 +11,9 @@ public static void main(String[] args) { LottoStart lottoStart = new LottoStart(inputMoney); lottoStart.printRandomLottos(); + int[] winningNumbers = Input.inputWinningNumbers(); + int bonusNumber = Input.inputBonusNumber(); + lottoStart.lottoMatcher(winningNumbers, bonusNumber); + } } diff --git a/src/main/java/lotto/Input.java b/src/main/java/lotto/Input.java new file mode 100644 index 0000000000..a1773ad27d --- /dev/null +++ b/src/main/java/lotto/Input.java @@ -0,0 +1,60 @@ +package lotto; + +import static camp.nextstep.edu.missionutils.Console.readLine; + +public class Input { + + private final Validator validator; + + public Input() { + this.validator = new Validator(); + } + + public int inputMoney() { + int inputMoney; + while (true) { + try { + System.out.println("구입할 금액을 입력해 주세요."); + inputMoney = Integer.parseInt(readLine()); + validator.thousandUnitValidate(inputMoney); + break; + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] 1000원 단위로 입력해주세요."); + } + } + + return inputMoney; + } + + public int[] inputWinningNumbers() { + + int[] winningNumbers; + while (true) { + try { + System.out.println("당첨 번호를 입력해 주세요."); + String winningNumber = readLine(); + winningNumbers = validator.winningNumberCheck(winningNumber); + break; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + return winningNumbers; + } + + public int inputBonusNumber() { + + String bonusNumber; + while (true) { + try { + System.out.println("보너스 번호를 입력해 주세요."); + bonusNumber = readLine(); + validator.bonusNumberCheck(bonusNumber); + break; + } catch (IllegalArgumentException e) { + System.out.println("[ERROR] 1~45 사이의 번호를 입력해 주세요."); + } + } + return Integer.parseInt(bonusNumber); + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index f50e0768f3..da97d872fb 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,14 +1,15 @@ 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) { validateSix(numbers); - + validateUnique(numbers); this.numbers = numbers; } @@ -18,5 +19,21 @@ private void validateSix(List numbers) { } } + private void validateUnique(List numbers) { + Set unique = new HashSet<>(numbers); + if (unique.size() != numbers.size()) { + throw new IllegalArgumentException("[ERROR] 숫자는 중복될 수 없습니다."); + } + } + + @Override + public String toString() { + return numbers.toString(); + } + + public List getNumbers() { + return numbers; + + } } diff --git a/src/main/java/lotto/LottoGenerator.java b/src/main/java/lotto/LottoGenerator.java new file mode 100644 index 0000000000..0bbdf7b8bb --- /dev/null +++ b/src/main/java/lotto/LottoGenerator.java @@ -0,0 +1,34 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class LottoGenerator { + + private final int lottoCount; + private List randomLottos; + + public LottoGenerator(int inputMoney) { + + this.lottoCount = inputMoney / 1000; + this.randomLottos = new ArrayList<>(); + generateRandomNumber(); + } + + private void generateRandomNumber() { + + for (int i = 0; i < lottoCount; i++) { + List randomLotto = new ArrayList<>(Randoms.pickUniqueNumbersInRange(1, 45, 6)); + Collections.sort(randomLotto); + Lotto lotto = new Lotto(randomLotto); + randomLottos.add(lotto); + } + } + + public List getRandomLotto() { + return randomLottos; + } + +} diff --git a/src/main/java/lotto/LottoMatch.java b/src/main/java/lotto/LottoMatch.java new file mode 100644 index 0000000000..494ff65cc6 --- /dev/null +++ b/src/main/java/lotto/LottoMatch.java @@ -0,0 +1,82 @@ +package lotto; + + +import java.util.List; + +public class LottoMatch { + + private final int[] winningNumbers; + private final int bonusNumber; + private final List generateLottos; + + public LottoMatch(int[] winningNumbers, int bonusNumber, List generateLottos){ + + this.winningNumbers = winningNumbers; + this.bonusNumber = bonusNumber; + this.generateLottos = generateLottos; + calculateMatch(); + + } + public int matchThree = 0; + public int matchFour = 0; + public int matchFive = 0; + public int matchFiveBonus = 0; + public int matchSix = 0; + + public void calculateMatch(){ + + for(Lotto lotto : generateLottos){ + List myNumber = lotto.getNumbers(); + int count = countMatch(myNumber, winningNumbers); + boolean bonusExist = checkBonus(myNumber, bonusNumber); + + switch(count){ + case 3 -> matchThree++; + case 4 -> matchFour++; + case 5 -> matchFive++; + case 6 -> matchSix++; + }; + if(count == 5 && bonusExist){ + matchFiveBonus++; + } + } + + printCountMatchingNumbers(); + + } + private int countMatch(List myNumber, int[] winningNumbers){ + + int count = 0; + for (int number : myNumber) { // depth 1 + if (inWinningNumbers(winningNumbers, number)) { + count++; + } + } + return count; + + } + private boolean checkBonus(List myNumber, int bonusNumber) { + for (int number : myNumber) { + if (number == bonusNumber) { + return true; + } + } + return false; + } + private boolean inWinningNumbers(int[] winningNumbers, int number) { + for (int i = 0; i < winningNumbers.length; i++) { + if (winningNumbers[i] == number) { + return true; + } + } + return false; + } + public void printCountMatchingNumbers(){ + + System.out.println("3개 일치 (5,000원) - " +matchThree +"개"); + System.out.println("4개 일치 (50,000원) - "+matchFour +"개"); + System.out.println("5개 일치 (1,500,000원) - "+matchFive+"개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - "+matchFiveBonus +"개"); + System.out.println("6개 일치 (2,000,000,000원) - "+matchSix+"개"); + } +} diff --git a/src/main/java/lotto/Prize.java b/src/main/java/lotto/Prize.java new file mode 100644 index 0000000000..6dc291614e --- /dev/null +++ b/src/main/java/lotto/Prize.java @@ -0,0 +1,20 @@ +package lotto; + +public enum Prize { + + THREE_MATCH(5000), + FOUR_MATCH(50000), + FIVE_MATCH(1500000), + FIVE_MATCH_BONUS(30000000), + SIX_MATCH(2000000000); + + private final int prizeValue; + + Prize(int prizeValue){ + this.prizeValue = prizeValue; + } + + public int getPrizeValue(){ + return prizeValue; + } +} diff --git a/src/main/java/lotto/Validator.java b/src/main/java/lotto/Validator.java new file mode 100644 index 0000000000..b7892e9839 --- /dev/null +++ b/src/main/java/lotto/Validator.java @@ -0,0 +1,66 @@ +package lotto; + +import java.util.HashSet; +import java.util.Set; + +public class Validator { + + public void thousandUnitValidate(int inputMoney) { + + int inputMoneyCheck = inputMoney % 1000; + + if (inputMoneyCheck != 0) { + throw new IllegalArgumentException(); + } + } + + public int[] winningNumberCheck(String winningNumber) { + + String[] splitNumbers = winningNumber.split(","); + int[] winningNumbers = new int[splitNumbers.length]; + + for (int i = 0; i < winningNumbers.length; i++) { + + winningNumbers[i] = Integer.parseInt(splitNumbers[i]); + + } + + for (int number : winningNumbers) { + if (number < 1 || number > 45) { + throw new IllegalArgumentException("[ERROR] 1~45 사이의 숫자를 입력해 주세요."); + } + } + + sixNumberCheck(winningNumbers); + uniqueNumberCheck(winningNumbers); + + return winningNumbers; + } + + public void bonusNumberCheck(String bonusNumber) { + + int bonusNumberInteger = Integer.parseInt(bonusNumber); + + if (bonusNumberInteger < 1 || bonusNumberInteger > 45) { + throw new IllegalArgumentException(); + } + } + + public void sixNumberCheck(int[] winningNumber) { + + if (winningNumber.length != 6) { + + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } + + public void uniqueNumberCheck(int[] winningNumbers) { + Set uniqueNumber = new HashSet<>(); + for (int number : winningNumbers) { + if (uniqueNumber.contains(number)) { + throw new IllegalArgumentException("[ERROR] 숫자는 중복될 수 없습니다."); + } + uniqueNumber.add(number); + } + } +}