From b1000d5d8a1845a8d1c5cd361ab40683b5d3d63c Mon Sep 17 00:00:00 2001 From: siru02 Date: Wed, 20 Mar 2024 23:55:43 +0900 Subject: [PATCH 01/10] puchase and make numbers --- src/main/java/lotto/Application.java | 23 +++++++++++++++++++++++ src/main/java/lotto/Lotto.java | 8 ++++++-- src/main/java/lotto/Main.java | 0 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/main/java/lotto/Main.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922..83dd8cd 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,30 @@ package lotto; +import java.util.Scanner; +import java.util.List; +import java.util.ArrayList; public class Application { public static void main(String[] args) { // TODO: 프로그램 구현 + Scanner sc = new Scanner(System.in); + int cash = 0; + while (true) { + System.out.println("구매금액을 입력해 주세요."); + cash = sc.nextInt(); //금액입력받음 + if (cash >= 1000) + break; + System.out.println("[ERROR] 1000이상의 금액을 입력해야 합니다."); + } + int cnt = cash / 1000; + System.out.println(cnt + "개를 구매하셨습니다"); + Lotto lottoArr[cnt]; + Random rd = new Random(); + for (int i = 0; i < cnt; i++) + { + List numbers = new ArrayList<>(); + numbers.add(fd.nextInt(44) + 1); + Lotto[i] = new Lotto(numbers); //i번째 로또객체 생성 + System.out.println("[" + Lotto[i].getNumbers() + "]"); //한줄씩 출력 // 굳이 객체호출하지말고 바로 쓰는편이 낫나 의문 + } } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d..e8b0b92 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -3,17 +3,21 @@ import java.util.List; public class Lotto { - private final List numbers; + private final List numbers; //로또 숫자들을 저장할 배열 public Lotto(List numbers) { validate(numbers); this.numbers = numbers; - } + } //constructor private void validate(List numbers) { if (numbers.size() != 6) { throw new IllegalArgumentException(); } + public List getNumbers() + { + return numbers; + } } // TODO: 추가 기능 구현 diff --git a/src/main/java/lotto/Main.java b/src/main/java/lotto/Main.java new file mode 100644 index 0000000..e69de29 From e3e4192cf42bbcd3ae6f37cdd43cd96ee1304ad7 Mon Sep 17 00:00:00 2001 From: siru02 Date: Thu, 21 Mar 2024 15:18:46 +0900 Subject: [PATCH 02/10] jdk22 to jdk17 fix and lotto number generate logic fix --- src/main/java/lotto/Application.java | 39 ++++++++++++++++++++++------ src/main/java/lotto/Lotto.java | 10 +++---- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 83dd8cd..593fadb 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -4,6 +4,18 @@ import java.util.ArrayList; public class Application { + + private static List generateLottoNumbers() { + List numbers = new ArrayList<>(); + // 여기에서 각 번호를 랜덤하게 생성하는 코드를 작성하세요. + // 이 예시에서는 간단하게 1부터 45까지의 랜덤한 번호를 생성합니다. + for (int i = 0; i < 6; i++) { + int randomNumber = (int) (Math.random() * 45) + 1; // 1부터 45까지의 랜덤한 숫자 생성 + numbers.add(randomNumber); // 생성된 숫자를 리스트에 추가 + } + return numbers; + } + public static void main(String[] args) { // TODO: 프로그램 구현 Scanner sc = new Scanner(System.in); @@ -17,14 +29,25 @@ public static void main(String[] args) { } int cnt = cash / 1000; System.out.println(cnt + "개를 구매하셨습니다"); - Lotto lottoArr[cnt]; - Random rd = new Random(); - for (int i = 0; i < cnt; i++) - { - List numbers = new ArrayList<>(); - numbers.add(fd.nextInt(44) + 1); - Lotto[i] = new Lotto(numbers); //i번째 로또객체 생성 - System.out.println("[" + Lotto[i].getNumbers() + "]"); //한줄씩 출력 // 굳이 객체호출하지말고 바로 쓰는편이 낫나 의문 + List lottoList = new ArrayList<>(); + for (int i = 0; i < 6; i++) { + List numbers = generateLottoNumbers(); // 6개의 랜덤한 번호 생성 + Lotto lotto = new Lotto(numbers); // 생성된 번호로 Lotto 객체 생성 + lottoList.add(lotto); // 리스트에 Lotto 객체 추가 + } + + // 생성된 Lotto 객체들을 사용하거나 출력 + for (int i = 0; i < lottoList.size(); i++) { + Lotto lotto = lottoList.get(i); + System.out.println("Lotto " + (i + 1) + " numbers: " + lotto.getNumbers()); } +// Random rd = new Random(); +// for (int i = 0; i < cnt; i++) +// { +// List numbers = new ArrayList<>(); +// numbers.add(rd.nextInt(44) + 1); +// Lotto[i] = new Lotto(numbers); //i번째 로또객체 생성 +// System.out.println("[" + Lotto[i].getNumbers() + "]"); //한줄씩 출력 // 굳이 객체호출하지말고 바로 쓰는편이 낫나 의문 +// } } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index e8b0b92..53da518 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,6 +1,6 @@ package lotto; - import java.util.List; +import java.util.ArrayList; public class Lotto { private final List numbers; //로또 숫자들을 저장할 배열 @@ -14,10 +14,10 @@ private void validate(List numbers) { if (numbers.size() != 6) { throw new IllegalArgumentException(); } - public List getNumbers() - { - return numbers; - } + } + public List getNumbers() + { + return numbers; } // TODO: 추가 기능 구현 From c28939cb10667714d5ad663f2fdf6311f69cc11b Mon Sep 17 00:00:00 2001 From: siru02 Date: Thu, 21 Mar 2024 16:02:51 +0900 Subject: [PATCH 03/10] fix generate_func by using Randoms --- src/main/java/lotto/Application.java | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 593fadb..9f1016e 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -2,17 +2,13 @@ import java.util.Scanner; import java.util.List; import java.util.ArrayList; +import org.kokodak.Randoms; public class Application { private static List generateLottoNumbers() { List numbers = new ArrayList<>(); - // 여기에서 각 번호를 랜덤하게 생성하는 코드를 작성하세요. - // 이 예시에서는 간단하게 1부터 45까지의 랜덤한 번호를 생성합니다. - for (int i = 0; i < 6; i++) { - int randomNumber = (int) (Math.random() * 45) + 1; // 1부터 45까지의 랜덤한 숫자 생성 - numbers.add(randomNumber); // 생성된 숫자를 리스트에 추가 - } + numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); return numbers; } @@ -41,13 +37,5 @@ public static void main(String[] args) { Lotto lotto = lottoList.get(i); System.out.println("Lotto " + (i + 1) + " numbers: " + lotto.getNumbers()); } -// Random rd = new Random(); -// for (int i = 0; i < cnt; i++) -// { -// List numbers = new ArrayList<>(); -// numbers.add(rd.nextInt(44) + 1); -// Lotto[i] = new Lotto(numbers); //i번째 로또객체 생성 -// System.out.println("[" + Lotto[i].getNumbers() + "]"); //한줄씩 출력 // 굳이 객체호출하지말고 바로 쓰는편이 낫나 의문 -// } } } From ea491ab425530d90d40acc3476fd86e803eeaa75 Mon Sep 17 00:00:00 2001 From: siru02 Date: Sun, 24 Mar 2024 18:24:33 +0900 Subject: [PATCH 04/10] disunite cashInput method from main --- src/main/java/lotto/Application.java | 68 ++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 9f1016e..d3c5230 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -2,7 +2,10 @@ import java.util.Scanner; import java.util.List; import java.util.ArrayList; + +import org.kokodak.Console; import org.kokodak.Randoms; +import org.mockito.internal.matchers.Null; public class Application { @@ -12,19 +15,26 @@ private static List generateLottoNumbers() { return numbers; } - public static void main(String[] args) { - // TODO: 프로그램 구현 - Scanner sc = new Scanner(System.in); - int cash = 0; + public static int cashInput() + { + int cash; while (true) { System.out.println("구매금액을 입력해 주세요."); - cash = sc.nextInt(); //금액입력받음 + String input = Console.readLine(); //수정중 +// cash = sc.nextInt(); //금액입력받음 //Console 메소드 사용으로 바꿔야한다 + cash = Integer.parseInt(input); if (cash >= 1000) - break; + return cash; System.out.println("[ERROR] 1000이상의 금액을 입력해야 합니다."); } - int cnt = cash / 1000; - System.out.println(cnt + "개를 구매하셨습니다"); + } + + public static void main(String[] args) { + // TODO: 프로그램 구현 + // Scanner sc = new Scanner(System.in); + int cash = cashInput(); + int numOfPurchase = cash / 1000; + System.out.println(numOfPurchase + "개를 구매하셨습니다"); List lottoList = new ArrayList<>(); for (int i = 0; i < 6; i++) { List numbers = generateLottoNumbers(); // 6개의 랜덤한 번호 생성 @@ -37,5 +47,47 @@ public static void main(String[] args) { Lotto lotto = lottoList.get(i); System.out.println("Lotto " + (i + 1) + " numbers: " + lotto.getNumbers()); } + System.out.println("당첨 번호를 입력해 주세요."); + String str = Console.readLine(); + String[] array = str.split(","); + if (array.length != 6) + { + System.out.println("error"); + //error처리 추가 + } + //숫자 자르기 + List winnigNum = new ArrayList<>(); + int i = 0; + //위닝넘버를 저장한다 + for (int j = 0; j < 6; j++) + { + int num = Integer.parseInt(array[i++]); + if (num > 45 || num < 1) { + System.out.println("error"); + //error동작 + } + winnigNum.add(num); //winning num 리스트에 값들을 추가한다 + } + int bonusNum; + while (true) { + System.out.println("보너스 번호를 입력해 주세요."); + bonusNum = Integer.parseInt(Console.readLine()); + if (bonusNum > 45 || bonusNum < 1) { + System.out.println("error"); + //error동작 + } + else { + break; + } + } + winnigNum.add(bonusNum); + //7개의 당첨번호로 로또마다 당첨금액을 계산한다 + for (int j = 0; j < numOfPurchase ; j++) + { + for (int k = 0; k < 6; k++) + { + + } + } } } From bf3e5c9d1c61054678092e5d9bf3b979abdd5b9f Mon Sep 17 00:00:00 2001 From: siru02 Date: Sun, 24 Mar 2024 18:32:43 +0900 Subject: [PATCH 05/10] disunite getLottoNums method from main --- src/main/java/lotto/Application.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d3c5230..464ed11 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -29,18 +29,25 @@ public static int cashInput() } } + private static List getLottoNums(int numOfPurchase) { + List lottoList = new ArrayList<>(); + for (int i = 0; i < numOfPurchase; i++) { + List numbers = generateLottoNumbers(); // 6개의 랜덤한 번호 생성 + Lotto lotto = new Lotto(numbers); // 생성된 번호로 Lotto 객체 생성 + lottoList.add(lotto); // 리스트에 Lotto 객체 추가 + } + + return lottoList; + } + public static void main(String[] args) { // TODO: 프로그램 구현 // Scanner sc = new Scanner(System.in); int cash = cashInput(); int numOfPurchase = cash / 1000; System.out.println(numOfPurchase + "개를 구매하셨습니다"); - List lottoList = new ArrayList<>(); - for (int i = 0; i < 6; i++) { - List numbers = generateLottoNumbers(); // 6개의 랜덤한 번호 생성 - Lotto lotto = new Lotto(numbers); // 생성된 번호로 Lotto 객체 생성 - lottoList.add(lotto); // 리스트에 Lotto 객체 추가 - } + // + List lottoList = getLottoNums(numOfPurchase); // 생성된 Lotto 객체들을 사용하거나 출력 for (int i = 0; i < lottoList.size(); i++) { From 638bc4de60f68c7326e3d9f555fd1d6bf027c4bf Mon Sep 17 00:00:00 2001 From: siru02 Date: Sun, 24 Mar 2024 19:45:00 +0900 Subject: [PATCH 06/10] disunite getWinningNum method from main --- src/main/java/lotto/Application.java | 73 +++++++++++++++++++--------- 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 464ed11..09ba069 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -40,41 +40,66 @@ private static List getLottoNums(int numOfPurchase) { return lottoList; } - public static void main(String[] args) { - // TODO: 프로그램 구현 - // Scanner sc = new Scanner(System.in); - int cash = cashInput(); - int numOfPurchase = cash / 1000; - System.out.println(numOfPurchase + "개를 구매하셨습니다"); - // - List lottoList = getLottoNums(numOfPurchase); - - // 생성된 Lotto 객체들을 사용하거나 출력 + public static void printLotto(List lottoList){ for (int i = 0; i < lottoList.size(); i++) { Lotto lotto = lottoList.get(i); System.out.println("Lotto " + (i + 1) + " numbers: " + lotto.getNumbers()); } + } + + private static String[] winningNumInput(){ System.out.println("당첨 번호를 입력해 주세요."); String str = Console.readLine(); String[] array = str.split(","); - if (array.length != 6) - { - System.out.println("error"); - //error처리 추가 + return array; + } + + private static boolean InputValidationCheck(String[] array){ + if (array.length != 6) { + System.out.println("[ERROR]Invalid number count"); + return false; } - //숫자 자르기 - List winnigNum = new ArrayList<>(); - int i = 0; - //위닝넘버를 저장한다 - for (int j = 0; j < 6; j++) - { - int num = Integer.parseInt(array[i++]); + for (String tmp: array) { + int num = Integer.parseInt(tmp); if (num > 45 || num < 1) { - System.out.println("error"); - //error동작 + System.out.println("[ERROR]Invalid Num Arrange"); + return false; } - winnigNum.add(num); //winning num 리스트에 값들을 추가한다 } + return true; + } + + public static List getWinningNum(){ + List winningNum = new ArrayList<>(); + String[] winningNumStr; + //올바른 값이 들어오도록 계속 입력받아야한다 + while (true) { + winningNumStr = winningNumInput(); + if (InputValidationCheck(winningNumStr) == true) { + break; + } + } + //올바르게 입력받은 당첨 번호들을 나눠야한다 + for (String tmp : winningNumStr) { + winningNum.add(Integer.parseInt((tmp))); + } + return winningNum; + } + + public static void main(String[] args) { + // TODO: 프로그램 구현 + int cash = cashInput(); //금액을 입력받음 + int numOfPurchase = cash / 1000; + System.out.println(numOfPurchase + "개를 구매하셨습니다"); + + //로또 번호를 생성한다 + List lottoList = getLottoNums(numOfPurchase); + + // 생성된 Lotto 객체들을 사용하거나 출력 + printLotto(lottoList); + + //당첨번호 입력받기 + List winnigNum = getWinningNum();//new ArrayList<>(); int bonusNum; while (true) { System.out.println("보너스 번호를 입력해 주세요."); From d10c77eed1ab71a94db82853dbbf2e7ea390a7f4 Mon Sep 17 00:00:00 2001 From: siru02 Date: Sun, 24 Mar 2024 20:46:58 +0900 Subject: [PATCH 07/10] print match counts(but not calculate the total amount --- src/main/java/lotto/Application.java | 82 +++++++++++++++++++--------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 09ba069..573bd55 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,5 @@ package lotto; -import java.util.Scanner; -import java.util.List; -import java.util.ArrayList; +import java.util.*; import org.kokodak.Console; import org.kokodak.Randoms; @@ -21,7 +19,6 @@ public static int cashInput() while (true) { System.out.println("구매금액을 입력해 주세요."); String input = Console.readLine(); //수정중 -// cash = sc.nextInt(); //금액입력받음 //Console 메소드 사용으로 바꿔야한다 cash = Integer.parseInt(input); if (cash >= 1000) return cash; @@ -60,7 +57,14 @@ private static boolean InputValidationCheck(String[] array){ return false; } for (String tmp: array) { - int num = Integer.parseInt(tmp); + int num; + try { + num = Integer.parseInt(tmp); + } catch (NumberFormatException e) { + System.out.println("[ERROR]Invalid Num Input"); + return false; + // 변환에 실패한 경우, 문자열에 숫자 이외의 문자가 포함되어 있다. + } if (num > 45 || num < 1) { System.out.println("[ERROR]Invalid Num Arrange"); return false; @@ -86,6 +90,48 @@ public static List getWinningNum(){ return winningNum; } + private static boolean checkNumInWinningNum(List winningNum, int number){ + for (int winNum : winningNum) { + if (number == winNum) + return true; + } + return false; + } + + + private static int matches(List winningNum, Lotto lottos){ + int match = 0; + + for (int number : lottos.getNumbers()){ + if (checkNumInWinningNum(winningNum, number) == true) + match++; + } + return match; //몇개가 일치하는지 제공 + } + + public static void printSpecificNumberCounts(List numbers) { + Map counts = new HashMap<>(); + for (int num : numbers) { + if (num >= 3 && num <= 6) { + counts.put(num, counts.getOrDefault(num, 0) + 1); + } + } + + for (int i = 3; i <= 6; i++) { + System.out.println(i + "개 일치" + counts.getOrDefault(i, 0) + "개"); + } + } + + public static void winningStatistics(List winningNum, List lottoList){ + List lottoResult = new ArrayList<>(); + + for (Lotto lottos : lottoList){ + lottoResult.add(matches(winningNum, lottos)); + } + + printSpecificNumberCounts(lottoResult); + } + public static void main(String[] args) { // TODO: 프로그램 구현 int cash = cashInput(); //금액을 입력받음 @@ -99,27 +145,13 @@ public static void main(String[] args) { printLotto(lottoList); //당첨번호 입력받기 - List winnigNum = getWinningNum();//new ArrayList<>(); + List winningNum = getWinningNum(); int bonusNum; - while (true) { - System.out.println("보너스 번호를 입력해 주세요."); - bonusNum = Integer.parseInt(Console.readLine()); - if (bonusNum > 45 || bonusNum < 1) { - System.out.println("error"); - //error동작 - } - else { - break; - } - } - winnigNum.add(bonusNum); - //7개의 당첨번호로 로또마다 당첨금액을 계산한다 - for (int j = 0; j < numOfPurchase ; j++) - { - for (int k = 0; k < 6; k++) - { + System.out.println("보너스 번호를 입력해 주세요."); + bonusNum = Integer.parseInt(Console.readLine()); + winningNum.add(bonusNum); - } - } + //7개의 당첨번호로 로또마다 당첨금액을 계산한다 + winningStatistics(winningNum, lottoList); } } From 588f01aefed89a00bac4a0eb43153f16f0919ae1 Mon Sep 17 00:00:00 2001 From: siru02 Date: Sun, 24 Mar 2024 21:28:06 +0900 Subject: [PATCH 08/10] add winningStatistics --- src/main/java/lotto/Application.java | 34 +++++++++++++++++++--------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 573bd55..b939aa0 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -99,37 +99,50 @@ private static boolean checkNumInWinningNum(List winningNum, int number } - private static int matches(List winningNum, Lotto lottos){ + private static int matches(List winningNum, Lotto lottos, int bonusNum){ int match = 0; + int flag = 0; for (int number : lottos.getNumbers()){ if (checkNumInWinningNum(winningNum, number) == true) match++; + if (number == bonusNum) + flag = 1; } + if (match == 6) + match = 7; + if (match == 5 && flag == 1) + match = 6; return match; //몇개가 일치하는지 제공 } - public static void printSpecificNumberCounts(List numbers) { + public static int printSpecificNumberCounts(List numbers, ListlottoList) { Map counts = new HashMap<>(); for (int num : numbers) { if (num >= 3 && num <= 6) { counts.put(num, counts.getOrDefault(num, 0) + 1); } } - - for (int i = 3; i <= 6; i++) { - System.out.println(i + "개 일치" + counts.getOrDefault(i, 0) + "개"); + int sum = 0; + String[] winnigAmount = {"5,000", "50,000", "1,500,000", "30,000,000", "2,000,000,000"}; + for (int i = 3; i <= 7; i++) { + System.out.println(i + "개 일치 (" + winnigAmount[i - 3] + "원) - " + counts.getOrDefault(i, 0) + "개"); + sum += counts.getOrDefault(i, 0) * Integer.parseInt(winnigAmount[i - 3].replace(",", "")); } + return sum; } - public static void winningStatistics(List winningNum, List lottoList){ + public static void winningStatistics(List winningNum, List lottoList, int bonusNum, int numOfPurchase){ List lottoResult = new ArrayList<>(); for (Lotto lottos : lottoList){ - lottoResult.add(matches(winningNum, lottos)); + lottoResult.add(matches(winningNum, lottos, bonusNum)); } - - printSpecificNumberCounts(lottoResult); + //당첨개수를 출력한다 + int sum = printSpecificNumberCounts(lottoResult, lottoList); + System.out.println("sum : " + sum); + double rateOfReturn = Math.round(((double)sum / (numOfPurchase * 1000)) * 10.0) / 10.0; + System.out.println("총 수익률은 " + rateOfReturn + "%입니다."); } public static void main(String[] args) { @@ -149,9 +162,8 @@ public static void main(String[] args) { int bonusNum; System.out.println("보너스 번호를 입력해 주세요."); bonusNum = Integer.parseInt(Console.readLine()); - winningNum.add(bonusNum); //7개의 당첨번호로 로또마다 당첨금액을 계산한다 - winningStatistics(winningNum, lottoList); + winningStatistics(winningNum, lottoList, bonusNum, numOfPurchase); } } From dace70e3e15e6d3b5187d4927c299d11b9ace7ed Mon Sep 17 00:00:00 2001 From: siru02 Date: Sun, 24 Mar 2024 21:41:52 +0900 Subject: [PATCH 09/10] test1_clear, test2_fail --- src/main/java/lotto/Application.java | 10 +++- src/test/java/lotto/ApplicationTest.java | 66 ++++++++++++------------ 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index b939aa0..0338339 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -51,6 +51,7 @@ private static String[] winningNumInput(){ return array; } + private static boolean InputValidationCheck(String[] array){ if (array.length != 6) { System.out.println("[ERROR]Invalid number count"); @@ -126,6 +127,11 @@ public static int printSpecificNumberCounts(List numbers, Listlo int sum = 0; String[] winnigAmount = {"5,000", "50,000", "1,500,000", "30,000,000", "2,000,000,000"}; for (int i = 3; i <= 7; i++) { + if (i == 6) + System.out.println(i - 1 + "개 일치, 보너스 볼 일치 (" + winnigAmount[i - 3] + "원) - " + counts.getOrDefault(i, 0) + "개"); + else if (i == 7) + System.out.println(i - 1 + "개 일치 (" + winnigAmount[i - 3] + "원) - " + counts.getOrDefault(i, 0) + "개"); + else System.out.println(i + "개 일치 (" + winnigAmount[i - 3] + "원) - " + counts.getOrDefault(i, 0) + "개"); sum += counts.getOrDefault(i, 0) * Integer.parseInt(winnigAmount[i - 3].replace(",", "")); } @@ -141,7 +147,7 @@ public static void winningStatistics(List winningNum, List lotto //당첨개수를 출력한다 int sum = printSpecificNumberCounts(lottoResult, lottoList); System.out.println("sum : " + sum); - double rateOfReturn = Math.round(((double)sum / (numOfPurchase * 1000)) * 10.0) / 10.0; + double rateOfReturn = Math.round(((double)sum / (numOfPurchase * 1000) * 100) * 10.0) / 10.0; System.out.println("총 수익률은 " + rateOfReturn + "%입니다."); } @@ -149,7 +155,7 @@ public static void main(String[] args) { // TODO: 프로그램 구현 int cash = cashInput(); //금액을 입력받음 int numOfPurchase = cash / 1000; - System.out.println(numOfPurchase + "개를 구매하셨습니다"); + System.out.println(numOfPurchase + "개를 구매했습니다."); //로또 번호를 생성한다 List lottoList = getLottoNums(numOfPurchase); diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index 404cbe1..ea936c2 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -11,39 +11,39 @@ class ApplicationTest extends NsTest { private static final String ERROR_MESSAGE = "[ERROR]"; - @Test - void 기능_테스트() { - assertRandomUniqueNumbersInRangeTest( - () -> { - run("8000", "1,2,3,4,5,6", "7"); - assertThat(output()).contains( - "8개를 구매했습니다.", - "[8, 21, 23, 41, 42, 43]", - "[3, 5, 11, 16, 32, 38]", - "[7, 11, 16, 35, 36, 44]", - "[1, 8, 11, 31, 41, 42]", - "[13, 14, 16, 38, 42, 45]", - "[7, 11, 30, 40, 42, 43]", - "[2, 13, 22, 32, 38, 45]", - "[1, 3, 5, 14, 22, 45]", - "3개 일치 (5,000원) - 1개", - "4개 일치 (50,000원) - 0개", - "5개 일치 (1,500,000원) - 0개", - "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", - "6개 일치 (2,000,000,000원) - 0개", - "총 수익률은 62.5%입니다." - ); - }, - List.of(8, 21, 23, 41, 42, 43), - List.of(3, 5, 11, 16, 32, 38), - List.of(7, 11, 16, 35, 36, 44), - List.of(1, 8, 11, 31, 41, 42), - List.of(13, 14, 16, 38, 42, 45), - List.of(7, 11, 30, 40, 42, 43), - List.of(2, 13, 22, 32, 38, 45), - List.of(1, 3, 5, 14, 22, 45) - ); - } +// @Test +// void 기능_테스트() { +// assertRandomUniqueNumbersInRangeTest( +// () -> { +// run("8000", "1,2,3,4,5,6", "7"); +// assertThat(output()).contains( +// "8개를 구매했습니다.", +// "[8, 21, 23, 41, 42, 43]", +// "[3, 5, 11, 16, 32, 38]", +// "[7, 11, 16, 35, 36, 44]", +// "[1, 8, 11, 31, 41, 42]", +// "[13, 14, 16, 38, 42, 45]", +// "[7, 11, 30, 40, 42, 43]", +// "[2, 13, 22, 32, 38, 45]", +// "[1, 3, 5, 14, 22, 45]", +// "3개 일치 (5,000원) - 1개", +// "4개 일치 (50,000원) - 0개", +// "5개 일치 (1,500,000원) - 0개", +// "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", +// "6개 일치 (2,000,000,000원) - 0개", +// "총 수익률은 62.5%입니다." +// ); +// }, +// List.of(8, 21, 23, 41, 42, 43), +// List.of(3, 5, 11, 16, 32, 38), +// List.of(7, 11, 16, 35, 36, 44), +// List.of(1, 8, 11, 31, 41, 42), +// List.of(13, 14, 16, 38, 42, 45), +// List.of(7, 11, 30, 40, 42, 43), +// List.of(2, 13, 22, 32, 38, 45), +// List.of(1, 3, 5, 14, 22, 45) +// ); +// } @Test void 예외_테스트() { From 29c604308d7e9b4ef679c685f9c2e84c4e4112b7 Mon Sep 17 00:00:00 2001 From: siru02 Date: Sun, 24 Mar 2024 21:56:53 +0900 Subject: [PATCH 10/10] =?UTF-8?q?=EB=88=84=EB=8D=94=EA=B8=B0=EA=B3=A8?= =?UTF-8?q?=EB=A0=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 37 ++++++++---- src/test/java/lotto/ApplicationTest.java | 74 +++++++++++++----------- 2 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index 0338339..6eaf049 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -19,6 +19,8 @@ public static int cashInput() while (true) { System.out.println("구매금액을 입력해 주세요."); String input = Console.readLine(); //수정중 + if (stringIsInt(input) == false) + continue; cash = Integer.parseInt(input); if (cash >= 1000) return cash; @@ -51,6 +53,17 @@ private static String[] winningNumInput(){ return array; } + public static boolean stringIsInt(String str){ + int num; + try { + num = Integer.parseInt(str); + } catch (NumberFormatException e) { + System.out.println("[ERROR]Invalid Num Input"); + return false; + // 변환에 실패한 경우, 문자열에 숫자 이외의 문자가 포함되어 있다. + } + return true; + } private static boolean InputValidationCheck(String[] array){ if (array.length != 6) { @@ -58,14 +71,9 @@ private static boolean InputValidationCheck(String[] array){ return false; } for (String tmp: array) { - int num; - try { - num = Integer.parseInt(tmp); - } catch (NumberFormatException e) { - System.out.println("[ERROR]Invalid Num Input"); + if (stringIsInt(tmp) == false) return false; - // 변환에 실패한 경우, 문자열에 숫자 이외의 문자가 포함되어 있다. - } + int num = Integer.parseInt(tmp); if (num > 45 || num < 1) { System.out.println("[ERROR]Invalid Num Arrange"); return false; @@ -151,6 +159,16 @@ public static void winningStatistics(List winningNum, List lotto System.out.println("총 수익률은 " + rateOfReturn + "%입니다."); } + private static int getBonusNum(){ + while (true) { + System.out.println("보너스 번호를 입력해 주세요."); + String str = Console.readLine(); + if (stringIsInt(str) == false) + continue; + return Integer.parseInt(str); + } + } + public static void main(String[] args) { // TODO: 프로그램 구현 int cash = cashInput(); //금액을 입력받음 @@ -165,11 +183,8 @@ public static void main(String[] args) { //당첨번호 입력받기 List winningNum = getWinningNum(); - int bonusNum; - System.out.println("보너스 번호를 입력해 주세요."); - bonusNum = Integer.parseInt(Console.readLine()); //7개의 당첨번호로 로또마다 당첨금액을 계산한다 - winningStatistics(winningNum, lottoList, bonusNum, numOfPurchase); + winningStatistics(winningNum, lottoList, getBonusNum(), numOfPurchase); } } diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index ea936c2..0db79bd 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -11,39 +11,47 @@ class ApplicationTest extends NsTest { private static final String ERROR_MESSAGE = "[ERROR]"; -// @Test -// void 기능_테스트() { -// assertRandomUniqueNumbersInRangeTest( -// () -> { -// run("8000", "1,2,3,4,5,6", "7"); -// assertThat(output()).contains( -// "8개를 구매했습니다.", -// "[8, 21, 23, 41, 42, 43]", -// "[3, 5, 11, 16, 32, 38]", -// "[7, 11, 16, 35, 36, 44]", -// "[1, 8, 11, 31, 41, 42]", -// "[13, 14, 16, 38, 42, 45]", -// "[7, 11, 30, 40, 42, 43]", -// "[2, 13, 22, 32, 38, 45]", -// "[1, 3, 5, 14, 22, 45]", -// "3개 일치 (5,000원) - 1개", -// "4개 일치 (50,000원) - 0개", -// "5개 일치 (1,500,000원) - 0개", -// "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", -// "6개 일치 (2,000,000,000원) - 0개", -// "총 수익률은 62.5%입니다." -// ); -// }, -// List.of(8, 21, 23, 41, 42, 43), -// List.of(3, 5, 11, 16, 32, 38), -// List.of(7, 11, 16, 35, 36, 44), -// List.of(1, 8, 11, 31, 41, 42), -// List.of(13, 14, 16, 38, 42, 45), -// List.of(7, 11, 30, 40, 42, 43), -// List.of(2, 13, 22, 32, 38, 45), -// List.of(1, 3, 5, 14, 22, 45) -// ); -// } + @Test + void input_count_error() { + assertSimpleTest(() -> { + runException("-8000"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + + @Test + void 기능_테스트() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("8000", "1,2,3,4,5,6", "7"); + assertThat(output()).contains( + "8개를 구매했습니다.", + "[8, 21, 23, 41, 42, 43]", + "[3, 5, 11, 16, 32, 38]", + "[7, 11, 16, 35, 36, 44]", + "[1, 8, 11, 31, 41, 42]", + "[13, 14, 16, 38, 42, 45]", + "[7, 11, 30, 40, 42, 43]", + "[2, 13, 22, 32, 38, 45]", + "[1, 3, 5, 14, 22, 45]", + "3개 일치 (5,000원) - 1개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 0개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", + "6개 일치 (2,000,000,000원) - 0개", + "총 수익률은 62.5%입니다." + ); + }, + List.of(8, 21, 23, 41, 42, 43), + List.of(3, 5, 11, 16, 32, 38), + List.of(7, 11, 16, 35, 36, 44), + List.of(1, 8, 11, 31, 41, 42), + List.of(13, 14, 16, 38, 42, 45), + List.of(7, 11, 30, 40, 42, 43), + List.of(2, 13, 22, 32, 38, 45), + List.of(1, 3, 5, 14, 22, 45) + ); + } @Test void 예외_테스트() {