From e66b9a2b45a6e2b9365b428a9bc38efeb31a649a Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Mon, 8 Apr 2019 19:25:10 +0900 Subject: [PATCH 01/14] Make a list to implement at README --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index e69de29b..fb86e6af 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,32 @@ +## 구현 기능 목록 + +* 사용자로부터 구입 금액을 입력받아야 한다. + * (예외상황) 입력된 값이 숫자인지 검증해야 한다. + * (예외상황) 입력된 값이 1000원 이상인지 검증해야 한다. + * (예외상황) 입력된 값이 10만원 이하인지 검증해야 한다. > 1인당 로또 최대 구매가능 금액 + +* 입력받은 금액만큼 로또를 구매해야 한다. + * 입력받은 금액을 로또금액(LOTTO_PRICE)으로 나누어 구매 개수를 구해야 한다. + * 구매 개수만큼 Lotto 인스턴스를 생성해야 한다. + * 1~45 숫자 중 중복없이 랜덤하게 6개의 숫자를 생성한다. + + * 구매하고 남은 돈인 거스름돈을 구해야 한다. + * 구매 개수와 거스름돈, 구매 결과를 사용자에게 알려준다. + +* 지난주 당첨번호를 입력받아야 한다. + * 입력한 번호를 쉼표 기준으로 분리해야 한다. + * (예외상황) 입력된 값이 숫자인지 검증해야 한다. + * (예외상황) 입력된 값이 1~45 사이의 숫자인지 검증해야 한다. + * (예외상황) 입력된 값의 수가 6개인지 검증해야 한다. + +* 지난주 보너스번호를 입력받아야 한다. + * (예외상황) 입력된 값이 숫자인지 검증해야 한다. + * (예외상황) 입력된 값이 1~45 사이의 숫자인지 검증해야 한다. + * WinningLotto 인스턴스를 생성해야 한다. + +* 로또 당첨 결과를 도출해야 한다. + * 구매한 각 로또가 당첨번호+보너스번호와 매치되는 숫자 갯수(countOfMatch)를 구해야 한다. + * 구매한 각 로또가 보너스번호와 매치되는지 여부(matchBonus)를 구해야 한다. + * MISS를 제외 각 등수별 로또 개수를 구해야 한다. + * 총 상금액을 구해야 한다. + * 총 수익률을 구해야 한다. From 469cdd53dd1cf38c068d7eb62cad26ffaf8ab243 Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Mon, 8 Apr 2019 19:57:38 +0900 Subject: [PATCH 02/14] Receive money to buy lotto --- src/main/java/domain/LottoGame.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/domain/LottoGame.java diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java new file mode 100644 index 00000000..ebae0625 --- /dev/null +++ b/src/main/java/domain/LottoGame.java @@ -0,0 +1,22 @@ +package domain; + +import java.util.Scanner; + +public class LottoGame { + public static int payMoney() { + boolean isProperMoney = false; + int inputMoney = 0; + + while (!isProperMoney) { + Scanner scanner = new Scanner(System.in); + System.out.println("구입금액을 입력해주세요. \n"); + inputMoney = scanner.nextInt(); + } + + return inputMoney; + } + + public static void main(String[] args) { + payMoney(); + } +} From 5f9321db29db41b5b496a0da4dcbf63478991649 Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Tue, 9 Apr 2019 01:23:53 +0900 Subject: [PATCH 03/14] Verifiy receiced money --- src/main/java/domain/LottoGame.java | 71 ++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index ebae0625..0a8cb8d9 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -3,20 +3,79 @@ import java.util.Scanner; public class LottoGame { + public static boolean isStringNumber(String inputMoney) { + try { + Integer.parseInt(inputMoney); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + public static void alertMoneyNotice(int inputMoney, int MIN, int MAX) { + if (MIN >= inputMoney) { + System.out.println("금액이 부족합니다."); + } + + if (inputMoney >= MAX) { + System.out.println("1인당 최대 10만원까지 구매 가능합니다."); + } + } + + public static boolean isProperMoney(int inputMoney) { + int MIN_LOTTO_PRICE = 1000; + int MAX_LOTTO_PRICE = 100000; + + alertMoneyNotice(inputMoney, MIN_LOTTO_PRICE, MAX_LOTTO_PRICE); + + if ((MIN_LOTTO_PRICE <= inputMoney) + && (inputMoney <= MAX_LOTTO_PRICE)) { + return true; + } + + return false; + } + public static int payMoney() { - boolean isProperMoney = false; - int inputMoney = 0; + Scanner scanner = new Scanner(System.in); + System.out.println("구입금액을 입력해주세요."); + String inputMoney = scanner.nextLine(); + boolean isProper = isStringNumber(inputMoney); + + if (!isProper) { + inputMoney = repayMoney(); + } + + return Integer.parseInt(inputMoney); + } - while (!isProperMoney) { + public static String repayMoney() { + boolean isProper = false; + String inputMoney = ""; + + while (!isProper) { Scanner scanner = new Scanner(System.in); - System.out.println("구입금액을 입력해주세요. \n"); - inputMoney = scanner.nextInt(); + System.out.println("구입금액을 제대로 입력해주세요."); + inputMoney = scanner.nextLine(); + isProper = isStringNumber(inputMoney); } return inputMoney; } + public static int receiveMoney() { + boolean isProper = false; + int paidMoney = 0; + + while (!isProper) { + paidMoney = payMoney(); + isProper = isProperMoney(paidMoney); + } + + return paidMoney; + } + public static void main(String[] args) { - payMoney(); + receiveMoney(); } } From 13611b7fa388de848d8a4d88049607c6f5a55836 Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Tue, 9 Apr 2019 17:06:07 +0900 Subject: [PATCH 04/14] Compute purchasable number, charge and purchase lotto --- src/main/java/domain/LottoGame.java | 36 ++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index 0a8cb8d9..435fe987 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -1,5 +1,8 @@ package domain; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Scanner; public class LottoGame { @@ -75,7 +78,38 @@ public static int receiveMoney() { return paidMoney; } + public static int getPurchasableNumber(int money) { + int ONE_LOTTO_PRICE = 1000; + int purchasableNumber = money / ONE_LOTTO_PRICE; + + return purchasableNumber; + } + + public static int getCharge(int money) { + int ONE_LOTTO_PRICE = 1000; + int charge = money % ONE_LOTTO_PRICE; + + return charge + } + + public static Lotto generateLotto() { + + } + + public static List purchaseLotto(int money) { + int purchasableLotto = getPurchasableNumber(money); + List boughtLotto = new ArrayList<>(); + + for (int i = 0; i < purchasableLotto; i++) { + Lotto lotto = generateLotto(); + boughtLotto.add(lotto); + } + + return boughtLotto; + } + public static void main(String[] args) { - receiveMoney(); + int paidMoney = receiveMoney(); + List purchasedLotto = purchaseLotto(paidMoney); } } From 0ad12b128ab038740a9f458a4d26954ceecd95d4 Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Tue, 9 Apr 2019 17:59:18 +0900 Subject: [PATCH 05/14] Generate lotto and announce purchase result --- src/main/java/domain/Lotto.java | 4 +++- src/main/java/domain/LottoGame.java | 32 +++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/domain/Lotto.java b/src/main/java/domain/Lotto.java index 06694869..930dff82 100644 --- a/src/main/java/domain/Lotto.java +++ b/src/main/java/domain/Lotto.java @@ -12,5 +12,7 @@ public Lotto(List numbers) { this.numbers = numbers; } - // 추가 기능 구현 + public void showNumbers() { + System.out.println(numbers); + } } diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index 435fe987..6441fe91 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -1,7 +1,7 @@ package domain; import java.util.ArrayList; -import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Scanner; @@ -16,11 +16,11 @@ public static boolean isStringNumber(String inputMoney) { } public static void alertMoneyNotice(int inputMoney, int MIN, int MAX) { - if (MIN >= inputMoney) { + if (MIN > inputMoney) { System.out.println("금액이 부족합니다."); } - if (inputMoney >= MAX) { + if (inputMoney > MAX) { System.out.println("1인당 최대 10만원까지 구매 가능합니다."); } } @@ -89,11 +89,20 @@ public static int getCharge(int money) { int ONE_LOTTO_PRICE = 1000; int charge = money % ONE_LOTTO_PRICE; - return charge + return charge; } public static Lotto generateLotto() { + HashSet numbers = new HashSet<>(); + while (numbers.size() != 6) { + numbers.add((int) (Math.random() * 45 + 1)); + } + + List lottoNumbers = new ArrayList<>(numbers); + Lotto lotto = new Lotto(lottoNumbers); + + return lotto; } public static List purchaseLotto(int money) { @@ -108,8 +117,23 @@ public static List purchaseLotto(int money) { return boughtLotto; } + public static void announcePurchaseResult(List lottos, int charge) { + int numberOfNumber = lottos.size(); + String message = String.format( + "\n거스름돈은 %d 원이고, %d개를 구매하였습니다.",charge, numberOfNumber); + + System.out.println(message); + + for (Lotto lotto : lottos) { + lotto.showNumbers(); + } + } + public static void main(String[] args) { int paidMoney = receiveMoney(); + int charge = getCharge(paidMoney); List purchasedLotto = purchaseLotto(paidMoney); + + announcePurchaseResult(purchasedLotto, charge); } } From b6260295ed297e44bdd4258f58025a702bc71e2b Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Tue, 9 Apr 2019 19:34:01 +0900 Subject: [PATCH 06/14] Receive winning lotto numbers and verify --- src/main/java/domain/LottoGame.java | 80 +++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index 6441fe91..cb2c22de 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -92,7 +92,7 @@ public static int getCharge(int money) { return charge; } - public static Lotto generateLotto() { + public static List generateLottoNumber() { HashSet numbers = new HashSet<>(); while (numbers.size() != 6) { @@ -100,9 +100,8 @@ public static Lotto generateLotto() { } List lottoNumbers = new ArrayList<>(numbers); - Lotto lotto = new Lotto(lottoNumbers); - return lotto; + return lottoNumbers; } public static List purchaseLotto(int money) { @@ -110,7 +109,7 @@ public static List purchaseLotto(int money) { List boughtLotto = new ArrayList<>(); for (int i = 0; i < purchasableLotto; i++) { - Lotto lotto = generateLotto(); + Lotto lotto = new Lotto(generateLottoNumber()); boughtLotto.add(lotto); } @@ -129,6 +128,79 @@ public static void announcePurchaseResult(List lottos, int charge) { } } + public static boolean isProperLength(String[] inputNumbers) { + int PROPER_LOTTO_LENGTH = 6; + + if (inputNumbers.length == PROPER_LOTTO_LENGTH) { + return true; + } + + return false; + } + + public static boolean isProperLottoNumber(String number) { + if (isStringNumber(number)) { + int numberInt = Integer.parseInt(number); + + return (1 <= numberInt) && (numberInt <= 45); + } + + return false; + } + + public static boolean isProperLottoNumbers(String[] inputNumbers) { + if (isProperLength(inputNumbers)) { + return false; + } + + for (String number : inputNumbers) { + if (!isProperLottoNumber(number)) { + return false; + } + } + + return true; + } + + public static String[] inputWinningNumbers(boolean isFirstTry) { + Scanner scanner = new Scanner(System.in); + + if (isFirstTry) { + System.out.println("지난 주 당첨 번호를 입력해 주세요."); + } else { + System.out.println("지난 주 당첨 번호를 제대로 입력해 주세요."); + } + + String inputNumbers = scanner.nextLine(); + String[] inputNumbersList = inputNumbers.split(","); + + return inputNumbersList; + } + + public static String[] receiveWinningNumbers() { + boolean isFirstTry = true; + String[] inputNumbers = inputWinningNumbers(isFirstTry); + boolean isProper = isProperLottoNumbers(inputNumbers); + + while (!isProper) { + isFirstTry = false; + inputNumbers = inputWinningNumbers(isFirstTry); + isProper = isProperLottoNumbers(inputNumbers); + } + + return inputNumbers; + } + + public static WinningLotto generateWinningLotto(String[] lottoNumbers) { + List numbers = new ArrayList<>(); + + for (String number : lottoNumbers) { + numbers.add(Integer.parseInt(number)); + } + + return + } + public static void main(String[] args) { int paidMoney = receiveMoney(); int charge = getCharge(paidMoney); From 523aaae6e0e556eca6a9d9a6a952b70d029680fa Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Tue, 9 Apr 2019 19:59:11 +0900 Subject: [PATCH 07/14] Generate winning lotto --- src/main/java/domain/LottoGame.java | 52 +++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index cb2c22de..d30a5d53 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -149,7 +149,7 @@ public static boolean isProperLottoNumber(String number) { } public static boolean isProperLottoNumbers(String[] inputNumbers) { - if (isProperLength(inputNumbers)) { + if (!isProperLength(inputNumbers)) { return false; } @@ -166,9 +166,9 @@ public static String[] inputWinningNumbers(boolean isFirstTry) { Scanner scanner = new Scanner(System.in); if (isFirstTry) { - System.out.println("지난 주 당첨 번호를 입력해 주세요."); + System.out.println("\n지난 주 당첨 번호를 입력해 주세요."); } else { - System.out.println("지난 주 당첨 번호를 제대로 입력해 주세요."); + System.out.println("\n지난 주 당첨 번호를 제대로 입력해 주세요."); } String inputNumbers = scanner.nextLine(); @@ -190,15 +190,53 @@ public static String[] receiveWinningNumbers() { return inputNumbers; } - - public static WinningLotto generateWinningLotto(String[] lottoNumbers) { + + public static String inputBonusNumber(boolean isFirstTry) { + Scanner scanner = new Scanner(System.in); + + if (isFirstTry) { + System.out.println("\n보너스 볼을 입력해 주세요."); + } else { + System.out.println("\n보너스 볼을 제대로 입력해 주세요."); + } + + String inputBonusNumber = scanner.nextLine(); + + return inputBonusNumber; + } + + public static int receiveBonusNumber() { + boolean isFirstTry = true; + String inputBonusNumber = inputBonusNumber(isFirstTry); + boolean isProper = isProperLottoNumber(inputBonusNumber); + + while (!isProper) { + isFirstTry = false; + inputBonusNumber = inputBonusNumber(isFirstTry); + isProper = isProperLottoNumber(inputBonusNumber); + } + + return Integer.parseInt(inputBonusNumber); + } + + public static List changeElementTypeStrToInt(String[] lottoNumbers) { List numbers = new ArrayList<>(); for (String number : lottoNumbers) { numbers.add(Integer.parseInt(number)); } - return + return numbers; + } + + public static WinningLotto generateWinningLotto() { + List numbers = changeElementTypeStrToInt(receiveWinningNumbers()); + int bonusNumber = receiveBonusNumber(); + + Lotto lotto = new Lotto(numbers); + WinningLotto winningLotto = new WinningLotto(lotto, bonusNumber); + + return winningLotto; } public static void main(String[] args) { @@ -207,5 +245,7 @@ public static void main(String[] args) { List purchasedLotto = purchaseLotto(paidMoney); announcePurchaseResult(purchasedLotto, charge); + generateWinningLotto(); + } } From 01309b8ad158e026ea2b8fb199fe0373dc7322cc Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Tue, 9 Apr 2019 21:13:04 +0900 Subject: [PATCH 08/14] Edit README for refactoring --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index fb86e6af..fa888c1d 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,13 @@ * (예외상황) 입력된 값이 1~45 사이의 숫자인지 검증해야 한다. * WinningLotto 인스턴스를 생성해야 한다. +--- +### 여기까지 중복기능 정리 (리팩토링) +1. 입력받은 String이 숫자인지 검증하는 메소드 +2. 숫자인지 검증 이후, 해당 값이 특정 범위 내에 있는지 검증하는 메소드 +3. 사용자로부터 값을 올바른 값이 입력될 때까지 입력받는 메소드 +--- + * 로또 당첨 결과를 도출해야 한다. * 구매한 각 로또가 당첨번호+보너스번호와 매치되는 숫자 갯수(countOfMatch)를 구해야 한다. * 구매한 각 로또가 보너스번호와 매치되는지 여부(matchBonus)를 구해야 한다. From 811ad17baa83693de2a2dea89457be397182ca7d Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Tue, 9 Apr 2019 23:41:01 +0900 Subject: [PATCH 09/14] Refactoring and verify some exceptions in generate winner lotto --- README.md | 2 + src/main/java/domain/LottoGame.java | 152 ++++++++++++++-------------- 2 files changed, 78 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index fa888c1d..b0ab8283 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,12 @@ * (예외상황) 입력된 값이 숫자인지 검증해야 한다. * (예외상황) 입력된 값이 1~45 사이의 숫자인지 검증해야 한다. * (예외상황) 입력된 값의 수가 6개인지 검증해야 한다. + * (예외상황) 입력된 값에 중복이 없는지 검증해야 한다. * 지난주 보너스번호를 입력받아야 한다. * (예외상황) 입력된 값이 숫자인지 검증해야 한다. * (예외상황) 입력된 값이 1~45 사이의 숫자인지 검증해야 한다. + * (예외상황) 입력된 값이 당첨번호와 겹치지 않는지 검증해야 한다. * WinningLotto 인스턴스를 생성해야 한다. --- diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index d30a5d53..10e417d2 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -1,81 +1,74 @@ package domain; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Scanner; +import java.util.*; public class LottoGame { - public static boolean isStringNumber(String inputMoney) { + public static boolean isStringNumber(String number) { try { - Integer.parseInt(inputMoney); + Integer.parseInt(number); return true; } catch (NumberFormatException e) { return false; } } - public static void alertMoneyNotice(int inputMoney, int MIN, int MAX) { - if (MIN > inputMoney) { + public static boolean isRangeIn(int number, int min, int max) { + if ((min <= number) && (number <= max)) { + return true; + } + + return false; + } + + public static void alertMoneyNotice(int inputMoney, int min, int max) { + if (min > inputMoney) { System.out.println("금액이 부족합니다."); } - if (inputMoney > MAX) { + if (inputMoney > max) { System.out.println("1인당 최대 10만원까지 구매 가능합니다."); } } - public static boolean isProperMoney(int inputMoney) { - int MIN_LOTTO_PRICE = 1000; - int MAX_LOTTO_PRICE = 100000; + public static boolean isProperMoney(String inputMoney) { + if (isStringNumber(inputMoney)) { + int MIN_LOTTO_PRICE = 1000; + int MAX_LOTTO_PRICE = 100000; + int money = Integer.parseInt(inputMoney); - alertMoneyNotice(inputMoney, MIN_LOTTO_PRICE, MAX_LOTTO_PRICE); + alertMoneyNotice(money, MIN_LOTTO_PRICE, MAX_LOTTO_PRICE); - if ((MIN_LOTTO_PRICE <= inputMoney) - && (inputMoney <= MAX_LOTTO_PRICE)) { - return true; + return isRangeIn(money, MIN_LOTTO_PRICE, MAX_LOTTO_PRICE); } return false; } - public static int payMoney() { + public static String inputMoney(boolean isFirstTry) { Scanner scanner = new Scanner(System.in); - System.out.println("구입금액을 입력해주세요."); - String inputMoney = scanner.nextLine(); - boolean isProper = isStringNumber(inputMoney); - - if (!isProper) { - inputMoney = repayMoney(); - } - - return Integer.parseInt(inputMoney); - } - - public static String repayMoney() { - boolean isProper = false; - String inputMoney = ""; - while (!isProper) { - Scanner scanner = new Scanner(System.in); - System.out.println("구입금액을 제대로 입력해주세요."); - inputMoney = scanner.nextLine(); - isProper = isStringNumber(inputMoney); + if (isFirstTry) { + System.out.println("구입 금액을 입력해 주세요."); + } else { + System.out.println("구입 금액을 제대로 입력해 주세요."); } - return inputMoney; + String answer = scanner.nextLine(); + return answer; } - public static int receiveMoney() { - boolean isProper = false; - int paidMoney = 0; + public static int payMoney() { + boolean isFirstTry = true; + String inputMoney = inputMoney(isFirstTry); + boolean isProper = isProperMoney(inputMoney); while (!isProper) { - paidMoney = payMoney(); - isProper = isProperMoney(paidMoney); + isFirstTry = false; + inputMoney = inputMoney(isFirstTry); + isProper = isProperMoney(inputMoney); } - return paidMoney; + return Integer.parseInt(inputMoney); } public static int getPurchasableNumber(int money) { @@ -100,22 +93,9 @@ public static List generateLottoNumber() { } List lottoNumbers = new ArrayList<>(numbers); - return lottoNumbers; } - public static List purchaseLotto(int money) { - int purchasableLotto = getPurchasableNumber(money); - List boughtLotto = new ArrayList<>(); - - for (int i = 0; i < purchasableLotto; i++) { - Lotto lotto = new Lotto(generateLottoNumber()); - boughtLotto.add(lotto); - } - - return boughtLotto; - } - public static void announcePurchaseResult(List lottos, int charge) { int numberOfNumber = lottos.size(); String message = String.format( @@ -128,21 +108,38 @@ public static void announcePurchaseResult(List lottos, int charge) { } } + public static List purchaseLotto(int money) { + int purchasableLotto = getPurchasableNumber(money); + int charge = getCharge(money); + List boughtLotto = new ArrayList<>(); + + for (int i = 0; i < purchasableLotto; i++) { + Lotto lotto = new Lotto(generateLottoNumber()); + boughtLotto.add(lotto); + } + + announcePurchaseResult(boughtLotto, charge); + return boughtLotto; + } + public static boolean isProperLength(String[] inputNumbers) { int PROPER_LOTTO_LENGTH = 6; + Set removeDuplicate = new HashSet<>(Arrays.asList(inputNumbers)); - if (inputNumbers.length == PROPER_LOTTO_LENGTH) { + if (removeDuplicate.size() == PROPER_LOTTO_LENGTH) { return true; } return false; } - public static boolean isProperLottoNumber(String number) { - if (isStringNumber(number)) { - int numberInt = Integer.parseInt(number); + public static boolean isProperLottoNumber(String inputnumber) { + if (isStringNumber(inputnumber)) { + int MIN_LOTTO_NUMBER = 1; + int MAX_LOTTO_NUMBER = 45; + int number = Integer.parseInt(inputnumber); - return (1 <= numberInt) && (numberInt <= 45); + return isRangeIn(number, MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER); } return false; @@ -172,9 +169,7 @@ public static String[] inputWinningNumbers(boolean isFirstTry) { } String inputNumbers = scanner.nextLine(); - String[] inputNumbersList = inputNumbers.split(","); - - return inputNumbersList; + return inputNumbers.split(","); } public static String[] receiveWinningNumbers() { @@ -191,6 +186,15 @@ public static String[] receiveWinningNumbers() { return inputNumbers; } + public static boolean isProperBonusNumber(String inputBonusNumber, + String[] inputLottoNumber) { + if (isProperLottoNumber(inputBonusNumber)) { + return !Arrays.asList(inputLottoNumber).contains(inputBonusNumber); + } + + return false; + } + public static String inputBonusNumber(boolean isFirstTry) { Scanner scanner = new Scanner(System.in); @@ -201,19 +205,18 @@ public static String inputBonusNumber(boolean isFirstTry) { } String inputBonusNumber = scanner.nextLine(); - return inputBonusNumber; } - public static int receiveBonusNumber() { + public static int receiveBonusNumber(String[] lottoNumbers) { boolean isFirstTry = true; String inputBonusNumber = inputBonusNumber(isFirstTry); - boolean isProper = isProperLottoNumber(inputBonusNumber); + boolean isProper = isProperBonusNumber(inputBonusNumber, lottoNumbers); while (!isProper) { isFirstTry = false; inputBonusNumber = inputBonusNumber(isFirstTry); - isProper = isProperLottoNumber(inputBonusNumber); + isProper = isProperBonusNumber(inputBonusNumber, lottoNumbers); } return Integer.parseInt(inputBonusNumber); @@ -230,8 +233,9 @@ public static List changeElementTypeStrToInt(String[] lottoNumbers) { } public static WinningLotto generateWinningLotto() { - List numbers = changeElementTypeStrToInt(receiveWinningNumbers()); - int bonusNumber = receiveBonusNumber(); + String[] inputNumbers = receiveWinningNumbers(); + List numbers = changeElementTypeStrToInt(inputNumbers); + int bonusNumber = receiveBonusNumber(inputNumbers); Lotto lotto = new Lotto(numbers); WinningLotto winningLotto = new WinningLotto(lotto, bonusNumber); @@ -240,12 +244,8 @@ public static WinningLotto generateWinningLotto() { } public static void main(String[] args) { - int paidMoney = receiveMoney(); - int charge = getCharge(paidMoney); + int paidMoney = payMoney(); List purchasedLotto = purchaseLotto(paidMoney); - - announcePurchaseResult(purchasedLotto, charge); - generateWinningLotto(); - + WinningLotto winningLotto = generateWinningLotto(); } } From 6ec4a3f80266d3bf3bd67dad2f644765b477ae3e Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Wed, 10 Apr 2019 22:59:02 +0900 Subject: [PATCH 10/14] Add method for get rank to each user lotto --- src/main/java/domain/Lotto.java | 22 ++++++++++++++++++++-- src/main/java/domain/LottoGame.java | 2 +- src/main/java/domain/WinningLotto.java | 7 +++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/domain/Lotto.java b/src/main/java/domain/Lotto.java index 930dff82..287d46e1 100644 --- a/src/main/java/domain/Lotto.java +++ b/src/main/java/domain/Lotto.java @@ -12,7 +12,25 @@ public Lotto(List numbers) { this.numbers = numbers; } - public void showNumbers() { - System.out.println(numbers); + public List getNumbers() { + return numbers; + } + + public int isMatch(int number) { + if (numbers.contains(number)) { + return 1; + } + + return 0; + } + + public int howManyMatch(Lotto lotto) { + int countOfMatch = 0; + + for (int number : numbers) { + countOfMatch += lotto.isMatch(number); + } + + return countOfMatch; } } diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index 10e417d2..33ed3dd8 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -104,7 +104,7 @@ public static void announcePurchaseResult(List lottos, int charge) { System.out.println(message); for (Lotto lotto : lottos) { - lotto.showNumbers(); + System.out.println(lotto.getNumbers()); } } diff --git a/src/main/java/domain/WinningLotto.java b/src/main/java/domain/WinningLotto.java index d810418b..4fc10c8f 100644 --- a/src/main/java/domain/WinningLotto.java +++ b/src/main/java/domain/WinningLotto.java @@ -13,7 +13,10 @@ public WinningLotto(Lotto lotto, int bonusNo) { } public Rank match(Lotto userLotto) { - // TODO 로직 구현 - return null; + int countOfMatch = lotto.howManyMatch(userLotto); + boolean matchBonus = userLotto.getNumbers().contains(bonusNo); + Rank rank = Rank.valueOf(countOfMatch, matchBonus); + + return rank; } } From 6ac22949120d9afbd2bd3bc6ec800bf797332929 Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Thu, 11 Apr 2019 01:15:47 +0900 Subject: [PATCH 11/14] Get lotto result and calculate winning money --- src/main/java/domain/LottoGame.java | 49 +++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index 33ed3dd8..e8be9824 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -243,9 +243,58 @@ public static WinningLotto generateWinningLotto() { return winningLotto; } + public static List getLottoResult(List userLottos, + WinningLotto winningLotto) { + List lottoResult = Arrays.asList(0,0,0,0,0,0); + + for (Lotto lotto : userLottos) { + Rank rank = winningLotto.match(lotto); + lottoResult.set(rank.ordinal(), lottoResult.get(rank.ordinal()) + 1); + } + + return lottoResult; + } + + public static void announceRankResult(Rank rank, int countOfMatch, + int winningMoney, int countOfWin) { + if (rank == Rank.SECOND) { + System.out.println(String.format("%d개 일치, 보너스 볼 일치(%d원) - %d개", + countOfMatch, winningMoney, countOfWin)); + } else if (rank != Rank.MISS) { + System.out.println(String.format("%d개 일치(%d원) - %d개", + countOfMatch, winningMoney, countOfWin)); + } + } + + public static int getRankResult(List result, Rank rank) { + int countOfMatch = rank.getCountOfMatch(); + int winningMoney = rank.getWinningMoney(); + int countOfWin = result.get(rank.ordinal()); + int totalWinningMoney = winningMoney * countOfWin; + + announceRankResult(rank, countOfMatch, winningMoney, countOfWin); + + return totalWinningMoney; + } + + public static int getWinningMoney(List userLottos, + WinningLotto winningLotto) { + int totalWinningMoney = 0; + List result = getLottoResult(userLottos,winningLotto); + + System.out.println("\n당첨통계\n--------"); + + for (Rank rank : Rank.values()) { + totalWinningMoney += getRankResult(result, rank); + } + + return totalWinningMoney; + } + public static void main(String[] args) { int paidMoney = payMoney(); List purchasedLotto = purchaseLotto(paidMoney); WinningLotto winningLotto = generateWinningLotto(); + System.out.println(getWinningMoney(purchasedLotto, winningLotto)); } } From bf5be0225694ad4dc06d7aba0d889c2277659ed0 Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Thu, 11 Apr 2019 01:26:17 +0900 Subject: [PATCH 12/14] Calculate and announce earning rate --- src/main/java/domain/LottoGame.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index e8be9824..a58b2480 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -291,10 +291,18 @@ public static int getWinningMoney(List userLottos, return totalWinningMoney; } + public static void announceEarningRate(int paidMoney, int winningMoney) { + double earningRate = winningMoney / paidMoney; + + System.out.println(String.format("총 수익률은 %.3f 입니다.", earningRate)); + } + public static void main(String[] args) { int paidMoney = payMoney(); List purchasedLotto = purchaseLotto(paidMoney); WinningLotto winningLotto = generateWinningLotto(); - System.out.println(getWinningMoney(purchasedLotto, winningLotto)); + int winningMoney = getWinningMoney(purchasedLotto, winningLotto); + + announceEarningRate(paidMoney, winningMoney); } } From 8bd145e3f070335b080863083d2b8e02092c5a30 Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Thu, 11 Apr 2019 17:23:20 +0900 Subject: [PATCH 13/14] Fix rank print order --- src/main/java/domain/LottoGame.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index a58b2480..8fad9d2d 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -277,6 +277,16 @@ public static int getRankResult(List result, Rank rank) { return totalWinningMoney; } + public static List reverseEnum(Rank[] initEnum) { + List reverseEnum = new ArrayList<>(); + + for (int i = initEnum.length - 1; i >= 0; i--) { + reverseEnum.add(initEnum[i]); + } + + return reverseEnum; + } + public static int getWinningMoney(List userLottos, WinningLotto winningLotto) { int totalWinningMoney = 0; @@ -284,7 +294,7 @@ public static int getWinningMoney(List userLottos, System.out.println("\n당첨통계\n--------"); - for (Rank rank : Rank.values()) { + for (Rank rank : reverseEnum(Rank.values())) { totalWinningMoney += getRankResult(result, rank); } From 0c417f9f9f912ec144566cd31d56cd5bad75ed30 Mon Sep 17 00:00:00 2001 From: stopsilver13 Date: Thu, 11 Apr 2019 17:25:24 +0900 Subject: [PATCH 14/14] Add remark --- src/main/java/domain/LottoGame.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/domain/LottoGame.java b/src/main/java/domain/LottoGame.java index 8fad9d2d..4212b0b5 100644 --- a/src/main/java/domain/LottoGame.java +++ b/src/main/java/domain/LottoGame.java @@ -1,3 +1,14 @@ + +/* + * LottoGame + * + * ver 1.0 + * + * 2019/04/11 + * + * Copyright 2019. Jieun Jeong. All ringts reserved. + */ + package domain; import java.util.*;