diff --git a/README.md b/README.md index 5fa2560b46..5c505657fb 100644 --- a/README.md +++ b/README.md @@ -1 +1,37 @@ # java-lotto-precourse + +## 기능 요구사항 + +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원 + +6. **예외 처리** + - 잘못된 입력 시 `IllegalArgumentException` 발생 + - 메시지는 `[ERROR]`로 시작 + - 입력 오류 발생 시 해당 부분부터 재입력 받는다. + diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..838ff736d0 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,19 @@ 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(); + + 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 88fc5cf12b..da97d872fb 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,20 +1,39 @@ 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); + validateSix(numbers); + validateUnique(numbers); this.numbers = numbers; } - private void validate(List numbers) { + private void validateSix(List numbers) { if (numbers.size() != 6) { throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); } } - // TODO: 추가 기능 구현 + 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/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); + } + + } + +} 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); + } + } +}