From 958541e9c5ea99fe99c663258e8dc02ccf7c1381 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:09:12 +0900 Subject: [PATCH 01/32] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EA=B5=AC?= =?UTF-8?q?=EB=A7=A4=ED=95=A0=20=EA=B8=88=EC=95=A1=20=EC=9E=85=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/view/InputView.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/view/InputView.java diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 00000000..dfc734d7 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,16 @@ +package view; + +import java.util.Scanner; + +public class InputView { + + private final static String INPUT_PRICE = "구입금액을 입력해 주세요."; + private static final Scanner scanner = new Scanner(System.in); + + + public int inputPrice(){ + System.out.println(INPUT_PRICE); + int price = scanner.nextInt(); + return price; + } +} From 86275867b87fa57c08c1e14448cba48c82018dcc Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:09:20 +0900 Subject: [PATCH 02/32] =?UTF-8?q?feat:=20=EA=B5=AC=EB=A7=A4=ED=95=9C=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/view/OutputView.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/view/OutputView.java diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 00000000..7226ff81 --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,16 @@ +package view; + +import java.util.List; + +public class OutputView { + + private final static String BUY_MESSAGE = "개를 구매했습니다."; + + public void countPrint (int count){ + System.out.println("\n" + count + BUY_MESSAGE); + + } + public void lottoPrint(List numbers) { + System.out.println(numbers); + } +} From c0861500d45ecdc49c4704e71b1914aeeb11111b Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:09:38 +0900 Subject: [PATCH 03/32] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=20=EB=B2=88=ED=98=B8=EB=A1=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/AutoLotto.java | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/model/AutoLotto.java diff --git a/src/main/java/model/AutoLotto.java b/src/main/java/model/AutoLotto.java new file mode 100644 index 00000000..287069a6 --- /dev/null +++ b/src/main/java/model/AutoLotto.java @@ -0,0 +1,36 @@ +package model; + +import java.util.*; + +public class AutoLotto { + + private static final int MAX_LOTTO_NUMBER = 45; + private static final int CNT_LOTTO_NUMBER = 6; + + private static List lottoNums = new ArrayList<>(); + public AutoLotto(){ + createAutoLotto(); + } + public void createAutoLotto(){ + Random random = new Random(); + Set uniqueNumbers = new TreeSet<>(); // 중복되지 않는 숫자를 보장하는 TreeSet 사용 + + // 로또 번호 생성 + while (uniqueNumbers.size() < CNT_LOTTO_NUMBER) { + int num = random.nextInt(MAX_LOTTO_NUMBER) + 1; + uniqueNumbers.add(num); + } + + lottoNums = new ArrayList<>(uniqueNumbers); + + } + public List sortLotto(List lottoNums){ + List sortLottoList = new ArrayList<>(lottoNums); + sortLottoList.sort(Comparator.naturalOrder()); + return sortLottoList; + } + public List getAutoLotto(){ + return lottoNums; + } + +} From d435b8e5be50c38e5b0fd63a3015ce1987798313 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:09:47 +0900 Subject: [PATCH 04/32] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/Lotto.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/model/Lotto.java diff --git a/src/main/java/model/Lotto.java b/src/main/java/model/Lotto.java new file mode 100644 index 00000000..a3bd0f70 --- /dev/null +++ b/src/main/java/model/Lotto.java @@ -0,0 +1,16 @@ +package model; + +import java.util.List; + +public class Lotto { + private static final int BONUS_NUMBER_INDEX = 6; + private final List numbers; + + public Lotto(List numbers) { + this.numbers = numbers; + } + + public ListgetNumbers(){ + return numbers; + } +} From 07b7bc73f30be653d69b28c756c47c6b918a2ccc Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:09:55 +0900 Subject: [PATCH 05/32] =?UTF-8?q?feat:=20=EC=B4=9D=20=EA=B5=AC=EB=A7=A4?= =?UTF-8?q?=ED=95=9C=20=EB=A1=9C=EB=98=90=EB=A5=BC=20list=EB=A1=9C=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/LottoList.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/model/LottoList.java diff --git a/src/main/java/model/LottoList.java b/src/main/java/model/LottoList.java new file mode 100644 index 00000000..f15102d7 --- /dev/null +++ b/src/main/java/model/LottoList.java @@ -0,0 +1,22 @@ +package model; + +import java.util.ArrayList; +import java.util.List; + +public class LottoList { + List lottoList = new ArrayList<>(); + private int totalPrice; + + public LottoList(Lotto lotto){ + lottoList.add(lotto); + } + public List getLottoList(){ + return lottoList; + } + public void setTotalPrice(int reward){ + this.totalPrice+=reward; + } + public int getTotalPrice(){ + return totalPrice; + } +} From 5ed6feaceaa9578d6e696cd752abf4569c073b39 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:10:13 +0900 Subject: [PATCH 06/32] =?UTF-8?q?feat:=20=EA=B5=AC=EB=A7=A4=ED=95=A0=20?= =?UTF-8?q?=EA=B0=80=EA=B2=A9=20=EC=9E=85=EB=A0=A5=20=ED=9B=84=20=ED=95=B4?= =?UTF-8?q?=EB=8B=B9=20=EA=B0=80=EA=B2=A9=20=EB=A7=8C=ED=81=BC=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/LottoController.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/controller/LottoController.java diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java new file mode 100644 index 00000000..42b66c53 --- /dev/null +++ b/src/main/java/controller/LottoController.java @@ -0,0 +1,48 @@ +package controller; + +import model.AutoLotto; +import model.Lotto; +import model.LottoList; +import view.InputView; +import view.OutputView; + +import java.util.ArrayList; +import java.util.List; + +public class LottoController { + private InputView inputView = new InputView(); + private OutputView outputView = new OutputView(); + private AutoLotto autoLotto; + private LottoList lottoList; + + + public LottoController(){ + lottoStart(); + } + public void lottoStart() { + int price = inputMoney(); + viewLotto(price); + } + + public int inputMoney(){ + return inputView.inputPrice(); + } + public void viewLotto(int price){ + int count = price/1000; + outputView.countPrint(count); + lottoList = makeLottolist(count); + } + public LottoList makeLottolist(int count){ + for(int i=0; i lotto = new ArrayList<>(); + AutoLotto autoLotto = new AutoLotto(); + lotto = autoLotto.getAutoLotto(); + outputView.lottoPrint(lotto); + return new Lotto(lotto); + } +} From 2fd7c316bebae88497d7072bedf989fc67ab6148 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:10:20 +0900 Subject: [PATCH 07/32] =?UTF-8?q?feat:=20=EC=8B=A4=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/Application.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..2588f582 --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,10 @@ +import controller.LottoController; + +import java.io.IOException; + +public class Application { + + public static void main(String [] args) throws IOException { + LottoController lottoController = new LottoController(); + } +} From ebbd2b3e4b3169d487503e245a42e865cfd8fb27 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:38:39 +0900 Subject: [PATCH 08/32] =?UTF-8?q?feat:=20=EB=9E=9C=EB=8D=A4=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EC=83=9D=EC=84=B1=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/AutoLotto.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/model/AutoLotto.java b/src/main/java/model/AutoLotto.java index 287069a6..c7890631 100644 --- a/src/main/java/model/AutoLotto.java +++ b/src/main/java/model/AutoLotto.java @@ -1,9 +1,10 @@ package model; import java.util.*; +import java.util.stream.IntStream; public class AutoLotto { - + private static final int MIN_LOTTO_NUMBER = 1; private static final int MAX_LOTTO_NUMBER = 45; private static final int CNT_LOTTO_NUMBER = 6; @@ -12,16 +13,16 @@ public AutoLotto(){ createAutoLotto(); } public void createAutoLotto(){ - Random random = new Random(); - Set uniqueNumbers = new TreeSet<>(); // 중복되지 않는 숫자를 보장하는 TreeSet 사용 - // 로또 번호 생성 - while (uniqueNumbers.size() < CNT_LOTTO_NUMBER) { - int num = random.nextInt(MAX_LOTTO_NUMBER) + 1; - uniqueNumbers.add(num); + List numbers = new ArrayList<>(); + IntStream.rangeClosed(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER).forEach(lottoNums::add); + Collections.shuffle(lottoNums); + for(int i = 0; i < CNT_LOTTO_NUMBER; i++){ + numbers.add(lottoNums.get(i)); } + Collections.sort(numbers); - lottoNums = new ArrayList<>(uniqueNumbers); + lottoNums = new ArrayList<>(numbers); } public List sortLotto(List lottoNums){ From 2a199f14aa83734fe9d7184b8581b3ed41c1c600 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Wed, 1 May 2024 21:40:03 +0900 Subject: [PATCH 09/32] =?UTF-8?q?feat:=20=EC=A0=95=EB=A0=AC=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/AutoLotto.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/model/AutoLotto.java b/src/main/java/model/AutoLotto.java index c7890631..ca3e352e 100644 --- a/src/main/java/model/AutoLotto.java +++ b/src/main/java/model/AutoLotto.java @@ -25,11 +25,6 @@ public void createAutoLotto(){ lottoNums = new ArrayList<>(numbers); } - public List sortLotto(List lottoNums){ - List sortLottoList = new ArrayList<>(lottoNums); - sortLottoList.sort(Comparator.naturalOrder()); - return sortLottoList; - } public List getAutoLotto(){ return lottoNums; } From 2d59e3d81e29a6a75ea8967784d3c0145b48ec8c Mon Sep 17 00:00:00 2001 From: nyeroni Date: Thu, 2 May 2024 16:47:40 +0900 Subject: [PATCH 10/32] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=20=EC=83=9D=EC=84=B1=20=EC=A4=91=EB=B3=B5=20=EB=B0=A9?= =?UTF-8?q?=EC=A7=80=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/model/AutoLotto.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/model/AutoLotto.java b/src/main/java/model/AutoLotto.java index ca3e352e..7f19e4bf 100644 --- a/src/main/java/model/AutoLotto.java +++ b/src/main/java/model/AutoLotto.java @@ -15,15 +15,21 @@ public AutoLotto(){ public void createAutoLotto(){ List numbers = new ArrayList<>(); - IntStream.rangeClosed(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER).forEach(lottoNums::add); - Collections.shuffle(lottoNums); - for(int i = 0; i < CNT_LOTTO_NUMBER; i++){ - numbers.add(lottoNums.get(i)); + for (int i = MIN_LOTTO_NUMBER; i <= MAX_LOTTO_NUMBER; i++) { + numbers.add(i); } - Collections.sort(numbers); - - lottoNums = new ArrayList<>(numbers); - + Collections.shuffle(numbers); + lottoNums = new ArrayList<>(); + for (int i = 0; i < CNT_LOTTO_NUMBER; i++) { + int uniqueNumber = numbers.get(i); + while (lottoNums.contains(uniqueNumber)) { + // 중복된 번호라면 다시 랜덤하게 선택 + Collections.shuffle(numbers); + uniqueNumber = numbers.get(i); + } + lottoNums.add(uniqueNumber); + } + Collections.sort(lottoNums); } public List getAutoLotto(){ return lottoNums; From 6b2269ceedd60431b4c78688ce4500a7dab45045 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Thu, 2 May 2024 16:48:26 +0900 Subject: [PATCH 11/32] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EB=B0=8F=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=20=EA=B0=9C=EC=88=98,=20=EC=88=98=EC=9D=B5=EB=A5=A0=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=20=EB=B0=8F=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/LottoController.java | 44 +++++++++++++++++-- src/main/java/model/Lotto.java | 25 ++++++++++- src/main/java/view/InputView.java | 13 ++++-- src/main/java/view/OutputView.java | 17 +++++++ 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java index 42b66c53..1cf1ab35 100644 --- a/src/main/java/controller/LottoController.java +++ b/src/main/java/controller/LottoController.java @@ -12,20 +12,25 @@ public class LottoController { private InputView inputView = new InputView(); private OutputView outputView = new OutputView(); - private AutoLotto autoLotto; private LottoList lottoList; - public LottoController(){ lottoStart(); } public void lottoStart() { int price = inputMoney(); viewLotto(price); + Lotto winningLotto = winnerNumberToInteger(); + int[] match = match(lottoList, winningLotto); + double rate = calculateRate(match, price); + outputView.resultPrint(match, rate); } public int inputMoney(){ - return inputView.inputPrice(); + return toIntegerPrice(inputView.inputPrice()); + } + public int toIntegerPrice(String price){ + return Integer.parseInt(price); } public void viewLotto(int price){ int count = price/1000; @@ -45,4 +50,37 @@ public Lotto makeLotto(){ outputView.lottoPrint(lotto); return new Lotto(lotto); } + + public Lotto winnerNumberToInteger(){ + String winnerNumsStr = inputView.inputWinnerNumber(); + Lotto winningLotto = new Lotto(); + return winningLotto.toIntegerWinner(winnerNumsStr); + } + + + public int [] match(LottoList lottoList, Lotto winningLotto){ + int [] arr = new int[4]; + for(Lotto lotto : lottoList.getLottoList()) { + int cnt = lotto.winningCalculate(winningLotto); + if(cnt == 3){ + arr[0] ++; + } + else if (cnt == 4){ + arr[1] ++; + } + else if(cnt == 5){ + arr[2] ++; + } + else if(cnt == 6){ + arr[3] ++; + } + } + return arr; + } + public double calculateRate(int arr[], int price){ + int total = arr[0] * 5000 + arr[1] * 50000 + arr[2] * 1500000 + arr[3] * 2000000000; + double rate = (double) total / (double) price; + return rate; + } + } diff --git a/src/main/java/model/Lotto.java b/src/main/java/model/Lotto.java index a3bd0f70..e969c89d 100644 --- a/src/main/java/model/Lotto.java +++ b/src/main/java/model/Lotto.java @@ -1,16 +1,39 @@ package model; +import java.util.ArrayList; import java.util.List; public class Lotto { private static final int BONUS_NUMBER_INDEX = 6; - private final List numbers; + private List numbers; public Lotto(List numbers) { this.numbers = numbers; } + public Lotto(){ + } + public Lotto toIntegerWinner(String winningNumbersStr) { + String[] str = winningNumbersStr.split(",\\s*"); + List winningLottoList = new ArrayList<>(); + for (String s : str) { + winningLottoList.add(Integer.parseInt(s)); + } + Lotto winningLotto = new Lotto(winningLottoList); + return winningLotto; + } public ListgetNumbers(){ return numbers; } + + public int winningCalculate(Lotto winningLotto) { + int cnt = 0; + List winningLottoList = winningLotto.getNumbers(); + for (Integer integer : numbers) { + if(numbers.contains(integer)){ + cnt ++; + } + } + return cnt; + } } diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index dfc734d7..0012752f 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -5,12 +5,19 @@ public class InputView { private final static String INPUT_PRICE = "구입금액을 입력해 주세요."; + private final static String INPUT_WINNER_NUMBER = "지난 주 당첨 번호를 입력해 주세요."; + private static final Scanner scanner = new Scanner(System.in); - public int inputPrice(){ + public String inputPrice(){ System.out.println(INPUT_PRICE); - int price = scanner.nextInt(); - return price; + return scanner.nextLine(); + } + + public String inputWinnerNumber(){ + System.out.println(); + System.out.println(INPUT_WINNER_NUMBER); + return scanner.nextLine(); } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 7226ff81..11b35628 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -5,6 +5,11 @@ public class OutputView { private final static String BUY_MESSAGE = "개를 구매했습니다."; + private final static String THREE_MATCHES = "3개 일치 (5000원)- "; + private final static String FOUR_MATCHES = "4개 일치 (50000원)- "; + private final static String FIVE_MATCHES = "5개 일치 (1500000원)- "; + private final static String SIX_MATCHES = "6개 일치 (2000000000원)- "; + private final static String COUNT = "개"; public void countPrint (int count){ System.out.println("\n" + count + BUY_MESSAGE); @@ -13,4 +18,16 @@ public void countPrint (int count){ public void lottoPrint(List numbers) { System.out.println(numbers); } + + public void resultPrint(int [] arr, double rate){ + System.out.println(); + System.out.println("당첨 통계"); + System.out.println("---------"); + System.out.println(THREE_MATCHES + arr[0] + COUNT); + System.out.println(FOUR_MATCHES + arr[1] + COUNT); + System.out.println(FIVE_MATCHES + arr[2] + COUNT); + System.out.println(SIX_MATCHES + arr[3] + COUNT); + + System.out.println("총 수익률은 " + rate + " 입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)"); + } } From 7052bdd3219e63a2e68ebc3851ea60b584fcf95d Mon Sep 17 00:00:00 2001 From: nyeroni Date: Fri, 3 May 2024 21:37:57 +0900 Subject: [PATCH 12/32] =?UTF-8?q?feat:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=AA=85=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EB=A1=9C=EB=98=90?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/LottoController.java | 91 ++++++++++--------- src/main/java/model/Lotto.java | 6 +- src/main/java/model/LottoList.java | 4 +- src/main/java/view/OutputView.java | 16 ++-- 4 files changed, 62 insertions(+), 55 deletions(-) diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java index 1cf1ab35..b44587a0 100644 --- a/src/main/java/controller/LottoController.java +++ b/src/main/java/controller/LottoController.java @@ -15,72 +15,77 @@ public class LottoController { private LottoList lottoList; public LottoController(){ - lottoStart(); + startLottoGame(); } - public void lottoStart() { - int price = inputMoney(); - viewLotto(price); - Lotto winningLotto = winnerNumberToInteger(); - int[] match = match(lottoList, winningLotto); - double rate = calculateRate(match, price); - outputView.resultPrint(match, rate); + public void startLottoGame() { + int price = receiveMoneyInput(); + displayLottoTickets(price); + Lotto winningNumbers = getWinningNumbers(); + int[] matchCounts = calculateMatches(lottoList, winningNumbers); + double winRate = calculateWinningRate(matchCounts, price); + outputView.printResults(matchCounts, winRate); } - public int inputMoney(){ - return toIntegerPrice(inputView.inputPrice()); + public int receiveMoneyInput(){ + return convertPriceToInt(inputView.inputPrice()); } - public int toIntegerPrice(String price){ + public int convertPriceToInt(String price){ return Integer.parseInt(price); } - public void viewLotto(int price){ + public void displayLottoTickets(int price){ int count = price/1000; - outputView.countPrint(count); - lottoList = makeLottolist(count); + outputView.printTicketCount(count); + lottoList = generateLottoList(count); + System.out.println("=========="); + for(Lotto lotto : lottoList.getLottoList()){ + System.out.println(lotto.getNumbers()); + } + System.out.println("=========="); } - public LottoList makeLottolist(int count){ + public LottoList generateLottoList(int count){ + lottoList = new LottoList(); for(int i=0; i lotto = new ArrayList<>(); + + public Lotto createLotto(){ + List numbers = new ArrayList<>(); AutoLotto autoLotto = new AutoLotto(); - lotto = autoLotto.getAutoLotto(); - outputView.lottoPrint(lotto); - return new Lotto(lotto); + numbers = autoLotto.getAutoLotto(); + outputView.printLottoNumbers(numbers); + return new Lotto(numbers); } - public Lotto winnerNumberToInteger(){ - String winnerNumsStr = inputView.inputWinnerNumber(); + public Lotto getWinningNumbers(){ + String winnerNumbersStr = inputView.inputWinnerNumber(); Lotto winningLotto = new Lotto(); - return winningLotto.toIntegerWinner(winnerNumsStr); + return winningLotto.convertToList(winnerNumbersStr); } - - public int [] match(LottoList lottoList, Lotto winningLotto){ - int [] arr = new int[4]; - for(Lotto lotto : lottoList.getLottoList()) { - int cnt = lotto.winningCalculate(winningLotto); - if(cnt == 3){ - arr[0] ++; + public int [] calculateMatches(LottoList ticketList, Lotto winningNumbers){ + int [] matchCounts = new int[4]; + for(Lotto ticket : ticketList.getLottoList()) { + int countMatches = ticket.calculateMatches(winningNumbers); + if(countMatches == 3){ + matchCounts[0] ++; } - else if (cnt == 4){ - arr[1] ++; + else if (countMatches == 4){ + matchCounts[1] ++; } - else if(cnt == 5){ - arr[2] ++; + else if(countMatches == 5){ + matchCounts[2] ++; } - else if(cnt == 6){ - arr[3] ++; + else if(countMatches == 6){ + matchCounts[3] ++; } } - return arr; + return matchCounts; } - public double calculateRate(int arr[], int price){ - int total = arr[0] * 5000 + arr[1] * 50000 + arr[2] * 1500000 + arr[3] * 2000000000; - double rate = (double) total / (double) price; - return rate; + public double calculateWinningRate(int [] matchCounts, int price){ + int totalWinnings = matchCounts[0] * 5000 + matchCounts[1] * 50000 + matchCounts[2] * 1500000 + matchCounts[3] * 2000000000; + double winningRate = (double) totalWinnings / (double) price; + return winningRate; } - } diff --git a/src/main/java/model/Lotto.java b/src/main/java/model/Lotto.java index e969c89d..3c816cd9 100644 --- a/src/main/java/model/Lotto.java +++ b/src/main/java/model/Lotto.java @@ -13,7 +13,7 @@ public Lotto(List numbers) { public Lotto(){ } - public Lotto toIntegerWinner(String winningNumbersStr) { + public Lotto convertToList(String winningNumbersStr) { String[] str = winningNumbersStr.split(",\\s*"); List winningLottoList = new ArrayList<>(); for (String s : str) { @@ -26,10 +26,10 @@ public Lotto toIntegerWinner(String winningNumbersStr) { return numbers; } - public int winningCalculate(Lotto winningLotto) { + public int calculateMatches(Lotto winningLotto) { int cnt = 0; List winningLottoList = winningLotto.getNumbers(); - for (Integer integer : numbers) { + for (Integer integer : winningLottoList) { if(numbers.contains(integer)){ cnt ++; } diff --git a/src/main/java/model/LottoList.java b/src/main/java/model/LottoList.java index f15102d7..2e661774 100644 --- a/src/main/java/model/LottoList.java +++ b/src/main/java/model/LottoList.java @@ -6,8 +6,10 @@ public class LottoList { List lottoList = new ArrayList<>(); private int totalPrice; + public LottoList(){ - public LottoList(Lotto lotto){ + } + public void setLottoList(Lotto lotto){ lottoList.add(lotto); } public List getLottoList(){ diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 11b35628..d51bc7a5 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -9,24 +9,24 @@ public class OutputView { private final static String FOUR_MATCHES = "4개 일치 (50000원)- "; private final static String FIVE_MATCHES = "5개 일치 (1500000원)- "; private final static String SIX_MATCHES = "6개 일치 (2000000000원)- "; - private final static String COUNT = "개"; + private final static String COUNT_SUFFIX = "개"; - public void countPrint (int count){ + public void printTicketCount (int count){ System.out.println("\n" + count + BUY_MESSAGE); } - public void lottoPrint(List numbers) { + public void printLottoNumbers(List numbers) { System.out.println(numbers); } - public void resultPrint(int [] arr, double rate){ + public void printResults(int [] arr, double rate){ System.out.println(); System.out.println("당첨 통계"); System.out.println("---------"); - System.out.println(THREE_MATCHES + arr[0] + COUNT); - System.out.println(FOUR_MATCHES + arr[1] + COUNT); - System.out.println(FIVE_MATCHES + arr[2] + COUNT); - System.out.println(SIX_MATCHES + arr[3] + COUNT); + System.out.println(THREE_MATCHES + arr[0] + COUNT_SUFFIX); + System.out.println(FOUR_MATCHES + arr[1] + COUNT_SUFFIX); + System.out.println(FIVE_MATCHES + arr[2] + COUNT_SUFFIX); + System.out.println(SIX_MATCHES + arr[3] + COUNT_SUFFIX); System.out.println("총 수익률은 " + rate + " 입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)"); } From 43753014d9e3cab7e763abb8ec401603cc20a277 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Sat, 4 May 2024 11:11:54 +0900 Subject: [PATCH 13/32] =?UTF-8?q?Refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/LottoController.java | 5 ----- src/main/java/model/AutoLotto.java | 2 -- src/main/java/model/LottoList.java | 6 ------ 3 files changed, 13 deletions(-) diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java index b44587a0..f9ffff61 100644 --- a/src/main/java/controller/LottoController.java +++ b/src/main/java/controller/LottoController.java @@ -36,11 +36,6 @@ public void displayLottoTickets(int price){ int count = price/1000; outputView.printTicketCount(count); lottoList = generateLottoList(count); - System.out.println("=========="); - for(Lotto lotto : lottoList.getLottoList()){ - System.out.println(lotto.getNumbers()); - } - System.out.println("=========="); } public LottoList generateLottoList(int count){ lottoList = new LottoList(); diff --git a/src/main/java/model/AutoLotto.java b/src/main/java/model/AutoLotto.java index 7f19e4bf..d7f65064 100644 --- a/src/main/java/model/AutoLotto.java +++ b/src/main/java/model/AutoLotto.java @@ -1,7 +1,6 @@ package model; import java.util.*; -import java.util.stream.IntStream; public class AutoLotto { private static final int MIN_LOTTO_NUMBER = 1; @@ -23,7 +22,6 @@ public void createAutoLotto(){ for (int i = 0; i < CNT_LOTTO_NUMBER; i++) { int uniqueNumber = numbers.get(i); while (lottoNums.contains(uniqueNumber)) { - // 중복된 번호라면 다시 랜덤하게 선택 Collections.shuffle(numbers); uniqueNumber = numbers.get(i); } diff --git a/src/main/java/model/LottoList.java b/src/main/java/model/LottoList.java index 2e661774..ce3ef403 100644 --- a/src/main/java/model/LottoList.java +++ b/src/main/java/model/LottoList.java @@ -15,10 +15,4 @@ public void setLottoList(Lotto lotto){ public List getLottoList(){ return lottoList; } - public void setTotalPrice(int reward){ - this.totalPrice+=reward; - } - public int getTotalPrice(){ - return totalPrice; - } } From e2860dac3549df27b0c2b42736b339573488cb49 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 16:33:57 +0900 Subject: [PATCH 14/32] =?UTF-8?q?feat:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..e5b08ef0 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +## 🔨 구현 기능 목록 + +### 입력 + +- 구입 금액 입력 + - 한 장당 1000원 + - 1000원으로 나누어 떨어지지 않으면 예외 처리 +- 수동으로 구매할 로또 수 입력 + - 숫자 외의 입력은 모두 예외처리 +- 수동으로 구매할 로또 번호 입력 + - 번호는 쉼표(,)로 구분 + - 숫자와 쉼표(,) 외의 입력은 모두 예외 처리 + - 6개가 되지 않는다면 예외 처리 +- 당첨 번호 입력 + - 번호는 쉼표(,)로 구분 + - 숫자와 쉼표(,) 외의 입력은 모두 예외 처리 + - 6개가 되지 않는다면 예외 처리 +- 보너스 번호 입력 + - 숫자 외의 입력은 모두 예외처리 + + +### 출력 + + +- 수동 로또와 자동 로또 발행 + - 오름차순으로 정렬해서 보여줌 + - 중복되지 않는 6개 숫자 +- 당첨 내역 출력 + - `3개 일치 (5000원) - n개` + - `4개 일치 (50000원) - n개` + - `5개 일치 (1500000원) - n개` + - `5개 일치, 보너스 볼 일치 (30000000원) - n개` + - `6개 일치 (2000000000원) - n개` +- 수익률 출력 + - `총 수익률은 n입니다.` + - 소숫점 둘째자리 밑으론 버림 +- 예외 상황시 에러 문구 출력 + - `[ERROR] error message` + +### 게임 진행 + +- 구입 금액을 입력 +- 수동으로 구매할 로또 수 입력 +- 수동 로또 번호 입력 +- 로또 번호 1~45 사이의 중복되지 않는 정수로 6개 무작위 추첨해서 구입한 개수(수동 로또 개수 제외) 만큼 자동 로또 발행 +- 당첨 번호와 보너스 번호를 입력받음 +- 일치하는 내역과 총 수익률을 출력 +- 예외가 발생할 경우 예외 문구 출력 \ No newline at end of file From 49773c8eb8c3be62fed11f2eeb522bb5e3c99ff9 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 16:34:33 +0900 Subject: [PATCH 15/32] =?UTF-8?q?feat:=20=EC=B6=9C=EB=A0=A5=20=EB=AC=B8?= =?UTF-8?q?=EA=B5=AC=20enum=EC=9C=BC=EB=A1=9C=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/WinningType.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/model/WinningType.java diff --git a/src/main/java/model/WinningType.java b/src/main/java/model/WinningType.java new file mode 100644 index 00000000..1343fcae --- /dev/null +++ b/src/main/java/model/WinningType.java @@ -0,0 +1,19 @@ +package model; + +public enum WinningType { + FIRST("6개 일치 (2000000000원)"), + SECOND("5개 일치, 보너스 볼 일치 (30000000원)"), + THIRD("5개 일치 (1500000원)"), + FOURTH("4개 일치 (50000원)"), + FIFTH("3개 일치 (5000원)"); + + private String sameCountMessage; + + WinningType(String sameCountMessage) { + this.sameCountMessage = sameCountMessage; + } + + public String getSameCountMessage() { + return sameCountMessage; + } +} From 9c2832071b9d0b30dcb46aa854c1981e703593bd Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 16:35:02 +0900 Subject: [PATCH 16/32] =?UTF-8?q?feat:=20=EC=B6=9C=EB=A0=A5=20=EB=AC=B8?= =?UTF-8?q?=EA=B5=AC=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EB=A1=9C=EB=98=90?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/view/OutputView.java | 36 +++++++++++++++++------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index d51bc7a5..16aa4da4 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,33 +1,37 @@ package view; +import model.Lotto; +import model.LottoList; +import model.WinningType; + import java.util.List; public class OutputView { - private final static String BUY_MESSAGE = "개를 구매했습니다."; - private final static String THREE_MATCHES = "3개 일치 (5000원)- "; - private final static String FOUR_MATCHES = "4개 일치 (50000원)- "; - private final static String FIVE_MATCHES = "5개 일치 (1500000원)- "; - private final static String SIX_MATCHES = "6개 일치 (2000000000원)- "; + private final static String BUY_MESSAGE = "수동으로 %d장, 자동으로 %d개를 구매했습니다."; private final static String COUNT_SUFFIX = "개"; - public void printTicketCount (int count){ - System.out.println("\n" + count + BUY_MESSAGE); - } - public void printLottoNumbers(List numbers) { - System.out.println(numbers); + public void printTicketCount (int autoCount, int manualCount){ + System.out.println(); + String message = String.format(BUY_MESSAGE, manualCount, autoCount); + System.out.println(message); } + public void printLottoNumbers(LottoList lottoList) { + List lottos = lottoList.getLottoList(); + for (Lotto lotto : lottos) { + System.out.println(lotto.getNumbers()); + } } public void printResults(int [] arr, double rate){ System.out.println(); System.out.println("당첨 통계"); System.out.println("---------"); - System.out.println(THREE_MATCHES + arr[0] + COUNT_SUFFIX); - System.out.println(FOUR_MATCHES + arr[1] + COUNT_SUFFIX); - System.out.println(FIVE_MATCHES + arr[2] + COUNT_SUFFIX); - System.out.println(SIX_MATCHES + arr[3] + COUNT_SUFFIX); - - System.out.println("총 수익률은 " + rate + " 입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)"); + System.out.println(WinningType.FIFTH.getSameCountMessage() + arr[0] + COUNT_SUFFIX); + System.out.println(WinningType.FOURTH.getSameCountMessage() + arr[1] + COUNT_SUFFIX); + System.out.println(WinningType.THIRD.getSameCountMessage() + arr[2] + COUNT_SUFFIX); + System.out.println(WinningType.SECOND.getSameCountMessage() + arr[3] + COUNT_SUFFIX); + System.out.println(WinningType.FIRST.getSameCountMessage() + arr[4] + COUNT_SUFFIX); + System.out.printf("총 수익률은 %.2f 입니다. (기준이 1이기 때문에 결과적으로 손해라는 의미임)%n", rate); } } From 2fe510566907621cbc9023c8b1330f89647a7d05 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 16:35:27 +0900 Subject: [PATCH 17/32] =?UTF-8?q?feat:=20=EB=B3=80=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EB=B3=B4=EB=84=88=EC=8A=A4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EB=A7=A4=EC=B9=98=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/Lotto.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/model/Lotto.java b/src/main/java/model/Lotto.java index 3c816cd9..2088d109 100644 --- a/src/main/java/model/Lotto.java +++ b/src/main/java/model/Lotto.java @@ -13,14 +13,14 @@ public Lotto(List numbers) { public Lotto(){ } - public Lotto convertToList(String winningNumbersStr) { - String[] str = winningNumbersStr.split(",\\s*"); - List winningLottoList = new ArrayList<>(); + public Lotto convertToList(String lottoNumbersStr) { + String[] str = lottoNumbersStr.split(",\\s*"); + List lottoList = new ArrayList<>(); for (String s : str) { - winningLottoList.add(Integer.parseInt(s)); + lottoList.add(Integer.parseInt(s)); } - Lotto winningLotto = new Lotto(winningLottoList); - return winningLotto; + Lotto lotto = new Lotto(lottoList); + return lotto; } public ListgetNumbers(){ return numbers; @@ -36,4 +36,10 @@ public int calculateMatches(Lotto winningLotto) { } return cnt; } + public boolean bonusMatches(int bonusNum) { + if(numbers.contains(bonusNum)){ + return true; + } + return false; + } } From 4595b554f5d5dbdb58e8e1b0be0049a72bff51b5 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 16:35:37 +0900 Subject: [PATCH 18/32] =?UTF-8?q?feat:=20=EC=88=98=EB=8F=99=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/view/InputView.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index 0012752f..fdae7afc 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -1,11 +1,17 @@ package view; +import java.util.ArrayList; +import java.util.List; import java.util.Scanner; public class InputView { private final static String INPUT_PRICE = "구입금액을 입력해 주세요."; + private final static String MANUAL_BUY_LOTTO = "수동으로 구매할 로또 수를 입력해 주세요."; + private final static String INPUT_MANUAL_LOTTO_NUMBERS = "수동으로 구매할 번호를 입력해 주세요."; private final static String INPUT_WINNER_NUMBER = "지난 주 당첨 번호를 입력해 주세요."; + private final static String INPUT_BONUS_NUMBER = "보너스 볼을 입력해 주세요."; + private static final Scanner scanner = new Scanner(System.in); @@ -15,9 +21,32 @@ public String inputPrice(){ return scanner.nextLine(); } + public String inputManualLottoCount(){ + System.out.println(); + System.out.println(MANUAL_BUY_LOTTO); + return scanner.nextLine(); + } + + public List manualLottos(int cnt){ + System.out.println(); + System.out.println(INPUT_MANUAL_LOTTO_NUMBERS); + List manualLottoNums = new ArrayList<>(); + for (int i = 0; i < cnt; i++) { + manualLottoNums.add(scanner.nextLine()); + } + return manualLottoNums; + } + public String inputWinnerNumber(){ System.out.println(); System.out.println(INPUT_WINNER_NUMBER); return scanner.nextLine(); } + + public String inputBonusNumber(){ + + System.out.println(); + System.out.println(INPUT_BONUS_NUMBER); + return scanner.nextLine(); + } } From 561a3b330bb4f4398f879ea879f504a7260440b5 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 16:35:50 +0900 Subject: [PATCH 19/32] =?UTF-8?q?feat:=20=EC=88=98=EB=8F=99=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=EB=A1=9C=EB=98=90?= =?UTF-8?q?=20=EB=AA=A9=EB=A1=9D=20=EC=B6=9C=EB=A0=A5=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/LottoController.java | 71 +++++++++++++------ 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java index f9ffff61..0dde522c 100644 --- a/src/main/java/controller/LottoController.java +++ b/src/main/java/controller/LottoController.java @@ -19,48 +19,72 @@ public LottoController(){ } public void startLottoGame() { int price = receiveMoneyInput(); - displayLottoTickets(price); + int manualLottoCount = manualLottoCount(); + displayLottoTickets(price, manualLottoNumbers(manualLottoCount)); Lotto winningNumbers = getWinningNumbers(); - int[] matchCounts = calculateMatches(lottoList, winningNumbers); + int bonusNum = receiveBonusNumberInput(); + + int[] matchCounts = calculateMatches(lottoList, winningNumbers, bonusNum); double winRate = calculateWinningRate(matchCounts, price); - outputView.printResults(matchCounts, winRate); + outputView.printResults(matchCounts, Math.floor(winRate*100)/100 ); + } + private int manualLottoCount(){ + return convertStringToInt(inputView.inputManualLottoCount()); + } + private List manualLottoNumbers(int count){ + List manualLottoNums = inputView.manualLottos(count); + List manualLottos = new ArrayList<>(); + + for (String manualLotto : manualLottoNums) { + Lotto lotto = new Lotto(); + manualLottos.add(lotto.convertToList(manualLotto)); + } + return manualLottos; } - public int receiveMoneyInput(){ - return convertPriceToInt(inputView.inputPrice()); + private int receiveBonusNumberInput() { + return convertStringToInt(inputView.inputBonusNumber()); } - public int convertPriceToInt(String price){ + + private int receiveMoneyInput(){ + return convertStringToInt(inputView.inputPrice()); + } + private int convertStringToInt(String price){ return Integer.parseInt(price); } - public void displayLottoTickets(int price){ - int count = price/1000; - outputView.printTicketCount(count); - lottoList = generateLottoList(count); + private void displayLottoTickets(int price, List lottos){ + int autoCount = price/1000 - lottos.size(); + int manualCount = lottos.size(); + outputView.printTicketCount(autoCount, manualCount); + lottoList = generateLottoList(autoCount, lottos); + outputView.printLottoNumbers(lottoList); } - public LottoList generateLottoList(int count){ + private LottoList generateLottoList(int count, List lottos){ lottoList = new LottoList(); + + for (Lotto lotto : lottos) { + lottoList.setLottoList(lotto); + } for(int i=0; i numbers = new ArrayList<>(); AutoLotto autoLotto = new AutoLotto(); numbers = autoLotto.getAutoLotto(); - outputView.printLottoNumbers(numbers); return new Lotto(numbers); } - public Lotto getWinningNumbers(){ + private Lotto getWinningNumbers(){ String winnerNumbersStr = inputView.inputWinnerNumber(); Lotto winningLotto = new Lotto(); return winningLotto.convertToList(winnerNumbersStr); } - public int [] calculateMatches(LottoList ticketList, Lotto winningNumbers){ - int [] matchCounts = new int[4]; + private int [] calculateMatches(LottoList ticketList, Lotto winningNumbers, int bonusNum){ + int [] matchCounts = new int[5]; for(Lotto ticket : ticketList.getLottoList()) { int countMatches = ticket.calculateMatches(winningNumbers); if(countMatches == 3){ @@ -70,16 +94,19 @@ else if (countMatches == 4){ matchCounts[1] ++; } else if(countMatches == 5){ - matchCounts[2] ++; + if(ticket.bonusMatches(bonusNum)){ + matchCounts[3] ++; + } + else matchCounts[2] ++; } else if(countMatches == 6){ - matchCounts[3] ++; + matchCounts[4] ++; } } return matchCounts; } - public double calculateWinningRate(int [] matchCounts, int price){ - int totalWinnings = matchCounts[0] * 5000 + matchCounts[1] * 50000 + matchCounts[2] * 1500000 + matchCounts[3] * 2000000000; + private double calculateWinningRate(int [] matchCounts, int price){ + int totalWinnings = matchCounts[0] * 5000 + matchCounts[1] * 50000 + matchCounts[2] * 1500000 + matchCounts[3] * 30000000 + matchCounts[4] * 2000000000; double winningRate = (double) totalWinnings / (double) price; return winningRate; } From 0e387fb5c897921d0d85b5d7410402aa61cd8c98 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:49:21 +0900 Subject: [PATCH 20/32] =?UTF-8?q?feat:=20enum=ED=83=80=EC=9E=85=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 --- src/main/java/model/WinningType.java | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/model/WinningType.java b/src/main/java/model/WinningType.java index 1343fcae..02925f40 100644 --- a/src/main/java/model/WinningType.java +++ b/src/main/java/model/WinningType.java @@ -1,19 +1,27 @@ package model; public enum WinningType { - FIRST("6개 일치 (2000000000원)"), - SECOND("5개 일치, 보너스 볼 일치 (30000000원)"), - THIRD("5개 일치 (1500000원)"), - FOURTH("4개 일치 (50000원)"), - FIFTH("3개 일치 (5000원)"); + FIFTH(3, 5000, "3개 일치 (5000원)- "), + FOURTH(4, 50000, "4개 일치 (50000원)- "), + THIRD(5, 150000, "5개 일치 (1500000원)- "), + SECOND(5, 30000000, "5개 일치, 보너스 볼 일치(30000000원)- "), + FIRTH(6, 2000000000, "6개 일치 (2000000000원)- "); - private String sameCountMessage; + private static final int WINNING_MIN_COUNT = 3; + private static final String ERROR_MESSAGE = "[ERROR]"; - WinningType(String sameCountMessage) { - this.sameCountMessage = sameCountMessage; + private int countOfCorrect; + private int reward; + private String message; + + WinningType(int countOfCorrect, int reward, String message) { + this.countOfCorrect = countOfCorrect; + this.reward = reward; + this.message = message; } - public String getSameCountMessage() { - return sameCountMessage; + public String getMessage(){ + return message; } + } From cfe53f4309e15a91ff89d837a842329afbea3c08 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:49:51 +0900 Subject: [PATCH 21/32] =?UTF-8?q?feat:=20=ED=88=AC=EC=9E=85=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=20=EB=B0=8F=20=EB=A1=9C=EB=98=90=20=EA=B5=AC=EB=A7=A4?= =?UTF-8?q?=20=EA=B0=9C=EC=88=98=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/UserCount.java | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/model/UserCount.java diff --git a/src/main/java/model/UserCount.java b/src/main/java/model/UserCount.java new file mode 100644 index 00000000..0b110d66 --- /dev/null +++ b/src/main/java/model/UserCount.java @@ -0,0 +1,48 @@ +package model; + +public class UserCount { + + private static final int LOTTO_UNIT = 1000; + + private final int count; + private final int price; + + public UserCount(String price){ + int priceNum = convertStringToInt(price); + validate(priceNum); + this.price = priceNum; + this.count = calculateCount(priceNum); + } + public int calculateCount(int price){ + return price/1000; + } + public int getCount(){ + return count; + } + public int getPrice(){ + return price; + } + public int convertStringToInt(String price){ + try { + return Integer.parseInt(price); + } catch (NumberFormatException e) { + System.err.println("[ERROR] 숫자로만 입력해주세요."); + throw e; + } + } + + public void validate(int priceNum){ + validateRange(priceNum); + validateUnit(priceNum); + } + public void validateRange(int priceNum){ + if(priceNum<=0){ + throw new IllegalArgumentException(); + } + } + public void validateUnit(int priceNum){ + if(priceNum % LOTTO_UNIT != 0){ + throw new IllegalArgumentException("[ERROR] 구입 금액은 1000원 단위로 입력되어야 합니다."); + } + } +} From ef9bdabda9d54490cbfae34fc261aef5625c6ac6 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:50:04 +0900 Subject: [PATCH 22/32] =?UTF-8?q?feat:=20=EC=88=98=EB=8F=99=20=EA=B0=9C?= =?UTF-8?q?=EC=88=98=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EB=B0=8F?= =?UTF-8?q?=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/ManualCount.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/model/ManualCount.java diff --git a/src/main/java/model/ManualCount.java b/src/main/java/model/ManualCount.java new file mode 100644 index 00000000..e0eec989 --- /dev/null +++ b/src/main/java/model/ManualCount.java @@ -0,0 +1,22 @@ +package model; + +public class ManualCount { + private int manualCount; + + public ManualCount(String manualCount){ + this.manualCount = convertStringToInt(manualCount) + ; + } + + public int convertStringToInt(String manualCount){ + try { + return Integer.parseInt(manualCount); + } catch (NumberFormatException e) { + System.err.println("[ERROR] 숫자로만 입력해주세요."); + throw e; + } + } + public int getManualCount(){ + return manualCount; + } +} From 3d69130be733a7c786d782b90977c61e7d7845d0 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:50:19 +0900 Subject: [PATCH 23/32] =?UTF-8?q?feat:=20=EB=B3=B4=EB=84=88=EC=8A=A4?= =?UTF-8?q?=EB=84=98=EB=B2=84=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=8F=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/BonusNumber.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/model/BonusNumber.java diff --git a/src/main/java/model/BonusNumber.java b/src/main/java/model/BonusNumber.java new file mode 100644 index 00000000..8bbea538 --- /dev/null +++ b/src/main/java/model/BonusNumber.java @@ -0,0 +1,23 @@ +package model; + +public class BonusNumber { + + private int bonusNumber; + + public BonusNumber(String bonusNumber){ + this.bonusNumber = convertStringToInt(bonusNumber) + ; + } + + public int convertStringToInt(String bonusNumber){ + try { + return Integer.parseInt(bonusNumber); + } catch (NumberFormatException e) { + System.err.println("[ERROR] 숫자로만 입력해주세요."); + throw e; + } + } + public int getBonusNumber(){ + return bonusNumber; + } +} From f1804d51b4da71f96a16c4297d4965776827149c Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:50:30 +0900 Subject: [PATCH 24/32] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/Lotto.java | 47 +++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/main/java/model/Lotto.java b/src/main/java/model/Lotto.java index 2088d109..149dfaaf 100644 --- a/src/main/java/model/Lotto.java +++ b/src/main/java/model/Lotto.java @@ -11,17 +11,50 @@ public Lotto(List numbers) { this.numbers = numbers; } public Lotto(){ - } public Lotto convertToList(String lottoNumbersStr) { String[] str = lottoNumbersStr.split(",\\s*"); List lottoList = new ArrayList<>(); for (String s : str) { - lottoList.add(Integer.parseInt(s)); + lottoList.add(convertStringToInt(s)); } + Lotto lotto = new Lotto(lottoList); + validate(lotto.getNumbers()); return lotto; } + private int convertStringToInt(String price){ + try { + return Integer.parseInt(price); + } catch (NumberFormatException e) { + System.err.println("[ERROR] 숫자로만 입력해주세요."); + throw e; + } + } + private void validate(List numbers){ + validateSize(numbers); + validateRange(numbers); + validateDuplicate(numbers); + } + private void validateSize(List numbers){ + if(numbers.size() != 6){ + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개 입력해야 합니다."); + } + } + private void validateRange(List numbers){ + for(int num : numbers){ + if(num<=0||num>45){ + throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자로 입력해야 합니다."); + } + } + } + private void validateDuplicate(List numbers){ + List tmp = numbers; + if(tmp.stream().distinct().count()!=numbers.size()){ + throw new IllegalArgumentException("[ERROR] 로또 번호에 중복된 숫자가 있습니다."); + } + + } public ListgetNumbers(){ return numbers; } @@ -36,10 +69,10 @@ public int calculateMatches(Lotto winningLotto) { } return cnt; } - public boolean bonusMatches(int bonusNum) { - if(numbers.contains(bonusNum)){ - return true; - } - return false; + + public void validateDuplicateWithBonus(int bonusNumber) { + if (numbers.contains(bonusNumber)) { + throw new IllegalArgumentException("[ERROR] 보너스 번호는 당첨 번호와 중복되어서는 안 됩니다."); + } } } From 652c0853aa1befe5356792b87848e05f7a843435 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:50:48 +0900 Subject: [PATCH 25/32] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EB=B0=8F=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/controller/LottoController.java | 127 +++++++++++------- 1 file changed, 81 insertions(+), 46 deletions(-) diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java index 0dde522c..81e9fdcc 100644 --- a/src/main/java/controller/LottoController.java +++ b/src/main/java/controller/LottoController.java @@ -1,35 +1,43 @@ package controller; -import model.AutoLotto; -import model.Lotto; -import model.LottoList; +import model.*; import view.InputView; import view.OutputView; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.function.Predicate; public class LottoController { - private InputView inputView = new InputView(); - private OutputView outputView = new OutputView(); + private InputView inputView; + private OutputView outputView; private LottoList lottoList; - public LottoController(){ + public LottoController() { + this.inputView = new InputView(); + this.outputView = new OutputView(); startLottoGame(); } public void startLottoGame() { - int price = receiveMoneyInput(); - int manualLottoCount = manualLottoCount(); - displayLottoTickets(price, manualLottoNumbers(manualLottoCount)); + UserCount userMoney = receiveMoneyInput(); + ManualCount manualCount = manualLottoCount(); + if(manualCount.getManualCount() == 0){ + displayLottoTickets(userMoney, null); + } + else{ + displayLottoTickets(userMoney, manualLottoNumbers(manualCount.getManualCount())); + } Lotto winningNumbers = getWinningNumbers(); - int bonusNum = receiveBonusNumberInput(); + BonusNumber bonusNum = receiveBonusNumberInput(); + validateDuplicate(winningNumbers, bonusNum); - int[] matchCounts = calculateMatches(lottoList, winningNumbers, bonusNum); - double winRate = calculateWinningRate(matchCounts, price); + List matchCounts = calculateMatches(lottoList, winningNumbers, bonusNum); + double winRate = calculateWinningRate(matchCounts, userMoney); outputView.printResults(matchCounts, Math.floor(winRate*100)/100 ); } - private int manualLottoCount(){ - return convertStringToInt(inputView.inputManualLottoCount()); + private ManualCount manualLottoCount(){ + return new ManualCount(inputView.inputManualLottoCount()); } private List manualLottoNumbers(int count){ List manualLottoNums = inputView.manualLottos(count); @@ -42,19 +50,29 @@ private List manualLottoNumbers(int count){ return manualLottos; } - private int receiveBonusNumberInput() { - return convertStringToInt(inputView.inputBonusNumber()); + private BonusNumber receiveBonusNumberInput() { + return new BonusNumber(inputView.inputBonusNumber()); } - private int receiveMoneyInput(){ - return convertStringToInt(inputView.inputPrice()); + private void validateDuplicate(Lotto winningLotto, BonusNumber bonusNumber){ + winningLotto.validateDuplicateWithBonus(bonusNumber.getBonusNumber()); } - private int convertStringToInt(String price){ - return Integer.parseInt(price); + + private UserCount receiveMoneyInput() { + return new UserCount(inputView.inputPrice()); } - private void displayLottoTickets(int price, List lottos){ - int autoCount = price/1000 - lottos.size(); - int manualCount = lottos.size(); + + private void displayLottoTickets(UserCount count, List lottos){ + int autoCount = 0; + int manualCount = 0; + if(lottos == null){ + autoCount = count.getCount(); + manualCount = 0; + } + else{ + autoCount = count.getCount() - lottos.size(); + manualCount = lottos.size(); + } outputView.printTicketCount(autoCount, manualCount); lottoList = generateLottoList(autoCount, lottos); outputView.printLottoNumbers(lottoList); @@ -62,8 +80,10 @@ private void displayLottoTickets(int price, List lottos){ private LottoList generateLottoList(int count, List lottos){ lottoList = new LottoList(); - for (Lotto lotto : lottos) { - lottoList.setLottoList(lotto); + if(lottos != null){ + for (Lotto lotto : lottos) { + lottoList.setLottoList(lotto); + } } for(int i=0; i calculateMatches(LottoList ticketList, Lotto winningNumbers, BonusNumber bonusNum) { + List matchCounts = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0)); + + for (Lotto ticket : ticketList.getLottoList()) { int countMatches = ticket.calculateMatches(winningNumbers); - if(countMatches == 3){ - matchCounts[0] ++; - } - else if (countMatches == 4){ - matchCounts[1] ++; - } - else if(countMatches == 5){ - if(ticket.bonusMatches(bonusNum)){ - matchCounts[3] ++; - } - else matchCounts[2] ++; - } - else if(countMatches == 6){ - matchCounts[4] ++; - } + updateMatchCounts(matchCounts, countMatches, bonusNum); } + return matchCounts; } - private double calculateWinningRate(int [] matchCounts, int price){ - int totalWinnings = matchCounts[0] * 5000 + matchCounts[1] * 50000 + matchCounts[2] * 1500000 + matchCounts[3] * 30000000 + matchCounts[4] * 2000000000; - double winningRate = (double) totalWinnings / (double) price; + + + private void updateMatchCounts(List matchCounts, int countMatches, BonusNumber bonusNum) { + Predicate isBonusMatch = num -> num == 5 && bonusNum.getBonusNumber() > 0; + + if (countMatches == 3) { + matchCounts.set(0, matchCounts.get(0) + 1); + } else if (countMatches == 4) { + matchCounts.set(1, matchCounts.get(1) + 1); + } else if (countMatches == 5) { + if (isBonusMatch.test(countMatches)) { + matchCounts.set(3, matchCounts.get(3) + 1); + } else { + matchCounts.set(2, matchCounts.get(2) + 1); + } + } else if (countMatches == 6) { + matchCounts.set(4, matchCounts.get(4) + 1); + } + + } + private double calculateWinningRate(List matchCounts, UserCount count) { + int totalWinnings = matchCounts.get(0) * 5000 + + matchCounts.get(1) * 50000 + + matchCounts.get(2) * 1500000 + + matchCounts.get(3) * 30000000 + + matchCounts.get(4) * 2000000000; + + double winningRate = (double) totalWinnings / (double) count.getPrice(); return winningRate; } + } From 7beec8d7ba25ec35cc4267b89004e742a48c9b35 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:50:58 +0900 Subject: [PATCH 26/32] =?UTF-8?q?feat:=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/view/OutputView.java | 33 ++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 16aa4da4..3bd253d7 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -23,15 +23,36 @@ public void printLottoNumbers(LottoList lottoList) { } } - public void printResults(int [] arr, double rate){ + public void printResults(List matchCounts, double rate) { + printResultHeader(); + printMatchCounts(matchCounts); + printWinningRate(rate); + } + + private void printResultHeader() { System.out.println(); System.out.println("당첨 통계"); System.out.println("---------"); - System.out.println(WinningType.FIFTH.getSameCountMessage() + arr[0] + COUNT_SUFFIX); - System.out.println(WinningType.FOURTH.getSameCountMessage() + arr[1] + COUNT_SUFFIX); - System.out.println(WinningType.THIRD.getSameCountMessage() + arr[2] + COUNT_SUFFIX); - System.out.println(WinningType.SECOND.getSameCountMessage() + arr[3] + COUNT_SUFFIX); - System.out.println(WinningType.FIRST.getSameCountMessage() + arr[4] + COUNT_SUFFIX); + } + + private void printMatchCounts(List matchCounts) { + WinningType[] winningTypes = WinningType.values(); + + for (int i = 0; i < matchCounts.size(); i++) { + WinningType type = winningTypes[i]; + int count = matchCounts.get(i); + + String message = getMessageForWinningType(type, count); + System.out.println(message); + } + } + private String getMessageForWinningType(WinningType type, int count) { + String message = type.getMessage(); + return message + count + COUNT_SUFFIX + " - " + count + "개"; + + } + + private void printWinningRate(double rate) { System.out.printf("총 수익률은 %.2f 입니다. (기준이 1이기 때문에 결과적으로 손해라는 의미임)%n", rate); } } From 48b8f002ce908aeacb307087bd4334c1452b99b4 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:53:16 +0900 Subject: [PATCH 27/32] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=20=EB=AC=B8?= =?UTF-8?q?=EA=B5=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/UserCount.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/model/UserCount.java b/src/main/java/model/UserCount.java index 0b110d66..fb70118b 100644 --- a/src/main/java/model/UserCount.java +++ b/src/main/java/model/UserCount.java @@ -37,7 +37,7 @@ public void validate(int priceNum){ } public void validateRange(int priceNum){ if(priceNum<=0){ - throw new IllegalArgumentException(); + throw new IllegalArgumentException("0원 이하는 구매가 불가능합니다"); } } public void validateUnit(int priceNum){ From 04d0f96a54bd949a681871e9c5e525528957a75a Mon Sep 17 00:00:00 2001 From: nyeroni Date: Mon, 6 May 2024 23:59:32 +0900 Subject: [PATCH 28/32] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=20=EB=AC=B8?= =?UTF-8?q?=EA=B5=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/Lotto.java | 6 +++--- src/main/java/model/UserCount.java | 2 +- src/main/java/model/WinningType.java | 4 ---- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/model/Lotto.java b/src/main/java/model/Lotto.java index 149dfaaf..cc4ac863 100644 --- a/src/main/java/model/Lotto.java +++ b/src/main/java/model/Lotto.java @@ -36,19 +36,19 @@ private void validate(List numbers){ validateRange(numbers); validateDuplicate(numbers); } - private void validateSize(List numbers){ + void validateSize(List numbers){ if(numbers.size() != 6){ throw new IllegalArgumentException("[ERROR] 로또 번호는 6개 입력해야 합니다."); } } - private void validateRange(List numbers){ + void validateRange(List numbers){ for(int num : numbers){ if(num<=0||num>45){ throw new IllegalArgumentException("[ERROR] 로또 번호는 1부터 45 사이의 숫자로 입력해야 합니다."); } } } - private void validateDuplicate(List numbers){ + void validateDuplicate(List numbers){ List tmp = numbers; if(tmp.stream().distinct().count()!=numbers.size()){ throw new IllegalArgumentException("[ERROR] 로또 번호에 중복된 숫자가 있습니다."); diff --git a/src/main/java/model/UserCount.java b/src/main/java/model/UserCount.java index fb70118b..efd84137 100644 --- a/src/main/java/model/UserCount.java +++ b/src/main/java/model/UserCount.java @@ -37,7 +37,7 @@ public void validate(int priceNum){ } public void validateRange(int priceNum){ if(priceNum<=0){ - throw new IllegalArgumentException("0원 이하는 구매가 불가능합니다"); + throw new IllegalArgumentException("[ERROR] 0원 이하는 구매가 불가능합니다"); } } public void validateUnit(int priceNum){ diff --git a/src/main/java/model/WinningType.java b/src/main/java/model/WinningType.java index 02925f40..2212a341 100644 --- a/src/main/java/model/WinningType.java +++ b/src/main/java/model/WinningType.java @@ -10,13 +10,9 @@ public enum WinningType { private static final int WINNING_MIN_COUNT = 3; private static final String ERROR_MESSAGE = "[ERROR]"; - private int countOfCorrect; - private int reward; private String message; WinningType(int countOfCorrect, int reward, String message) { - this.countOfCorrect = countOfCorrect; - this.reward = reward; this.message = message; } From 2fd040c2f808cc76a0de5737e1507890fce7f938 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Tue, 7 May 2024 00:04:43 +0900 Subject: [PATCH 29/32] =?UTF-8?q?feat:=20=EC=83=9D=EC=84=B1=EC=9E=90=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/model/UserCount.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/model/UserCount.java b/src/main/java/model/UserCount.java index efd84137..1f366a13 100644 --- a/src/main/java/model/UserCount.java +++ b/src/main/java/model/UserCount.java @@ -4,9 +4,11 @@ public class UserCount { private static final int LOTTO_UNIT = 1000; - private final int count; - private final int price; + private int count; + private int price; + public UserCount(){ + } public UserCount(String price){ int priceNum = convertStringToInt(price); validate(priceNum); From 4f9977ce719a1bbaa51ef21a4cb317aae0c35f5e Mon Sep 17 00:00:00 2001 From: nyeroni Date: Tue, 7 May 2024 00:06:05 +0900 Subject: [PATCH 30/32] =?UTF-8?q?feat:=20=EC=83=9D=EC=84=B1=EC=9E=90=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/model/ManualCount.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/model/ManualCount.java b/src/main/java/model/ManualCount.java index e0eec989..c97da843 100644 --- a/src/main/java/model/ManualCount.java +++ b/src/main/java/model/ManualCount.java @@ -3,6 +3,9 @@ public class ManualCount { private int manualCount; + public ManualCount(){ + + } public ManualCount(String manualCount){ this.manualCount = convertStringToInt(manualCount) ; From 2f51b48dca7ff65581f0fcf93071391b3e9bab89 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Tue, 7 May 2024 00:20:07 +0900 Subject: [PATCH 31/32] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/model/BonusNumberTest.java | 21 ++++++ src/test/java/model/LottoTest.java | 92 ++++++++++++++++++++++++ src/test/java/model/ManualCountTest.java | 43 +++++++++++ src/test/java/model/UserCountTest.java | 81 +++++++++++++++++++++ 4 files changed, 237 insertions(+) create mode 100644 src/test/java/model/BonusNumberTest.java create mode 100644 src/test/java/model/LottoTest.java create mode 100644 src/test/java/model/ManualCountTest.java create mode 100644 src/test/java/model/UserCountTest.java diff --git a/src/test/java/model/BonusNumberTest.java b/src/test/java/model/BonusNumberTest.java new file mode 100644 index 00000000..a005c39a --- /dev/null +++ b/src/test/java/model/BonusNumberTest.java @@ -0,0 +1,21 @@ +package model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class BonusNumberTest { + @Test + void bonus_ValidInput() { + String bonusNumberStr = "10"; + BonusNumber bonusNumber = new BonusNumber(bonusNumberStr); + + assertEquals(10, bonusNumber.getBonusNumber()); + } + + @Test + void bonus_InvalidInput() { + String bonusNumberStr = "ABC"; + assertThrows(NumberFormatException.class, () -> new BonusNumber(bonusNumberStr)); + } +} \ No newline at end of file diff --git a/src/test/java/model/LottoTest.java b/src/test/java/model/LottoTest.java new file mode 100644 index 00000000..923b559c --- /dev/null +++ b/src/test/java/model/LottoTest.java @@ -0,0 +1,92 @@ +package model; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class LottoTest { + @Test + void convertToList_ValidInput() { + String lottoNumbersStr = "1, 2, 3, 4, 5, 6"; + Lotto lotto = new Lotto().convertToList(lottoNumbersStr); + + List expectedNumbers = Arrays.asList(1, 2, 3, 4, 5, 6); + assertEquals(expectedNumbers, lotto.getNumbers()); + } + + @Test + void convertToList_InvalidInput() { + String lottoNumbersStr = "1, 2, 3, 4, A, 6"; + assertThrows(NumberFormatException.class, () -> new Lotto().convertToList(lottoNumbersStr)); + } + + @Test + void validateSize_ValidSize() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertDoesNotThrow(() -> lotto.validateSize(numbers)); + } + + @Test + void validateSize_InvalidSize() { + List numbers = Arrays.asList(1, 2, 3, 4, 5); + Lotto lotto = new Lotto(numbers); + assertThrows(IllegalArgumentException.class, () -> lotto.validateSize(numbers)); + } + + @Test + void validateRange_ValidRange() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertDoesNotThrow(() -> lotto.validateRange(numbers)); + } + + @Test + void validateRange_InvalidRange() { + List numbers = Arrays.asList(1, 2, 3, 4, 50, 6); + Lotto lotto = new Lotto(numbers); + assertThrows(IllegalArgumentException.class, () -> lotto.validateRange(numbers)); + } + + @Test + void validateDuplicate_NoDuplicates() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertDoesNotThrow(() -> lotto.validateDuplicate(numbers)); + } + + @Test + void validateDuplicate_DuplicateNumbers() { + List numbers = Arrays.asList(1, 2, 3, 4, 4, 6); + Lotto lotto = new Lotto(numbers); + assertThrows(IllegalArgumentException.class, () -> lotto.validateDuplicate(numbers)); + } + + @Test + void calculateMatches_WithWinningLotto() { + List myNumbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto myLotto = new Lotto(myNumbers); + + List winningNumbers = Arrays.asList(1, 3, 5, 7, 9, 11); + Lotto winningLotto = new Lotto(winningNumbers); + + assertEquals(3, myLotto.calculateMatches(winningLotto)); + } + + @Test + void validateDuplicateWithBonus_ValidBonusNumber() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertDoesNotThrow(() -> lotto.validateDuplicateWithBonus(7)); + } + + @Test + void validateDuplicateWithBonus_InvalidBonusNumber() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertThrows(IllegalArgumentException.class, () -> lotto.validateDuplicateWithBonus(6)); + } +} \ No newline at end of file diff --git a/src/test/java/model/ManualCountTest.java b/src/test/java/model/ManualCountTest.java new file mode 100644 index 00000000..7a82b1b8 --- /dev/null +++ b/src/test/java/model/ManualCountTest.java @@ -0,0 +1,43 @@ +package model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ManualCountTest { + + @Test + void validManualCount() { + String manualCountStr = "5"; + ManualCount manualCount = new ManualCount(manualCountStr); + + assertEquals(5, manualCount.getManualCount()); + } + + @Test + void invalidInput() { + String manualCountStr = "abc"; + assertThrows(NumberFormatException.class, () -> new ManualCount(manualCountStr)); + } + + @Test + void getManualCount() { + String manualCountStr = "10"; + ManualCount manualCount = new ManualCount(manualCountStr); + + assertEquals(10, manualCount.getManualCount()); + } + + @Test + void convertStringToInt_ValidInput() { + ManualCount manualCount = new ManualCount(); + assertEquals(15, manualCount.convertStringToInt("15")); + } + + @Test + void convertStringToInt_InvalidInput() { + ManualCount manualCount = new ManualCount(); + assertThrows(NumberFormatException.class, () -> manualCount.convertStringToInt("abc")); + } + +} \ No newline at end of file diff --git a/src/test/java/model/UserCountTest.java b/src/test/java/model/UserCountTest.java new file mode 100644 index 00000000..4a50deec --- /dev/null +++ b/src/test/java/model/UserCountTest.java @@ -0,0 +1,81 @@ +package model; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class UserCountTest { + + @Test + void validPrice() { + String priceStr = "3000"; + UserCount userCount = new UserCount(priceStr); + + assertEquals(3000, userCount.getPrice()); + assertEquals(3, userCount.getCount()); + } + + @Test + void invalidZeroPrice() { + String priceStr = "0"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new UserCount(priceStr)); + assertEquals("[ERROR] 0원 이하는 구매가 불가능합니다", exception.getMessage()); + } + + @Test + void invalidNegativePrice() { + String priceStr = "-5000"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new UserCount(priceStr)); + assertEquals("[ERROR] 0원 이하는 구매가 불가능합니다", exception.getMessage()); + } + + @Test + void invalidNonMultipleOf1000() { + String priceStr = "2500"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new UserCount(priceStr)); + assertEquals("[ERROR] 구입 금액은 1000원 단위로 입력되어야 합니다.", exception.getMessage()); + } + + @Test + void calculateCount_ValidPrice() { + UserCount userCount = new UserCount("4000"); + assertEquals(4, userCount.getCount()); + } + + @Test + void convertStringToInt_ValidInput() { + UserCount userCount = new UserCount(); + assertEquals(5000, userCount.convertStringToInt("5000")); + } + + @Test + void convertStringToInt_InvalidInput() { + UserCount userCount = new UserCount(); + assertThrows(NumberFormatException.class, () -> userCount.convertStringToInt("abc")); + } + + @Test + void validateRange_ValidPrice() { + UserCount userCount = new UserCount(); + assertDoesNotThrow(() -> userCount.validateRange(2000)); + } + + @Test + void validateRange_InvalidZeroPrice() { + UserCount userCount = new UserCount(); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> userCount.validateRange(0)); + assertEquals("[ERROR] 0원 이하는 구매가 불가능합니다", exception.getMessage()); + } + + @Test + void validateUnit_ValidPrice() { + UserCount userCount = new UserCount(); + assertDoesNotThrow(() -> userCount.validateUnit(3000)); + } + + @Test + void validateUnit_InvalidNonMultipleOf1000() { + UserCount userCount = new UserCount(); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> userCount.validateUnit(2500)); + assertEquals("[ERROR] 구입 금액은 1000원 단위로 입력되어야 합니다.", exception.getMessage()); + } +} From 36e8947081cf2b156657d309c7dd8a4c940961a2 Mon Sep 17 00:00:00 2001 From: nyeroni Date: Tue, 7 May 2024 00:38:48 +0900 Subject: [PATCH 32/32] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 1 + src/main/java/controller/LottoController.java | 13 ++--- .../java/controller/LottoControllerTest.java | 47 +++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/test/java/controller/LottoControllerTest.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java index 2588f582..bf82be8e 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -6,5 +6,6 @@ public class Application { public static void main(String [] args) throws IOException { LottoController lottoController = new LottoController(); + lottoController.startLottoGame(); } } diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java index 81e9fdcc..b7b9ee73 100644 --- a/src/main/java/controller/LottoController.java +++ b/src/main/java/controller/LottoController.java @@ -10,14 +10,11 @@ import java.util.function.Predicate; public class LottoController { - private InputView inputView; - private OutputView outputView; + private InputView inputView = new InputView(); + private OutputView outputView = new OutputView(); private LottoList lottoList; public LottoController() { - this.inputView = new InputView(); - this.outputView = new OutputView(); - startLottoGame(); } public void startLottoGame() { UserCount userMoney = receiveMoneyInput(); @@ -77,7 +74,7 @@ private void displayLottoTickets(UserCount count, List lottos){ lottoList = generateLottoList(autoCount, lottos); outputView.printLottoNumbers(lottoList); } - private LottoList generateLottoList(int count, List lottos){ + LottoList generateLottoList(int count, List lottos){ lottoList = new LottoList(); if(lottos != null){ @@ -134,7 +131,7 @@ private void updateMatchCounts(List matchCounts, int countMatches, Bonu } } - private double calculateWinningRate(List matchCounts, UserCount count) { + double calculateWinningRate(List matchCounts, UserCount count) { int totalWinnings = matchCounts.get(0) * 5000 + matchCounts.get(1) * 50000 + matchCounts.get(2) * 1500000 + @@ -142,7 +139,7 @@ private double calculateWinningRate(List matchCounts, UserCount count) matchCounts.get(4) * 2000000000; double winningRate = (double) totalWinnings / (double) count.getPrice(); - return winningRate; + return Math.floor(winningRate * 100) / 100; } } diff --git a/src/test/java/controller/LottoControllerTest.java b/src/test/java/controller/LottoControllerTest.java new file mode 100644 index 00000000..d146526b --- /dev/null +++ b/src/test/java/controller/LottoControllerTest.java @@ -0,0 +1,47 @@ +package controller; + +import model.Lotto; +import model.LottoList; +import model.UserCount; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class LottoControllerTest { + @Test + void testCalculateWinningRate() { + // Given + LottoController controller = new LottoController(); + List matchCounts = Arrays.asList(1, 0, 0, 0, 0, 0); + String price = "14000"; + double priceNum = 14000.0; + UserCount userCount = new UserCount(price); + + // When + double winningRate = controller.calculateWinningRate(matchCounts, userCount); + + // Then + double expectedRate = (double) (1 * 5000 + 0 * 50000 + 0 * 150000 + 0 * 30000000 + 0 * 2000000000) / priceNum; + expectedRate = Math.floor(expectedRate * 100) / 100; + System.out.println("dd" + winningRate); + assertEquals(expectedRate, winningRate); + } + + @Test + void testGenerateLottoList() { + // Given + LottoController controller = new LottoController(); + int count = 5; + + // When + LottoList lottoList = controller.generateLottoList(count, null); + + // Then + List lottos = lottoList.getLottoList(); + assertEquals(count, lottos.size()); + + } +} \ No newline at end of file