From 6996f889f50fe3c3e35e74e0e5bfedbe8c18463f Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Wed, 13 Mar 2024 12:05:53 +0900 Subject: [PATCH 01/11] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29..ee6dce4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,5 @@ +# 로또 +1. 로또번호 검증 +2. 로또 당첨번호와 로또 번호확인 + - 정규번호 6자리 와 당첨번호 비교, 5자리와 보너스 번호가 맞으면 2등 +3. 당첨결과 프린트 From 7078add9761cf666bb1a6947ff2f6d1ceab488bb Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sat, 16 Mar 2024 10:54:31 +0900 Subject: [PATCH 02/11] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/docs/README.md b/docs/README.md index ee6dce4..5c72536 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,31 @@ -# 로또 -1. 로또번호 검증 -2. 로또 당첨번호와 로또 번호확인 - - 정규번호 6자리 와 당첨번호 비교, 5자리와 보너스 번호가 맞으면 2등 -3. 당첨결과 프린트 +# 로또 +1. 입력 +- 금액 입력 + - 정수인지 검증 + - 0인지 검증 + - 1000원 단위인지 검증 +- 당첨번호 입력 + - 정수인지 검증 + - ,로 구분되어있는지 검증 + - 1~45사이 정수인지 검증 + - 중복된 로또 번호가 존재하는지 검증 + - 개수가 6개인지 검증 +- 보너스번호 입력 + - 정수인지 검증 + - 1~45사이 정수인지 검증 + - 정규 당첨번호에 있는 수인지 검증 +2. 로또 생성 + - 금액//1000 만큼 로또 생성 + - 검증 + - 1~45사이 정수인지 검증 + - 중복된 로또 번호가 존재하는지 검증 + - 개수가 6개인지 검증 +3. 로또 출력 +- 로또 수 출력 +- 로또 번호 출력 +4. 당첨번호와 로또 번호확인 + - 정규번호 6자리 와 당첨번호 비교, 5자리와 보너스 번호가 맞으면 2등 + - 당첨 수 계산 +5. 당첨결과 출력 + - 당첨 내역 출력 + - 수익률 출력 From 4081a5ec7852bd1444c68906c6aea49bd53933e5 Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sat, 16 Mar 2024 10:59:04 +0900 Subject: [PATCH 03/11] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 51 +++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/docs/README.md b/docs/README.md index 5c72536..5122567 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,31 +1,32 @@ # 로또 1. 입력 -- 금액 입력 - - 정수인지 검증 - - 0인지 검증 - - 1000원 단위인지 검증 -- 당첨번호 입력 - - 정수인지 검증 - - ,로 구분되어있는지 검증 - - 1~45사이 정수인지 검증 - - 중복된 로또 번호가 존재하는지 검증 - - 개수가 6개인지 검증 -- 보너스번호 입력 - - 정수인지 검증 - - 1~45사이 정수인지 검증 - - 정규 당첨번호에 있는 수인지 검증 + 1. 금액 입력 + - 정수인지 검증 + - 0인지 검증 + - 1000원 단위인지 검증 + 2. 당첨번호 입력 + - 정수인지 검증 + - ,로 구분되어있는지 검증 + - 1~45사이 정수인지 검증 + - 중복된 로또 번호가 존재하는지 검증 + - 개수가 6개인지 검증 + 3. 보너스번호 입력 + - 정수인지 검증 + - 1~45사이 정수인지 검증 + - 정규 당첨번호에 있는 수인지 검증 2. 로또 생성 - - 금액//1000 만큼 로또 생성 - - 검증 - - 1~45사이 정수인지 검증 - - 중복된 로또 번호가 존재하는지 검증 - - 개수가 6개인지 검증 + 1. 금액//1000 만큼 로또 생성 + 2. 검증 + - 1~45사이 정수인지 검증 + - 중복된 로또 번호가 존재하는지 검증 + - 개수가 6개인지 검증 3. 로또 출력 -- 로또 수 출력 -- 로또 번호 출력 + 1. 로또 수 출력 + 2. 로또 번호 출력 4. 당첨번호와 로또 번호확인 - - 정규번호 6자리 와 당첨번호 비교, 5자리와 보너스 번호가 맞으면 2등 - - 당첨 수 계산 + 1. 정규번호 6자리 와 당첨번호 비교, 5자리와 보너스 번호가 맞으면 2등 + 2. 당첨 수 계산 5. 당첨결과 출력 - - 당첨 내역 출력 - - 수익률 출력 + 1. 당첨 내역 출력 + 2. 수익률 출력 + From 93baef3b74a849a2bab1fee99fe2ae9f4358c4b4 Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sat, 16 Mar 2024 22:25:16 +0900 Subject: [PATCH 04/11] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 20 ----- .../lotto/controller/LottoController.java | 81 +++++++++++++++++++ src/main/java/lotto/domain/Lotto.java | 30 +++++++ src/main/java/lotto/domain/Ranking.java | 54 +++++++++++++ src/main/java/lotto/domain/WinnigResult.java | 18 +++++ .../java/lotto/util/BonusNumValidator.java | 9 +++ src/main/java/lotto/util/Constants.java | 8 ++ .../java/lotto/util/ExceptionMessage.java | 24 ++++++ src/main/java/lotto/util/LottoGenerator.java | 26 ++++++ src/main/java/lotto/util/MoneyValidator.java | 26 ++++++ src/main/java/lotto/util/Validator.java | 25 ++++++ .../lotto/util/WinningNumberValidator.java | 35 ++++++++ src/main/java/lotto/view/InputView.java | 47 +++++++++++ src/main/java/lotto/view/OutputView.java | 23 ++++++ src/test/java/lotto/LottoTest.java | 1 + 15 files changed, 407 insertions(+), 20 deletions(-) delete mode 100644 src/main/java/lotto/Lotto.java create mode 100644 src/main/java/lotto/controller/LottoController.java create mode 100644 src/main/java/lotto/domain/Lotto.java create mode 100644 src/main/java/lotto/domain/Ranking.java create mode 100644 src/main/java/lotto/domain/WinnigResult.java create mode 100644 src/main/java/lotto/util/BonusNumValidator.java create mode 100644 src/main/java/lotto/util/Constants.java create mode 100644 src/main/java/lotto/util/ExceptionMessage.java create mode 100644 src/main/java/lotto/util/LottoGenerator.java create mode 100644 src/main/java/lotto/util/MoneyValidator.java create mode 100644 src/main/java/lotto/util/Validator.java create mode 100644 src/main/java/lotto/util/WinningNumberValidator.java create mode 100644 src/main/java/lotto/view/InputView.java create mode 100644 src/main/java/lotto/view/OutputView.java diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 519793d..0000000 --- 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/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java new file mode 100644 index 0000000..4cd7401 --- /dev/null +++ b/src/main/java/lotto/controller/LottoController.java @@ -0,0 +1,81 @@ +package lotto.controller; + +import lotto.domain.Lotto; +import lotto.domain.Ranking; +import lotto.domain.WinnigResult; +import lotto.util.Constants; +import lotto.util.ExceptionMessage; +import lotto.util.LottoGenerator; +import lotto.util.MoneyValidator; +import lotto.view.InputView; +import lotto.view.OutputView; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class LottoController { + private final InputView inputView; + private final OutputView outputView; + + public LottoController(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void start(){ + String input = inputView.readMoney(); + new MoneyValidator().validate(input); + int money=Integer.parseInt(input); + LottoGenerator lottoGenerator = new LottoGenerator(); + + List lottos = lottoGenerator.generate(money); + List winningNumbers = inputView.readWinningNumbers(); + int bonusNum=inputView.readBonusNumber(); + if (winningNumbers.contains(bonusNum)==true){ + throw new IllegalArgumentException(ExceptionMessage.INVALID_BONUS_NUM.getMessage()); + } + WinnigResult winnigResult = new WinnigResult(new Lotto(winningNumbers), bonusNum); + lottoResult(lottos,winnigResult,money/1000); + + } + private void lottoResult(List lottoList, WinnigResult winnigResult, int ticketNum){ + Map result = setResult(); + Ranking rank; + + OutputView.printSuccessResult(); + for (int i = 0; i < lottoList.size(); i++) { + rank = winnigResult.match(lottoList.get(i)); + + result.put(rank, result.get(rank) + 1); + } + printResult(result); + printEarningRate(result, ticketNum); + } + + private void printResult(Map result) { + for (int i = Ranking.values().length - 1; i >= 0; i--) { + Ranking.values()[i].printMessage(result.get(Ranking.values()[i])); + } + } + + private void printEarningRate(Map result, int lottoAmount) { + double EarningRate = 0; + for (Ranking rank : result.keySet()) { + EarningRate = + EarningRate + ((double) (rank.getWinningAmount()) / (lottoAmount * Constants.LOTTO_PRICE) * (result.get( + rank)) * (100)); + + } + OutputView.printRevenueRate(EarningRate); + } + + private Map setResult() { + Map result = new LinkedHashMap<>(); + + for (Ranking rank : Ranking.values()) { + result.put(rank, 0); + } + return result; + } +} diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java new file mode 100644 index 0000000..86c32c8 --- /dev/null +++ b/src/main/java/lotto/domain/Lotto.java @@ -0,0 +1,30 @@ +package lotto.domain; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + this.numbers = numbers; + Collections.sort(numbers); + } + + + // TODO: 추가 기능 구현 + //public void printResult + public int countMatch(List winningNum){ + return (int) numbers.stream() + .filter(winningNum::contains).count(); + } + public boolean containNum(int num){ + return numbers.contains(num); + } + + public List getNumbers() { + return numbers; + } +} diff --git a/src/main/java/lotto/domain/Ranking.java b/src/main/java/lotto/domain/Ranking.java new file mode 100644 index 0000000..729b40f --- /dev/null +++ b/src/main/java/lotto/domain/Ranking.java @@ -0,0 +1,54 @@ +package lotto.domain; + +import lotto.view.OutputView; + +import java.util.Random; + +public enum Ranking { + FIRST(6, 2_000_000_000, "6개 일치 (2,000,000,000원) - "), // 1등 + SECOND(5, 30_000_000, "5개 일치, 보너스 볼 일치 (30,000,000원) - "), // 2등 + THIRD(5, 1_500_000, "5개 일치 (1,500,000원) - "), // 3등 + FOURTH(4, 50_000, "4개 일치 (50,000원) - "), // 4등 + FIFTH(3, 5_000, "3개 일치 (5,000원) - "), // 5등 + MISS(0, 0, ""); + Ranking(int countOfMatch, int winningAmount, String message) { + this.countOfMatch = countOfMatch; + this.winningAmount = winningAmount; + this.message = message; + } + private int countOfMatch; + private int winningAmount; + private String message; + + private static final int WINNG_MIN_COUNT=3; + public static Ranking valueOf(int countOfMatch, boolean matchBonus){ + if (countOfMatch generate(int money){ + int ticketNum=money/1000; + + OutputView.printTicketCount(ticketNum); + List lottos=new ArrayList<>(); + for (int i=0; ticketNum>i; i++){ + List lottoNum= new ArrayList<>(Randoms.pickUniqueNumbersInRange(MIN_NUM,MAX_NUM,NUM_COUNT)); + Lotto lotto = new Lotto(lottoNum); + lottos.add(lotto); + System.out.println(lotto.getNumbers()); + + } + return lottos; + } +} diff --git a/src/main/java/lotto/util/MoneyValidator.java b/src/main/java/lotto/util/MoneyValidator.java new file mode 100644 index 0000000..ea4007c --- /dev/null +++ b/src/main/java/lotto/util/MoneyValidator.java @@ -0,0 +1,26 @@ +package lotto.util; + +import static lotto.util.Constants.LOTTO_PRICE; +import static lotto.util.ExceptionMessage.*; + +public class MoneyValidator extends Validator{ + @Override + public void validate(String money) throws IllegalStateException { + validateInt(money); + validateMoneyUnit(money); + validateIsZero(money); + } + + private void validateMoneyUnit(String money){ + if (Integer.parseInt(money)% LOTTO_PRICE!=0){ + throw new IllegalArgumentException(INVALID_MONEY_UNIT.getMessage()); + } + } + + private void validateIsZero(String money){ + if(Integer.parseInt(money)==0){ + throw new IllegalArgumentException(ZERO_EXCEPTION.getMessage()); + } + } + +} diff --git a/src/main/java/lotto/util/Validator.java b/src/main/java/lotto/util/Validator.java new file mode 100644 index 0000000..c3cf9ff --- /dev/null +++ b/src/main/java/lotto/util/Validator.java @@ -0,0 +1,25 @@ +package lotto.util; + +import static lotto.util.Constants.MAX_NUM; +import static lotto.util.Constants.MIN_NUM; +import static lotto.util.ExceptionMessage.INVALID_LOTTO_NUMBER_RANGE; +import static lotto.util.ExceptionMessage.NOT_INT; + +public abstract class Validator { + abstract void validate(String input) throws IllegalStateException; + + void validateInt(String input){ + try { + Integer.parseInt(input); + }catch (NumberFormatException e){ + throw new IllegalArgumentException(NOT_INT.getMessage()); + } + } + + void validateLottoNumberRange(String lottoNumber){ + int num= Integer.parseInt(lottoNumber); + if (num< MIN_NUM || num>MAX_NUM){ + throw new IllegalArgumentException(INVALID_LOTTO_NUMBER_RANGE.getMessage()); + } + } +} diff --git a/src/main/java/lotto/util/WinningNumberValidator.java b/src/main/java/lotto/util/WinningNumberValidator.java new file mode 100644 index 0000000..72e7013 --- /dev/null +++ b/src/main/java/lotto/util/WinningNumberValidator.java @@ -0,0 +1,35 @@ +package lotto.util; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static lotto.util.ExceptionMessage.INVALID_WINNING_NUMBER_DUPLICATE; +import static lotto.util.ExceptionMessage.INVALID_WINNING_SIZE; + +public class WinningNumberValidator extends Validator{ + @Override + public void validate(String input) throws IllegalStateException { + List winnigNumbers = new ArrayList<>(List.of(input.split(","))); + validateWinningNumberSize(winnigNumbers); + validateWinningNumberDuplicate(winnigNumbers); + for (String winnigNumber : winnigNumbers) { + validateInt(winnigNumber); + validateLottoNumberRange(winnigNumber); + } + + } + private void validateWinningNumberSize(List winngNumbers){ + if (winngNumbers.size()!=Constants.NUM_COUNT){ + throw new IllegalArgumentException(INVALID_WINNING_SIZE.getMessage()); + } + } + + private void validateWinningNumberDuplicate(List winningNumbers){ + Set set = new HashSet<>(winningNumbers); + if (set.size()!=6){ + throw new IllegalArgumentException(INVALID_WINNING_NUMBER_DUPLICATE.getMessage()); + } + } +} diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java new file mode 100644 index 0000000..daeeefe --- /dev/null +++ b/src/main/java/lotto/view/InputView.java @@ -0,0 +1,47 @@ +package lotto.view; + +import camp.nextstep.edu.missionutils.Console; +import lotto.util.BonusNumValidator; +import lotto.util.MoneyValidator; +import lotto.util.WinningNumberValidator; + +import java.util.ArrayList; +import java.util.List; + +public class InputView { + private enum ConsoleMessage{ + INPUT_MONEY("구매금액을 입력해주세요."), + INPUT_WINNIG_NUMBER("당첨 번호를 입력해주세요."), + INPUT_BONUS_NUMBER("보너스 번호를 입력해주세요"); + private final String message; + ConsoleMessage(String message){ + this.message=message; + } + } + + public String readMoney(){ + System.out.println(ConsoleMessage.INPUT_MONEY.message); + //String money = Console.readLine(); + String money = Console.readLine(); + //new MoneyValidator().validate(money); + return money; + } + + public List readWinningNumbers(){ + System.out.println(ConsoleMessage.INPUT_WINNIG_NUMBER.message); + String input = Console.readLine(); + new WinningNumberValidator().validate(input); + List winnigNumbersString = new ArrayList<>(List.of(input.split(","))); + List winnigNumbersInteger=new ArrayList<>(); + for (String s : winnigNumbersString) { + winnigNumbersInteger.add(Integer.parseInt(s)); + } + return winnigNumbersInteger; + } + + public int readBonusNumber(){ + System.out.println(ConsoleMessage.INPUT_BONUS_NUMBER.message); + String input = Console.readLine(); + return Integer.parseInt(input); + } +} diff --git a/src/main/java/lotto/view/OutputView.java b/src/main/java/lotto/view/OutputView.java new file mode 100644 index 0000000..1c68322 --- /dev/null +++ b/src/main/java/lotto/view/OutputView.java @@ -0,0 +1,23 @@ +package lotto.view; + +public class OutputView { + public static final String TICKET_COUNT = "개를 구매했습니다."; + + public static void printTicketCount(int count) { + System.out.println(); + System.out.println(count + TICKET_COUNT); + } + + public static void printSuccessResult() { + System.out.println("당첨 통계"); + System.out.println("---"); + } + + public static void printSuccessMessage(String message, int numberOfMatch) { + System.out.println(message + numberOfMatch + "개"); + } + + public static void printRevenueRate(double EarningRate) { + System.out.println("총 수익률은 " + String.format("%.1f", EarningRate) + "%입니다."); + } +} \ No newline at end of file diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 0f3af0f..d2bca39 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,5 +1,6 @@ package lotto; +import lotto.domain.Lotto; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From 3f9ecef7998e07b7d458fd8df04e0b93b968111e Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sat, 16 Mar 2024 22:25:34 +0900 Subject: [PATCH 05/11] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 14 ++++++++ src/main/java/lotto/LottoUtils.java | 54 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/main/java/lotto/LottoUtils.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..458d53b 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,21 @@ package lotto; +import lotto.controller.LottoController; +import lotto.view.InputView; +import lotto.view.OutputView; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + InputView inputView = new InputView(); + OutputView outputView = new OutputView(); + LottoController lottoController = new LottoController(inputView,outputView); + lottoController.start(); + } + } diff --git a/src/main/java/lotto/LottoUtils.java b/src/main/java/lotto/LottoUtils.java new file mode 100644 index 0000000..06e8453 --- /dev/null +++ b/src/main/java/lotto/LottoUtils.java @@ -0,0 +1,54 @@ +package lotto; + +import lotto.domain.Lotto; + +import java.util.*; + +public class LottoUtils { + + public List create(){ + Random random = new Random(); + Set lotto = new HashSet<>(); + + while (lotto.size()<6){ + int randomNum = random.nextInt(45)+1; + lotto.add(randomNum); + } + return new ArrayList<>(lotto); + } + + public void printResult(int[] result,int money){ + int sum=0; + System.out.println(); + System.out.println("당첨 통계"); + System.out.println("---"); + System.out.println("3개 일치 (5,000원) - "+result[3]+"개"); + System.out.println("4개 일치 (50,000원) - "+result[4]+"개"); + System.out.println("5개 일치 (1,500,000원) - "+result[5]+"개"); + System.out.println("6개 일치 (2,000,000,000원) - "+result[6]+"개"); + System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - "+result[7]+"개"); + + returnRate(money,sum); + System.out.println(result); + System.out.println(sum); + } + + private void returnRate(int money,int sum) { + double returnRat=(double) sum/(double) money; + System.out.println("총 수익률은 "+ returnRat +"%입니다."); + } + + + public int[] compareNum(int money, List winningNumberInteger, int bonusNum,LottoUtils generator) { + + int[] result = {0,0,0,0,0,0,0,0}; + System.out.println(money/1000+"개를 구매했습니다."); + while (money >=1000) { + Lotto lotto = new Lotto(generator.create()); + money -=1000; + + + } + return result; + } +} From d018c7c30d93483addfba181529bc399280e013a Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:18:25 +0900 Subject: [PATCH 06/11] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EA=B0=B1=EC=8B=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 5122567..48da283 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,6 @@ - 1000원 단위인지 검증 2. 당첨번호 입력 - 정수인지 검증 - - ,로 구분되어있는지 검증 - 1~45사이 정수인지 검증 - 중복된 로또 번호가 존재하는지 검증 - 개수가 6개인지 검증 @@ -20,6 +19,7 @@ - 1~45사이 정수인지 검증 - 중복된 로또 번호가 존재하는지 검증 - 개수가 6개인지 검증 + - 오름차순인지 검증 3. 로또 출력 1. 로또 수 출력 2. 로또 번호 출력 From eb2df15ee59cff3dcd38b9354c7c7453159872c2 Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:20:38 +0900 Subject: [PATCH 07/11] =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index ee0f309..a1a06fb 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,13 @@ repositories { } dependencies { - implementation 'com.github.woowacourse-projects:mission-utils:1.0.0' + implementation 'com.github.kokodak:mission-utils:1.0.0' + + implementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' + implementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1' + implementation 'org.mockito:mockito-inline:3.12.4' + implementation 'org.assertj:assertj-core:3.21.0' + implementation 'org.junit.jupiter:junit-jupiter:5.8.1' } java { @@ -19,4 +25,4 @@ java { test { useJUnitPlatform() -} +} \ No newline at end of file From 92d91fc5c1c4f97683dc05bd20001b934bb1209e Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:20:52 +0900 Subject: [PATCH 08/11] =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=84=B0=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/lotto/ApplicationTest.java | 50 ++++++++++++++++++++---- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index a15c7d1..d3f1135 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -1,13 +1,12 @@ package lotto; -import camp.nextstep.edu.missionutils.test.NsTest; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.kokodak.test.Assertions.assertRandomUniqueNumbersInRangeTest; +import static org.kokodak.test.Assertions.assertSimpleTest; 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; +import org.junit.jupiter.api.Test; +import org.kokodak.test.NsTest; class ApplicationTest extends NsTest { private static final String ERROR_MESSAGE = "[ERROR]"; @@ -58,4 +57,41 @@ class ApplicationTest extends NsTest { public void runMain() { Application.main(new String[]{}); } -} + @Test + void 기능_테스트2등() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("1000", "1,2,3,4,5,7", "42"); + assertThat(output()).contains( + "1개를 구매했습니다.", + "[1, 2, 3, 4, 7, 42]", + "3개 일치 (5,000원) - 0개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 0개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 1개", + "6개 일치 (2,000,000,000원) - 0개", + "총 수익률은 3000000.0%입니다." + ); + }, + List.of(1, 2, 3, 4, 42, 7) + ); + }@Test + void 기능_테스트1등() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("1000", "1,2,3,4,5,6", "7"); + assertThat(output()).contains( + "1개를 구매했습니다.", + "[1, 2, 3, 4, 5, 6]", + "3개 일치 (5,000원) - 0개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 0개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", + "6개 일치 (2,000,000,000원) - 1개", + "총 수익률은 200000000.0%입니다." + ); + }, + List.of(1, 2, 3, 4, 5,6) + ); + } +} \ No newline at end of file From 74e11b051d2cd61e3ef58397f5c2b04baf04e646 Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:21:00 +0900 Subject: [PATCH 09/11] =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=84=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/lotto/InputViewTest.java | 114 +++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/test/java/lotto/InputViewTest.java diff --git a/src/test/java/lotto/InputViewTest.java b/src/test/java/lotto/InputViewTest.java new file mode 100644 index 0000000..1eb6300 --- /dev/null +++ b/src/test/java/lotto/InputViewTest.java @@ -0,0 +1,114 @@ +package lotto; + +import lotto.util.ExceptionMessage; +import lotto.view.InputView; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class InputViewTest { + private InputView inputview= new InputView(); + @DisplayName("금액은 1000원가 아니면 예외가 발생한다. ") + @Test + void 천원단위검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("101".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + assertThatThrownBy(()-> inputview.readMoney()). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.INVALID_MONEY_UNIT.getMessage()); + } + + @DisplayName("0원이면 예외가 발생한다. ") + @Test + void 빵원검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("0".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + assertThatThrownBy(()-> inputview.readMoney()). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.ZERO_EXCEPTION.getMessage()); + } + + @DisplayName("당첨번호가 정수인지 검증") + @Test + void 당첨번호정수검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("1,2,3,4,5,k".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + assertThatThrownBy(()-> inputview.readWinningNumbers()). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.NOT_INT.getMessage()); + } + + @DisplayName("당첨번호가 1~45인지 검증") + @Test + void 당첨번호범위검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("1,2,3,4,5,800".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + assertThatThrownBy(()-> inputview.readWinningNumbers()). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.INVALID_LOTTO_NUMBER_RANGE.getMessage()); + } + + @DisplayName("당첨번호가 6개인지 검증") + @Test + void 당첨번호개수검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("1,2,3,4,5".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + assertThatThrownBy(()-> inputview.readWinningNumbers()). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.INVALID_WINNING_SIZE.getMessage()); + in = new ByteArrayInputStream("1,2,3,4,5,6,7".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + assertThatThrownBy(()-> inputview.readWinningNumbers()). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.INVALID_WINNING_SIZE.getMessage()); + } + @DisplayName("당첨번호중에 중복이 있는지 검증") + @Test + void 당첨번호중복검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("1,2,3,4,5,5".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + assertThatThrownBy(()-> inputview.readWinningNumbers()). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.INVALID_WINNING_NUMBER_DUPLICATE.getMessage()); + } + @DisplayName("보너스번호 정수 검증") + @Test + void 보너스번호정수검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("1k".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + + assertThatThrownBy(()-> inputview.readBonusNumber(List.of(1, 2, 3, 4, 5, 6))). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.NOT_INT.getMessage()); + } + @DisplayName("보너스번호 중복 검증") + @Test + void 보너스번호중복검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("5".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + + assertThatThrownBy(()-> inputview.readBonusNumber(List.of(1, 2, 3, 4, 5, 6))). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.INVALID_BONUS_NUM.getMessage()); + } + + @DisplayName("보너스번호 범위 검증") + @Test + void 보너스번호범위검증(){ + ByteArrayInputStream in = new ByteArrayInputStream("51".getBytes(StandardCharsets.UTF_8)); + System.setIn(in); + + assertThatThrownBy(()-> inputview.readBonusNumber(List.of(1, 2, 3, 4, 5, 6))). + isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.INVALID_LOTTO_NUMBER_RANGE.getMessage()); + } + + + +} From 64cee95bb102c91db95f3972a403f4571f213ae6 Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:21:14 +0900 Subject: [PATCH 10/11] =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/controller/LottoController.java | 22 ++++++------ src/main/java/lotto/domain/Lotto.java | 34 +++++++++++++++++++ .../java/lotto/util/BonusNumValidator.java | 3 ++ src/main/java/lotto/util/LottoGenerator.java | 2 +- src/main/java/lotto/util/Validator.java | 3 ++ src/main/java/lotto/view/InputView.java | 22 ++++++++---- src/test/java/lotto/LottoTest.java | 10 ++++++ 7 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/main/java/lotto/controller/LottoController.java b/src/main/java/lotto/controller/LottoController.java index 4cd7401..9aea8a2 100644 --- a/src/main/java/lotto/controller/LottoController.java +++ b/src/main/java/lotto/controller/LottoController.java @@ -24,20 +24,18 @@ public LottoController(InputView inputView, OutputView outputView) { } public void start(){ - String input = inputView.readMoney(); - new MoneyValidator().validate(input); - int money=Integer.parseInt(input); - LottoGenerator lottoGenerator = new LottoGenerator(); + try { + int money=inputView.readMoney(); + LottoGenerator lottoGenerator = new LottoGenerator(); - List lottos = lottoGenerator.generate(money); - List winningNumbers = inputView.readWinningNumbers(); - int bonusNum=inputView.readBonusNumber(); - if (winningNumbers.contains(bonusNum)==true){ - throw new IllegalArgumentException(ExceptionMessage.INVALID_BONUS_NUM.getMessage()); + List lottos = lottoGenerator.generate(money); + List winningNumbers = inputView.readWinningNumbers(); + int bonusNum = inputView.readBonusNumber(winningNumbers); + WinnigResult winnigResult = new WinnigResult(new Lotto(winningNumbers), bonusNum); + lottoResult(lottos, winnigResult, money / 1000); + }catch (IllegalArgumentException e){ + System.out.println(e.getMessage()); } - WinnigResult winnigResult = new WinnigResult(new Lotto(winningNumbers), bonusNum); - lottoResult(lottos,winnigResult,money/1000); - } private void lottoResult(List lottoList, WinnigResult winnigResult, int ticketNum){ Map result = setResult(); diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 86c32c8..f72e985 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -1,19 +1,52 @@ package lotto.domain; +import lotto.util.Constants; +import lotto.util.ExceptionMessage; + import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import static lotto.util.ExceptionMessage.INVALID_WINNING_NUMBER_DUPLICATE; +import static lotto.util.ExceptionMessage.INVALID_WINNING_SIZE; + public class Lotto { private final List numbers; public Lotto(List numbers) { + validate(numbers); + validateDuplicateNum(numbers); + validateNumRange(numbers); this.numbers = numbers; Collections.sort(numbers); } + private void validate(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException(INVALID_WINNING_SIZE.getMessage()); + } + Set set = new HashSet<>(numbers); + if (set.size()!=6){ + throw new IllegalArgumentException(INVALID_WINNING_NUMBER_DUPLICATE.getMessage()); + } + } + private void validateDuplicateNum(List numbers) { + Set set = new HashSet<>(numbers); + if (set.size()!=6){ + throw new IllegalArgumentException(INVALID_WINNING_NUMBER_DUPLICATE.getMessage()); + } + } + private void validateNumRange(List numbers) { + + for (Integer number : numbers) { + if (number<=0 || number>45){ + throw new IllegalArgumentException(ExceptionMessage.INVALID_LOTTO_NUMBER_RANGE.getMessage()); + } + } + } + // TODO: 추가 기능 구현 //public void printResult public int countMatch(List winningNum){ @@ -27,4 +60,5 @@ public boolean containNum(int num){ public List getNumbers() { return numbers; } + } diff --git a/src/main/java/lotto/util/BonusNumValidator.java b/src/main/java/lotto/util/BonusNumValidator.java index 214b3a3..d9daf03 100644 --- a/src/main/java/lotto/util/BonusNumValidator.java +++ b/src/main/java/lotto/util/BonusNumValidator.java @@ -1,9 +1,12 @@ package lotto.util; +import java.util.List; + public class BonusNumValidator extends Validator{ @Override public void validate(String input) throws IllegalStateException { validateInt(input); validateLottoNumberRange(input); } + } diff --git a/src/main/java/lotto/util/LottoGenerator.java b/src/main/java/lotto/util/LottoGenerator.java index 4b99172..9067a3c 100644 --- a/src/main/java/lotto/util/LottoGenerator.java +++ b/src/main/java/lotto/util/LottoGenerator.java @@ -1,8 +1,8 @@ package lotto.util; -import camp.nextstep.edu.missionutils.Randoms; import lotto.domain.Lotto; import lotto.view.OutputView; +import org.kokodak.Randoms; import java.util.*; diff --git a/src/main/java/lotto/util/Validator.java b/src/main/java/lotto/util/Validator.java index c3cf9ff..08c9d83 100644 --- a/src/main/java/lotto/util/Validator.java +++ b/src/main/java/lotto/util/Validator.java @@ -1,5 +1,7 @@ package lotto.util; +import java.util.List; + import static lotto.util.Constants.MAX_NUM; import static lotto.util.Constants.MIN_NUM; import static lotto.util.ExceptionMessage.INVALID_LOTTO_NUMBER_RANGE; @@ -7,6 +9,7 @@ public abstract class Validator { abstract void validate(String input) throws IllegalStateException; +// abstract void validate(String input, List winningNums) throws IllegalStateException; void validateInt(String input){ try { diff --git a/src/main/java/lotto/view/InputView.java b/src/main/java/lotto/view/InputView.java index daeeefe..5cc6360 100644 --- a/src/main/java/lotto/view/InputView.java +++ b/src/main/java/lotto/view/InputView.java @@ -1,9 +1,11 @@ package lotto.view; -import camp.nextstep.edu.missionutils.Console; + import lotto.util.BonusNumValidator; +import lotto.util.ExceptionMessage; import lotto.util.MoneyValidator; import lotto.util.WinningNumberValidator; +import org.kokodak.Console; import java.util.ArrayList; import java.util.List; @@ -19,12 +21,13 @@ private enum ConsoleMessage{ } } - public String readMoney(){ + public int readMoney(){ System.out.println(ConsoleMessage.INPUT_MONEY.message); //String money = Console.readLine(); - String money = Console.readLine(); - //new MoneyValidator().validate(money); - return money; + + String money = org.kokodak.Console.readLine(); + new MoneyValidator().validate(money); + return Integer.parseInt(money); } public List readWinningNumbers(){ @@ -39,9 +42,14 @@ public List readWinningNumbers(){ return winnigNumbersInteger; } - public int readBonusNumber(){ + public int readBonusNumber(List winningNums){ System.out.println(ConsoleMessage.INPUT_BONUS_NUMBER.message); String input = Console.readLine(); - return Integer.parseInt(input); + new BonusNumValidator().validate(input); + int bonusNum=Integer.parseInt(input); + if (winningNums.contains(bonusNum) == true) { + throw new IllegalArgumentException(ExceptionMessage.INVALID_BONUS_NUM.getMessage()); + } + return bonusNum; } } diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index d2bca39..d593834 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,6 +1,7 @@ package lotto; import lotto.domain.Lotto; +import lotto.util.ExceptionMessage; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -24,5 +25,14 @@ void createLottoByDuplicatedNumber() { .isInstanceOf(IllegalArgumentException.class); } + // 아래에 추가 테스트 작성 가능 + @DisplayName("로또 번호에 범위가 넘어간 숫자가 있으면 예외가 발생한다.") + @Test + void 로또범위테스트() { + // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 51))) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(ExceptionMessage.INVALID_LOTTO_NUMBER_RANGE.getMessage()); + } } From b41e47ebe2a9222989e64dc0a073ec7dec62cc8e Mon Sep 17 00:00:00 2001 From: CheolPark98 <96028198+CheolPark98@users.noreply.github.com> Date: Tue, 19 Mar 2024 09:14:47 +0900 Subject: [PATCH 11/11] Delete LottoUtils.java --- src/main/java/lotto/LottoUtils.java | 54 ----------------------------- 1 file changed, 54 deletions(-) delete mode 100644 src/main/java/lotto/LottoUtils.java diff --git a/src/main/java/lotto/LottoUtils.java b/src/main/java/lotto/LottoUtils.java deleted file mode 100644 index 06e8453..0000000 --- a/src/main/java/lotto/LottoUtils.java +++ /dev/null @@ -1,54 +0,0 @@ -package lotto; - -import lotto.domain.Lotto; - -import java.util.*; - -public class LottoUtils { - - public List create(){ - Random random = new Random(); - Set lotto = new HashSet<>(); - - while (lotto.size()<6){ - int randomNum = random.nextInt(45)+1; - lotto.add(randomNum); - } - return new ArrayList<>(lotto); - } - - public void printResult(int[] result,int money){ - int sum=0; - System.out.println(); - System.out.println("당첨 통계"); - System.out.println("---"); - System.out.println("3개 일치 (5,000원) - "+result[3]+"개"); - System.out.println("4개 일치 (50,000원) - "+result[4]+"개"); - System.out.println("5개 일치 (1,500,000원) - "+result[5]+"개"); - System.out.println("6개 일치 (2,000,000,000원) - "+result[6]+"개"); - System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원) - "+result[7]+"개"); - - returnRate(money,sum); - System.out.println(result); - System.out.println(sum); - } - - private void returnRate(int money,int sum) { - double returnRat=(double) sum/(double) money; - System.out.println("총 수익률은 "+ returnRat +"%입니다."); - } - - - public int[] compareNum(int money, List winningNumberInteger, int bonusNum,LottoUtils generator) { - - int[] result = {0,0,0,0,0,0,0,0}; - System.out.println(money/1000+"개를 구매했습니다."); - while (money >=1000) { - Lotto lotto = new Lotto(generator.create()); - money -=1000; - - - } - return result; - } -}