From 9f52f00fb58e62854cb22f4988178f688d06ab2f Mon Sep 17 00:00:00 2001 From: yohanii Date: Mon, 30 Sep 2024 19:29:50 +0900 Subject: [PATCH 01/25] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29bb2..7d4d5ce933 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,16 @@ +## 기능 목록 + +- [ ] 로또 Lotto + - [x] 여섯 개인지 검증 - validate() + - [ ] 정답과 비교 후 등수 반환 - calculateRank() +- [ ] 로또 판매기 Seller + - [ ] 로또 발행 - getLottoNums() + - [ ] 개수만큼 로또 발행 - getLottosNums() + - [ ] 로또 당첨 통계 반환 - getResult() + - [ ] 수익률 계산 - calculateRate() +- [ ] UI + - [ ] 구입금액 입력받기 - receivePurchaseMoney() + - [ ] 구매한 로또 출력 - printBoughtLottos() + - [ ] 당첨 번호 입력받기 - receiveAnswers() + - [ ] 보너스 번호 입력받기 - receiveBonus() + - [ ] 당첨 통계 출력 - printResult() \ No newline at end of file From 7576de1f4558a96d4ec35b3984d87541c095a03e Mon Sep 17 00:00:00 2001 From: yohanii Date: Mon, 30 Sep 2024 19:38:32 +0900 Subject: [PATCH 02/25] =?UTF-8?q?test:=20LottoTest=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 5 +- src/test/java/lotto/LottoTest.java | 93 +++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 519793d1f7..35062542fd 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -16,5 +16,8 @@ private void validate(List numbers) { } } - // TODO: 추가 기능 구현 + public int calculateRank(List answers, int bonus) { + return 0; + } + } diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 0f3af0f6c4..23f83bb657 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,10 +1,12 @@ package lotto; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; class LottoTest { @@ -18,10 +20,97 @@ void createLottoByOverSize() { @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") @Test void createLottoByDuplicatedNumber() { - // TODO: 이 테스트가 통과할 수 있게 구현 코드 작성 assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) .isInstanceOf(IllegalArgumentException.class); } - // 아래에 추가 테스트 작성 가능 + @DisplayName("6개 일치하면, 1을 반환한다.") + @Test + void calculateRank_1st() { + //given + Lotto lotto = new Lotto(List.of(1, 2, 3, 4, 5, 6)); + List answers = List.of(1, 2, 3, 4, 5, 6); + int bonus = 7; + + //when + int rank = lotto.calculateRank(answers, bonus); + + //then + assertThat(rank).isEqualTo(1); + } + + @DisplayName("5개 일치하고, 보너스 볼 일치하면, 2을 반환한다.") + @Test + void calculateRank_2nd() { + //given + Lotto lotto = new Lotto(List.of(1, 2, 3, 4, 5, 7)); + List answers = List.of(1, 2, 3, 4, 5, 6); + int bonus = 7; + + //when + int rank = lotto.calculateRank(answers, bonus); + + //then + assertThat(rank).isEqualTo(2); + } + + @DisplayName("5개 일치하면, 3을 반환한다.") + @Test + void calculateRank_3rd() { + //given + Lotto lotto = new Lotto(List.of(1, 2, 3, 4, 5, 8)); + List answers = List.of(1, 2, 3, 4, 5, 6); + int bonus = 7; + + //when + int rank = lotto.calculateRank(answers, bonus); + + //then + assertThat(rank).isEqualTo(3); + } + + @DisplayName("4개 일치하면, 4을 반환한다.") + @Test + void calculateRank_4th() { + //given + Lotto lotto = new Lotto(List.of(1, 2, 3, 4, 7, 8)); + List answers = List.of(1, 2, 3, 4, 5, 6); + int bonus = 7; + + //when + int rank = lotto.calculateRank(answers, bonus); + + //then + assertThat(rank).isEqualTo(4); + } + + @DisplayName("3개 일치하면, 5을 반환한다.") + @Test + void calculateRank_5th() { + //given + Lotto lotto = new Lotto(List.of(1, 2, 3, 7, 8, 9)); + List answers = List.of(1, 2, 3, 4, 5, 6); + int bonus = 7; + + //when + int rank = lotto.calculateRank(answers, bonus); + + //then + assertThat(rank).isEqualTo(5); + } + + @DisplayName("2개 이하 일치하면, 6을 반환한다.") + @Test + void calculateRank_6th() { + //given + Lotto lotto = new Lotto(List.of(1, 2, 7, 8, 9, 10)); + List answers = List.of(1, 2, 3, 4, 5, 6); + int bonus = 7; + + //when + int rank = lotto.calculateRank(answers, bonus); + + //then + assertThat(rank).isEqualTo(6); + } } From 008b6c1f5eccc0d3ad6d38dcd2564ae419b165b1 Mon Sep 17 00:00:00 2001 From: yohanii Date: Mon, 30 Sep 2024 19:40:21 +0900 Subject: [PATCH 03/25] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 7d4d5ce933..8868019053 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,7 +1,8 @@ ## 기능 목록 - [ ] 로또 Lotto - - [x] 여섯 개인지 검증 - validate() + - [x] 여섯 개인지 검증 - validateCounts() + - [ ] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [ ] 정답과 비교 후 등수 반환 - calculateRank() - [ ] 로또 판매기 Seller - [ ] 로또 발행 - getLottoNums() From 52c519f2519bbd61aab9fa396c634af41f9d6144 Mon Sep 17 00:00:00 2001 From: yohanii Date: Mon, 30 Sep 2024 19:45:17 +0900 Subject: [PATCH 04/25] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=EC=97=90=20=EC=A4=91=EB=B3=B5=EB=90=9C=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=EA=B0=80=20=EC=9E=88=EB=8A=94=EC=A7=80=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20-=20validateDuplication()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/lotto/Lotto.java | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8868019053..2d28f96fbf 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ - [ ] 로또 Lotto - [x] 여섯 개인지 검증 - validateCounts() - - [ ] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() + - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [ ] 정답과 비교 후 등수 반환 - calculateRank() - [ ] 로또 판매기 Seller - [ ] 로또 발행 - getLottoNums() diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 35062542fd..46df6e1519 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,5 +1,6 @@ package lotto; +import java.util.HashSet; import java.util.List; public class Lotto { @@ -11,8 +12,19 @@ public Lotto(List numbers) { } private void validate(List numbers) { + validateCounts(numbers); + validateDuplication(numbers); + } + + private void validateCounts(List numbers) { if (numbers.size() != 6) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("로또 번호의 개수는 6개여야 합니다."); + } + } + + private void validateDuplication(List numbers) { + if (new HashSet<>(numbers).size() != numbers.size()) { + throw new IllegalArgumentException("로또 번호는 중복되어선 안됩니다."); } } From 4638e9f19082d03de67124da273e7b5ab4d50058 Mon Sep 17 00:00:00 2001 From: yohanii Date: Mon, 30 Sep 2024 20:01:29 +0900 Subject: [PATCH 05/25] =?UTF-8?q?feat:=20=EC=A0=95=EB=8B=B5=EA=B3=BC=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=20=ED=9B=84=20=EB=93=B1=EC=88=98=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20-=20calculateRank()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- src/main/java/lotto/Lotto.java | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2d28f96fbf..bae4ea8a36 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,9 +1,9 @@ ## 기능 목록 -- [ ] 로또 Lotto +- [x] 로또 Lotto - [x] 여섯 개인지 검증 - validateCounts() - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - - [ ] 정답과 비교 후 등수 반환 - calculateRank() + - [x] 정답과 비교 후 등수 반환 - calculateRank() - [ ] 로또 판매기 Seller - [ ] 로또 발행 - getLottoNums() - [ ] 개수만큼 로또 발행 - getLottosNums() diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 46df6e1519..f5d1f31ed9 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,7 +1,9 @@ package lotto; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Set; public class Lotto { private final List numbers; @@ -29,7 +31,28 @@ private void validateDuplication(List numbers) { } public int calculateRank(List answers, int bonus) { - return 0; + + Set set = new HashSet<>(numbers); + + int correct = (int) answers.stream() + .filter(set::contains) + .count(); + + int bonusCorrect = set.contains(bonus) && correct == 5 ? 1 : 0; + + return rankByCondition(correct, bonusCorrect); + } + + private int rankByCondition(int correct, int bonusCorrect) { + if (correct == 6) { + return 1; + } + + if (correct + bonusCorrect <= 2) { + return 6; + } + + return 8 - (correct + bonusCorrect); } } From 142589662a0b93a9768f4bf9096c64723bf87f76 Mon Sep 17 00:00:00 2001 From: yohanii Date: Mon, 30 Sep 2024 20:03:58 +0900 Subject: [PATCH 06/25] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index bae4ea8a36..dd926c7dd1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,8 +5,8 @@ - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [x] 정답과 비교 후 등수 반환 - calculateRank() - [ ] 로또 판매기 Seller - - [ ] 로또 발행 - getLottoNums() - - [ ] 개수만큼 로또 발행 - getLottosNums() + - [ ] 로또 발행 - getLotto() + - [ ] 개수만큼 로또 발행 - getLottos() - [ ] 로또 당첨 통계 반환 - getResult() - [ ] 수익률 계산 - calculateRate() - [ ] UI From 1a0f5bd2f79691db0b9036d6ba95c125e039d7c2 Mon Sep 17 00:00:00 2001 From: yohanii Date: Mon, 30 Sep 2024 20:13:54 +0900 Subject: [PATCH 07/25] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/README.md b/docs/README.md index dd926c7dd1..42db6114b6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,6 +5,7 @@ - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [x] 정답과 비교 후 등수 반환 - calculateRank() - [ ] 로또 판매기 Seller + - [ ] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [ ] 로또 발행 - getLotto() - [ ] 개수만큼 로또 발행 - getLottos() - [ ] 로또 당첨 통계 반환 - getResult() From 83c1bdd38fdc26d1d4a575cefc42282472d46b9d Mon Sep 17 00:00:00 2001 From: yohanii Date: Mon, 30 Sep 2024 21:49:34 +0900 Subject: [PATCH 08/25] feat: add Seller skeleton --- src/main/java/lotto/Seller.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/lotto/Seller.java diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java new file mode 100644 index 0000000000..dbd456798f --- /dev/null +++ b/src/main/java/lotto/Seller.java @@ -0,0 +1,32 @@ +package lotto; + +import java.util.List; + +public class Seller { + + private final int lottoPrice; + + public Seller(int lottoPrice) { + this.lottoPrice = lottoPrice; + } + + public int calculateLottoCount(int money) { + return 0; + } + + public Lotto getLotto() { + return null; + } + + public List getLottos(int count) { + return null; + } + + public List getResult(List lottos) { + return null; + } + + public double calculateRate(List ranks) { + return 0D; + } +} From 8355f3bd10c48a63662d338aa5e7318ee290e201 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 00:03:32 +0900 Subject: [PATCH 09/25] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 42db6114b6..c8213b541b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,9 +4,9 @@ - [x] 여섯 개인지 검증 - validateCounts() - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [x] 정답과 비교 후 등수 반환 - calculateRank() + - [ ] 랜덤한 값 생성자 - Lotto() - [ ] 로또 판매기 Seller - [ ] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - - [ ] 로또 발행 - getLotto() - [ ] 개수만큼 로또 발행 - getLottos() - [ ] 로또 당첨 통계 반환 - getResult() - [ ] 수익률 계산 - calculateRate() From 57d7b7d24b21c23fe09052eb5b79120dc00d05bd Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 00:10:31 +0900 Subject: [PATCH 10/25] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index c8213b541b..42db6114b6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,9 +4,9 @@ - [x] 여섯 개인지 검증 - validateCounts() - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [x] 정답과 비교 후 등수 반환 - calculateRank() - - [ ] 랜덤한 값 생성자 - Lotto() - [ ] 로또 판매기 Seller - [ ] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() + - [ ] 로또 발행 - getLotto() - [ ] 개수만큼 로또 발행 - getLottos() - [ ] 로또 당첨 통계 반환 - getResult() - [ ] 수익률 계산 - calculateRate() From ac4142d81aed0fb9c5fead366ea699a1ebf0e0b6 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 00:29:43 +0900 Subject: [PATCH 11/25] =?UTF-8?q?test:=20SellerTest=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/lotto/SellerTest.java | 121 ++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/test/java/lotto/SellerTest.java diff --git a/src/test/java/lotto/SellerTest.java b/src/test/java/lotto/SellerTest.java new file mode 100644 index 0000000000..bc029d4538 --- /dev/null +++ b/src/test/java/lotto/SellerTest.java @@ -0,0 +1,121 @@ +package lotto; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.platform.commons.util.CollectionUtils; + +import java.util.List; + +import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class SellerTest { + + @DisplayName("받은 금액을 lottoPrice로 나눈 몫을 반환한다.") + @Test + void calculateLottoCount() { + //given + final int lottoPrice = 1_000; + Seller seller = new Seller(lottoPrice); + int money = 10_000; + + //when + int count = seller.calculateLottoCount(money); + + //then + assertThat(count).isEqualTo(10); + } + + @DisplayName("random한 Lotto 한 개 반환") + @Test + void getLotto() { + //given + List answers = List.of(8, 21, 23, 41, 42, 43); + final int lottoPrice = 1_000; + Seller seller = new Seller(lottoPrice); + + assertRandomUniqueNumbersInRangeTest( + () -> { + //when + Lotto lotto = seller.getLotto(); + + //then + int rank = lotto.calculateRank(answers, -1); + assertThat(rank).isEqualTo(1); + }, + answers + ); + } + + @DisplayName("개수만큼의 로또를 반환한다.") + @Test + void getLottos() { + //given + int count = 10; + final int lottoPrice = 1_000; + Seller seller = new Seller(lottoPrice); + + //when + List lottos = seller.getLottos(10); + + //then + assertThat(lottos.size()).isEqualTo(count); + } + + @DisplayName("반환된 길이는 6이다.") + @Test + void getResult_length() { + //given + final int lottoPrice = 1_000; + Seller seller = new Seller(lottoPrice); + + //when + List result = seller.getResult(List.of()); + + //then + assertThat(result.size()).isEqualTo(6); + } + + @DisplayName("1~6등의 개수를 반환한다.") + @Test + void getResult() { + //given + List answers = List.of(8, 21, 23, 41, 42, 43); + List second = List.of(8, 21, 23, 41, 42, 44); + final int lottoPrice = 1_000; + Seller seller = new Seller(lottoPrice); + List lottos = seller.getLottos(2); + + assertRandomUniqueNumbersInRangeTest( + () -> { + //when + List result = seller.getResult(lottos); + + //then + assertThat(result.equals(List.of(1, 1, 0, 0, 0, 0))).isTrue(); + }, + answers, + second + ); + } + + @DisplayName("수익률을 계산해 반환한다.") + @Test + void calculateRate() { + //given + List result = List.of(1, 1, 0, 0, 0, 0); + int count = 2; + int expectedRate = 2_000_000_000 + 30_000_000 / 2_000; + + final int lottoPrice = 1_000; + Seller seller = new Seller(lottoPrice); + + //when + double rate = seller.calculateRate(result, count); + + //then + assertThat(rate).isEqualTo(expectedRate); + } +} \ No newline at end of file From d30908f0adfb1cd922e7339cb9db345af14b0f9d Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 00:30:20 +0900 Subject: [PATCH 12/25] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=B0=9C?= =?UTF-8?q?=ED=96=89=20-=20getLotto()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/lotto/Seller.java | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 42db6114b6..8ba9fb1d8b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,7 @@ - [x] 정답과 비교 후 등수 반환 - calculateRank() - [ ] 로또 판매기 Seller - [ ] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - - [ ] 로또 발행 - getLotto() + - [x] 로또 발행 - getLotto() - [ ] 개수만큼 로또 발행 - getLottos() - [ ] 로또 당첨 통계 반환 - getResult() - [ ] 수익률 계산 - calculateRate() diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index dbd456798f..8b44877f1c 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -1,5 +1,7 @@ package lotto; +import camp.nextstep.edu.missionutils.Randoms; + import java.util.List; public class Seller { @@ -15,7 +17,7 @@ public int calculateLottoCount(int money) { } public Lotto getLotto() { - return null; + return new Lotto(Randoms.pickUniqueNumbersInRange(1, 45, 6)); } public List getLottos(int count) { @@ -26,7 +28,7 @@ public List getResult(List lottos) { return null; } - public double calculateRate(List ranks) { + public double calculateRate(List ranks, int count) { return 0D; } } From a57eeae2ce8a269f735358788263e256974f8c3a Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 00:32:34 +0900 Subject: [PATCH 13/25] =?UTF-8?q?feat:=20=EA=B0=9C=EC=88=98=EB=A7=8C?= =?UTF-8?q?=ED=81=BC=20=EB=A1=9C=EB=98=90=20=EB=B0=9C=ED=96=89=20-=20getLo?= =?UTF-8?q?ttos()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/lotto/Seller.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8ba9fb1d8b..159761457a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,7 +7,7 @@ - [ ] 로또 판매기 Seller - [ ] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [x] 로또 발행 - getLotto() - - [ ] 개수만큼 로또 발행 - getLottos() + - [x] 개수만큼 로또 발행 - getLottos() - [ ] 로또 당첨 통계 반환 - getResult() - [ ] 수익률 계산 - calculateRate() - [ ] UI diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index 8b44877f1c..9e6159bae5 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -3,6 +3,8 @@ import camp.nextstep.edu.missionutils.Randoms; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class Seller { @@ -21,7 +23,9 @@ public Lotto getLotto() { } public List getLottos(int count) { - return null; + return IntStream.range(0, count) + .mapToObj(i -> getLotto()) + .collect(Collectors.toList()); } public List getResult(List lottos) { From c21af957c87d27ab91a1582bed556bad592cca0a Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 00:33:32 +0900 Subject: [PATCH 14/25] =?UTF-8?q?feat:=20=EA=B8=88=EC=95=A1=EC=97=90=20?= =?UTF-8?q?=EB=B0=9B=EC=95=84=20=EA=B5=AC=EB=A7=A4=20=EA=B0=80=EB=8A=A5?= =?UTF-8?q?=ED=95=9C=20=EB=A1=9C=EB=98=90=20=EA=B0=9C=EC=88=98=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20-=20calculateLottoCount()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/lotto/Seller.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 159761457a..8ff1f1c0ee 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,7 +5,7 @@ - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [x] 정답과 비교 후 등수 반환 - calculateRank() - [ ] 로또 판매기 Seller - - [ ] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() + - [x] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [x] 로또 발행 - getLotto() - [x] 개수만큼 로또 발행 - getLottos() - [ ] 로또 당첨 통계 반환 - getResult() diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index 9e6159bae5..1e460e66af 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -15,7 +15,7 @@ public Seller(int lottoPrice) { } public int calculateLottoCount(int money) { - return 0; + return money / lottoPrice; } public Lotto getLotto() { From 7a8900b06b3eec6a81d4cdb3fdd33298e298ad05 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 00:43:39 +0900 Subject: [PATCH 15/25] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=ED=86=B5=EA=B3=84=20=EB=B0=98=ED=99=98=20-=20getRe?= =?UTF-8?q?sult()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/lotto/Seller.java | 11 +++++++++-- src/test/java/lotto/SellerTest.java | 7 ++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8ff1f1c0ee..36775485c8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,7 +8,7 @@ - [x] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [x] 로또 발행 - getLotto() - [x] 개수만큼 로또 발행 - getLottos() - - [ ] 로또 당첨 통계 반환 - getResult() + - [x] 로또 당첨 통계 반환 - getResult() - [ ] 수익률 계산 - calculateRate() - [ ] UI - [ ] 구입금액 입력받기 - receivePurchaseMoney() diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index 1e460e66af..b02b27f30a 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -2,6 +2,7 @@ import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -28,8 +29,14 @@ public List getLottos(int count) { .collect(Collectors.toList()); } - public List getResult(List lottos) { - return null; + public List getResult(List lottos, List answers, int bonus) { + List result = new ArrayList<>(List.of(0, 0, 0, 0, 0, 0)); + + for (Lotto lotto: lottos) { + int index = lotto.calculateRank(answers, bonus) - 1; + result.set(index, result.get(index) + 1); + } + return result; } public double calculateRate(List ranks, int count) { diff --git a/src/test/java/lotto/SellerTest.java b/src/test/java/lotto/SellerTest.java index bc029d4538..64fda2a198 100644 --- a/src/test/java/lotto/SellerTest.java +++ b/src/test/java/lotto/SellerTest.java @@ -72,7 +72,7 @@ void getResult_length() { Seller seller = new Seller(lottoPrice); //when - List result = seller.getResult(List.of()); + List result = seller.getResult(List.of(), List.of(), -1); //then assertThat(result.size()).isEqualTo(6); @@ -86,12 +86,13 @@ void getResult() { List second = List.of(8, 21, 23, 41, 42, 44); final int lottoPrice = 1_000; Seller seller = new Seller(lottoPrice); - List lottos = seller.getLottos(2); assertRandomUniqueNumbersInRangeTest( () -> { + List lottos = seller.getLottos(2); + //when - List result = seller.getResult(lottos); + List result = seller.getResult(lottos, answers, 44); //then assertThat(result.equals(List.of(1, 1, 0, 0, 0, 0))).isTrue(); From f620885d4bd347fbc75e6eb2d28b05453aa1f89c Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 00:50:51 +0900 Subject: [PATCH 16/25] =?UTF-8?q?feat:=20=EC=88=98=EC=9D=B5=EB=A5=A0=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=20-=20calculateRate()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- src/main/java/lotto/Seller.java | 9 ++++++++- src/test/java/lotto/SellerTest.java | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/README.md b/docs/README.md index 36775485c8..965dec0338 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,12 +4,12 @@ - [x] 여섯 개인지 검증 - validateCounts() - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [x] 정답과 비교 후 등수 반환 - calculateRank() -- [ ] 로또 판매기 Seller +- [x] 로또 판매기 Seller - [x] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [x] 로또 발행 - getLotto() - [x] 개수만큼 로또 발행 - getLottos() - [x] 로또 당첨 통계 반환 - getResult() - - [ ] 수익률 계산 - calculateRate() + - [x] 수익률 계산 - calculateRate() - [ ] UI - [ ] 구입금액 입력받기 - receivePurchaseMoney() - [ ] 구매한 로또 출력 - printBoughtLottos() diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index b02b27f30a..cd478c79df 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -10,6 +10,7 @@ public class Seller { private final int lottoPrice; + private static final List prize = List.of(2_000_000_000, 30_000_000, 1_500_000, 50_000, 5_000, 0); public Seller(int lottoPrice) { this.lottoPrice = lottoPrice; @@ -40,6 +41,12 @@ public List getResult(List lottos, List answers, int bo } public double calculateRate(List ranks, int count) { - return 0D; + + long prizeSum = IntStream.range(0, 6) + .mapToLong(index -> (long) ranks.get(index) * prize.get(index)) + .sum(); + int cost = lottoPrice * count; + + return prizeSum / (double) cost; } } diff --git a/src/test/java/lotto/SellerTest.java b/src/test/java/lotto/SellerTest.java index 64fda2a198..106d63ff5b 100644 --- a/src/test/java/lotto/SellerTest.java +++ b/src/test/java/lotto/SellerTest.java @@ -108,7 +108,7 @@ void calculateRate() { //given List result = List.of(1, 1, 0, 0, 0, 0); int count = 2; - int expectedRate = 2_000_000_000 + 30_000_000 / 2_000; + int expectedRate = (2_000_000_000 + 30_000_000) / 2_000; final int lottoPrice = 1_000; Seller seller = new Seller(lottoPrice); From c544e515008c1a85c2446657688e8e34f45a1061 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 09:55:09 +0900 Subject: [PATCH 17/25] =?UTF-8?q?feat:=20UI=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 13 ++++---- src/main/java/lotto/Application.java | 17 +++++++++- src/main/java/lotto/Lotto.java | 4 +++ src/main/java/lotto/UI.java | 48 ++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 src/main/java/lotto/UI.java diff --git a/docs/README.md b/docs/README.md index 965dec0338..ee43a93b53 100644 --- a/docs/README.md +++ b/docs/README.md @@ -10,9 +10,10 @@ - [x] 개수만큼 로또 발행 - getLottos() - [x] 로또 당첨 통계 반환 - getResult() - [x] 수익률 계산 - calculateRate() -- [ ] UI - - [ ] 구입금액 입력받기 - receivePurchaseMoney() - - [ ] 구매한 로또 출력 - printBoughtLottos() - - [ ] 당첨 번호 입력받기 - receiveAnswers() - - [ ] 보너스 번호 입력받기 - receiveBonus() - - [ ] 당첨 통계 출력 - printResult() \ No newline at end of file +- [x] UI + - [x] 구입금액 입력받기 - receivePurchaseMoney() + - [x] 구매한 로또 출력 - printBoughtLottos() + - [x] 당첨 번호 입력받기 - receiveAnswers() + - [x] 보너스 번호 입력받기 - receiveBonus() + - [x] 당첨 통계 출력 - printResult() + - [x] 수익률 출력 - printRate() \ No newline at end of file diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..f19698e7c1 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,22 @@ package lotto; +import java.util.List; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + + int purchaseMoney = UI.receivePurchaseMoney(); + Seller seller = new Seller(1000); + + int purchaseCount = seller.calculateLottoCount(purchaseMoney); + List lottos = seller.getLottos(purchaseCount); + UI.printBoughtLottos(lottos); + + List answers = UI.receiveAnswers(); + int bonus = UI.receiveBonus(); + + List result = seller.getResult(lottos, answers, bonus); + UI.printResult(result); + UI.printRate(seller.calculateRate(result, purchaseCount)); } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index f5d1f31ed9..eaf40986ea 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -55,4 +55,8 @@ private int rankByCondition(int correct, int bonusCorrect) { return 8 - (correct + bonusCorrect); } + @Override + public String toString() { + return numbers.toString(); + } } diff --git a/src/main/java/lotto/UI.java b/src/main/java/lotto/UI.java new file mode 100644 index 0000000000..81462957fb --- /dev/null +++ b/src/main/java/lotto/UI.java @@ -0,0 +1,48 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class UI { + + public static int receivePurchaseMoney() { + System.out.println("구입금액을 입력해 주세요."); + return Integer.parseInt(Console.readLine()); + } + + public static void printBoughtLottos(List lottos) { + System.out.println("\n" + lottos.size() + "개를 구매했습니다."); + for (Lotto lotto : lottos) { + System.out.println(lotto); + } + } + + public static List receiveAnswers() { + System.out.println("\n당첨 번호를 입력해 주세요."); + String[] split = Console.readLine().split(","); + return Arrays.stream(split) + .map(Integer::parseInt) + .collect(Collectors.toList()); + } + + public static int receiveBonus() { + System.out.println("\n보너스 번호를 입력해 주세요."); + return Integer.parseInt(Console.readLine()); + } + + public static void printResult(List result) { + System.out.println("\n당첨 통계"); + System.out.println("---"); + + for (int index = 4; index >= 0; index--) { + System.out.println(" - " + result.get(index) + "개"); + } + } + + public static void printRate(double rate) { + System.out.println("총 수익률은 " + rate + "입니다."); + } +} From c63bddb3197209f07685a3ad7b450b8d5dbee5e9 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 10:31:18 +0900 Subject: [PATCH 18/25] =?UTF-8?q?style:=20style,=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=EB=AC=B8=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/Application.java | 5 ++++- src/main/java/lotto/UI.java | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index f19698e7c1..fd3e6388cc 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -3,10 +3,13 @@ import java.util.List; public class Application { + + public static final int LOTTO_PRICE = 1_000; + public static void main(String[] args) { int purchaseMoney = UI.receivePurchaseMoney(); - Seller seller = new Seller(1000); + Seller seller = new Seller(LOTTO_PRICE); int purchaseCount = seller.calculateLottoCount(purchaseMoney); List lottos = seller.getLottos(purchaseCount); diff --git a/src/main/java/lotto/UI.java b/src/main/java/lotto/UI.java index 81462957fb..ca8b3fa5a5 100644 --- a/src/main/java/lotto/UI.java +++ b/src/main/java/lotto/UI.java @@ -4,10 +4,19 @@ import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class UI { + private static final Map resultOutputByRank = Map.of( + 1, "6개 일치 (2,000,000,000원)", + 2, "5개 일치, 보너스 볼 일치 (30,000,000원)", + 3, "5개 일치 (1,500,000원)", + 4, "4개 일치 (50,000원)", + 5, "3개 일치 (5,000원)" + ); + public static int receivePurchaseMoney() { System.out.println("구입금액을 입력해 주세요."); return Integer.parseInt(Console.readLine()); @@ -38,11 +47,11 @@ public static void printResult(List result) { System.out.println("---"); for (int index = 4; index >= 0; index--) { - System.out.println(" - " + result.get(index) + "개"); + System.out.println(resultOutputByRank.get(index + 1) + " - " + result.get(index) + "개"); } } public static void printRate(double rate) { - System.out.println("총 수익률은 " + rate + "입니다."); + System.out.println("총 수익률은 " + rate + "%입니다."); } } From 1dfbf776c66869c673952c487ece3f33d5cd9f57 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 10:31:26 +0900 Subject: [PATCH 19/25] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/README.md b/docs/README.md index ee43a93b53..7b4821882c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,9 +7,12 @@ - [x] 로또 판매기 Seller - [x] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [x] 로또 발행 - getLotto() + - [ ] 오름차순으로 발행 - [x] 개수만큼 로또 발행 - getLottos() - [x] 로또 당첨 통계 반환 - getResult() - [x] 수익률 계산 - calculateRate() + - [ ] 수익률은 소수점 둘째자리에서 반올림 + - [ ] 3항 연산자 제거 - [x] UI - [x] 구입금액 입력받기 - receivePurchaseMoney() - [x] 구매한 로또 출력 - printBoughtLottos() From 3a4147fde06e1a4b8113028e5ef6d4d57fd61777 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 10:44:44 +0900 Subject: [PATCH 20/25] =?UTF-8?q?test:=20Seller=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=EB=90=9C=20=EC=A1=B0=EA=B1=B4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/lotto/SellerTest.java | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/test/java/lotto/SellerTest.java b/src/test/java/lotto/SellerTest.java index 106d63ff5b..f4b02c0b8e 100644 --- a/src/test/java/lotto/SellerTest.java +++ b/src/test/java/lotto/SellerTest.java @@ -49,6 +49,27 @@ void getLotto() { ); } + @DisplayName("발행된 로또는 오름차순이어야한다.") + @Test + void getLotto_Ascending() { + //given + List answers = List.of(8, 21, 23, 41, 42, 43); + List unsortedAnswers = List.of(42, 21, 43, 41, 8, 23); + final int lottoPrice = 1_000; + Seller seller = new Seller(lottoPrice); + + assertRandomUniqueNumbersInRangeTest( + () -> { + //when + Lotto lotto = seller.getLotto(); + + //then + assertThat(lotto.toString()).isEqualTo(answers.toString()); + }, + unsortedAnswers + ); + } + @DisplayName("개수만큼의 로또를 반환한다.") @Test void getLottos() { @@ -119,4 +140,24 @@ void calculateRate() { //then assertThat(rate).isEqualTo(expectedRate); } + + @DisplayName("수익률은 소수점 둘째자리에서 반올림") + @Test + void calculateRate_decimal() { + //given + List result = List.of(1, 1, 0, 0, 0, 0); + int count = 3; + + final int lottoPrice = 1_000; + Seller seller = new Seller(lottoPrice); + + //when + double rate = seller.calculateRate(result, count); + + //then + String rateStr = String.valueOf(rate); + String decimal = rateStr.split("\\.")[1]; + + assertThat(decimal.length()).isEqualTo(1); + } } \ No newline at end of file From 1765eca9a3621be1311916878498778ffa947c40 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 10:47:08 +0900 Subject: [PATCH 21/25] =?UTF-8?q?refactor:=20=EC=98=A4=EB=A6=84=EC=B0=A8?= =?UTF-8?q?=EC=88=9C=EC=9C=BC=EB=A1=9C=20=EB=B0=9C=ED=96=89=20-=20getLotto?= =?UTF-8?q?()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/lotto/Seller.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 7b4821882c..106c74c44d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,7 +7,7 @@ - [x] 로또 판매기 Seller - [x] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [x] 로또 발행 - getLotto() - - [ ] 오름차순으로 발행 + - [x] 오름차순으로 발행 - [x] 개수만큼 로또 발행 - getLottos() - [x] 로또 당첨 통계 반환 - getResult() - [x] 수익률 계산 - calculateRate() diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index cd478c79df..dc97b7b80d 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -3,6 +3,7 @@ import camp.nextstep.edu.missionutils.Randoms; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -21,7 +22,9 @@ public int calculateLottoCount(int money) { } public Lotto getLotto() { - return new Lotto(Randoms.pickUniqueNumbersInRange(1, 45, 6)); + List randoms = new ArrayList<>(Randoms.pickUniqueNumbersInRange(1, 45, 6)); + Collections.sort(randoms); + return new Lotto(randoms); } public List getLottos(int count) { From e405f9645284a4f7ae747138b112765233bc585d Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 10:49:14 +0900 Subject: [PATCH 22/25] =?UTF-8?q?refactor:=20=EC=88=98=EC=9D=B5=EB=A5=A0?= =?UTF-8?q?=EC=9D=80=20=EC=86=8C=EC=88=98=EC=A0=90=20=EB=91=98=EC=A7=B8?= =?UTF-8?q?=EC=9E=90=EB=A6=AC=EC=97=90=EC=84=9C=20=EB=B0=98=EC=98=AC?= =?UTF-8?q?=EB=A6=BC=20-=20calculateRate()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- src/main/java/lotto/Seller.java | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 106c74c44d..cc8070591c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,6 +4,7 @@ - [x] 여섯 개인지 검증 - validateCounts() - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [x] 정답과 비교 후 등수 반환 - calculateRank() + - [ ] 3항 연산자 제거 - [x] 로또 판매기 Seller - [x] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [x] 로또 발행 - getLotto() @@ -11,8 +12,7 @@ - [x] 개수만큼 로또 발행 - getLottos() - [x] 로또 당첨 통계 반환 - getResult() - [x] 수익률 계산 - calculateRate() - - [ ] 수익률은 소수점 둘째자리에서 반올림 - - [ ] 3항 연산자 제거 + - [x] 수익률은 소수점 둘째자리에서 반올림 - [x] UI - [x] 구입금액 입력받기 - receivePurchaseMoney() - [x] 구매한 로또 출력 - printBoughtLottos() diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index dc97b7b80d..618a0ea5d9 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -50,6 +50,8 @@ public double calculateRate(List ranks, int count) { .sum(); int cost = lottoPrice * count; - return prizeSum / (double) cost; + double rate = prizeSum / (double) cost; + + return (double) Math.round(rate * 10) / 10; } } From 1c8507affbcc1d401c01bf3c88efc04fe07dc940 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 10:51:09 +0900 Subject: [PATCH 23/25] =?UTF-8?q?refactor:=203=ED=95=AD=20=EC=97=B0?= =?UTF-8?q?=EC=82=B0=EC=9E=90=20=EC=A0=9C=EA=B1=B0=20-=20calculateRank()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/lotto/Lotto.java | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/README.md b/docs/README.md index cc8070591c..6565a568d8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,7 +4,7 @@ - [x] 여섯 개인지 검증 - validateCounts() - [x] 로또 번호에 중복된 숫자가 있는지 검증 - validateDuplication() - [x] 정답과 비교 후 등수 반환 - calculateRank() - - [ ] 3항 연산자 제거 + - [x] 3항 연산자 제거 - [x] 로또 판매기 Seller - [x] 금액에 받아 구매 가능한 로또 개수 반환 - calculateLottoCount() - [x] 로또 발행 - getLotto() diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index eaf40986ea..ea2e0713e7 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -34,13 +34,23 @@ public int calculateRank(List answers, int bonus) { Set set = new HashSet<>(numbers); - int correct = (int) answers.stream() + int correct = getCorrect(answers, set); + int bonusCorrect = getBonusCorrect(bonus, set, correct); + + return rankByCondition(correct, bonusCorrect); + } + + private static int getCorrect(List answers, Set set) { + return (int) answers.stream() .filter(set::contains) .count(); + } - int bonusCorrect = set.contains(bonus) && correct == 5 ? 1 : 0; - - return rankByCondition(correct, bonusCorrect); + private static int getBonusCorrect(int bonus, Set set, int correct) { + if (set.contains(bonus) && correct == 5) { + return 1; + } + return 0; } private int rankByCondition(int correct, int bonusCorrect) { From 33e67dc1a82bf72f6ae60b2695c02c3d722fadc5 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 11:29:12 +0900 Subject: [PATCH 24/25] =?UTF-8?q?fix=20:=20=EB=B6=80=EB=8F=99=EC=86=8C?= =?UTF-8?q?=EC=88=98=EC=A0=90=20E=20=ED=8F=AC=ED=95=A8=EB=90=9C=20?= =?UTF-8?q?=ED=91=9C=EA=B8=B0=EB=AC=B8=EC=A0=9C=20BigDecimal=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=B4=20=ED=95=B4=EA=B2=B0=20-=20calculateRate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Seller.java | 8 ++++---- src/main/java/lotto/UI.java | 5 +++-- src/test/java/lotto/SellerTest.java | 15 +++++++-------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index 618a0ea5d9..e68853babf 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -2,6 +2,7 @@ import camp.nextstep.edu.missionutils.Randoms; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -43,15 +44,14 @@ public List getResult(List lottos, List answers, int bo return result; } - public double calculateRate(List ranks, int count) { + public BigDecimal calculateRate(List ranks, int count) { long prizeSum = IntStream.range(0, 6) .mapToLong(index -> (long) ranks.get(index) * prize.get(index)) .sum(); int cost = lottoPrice * count; - double rate = prizeSum / (double) cost; - - return (double) Math.round(rate * 10) / 10; + double rate = prizeSum * 100 / (double) cost; + return new BigDecimal(rate).setScale(1, BigDecimal.ROUND_HALF_UP); } } diff --git a/src/main/java/lotto/UI.java b/src/main/java/lotto/UI.java index ca8b3fa5a5..07195b8c3f 100644 --- a/src/main/java/lotto/UI.java +++ b/src/main/java/lotto/UI.java @@ -2,6 +2,7 @@ import camp.nextstep.edu.missionutils.Console; +import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -51,7 +52,7 @@ public static void printResult(List result) { } } - public static void printRate(double rate) { - System.out.println("총 수익률은 " + rate + "%입니다."); + public static void printRate(BigDecimal rate) { + System.out.println("총 수익률은 " + rate.toPlainString() + "%입니다."); } } diff --git a/src/test/java/lotto/SellerTest.java b/src/test/java/lotto/SellerTest.java index f4b02c0b8e..c10dfab307 100644 --- a/src/test/java/lotto/SellerTest.java +++ b/src/test/java/lotto/SellerTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import org.junit.platform.commons.util.CollectionUtils; +import java.math.BigDecimal; +import java.text.DecimalFormat; import java.util.List; import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; @@ -129,16 +131,16 @@ void calculateRate() { //given List result = List.of(1, 1, 0, 0, 0, 0); int count = 2; - int expectedRate = (2_000_000_000 + 30_000_000) / 2_000; + double expectedRate = (2_000_000_000 + 30_000_000) / (double) 2_000 * 100; final int lottoPrice = 1_000; Seller seller = new Seller(lottoPrice); //when - double rate = seller.calculateRate(result, count); + BigDecimal rate = seller.calculateRate(result, count); //then - assertThat(rate).isEqualTo(expectedRate); + assertThat(rate.doubleValue()).isEqualTo(expectedRate); } @DisplayName("수익률은 소수점 둘째자리에서 반올림") @@ -152,12 +154,9 @@ void calculateRate_decimal() { Seller seller = new Seller(lottoPrice); //when - double rate = seller.calculateRate(result, count); + BigDecimal rate = seller.calculateRate(result, count); //then - String rateStr = String.valueOf(rate); - String decimal = rateStr.split("\\.")[1]; - - assertThat(decimal.length()).isEqualTo(1); + assertThat(rate.toPlainString().split("\\.")[1].length()).isEqualTo(1); } } \ No newline at end of file From d08ea77e2ab88c64c40e8504eee046e7d84f8895 Mon Sep 17 00:00:00 2001 From: yohanii Date: Tue, 1 Oct 2024 11:39:10 +0900 Subject: [PATCH 25/25] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?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/Application.java | 24 +++++++++++++++--------- src/main/java/lotto/Seller.java | 8 ++++++++ src/main/java/lotto/UI.java | 8 ++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index fd3e6388cc..fd28b6ffca 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -8,18 +8,24 @@ public class Application { public static void main(String[] args) { - int purchaseMoney = UI.receivePurchaseMoney(); + String purchaseMoneyStr = UI.receivePurchaseMoney(); Seller seller = new Seller(LOTTO_PRICE); - int purchaseCount = seller.calculateLottoCount(purchaseMoney); - List lottos = seller.getLottos(purchaseCount); - UI.printBoughtLottos(lottos); + try { + int purchaseCount = seller.calculateLottoCount(seller.parsePurchaseMoneyStr(purchaseMoneyStr)); - List answers = UI.receiveAnswers(); - int bonus = UI.receiveBonus(); + List lottos = seller.getLottos(purchaseCount); + UI.printBoughtLottos(lottos); + + List answers = UI.receiveAnswers(); + int bonus = UI.receiveBonus(); + + List result = seller.getResult(lottos, answers, bonus); + UI.printResult(result); + UI.printRate(seller.calculateRate(result, purchaseCount)); + } catch (IllegalArgumentException e) { + UI.handlingIllegalArgumentException(e); + } - List result = seller.getResult(lottos, answers, bonus); - UI.printResult(result); - UI.printRate(seller.calculateRate(result, purchaseCount)); } } diff --git a/src/main/java/lotto/Seller.java b/src/main/java/lotto/Seller.java index e68853babf..bb61f7a343 100644 --- a/src/main/java/lotto/Seller.java +++ b/src/main/java/lotto/Seller.java @@ -18,6 +18,14 @@ public Seller(int lottoPrice) { this.lottoPrice = lottoPrice; } + public int parsePurchaseMoneyStr(String purchaseMoneyStr) { + try { + return Integer.parseInt(purchaseMoneyStr); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("입력은 1000으로 나눠지는 값이어야 합니다."); + } + } + public int calculateLottoCount(int money) { return money / lottoPrice; } diff --git a/src/main/java/lotto/UI.java b/src/main/java/lotto/UI.java index 07195b8c3f..34b1b1080c 100644 --- a/src/main/java/lotto/UI.java +++ b/src/main/java/lotto/UI.java @@ -18,9 +18,9 @@ public class UI { 5, "3개 일치 (5,000원)" ); - public static int receivePurchaseMoney() { + public static String receivePurchaseMoney() { System.out.println("구입금액을 입력해 주세요."); - return Integer.parseInt(Console.readLine()); + return Console.readLine(); } public static void printBoughtLottos(List lottos) { @@ -55,4 +55,8 @@ public static void printResult(List result) { public static void printRate(BigDecimal rate) { System.out.println("총 수익률은 " + rate.toPlainString() + "%입니다."); } + + public static void handlingIllegalArgumentException(IllegalArgumentException e) { + System.out.println("[ERROR] " + e.getMessage()); + } }