From 913b18b9cda18e6447ec7a718d28b05d79154644 Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:47:17 +0900
Subject: [PATCH 01/11] docs: Add program description in README.md
---
README.md | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 97 insertions(+)
diff --git a/README.md b/README.md
index 5fa2560b46..c75ef97ffc 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,98 @@
# java-lotto-precourse
+
+### ๐ ํ ์ค ์๊ฐ
+
+1์ฅ์ 1000์ ์ง๋ฆฌ ๋ก๋๋ฅผ ๊ตฌ์
ํ ๊ธ์ก์ ๋ง์ถฐ ๋๋คํ๊ณ ์ค๋ณต์ด ์๋ 6์๋ฆฌ ์๋ฅผ ๋ฐํํด์ฃผ๋ฉด, ์ด๋ฅผ ๋น์ฒจ ๋ฒํธ์ ๋ณด๋์ค ๋ฒํธ์ ๊ธฐ๋ฐํ์ฌ ๋น์ฒจ ๊ฒฐ๊ณผ(๋น์ฒจ ๋ฑ์, ์์ต๋ฅ )์ ์๋ ค์ค๋ค.
+
+
+
+### ๐ ๊ตฌํ ์ฌํญ
+
+#### 1. ๊ธฐ๋ณธ ์ธํ
+ - jdk 21 ๋ฒ์ ์ค์
+ - java style guide ์ค์
+
+#### 2. ๊ตฌ์
๊ธ์ก ์
๋ ฅ
+ - readLine() ์ผ๋ก ๋ฌธ์์ด ์
๋ ฅ ๋ฐ๊ธฐ
+ - ์
๋ ฅ ๊ฐ์ ์์ ์ ์ ์ด์ธ์ ๊ฐ์ด ์ค๋ ๊ฒฝ์ฐ IllegalArgumentException ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+ - ์
๋ ฅ ๊ฐ์ด 1000 ๋จ์๊ฐ ์๋ ๊ฒฝ์ฐ IllegalArgumentException ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+ - IlleagalArgumentException ๋ฐ์ ์, ์ฌ์
๋ ฅ ๋ฐ๊ธฐ
+
+#### 3. ๋ก๋ ์ฅ์ ๊ณ์ฐ ๋ฐ ์ถ๋ ฅ
+ - ๊ธ์ก์ ๋ฐ๋ผ ๋ก๋ ์ฅ์ ๊ณ์ฐ
+ - ๊ตฌ๋งค ์ฅ์ ์ถ๋ ฅ
+
+#### 4. ๋ก๋ ๋ฐํ ๋ฐ ์ถ๋ ฅ
+ - ์ค๋ณต์ด ์๋ ๋๋คํ 6๊ฐ์ ์ ๋ฐ๊ธ
+ - ๊ตฌ๋งค ์ฅ์๋งํผ ์ถ๋ ฅ
+
+#### 5. ๋ก๋ ๋น์ฒจ ๋ฒํธ ๋ฐ ๋ณด๋์ค ๋ฒํธ ์
๋ ฅ
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ๋ฐ๊ธฐ
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ๋ฌธ์์ด์ 6๊ฐ ์ด๊ณผ์ ์๋ฅผ ์
๋ ฅํ ๊ฒฝ์ฐ IllegalArgumentException ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ๋ฌธ์์ด์ ์ค๋ณต๋ ์๊ฐ ๋์ค๋ ๊ฒฝ์ฐ IllegalArgumentException ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ๋ฌธ์์ด์ `,`์ ์ซ์ ์ด์ธ์ ๊ฐ์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ, IllegalArgumentException ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ๋ฌธ์์ด์ 1 ~ 45 ์ฌ์ด์ ์๊ฐ ์๋ ์๊ฐ ์
๋ ฅ๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+
+ - ๋ณด๋์ค ๋ฒํธ ์
๋ ฅ ๋ฐ๊ธฐ
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ๊ฐ์ด ๋น์ฒจ ๋ฒํธ ๋ฆฌ์คํธ ์์ ์๋ ๊ฐ์ธ ๊ฒฝ์ฐ(์ค๋ณต๋ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ) IllegalArgumentException ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ๋ฌธ์์ด์ ์ซ์ ์ด์ธ์ ๊ฐ์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ, IllegalArgumentException ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ๋ฌธ์์ด์ 1 ~ 45 ์ฌ์ด์ ์๊ฐ ์๋ ์๊ฐ ์
๋ ฅ๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ ์๋ฌ ๋ฉ์ธ์ง ํ์์ ๋ฐ๋ผ ์ถ๋ ฅ
+ - IlleagalArgumentException ๋ฐ์ ์, ์ฌ์
๋ ฅ ๋ฐ๊ธฐ
+
+#### 6. ๋น์ฒจ ํต๊ณ ์ถ๋ ฅ
+ - ๋ฐํํ ๋ก๋ ๋ฒํธ์ ๋น์ฒจ ๋ฒํธ ์ผ์น ๊ฐ์ ํ์ธ
+ - ๋ฐํํ ๋ก๋ ๋ฒํธ ์ผ์น ๊ฐ์ ๋ณ ํต๊ณ ์ถ๋ ฅ
+
+#### 7. ์์ต๋ฅ ๊ณ์ฐ ๋ฐ ์ถ๋ ฅ
+ - (์์ต๊ธ / ๊ตฌ๋งค ๊ธ์ก) * 100 ๊ฒฐ๊ณผ์์ ์์์ ๋์งธ์๋ฆฌ ๋ฐ์ฌ๋ฆผํ์ฌ ์์ต๋ฅ ์ถ๋ ฅ
+
+#### 8. ํ
์คํธ ์ฝ๋ ์์ฑ
+ [์ฑ๊ณต]
+ - 8000 / 1,2,3,4,5,6 / 7 ์
๋ ฅ ์ ์์ ๋ฐ์ดํฐ์ ๋ฐ๋ฅธ ์ถ๋ ฅ ๊ฒฐ๊ณผ ๋ฐ ์์ต๋ฅ ํ์ธ
+ - 7000 / 1,2,3,4,5,6 / 7 ์
๋ ฅ ์, 4๊ฐ ์ผ์น ๊ฒฝ์ฐ 1๊ฐ ๋ฐ์ ์ ์์ต๋ฅ ์ถ๋ ฅ ๊ฒฐ๊ณผ 2์๋ฆฌ ์ ๋ฐ์ฌ๋ฆผ ํ์ธ
+
+ [์คํจ-๋ก๋ ๊ฐ๊ฒฉ]
+ - ๋ก๋ ๊ฐ๊ฒฉ์ด 1000 ์ ๋จ์๊ฐ ์๋ ๊ฒฝ์ฐ, ERROR ๋ฉ์ธ์ง ํฌํจ ์ฌ๋ถ ๊ฒ์ฌ
+ - ๋ก๋ ๊ฐ๊ฒฉ์ ์์ ์ ์ ์ด์ธ์ ๊ฐ์ด ์ค๋ ๊ฒฝ์ฐ, ERROR ๋ฉ์ธ์ง ํฌํจ ์ฌ๋ถ ๊ฒ์ฌ
+
+ [์คํจ-๋น์ฒจ ๋ฒํธ]
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ์ `,`์ ์ซ์ ์ด์ธ์ ๊ฐ์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ, ERROR ๋ฉ์ธ์ง ํฌํจ ์ฌ๋ถ ๊ฒ์ฌ
+
+ [์คํจ-๋ณด๋์ค ๋ฒํธ]
+ - ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ์ ์ซ์ ์ด์ธ์ ๊ฐ์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ, ERROR ๋ฉ์ธ์ง ํฌํจ ์ฌ๋ถ ๊ฒ์ฌ
+ - ๋ณด๋์ค ๋ฒํธ ์
๋ ฅ ์ ๋น์ฒจ ๋ฒํธ์ ์ค๋ณต๋ ์๊ฐ ๋์ค๋ ๊ฒฝ์ฐ, ERROR ๋ฉ์ธ์ง ํฌํจ ์ฌ๋ถ ๊ฒ์ฌ
+
+ [์คํจ-๋ก๋]
+ - 6๊ฐ ์ด๊ณผ์ ์๋ฅผ ์
๋ ฅํ ๊ฒฝ์ฐ, ERROR ๋ฉ์ธ์ง ํฌํจ ์ฌ๋ถ ๊ฒ์ฌ
+ - ์ค๋ณต๋ ์๊ฐ ๋์ค๋ ๊ฒฝ์ฐ, ERROR ๋ฉ์ธ์ง ํฌํจ ์ฌ๋ถ ๊ฒ์ฌ
+ - 1 ~ 45 ์ฌ์ด์ ์๊ฐ ์๋ ์๊ฐ ์
๋ ฅ๋ ๊ฒฝ์ฐ, ERROR ๋ฉ์ธ์ง ํฌํจ ์ฌ๋ถ ๊ฒ์ฌ
+
+
+
+### โ
์กฐ๊ฑด
+- ๋ก๋ ๋ฒํธ
+- [ ] ๋ก๋ ๋ฒํธ์ ์ซ์ ๋ฒ์๋ 1 ~ 45 ์ฌ์ด์๋ง ์กด์ฌํจ
+- [ ] ๋ก๋ ๋ฒํธ๋ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ ๋ณด์ฌ์ค
+- [ ] 1๊ฐ์ ๋ก๋ ๋ฐํ(6๊ฐ์ ๋ฒํธ ์ถ์ถ) ์, ๋น์ฒจ๋ฒํธ 6๊ฐ ์ถ์ฒจ ์ ์ค๋ณต๋ ์ซ์๊ฐ ๋์ค๋ฉด ์๋จ
+ - ๋ณด๋์ค ๋ฒํธ์ ๊ฒฝ์ฐ๋ ๋น์ฒจ๋ฒํธ์ ์ค๋ณต๋ ์ซ์์ด๋ฉด ์๋จ
+
+- ๋ก๋ ๊ฐ๊ฒฉ
+- [ ] ๋ก๋ ๊ฐ๊ฒฉ์ ์์ ์ ์ ์ด์ธ์ ๊ฐ์ด ์ค๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+- [ ] ๋ก๋ ๊ฐ๊ฒฉ์ด 1000 ๋จ์๊ฐ ์๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+
+- ๋น์ฒจ ๋ฒํธ
+- [ ] ๋น์ฒจ ๋ฒํธ๋ `,`๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ๋จ
+- [ ] ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ์ 6๊ฐ ์ด๊ณผ์ ์๋ฅผ ์
๋ ฅํ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+- [ ] ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ์ ์ค๋ณต๋ ์๊ฐ ๋์ค๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+- [ ] ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ์ `,`์ ์ซ์ ์ด์ธ์ ๊ฐ์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+- [ ] ๋น์ฒจ ๋ฒํธ ์
๋ ฅ ์ 1 ~ 45 ์ฌ์ด์ ์๊ฐ ์๋ ์๊ฐ ์
๋ ฅ๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+
+- ๋ณด๋์ค ๋ฒํธ
+- [ ] ๋ณด๋์ค ๋ฒํธ ์
๋ ฅ ์ ๋น์ฒจ ๋ฒํธ์ ์ค๋ณต๋ ์๊ฐ ๋์ค๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+- [ ] ๋ณด๋์ค ๋ฒํธ ์
๋ ฅ ์ ์ซ์ ์ด์ธ์ ๊ฐ์ด ์
๋ ฅ๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+- [ ] ๋ณด๋์ค ๋ฒํธ ์
๋ ฅ ์ ์
๋ ฅ ์ 1 ~ 45 ์ฌ์ด์ ์๊ฐ ์๋ ์๊ฐ ์
๋ ฅ๋ ๊ฒฝ์ฐ, `IllegalArgumentException` ๋ฐ์ ๋ฐ `[ERROR]` ๋ก ์์ํ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ
+
+- ์์ต๋ฅ
+- [ ] ์์์ ๋์งธ ์๋ฆฌ์์ ๋ฐ์ฌ๋ฆผํ ๊ฐ์ผ๋ก ์ถ๋ ฅ
+
+
From 74749f36ddf8a9e154c76ea9bafbbd4feb67342f Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:49:14 +0900
Subject: [PATCH 02/11] feat: Add input string checking function in
InputUtil.java
---
src/main/java/lotto/InputUtil.java | 57 ++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+)
create mode 100644 src/main/java/lotto/InputUtil.java
diff --git a/src/main/java/lotto/InputUtil.java b/src/main/java/lotto/InputUtil.java
new file mode 100644
index 0000000000..75b85dd5b8
--- /dev/null
+++ b/src/main/java/lotto/InputUtil.java
@@ -0,0 +1,57 @@
+package lotto;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class InputUtil {
+ // ์ ์์ธ์ง ํ์ธ
+ public static void isNumeric(String input) {
+ if (input == null || !input.matches("\\d+")) {
+ throw new IllegalArgumentException("[ERROR] ์
๋ ฅ ๊ฐ ๋ด์ ์ ์ ์ธ์ ๊ฐ์ด ์์ต๋๋ค.");
+ }
+ }
+
+ // ์ ์ ๋ฐ , ์ธ์ง ํ์ธ
+ public static void isNumericWithCommas(String input) {
+ if (input == null || !input.matches("[\\d,]+")) {
+ throw new IllegalArgumentException("[ERROR] ์
๋ ฅ ๊ฐ ๋ด์ ์ ์๋ , ์ธ์ ๊ฐ์ด ์์ต๋๋ค.");
+ }
+ }
+
+ // ๋จ์ ํ์ธ
+ public static void isClearedByNum(String input, int num) {
+ if ((Integer.parseInt(input) % num) != 0) {
+ throw new IllegalArgumentException("[ERROR] " + num + "๋จ์์ ๊ฐ๋ง ์
๋ ฅ์ด ๊ฐ๋ฅํฉ๋๋ค.");
+ }
+ }
+
+ // ์
๋ ฅ๊ฐ์ด min ์ด์, max ๋ฏธ๋ง์ธ์ง ํ์ธํ๋
+ public static void checkOverThanMinAndLessThanMax(int i, int min, int max) {
+ if (!(i >= min && i <= max)) {
+ throw new IllegalArgumentException("[ERROR] ์
๋ ฅ ๊ฐ์ " + min + "๊ณผ " + max + " ์ฌ์ด์ฌ์ผ ํฉ๋๋ค.");
+ }
+ }
+
+ public static void checkInputIsNotInNumbers(List numbers, String input, String numbersName) {
+ for (int i : numbers) {
+ if (i == Integer.parseInt(input)) {
+ throw new IllegalArgumentException("[ERROR] ํด๋น ๊ฐ์ด " + numbersName + " ๋ด์ ์กด์ฌํ๋ฉด ์๋ฉ๋๋ค.");
+ }
+ }
+ }
+
+ // ๋ฌธ์์ด , ๊ธฐ์ค์ผ๋ก ์ชผ๊ฐ๊ธฐ
+ public static List splitByComma(String input) {
+ List numbersList = new ArrayList<>();
+
+ if (input != null && !input.isEmpty()) {
+ String[] parts = input.split(",");
+ for (String part : parts) {
+ numbersList.add(Integer.parseInt(part.trim()));
+ }
+ }
+
+ return numbersList;
+ }
+
+}
From 0ad3ed64e62bcf07cae9d20a0e7a405fc66abde9 Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:50:05 +0900
Subject: [PATCH 03/11] feat: Add check price validate in Application.java
---
src/main/java/lotto/Application.java | 29 +++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java
index d190922ba4..f58bfead39 100644
--- a/src/main/java/lotto/Application.java
+++ b/src/main/java/lotto/Application.java
@@ -1,7 +1,34 @@
package lotto;
+import static lotto.InputUtil.isClearedByNum;
+import static lotto.InputUtil.isNumeric;
+import static lotto.Lotto.LOTTO_BASIC_PRICE;
+
+import camp.nextstep.edu.missionutils.Console;
+
public class Application {
+
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+
+ // ๊ตฌ์
๊ธ์ก ์
๋ ฅ ๋ฐ๊ณ ์ ํจ์ฑ ๊ฒ์ฌ
+ int purchasesCount;
+ while (true) {
+ try {
+ System.out.println("๊ตฌ์
๊ธ์ก์ ์
๋ ฅํด ์ฃผ์ธ์.\n");
+ String price = Console.readLine();
+
+ isNumeric(price);
+ isClearedByNum(price, LOTTO_BASIC_PRICE); // ๊ตฌ์
๊ธ์ก์ด 1000์ ๋จ์์ธ์ง ํ์ธ
+
+ purchasesCount = Integer.parseInt(price) / LOTTO_BASIC_PRICE;
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println("์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์\n ์์ธ ๋ฉ์ธ์ง: " + e.getMessage());
+ }
+ }
+
+
}
+
+
}
From abf176686c64c1d8bdbeba3b055fff5619adf685 Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:50:56 +0900
Subject: [PATCH 04/11] feat: Add printing purchasesCount in Application.java
---
src/main/java/lotto/Application.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java
index f58bfead39..42c851272f 100644
--- a/src/main/java/lotto/Application.java
+++ b/src/main/java/lotto/Application.java
@@ -27,8 +27,8 @@ public static void main(String[] args) {
}
}
-
+ // ๊ตฌ๋งค ๊ฐ์ ์ถ๋ ฅ
+ System.out.println(purchasesCount + "๊ฐ๋ฅผ ๊ตฌ๋งคํ์ต๋๋ค.\n");
}
-
}
From 8b26d1b76dae3f86dd2ee5a5f0d85baf2692cf8f Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:52:28 +0900
Subject: [PATCH 05/11] feat: Add random lotto printing Application.java
---
src/main/java/lotto/Application.java | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java
index 42c851272f..8c41e3be5e 100644
--- a/src/main/java/lotto/Application.java
+++ b/src/main/java/lotto/Application.java
@@ -1,10 +1,21 @@
package lotto;
+import static lotto.InputUtil.checkInputIsNotInNumbers;
+import static lotto.InputUtil.checkOverThanMinAndLessThanMax;
import static lotto.InputUtil.isClearedByNum;
import static lotto.InputUtil.isNumeric;
+import static lotto.InputUtil.isNumericWithCommas;
+import static lotto.InputUtil.splitByComma;
import static lotto.Lotto.LOTTO_BASIC_PRICE;
+import static lotto.Lotto.LOTTO_MAX_NUM;
+import static lotto.Lotto.LOTTO_MIN_NUM;
+import static lotto.Lotto.checkLotto;
+import static lotto.Lotto.makeRandomLotto;
import camp.nextstep.edu.missionutils.Console;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
public class Application {
@@ -29,6 +40,15 @@ public static void main(String[] args) {
// ๊ตฌ๋งค ๊ฐ์ ์ถ๋ ฅ
System.out.println(purchasesCount + "๊ฐ๋ฅผ ๊ตฌ๋งคํ์ต๋๋ค.\n");
+
+ // ๊ตฌ๋งคํ ๋ก๋ ๋ฐํ
+ List purchases = new ArrayList<>();
+ for (int i = 0; i < purchasesCount; i++) {
+ purchases.add(makeRandomLotto());
+ System.out.println(purchases.get(i).getNumbers());
+ }
+
}
+
}
From c9f4a1376c9357a26957dfde865aa30491262658 Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:53:05 +0900
Subject: [PATCH 06/11] feat: Add random lotto generate and check validate in
Lotto.java
---
src/main/java/lotto/Lotto.java | 59 +++++++++++++++++++++++++++++++---
1 file changed, 55 insertions(+), 4 deletions(-)
diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java
index 88fc5cf12b..791e04b705 100644
--- a/src/main/java/lotto/Lotto.java
+++ b/src/main/java/lotto/Lotto.java
@@ -1,20 +1,71 @@
package lotto;
+import static lotto.InputUtil.checkOverThanMinAndLessThanMax;
+
+import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Random;
+import java.util.Set;
+import java.util.function.Supplier;
public class Lotto {
+ public static final int LOTTO_SIZE = 6;
+ public static final int LOTTO_MAX_NUM = 45;
+ public static final int LOTTO_MIN_NUM = 1;
+ public static final int LOTTO_BASIC_PRICE = 1000;
+
private final List numbers;
public Lotto(List numbers) {
- validate(numbers);
+ validateSixCount(numbers);
+ validateNoDuplicates(numbers);
+ validateCoverage(numbers);
this.numbers = numbers;
}
- private void validate(List numbers) {
- if (numbers.size() != 6) {
+ // ๋๋ค ๋ก๋ ์์ฑ
+ public static Lotto makeRandomLotto(){
+ return new Lotto(generateUniqueRandomNumbers());
+ }
+
+ public List getNumbers() {
+ return numbers;
+ }
+
+ // 6์๋ฆฌ ์ซ์์ธ์ง ํ์ธ
+ private void validateSixCount(List numbers) {
+ if (numbers.size() != LOTTO_SIZE) {
throw new IllegalArgumentException("[ERROR] ๋ก๋ ๋ฒํธ๋ 6๊ฐ์ฌ์ผ ํฉ๋๋ค.");
}
}
- // TODO: ์ถ๊ฐ ๊ธฐ๋ฅ ๊ตฌํ
+ // ์ค๋ณต๋ ๊ฐ์ด ์๋์ง ํ์ธ
+ private void validateNoDuplicates(List numbers) {
+ Set uniqueNumbers = new HashSet<>(numbers);
+ if (uniqueNumbers.size() != numbers.size()) {
+ throw new IllegalArgumentException("[ERROR] ์ค๋ณต๋ ์ซ์๊ฐ ์์ต๋๋ค.");
+ }
+ }
+
+ // ๋ฆฌ์คํธ ๋ด์ ๋ชจ๋ ์ซ์๊ฐ ๋ฒ์ ๋ด์ธ์ง ํ์ธ
+ private void validateCoverage(List numbers) {
+ for (int i : numbers) {
+ checkOverThanMinAndLessThanMax(i, LOTTO_MIN_NUM, LOTTO_MAX_NUM);
+ }
+ }
+
+ // ๋๋ค ๋ฒํธ ์์ฑ
+ private static List generateUniqueRandomNumbers() {
+ Set uniqueNumbers = new HashSet<>();
+ Random random = new Random();
+
+ while (uniqueNumbers.size() < LOTTO_SIZE) {
+ int randomNumber = random.nextInt(LOTTO_MAX_NUM - LOTTO_MIN_NUM + 1) + LOTTO_MIN_NUM;
+ uniqueNumbers.add(randomNumber);
+ }
+
+ return new ArrayList<>(uniqueNumbers);
+ }
+
}
From 7032a86d5f776b8b666f9d66036ace228d4da0ab Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:54:36 +0900
Subject: [PATCH 07/11] feat: Add get winningLottoNum, bonusNum and check
validate in Application.java
---
src/main/java/lotto/Application.java | 41 ++++++++++++++++++++++++++--
1 file changed, 39 insertions(+), 2 deletions(-)
diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java
index 8c41e3be5e..3319f63e49 100644
--- a/src/main/java/lotto/Application.java
+++ b/src/main/java/lotto/Application.java
@@ -9,16 +9,16 @@
import static lotto.Lotto.LOTTO_BASIC_PRICE;
import static lotto.Lotto.LOTTO_MAX_NUM;
import static lotto.Lotto.LOTTO_MIN_NUM;
-import static lotto.Lotto.checkLotto;
import static lotto.Lotto.makeRandomLotto;
import camp.nextstep.edu.missionutils.Console;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
public class Application {
+ public static final String WINNING_NUMBERS_NAME = "๋น์ฒจ ๋ฒํธ";
+
public static void main(String[] args) {
// ๊ตฌ์
๊ธ์ก ์
๋ ฅ ๋ฐ๊ณ ์ ํจ์ฑ ๊ฒ์ฌ
@@ -48,6 +48,43 @@ public static void main(String[] args) {
System.out.println(purchases.get(i).getNumbers());
}
+ // ๋น์ฒจ ๋ก๋ ๋ฒํธ ์
๋ ฅ ๋ฐ๊ธฐ
+ Lotto winningLotto;
+ while (true) {
+ try {
+ System.out.println("๋น์ฒจ ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.\n");
+ String winningNumbers = Console.readLine();
+
+ isNumericWithCommas(winningNumbers);
+
+ // ๋น์ฒจ ๋ฒํธ๋ก ๋ก๋ ๋ง๋ค๊ธฐ
+ winningLotto = new Lotto(splitByComma(winningNumbers));
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println("์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์\n ์์ธ ๋ฉ์ธ์ง: " + e.getMessage());
+ }
+ }
+
+ int bonusNumber;
+ // ๋ณด๋์ค ๋ฒํธ ์
๋ ฅ ๋ฐ๊ธฐ
+ while (true) {
+ try {
+ System.out.println("๋ณด๋์ค ๋ฒํธ๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.\n");
+ String enteredBonusNumber = Console.readLine();
+
+ isNumeric(enteredBonusNumber);
+ checkOverThanMinAndLessThanMax(Integer.parseInt(enteredBonusNumber), LOTTO_MIN_NUM,
+ LOTTO_MAX_NUM);
+ checkInputIsNotInNumbers(winningLotto.getNumbers(), enteredBonusNumber,
+ WINNING_NUMBERS_NAME);
+
+ bonusNumber = Integer.parseInt(enteredBonusNumber);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println("์๋ชป๋ ์
๋ ฅ์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์\n ์์ธ ๋ฉ์ธ์ง: " + e.getMessage());
+ }
+ }
+
}
From 67eeef3aa00fa6b3876cae5a0b053baa5d6229a1 Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:55:20 +0900
Subject: [PATCH 08/11] feat: Add printing winning details in Application.java
---
src/main/java/lotto/Application.java | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java
index 3319f63e49..d8af77878a 100644
--- a/src/main/java/lotto/Application.java
+++ b/src/main/java/lotto/Application.java
@@ -9,10 +9,12 @@
import static lotto.Lotto.LOTTO_BASIC_PRICE;
import static lotto.Lotto.LOTTO_MAX_NUM;
import static lotto.Lotto.LOTTO_MIN_NUM;
+import static lotto.Lotto.checkLotto;
import static lotto.Lotto.makeRandomLotto;
import camp.nextstep.edu.missionutils.Console;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
public class Application {
@@ -85,7 +87,22 @@ public static void main(String[] args) {
}
}
- }
+ ArrayList statics = new ArrayList(Arrays.asList(0, 0, 0, 0, 0));
+
+ // ๋น์ฒจ ๋ด์ญ ์ถ๋ ฅ
+ for (Lotto lotto : purchases) {
+ int result = checkLotto(lotto.getNumbers(), winningLotto.getNumbers(), bonusNumber);
+ if (result >= 0 && result < 5) {
+ statics.set(result, statics.get(result) + 1);
+ }
+ }
+
+ System.out.println("3๊ฐ ์ผ์น (5,000์) - " + statics.get(0) + "๊ฐ");
+ System.out.println("4๊ฐ ์ผ์น (50,000์) - " + statics.get(1) + "๊ฐ");
+ System.out.println("5๊ฐ ์ผ์น (1,500,000์) - " + statics.get(2) + "๊ฐ");
+ System.out.println("5๊ฐ ์ผ์น, ๋ณด๋์ค ๋ณผ ์ผ์น (30,000,000์) - " + statics.get(3) + "๊ฐ");
+ System.out.println("6๊ฐ ์ผ์น (2,000,000,000์) - " + statics.get(4) + "๊ฐ");
+ }
}
From 6215dbfd9722c92bf021a2038a9895fc9ca0afd9 Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:55:38 +0900
Subject: [PATCH 09/11] feat: Add calculate winning details in Lotto.java
---
src/main/java/lotto/Lotto.java | 42 ++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java
index 791e04b705..53811512a2 100644
--- a/src/main/java/lotto/Lotto.java
+++ b/src/main/java/lotto/Lotto.java
@@ -68,4 +68,46 @@ private static List generateUniqueRandomNumbers() {
return new ArrayList<>(uniqueNumbers);
}
+ // ๋น์ฒจ ๋ฒํธ ๋น๊ต
+ private static int countMatchingNumbers(List uniqueNumbers, List winningNumbers) {
+ Set unique = new HashSet<>(uniqueNumbers);
+ Set winning = new HashSet<>(winningNumbers);
+
+ unique.retainAll(winning);
+ return unique.size();
+ }
+
+ // ๋ณด๋์ค ๋ฒํธ์ ์ผ์น ๋น๊ต
+ private static boolean isBonusMatched(List uniqueNumbers, int bonusNumber) {
+ return uniqueNumbers.contains(bonusNumber);
+ }
+
+ // ์ผ์น ๊ฐ์์ ๋ณด๋์ค ์ผ์น ์ฌ๋ถ์ ๋ฐ๋ฅธ ๊ฒฐ๊ณผ ๋ฐํ
+ public static int getPrizeForMatchCount(int matchCount, boolean bonusMatch) {
+ if (matchCount == 6) {
+ return 4;
+ }
+ if (matchCount == 5 && bonusMatch) {
+ return 3;
+ }
+ if (matchCount == 5) {
+ return 2;
+ }
+ if (matchCount == 4) {
+ return 1;
+ }
+ if (matchCount == 3) {
+ return 0;
+ }
+ return -1;
+ }
+
+ // ์ต์ข
๊ฒฐ๊ณผ ๊ณ์ฐ ํจ์
+ public static int checkLotto(List uniqueNumbers, List winningNumbers, int bonusNumber) {
+ int matchCount = countMatchingNumbers(uniqueNumbers, winningNumbers);
+ boolean bonusMatch = isBonusMatched(uniqueNumbers, bonusNumber);
+ return getPrizeForMatchCount(matchCount, bonusMatch);
+ }
+
+
}
From 2b064f0b34a8f4862f913dd0ca7f36953f26acd5 Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:56:01 +0900
Subject: [PATCH 10/11] feat: Add calculate rate of return in Application.java
---
src/main/java/lotto/Application.java | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java
index d8af77878a..dbc07a09d3 100644
--- a/src/main/java/lotto/Application.java
+++ b/src/main/java/lotto/Application.java
@@ -103,6 +103,13 @@ public static void main(String[] args) {
System.out.println("5๊ฐ ์ผ์น, ๋ณด๋์ค ๋ณผ ์ผ์น (30,000,000์) - " + statics.get(3) + "๊ฐ");
System.out.println("6๊ฐ ์ผ์น (2,000,000,000์) - " + statics.get(4) + "๊ฐ");
+ // ์์ต๋ฅ ๊ณ์ฐ ๋ฐ ์ถ๋ ฅ
+ int profits = statics.get(0)*5000 + statics.get(1)*50000 + statics.get(2)*1500000 + statics.get(3)*30000000 + statics.get(4)*2000000000;
+ double rateOfReturn = (profits / (double)(purchasesCount * LOTTO_BASIC_PRICE)) * 100;
+ double roundedRateOfReturn = Math.round(rateOfReturn * 100) / 100.0;
+ System.out.println("์ด ์์ต๋ฅ ์ " + roundedRateOfReturn + "%์
๋๋ค.");
+
+
}
}
From a65fccb3d2452ae45741498f5fb3dc3ebea85d73 Mon Sep 17 00:00:00 2001
From: yerim1ee <97941141+yerim123456@users.noreply.github.com>
Date: Mon, 4 Nov 2024 23:56:35 +0900
Subject: [PATCH 11/11] test: Add unit test for lotto and application
---
src/test/java/lotto/ApplicationTest.java | 33 +++++++++++-
src/test/java/lotto/LottoTest.java | 65 ++++++++++++++++++++++--
2 files changed, 94 insertions(+), 4 deletions(-)
diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java
index a15c7d1f52..2f2b97b4ea 100644
--- a/src/test/java/lotto/ApplicationTest.java
+++ b/src/test/java/lotto/ApplicationTest.java
@@ -47,13 +47,44 @@ class ApplicationTest extends NsTest {
}
@Test
- void ์์ธ_ํ
์คํธ() {
+ void ๋ก๋๊ฐ๊ฒฉ_์ซ์_์ด์ธ์_๊ฐ_์
๋ ฅ_์์ธ_ํ
์คํธ() {
assertSimpleTest(() -> {
runException("1000j");
assertThat(output()).contains(ERROR_MESSAGE);
});
}
+ @Test
+ void ๋ก๋๊ฐ๊ฒฉ_์ฒ์_๋จ์_์๋_๊ฒฝ์ฐ_์์ธ_ํ
์คํธ() {
+ assertSimpleTest(() -> {
+ runException("1003");
+ assertThat(output()).contains(ERROR_MESSAGE);
+ });
+ }
+
+ @Test
+ void ๋น์ฒจ๋ฒํธ_์ผํ_์ซ์_์ด์ธ์_๊ฐ_์
๋ ฅ์ธ_๊ฒฝ์ฐ_์์ธ_ํ
์คํธ() {
+ assertSimpleTest(() -> {
+ runException("1000", "1,2,3&4,5,6");
+ assertThat(output()).contains(ERROR_MESSAGE);
+ });
+ }
+ @Test
+ void ๋ณด๋์ค๋ฒํธ_์ฝํ_์ซ์_์ด์ธ์_๊ฐ_์
๋ ฅ์ธ_๊ฒฝ์ฐ_์์ธ_ํ
์คํธ() {
+ assertSimpleTest(() -> {
+ runException("1000", "1,2,3,4,5,6", "12A");
+ assertThat(output()).contains(ERROR_MESSAGE);
+ });
+ }
+
+ @Test
+ void ๋ณด๋์ค๋ฒํธ_๋น์ฒจ๋ฒํธ์_์ค๋ณต์ธ_๊ฒฝ์ฐ_์์ธ_ํ
์คํธ() {
+ assertSimpleTest(() -> {
+ runException("1000", "1,2,3,4,5,6", "1");
+ assertThat(output()).contains(ERROR_MESSAGE);
+ });
+ }
+
@Override
public void runMain() {
Application.main(new String[]{});
diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java
index 309f4e50ae..5e657018d9 100644
--- a/src/test/java/lotto/LottoTest.java
+++ b/src/test/java/lotto/LottoTest.java
@@ -1,25 +1,84 @@
package lotto;
+import java.util.Arrays;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class LottoTest {
+
+ private List validNumbers;
+ private List winningNumbers;
+ private int bonusNumber;
+
+ @BeforeEach
+ void setUp() {
+ validNumbers = Arrays.asList(3, 7, 12, 19, 25, 32);
+ winningNumbers = Arrays.asList(3, 7, 12, 25, 30, 32);
+ bonusNumber = 19;
+ }
+
+ @DisplayName("๋ก๋ ๋ฒํธ์ ๊ฐ์๊ฐ 6๊ฐ๊ฐ ๋์ด๊ฐ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค")
@Test
void ๋ก๋_๋ฒํธ์_๊ฐ์๊ฐ_6๊ฐ๊ฐ_๋์ด๊ฐ๋ฉด_์์ธ๊ฐ_๋ฐ์ํ๋ค() {
assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7)))
.isInstanceOf(IllegalArgumentException.class);
}
- @DisplayName("๋ก๋ ๋ฒํธ์ ์ค๋ณต๋ ์ซ์๊ฐ ์์ผ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
@Test
void ๋ก๋_๋ฒํธ์_์ค๋ณต๋_์ซ์๊ฐ_์์ผ๋ฉด_์์ธ๊ฐ_๋ฐ์ํ๋ค() {
assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5)))
.isInstanceOf(IllegalArgumentException.class);
}
+ @Test
+ void ๋ก๋_๋ฒํธ์_1๊ณผ_45์ฌ์ด๊ฐ_์๋_๊ฐ์ด_์์ผ๋ฉด_์์ธ๊ฐ_๋ฐ์ํ๋ค(){
+ assertThatThrownBy(() -> new Lotto(List.of(46, 1, 2, 3, 4, 5)))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ void ๋ก๋_๋ฒํธ_๋๋ค_๋ฐํ_์_1๊ณผ_45์ฌ์ด์_6๊ฐ์_์๊ฐ_๋ฐํ๋๋ค(){
+ Lotto randomLotto = Lotto.makeRandomLotto();
+ List numbers = randomLotto.getNumbers();
+ assertEquals(Lotto.LOTTO_SIZE, numbers.size(), "6๊ฐ์ ์๋ฅผ ๊ฐ๊ณ ์๋ค.");
+ assertTrue(numbers.stream().allMatch(num -> num >= Lotto.LOTTO_MIN_NUM && num <= Lotto.LOTTO_MAX_NUM),
+ "1๊ณผ 45 ์ฌ์ด์ ๊ฐ์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋ค.");
+ }
+
+ @Test
+ void ๋น์ฒจ_๋ฒํธ์_๋ฐํ_๋ก๋_๋ฒํธ_์ฌ์ด์_์ผ์น๋_๊ฐ์๋ฅผ_ํ์ธ_๊ฐ๋ฅํ๋ค(){
+ int matchCount = Lotto.checkLotto(validNumbers, winningNumbers, bonusNumber);
+ assertEquals(3, matchCount, "๋ณด๋์ค ๋ฒํธ ์ ์ธ 3๊ฐ์ ์๊ฐ ์ผ์นํ๋ค.");
+ }
+
+ @Test
+ void ์ด๋ฑ์ธ_๊ฒฝ์ฐ_๋ณด๋์ค_๋ฒํธ์_๊ด๋ จ์ด_์๋ค() {
+ List userNumbers = Arrays.asList(3, 7, 12, 19, 25, 32);
+ int prize = Lotto.checkLotto(userNumbers, winningNumbers, bonusNumber);
+ assertEquals(3, prize, "5๊ฐ ์ ์ผ์น, ๋ณด๋์ค ๋ฒํธ ์ผ์น");
+ }
+
+ @Test
+ void ์ผ๋ฑ์ธ_๊ฒฝ์ฐ_๋ณด๋์ค_๋ฒํธ์_๊ด๋ จ์ด_์๋ค() {
+ List userNumbers = Arrays.asList(3, 7, 12, 25, 32, 40);
+ int prize = Lotto.checkLotto(userNumbers, winningNumbers, bonusNumber);
+ assertEquals(2, prize, "5๊ฐ ์ ์ผ์น, ๋ณด๋์ค ๋ฒํธ ๋ถ์ผ์น");
+ }
+
+ @Test
+ void ๋ณด๋์ค_๋ฒํธ_ํฌํจ_๋ฑ์_๊ท์น_๋งค์นญ_๋ฒํธ์_๋ง๊ฒ_์ถ๋ ฅ๋๋ค() {
+ assertEquals(4, Lotto.getPrizeForMatchCount(6, false), "6๊ฐ ์ผ์น");
+ assertEquals(3, Lotto.getPrizeForMatchCount(5, true), "5๊ฐ ์ผ์น, ๋ณด๋์ค ๋ณผ ์ผ์น");
+ assertEquals(2, Lotto.getPrizeForMatchCount(5, false), "5๊ฐ ์ผ์น");
+ assertEquals(1, Lotto.getPrizeForMatchCount(4, false), "4๊ฐ ์ผ์น");
+ assertEquals(0, Lotto.getPrizeForMatchCount(3, false), "3๊ฐ ์ผ์น");
+ assertEquals(-1, Lotto.getPrizeForMatchCount(2, false), "3๊ฐ ๋ฏธ๋ง ์ผ์น");
+ }
- // TODO: ์ถ๊ฐ ๊ธฐ๋ฅ ๊ตฌํ์ ๋ฐ๋ฅธ ํ
์คํธ ์ฝ๋ ์์ฑ
-}
+}
\ No newline at end of file