From c867c8a817a82527dabefaba7108d8fa8299a4f3 Mon Sep 17 00:00:00 2001 From: heopaka <94223526+heoseungjun@users.noreply.github.com> Date: Wed, 6 Mar 2024 00:04:13 +0900 Subject: [PATCH 1/7] =?UTF-8?q?StringTest=20=EB=B0=8F=20SetCollectionTest?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=20(#5237)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/SetCollectionTest.java | 42 +++++++++++++++++++++++++ src/test/java/StringTest.java | 46 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/test/java/SetCollectionTest.java create mode 100644 src/test/java/StringTest.java diff --git a/src/test/java/SetCollectionTest.java b/src/test/java/SetCollectionTest.java new file mode 100644 index 00000000000..7f6eeea071d --- /dev/null +++ b/src/test/java/SetCollectionTest.java @@ -0,0 +1,42 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SetCollectionTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @Test + void sizeTest() { + assertThat(numbers).hasSize(numbers.size()); + } + + @ParameterizedTest + @ValueSource(ints = {1,2,3}) + void containsTest1(int num) { + assertThat(numbers.contains(num)).isTrue(); + } + + @ParameterizedTest + @CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':') + void containsTest2(int num, boolean result) { + assertThat(numbers.contains(num)).isEqualTo(result); + } + + +} diff --git a/src/test/java/StringTest.java b/src/test/java/StringTest.java new file mode 100644 index 00000000000..20af964ae55 --- /dev/null +++ b/src/test/java/StringTest.java @@ -0,0 +1,46 @@ +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.stream.IntStream; + +import static org.assertj.core.api.Assertions.*; + +public class StringTest { + @Test + void splitTest1() { + String[] value = "1,2".split(","); + assertThat(value).contains("1"); + assertThat(value).containsExactly("1","2"); + } + + @Test + void splitTest2() { + String[] value = "1".split(","); + assertThat(value).contains("1"); + assertThat(value).containsExactly("1"); + } + + @ParameterizedTest + @ValueSource(strings = {"(1,2)"}) + void substringTest(String text) { + String value = text.substring(1, text.indexOf(")")); + assertThat(value).isEqualTo("1,2"); + } + + @DisplayName("문자열에서 특정 위치의 문자를 확인하고 인덱스를 벗어나면 오류를 발생시킨다") + @ParameterizedTest + @ValueSource(strings = {"abc"}) + void charAtTest(String text) { + IntStream.range(0, text.length()).forEach(index -> { + if (text.charAt(index) == 0) { + assertThatThrownBy(() -> { + throw new StringIndexOutOfBoundsException(); + }).isInstanceOf(IndexOutOfBoundsException.class) + .hasMessageContaining("Index: " + index + + " Size: " + text.length()); + } + }); + } +} \ No newline at end of file From a5374135b281987a83b8b5208cc35432cbf7ea21 Mon Sep 17 00:00:00 2001 From: gjtmdwns Date: Thu, 7 Mar 2024 09:58:15 +0900 Subject: [PATCH 2/7] =?UTF-8?q?StringTest=20charAt=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/StringTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/StringTest.java b/src/test/java/StringTest.java index 20af964ae55..d6c7239e162 100644 --- a/src/test/java/StringTest.java +++ b/src/test/java/StringTest.java @@ -34,7 +34,7 @@ void substringTest(String text) { @ValueSource(strings = {"abc"}) void charAtTest(String text) { IntStream.range(0, text.length()).forEach(index -> { - if (text.charAt(index) == 0) { + if (index > text.length()) { assertThatThrownBy(() -> { throw new StringIndexOutOfBoundsException(); }).isInstanceOf(IndexOutOfBoundsException.class) From 6e9c8fa1f07e01d36376cdaac4a15680890fbe55 Mon Sep 17 00:00:00 2001 From: gjtmdwns Date: Mon, 11 Mar 2024 18:03:53 +0900 Subject: [PATCH 3/7] =?UTF-8?q?2=EB=8B=A8=EA=B3=84=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EC=97=B4=20=EB=8D=A7=EC=85=88=20=EA=B3=84=EC=82=B0=EA=B8=B0=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20:=20Caculator.java=20=EB=B0=8F=20StringCal?= =?UTF-8?q?culator=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 78 ++++++++++++++++++++++++++ src/test/java/StringCalculateTest.java | 44 +++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/main/java/Calculator.java create mode 100644 src/test/java/StringCalculateTest.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 00000000000..b84310b2fa7 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,78 @@ +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Calculator { + public static int calculate(String text) { + if (isTextEmpty(text)) { + return 0; + } + if (isContainsSlash(text)) { + return addText(splitTextByCustomByDelimiter(text)); + } + if (isContainsCommaOrColon(text)) { + return addText(splitText(text)); + } + return stringToInt(text); + } + + // text가 빈 문자열인지 확인 + private static boolean isTextEmpty(String text) { + return text.isEmpty(); + } + + private static boolean isContainsCommaOrColon(String text) { + return text.contains(",") || text.contains(":"); + } + + private static boolean isContainsSlash(String text) { + return text.contains("/") && text.contains("\n"); + } + + private static String[] splitText(String text) { + return text.split(",|:"); + } + + private static String[] splitTextByCustomByDelimiter(String text) { + Matcher m = findPattern(text); + if (canFindPattern(m)) { + return findNumbers(m).split(findDelimiter(m)); + } + return null; + } + + private static Matcher findPattern(String text) { + return Pattern.compile("//(.)\n(.*)").matcher(text); + } + + private static String findNumbers(Matcher matcher) { + return matcher.group(2); + } + + private static String findDelimiter(Matcher matcher) { + return matcher.group(1); + } + + private static boolean canFindPattern(Matcher matcher) { + return matcher.find(); + } + + private static int stringToInt(String text) { + return Integer.parseInt(text); + } + + private static int addText(String[] splitted) { + int sum = 0; + for (int token : stringArrayToIntArray(splitted)) { + sum += token; + } + return sum; + } + + private static int[] stringArrayToIntArray(String [] results) { + int[] intArray = new int[results.length]; + for (int i=0; i { + throw new RuntimeException(); + }); + } +} From fd070e3ef449e620d4484fe6b41d20d8678b7ff6 Mon Sep 17 00:00:00 2001 From: gjtmdwns Date: Mon, 11 Mar 2024 21:24:15 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EB=8D=A7?= =?UTF-8?q?=EC=85=88=20=EA=B3=84=EC=82=B0=EA=B8=B0=201=EC=B0=A8=20?= =?UTF-8?q?=ED=94=BC=EB=93=9C=EB=B0=B1=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/{ => calculator}/Calculator.java | 42 ++++++++++++------- .../{ => calculator}/StringCalculateTest.java | 8 ++-- .../java/{ => study}/SetCollectionTest.java | 2 + src/test/java/{ => study}/StringTest.java | 2 + 4 files changed, 36 insertions(+), 18 deletions(-) rename src/main/java/{ => calculator}/Calculator.java (55%) rename src/test/java/{ => calculator}/StringCalculateTest.java (86%) rename src/test/java/{ => study}/SetCollectionTest.java (98%) rename src/test/java/{ => study}/StringTest.java (98%) diff --git a/src/main/java/Calculator.java b/src/main/java/calculator/Calculator.java similarity index 55% rename from src/main/java/Calculator.java rename to src/main/java/calculator/Calculator.java index b84310b2fa7..65fa0125bd6 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/calculator/Calculator.java @@ -1,15 +1,25 @@ +package calculator; + import java.util.regex.Matcher; import java.util.regex.Pattern; public class Calculator { + private static final String COMMA = ","; + private static final String COLON = ":"; + private static final String SLASH = "//"; + private static final String LINECHANGE = "\n"; + private static final Pattern COMPILTEDPATTERN = Pattern.compile(SLASH+"(.)"+LINECHANGE+"(.*)"); + private static final int FIRST = 1; + private static final int SECOND = 2; + public static int calculate(String text) { if (isTextEmpty(text)) { return 0; } - if (isContainsSlash(text)) { + if (isContainsCustom(text)) { return addText(splitTextByCustomByDelimiter(text)); } - if (isContainsCommaOrColon(text)) { + if (isContainsDelimiter(text)) { return addText(splitText(text)); } return stringToInt(text); @@ -20,36 +30,36 @@ private static boolean isTextEmpty(String text) { return text.isEmpty(); } - private static boolean isContainsCommaOrColon(String text) { - return text.contains(",") || text.contains(":"); + private static boolean isContainsDelimiter(String text) { + return text.contains(COMMA) || text.contains(COLON); } - private static boolean isContainsSlash(String text) { - return text.contains("/") && text.contains("\n"); + private static boolean isContainsCustom(String text) { + return text.contains(SLASH) && text.contains(LINECHANGE); } private static String[] splitText(String text) { - return text.split(",|:"); + return text.split(COMMA+"|"+COLON); } private static String[] splitTextByCustomByDelimiter(String text) { - Matcher m = findPattern(text); - if (canFindPattern(m)) { - return findNumbers(m).split(findDelimiter(m)); + Matcher matcher = findPattern(text); + if (canFindPattern(matcher)) { + return findNumbers(matcher).split(findDelimiter(matcher)); } return null; } private static Matcher findPattern(String text) { - return Pattern.compile("//(.)\n(.*)").matcher(text); + return COMPILTEDPATTERN.matcher(text); } private static String findNumbers(Matcher matcher) { - return matcher.group(2); + return matcher.group(SECOND); } private static String findDelimiter(Matcher matcher) { - return matcher.group(1); + return matcher.group(FIRST); } private static boolean canFindPattern(Matcher matcher) { @@ -57,7 +67,11 @@ private static boolean canFindPattern(Matcher matcher) { } private static int stringToInt(String text) { - return Integer.parseInt(text); + int value = Integer.parseInt(text); + if (value < 0) { + throw new RuntimeException(); + } + return value; } private static int addText(String[] splitted) { diff --git a/src/test/java/StringCalculateTest.java b/src/test/java/calculator/StringCalculateTest.java similarity index 86% rename from src/test/java/StringCalculateTest.java rename to src/test/java/calculator/StringCalculateTest.java index 14228aa50ff..4d26bb50be7 100644 --- a/src/test/java/StringCalculateTest.java +++ b/src/test/java/calculator/StringCalculateTest.java @@ -1,3 +1,5 @@ +package calculator; + import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -36,9 +38,7 @@ public class StringCalculateTest { @Test void 음수_값() { - int value = Calculator.calculate("-1"); - assertThatThrownBy(() -> { - throw new RuntimeException(); - }); + assertThatThrownBy(() -> Calculator.calculate("-1,2,3")) + .isInstanceOf(RuntimeException.class); } } diff --git a/src/test/java/SetCollectionTest.java b/src/test/java/study/SetCollectionTest.java similarity index 98% rename from src/test/java/SetCollectionTest.java rename to src/test/java/study/SetCollectionTest.java index 7f6eeea071d..e5798e1019c 100644 --- a/src/test/java/SetCollectionTest.java +++ b/src/test/java/study/SetCollectionTest.java @@ -1,3 +1,5 @@ +package study; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; diff --git a/src/test/java/StringTest.java b/src/test/java/study/StringTest.java similarity index 98% rename from src/test/java/StringTest.java rename to src/test/java/study/StringTest.java index d6c7239e162..e07872ebb9d 100644 --- a/src/test/java/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,3 +1,5 @@ +package study; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; From ed3a07b3a3eeef2b3c75caf140ed260e0bc67969 Mon Sep 17 00:00:00 2001 From: gjtmdwns Date: Mon, 11 Mar 2024 22:15:11 +0900 Subject: [PATCH 5/7] =?UTF-8?q?.gitkeep=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/.gitkeep diff --git a/src/main/java/.gitkeep b/src/main/java/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 From ae1be47e649f5e391780ec10a4de8f3e6b2f3b8c Mon Sep 17 00:00:00 2001 From: heopaka <94223526+heoseungjun@users.noreply.github.com> Date: Tue, 12 Mar 2024 07:44:39 +0900 Subject: [PATCH 6/7] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 구현할 기능 목록 작성 --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c550c4c2a09..afde687dc9b 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,14 @@ * 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다. ## 온라인 코드 리뷰 과정 -* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview) \ No newline at end of file +* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview) + +## To-Do +* Scanner를 이용하여 자동차 대수 입력 +* Scanner를 이용하여 시도 횟수 입력 +* Random 클래스의 nextInt(10)을 이용하여 랜덤값 추출 +* 랜덤값을 기준으로 전진/정지 결정 +* 자동차마다 전진 횟수 기록 +* 경주 결과 출력 + +## Done From 6b18ef930a531a50052d2c72e3c5c05d66638572 Mon Sep 17 00:00:00 2001 From: heopaka <94223526+heoseungjun@users.noreply.github.com> Date: Tue, 12 Mar 2024 19:31:18 +0900 Subject: [PATCH 7/7] To-Do -> Done MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scanner를 이용한 자동차 대수 및 시도 횟수 입력 Random을 이용한 랜덤값 추출 자동차 전진 정보를 담는 리스트 생성 랜덤값 기준으로 자동차 전진/정지 결정(전진 시 전진 횟수 증가) 경주 결과 출력 --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index afde687dc9b..da49a7f353c 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,12 @@ * [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview) ## To-Do + +## Done * Scanner를 이용하여 자동차 대수 입력 * Scanner를 이용하여 시도 횟수 입력 * Random 클래스의 nextInt(10)을 이용하여 랜덤값 추출 -* 랜덤값을 기준으로 전진/정지 결정 -* 자동차마다 전진 횟수 기록 +* 자동차의 전진 정보를 담는 리스트 생성 +* 랜덤값을 기준으로 자동차 전진/정지 결정 +* 리스트의 자동차마다 전진 횟수 증가시키기 * 경주 결과 출력 - -## Done