From 0ec6e6282727ae63233ace8d015485ebb5205dbd Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sat, 4 May 2024 20:49:31 +0900 Subject: [PATCH 01/31] feat(Utility): add utility class --- src/main/java/Application.java | 6 ++++++ src/main/java/Utility.java | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 src/main/java/Application.java create mode 100644 src/main/java/Utility.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..7200c38f --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,6 @@ +public class Application { + public static void main(String[] args) { + + + } +} diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java new file mode 100644 index 00000000..ef5f45ca --- /dev/null +++ b/src/main/java/Utility.java @@ -0,0 +1,3 @@ +public class Utility { + private Utility() {} +} From 9d20397ca5c54eecc4a18e6d4eaf78d717a915ca Mon Sep 17 00:00:00 2001 From: kangwlgns <71175483+kangwlgns@users.noreply.github.com> Date: Sat, 4 May 2024 20:56:58 +0900 Subject: [PATCH 02/31] docs(README): update readme --- README.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d7e8aee..11e05b90 100644 --- a/README.md +++ b/README.md @@ -1 +1,31 @@ -# java-baseball-precourse \ No newline at end of file +# java-baseball-precourse +* Utilty Class + * 난수 생성 함수 + * digit 범위의 3가지 난수를 중복되지 않을 때까지 뽑음. + * 난수 생성 시에는 Math.random() 사용 + * 3자리 난수를 int[]로 반환 + * 입력 받는 함수 + * Scanner()를 사용해서 사용자 입력을 String으로 받고 반환. + * 입력값 유효성 검사 함수 1 + * 파라미터로 String을 받음 + * 길이가 3이 아닌 경우 throw + * 입력이 digit의 조합이 아닌 경우 throw + * 입력 digit이 중복되는 경우 throw + * 입력을 int[]로 반환 + * strike 판정 함수 + * 각 자릿수를 비교해서 strike 개수를 int로 반환 + * ball 판정 함수 + * 다른 자릿수를 비교하여 ball 개수를 int로 반환 + * 난수도, 입력값도 중복이 없다고 가정했으므로 로직은 유효 + * 출력문 반환 함수 + * 파라미터로 strike 개수와 ball 개수를 받아 모두 0이면 "낫싱" 반환 + * ball 개수가 0이 아니면 (ball 개수) + "볼 "을 저장, 0이라면 빈 문자열을 저장 + * strike 개수가 0이 아니면 위에서 저장된 문자열에 (strike 개수) + "스트라이크"를 붙여서 반환, 0이라면 위에서 저장한 문자열만 반환 + * 종료 후 출력 함수 + * 파라미터로 strike 개수를 받아 3이 아니면 빈 문자열 반환 + * 3이라면 안내문 출력 및 입력 받는 함수를 호출하여 입력 받기 + * 입력값 유효성 검사 함수 2 + * 파라미터로 String과 strike 개수를 받음 + * strike 개수가 3이 아니면 true 반환 + * "1"인 경우 true 반환 + * "2"인 경우 false 반환 From 7e959e860629a93e49516d8f98eb5db306ac2e0a Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 16:05:21 +0900 Subject: [PATCH 03/31] feat(Utility): add method of making random numbers --- src/main/java/Utility.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index ef5f45ca..6ff8c889 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -1,3 +1,24 @@ public class Utility { - private Utility() {} + + private Utility() { + } + + public static int[] makeRandomNumbers() { + boolean[] flags = new boolean[10]; + int[] randomNumbers = new int[3]; + int count = 0; + + while (count < 3) { + int temp = (int) (Math.random() * 10); + if (flags[temp]) { + continue; + } else { + randomNumbers[count] = temp; + flags[temp] = true; + count += 1; + } + } + + return randomNumbers; + } } From deb890823bcf9eb6132ccda71d88ac0c7e77eee9 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 16:07:38 +0900 Subject: [PATCH 04/31] test(UtilityTest): add making random numbers test --- src/test/java/UtilityTest.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/test/java/UtilityTest.java diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java new file mode 100644 index 00000000..93f3b5c9 --- /dev/null +++ b/src/test/java/UtilityTest.java @@ -0,0 +1,26 @@ +import java.util.HashSet; +import org.junit.jupiter.api.Test; +import org.assertj.core.api.Assertions; + +class UtilityTest { + + @Test + void testMakeRandomNumbers() { + // 난수를 체크하므로 충분히 많은 테스트를 반복 + for (int i = 0; i < 1000; i++) { + int[] randomNumbers = Utility.makeRandomNumbers(); + + Assertions.assertThat(randomNumbers).hasSize(3); + + HashSet tempSet = new HashSet<>(); + for (int randomNumber : randomNumbers) { + tempSet.add(randomNumber); + } + Assertions.assertThat(randomNumbers).hasSize(tempSet.size()); + + for (int j = 0; j < 3; j++) { + Assertions.assertThat(randomNumbers[j]).isBetween(0, 9); + } + } + } +} \ No newline at end of file From ce4f2990649b065d864e6f793648e9c1cd209de4 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 16:23:52 +0900 Subject: [PATCH 05/31] feat(Utility): add method of getting user numbers as string --- src/main/java/Utility.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 6ff8c889..dafcaa6c 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -1,3 +1,5 @@ +import java.util.Scanner; + public class Utility { private Utility() { @@ -21,4 +23,13 @@ public static int[] makeRandomNumbers() { return randomNumbers; } + + public static String getUserNumbers() { + System.out.print("숫자를 입력해 주세요 : "); + + Scanner sc = new Scanner(System.in); + String userNumbers = sc.nextLine(); + + return userNumbers; + } } From c1bd04b6eb5de81e98169aa9c612d8ea289d1f75 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 16:40:44 +0900 Subject: [PATCH 06/31] refactor(getUserString()): rename userNumbers to userString --- src/main/java/Utility.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index dafcaa6c..8505616c 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -24,12 +24,12 @@ public static int[] makeRandomNumbers() { return randomNumbers; } - public static String getUserNumbers() { + public static String getUserString() { System.out.print("숫자를 입력해 주세요 : "); Scanner sc = new Scanner(System.in); - String userNumbers = sc.nextLine(); + String userString = sc.nextLine(); - return userNumbers; + return userString; } } From 3f377e6525eb31d538a345645b6a012ce31e44e5 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 17:07:42 +0900 Subject: [PATCH 07/31] feat(Utility): add method of checking string length --- src/main/java/Utility.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 8505616c..1f7c44ab 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -32,4 +32,9 @@ public static String getUserString() { return userString; } + + public static boolean checkStringLength(String userString) { + return userString.length() == 3; + } + } From a28b39b5297e1574f67b1c96cfac266068ea80f9 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 17:17:32 +0900 Subject: [PATCH 08/31] test(UtilityTest): add checking string length test --- src/test/java/UtilityTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index 93f3b5c9..e4640984 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -23,4 +23,14 @@ void testMakeRandomNumbers() { } } } + + @Test + void testCheckStringLength() { + Assertions.assertThat(Utility.checkStringLength("123")).isTrue(); + Assertions.assertThat(Utility.checkStringLength("12")).isFalse(); + Assertions.assertThat(Utility.checkStringLength("1")).isFalse(); + Assertions.assertThat(Utility.checkStringLength("")).isFalse(); + Assertions.assertThat(Utility.checkStringLength("1234")).isFalse(); + Assertions.assertThat(Utility.checkStringLength("12 45")).isFalse(); + } } \ No newline at end of file From 11e066af978738e152571e01f8da9c11a810b509 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 17:24:37 +0900 Subject: [PATCH 09/31] feat(Utility): add method of checking distinct string --- src/main/java/Utility.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 1f7c44ab..a0c3c338 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Scanner; public class Utility { @@ -37,4 +38,14 @@ public static boolean checkStringLength(String userString) { return userString.length() == 3; } + public static boolean checkStringDistinct(String userString) { + HashSet tempSet = new HashSet<>(); + + for (int i = 0; i < userString.length(); i++) { + tempSet.add(userString.charAt(i)); + } + + return tempSet.size() == userString.length(); + } + } From 52297d9b836a0a991b6a8fd10e1f0684e8f6a387 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 17:25:32 +0900 Subject: [PATCH 10/31] test(UtilityTest): add checking distinct string test --- src/test/java/UtilityTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index e4640984..04a06cea 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -33,4 +33,14 @@ void testCheckStringLength() { Assertions.assertThat(Utility.checkStringLength("1234")).isFalse(); Assertions.assertThat(Utility.checkStringLength("12 45")).isFalse(); } + + @Test + void testCheckStringDistinct() { + Assertions.assertThat(Utility.checkStringDistinct("123")).isTrue(); + Assertions.assertThat(Utility.checkStringDistinct("abc")).isTrue(); + Assertions.assertThat(Utility.checkStringDistinct("123456789")).isTrue(); + Assertions.assertThat(Utility.checkStringDistinct("112")).isFalse(); + Assertions.assertThat(Utility.checkStringDistinct("aaa")).isFalse(); + Assertions.assertThat(Utility.checkStringDistinct("99")).isFalse(); + } } \ No newline at end of file From aa0caa69ed93f8895a0498b2c0da7d9ff495b0ff Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 22:27:00 +0900 Subject: [PATCH 11/31] feat(Utility): add method of checking string consisted of digit --- src/main/java/Utility.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index a0c3c338..83d3d7af 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -48,4 +48,13 @@ public static boolean checkStringDistinct(String userString) { return tempSet.size() == userString.length(); } + public static void checkStringDigit(String userString) { + for (int i = 0; i < userString.length(); i++) { + int temp = (int) userString.charAt(i) - (int) '0'; + if (temp < 0 || temp > 9) { + throw new IllegalArgumentException(); + } + } + } + } From 7e89330f3f51694cceb4f484ba21acf45f6c73fa Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 22:28:32 +0900 Subject: [PATCH 12/31] feat(UtilityTest): checking string consisted of digit test --- src/test/java/UtilityTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index 04a06cea..afa9c69a 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -43,4 +43,14 @@ void testCheckStringDistinct() { Assertions.assertThat(Utility.checkStringDistinct("aaa")).isFalse(); Assertions.assertThat(Utility.checkStringDistinct("99")).isFalse(); } + + @Test + void testCheckStringDigit() { + Assertions.assertThatCode(() -> Utility.checkStringDigit("123")).doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringDigit("123456789101112")).doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringDigit("")).doesNotThrowAnyException(); + Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("1k3")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("abc")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("111111111a")).isInstanceOf(IllegalArgumentException.class); + } } \ No newline at end of file From 91ee117b021aa6a90f1f3ea82c67fd08a39b6fbb Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 22:33:21 +0900 Subject: [PATCH 13/31] refactor(Utility): refactor checking functions to void type --- src/main/java/Utility.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 83d3d7af..44a5432e 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -34,18 +34,22 @@ public static String getUserString() { return userString; } - public static boolean checkStringLength(String userString) { - return userString.length() == 3; + public static void checkStringLength(String userString) { + if (userString.length() != 3) { + throw new IllegalArgumentException(); + } } - public static boolean checkStringDistinct(String userString) { + public static void checkStringDistinct(String userString) { HashSet tempSet = new HashSet<>(); for (int i = 0; i < userString.length(); i++) { tempSet.add(userString.charAt(i)); } - return tempSet.size() == userString.length(); + if (tempSet.size() != userString.length()) { + throw new IllegalArgumentException(); + } } public static void checkStringDigit(String userString) { From f1dca13786277903ef382b4672dc7ac595455cf6 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 22:36:59 +0900 Subject: [PATCH 14/31] refactor(UtilityTest): refactor checking string tests --- src/test/java/UtilityTest.java | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index afa9c69a..394003ba 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -26,22 +26,22 @@ void testMakeRandomNumbers() { @Test void testCheckStringLength() { - Assertions.assertThat(Utility.checkStringLength("123")).isTrue(); - Assertions.assertThat(Utility.checkStringLength("12")).isFalse(); - Assertions.assertThat(Utility.checkStringLength("1")).isFalse(); - Assertions.assertThat(Utility.checkStringLength("")).isFalse(); - Assertions.assertThat(Utility.checkStringLength("1234")).isFalse(); - Assertions.assertThat(Utility.checkStringLength("12 45")).isFalse(); + Assertions.assertThatCode(() -> Utility.checkStringLength("123")).doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringLength("abc")).doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringLength("000")).doesNotThrowAnyException(); + Assertions.assertThatThrownBy(() -> Utility.checkStringLength("")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringLength("11111")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringLength("zbcda")).isInstanceOf(IllegalArgumentException.class); } @Test void testCheckStringDistinct() { - Assertions.assertThat(Utility.checkStringDistinct("123")).isTrue(); - Assertions.assertThat(Utility.checkStringDistinct("abc")).isTrue(); - Assertions.assertThat(Utility.checkStringDistinct("123456789")).isTrue(); - Assertions.assertThat(Utility.checkStringDistinct("112")).isFalse(); - Assertions.assertThat(Utility.checkStringDistinct("aaa")).isFalse(); - Assertions.assertThat(Utility.checkStringDistinct("99")).isFalse(); + Assertions.assertThatCode(() -> Utility.checkStringDistinct("123")).doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringDistinct("123456789")).doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringDistinct("abc")).doesNotThrowAnyException(); + Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("111")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("12345678910")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("aa")).isInstanceOf(IllegalArgumentException.class); } @Test From aab3d6f52b898c2ecfb200039d8f3690f85faa5b Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 22:43:39 +0900 Subject: [PATCH 15/31] feat(Utility): add method of changing string to int array --- src/main/java/Utility.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 44a5432e..e148362a 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -61,4 +61,14 @@ public static void checkStringDigit(String userString) { } } + public static int[] stringToIntArray(String userString) { + int[] userNumbers = new int[3]; + + for (int i = 0; i < 3; i++) { + int temp = (int) userString.charAt(i) - (int) '0'; + userNumbers[i] = temp; + } + + return userNumbers; + } } From 64cf5935afd1112325bc53471251573315104244 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 22:46:20 +0900 Subject: [PATCH 16/31] test(UtilityTest): add string to int array test --- src/test/java/UtilityTest.java | 58 ++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index 394003ba..54dfb577 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -26,31 +26,57 @@ void testMakeRandomNumbers() { @Test void testCheckStringLength() { - Assertions.assertThatCode(() -> Utility.checkStringLength("123")).doesNotThrowAnyException(); - Assertions.assertThatCode(() -> Utility.checkStringLength("abc")).doesNotThrowAnyException(); - Assertions.assertThatCode(() -> Utility.checkStringLength("000")).doesNotThrowAnyException(); - Assertions.assertThatThrownBy(() -> Utility.checkStringLength("")).isInstanceOf(IllegalArgumentException.class); - Assertions.assertThatThrownBy(() -> Utility.checkStringLength("11111")).isInstanceOf(IllegalArgumentException.class); - Assertions.assertThatThrownBy(() -> Utility.checkStringLength("zbcda")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatCode(() -> Utility.checkStringLength("123")) + .doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringLength("abc")) + .doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringLength("000")) + .doesNotThrowAnyException(); + Assertions.assertThatThrownBy(() -> Utility.checkStringLength("")) + .isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringLength("11111")) + .isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringLength("zbcda")) + .isInstanceOf(IllegalArgumentException.class); } @Test void testCheckStringDistinct() { - Assertions.assertThatCode(() -> Utility.checkStringDistinct("123")).doesNotThrowAnyException(); - Assertions.assertThatCode(() -> Utility.checkStringDistinct("123456789")).doesNotThrowAnyException(); - Assertions.assertThatCode(() -> Utility.checkStringDistinct("abc")).doesNotThrowAnyException(); - Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("111")).isInstanceOf(IllegalArgumentException.class); - Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("12345678910")).isInstanceOf(IllegalArgumentException.class); - Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("aa")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatCode(() -> Utility.checkStringDistinct("123")) + .doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringDistinct("123456789")) + .doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringDistinct("abc")) + .doesNotThrowAnyException(); + Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("111")) + .isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("12345678910")) + .isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDistinct("aa")) + .isInstanceOf(IllegalArgumentException.class); } @Test void testCheckStringDigit() { Assertions.assertThatCode(() -> Utility.checkStringDigit("123")).doesNotThrowAnyException(); - Assertions.assertThatCode(() -> Utility.checkStringDigit("123456789101112")).doesNotThrowAnyException(); + Assertions.assertThatCode(() -> Utility.checkStringDigit("123456789101112")) + .doesNotThrowAnyException(); Assertions.assertThatCode(() -> Utility.checkStringDigit("")).doesNotThrowAnyException(); - Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("1k3")).isInstanceOf(IllegalArgumentException.class); - Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("abc")).isInstanceOf(IllegalArgumentException.class); - Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("111111111a")).isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("1k3")) + .isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("abc")) + .isInstanceOf(IllegalArgumentException.class); + Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("111111111a")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void testStringToIntArray() { + int[] temp1 = {1, 2, 3}; + Assertions.assertThat(Utility.stringToIntArray("123")).isEqualTo(temp1); + int[] temp2 = {5, 4, 3}; + Assertions.assertThat(Utility.stringToIntArray("543")).isEqualTo(temp2); + int[] temp3 = {2, 4, 6}; + Assertions.assertThat(Utility.stringToIntArray("246")).isEqualTo(temp3); } } \ No newline at end of file From 1e949dada041b90c904903e6311301068f841303 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 22:59:34 +0900 Subject: [PATCH 17/31] feat(Utility): add method of checking count of strike --- src/main/java/Utility.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index e148362a..06659767 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -71,4 +71,16 @@ public static int[] stringToIntArray(String userString) { return userNumbers; } + + public static int checkStrikeCount(int[] randomNumbers, int[] userNumbers) { + int strike = 0; + + for (int i = 0; i < 3; i++) { + if (randomNumbers[i] == userNumbers[i]) { + strike += 1; + } + } + + return strike; + } } From b852ffdc012b517c3505372894aee1401e8278c7 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:04:21 +0900 Subject: [PATCH 18/31] add strike count test --- src/test/java/UtilityTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index 54dfb577..a727a146 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -79,4 +79,17 @@ void testStringToIntArray() { int[] temp3 = {2, 4, 6}; Assertions.assertThat(Utility.stringToIntArray("246")).isEqualTo(temp3); } + + @Test + void testStrikeCount() { + int[] randomNumbers = {1, 2, 3}; + int[] userNumbers = {1, 2, 3}; + Assertions.assertThat(Utility.strikeCount(randomNumbers, userNumbers)).isEqualTo(3); + int[] userNumbers2 = {1, 4, 3}; + Assertions.assertThat(Utility.strikeCount(randomNumbers, userNumbers2)).isEqualTo(2); + int[] userNumbers3 = {1, 4, 5}; + Assertions.assertThat(Utility.strikeCount(randomNumbers, userNumbers3)).isEqualTo(1); + int[] userNumbers4 = {2, 4, 5}; + Assertions.assertThat(Utility.strikeCount(randomNumbers, userNumbers4)).isEqualTo(0); + } } \ No newline at end of file From fcd80010581813331e5eb121cab722333c55b302 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:05:51 +0900 Subject: [PATCH 19/31] refactor(Utility): rename method of strike counter --- src/main/java/Utility.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 06659767..e30657be 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -72,7 +72,7 @@ public static int[] stringToIntArray(String userString) { return userNumbers; } - public static int checkStrikeCount(int[] randomNumbers, int[] userNumbers) { + public static int strikeCount(int[] randomNumbers, int[] userNumbers) { int strike = 0; for (int i = 0; i < 3; i++) { From 4067e0d199b14aa3c7ce4aa6d1515a9c61793b40 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:08:04 +0900 Subject: [PATCH 20/31] fix(checkStringDigit): fix a range of temp --- src/main/java/Utility.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index e30657be..399a111e 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -55,7 +55,7 @@ public static void checkStringDistinct(String userString) { public static void checkStringDigit(String userString) { for (int i = 0; i < userString.length(); i++) { int temp = (int) userString.charAt(i) - (int) '0'; - if (temp < 0 || temp > 9) { + if (temp < 1 || temp > 9) { throw new IllegalArgumentException(); } } From 63b0f597dda3d17b93798f6c5f9a3f3127bd5983 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:13:57 +0900 Subject: [PATCH 21/31] fix(testCheckStringDigit): fix the test because of fixing range --- src/test/java/UtilityTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index a727a146..e4b0b6ae 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -59,14 +59,14 @@ void testCheckStringDistinct() { @Test void testCheckStringDigit() { Assertions.assertThatCode(() -> Utility.checkStringDigit("123")).doesNotThrowAnyException(); - Assertions.assertThatCode(() -> Utility.checkStringDigit("123456789101112")) + Assertions.assertThatCode(() -> Utility.checkStringDigit("12345678911112")) .doesNotThrowAnyException(); Assertions.assertThatCode(() -> Utility.checkStringDigit("")).doesNotThrowAnyException(); Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("1k3")) .isInstanceOf(IllegalArgumentException.class); Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("abc")) .isInstanceOf(IllegalArgumentException.class); - Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("111111111a")) + Assertions.assertThatThrownBy(() -> Utility.checkStringDigit("1111111110")) .isInstanceOf(IllegalArgumentException.class); } From 322ad72203ad99edf68a9e7ff3775e7c69afe2f1 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:15:11 +0900 Subject: [PATCH 22/31] fix(testMakeRandomNumbers): fix the test because of fixing range --- src/test/java/UtilityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index e4b0b6ae..2e76a613 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -19,7 +19,7 @@ void testMakeRandomNumbers() { Assertions.assertThat(randomNumbers).hasSize(tempSet.size()); for (int j = 0; j < 3; j++) { - Assertions.assertThat(randomNumbers[j]).isBetween(0, 9); + Assertions.assertThat(randomNumbers[j]).isBetween(1, 9); } } } From 37963164dfc50d7bfae1596d8d30bc80ff74cf64 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:27:04 +0900 Subject: [PATCH 23/31] feat(Utility): add method of count of ball --- src/main/java/Utility.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 399a111e..2cab6791 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -12,7 +12,7 @@ public static int[] makeRandomNumbers() { int count = 0; while (count < 3) { - int temp = (int) (Math.random() * 10); + int temp = (int) (Math.random() * 9) + 1; if (flags[temp]) { continue; } else { @@ -83,4 +83,20 @@ public static int strikeCount(int[] randomNumbers, int[] userNumbers) { return strike; } + + public static int ballCount(int[] randomNumbers, int[] userNumbers) { + int ball = 0; + + if (userNumbers[0] == randomNumbers[1] || userNumbers[0] == randomNumbers[2]) { + ball += 1; + } + if (userNumbers[1] == randomNumbers[0] || userNumbers[1] == randomNumbers[2]) { + ball += 1; + } + if (userNumbers[2] == randomNumbers[0] || userNumbers[2] == randomNumbers[1]) { + ball += 1; + } + + return ball; + } } From 022452bcaa08a7217ae8e81f78a6580f17a0ff93 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:28:35 +0900 Subject: [PATCH 24/31] test(UtilityTest): add ball count test --- src/test/java/UtilityTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index 2e76a613..58977db6 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -92,4 +92,17 @@ void testStrikeCount() { int[] userNumbers4 = {2, 4, 5}; Assertions.assertThat(Utility.strikeCount(randomNumbers, userNumbers4)).isEqualTo(0); } + + @Test + void testBallCount() { + int[] randomNumbers = {1, 2, 3}; + int[] userNumbers = {2, 3, 1}; + Assertions.assertThat(Utility.ballCount(randomNumbers, userNumbers)).isEqualTo(3); + int[] userNumbers2 = {1, 3, 2}; + Assertions.assertThat(Utility.ballCount(randomNumbers, userNumbers2)).isEqualTo(2); + int[] userNumbers3 = {5, 6, 1}; + Assertions.assertThat(Utility.ballCount(randomNumbers, userNumbers3)).isEqualTo(1); + int[] userNumbers4 = {1, 2, 3}; + Assertions.assertThat(Utility.ballCount(randomNumbers, userNumbers4)).isEqualTo(0); + } } \ No newline at end of file From 98e18478d732167cf7260396bf3373f6bcc631e7 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:40:53 +0900 Subject: [PATCH 25/31] feat(Utility): add method of getting strike and ball --- src/main/java/Utility.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 2cab6791..7b6a6c6c 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -99,4 +99,16 @@ public static int ballCount(int[] randomNumbers, int[] userNumbers) { return ball; } + + public static String getStrikeBall(int strike, int ball) { + if (strike == 0 && ball == 0) { + return "낫싱"; + } else if (ball == 0) { + return strike + "스트라이크"; + } else if (strike == 0) { + return ball + "볼"; + } else { + return ball + "볼 " + strike + "스트라이크"; + } + } } From 0664d2f40ba2e7e149a26725c18fc16f63a1bfb7 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Sun, 5 May 2024 23:41:53 +0900 Subject: [PATCH 26/31] test(UtilityTest): add strike and ball message test --- src/test/java/UtilityTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index 58977db6..770a97e9 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -105,4 +105,12 @@ void testBallCount() { int[] userNumbers4 = {1, 2, 3}; Assertions.assertThat(Utility.ballCount(randomNumbers, userNumbers4)).isEqualTo(0); } + + @Test + void testGetStrikeBall() { + Assertions.assertThat(Utility.getStrikeBall(3, 0)).isEqualTo("3스트라이크"); + Assertions.assertThat(Utility.getStrikeBall(1, 1)).isEqualTo("1볼 1스트라이크"); + Assertions.assertThat(Utility.getStrikeBall(0, 2)).isEqualTo("2볼"); + Assertions.assertThat(Utility.getStrikeBall(0, 0)).isEqualTo("낫싱"); + } } \ No newline at end of file From fed349990b22d7405e26a3759783368a82d80f54 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Mon, 6 May 2024 01:09:58 +0900 Subject: [PATCH 27/31] refactor(getUserString): delete print --- src/main/java/Utility.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 7b6a6c6c..9d0d92e3 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -26,8 +26,6 @@ public static int[] makeRandomNumbers() { } public static String getUserString() { - System.out.print("숫자를 입력해 주세요 : "); - Scanner sc = new Scanner(System.in); String userString = sc.nextLine(); From c6de763b8c67d9407fed3167d1da38ed04f4dd68 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Mon, 6 May 2024 01:12:32 +0900 Subject: [PATCH 28/31] feat(Utility): add method of print end message --- src/main/java/Utility.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 9d0d92e3..1d7cd057 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -109,4 +109,11 @@ public static String getStrikeBall(int strike, int ball) { return ball + "볼 " + strike + "스트라이크"; } } + + public static void printEndMessage(int strike) { + if (strike == 3) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + } + } } From e229468deb1cbe8ea90b7fa746357eb5698c8861 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Mon, 6 May 2024 01:32:07 +0900 Subject: [PATCH 29/31] feat(Utility): add method of checking continuous --- src/main/java/Utility.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java index 1d7cd057..7fe55eee 100644 --- a/src/main/java/Utility.java +++ b/src/main/java/Utility.java @@ -116,4 +116,18 @@ public static void printEndMessage(int strike) { System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); } } + + public static boolean checkKeepGoing(String userString, int strike) { + if (strike != 3) { + return true; + } else { + if (userString.equals("1")) { + return true; + } else if (userString.equals("2")) { + return false; + } else { + throw new IllegalArgumentException(); + } + } + } } From 7e8d94f6ff4bd038eb9b0caa468475cfe5b0a557 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Mon, 6 May 2024 01:32:45 +0900 Subject: [PATCH 30/31] test(UtilityTest): add checking continuous test --- src/test/java/UtilityTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/UtilityTest.java b/src/test/java/UtilityTest.java index 770a97e9..d0549b00 100644 --- a/src/test/java/UtilityTest.java +++ b/src/test/java/UtilityTest.java @@ -113,4 +113,12 @@ void testGetStrikeBall() { Assertions.assertThat(Utility.getStrikeBall(0, 2)).isEqualTo("2볼"); Assertions.assertThat(Utility.getStrikeBall(0, 0)).isEqualTo("낫싱"); } + + @Test + void testCheckKeepGoing() { + Assertions.assertThat(Utility.checkKeepGoing("1", 3)).isTrue(); + Assertions.assertThat(Utility.checkKeepGoing("234", 2)).isTrue(); + Assertions.assertThat(Utility.checkKeepGoing("2", 3)).isFalse(); + Assertions.assertThatThrownBy(() -> Utility.checkKeepGoing("3", 3)).isInstanceOf(IllegalArgumentException.class); + } } \ No newline at end of file From f05f16e284cabeecd9bb4d073ad60bcb864fa1c7 Mon Sep 17 00:00:00 2001 From: kangwlgns Date: Mon, 6 May 2024 10:08:20 +0900 Subject: [PATCH 31/31] feat(Application): add main application --- src/main/java/Application.java | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index 7200c38f..981f0b47 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -1,6 +1,51 @@ public class Application { + public static void main(String[] args) { + boolean continueFlag = true; + boolean makeFlag = true; + int[] randomNumbers = Utility.makeRandomNumbers(); + int[] userNumbers; + + while (continueFlag) { + if (makeFlag) { + makeFlag = false; + randomNumbers = Utility.makeRandomNumbers(); + } + + System.out.print("숫자를 입력해 주세요 : "); + String userString = Utility.getUserString(); + + try { + Utility.checkStringLength(userString); + Utility.checkStringDistinct(userString); + Utility.checkStringDigit(userString); + } catch (IllegalArgumentException e) { + System.out.print("올바르지 않은 입력입니다. "); + break; + } + + userNumbers = Utility.stringToIntArray(userString); + + int strike = Utility.strikeCount(randomNumbers, userNumbers); + int ball = Utility.ballCount(randomNumbers, userNumbers); + + System.out.println(Utility.getStrikeBall(strike, ball)); + + Utility.printEndMessage(strike); + + if (strike == 3) { + userString = Utility.getUserString(); + makeFlag = true; + } + try { + continueFlag = Utility.checkKeepGoing(userString, strike); + } catch (IllegalArgumentException e) { + System.out.print("올바르지 않은 입력입니다. "); + break; + } + } + System.out.println("애플리케이션을 종료합니다."); } }