From daa50f6ed088503696e707ee7d793121aa8ccfaa Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 19:58:26 +0900 Subject: [PATCH 01/51] =?UTF-8?q?feat:=20Manager=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/lotto/service/Manager.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/java/lotto/service/Manager.java diff --git a/src/main/java/lotto/service/Manager.java b/src/main/java/lotto/service/Manager.java new file mode 100644 index 0000000000..04ebba3fa0 --- /dev/null +++ b/src/main/java/lotto/service/Manager.java @@ -0,0 +1,6 @@ +package lotto.service; + +public interface Manager { + + void run(); +} From b2cc359a8cc65f0fbb4015855b6750d527c243c8 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 19:58:46 +0900 Subject: [PATCH 02/51] =?UTF-8?q?feat:=20Lotto=20Manager=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 7 +++- src/main/java/lotto/service/LottoManager.java | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/main/java/lotto/service/LottoManager.java diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..b8dad14e18 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,12 @@ package lotto; +import lotto.service.LottoManager; +import lotto.service.Manager; + public class Application { + public static void main(String[] args) { - // TODO: 프로그램 구현 + Manager manager = new LottoManager(); + manager.run(); } } diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java new file mode 100644 index 0000000000..25589e20ec --- /dev/null +++ b/src/main/java/lotto/service/LottoManager.java @@ -0,0 +1,38 @@ +package lotto.service; + +import lotto.domain.BonusNumber; +import lotto.domain.Lotto; +import lotto.domain.Lottos; +import lotto.domain.PurchasingMoney; +import lotto.domain.generator.IssuedLottoGenerator; +import lotto.domain.generator.WinningLottoGenerator; +import lotto.domain.result.ProfitRate; +import lotto.domain.result.WinningDetail; +import lotto.util.Input; +import lotto.util.Output; + +public class LottoManager implements Manager { + + @Override + public void run() { + PurchasingMoney purchasingMoney = PurchasingMoney.from(Input.inputPurchasingMoney()); + Lottos issuedLotto = purchaseLotto(purchasingMoney); + + Lotto winningLotto = Lotto.from(WinningLottoGenerator.from(Input.inputWinningLotto())); + BonusNumber bonusNumber = BonusNumber.from(winningLotto, Input.inputBonusNumber()); + + compareLotto(purchasingMoney, issuedLotto, winningLotto, bonusNumber); + } + + private Lottos purchaseLotto(PurchasingMoney purchasingMoney) { + Lottos issuedLotto = Lottos.from(IssuedLottoGenerator.create(), purchasingMoney); + Output.printIssuedLotto(issuedLotto); + return issuedLotto; + } + + private void compareLotto(PurchasingMoney purchasingMoney, Lottos issuedLotto, Lotto winningLotto, BonusNumber bonusNumber) { + WinningDetail winningDetail = WinningDetail.from(winningLotto.compare(issuedLotto, bonusNumber)); + ProfitRate profitRate = ProfitRate.from(winningDetail.getTotalPrize(), purchasingMoney.getMoney()); + Output.printWinningDetail(winningDetail.getResult(), profitRate.getValue()); + } +} From 693dae49b339269f4c2a4d94e150c13577e1917c Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 19:59:27 +0900 Subject: [PATCH 03/51] =?UTF-8?q?feat:=20PurchasingMoney=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/domain/PurchasingMoney.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/lotto/domain/PurchasingMoney.java diff --git a/src/main/java/lotto/domain/PurchasingMoney.java b/src/main/java/lotto/domain/PurchasingMoney.java new file mode 100644 index 0000000000..147bc69927 --- /dev/null +++ b/src/main/java/lotto/domain/PurchasingMoney.java @@ -0,0 +1,35 @@ +package lotto.domain; + +import lotto.util.Convertor; + +public class PurchasingMoney { + + private static final int UNIT = 1000; + + private final int money; + private final int quantity; + + private PurchasingMoney(int money) { + validateUnit(money); + this.money = money; + this.quantity = money / UNIT; + } + + public static PurchasingMoney from(String input) { + return new PurchasingMoney(Convertor.toInteger(input)); + } + + public int getMoney() { + return money; + } + + public int getQuantity() { + return quantity; + } + + private void validateUnit(int money) { + if (money % UNIT != 0) { + throw new IllegalArgumentException(String.format("[ERROR] 구입금액은 %d원 단위여야 합니다.", UNIT)); + } + } +} From a06762c9fcc7824521f6c741b5162ab7b67e52f1 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 19:59:57 +0900 Subject: [PATCH 04/51] =?UTF-8?q?feat:=20Input=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/lotto/util/Input.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/main/java/lotto/util/Input.java diff --git a/src/main/java/lotto/util/Input.java b/src/main/java/lotto/util/Input.java new file mode 100644 index 0000000000..ad6ed6906b --- /dev/null +++ b/src/main/java/lotto/util/Input.java @@ -0,0 +1,21 @@ +package lotto.util; + +import camp.nextstep.edu.missionutils.Console; + +public class Input { + + private static final String BLANK = " "; + private static final String DELETE = ""; + + private Input() { + } + + public static String inputPurchasingMoney() { + System.out.println("구입금액을 입력해 주세요."); + return removeBlank(Console.readLine()); + } + + private static String removeBlank(String input) { + return input.replaceAll(BLANK, DELETE); + } +} From c0edc8670bb4d90a40eb0c06856ef550783abed0 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:01:02 +0900 Subject: [PATCH 05/51] =?UTF-8?q?feat:=20LottoGenerator=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/generator/LottoGenerator.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/java/lotto/domain/generator/LottoGenerator.java diff --git a/src/main/java/lotto/domain/generator/LottoGenerator.java b/src/main/java/lotto/domain/generator/LottoGenerator.java new file mode 100644 index 0000000000..51d4651188 --- /dev/null +++ b/src/main/java/lotto/domain/generator/LottoGenerator.java @@ -0,0 +1,8 @@ +package lotto.domain.generator; + +import java.util.List; + +public interface LottoGenerator { + + List issue(int minValue, int maxValue, int quantity); +} From 3530ae69492e931df4b91eb382bf97a713f0f6dd Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:01:16 +0900 Subject: [PATCH 06/51] =?UTF-8?q?feat:=20IssuedLottoGenerator=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/IssuedLottoGenerator.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/lotto/domain/generator/IssuedLottoGenerator.java diff --git a/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java b/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java new file mode 100644 index 0000000000..d231842840 --- /dev/null +++ b/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java @@ -0,0 +1,24 @@ +package lotto.domain.generator; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.List; +import java.util.stream.Collectors; + +public class IssuedLottoGenerator implements LottoGenerator { + + private IssuedLottoGenerator() { + } + + public static IssuedLottoGenerator create() { + return new IssuedLottoGenerator(); + } + + @Override + public List issue(int minValue, int maxValue, int quantity) { + return Randoms.pickUniqueNumbersInRange(minValue, maxValue, quantity) + .stream() + .sorted() + .collect(Collectors.toList()); + } +} From 5cda10e624c5e16963198960f8ed70b67302e3fc Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:01:38 +0900 Subject: [PATCH 07/51] =?UTF-8?q?feat:=20Output=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/lotto/util/Output.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/lotto/util/Output.java diff --git a/src/main/java/lotto/util/Output.java b/src/main/java/lotto/util/Output.java new file mode 100644 index 0000000000..7443ef8c82 --- /dev/null +++ b/src/main/java/lotto/util/Output.java @@ -0,0 +1,19 @@ +package lotto.util; + +import lotto.domain.Lotto; +import lotto.domain.Lottos; + +public class Output { + + private Output() { + } + + public static void printIssuedLotto(Lottos issuedLotto) { + System.out.printf("\n%d개를 구매했습니다.%n", issuedLotto.getQuantity()); + issuedLotto.getLottos() + .stream() + .map(Lotto::getNumbers) + .map(String::valueOf) + .forEach(System.out::println); + } +} From fd8c722596a95bddd27bbe349ed2a7dc3f5c3c20 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:02:15 +0900 Subject: [PATCH 08/51] =?UTF-8?q?feat:=20Lottos=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/lotto/domain/Lottos.java | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/main/java/lotto/domain/Lottos.java diff --git a/src/main/java/lotto/domain/Lottos.java b/src/main/java/lotto/domain/Lottos.java new file mode 100644 index 0000000000..6290c5955b --- /dev/null +++ b/src/main/java/lotto/domain/Lottos.java @@ -0,0 +1,40 @@ +package lotto.domain; + +import lotto.domain.generator.LottoGenerator; +import lotto.domain.result.LottoResult; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Lottos { + + private final List lottos; + private final int quantity; + + public Lottos(List lottos) { + this.lottos = new ArrayList<>(lottos); + this.quantity = lottos.size(); + } + + public static Lottos from(LottoGenerator lottoGenerator, PurchasingMoney purchasingMoney) { + return new Lottos(Stream.generate(() -> Lotto.from(lottoGenerator)) + .limit(purchasingMoney.getQuantity()) + .collect(Collectors.toList())); + } + + public List compare(List winningLotto, BonusNumber bonusNumber) { + return lottos.stream() + .map(lotto -> lotto.getResult(winningLotto, bonusNumber)) + .collect(Collectors.toList()); + } + + public List getLottos() { + return lottos; + } + + public int getQuantity() { + return quantity; + } +} From ae8f030f36b0ac33ccd432eef163580850662366 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:02:44 +0900 Subject: [PATCH 09/51] =?UTF-8?q?refactor:=20Lotto=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EC=9D=B4=EB=8F=99=20=EB=B0=8F=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=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/lotto/Lotto.java | 20 ------- src/main/java/lotto/domain/Lotto.java | 78 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 20 deletions(-) delete mode 100644 src/main/java/lotto/Lotto.java create mode 100644 src/main/java/lotto/domain/Lotto.java diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 519793d1f7..0000000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,20 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException(); - } - } - - // TODO: 추가 기능 구현 -} diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java new file mode 100644 index 0000000000..78a5b495ae --- /dev/null +++ b/src/main/java/lotto/domain/Lotto.java @@ -0,0 +1,78 @@ +package lotto.domain; + +import lotto.domain.generator.LottoGenerator; +import lotto.domain.result.LottoResult; + +import java.util.ArrayList; +import java.util.List; + +public class Lotto { + + private static final int THE_NUMBER_OF_LOTTO = 6; + private static final int MIN_VALUE = 1; + private static final int MAX_VALUE = 45; + + private final List numbers; + + public Lotto(List numbers) { + validateSize(numbers); + validateRange(numbers); + this.numbers = new ArrayList<>(numbers); + } + + public static Lotto from(LottoGenerator lottoGenerator) { + return new Lotto(lottoGenerator.issue(MIN_VALUE, MAX_VALUE, THE_NUMBER_OF_LOTTO)); + } + + public List compare(Lottos issuedLotto, BonusNumber bonusNumber) { + return issuedLotto.compare(numbers, bonusNumber); + } + + public void validateDuplication(int bonusNumber) { + if (numbers.contains(bonusNumber)) { + throw new IllegalArgumentException(String.format("[ERROR] %d는 당첨 번호와 중복됩니다.", bonusNumber)); + } + } + + public void validateRange(int number) { + if (isNotInRange(number)) { + throw new IllegalArgumentException(String.format("[ERROR] 로또 번호는 %d부터 %d 사이의 숫자여야 합니다.", MIN_VALUE, MAX_VALUE)); + } + } + + public LottoResult getResult(List other, BonusNumber bonusNumber) { + int matchingNumber = (int) numbers.stream() + .filter(other::contains) + .count(); + if (matchingNumber < LottoResult.FIFTH_PRIZE.getMatchingNumber()) { + return LottoResult.NO_PRIZE; + } + int matchingNumberWithBonusNumber = (int) numbers.stream() + .filter(number -> number == bonusNumber.getNumber()) + .count(); + return LottoResult.find(matchingNumber, matchingNumberWithBonusNumber); + } + + public List getNumbers() { + return numbers; + } + + private void validateSize(List numbers) { + if (numbers.size() != THE_NUMBER_OF_LOTTO) { + throw new IllegalArgumentException(String.format("[ERROR] 로또 번호의 개수가 %d개가 아닙니다.", THE_NUMBER_OF_LOTTO)); + } + } + + private void validateRange(List numbers) { + numbers.stream() + .filter(this::isNotInRange) + .findAny() + .ifPresent(number -> { + throw new IllegalArgumentException(String.format("[ERROR] 로또 번호는 %d부터 %d 사이의 숫자여야 합니다.", MIN_VALUE, MAX_VALUE)); + }); + } + + private boolean isNotInRange(int number) { + return number < MIN_VALUE || number > MAX_VALUE; + } +} From 594210f69efd81e4d4fc271624178ec429673a81 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:03:17 +0900 Subject: [PATCH 10/51] =?UTF-8?q?feat:=20WinningLottoGenerator=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/WinningLottoGenerator.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/lotto/domain/generator/WinningLottoGenerator.java diff --git a/src/main/java/lotto/domain/generator/WinningLottoGenerator.java b/src/main/java/lotto/domain/generator/WinningLottoGenerator.java new file mode 100644 index 0000000000..4d5513f19a --- /dev/null +++ b/src/main/java/lotto/domain/generator/WinningLottoGenerator.java @@ -0,0 +1,63 @@ +package lotto.domain.generator; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class WinningLottoGenerator implements LottoGenerator { + + private static final String DELIMITER = ","; + + private final String input; + + private WinningLottoGenerator(String input) { + this.input = input; + } + + public static WinningLottoGenerator from(String input) { + return new WinningLottoGenerator(input); + } + + @Override + public List issue(int minValue, int maxValue, int quantity) { + String[] numbers = input.split(DELIMITER); + validateDelimiter(numbers, quantity); + validateSize(numbers, quantity); + validateDuplication(numbers, quantity); + validateRange(numbers, minValue, maxValue); + return Arrays.stream(numbers) + .map(Integer::parseInt) + .collect(Collectors.toList()); + } + + private void validateDelimiter(String[] numbers, int quantity) { + if (numbers.length != quantity) { + throw new IllegalArgumentException(String.format("[ERROR] %s 외 구분자가 입력되었습니다.", DELIMITER)); + } + } + + private void validateDuplication(String[] numbers, int quantity) { + int numberWithoutDuplication = (int) Arrays.stream(numbers) + .distinct() + .count(); + if (numberWithoutDuplication != quantity) { + throw new IllegalArgumentException("[ERROR] 당첨 번호는 중복될 수 없습니다."); + } + } + + private void validateSize(String[] numbers, int quantity) { + if (numbers.length != quantity) { + throw new IllegalArgumentException(String.format("[ERROR] 당첨 번호의 개수가 %d개가 아닙니다.", quantity)); + } + } + + private void validateRange(String[] numbers, int minValue, int maxValue) { + Arrays.stream(numbers) + .mapToInt(Integer::parseInt) + .filter(number -> number < minValue || number > maxValue) + .findAny() + .ifPresent(number -> { + throw new IllegalArgumentException(String.format("[ERROR] 로또 번호는 %d부터 %d 사이의 숫자여야 합니다.", minValue, maxValue)); + }); + } +} From 3cc0e59182629f40b55b9975428919efa60e2ece Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:03:36 +0900 Subject: [PATCH 11/51] =?UTF-8?q?feat:=20WinningDetail=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/domain/result/WinningDetail.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/lotto/domain/result/WinningDetail.java diff --git a/src/main/java/lotto/domain/result/WinningDetail.java b/src/main/java/lotto/domain/result/WinningDetail.java new file mode 100644 index 0000000000..ec44cefa73 --- /dev/null +++ b/src/main/java/lotto/domain/result/WinningDetail.java @@ -0,0 +1,49 @@ +package lotto.domain.result; + +import java.text.DecimalFormat; +import java.util.List; + +public class WinningDetail { + + private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("###,###"); + + private final int firstPrize; + private final int secondPrize; + private final int thirdPrize; + private final int fourthPrize; + private final int fifthPrize; + private final int totalPrize; + + private WinningDetail(int firstPrize, int secondPrize, int thirdPrize, int fourthPrize, int fifthPrize, int totalPrize) { + this.firstPrize = firstPrize; + this.secondPrize = secondPrize; + this.thirdPrize = thirdPrize; + this.fourthPrize = fourthPrize; + this.fifthPrize = fifthPrize; + this.totalPrize = totalPrize; + } + + public static WinningDetail from(List lottoResults) { + int firstPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.FIRST_PRIZE == lottoResult).count(); + int secondPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.SECOND_PRIZE == lottoResult).count(); + int thirdPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.THIRD_PRIZE == lottoResult).count(); + int fourthPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.FOURTH_PRIZE == lottoResult).count(); + int fifthPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.FIFTH_PRIZE == lottoResult).count(); + int totalPrize = lottoResults.stream().mapToInt(LottoResult::getPrize).sum(); + return new WinningDetail(firstPrize, secondPrize, thirdPrize, fourthPrize, fifthPrize, totalPrize); + } + + public String getResult() { + StringBuilder detail = new StringBuilder(); + detail.append(String.format("%s (%s원) - %d개\n", LottoResult.FIFTH_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.FIFTH_PRIZE.getPrize()), fifthPrize)) + .append(String.format("%s (%s원) - %d개\n", LottoResult.FOURTH_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.FOURTH_PRIZE.getPrize()), fourthPrize)) + .append(String.format("%s (%s원) - %d개\n", LottoResult.THIRD_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.THIRD_PRIZE.getPrize()), thirdPrize)) + .append(String.format("%s (%s원) - %d개\n", LottoResult.SECOND_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.SECOND_PRIZE.getPrize()), secondPrize)) + .append(String.format("%s (%s원) - %d개\n", LottoResult.FIRST_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.FIRST_PRIZE.getPrize()), firstPrize)); + return detail.toString(); + } + + public int getTotalPrize() { + return totalPrize; + } +} From bb44cf0e11de988ee369f6c39011991d633c0e1b Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:04:00 +0900 Subject: [PATCH 12/51] =?UTF-8?q?feat:=20ProfileRate=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/domain/result/ProfitRate.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/lotto/domain/result/ProfitRate.java diff --git a/src/main/java/lotto/domain/result/ProfitRate.java b/src/main/java/lotto/domain/result/ProfitRate.java new file mode 100644 index 0000000000..cbe2f783c1 --- /dev/null +++ b/src/main/java/lotto/domain/result/ProfitRate.java @@ -0,0 +1,24 @@ +package lotto.domain.result; + +public class ProfitRate { + + private static final int CIPHER = 10; + + private final double value; + + private ProfitRate(double value) { + this.value = value; + } + + public static ProfitRate from(int prize, int purchasingMoney) { + return new ProfitRate(round(((double) prize / purchasingMoney) * 100)); + } + + public double getValue() { + return value; + } + + private static double round(double profitRate) { + return Math.round(profitRate * CIPHER) / (double) CIPHER; + } +} From 9b2502cb663abfc0a2a141f13e85697501ac9f88 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:04:26 +0900 Subject: [PATCH 13/51] =?UTF-8?q?feat:=20BonusNumber=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/lotto/domain/BonusNumber.java | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/lotto/domain/BonusNumber.java diff --git a/src/main/java/lotto/domain/BonusNumber.java b/src/main/java/lotto/domain/BonusNumber.java new file mode 100644 index 0000000000..4dadfc62ab --- /dev/null +++ b/src/main/java/lotto/domain/BonusNumber.java @@ -0,0 +1,23 @@ +package lotto.domain; + +import lotto.util.Convertor; + +public class BonusNumber { + + private final int number; + + private BonusNumber(int number) { + this.number = number; + } + + public static BonusNumber from(Lotto lotto, String input) { + int number = Convertor.toInteger(input); + lotto.validateRange(number); + lotto.validateDuplication(number); + return new BonusNumber(number); + } + + public int getNumber() { + return number; + } +} From 6927b614ac3c05919e200997dd0711b9de4b89e7 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:04:33 +0900 Subject: [PATCH 14/51] =?UTF-8?q?feat:=20Convertor=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/lotto/util/Convertor.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/lotto/util/Convertor.java diff --git a/src/main/java/lotto/util/Convertor.java b/src/main/java/lotto/util/Convertor.java new file mode 100644 index 0000000000..77eae6bcd9 --- /dev/null +++ b/src/main/java/lotto/util/Convertor.java @@ -0,0 +1,15 @@ +package lotto.util; + +public class Convertor { + + private Convertor() { + } + + public static int toInteger(String input) { + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("[ERROR] 정수가 아닌 값이 포함되었습니다."); + } + } +} From 7720622906ea568632ea5fec90824f63263c94dc Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:05:09 +0900 Subject: [PATCH 15/51] =?UTF-8?q?feat:=20LottoResult=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/domain/result/LottoResult.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/lotto/domain/result/LottoResult.java diff --git a/src/main/java/lotto/domain/result/LottoResult.java b/src/main/java/lotto/domain/result/LottoResult.java new file mode 100644 index 0000000000..93c79bbd50 --- /dev/null +++ b/src/main/java/lotto/domain/result/LottoResult.java @@ -0,0 +1,44 @@ +package lotto.domain.result; + +import java.util.Arrays; + +public enum LottoResult { + + FIRST_PRIZE(6, 0, 2000000000, "6개 일치"), + SECOND_PRIZE(5, 1, 30000000, "5개 일치, 보너스 볼 일치"), + THIRD_PRIZE(5, 0, 1500000, "5개 일치"), + FOURTH_PRIZE(4, 0, 50000, "4개 일치"), + FIFTH_PRIZE(3, 0, 5000, "3개 일치"), + NO_PRIZE(0, 0, 0, ""); + + private final int matchingNumber; + private final int matchingNumberWithBonusNumber; + private final int prize; + private final String message; + + LottoResult(int matchingNumber, int matchingNumberWithBonusNumber, int prize, String message) { + this.matchingNumber = matchingNumber; + this.matchingNumberWithBonusNumber = matchingNumberWithBonusNumber; + this.prize = prize; + this.message = message; + } + + public static LottoResult find(int matchingNumber, int matchingNumberWithBonusNumber) { + return Arrays.stream(values()) + .filter(value -> value.matchingNumber == matchingNumber && value.matchingNumberWithBonusNumber <= matchingNumberWithBonusNumber) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("[ERROR] 해당 로또에 대한 결과가 없습니다.")); + } + + public int getMatchingNumber() { + return matchingNumber; + } + + public int getPrize() { + return prize; + } + + public String getMessage() { + return message; + } +} From 7f09994d559b163c826195a604a6b0a0f55c19fc Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:05:44 +0900 Subject: [PATCH 16/51] =?UTF-8?q?feat:=20Input=20-=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=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/lotto/util/Input.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/lotto/util/Input.java b/src/main/java/lotto/util/Input.java index ad6ed6906b..5faf7f482c 100644 --- a/src/main/java/lotto/util/Input.java +++ b/src/main/java/lotto/util/Input.java @@ -15,6 +15,11 @@ public static String inputPurchasingMoney() { return removeBlank(Console.readLine()); } + public static String inputWinningLotto() { + System.out.println("\n당첨 번호를 입력해 주세요."); + return removeBlank(Console.readLine()); + } + private static String removeBlank(String input) { return input.replaceAll(BLANK, DELETE); } From 934047596b60af02856e785011eb87754ff3697d Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:06:21 +0900 Subject: [PATCH 17/51] =?UTF-8?q?feat:=20Input=20-=20=EB=B3=B4=EB=84=88?= =?UTF-8?q?=EC=8A=A4=EB=B2=88=ED=98=B8=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=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/lotto/util/Input.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/lotto/util/Input.java b/src/main/java/lotto/util/Input.java index 5faf7f482c..33b893f728 100644 --- a/src/main/java/lotto/util/Input.java +++ b/src/main/java/lotto/util/Input.java @@ -20,6 +20,11 @@ public static String inputWinningLotto() { return removeBlank(Console.readLine()); } + public static String inputBonusNumber() { + System.out.println("\n보너스 번호를 입력해 주세요."); + return removeBlank(Console.readLine()); + } + private static String removeBlank(String input) { return input.replaceAll(BLANK, DELETE); } From fc380a9779535f86a97e78c30f1ad0dd7332c593 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:06:42 +0900 Subject: [PATCH 18/51] =?UTF-8?q?feat:=20Output=20-=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=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/lotto/util/Output.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/lotto/util/Output.java b/src/main/java/lotto/util/Output.java index 7443ef8c82..5d018358d1 100644 --- a/src/main/java/lotto/util/Output.java +++ b/src/main/java/lotto/util/Output.java @@ -16,4 +16,8 @@ public static void printIssuedLotto(Lottos issuedLotto) { .map(String::valueOf) .forEach(System.out::println); } + + public static void printWinningDetail(String winningDetail, double profitRate) { + System.out.printf("\n당첨 통계\n---\n%s\n총 수익률은 %.1f%%입니다.\n", winningDetail, profitRate); + } } From fbff9e1f743fbc4ad5d30ddde5aa5ed3f38ed9f3 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:07:00 +0900 Subject: [PATCH 19/51] =?UTF-8?q?test:=20LottoTest=20-=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=9C=84=EC=B9=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/lotto/{ => domain}/LottoTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) rename src/test/java/lotto/{ => domain}/LottoTest.java (92%) diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/domain/LottoTest.java similarity index 92% rename from src/test/java/lotto/LottoTest.java rename to src/test/java/lotto/domain/LottoTest.java index 0f3af0f6c4..1b241d2357 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/domain/LottoTest.java @@ -1,4 +1,4 @@ -package lotto; +package lotto.domain; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -8,6 +8,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; class LottoTest { + @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") @Test void createLottoByOverSize() { @@ -22,6 +23,4 @@ void createLottoByDuplicatedNumber() { assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) .isInstanceOf(IllegalArgumentException.class); } - - // 아래에 추가 테스트 작성 가능 } From 8a4a9f7fb51eecba0bd85cc124af6e1be1ee8e92 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:07:20 +0900 Subject: [PATCH 20/51] =?UTF-8?q?test:=20ApplicationTest=20-=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/lotto/ApplicationTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index a15c7d1f52..ff06a07b1e 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -8,9 +8,9 @@ import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; class ApplicationTest extends NsTest { - private static final String ERROR_MESSAGE = "[ERROR]"; @Test void 기능_테스트() { @@ -49,8 +49,8 @@ class ApplicationTest extends NsTest { @Test void 예외_테스트() { assertSimpleTest(() -> { - runException("1000j"); - assertThat(output()).contains(ERROR_MESSAGE); + assertSimpleTest(() -> assertThatThrownBy(() -> runException("1000j")) + .isInstanceOf(IllegalArgumentException.class)); }); } From b861e935afd55c266cdb90ecf92c1b0e9427f007 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:07:35 +0900 Subject: [PATCH 21/51] =?UTF-8?q?test:=20BonusNumberTest=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/domain/BonusNumberTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/test/java/lotto/domain/BonusNumberTest.java diff --git a/src/test/java/lotto/domain/BonusNumberTest.java b/src/test/java/lotto/domain/BonusNumberTest.java new file mode 100644 index 0000000000..775f8f3f7b --- /dev/null +++ b/src/test/java/lotto/domain/BonusNumberTest.java @@ -0,0 +1,34 @@ +package lotto.domain; + +import lotto.domain.generator.LottoGenerator; +import lotto.domain.generator.WinningLottoGenerator; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class BonusNumberTest { + + @ParameterizedTest + @ValueSource(strings = {"7", "12", "45"}) + void 당첨번호와_미중복시_보너스번호_생성(String input) { + // given + LottoGenerator winningLottoGenerator = WinningLottoGenerator.from("1,2,3,4,5,6"); + Lotto lotto = Lotto.from(winningLottoGenerator); + // when & then + assertThatCode(() -> BonusNumber.from(lotto, input)) + .doesNotThrowAnyException(); + } + + @ParameterizedTest + @ValueSource(strings = {"6", "k2", "46"}) + void 보너스번호_검증실패시_예외발생(String input) { + // given + LottoGenerator winningLottoGenerator = WinningLottoGenerator.from("1,2,3,4,5,6"); + Lotto lotto = Lotto.from(winningLottoGenerator); + // when & then + assertThatThrownBy(() -> BonusNumber.from(lotto, input)) + .isInstanceOf(IllegalArgumentException.class); + } +} From fd76161f5115d6814188a2b2a2053eb873cd796c Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:08:09 +0900 Subject: [PATCH 22/51] =?UTF-8?q?test:=20ProfitRateTest=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/domain/result/ProfitRateTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/java/lotto/domain/result/ProfitRateTest.java diff --git a/src/test/java/lotto/domain/result/ProfitRateTest.java b/src/test/java/lotto/domain/result/ProfitRateTest.java new file mode 100644 index 0000000000..be0e4a4dba --- /dev/null +++ b/src/test/java/lotto/domain/result/ProfitRateTest.java @@ -0,0 +1,19 @@ +package lotto.domain.result; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ProfitRateTest { + + @Test + void 수익률_소숫점_둘째_자리에서_반올림() { + // given + int prize = 55000; + int purchasingMoney = 7000; + // when + ProfitRate profitRate = ProfitRate.from(prize, purchasingMoney); + // then + assertThat(profitRate.getValue()).isEqualTo(785.7); + } +} From 457e2d529b8fa5e6003934456ea4735f4b8e650d Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:08:33 +0900 Subject: [PATCH 23/51] =?UTF-8?q?test:=20IssuedLottoGeneratorTest=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 --- .../generator/IssuedLottoGeneratorTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/java/lotto/domain/generator/IssuedLottoGeneratorTest.java diff --git a/src/test/java/lotto/domain/generator/IssuedLottoGeneratorTest.java b/src/test/java/lotto/domain/generator/IssuedLottoGeneratorTest.java new file mode 100644 index 0000000000..270f2605dd --- /dev/null +++ b/src/test/java/lotto/domain/generator/IssuedLottoGeneratorTest.java @@ -0,0 +1,19 @@ +package lotto.domain.generator; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatCode; + +public class IssuedLottoGeneratorTest { + + @DisplayName("로또 발행") + @Test + void 로또_발행() { + // given + LottoGenerator issuedLottoGenerator = IssuedLottoGenerator.create(); + // when & then + assertThatCode(() -> issuedLottoGenerator.issue(1, 45, 6)) + .doesNotThrowAnyException(); + } +} From 2e555b5e7e5aa319f8021bf891a538c375f2b0f2 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:08:42 +0900 Subject: [PATCH 24/51] =?UTF-8?q?test:=20WinningLottoGeneratorTest=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 --- .../generator/WinningLottoGeneratorTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/test/java/lotto/domain/generator/WinningLottoGeneratorTest.java diff --git a/src/test/java/lotto/domain/generator/WinningLottoGeneratorTest.java b/src/test/java/lotto/domain/generator/WinningLottoGeneratorTest.java new file mode 100644 index 0000000000..5293f67385 --- /dev/null +++ b/src/test/java/lotto/domain/generator/WinningLottoGeneratorTest.java @@ -0,0 +1,30 @@ +package lotto.domain.generator; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class WinningLottoGeneratorTest { + + @ParameterizedTest + @ValueSource(strings = {"1,2,3,4,5,6", "31,22,45,10,8,29"}) + void 입력받은_당첨번호로_로또번호_생성(String input) { + // given + LottoGenerator winningLottoGenerator = WinningLottoGenerator.from(input); + // when & then + assertThatCode(() -> winningLottoGenerator.issue(1, 45, 6)) + .doesNotThrowAnyException(); + } + + @ParameterizedTest + @ValueSource(strings = {"1,2,3,4,5", "31,22,42,10,8,46", "1-2-3-4-5-6", "1,2,3-4-5-6", "1,1,2,3,4,5", "k,1,2,3,4,5"}) + void 입력받은_당첨번호_검증실패시_예외발생(String input) { + // given + LottoGenerator winningLottoGenerator = WinningLottoGenerator.from(input); + // when & then + assertThatThrownBy(() -> winningLottoGenerator.issue(1, 45, 6)) + .isInstanceOf(IllegalArgumentException.class); + } +} From 7c0c0cef13a1afbafff47bde2a12ac4587ff3d18 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Wed, 29 Mar 2023 20:11:55 +0900 Subject: [PATCH 25/51] =?UTF-8?q?refactor:=20Lotto=20-=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=EA=B2=80=EC=A6=9D=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/Lotto.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 78a5b495ae..06125bd2b6 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -4,6 +4,7 @@ import lotto.domain.result.LottoResult; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; public class Lotto { @@ -17,6 +18,7 @@ public class Lotto { public Lotto(List numbers) { validateSize(numbers); validateRange(numbers); + validateDuplication(numbers); this.numbers = new ArrayList<>(numbers); } @@ -75,4 +77,10 @@ private void validateRange(List numbers) { private boolean isNotInRange(int number) { return number < MIN_VALUE || number > MAX_VALUE; } + + private void validateDuplication(List numbers) { + if (new HashSet<>(numbers).size() != THE_NUMBER_OF_LOTTO) { + throw new IllegalArgumentException("중복되는 번호가 포함되어 있습니다."); + } + } } From f0823618fae803eacc63ed45b0e67bc74ff398d2 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Thu, 30 Mar 2023 19:09:56 +0900 Subject: [PATCH 26/51] =?UTF-8?q?refactor:=20LottoManager=20-=20=EB=B0=9C?= =?UTF-8?q?=ED=96=89=20=EB=A1=9C=EB=98=90=20=EB=B3=80=EC=88=98=20=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EB=B0=8D=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/lotto/service/LottoManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java index 25589e20ec..dbf07ea1c4 100644 --- a/src/main/java/lotto/service/LottoManager.java +++ b/src/main/java/lotto/service/LottoManager.java @@ -16,12 +16,12 @@ public class LottoManager implements Manager { @Override public void run() { PurchasingMoney purchasingMoney = PurchasingMoney.from(Input.inputPurchasingMoney()); - Lottos issuedLotto = purchaseLotto(purchasingMoney); + Lottos issuedLottos = purchaseLotto(purchasingMoney); Lotto winningLotto = Lotto.from(WinningLottoGenerator.from(Input.inputWinningLotto())); BonusNumber bonusNumber = BonusNumber.from(winningLotto, Input.inputBonusNumber()); - compareLotto(purchasingMoney, issuedLotto, winningLotto, bonusNumber); + compareLotto(purchasingMoney, issuedLottos, winningLotto, bonusNumber); } private Lottos purchaseLotto(PurchasingMoney purchasingMoney) { From d62110df84646243e3fc297efcbf27f1f0fce28c Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Thu, 30 Mar 2023 19:11:53 +0900 Subject: [PATCH 27/51] =?UTF-8?q?refactor:=20PurchasingMoney=20-=20Convert?= =?UTF-8?q?or=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/PurchasingMoney.java | 4 ++-- src/main/java/lotto/service/LottoManager.java | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/domain/PurchasingMoney.java b/src/main/java/lotto/domain/PurchasingMoney.java index 147bc69927..1dfb1bf67c 100644 --- a/src/main/java/lotto/domain/PurchasingMoney.java +++ b/src/main/java/lotto/domain/PurchasingMoney.java @@ -15,8 +15,8 @@ private PurchasingMoney(int money) { this.quantity = money / UNIT; } - public static PurchasingMoney from(String input) { - return new PurchasingMoney(Convertor.toInteger(input)); + public static PurchasingMoney from(int money) { + return new PurchasingMoney(money); } public int getMoney() { diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java index dbf07ea1c4..0971bfd89f 100644 --- a/src/main/java/lotto/service/LottoManager.java +++ b/src/main/java/lotto/service/LottoManager.java @@ -8,6 +8,7 @@ import lotto.domain.generator.WinningLottoGenerator; import lotto.domain.result.ProfitRate; import lotto.domain.result.WinningDetail; +import lotto.util.Convertor; import lotto.util.Input; import lotto.util.Output; @@ -15,7 +16,7 @@ public class LottoManager implements Manager { @Override public void run() { - PurchasingMoney purchasingMoney = PurchasingMoney.from(Input.inputPurchasingMoney()); + PurchasingMoney purchasingMoney = PurchasingMoney.from(Convertor.toInteger(Input.inputPurchasingMoney())); Lottos issuedLottos = purchaseLotto(purchasingMoney); Lotto winningLotto = Lotto.from(WinningLottoGenerator.from(Input.inputWinningLotto())); From 6dc337d90ae1080c458022a443cf75cd93749b72 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Thu, 30 Mar 2023 19:12:03 +0900 Subject: [PATCH 28/51] =?UTF-8?q?refactor:=20BonusNumber=20-=20Convertor?= =?UTF-8?q?=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/BonusNumber.java | 3 +-- src/main/java/lotto/service/LottoManager.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/domain/BonusNumber.java b/src/main/java/lotto/domain/BonusNumber.java index 4dadfc62ab..1a3fc1747d 100644 --- a/src/main/java/lotto/domain/BonusNumber.java +++ b/src/main/java/lotto/domain/BonusNumber.java @@ -10,8 +10,7 @@ private BonusNumber(int number) { this.number = number; } - public static BonusNumber from(Lotto lotto, String input) { - int number = Convertor.toInteger(input); + public static BonusNumber from(Lotto lotto, int number) { lotto.validateRange(number); lotto.validateDuplication(number); return new BonusNumber(number); diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java index 0971bfd89f..c53f27fc5c 100644 --- a/src/main/java/lotto/service/LottoManager.java +++ b/src/main/java/lotto/service/LottoManager.java @@ -20,7 +20,7 @@ public void run() { Lottos issuedLottos = purchaseLotto(purchasingMoney); Lotto winningLotto = Lotto.from(WinningLottoGenerator.from(Input.inputWinningLotto())); - BonusNumber bonusNumber = BonusNumber.from(winningLotto, Input.inputBonusNumber()); + BonusNumber bonusNumber = BonusNumber.from(winningLotto, Convertor.toInteger(Input.inputBonusNumber())); compareLotto(purchasingMoney, issuedLottos, winningLotto, bonusNumber); } From a64f3d14cfc648f13c666eeeef1f4cab1e7c6554 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Thu, 30 Mar 2023 19:22:02 +0900 Subject: [PATCH 29/51] =?UTF-8?q?feat:=20PurchasingMoney=20-=20=EC=96=91?= =?UTF-8?q?=EC=88=98=20=EA=B2=80=EC=A6=9D=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/PurchasingMoney.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/domain/PurchasingMoney.java b/src/main/java/lotto/domain/PurchasingMoney.java index 1dfb1bf67c..8f3527582a 100644 --- a/src/main/java/lotto/domain/PurchasingMoney.java +++ b/src/main/java/lotto/domain/PurchasingMoney.java @@ -1,7 +1,5 @@ package lotto.domain; -import lotto.util.Convertor; - public class PurchasingMoney { private static final int UNIT = 1000; @@ -10,6 +8,7 @@ public class PurchasingMoney { private final int quantity; private PurchasingMoney(int money) { + validatePositive(money); validateUnit(money); this.money = money; this.quantity = money / UNIT; @@ -27,6 +26,12 @@ public int getQuantity() { return quantity; } + private void validatePositive(int money) { + if (money <= 0) { + throw new IllegalArgumentException("[ERROR] 구입금액은 양수여야 합니다."); + } + } + private void validateUnit(int money) { if (money % UNIT != 0) { throw new IllegalArgumentException(String.format("[ERROR] 구입금액은 %d원 단위여야 합니다.", UNIT)); From 22abd29f7122cc9391a0a710a27fbf85680a89ef Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Thu, 30 Mar 2023 19:47:21 +0900 Subject: [PATCH 30/51] =?UTF-8?q?refactor:=20IssuedLottoGenerator=20-=20?= =?UTF-8?q?=EC=8B=B1=EA=B8=80=ED=86=A4=20=ED=8C=A8=ED=84=B4=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/domain/generator/IssuedLottoGenerator.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java b/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java index d231842840..7a0ea95c64 100644 --- a/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java +++ b/src/main/java/lotto/domain/generator/IssuedLottoGenerator.java @@ -7,11 +7,13 @@ public class IssuedLottoGenerator implements LottoGenerator { + private static final LottoGenerator ISSUED_LOTTO_GENERATOR = new IssuedLottoGenerator(); + private IssuedLottoGenerator() { } - public static IssuedLottoGenerator create() { - return new IssuedLottoGenerator(); + public static LottoGenerator create() { + return ISSUED_LOTTO_GENERATOR; } @Override From 823f4a3d9f9d5583b6f030c587c626cba5b07f37 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Thu, 30 Mar 2023 20:01:05 +0900 Subject: [PATCH 31/51] =?UTF-8?q?refactor:=20BonusNumber=20-=20=EB=AF=B8?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=20=EC=9E=84=ED=8F=AC=ED=8A=B8=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/BonusNumber.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/lotto/domain/BonusNumber.java b/src/main/java/lotto/domain/BonusNumber.java index 1a3fc1747d..841bdcdd07 100644 --- a/src/main/java/lotto/domain/BonusNumber.java +++ b/src/main/java/lotto/domain/BonusNumber.java @@ -1,7 +1,5 @@ package lotto.domain; -import lotto.util.Convertor; - public class BonusNumber { private final int number; From 17a599491a25ac884a8ff6cdd020c90021df592f Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Fri, 31 Mar 2023 16:00:38 +0900 Subject: [PATCH 32/51] =?UTF-8?q?refactor:=20Lottos=20-=20quantity=20?= =?UTF-8?q?=ED=95=84=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/lotto/domain/Lottos.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/lotto/domain/Lottos.java b/src/main/java/lotto/domain/Lottos.java index 6290c5955b..e2bf920d08 100644 --- a/src/main/java/lotto/domain/Lottos.java +++ b/src/main/java/lotto/domain/Lottos.java @@ -11,11 +11,9 @@ public class Lottos { private final List lottos; - private final int quantity; public Lottos(List lottos) { this.lottos = new ArrayList<>(lottos); - this.quantity = lottos.size(); } public static Lottos from(LottoGenerator lottoGenerator, PurchasingMoney purchasingMoney) { @@ -35,6 +33,6 @@ public List getLottos() { } public int getQuantity() { - return quantity; + return lottos.size(); } } From 8d67c300b436bdfe577992284a2f7b23fe679dfe Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Fri, 31 Mar 2023 16:04:25 +0900 Subject: [PATCH 33/51] =?UTF-8?q?refactor:=20Lotto=20-=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EB=B2=94=EC=9C=84=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=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/lotto/domain/Lotto.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 06125bd2b6..09870f5fcf 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -66,12 +66,13 @@ private void validateSize(List numbers) { } private void validateRange(List numbers) { - numbers.stream() - .filter(this::isNotInRange) - .findAny() - .ifPresent(number -> { - throw new IllegalArgumentException(String.format("[ERROR] 로또 번호는 %d부터 %d 사이의 숫자여야 합니다.", MIN_VALUE, MAX_VALUE)); - }); + if (hasNumberOutOfRange(numbers)) { + throw new IllegalArgumentException(String.format("[ERROR] 로또 번호는 %d부터 %d 사이의 숫자여야 합니다.", MIN_VALUE, MAX_VALUE)); + } + } + + private boolean hasNumberOutOfRange(List numbers) { + return numbers.stream().anyMatch(this::isNotInRange); } private boolean isNotInRange(int number) { From 82f1c0cdb17582c4f661471ca5f3d4fb153ada0a Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 00:53:01 +0900 Subject: [PATCH 34/51] =?UTF-8?q?refactor:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EC=82=B0=EC=B6=9C=20=EB=A1=9C=EC=A7=81=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/lotto/domain/Lotto.java | 8 +---- .../java/lotto/domain/result/LottoResult.java | 29 +++++++++---------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index 09870f5fcf..d0d6abeb55 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -46,13 +46,7 @@ public LottoResult getResult(List other, BonusNumber bonusNumber) { int matchingNumber = (int) numbers.stream() .filter(other::contains) .count(); - if (matchingNumber < LottoResult.FIFTH_PRIZE.getMatchingNumber()) { - return LottoResult.NO_PRIZE; - } - int matchingNumberWithBonusNumber = (int) numbers.stream() - .filter(number -> number == bonusNumber.getNumber()) - .count(); - return LottoResult.find(matchingNumber, matchingNumberWithBonusNumber); + return LottoResult.find(matchingNumber, numbers.contains(bonusNumber.getNumber())); } public List getNumbers() { diff --git a/src/main/java/lotto/domain/result/LottoResult.java b/src/main/java/lotto/domain/result/LottoResult.java index 93c79bbd50..1df73eef3c 100644 --- a/src/main/java/lotto/domain/result/LottoResult.java +++ b/src/main/java/lotto/domain/result/LottoResult.java @@ -4,34 +4,31 @@ public enum LottoResult { - FIRST_PRIZE(6, 0, 2000000000, "6개 일치"), - SECOND_PRIZE(5, 1, 30000000, "5개 일치, 보너스 볼 일치"), - THIRD_PRIZE(5, 0, 1500000, "5개 일치"), - FOURTH_PRIZE(4, 0, 50000, "4개 일치"), - FIFTH_PRIZE(3, 0, 5000, "3개 일치"), - NO_PRIZE(0, 0, 0, ""); + FIRST_PRIZE(6, false, 2000000000, "6개 일치"), + SECOND_PRIZE(5, true, 30000000, "5개 일치, 보너스 볼 일치"), + THIRD_PRIZE(5, false, 1500000, "5개 일치"), + FOURTH_PRIZE(4, false, 50000, "4개 일치"), + FIFTH_PRIZE(3, false, 5000, "3개 일치"), + NO_PRIZE(0, false, 0, ""); private final int matchingNumber; - private final int matchingNumberWithBonusNumber; + private final boolean hasBonusNumber; private final int prize; private final String message; - LottoResult(int matchingNumber, int matchingNumberWithBonusNumber, int prize, String message) { + LottoResult(int matchingNumber, boolean hasBonusNumber, int prize, String message) { this.matchingNumber = matchingNumber; - this.matchingNumberWithBonusNumber = matchingNumberWithBonusNumber; + this.hasBonusNumber = hasBonusNumber; this.prize = prize; this.message = message; } - public static LottoResult find(int matchingNumber, int matchingNumberWithBonusNumber) { + public static LottoResult find(int matchingNumber, boolean hasBonusNumber) { return Arrays.stream(values()) - .filter(value -> value.matchingNumber == matchingNumber && value.matchingNumberWithBonusNumber <= matchingNumberWithBonusNumber) + .filter(value -> value.matchingNumber == matchingNumber) + .filter(value -> !value.hasBonusNumber || hasBonusNumber) .findAny() - .orElseThrow(() -> new IllegalArgumentException("[ERROR] 해당 로또에 대한 결과가 없습니다.")); - } - - public int getMatchingNumber() { - return matchingNumber; + .orElse(NO_PRIZE); } public int getPrize() { From 05b2a0379eb9601c60b4589606ec50c0c3493ff0 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 01:09:12 +0900 Subject: [PATCH 35/51] =?UTF-8?q?style:=20LottoResult=20-=20=EC=83=81?= =?UTF-8?q?=EA=B8=88=20=EA=B0=92=20=EC=96=B8=EB=8D=94=EB=B0=94=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/result/LottoResult.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/lotto/domain/result/LottoResult.java b/src/main/java/lotto/domain/result/LottoResult.java index 1df73eef3c..db93f74e72 100644 --- a/src/main/java/lotto/domain/result/LottoResult.java +++ b/src/main/java/lotto/domain/result/LottoResult.java @@ -4,11 +4,11 @@ public enum LottoResult { - FIRST_PRIZE(6, false, 2000000000, "6개 일치"), - SECOND_PRIZE(5, true, 30000000, "5개 일치, 보너스 볼 일치"), - THIRD_PRIZE(5, false, 1500000, "5개 일치"), - FOURTH_PRIZE(4, false, 50000, "4개 일치"), - FIFTH_PRIZE(3, false, 5000, "3개 일치"), + FIRST_PRIZE(6, false, 2_000_000_000, "6개 일치"), + SECOND_PRIZE(5, true, 30_000_000, "5개 일치, 보너스 볼 일치"), + THIRD_PRIZE(5, false, 1_500_000, "5개 일치"), + FOURTH_PRIZE(4, false, 50_000, "4개 일치"), + FIFTH_PRIZE(3, false, 5_000, "3개 일치"), NO_PRIZE(0, false, 0, ""); private final int matchingNumber; From fb6ca9c37d2a3b8bc8d9aee1efe58e3b2ac70de3 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 01:13:13 +0900 Subject: [PATCH 36/51] =?UTF-8?q?refactor:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=20=EB=A1=9C=EC=A7=81=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/lotto/domain/Lotto.java | 17 ++++++++--------- src/main/java/lotto/domain/Lottos.java | 2 +- src/main/java/lotto/service/LottoManager.java | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index d0d6abeb55..d01b628314 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -26,10 +26,6 @@ public static Lotto from(LottoGenerator lottoGenerator) { return new Lotto(lottoGenerator.issue(MIN_VALUE, MAX_VALUE, THE_NUMBER_OF_LOTTO)); } - public List compare(Lottos issuedLotto, BonusNumber bonusNumber) { - return issuedLotto.compare(numbers, bonusNumber); - } - public void validateDuplication(int bonusNumber) { if (numbers.contains(bonusNumber)) { throw new IllegalArgumentException(String.format("[ERROR] %d는 당첨 번호와 중복됩니다.", bonusNumber)); @@ -42,11 +38,8 @@ public void validateRange(int number) { } } - public LottoResult getResult(List other, BonusNumber bonusNumber) { - int matchingNumber = (int) numbers.stream() - .filter(other::contains) - .count(); - return LottoResult.find(matchingNumber, numbers.contains(bonusNumber.getNumber())); + public LottoResult getResult(Lotto other, int bonusNumber) { + return LottoResult.find(getMatchingNumber(other), numbers.contains(bonusNumber)); } public List getNumbers() { @@ -78,4 +71,10 @@ private void validateDuplication(List numbers) { throw new IllegalArgumentException("중복되는 번호가 포함되어 있습니다."); } } + + private int getMatchingNumber(Lotto other) { + return (int) numbers.stream() + .filter(other.numbers::contains) + .count(); + } } diff --git a/src/main/java/lotto/domain/Lottos.java b/src/main/java/lotto/domain/Lottos.java index e2bf920d08..3a87987818 100644 --- a/src/main/java/lotto/domain/Lottos.java +++ b/src/main/java/lotto/domain/Lottos.java @@ -22,7 +22,7 @@ public static Lottos from(LottoGenerator lottoGenerator, PurchasingMoney purchas .collect(Collectors.toList())); } - public List compare(List winningLotto, BonusNumber bonusNumber) { + public List compare(Lotto winningLotto, int bonusNumber) { return lottos.stream() .map(lotto -> lotto.getResult(winningLotto, bonusNumber)) .collect(Collectors.toList()); diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java index c53f27fc5c..c23c54c015 100644 --- a/src/main/java/lotto/service/LottoManager.java +++ b/src/main/java/lotto/service/LottoManager.java @@ -32,7 +32,7 @@ private Lottos purchaseLotto(PurchasingMoney purchasingMoney) { } private void compareLotto(PurchasingMoney purchasingMoney, Lottos issuedLotto, Lotto winningLotto, BonusNumber bonusNumber) { - WinningDetail winningDetail = WinningDetail.from(winningLotto.compare(issuedLotto, bonusNumber)); + WinningDetail winningDetail = WinningDetail.from(issuedLotto.compare(winningLotto, bonusNumber.getNumber())); ProfitRate profitRate = ProfitRate.from(winningDetail.getTotalPrize(), purchasingMoney.getMoney()); Output.printWinningDetail(winningDetail.getResult(), profitRate.getValue()); } From 7eca0282873656fc3685ec77fe18ed5a19cffa59 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 01:13:49 +0900 Subject: [PATCH 37/51] =?UTF-8?q?refactor:=20ProfitRate=20-=20=EB=B0=98?= =?UTF-8?q?=EC=98=AC=EB=A6=BC=20=EB=A1=9C=EC=A7=81=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/lotto/domain/result/ProfitRate.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/lotto/domain/result/ProfitRate.java b/src/main/java/lotto/domain/result/ProfitRate.java index cbe2f783c1..2840b9ff1f 100644 --- a/src/main/java/lotto/domain/result/ProfitRate.java +++ b/src/main/java/lotto/domain/result/ProfitRate.java @@ -2,8 +2,6 @@ public class ProfitRate { - private static final int CIPHER = 10; - private final double value; private ProfitRate(double value) { @@ -11,14 +9,10 @@ private ProfitRate(double value) { } public static ProfitRate from(int prize, int purchasingMoney) { - return new ProfitRate(round(((double) prize / purchasingMoney) * 100)); + return new ProfitRate(((double) prize / purchasingMoney) * 100); } public double getValue() { return value; } - - private static double round(double profitRate) { - return Math.round(profitRate * CIPHER) / (double) CIPHER; - } } From 3ce2aab76b9fb381e91a3931c40528d0d0a3cb8d Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 01:17:52 +0900 Subject: [PATCH 38/51] =?UTF-8?q?test:=20ProfitRateTest=20-=20=EC=86=8C?= =?UTF-8?q?=EC=88=AB=EC=A0=90=20=EB=91=98=EC=A7=B8=20=EC=9E=90=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=98=EC=98=AC=EB=A6=BC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=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/test/java/lotto/domain/result/ProfitRateTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/lotto/domain/result/ProfitRateTest.java b/src/test/java/lotto/domain/result/ProfitRateTest.java index be0e4a4dba..1b025ddfa9 100644 --- a/src/test/java/lotto/domain/result/ProfitRateTest.java +++ b/src/test/java/lotto/domain/result/ProfitRateTest.java @@ -14,6 +14,6 @@ public class ProfitRateTest { // when ProfitRate profitRate = ProfitRate.from(prize, purchasingMoney); // then - assertThat(profitRate.getValue()).isEqualTo(785.7); + assertThat(String.format("%.1f", profitRate.getValue())).isEqualTo("785.7"); } } From 4239f816b32fab5eec18b46b6efebc4a153a7407 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 01:33:23 +0900 Subject: [PATCH 39/51] =?UTF-8?q?refactor:=20BonusNumber=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=EB=A1=9C=EC=A7=81=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/lotto/domain/BonusNumber.java | 17 +++++++++++++++-- src/main/java/lotto/domain/Lotto.java | 16 ++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main/java/lotto/domain/BonusNumber.java b/src/main/java/lotto/domain/BonusNumber.java index 841bdcdd07..e3523ed9c2 100644 --- a/src/main/java/lotto/domain/BonusNumber.java +++ b/src/main/java/lotto/domain/BonusNumber.java @@ -9,11 +9,24 @@ private BonusNumber(int number) { } public static BonusNumber from(Lotto lotto, int number) { - lotto.validateRange(number); - lotto.validateDuplication(number); + validateRange(lotto, number); + validateDuplication(lotto, number); return new BonusNumber(number); } + private static void validateRange(Lotto lotto, int number) { + if (lotto.isNotInRange(number)) { + throw new IllegalArgumentException(String.format("[ERROR] %d는 범위를 벗어난 숫자입니다.", number)); + } + + } + + private static void validateDuplication(Lotto lotto, int number) { + if (lotto.has(number)) { + throw new IllegalArgumentException(String.format("[ERROR] %d는 당첨 번호와 중복됩니다.", number)); + } + } + public int getNumber() { return number; } diff --git a/src/main/java/lotto/domain/Lotto.java b/src/main/java/lotto/domain/Lotto.java index d01b628314..b2a45c3736 100644 --- a/src/main/java/lotto/domain/Lotto.java +++ b/src/main/java/lotto/domain/Lotto.java @@ -26,16 +26,12 @@ public static Lotto from(LottoGenerator lottoGenerator) { return new Lotto(lottoGenerator.issue(MIN_VALUE, MAX_VALUE, THE_NUMBER_OF_LOTTO)); } - public void validateDuplication(int bonusNumber) { - if (numbers.contains(bonusNumber)) { - throw new IllegalArgumentException(String.format("[ERROR] %d는 당첨 번호와 중복됩니다.", bonusNumber)); - } + public boolean isNotInRange(int number) { + return number < MIN_VALUE || number > MAX_VALUE; } - public void validateRange(int number) { - if (isNotInRange(number)) { - throw new IllegalArgumentException(String.format("[ERROR] 로또 번호는 %d부터 %d 사이의 숫자여야 합니다.", MIN_VALUE, MAX_VALUE)); - } + public boolean has(int number) { + return numbers.contains(number); } public LottoResult getResult(Lotto other, int bonusNumber) { @@ -62,10 +58,6 @@ private boolean hasNumberOutOfRange(List numbers) { return numbers.stream().anyMatch(this::isNotInRange); } - private boolean isNotInRange(int number) { - return number < MIN_VALUE || number > MAX_VALUE; - } - private void validateDuplication(List numbers) { if (new HashSet<>(numbers).size() != THE_NUMBER_OF_LOTTO) { throw new IllegalArgumentException("중복되는 번호가 포함되어 있습니다."); From 51b31fb954af7c84db4b8f4fe46dd1d10d191d75 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 01:34:01 +0900 Subject: [PATCH 40/51] =?UTF-8?q?test:=20BonusNumberTest=20-=20BonusNumber?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/lotto/domain/BonusNumberTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/lotto/domain/BonusNumberTest.java b/src/test/java/lotto/domain/BonusNumberTest.java index 775f8f3f7b..079a9f9a26 100644 --- a/src/test/java/lotto/domain/BonusNumberTest.java +++ b/src/test/java/lotto/domain/BonusNumberTest.java @@ -2,6 +2,7 @@ import lotto.domain.generator.LottoGenerator; import lotto.domain.generator.WinningLottoGenerator; +import lotto.util.Convertor; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -17,7 +18,7 @@ public class BonusNumberTest { LottoGenerator winningLottoGenerator = WinningLottoGenerator.from("1,2,3,4,5,6"); Lotto lotto = Lotto.from(winningLottoGenerator); // when & then - assertThatCode(() -> BonusNumber.from(lotto, input)) + assertThatCode(() -> BonusNumber.from(lotto, Convertor.toInteger(input))) .doesNotThrowAnyException(); } @@ -28,7 +29,7 @@ public class BonusNumberTest { LottoGenerator winningLottoGenerator = WinningLottoGenerator.from("1,2,3,4,5,6"); Lotto lotto = Lotto.from(winningLottoGenerator); // when & then - assertThatThrownBy(() -> BonusNumber.from(lotto, input)) + assertThatThrownBy(() -> BonusNumber.from(lotto, Convertor.toInteger(input))) .isInstanceOf(IllegalArgumentException.class); } } From 8e212adca75b406231528ec66230c507ce383f4c Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 02:07:56 +0900 Subject: [PATCH 41/51] =?UTF-8?q?refactor:=20PurchasingMoney=20-=20?= =?UTF-8?q?=EC=88=98=EB=9F=89=20=ED=95=84=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/lotto/domain/PurchasingMoney.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/lotto/domain/PurchasingMoney.java b/src/main/java/lotto/domain/PurchasingMoney.java index 8f3527582a..1956584731 100644 --- a/src/main/java/lotto/domain/PurchasingMoney.java +++ b/src/main/java/lotto/domain/PurchasingMoney.java @@ -5,13 +5,11 @@ public class PurchasingMoney { private static final int UNIT = 1000; private final int money; - private final int quantity; private PurchasingMoney(int money) { validatePositive(money); validateUnit(money); this.money = money; - this.quantity = money / UNIT; } public static PurchasingMoney from(int money) { @@ -23,7 +21,7 @@ public int getMoney() { } public int getQuantity() { - return quantity; + return money / UNIT; } private void validatePositive(int money) { From 760bb1dd595ab2bce56315ef9390a125c08d4f1a Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:18:52 +0900 Subject: [PATCH 42/51] =?UTF-8?q?refactor:=20WinningDetail=20-=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lotto/domain/result/WinningDetail.java | 39 +++++++------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/src/main/java/lotto/domain/result/WinningDetail.java b/src/main/java/lotto/domain/result/WinningDetail.java index ec44cefa73..87e43f2e89 100644 --- a/src/main/java/lotto/domain/result/WinningDetail.java +++ b/src/main/java/lotto/domain/result/WinningDetail.java @@ -1,36 +1,20 @@ package lotto.domain.result; -import java.text.DecimalFormat; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; public class WinningDetail { - private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("###,###"); + private final Map lottoResults; - private final int firstPrize; - private final int secondPrize; - private final int thirdPrize; - private final int fourthPrize; - private final int fifthPrize; - private final int totalPrize; - - private WinningDetail(int firstPrize, int secondPrize, int thirdPrize, int fourthPrize, int fifthPrize, int totalPrize) { - this.firstPrize = firstPrize; - this.secondPrize = secondPrize; - this.thirdPrize = thirdPrize; - this.fourthPrize = fourthPrize; - this.fifthPrize = fifthPrize; - this.totalPrize = totalPrize; + private WinningDetail(Map lottoResultIntegerMap) { + this.lottoResults = new EnumMap<>(lottoResultIntegerMap); } public static WinningDetail from(List lottoResults) { - int firstPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.FIRST_PRIZE == lottoResult).count(); - int secondPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.SECOND_PRIZE == lottoResult).count(); - int thirdPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.THIRD_PRIZE == lottoResult).count(); - int fourthPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.FOURTH_PRIZE == lottoResult).count(); - int fifthPrize = (int) lottoResults.stream().filter(lottoResult -> LottoResult.FIFTH_PRIZE == lottoResult).count(); - int totalPrize = lottoResults.stream().mapToInt(LottoResult::getPrize).sum(); - return new WinningDetail(firstPrize, secondPrize, thirdPrize, fourthPrize, fifthPrize, totalPrize); + Map lottoResultMap = getLottoResultMap(); + lottoResults.forEach(lottoResult -> lottoResultMap.computeIfPresent(lottoResult, (key, value) -> value + 1)); + return new WinningDetail(lottoResultMap); } public String getResult() { @@ -43,7 +27,10 @@ public String getResult() { return detail.toString(); } - public int getTotalPrize() { - return totalPrize; + private static Map getLottoResultMap() { + Map lottoResultCounts = new EnumMap<>(LottoResult.class); + Arrays.stream(LottoResult.values()) + .forEach(lottoResult -> lottoResultCounts.put(lottoResult, 0)); + return lottoResultCounts; } } From 0f698e169043ed1ea15df55bb4d752f76919fa20 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:20:01 +0900 Subject: [PATCH 43/51] =?UTF-8?q?refactor:=20WinningDetail=20-=20=EC=B4=9D?= =?UTF-8?q?=20=EC=83=81=EA=B8=88=20=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/domain/result/WinningDetail.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/lotto/domain/result/WinningDetail.java b/src/main/java/lotto/domain/result/WinningDetail.java index 87e43f2e89..5827bacf1f 100644 --- a/src/main/java/lotto/domain/result/WinningDetail.java +++ b/src/main/java/lotto/domain/result/WinningDetail.java @@ -17,14 +17,11 @@ public static WinningDetail from(List lottoResults) { return new WinningDetail(lottoResultMap); } - public String getResult() { - StringBuilder detail = new StringBuilder(); - detail.append(String.format("%s (%s원) - %d개\n", LottoResult.FIFTH_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.FIFTH_PRIZE.getPrize()), fifthPrize)) - .append(String.format("%s (%s원) - %d개\n", LottoResult.FOURTH_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.FOURTH_PRIZE.getPrize()), fourthPrize)) - .append(String.format("%s (%s원) - %d개\n", LottoResult.THIRD_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.THIRD_PRIZE.getPrize()), thirdPrize)) - .append(String.format("%s (%s원) - %d개\n", LottoResult.SECOND_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.SECOND_PRIZE.getPrize()), secondPrize)) - .append(String.format("%s (%s원) - %d개\n", LottoResult.FIRST_PRIZE.getMessage(), DECIMAL_FORMAT.format(LottoResult.FIRST_PRIZE.getPrize()), firstPrize)); - return detail.toString(); + public int getTotalPrize() { + return lottoResults.entrySet() + .stream() + .mapToInt(lottoResult -> lottoResult.getKey().getPrize() * lottoResult.getValue()) + .sum(); } private static Map getLottoResultMap() { From 35135e53ed73e64906c2cac7c6ef5fc018609cf5 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:21:04 +0900 Subject: [PATCH 44/51] =?UTF-8?q?refactor:=20WinningDetail=20-=20toString?= =?UTF-8?q?=20=EB=A1=9C=EC=A7=81=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/lotto/domain/result/WinningDetail.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/lotto/domain/result/WinningDetail.java b/src/main/java/lotto/domain/result/WinningDetail.java index 5827bacf1f..728c399c9c 100644 --- a/src/main/java/lotto/domain/result/WinningDetail.java +++ b/src/main/java/lotto/domain/result/WinningDetail.java @@ -24,6 +24,16 @@ public int getTotalPrize() { .sum(); } + @Override + public String toString() { + return lottoResults.entrySet() + .stream() + .filter(lottoResult -> lottoResult.getKey() != LottoResult.NO_PRIZE) + .sorted(Comparator.comparing(lottoResult -> lottoResult.getKey().getPrize())) + .map(lottoResult -> String.format("%s - %d개", lottoResult.getKey(), lottoResult.getValue())) + .collect(Collectors.joining("\n")); + } + private static Map getLottoResultMap() { Map lottoResultCounts = new EnumMap<>(LottoResult.class); Arrays.stream(LottoResult.values()) From 083ef2c75e5fdeb909eda5dba79e02782f811cb7 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:21:52 +0900 Subject: [PATCH 45/51] =?UTF-8?q?refactor:=20LottoResult=20-=20message=20?= =?UTF-8?q?=ED=95=84=EB=93=9C=20=EC=82=AD=EC=A0=9C=20=EB=B0=8F=20toString?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lotto/domain/result/LottoResult.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/lotto/domain/result/LottoResult.java b/src/main/java/lotto/domain/result/LottoResult.java index db93f74e72..e6afe432b1 100644 --- a/src/main/java/lotto/domain/result/LottoResult.java +++ b/src/main/java/lotto/domain/result/LottoResult.java @@ -4,23 +4,21 @@ public enum LottoResult { - FIRST_PRIZE(6, false, 2_000_000_000, "6개 일치"), - SECOND_PRIZE(5, true, 30_000_000, "5개 일치, 보너스 볼 일치"), - THIRD_PRIZE(5, false, 1_500_000, "5개 일치"), - FOURTH_PRIZE(4, false, 50_000, "4개 일치"), - FIFTH_PRIZE(3, false, 5_000, "3개 일치"), - NO_PRIZE(0, false, 0, ""); + FIRST_PRIZE(6, false, 2_000_000_000), + SECOND_PRIZE(5, true, 30_000_000), + THIRD_PRIZE(5, false, 1_500_000), + FOURTH_PRIZE(4, false, 50_000), + FIFTH_PRIZE(3, false, 5_000), + NO_PRIZE(0, false, 0); private final int matchingNumber; private final boolean hasBonusNumber; private final int prize; - private final String message; - LottoResult(int matchingNumber, boolean hasBonusNumber, int prize, String message) { + LottoResult(int matchingNumber, boolean hasBonusNumber, int prize) { this.matchingNumber = matchingNumber; this.hasBonusNumber = hasBonusNumber; this.prize = prize; - this.message = message; } public static LottoResult find(int matchingNumber, boolean hasBonusNumber) { @@ -35,7 +33,11 @@ public int getPrize() { return prize; } - public String getMessage() { - return message; + @Override + public String toString() { + if (hasBonusNumber) { + return String.format("%d개 일치, 보너스 볼 일치 (%,d원)", matchingNumber, prize); + } + return String.format("%d개 일치 (%,d원)", matchingNumber, prize); } } From 1a689aaf7b6e08df419901d0c9bf34bf0c00fb84 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:22:18 +0900 Subject: [PATCH 46/51] =?UTF-8?q?refactor:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EB=8B=B9=EC=B2=A8=20=EA=B2=B0=EA=B3=BC=20=EB=A1=9C=EC=A7=81=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/lotto/service/LottoManager.java | 2 +- src/main/java/lotto/util/Output.java | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java index c23c54c015..60efada2f3 100644 --- a/src/main/java/lotto/service/LottoManager.java +++ b/src/main/java/lotto/service/LottoManager.java @@ -34,6 +34,6 @@ private Lottos purchaseLotto(PurchasingMoney purchasingMoney) { private void compareLotto(PurchasingMoney purchasingMoney, Lottos issuedLotto, Lotto winningLotto, BonusNumber bonusNumber) { WinningDetail winningDetail = WinningDetail.from(issuedLotto.compare(winningLotto, bonusNumber.getNumber())); ProfitRate profitRate = ProfitRate.from(winningDetail.getTotalPrize(), purchasingMoney.getMoney()); - Output.printWinningDetail(winningDetail.getResult(), profitRate.getValue()); + Output.printWinningDetail(winningDetail, profitRate); } } diff --git a/src/main/java/lotto/util/Output.java b/src/main/java/lotto/util/Output.java index 5d018358d1..2eb527ab17 100644 --- a/src/main/java/lotto/util/Output.java +++ b/src/main/java/lotto/util/Output.java @@ -2,6 +2,8 @@ import lotto.domain.Lotto; import lotto.domain.Lottos; +import lotto.domain.result.ProfitRate; +import lotto.domain.result.WinningDetail; public class Output { @@ -17,7 +19,9 @@ public static void printIssuedLotto(Lottos issuedLotto) { .forEach(System.out::println); } - public static void printWinningDetail(String winningDetail, double profitRate) { - System.out.printf("\n당첨 통계\n---\n%s\n총 수익률은 %.1f%%입니다.\n", winningDetail, profitRate); + public static void printWinningDetail(WinningDetail winningDetail, ProfitRate profitRate) { + System.out.println("\n당첨 통계\n---\n" + + winningDetail + + String.format("\n총 수익률은 %s%%입니다.\n", profitRate)); } } From b03ea09f63a85410d68bcc1ba3ed479dc5db1805 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:26:05 +0900 Subject: [PATCH 47/51] =?UTF-8?q?refactor:=20ProfitRate=20-=20toString=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/lotto/domain/result/ProfitRate.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/domain/result/ProfitRate.java b/src/main/java/lotto/domain/result/ProfitRate.java index 2840b9ff1f..c0cfbc158a 100644 --- a/src/main/java/lotto/domain/result/ProfitRate.java +++ b/src/main/java/lotto/domain/result/ProfitRate.java @@ -12,7 +12,8 @@ public static ProfitRate from(int prize, int purchasingMoney) { return new ProfitRate(((double) prize / purchasingMoney) * 100); } - public double getValue() { - return value; + @Override + public String toString() { + return String.format("%.1f", value); } } From 7c59d24250c8a6f38497a50f79614fae220607db Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:26:46 +0900 Subject: [PATCH 48/51] =?UTF-8?q?test:=20ProfitRateTest=20-=20=EC=86=8C?= =?UTF-8?q?=EC=88=AB=EC=A0=90=20=EB=91=98=EC=A7=B8=20=EC=9E=90=EB=A6=AC=20?= =?UTF-8?q?=EB=B0=98=EC=98=AC=EB=A6=BC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=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/test/java/lotto/domain/result/ProfitRateTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/lotto/domain/result/ProfitRateTest.java b/src/test/java/lotto/domain/result/ProfitRateTest.java index 1b025ddfa9..6dca68c72b 100644 --- a/src/test/java/lotto/domain/result/ProfitRateTest.java +++ b/src/test/java/lotto/domain/result/ProfitRateTest.java @@ -14,6 +14,6 @@ public class ProfitRateTest { // when ProfitRate profitRate = ProfitRate.from(prize, purchasingMoney); // then - assertThat(String.format("%.1f", profitRate.getValue())).isEqualTo("785.7"); + assertThat(profitRate).isEqualTo("785.7"); } } From d43590fa4cf77384506b076216220d0a2a6029c9 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:37:09 +0900 Subject: [PATCH 49/51] =?UTF-8?q?refactor:=20Lottos=20-=20PurchasingMoney?= =?UTF-8?q?=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/Lottos.java | 4 ++-- src/main/java/lotto/service/LottoManager.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/domain/Lottos.java b/src/main/java/lotto/domain/Lottos.java index 3a87987818..6debf0d5b8 100644 --- a/src/main/java/lotto/domain/Lottos.java +++ b/src/main/java/lotto/domain/Lottos.java @@ -16,9 +16,9 @@ public Lottos(List lottos) { this.lottos = new ArrayList<>(lottos); } - public static Lottos from(LottoGenerator lottoGenerator, PurchasingMoney purchasingMoney) { + public static Lottos from(LottoGenerator lottoGenerator, int quantity) { return new Lottos(Stream.generate(() -> Lotto.from(lottoGenerator)) - .limit(purchasingMoney.getQuantity()) + .limit(quantity) .collect(Collectors.toList())); } diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java index 60efada2f3..4fbd85330a 100644 --- a/src/main/java/lotto/service/LottoManager.java +++ b/src/main/java/lotto/service/LottoManager.java @@ -26,7 +26,7 @@ public void run() { } private Lottos purchaseLotto(PurchasingMoney purchasingMoney) { - Lottos issuedLotto = Lottos.from(IssuedLottoGenerator.create(), purchasingMoney); + Lottos issuedLotto = Lottos.from(IssuedLottoGenerator.create(), purchasingMoney.getQuantity()); Output.printIssuedLotto(issuedLotto); return issuedLotto; } From cb771f56c17f211c8cb6b83cc0d836c8d2d406a1 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:37:46 +0900 Subject: [PATCH 50/51] =?UTF-8?q?refactor:=20Lottos=20-=20=EC=88=98?= =?UTF-8?q?=EB=9F=89=20=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/domain/Lottos.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/lotto/domain/Lottos.java b/src/main/java/lotto/domain/Lottos.java index 6debf0d5b8..2402962d8b 100644 --- a/src/main/java/lotto/domain/Lottos.java +++ b/src/main/java/lotto/domain/Lottos.java @@ -31,8 +31,4 @@ public List compare(Lotto winningLotto, int bonusNumber) { public List getLottos() { return lottos; } - - public int getQuantity() { - return lottos.size(); - } } From 8cca416620b381f06557bc44fc571fdc7d748942 Mon Sep 17 00:00:00 2001 From: jeonyuneo Date: Sun, 2 Apr 2023 03:38:21 +0900 Subject: [PATCH 51/51] =?UTF-8?q?refactor:=20=EA=B5=AC=EB=A7=A4=20?= =?UTF-8?q?=EB=A1=9C=EB=98=90=20=EC=B6=9C=EB=A0=A5=20=EB=A1=9C=EC=A7=81=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/lotto/service/LottoManager.java | 2 +- src/main/java/lotto/util/Output.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/service/LottoManager.java b/src/main/java/lotto/service/LottoManager.java index 4fbd85330a..4ea294e55b 100644 --- a/src/main/java/lotto/service/LottoManager.java +++ b/src/main/java/lotto/service/LottoManager.java @@ -27,7 +27,7 @@ public void run() { private Lottos purchaseLotto(PurchasingMoney purchasingMoney) { Lottos issuedLotto = Lottos.from(IssuedLottoGenerator.create(), purchasingMoney.getQuantity()); - Output.printIssuedLotto(issuedLotto); + Output.printIssuedLotto(issuedLotto, purchasingMoney.getQuantity()); return issuedLotto; } diff --git a/src/main/java/lotto/util/Output.java b/src/main/java/lotto/util/Output.java index 2eb527ab17..c5fb709c5c 100644 --- a/src/main/java/lotto/util/Output.java +++ b/src/main/java/lotto/util/Output.java @@ -10,8 +10,8 @@ public class Output { private Output() { } - public static void printIssuedLotto(Lottos issuedLotto) { - System.out.printf("\n%d개를 구매했습니다.%n", issuedLotto.getQuantity()); + public static void printIssuedLotto(Lottos issuedLotto, int quantity) { + System.out.printf("\n%d개를 구매했습니다.%n", quantity); issuedLotto.getLottos() .stream() .map(Lotto::getNumbers)