From 99d2be8474af61b5fd340a5407e4be2061cdd2d4 Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Sat, 8 Jul 2023 23:00:15 +0900 Subject: [PATCH 01/16] Stringtest, SetTest commit --- build.gradle | 3 +- src/test/java/study/SetTest.java | 57 +++++++++++++++++++++++++++++ src/test/java/study/StringTest.java | 31 ++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/test/java/study/SetTest.java diff --git a/build.gradle b/build.gradle index 8172fb7..1cdbe77 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'eclipse' group = 'camp.nextstep' version = '1.0.0' -sourceCompatibility = '1.8' +sourceCompatibility = "11" repositories { mavenCentral() @@ -17,3 +17,4 @@ dependencies { test { useJUnitPlatform() } +targetCompatibility = JavaVersion.VERSION_11 diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java new file mode 100644 index 0000000..242b23d --- /dev/null +++ b/src/test/java/study/SetTest.java @@ -0,0 +1,57 @@ +package study; + +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; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SetTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @Test + void setsize() { + int result = numbers.size(); + assertEquals(result, 3); // Set은 중복을 허용하지 않는다. + } + + @Test + void contains() { + assertThat(numbers.contains(1)).isTrue(); + assertThat(numbers.contains(2)).isTrue(); + assertThat(numbers.contains(3)).isTrue(); + } + + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + void contains(int value) { + assertThat(numbers.contains(value)).isTrue(); + } + + @ParameterizedTest + @CsvSource({ + "1, true", + "2, true", + "3, true", + "4, false", + "5, false" + }) + void contains(int value, boolean expected) { + assertThat(numbers.contains(value)).isEqualTo(expected); + } +} \ No newline at end of file diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index 43e47d9..0c2a433 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,8 +1,11 @@ package study; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class StringTest { @Test @@ -10,4 +13,32 @@ void replace() { String actual = "abc".replace("b", "d"); assertThat(actual).isEqualTo("adc"); } + // 요구사항 1 + @Test + public void split() { + String[] values = "1,2".split(","); + assertThat(values).containsExactly("1", "2"); + values = "1".split(","); + assertThat(values).contains("1"); + } + @Test + public void substring() { + String input = "(1,2)"; + String result = input.substring(1, input.length() - 1); //문자열 인덱스 1부터 input의 길이 - 1 즉 2까지. + assertThat(result).isEqualTo("1,2"); + } + @Test + @DisplayName("String의 특정 위치의 문자 가져오기") + public void getindexvalue() { + String values = "abc"; + assertEquals('a', values.charAt(0)); + } + + @Test + @DisplayName("위치 값을 벗어났을 때의 예외") + public void stringindexoutofboundsexception() { + String values = "abc"; + assertThrows(StringIndexOutOfBoundsException.class, () -> values.charAt(-1)); + assertThrows(StringIndexOutOfBoundsException.class, () -> values.charAt(3)); + } } From 9f98a5053c39817b06af73ce94acf9d265948767 Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Mon, 10 Jul 2023 13:56:22 +0900 Subject: [PATCH 02/16] =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84,=20=EB=AC=B8=EC=9E=90=EC=97=B4=EC=9D=84=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EB=B0=9B=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/{ => exam}/SetTest.java | 2 +- src/test/java/study/{ => exam}/StringTest.java | 2 +- src/test/java/study/tdd/README.md | 11 +++++++++++ src/test/java/study/tdd/StringCalculator.java | 15 +++++++++++++++ .../java/study/tdd/StringCalculatorTest.java | 16 ++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) rename src/test/java/study/{ => exam}/SetTest.java (98%) rename src/test/java/study/{ => exam}/StringTest.java (98%) create mode 100644 src/test/java/study/tdd/README.md create mode 100644 src/test/java/study/tdd/StringCalculator.java create mode 100644 src/test/java/study/tdd/StringCalculatorTest.java diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/exam/SetTest.java similarity index 98% rename from src/test/java/study/SetTest.java rename to src/test/java/study/exam/SetTest.java index 242b23d..4753831 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/exam/SetTest.java @@ -1,4 +1,4 @@ -package study; +package study.exam; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/exam/StringTest.java similarity index 98% rename from src/test/java/study/StringTest.java rename to src/test/java/study/exam/StringTest.java index 0c2a433..11c4976 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/exam/StringTest.java @@ -1,4 +1,4 @@ -package study; +package study.exam; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/study/tdd/README.md b/src/test/java/study/tdd/README.md new file mode 100644 index 0000000..0e65b82 --- /dev/null +++ b/src/test/java/study/tdd/README.md @@ -0,0 +1,11 @@ +### **요구사항** + +- 사용자가 입력한 문자열 값에 따라 사칙연산을 수행할 수 있는 계산기를 구현해야 한다. +- 문자열 계산기는 사칙연산의 계산 우선순위가 아닌 입력 값에 따라 계산 순서가 결정된다. 즉, 수학에서는 곱셈, 나눗셈이 덧셈, 뺄셈 보다 먼저 계산해야 하지만 이를 무시한다. +- 예를 들어 "2 + 3 * 4 / 2"와 같은 문자열을 입력할 경우 2 + 3 * 4 / 2 실행 결과인 10을 출력해야 한다. + +### **체크리스트** +- [ ] 문자열을 입력받는다. +- [ ] 0으로 나눴을 때 IllegalArgumentException을 터뜨린다. +- [ ] 문자열을 숫자와 기호로 파싱한다. +- [ ] 파싱한 문자열로 계산한다. \ No newline at end of file diff --git a/src/test/java/study/tdd/StringCalculator.java b/src/test/java/study/tdd/StringCalculator.java new file mode 100644 index 0000000..d10e7ba --- /dev/null +++ b/src/test/java/study/tdd/StringCalculator.java @@ -0,0 +1,15 @@ +package study.tdd; + +public class StringCalculator { + + private final String text; + + public StringCalculator(String text) { + this.text = text; + } + + public String getText() { + return text; + } + +} diff --git a/src/test/java/study/tdd/StringCalculatorTest.java b/src/test/java/study/tdd/StringCalculatorTest.java new file mode 100644 index 0000000..857b5d9 --- /dev/null +++ b/src/test/java/study/tdd/StringCalculatorTest.java @@ -0,0 +1,16 @@ +package study.tdd; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +public class StringCalculatorTest { + + @Test + public void 문자열을_입력받는다() { + String input = "2 + 3 * 4 / 2"; + + StringCalculator stringCalculator = new StringCalculator(input); + + Assertions.assertThat(stringCalculator.getText()); + } +} From 8d7e03830e5c3363f6be6d026e1c261acad6c002 Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Mon, 10 Jul 2023 13:56:46 +0900 Subject: [PATCH 03/16] =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84,=20=EB=AC=B8=EC=9E=90=EC=97=B4=EC=9D=84=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EB=B0=9B=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/tdd/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/study/tdd/README.md b/src/test/java/study/tdd/README.md index 0e65b82..5091206 100644 --- a/src/test/java/study/tdd/README.md +++ b/src/test/java/study/tdd/README.md @@ -5,7 +5,7 @@ - 예를 들어 "2 + 3 * 4 / 2"와 같은 문자열을 입력할 경우 2 + 3 * 4 / 2 실행 결과인 10을 출력해야 한다. ### **체크리스트** -- [ ] 문자열을 입력받는다. +- [x] 문자열을 입력받는다. - [ ] 0으로 나눴을 때 IllegalArgumentException을 터뜨린다. - [ ] 문자열을 숫자와 기호로 파싱한다. - [ ] 파싱한 문자열로 계산한다. \ No newline at end of file From 548c5a69121796b52e98aec28688dc7d250ff753 Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Mon, 10 Jul 2023 14:44:13 +0900 Subject: [PATCH 04/16] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=EC=9D=84=20?= =?UTF-8?q?=ED=8C=8C=EC=8B=B1=ED=95=98=EB=8A=94=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/tdd/README.md | 3 ++- src/test/java/study/tdd/StringCalculator.java | 5 +++++ src/test/java/study/tdd/StringCalculatorTest.java | 14 ++++++++++++++ src/test/java/study/tdd/StringParser.java | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/test/java/study/tdd/StringParser.java diff --git a/src/test/java/study/tdd/README.md b/src/test/java/study/tdd/README.md index 5091206..9222b4e 100644 --- a/src/test/java/study/tdd/README.md +++ b/src/test/java/study/tdd/README.md @@ -6,6 +6,7 @@ ### **체크리스트** - [x] 문자열을 입력받는다. +- [x] 문자열을 파싱한다. +- [ ] 문자열을 연산자끼리, 숫자끼리 리스트 별로 나눠준다. - [ ] 0으로 나눴을 때 IllegalArgumentException을 터뜨린다. -- [ ] 문자열을 숫자와 기호로 파싱한다. - [ ] 파싱한 문자열로 계산한다. \ No newline at end of file diff --git a/src/test/java/study/tdd/StringCalculator.java b/src/test/java/study/tdd/StringCalculator.java index d10e7ba..10fb601 100644 --- a/src/test/java/study/tdd/StringCalculator.java +++ b/src/test/java/study/tdd/StringCalculator.java @@ -1,11 +1,16 @@ package study.tdd; +import java.util.List; + public class StringCalculator { private final String text; + private final StringParser stringParser = new StringParser(); public StringCalculator(String text) { this.text = text; + StringParser stringParser = new StringParser(); + List parseText = stringParser.parse(text); } public String getText() { diff --git a/src/test/java/study/tdd/StringCalculatorTest.java b/src/test/java/study/tdd/StringCalculatorTest.java index 857b5d9..fccf7dc 100644 --- a/src/test/java/study/tdd/StringCalculatorTest.java +++ b/src/test/java/study/tdd/StringCalculatorTest.java @@ -3,6 +3,8 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.List; + public class StringCalculatorTest { @Test @@ -13,4 +15,16 @@ public class StringCalculatorTest { Assertions.assertThat(stringCalculator.getText()); } + + @Test + public void 문자열을_파싱한다() { + String input = "2 + 3 * 4 / 2"; + + StringParser stringParser = new StringParser(); + + List parser = stringParser.parse(input); + List stringList = List.of("2", "+", "3", "*", "4", "/", "2"); + + Assertions.assertThat(parser).isEqualTo(stringList); + } } diff --git a/src/test/java/study/tdd/StringParser.java b/src/test/java/study/tdd/StringParser.java new file mode 100644 index 0000000..6b0a7ac --- /dev/null +++ b/src/test/java/study/tdd/StringParser.java @@ -0,0 +1,14 @@ +package study.tdd; + +import java.util.List; + +public class StringParser { + + private List actual; + public List parse(String input) { + for (int i = 0; i < input.length(); i++) { + actual = List.of(input.split(" ")); + } + return actual; + } +} From 6c914e5bdafdbcbd855a18cace00751225ad65b0 Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Mon, 10 Jul 2023 20:23:28 +0900 Subject: [PATCH 05/16] =?UTF-8?q?0=EC=9C=BC=EB=A1=9C=20=EB=82=98=EB=88=B4?= =?UTF-8?q?=EC=9D=84=20=EB=95=8C=20=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/tdd/README.md | 2 +- src/test/java/study/tdd/StringCalculator.java | 29 ++++++++++++++++++- .../java/study/tdd/StringCalculatorTest.java | 10 +++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/test/java/study/tdd/README.md b/src/test/java/study/tdd/README.md index 9222b4e..42aec24 100644 --- a/src/test/java/study/tdd/README.md +++ b/src/test/java/study/tdd/README.md @@ -8,5 +8,5 @@ - [x] 문자열을 입력받는다. - [x] 문자열을 파싱한다. - [ ] 문자열을 연산자끼리, 숫자끼리 리스트 별로 나눠준다. -- [ ] 0으로 나눴을 때 IllegalArgumentException을 터뜨린다. +- [x] 0으로 나눴을 때 IllegalArgumentException을 터뜨린다. - [ ] 파싱한 문자열로 계산한다. \ No newline at end of file diff --git a/src/test/java/study/tdd/StringCalculator.java b/src/test/java/study/tdd/StringCalculator.java index 10fb601..ca4c26b 100644 --- a/src/test/java/study/tdd/StringCalculator.java +++ b/src/test/java/study/tdd/StringCalculator.java @@ -1,12 +1,14 @@ package study.tdd; +import java.util.Arrays; import java.util.List; public class StringCalculator { - private final String text; + private String text; private final StringParser stringParser = new StringParser(); + public StringCalculator() { } public StringCalculator(String text) { this.text = text; StringParser stringParser = new StringParser(); @@ -17,4 +19,29 @@ public String getText() { return text; } + public int calculate(String text) { + String[] values = text.split(" "); + // 0번째 인덱스에 있는 숫자만 넣어준다. + int number = Integer.parseInt(values[0]); + + for (int i = 1; i < values.length; i += 2) { + String operator = values[i]; + int operand = Integer.parseInt(values[i+1]); + + if(operator.equals("+")) + number += operand; + else if (operator.equals("-")) + number -= operand; + else if (operator.equals("*")) + number *= operand; + else if (operator.equals("/")) { + if (operand == 0) { + throw new IllegalArgumentException(); + } + number /= operand; + } + } + + return number; + } } diff --git a/src/test/java/study/tdd/StringCalculatorTest.java b/src/test/java/study/tdd/StringCalculatorTest.java index fccf7dc..23233b0 100644 --- a/src/test/java/study/tdd/StringCalculatorTest.java +++ b/src/test/java/study/tdd/StringCalculatorTest.java @@ -27,4 +27,14 @@ public class StringCalculatorTest { Assertions.assertThat(parser).isEqualTo(stringList); } + + @Test + public void 숫자_0으로_나누면_IllegalArgumentException() { + String input = "2 + 3 * 4 / 0"; + + Assertions.assertThatThrownBy(() -> { + StringCalculator stringCalculator = new StringCalculator(); + stringCalculator.calculate(input); + }).isInstanceOf(IllegalArgumentException.class); + } } From e262a8ef72a132caef25c9e181e22eb3cc81fe49 Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Mon, 10 Jul 2023 21:01:10 +0900 Subject: [PATCH 06/16] =?UTF-8?q?=EC=88=AB=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/tdd/README.md | 3 +- src/test/java/study/tdd/StringCalculator.java | 31 ++++++++++++------- .../java/study/tdd/StringCalculatorTest.java | 13 +++++++- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/test/java/study/tdd/README.md b/src/test/java/study/tdd/README.md index 42aec24..086690b 100644 --- a/src/test/java/study/tdd/README.md +++ b/src/test/java/study/tdd/README.md @@ -7,6 +7,5 @@ ### **체크리스트** - [x] 문자열을 입력받는다. - [x] 문자열을 파싱한다. -- [ ] 문자열을 연산자끼리, 숫자끼리 리스트 별로 나눠준다. - [x] 0으로 나눴을 때 IllegalArgumentException을 터뜨린다. -- [ ] 파싱한 문자열로 계산한다. \ No newline at end of file +- [x] 파싱한 문자열로 계산한다. \ No newline at end of file diff --git a/src/test/java/study/tdd/StringCalculator.java b/src/test/java/study/tdd/StringCalculator.java index ca4c26b..a383dac 100644 --- a/src/test/java/study/tdd/StringCalculator.java +++ b/src/test/java/study/tdd/StringCalculator.java @@ -1,6 +1,5 @@ package study.tdd; -import java.util.Arrays; import java.util.List; public class StringCalculator { @@ -11,22 +10,33 @@ public class StringCalculator { public StringCalculator() { } public StringCalculator(String text) { this.text = text; - StringParser stringParser = new StringParser(); - List parseText = stringParser.parse(text); } public String getText() { return text; } + public void exception(String text) { + List values = stringParser.parse(text); + + for (int i = 1; i < values.size(); i += 2) { + + String operator = values.get(i); + int operand = Integer.parseInt(values.get(i + 1)); + + if (operator.equals("/") && operand == 0) { + throw new IllegalArgumentException(); + } + } + } public int calculate(String text) { - String[] values = text.split(" "); - // 0번째 인덱스에 있는 숫자만 넣어준다. - int number = Integer.parseInt(values[0]); + List values = stringParser.parse(text); + int number = Integer.parseInt(values.get(0)); + + for (int i = 1; i < values.size(); i += 2) { - for (int i = 1; i < values.length; i += 2) { - String operator = values[i]; - int operand = Integer.parseInt(values[i+1]); + int operand = Integer.parseInt(values.get(i + 1)); + String operator = values.get(i); if(operator.equals("+")) number += operand; @@ -35,9 +45,6 @@ else if (operator.equals("-")) else if (operator.equals("*")) number *= operand; else if (operator.equals("/")) { - if (operand == 0) { - throw new IllegalArgumentException(); - } number /= operand; } } diff --git a/src/test/java/study/tdd/StringCalculatorTest.java b/src/test/java/study/tdd/StringCalculatorTest.java index 23233b0..730dd5b 100644 --- a/src/test/java/study/tdd/StringCalculatorTest.java +++ b/src/test/java/study/tdd/StringCalculatorTest.java @@ -34,7 +34,18 @@ public class StringCalculatorTest { Assertions.assertThatThrownBy(() -> { StringCalculator stringCalculator = new StringCalculator(); - stringCalculator.calculate(input); + stringCalculator.exception(input); }).isInstanceOf(IllegalArgumentException.class); } + + @Test + public void 계산() { + String input = "2 + 3 * 4 / 2"; + StringCalculator stringCalculator = new StringCalculator(input); + + int result = stringCalculator.calculate(input); + Assertions.assertThat(result).isEqualTo(10); + + } + } From 5fd4c3f5f190eeff772a9d86cca45525f4e5183e Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Wed, 12 Jul 2023 01:11:23 +0900 Subject: [PATCH 07/16] =?UTF-8?q?=EC=88=AB=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=201=EC=B0=A8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/tdd/StringCalculator.java | 46 ++++++++++++++++ src/main/java/study/tdd/StringParser.java | 14 +++++ src/test/java/study/exam/SetTest.java | 35 ++++++++++-- src/test/java/study/exam/StringTest.java | 9 ++-- src/test/java/study/tdd/StringCalculator.java | 54 ------------------- .../java/study/tdd/StringCalculatorTest.java | 26 +-------- src/test/java/study/tdd/StringParser.java | 14 ----- src/test/java/study/tdd/StringParserTest.java | 21 ++++++++ 8 files changed, 120 insertions(+), 99 deletions(-) create mode 100644 src/main/java/study/tdd/StringCalculator.java create mode 100644 src/main/java/study/tdd/StringParser.java delete mode 100644 src/test/java/study/tdd/StringCalculator.java delete mode 100644 src/test/java/study/tdd/StringParser.java create mode 100644 src/test/java/study/tdd/StringParserTest.java diff --git a/src/main/java/study/tdd/StringCalculator.java b/src/main/java/study/tdd/StringCalculator.java new file mode 100644 index 0000000..88ce26f --- /dev/null +++ b/src/main/java/study/tdd/StringCalculator.java @@ -0,0 +1,46 @@ +package study.tdd; + +import java.util.List; + +public class StringCalculator { + + + private static String text; + + public StringCalculator(String text) { + this.text = text; + } + + public String getText() { + return text; + } + + static int calculate() { + List values = StringParser.parse(text); + int number = Integer.parseInt(values.get(0)); + + for (int i = 1; i < values.size(); i += 2) { + + int operand = Integer.parseInt(values.get(i + 1)); + String operator = values.get(i); + + number = getNumber(number, operand, operator); + } + + return number; + } + + private static int getNumber(int number, int operand, String operator) { + if ("+".equals(operator)) return number + operand; + if ("-".equals(operator)) return number - operand; + if ("*".equals(operator)) return number * operand; + if ("/".equals(operator)) { + if (operand == 0) { + throw new IllegalArgumentException(); + } + return number / operand; + } + + throw new IllegalArgumentException("잘못된 연산자 입니다."); + } +} diff --git a/src/main/java/study/tdd/StringParser.java b/src/main/java/study/tdd/StringParser.java new file mode 100644 index 0000000..356405f --- /dev/null +++ b/src/main/java/study/tdd/StringParser.java @@ -0,0 +1,14 @@ +package study.tdd; + +import java.util.List; + +public class StringParser { + + public StringParser() { } + + static List parse(String input) { + return List.of(input.split(" ")); + // 알아둘 것. ArrayList로 만들면 가변 + null값 허용 + // List.of 메서드로 만들면 불변 + null값은 허용 X + } +} diff --git a/src/test/java/study/exam/SetTest.java b/src/test/java/study/exam/SetTest.java index 4753831..4f6dcbd 100644 --- a/src/test/java/study/exam/SetTest.java +++ b/src/test/java/study/exam/SetTest.java @@ -1,6 +1,7 @@ package study.exam; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -16,6 +17,7 @@ public class SetTest { private Set numbers; @BeforeEach + @DisplayName("HashSet에 값을 추가해주는 메소드 (값 중복 X)") void setUp() { numbers = new HashSet<>(); numbers.add(1); @@ -25,13 +27,22 @@ void setUp() { } @Test - void setsize() { + @DisplayName("집합의 크기를 확인하는 테스트") + void setSize() { int result = numbers.size(); assertEquals(result, 3); // Set은 중복을 허용하지 않는다. } @Test - void contains() { + @DisplayName("집합에 값이 포함되어있는지 비교하는 테스트") + void compareContains() { + numbers = new HashSet<>(); + + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + assertThat(numbers.contains(1)).isTrue(); assertThat(numbers.contains(2)).isTrue(); assertThat(numbers.contains(3)).isTrue(); @@ -39,7 +50,15 @@ void contains() { @ParameterizedTest @ValueSource(ints = {1, 2, 3}) - void contains(int value) { + @DisplayName("중복되지 않게 ParameterizedTest로 contains를 실행하는 코드를 한 줄로 줄인 테스트") + void compareContains(int value) { + numbers = new HashSet<>(); + + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + assertThat(numbers.contains(value)).isTrue(); } @@ -51,7 +70,15 @@ void contains(int value) { "4, false", "5, false" }) - void contains(int value, boolean expected) { + @DisplayName("CsvSource로 true 값만 비교하는 것이 아닌 false 값도 비교해보는 테스트") + void compareContains(int value, boolean expected) { + numbers = new HashSet<>(); + + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + assertThat(numbers.contains(value)).isEqualTo(expected); } } \ No newline at end of file diff --git a/src/test/java/study/exam/StringTest.java b/src/test/java/study/exam/StringTest.java index 11c4976..37fa189 100644 --- a/src/test/java/study/exam/StringTest.java +++ b/src/test/java/study/exam/StringTest.java @@ -9,12 +9,14 @@ public class StringTest { @Test + @DisplayName("abc에서 b를 d로 대체했을 때 같은지 테스트하는 코드") void replace() { String actual = "abc".replace("b", "d"); assertThat(actual).isEqualTo("adc"); } // 요구사항 1 @Test + @DisplayName("특정 문자열로 분리하는 테스트 코드") public void split() { String[] values = "1,2".split(","); assertThat(values).containsExactly("1", "2"); @@ -22,21 +24,22 @@ public void split() { assertThat(values).contains("1"); } @Test - public void substring() { + @DisplayName("특정 문자열까지 읽어서 비교하는 테스트 코드") + public void subString() { String input = "(1,2)"; String result = input.substring(1, input.length() - 1); //문자열 인덱스 1부터 input의 길이 - 1 즉 2까지. assertThat(result).isEqualTo("1,2"); } @Test @DisplayName("String의 특정 위치의 문자 가져오기") - public void getindexvalue() { + public void getIndexValue() { String values = "abc"; assertEquals('a', values.charAt(0)); } @Test @DisplayName("위치 값을 벗어났을 때의 예외") - public void stringindexoutofboundsexception() { + public void stringIndexOutOfBoundsException() { String values = "abc"; assertThrows(StringIndexOutOfBoundsException.class, () -> values.charAt(-1)); assertThrows(StringIndexOutOfBoundsException.class, () -> values.charAt(3)); diff --git a/src/test/java/study/tdd/StringCalculator.java b/src/test/java/study/tdd/StringCalculator.java deleted file mode 100644 index a383dac..0000000 --- a/src/test/java/study/tdd/StringCalculator.java +++ /dev/null @@ -1,54 +0,0 @@ -package study.tdd; - -import java.util.List; - -public class StringCalculator { - - private String text; - private final StringParser stringParser = new StringParser(); - - public StringCalculator() { } - public StringCalculator(String text) { - this.text = text; - } - - public String getText() { - return text; - } - - public void exception(String text) { - List values = stringParser.parse(text); - - for (int i = 1; i < values.size(); i += 2) { - - String operator = values.get(i); - int operand = Integer.parseInt(values.get(i + 1)); - - if (operator.equals("/") && operand == 0) { - throw new IllegalArgumentException(); - } - } - } - public int calculate(String text) { - List values = stringParser.parse(text); - int number = Integer.parseInt(values.get(0)); - - for (int i = 1; i < values.size(); i += 2) { - - int operand = Integer.parseInt(values.get(i + 1)); - String operator = values.get(i); - - if(operator.equals("+")) - number += operand; - else if (operator.equals("-")) - number -= operand; - else if (operator.equals("*")) - number *= operand; - else if (operator.equals("/")) { - number /= operand; - } - } - - return number; - } -} diff --git a/src/test/java/study/tdd/StringCalculatorTest.java b/src/test/java/study/tdd/StringCalculatorTest.java index 730dd5b..14e9a6e 100644 --- a/src/test/java/study/tdd/StringCalculatorTest.java +++ b/src/test/java/study/tdd/StringCalculatorTest.java @@ -17,33 +17,11 @@ public class StringCalculatorTest { } @Test - public void 문자열을_파싱한다() { - String input = "2 + 3 * 4 / 2"; - - StringParser stringParser = new StringParser(); - - List parser = stringParser.parse(input); - List stringList = List.of("2", "+", "3", "*", "4", "/", "2"); - - Assertions.assertThat(parser).isEqualTo(stringList); - } - - @Test - public void 숫자_0으로_나누면_IllegalArgumentException() { - String input = "2 + 3 * 4 / 0"; - - Assertions.assertThatThrownBy(() -> { - StringCalculator stringCalculator = new StringCalculator(); - stringCalculator.exception(input); - }).isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void 계산() { + public void 수학적_계산이_아닌_문자열_인덱스_순서_대로_연산자와_피연산자를_나누어_계산한다() { String input = "2 + 3 * 4 / 2"; StringCalculator stringCalculator = new StringCalculator(input); - int result = stringCalculator.calculate(input); + int result = stringCalculator.calculate(); Assertions.assertThat(result).isEqualTo(10); } diff --git a/src/test/java/study/tdd/StringParser.java b/src/test/java/study/tdd/StringParser.java deleted file mode 100644 index 6b0a7ac..0000000 --- a/src/test/java/study/tdd/StringParser.java +++ /dev/null @@ -1,14 +0,0 @@ -package study.tdd; - -import java.util.List; - -public class StringParser { - - private List actual; - public List parse(String input) { - for (int i = 0; i < input.length(); i++) { - actual = List.of(input.split(" ")); - } - return actual; - } -} diff --git a/src/test/java/study/tdd/StringParserTest.java b/src/test/java/study/tdd/StringParserTest.java new file mode 100644 index 0000000..592d4c4 --- /dev/null +++ b/src/test/java/study/tdd/StringParserTest.java @@ -0,0 +1,21 @@ +package study.tdd; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class StringParserTest { + + @Test + public void 문자열을_파싱한다() { + String input = "2 + 3 * 4 / 2"; + + StringParser stringParser = new StringParser(); + + List parser = stringParser.parse(input); + List stringList = List.of("2", "+", "3", "*", "4", "/", "2"); + + Assertions.assertThat(parser).isEqualTo(stringList); + } +} From 8535a2bab310a5fd99d8d4e923f19873ba9ead7f Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Wed, 12 Jul 2023 10:22:48 +0900 Subject: [PATCH 08/16] =?UTF-8?q?=EC=88=AB=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=202=EC=B0=A8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/tdd/StringCalculator.java | 11 +++----- src/main/java/study/tdd/StringParser.java | 2 +- src/test/java/study/exam/SetTest.java | 10 +++---- .../java/study/tdd/StringCalculatorTest.java | 26 ++++++++++++++----- src/test/java/study/tdd/StringParserTest.java | 7 ++--- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/main/java/study/tdd/StringCalculator.java b/src/main/java/study/tdd/StringCalculator.java index 88ce26f..fb25c65 100644 --- a/src/main/java/study/tdd/StringCalculator.java +++ b/src/main/java/study/tdd/StringCalculator.java @@ -4,18 +4,13 @@ public class StringCalculator { + private StringCalculator() { } - private static String text; - - public StringCalculator(String text) { - this.text = text; - } - - public String getText() { + public static String getText(String text) { return text; } - static int calculate() { + static int calculate(String text) { List values = StringParser.parse(text); int number = Integer.parseInt(values.get(0)); diff --git a/src/main/java/study/tdd/StringParser.java b/src/main/java/study/tdd/StringParser.java index 356405f..ef73d1a 100644 --- a/src/main/java/study/tdd/StringParser.java +++ b/src/main/java/study/tdd/StringParser.java @@ -4,7 +4,7 @@ public class StringParser { - public StringParser() { } + private StringParser() { } static List parse(String input) { return List.of(input.split(" ")); diff --git a/src/test/java/study/exam/SetTest.java b/src/test/java/study/exam/SetTest.java index 4f6dcbd..bc2e527 100644 --- a/src/test/java/study/exam/SetTest.java +++ b/src/test/java/study/exam/SetTest.java @@ -17,7 +17,7 @@ public class SetTest { private Set numbers; @BeforeEach - @DisplayName("HashSet에 값을 추가해주는 메소드 (값 중복 X)") + @DisplayName("HashSet에 값을 추가한다.") void setUp() { numbers = new HashSet<>(); numbers.add(1); @@ -27,14 +27,14 @@ void setUp() { } @Test - @DisplayName("집합의 크기를 확인하는 테스트") + @DisplayName("집합의 크기를 확인할 수 있다.") void setSize() { int result = numbers.size(); assertEquals(result, 3); // Set은 중복을 허용하지 않는다. } @Test - @DisplayName("집합에 값이 포함되어있는지 비교하는 테스트") + @DisplayName("집합에 값이 포함되어있는지 비교할 수 있다.") void compareContains() { numbers = new HashSet<>(); @@ -50,7 +50,7 @@ void compareContains() { @ParameterizedTest @ValueSource(ints = {1, 2, 3}) - @DisplayName("중복되지 않게 ParameterizedTest로 contains를 실행하는 코드를 한 줄로 줄인 테스트") + @DisplayName("ValueSource를 사용해 입력 받은 숫자가 각각 numbers에 포함되는지 확인한다.") void compareContains(int value) { numbers = new HashSet<>(); @@ -70,7 +70,7 @@ void compareContains(int value) { "4, false", "5, false" }) - @DisplayName("CsvSource로 true 값만 비교하는 것이 아닌 false 값도 비교해보는 테스트") + @DisplayName("CsvSource로 true 값만 비교하는 것이 아닌 false 값도 비교할 수 있다.") void compareContains(int value, boolean expected) { numbers = new HashSet<>(); diff --git a/src/test/java/study/tdd/StringCalculatorTest.java b/src/test/java/study/tdd/StringCalculatorTest.java index 14e9a6e..c6d9d3d 100644 --- a/src/test/java/study/tdd/StringCalculatorTest.java +++ b/src/test/java/study/tdd/StringCalculatorTest.java @@ -1,6 +1,7 @@ package study.tdd; import org.assertj.core.api.Assertions; +import org.assertj.core.api.ThrowableAssert; import org.junit.jupiter.api.Test; import java.util.List; @@ -11,19 +12,32 @@ public class StringCalculatorTest { public void 문자열을_입력받는다() { String input = "2 + 3 * 4 / 2"; - StringCalculator stringCalculator = new StringCalculator(input); - - Assertions.assertThat(stringCalculator.getText()); + Assertions.assertThat(StringCalculator.getText(input)); } @Test public void 수학적_계산이_아닌_문자열_인덱스_순서_대로_연산자와_피연산자를_나누어_계산한다() { String input = "2 + 3 * 4 / 2"; - StringCalculator stringCalculator = new StringCalculator(input); - int result = stringCalculator.calculate(); - Assertions.assertThat(result).isEqualTo(10); + Assertions.assertThat(StringCalculator.calculate(input)).isEqualTo(10); + + } + + @Test + public void 잘못된_연산자가_입력됐을_때_예외가_발생한다() { + String input = "2 + 3 * 4 & 2"; + Assertions.assertThatThrownBy(() -> { + StringCalculator.calculate(input); + }).isInstanceOf(IllegalArgumentException.class); } + @Test + public void 나누는_숫자가_0이면_예외가_발생한다() { + String input = "2 + 3 * 4 / 0"; + + Assertions.assertThatThrownBy(() -> { + StringCalculator.calculate(input); + }).isInstanceOf(IllegalArgumentException.class); + } } diff --git a/src/test/java/study/tdd/StringParserTest.java b/src/test/java/study/tdd/StringParserTest.java index 592d4c4..f54c5f1 100644 --- a/src/test/java/study/tdd/StringParserTest.java +++ b/src/test/java/study/tdd/StringParserTest.java @@ -8,14 +8,11 @@ public class StringParserTest { @Test - public void 문자열을_파싱한다() { + public void 하나의_문자열을_파싱해서_String_배열로_만든다() { String input = "2 + 3 * 4 / 2"; - StringParser stringParser = new StringParser(); - - List parser = stringParser.parse(input); List stringList = List.of("2", "+", "3", "*", "4", "/", "2"); - Assertions.assertThat(parser).isEqualTo(stringList); + Assertions.assertThat(StringParser.parse(input)).isEqualTo(stringList); } } From 4f9da2731fb93f884644b85f1739bc5a11bc8dfe Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Wed, 12 Jul 2023 19:06:52 +0900 Subject: [PATCH 09/16] =?UTF-8?q?=EC=88=AB=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=20(=EC=99=84=EB=A3=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/tdd/StringCalculator.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/study/tdd/StringCalculator.java b/src/main/java/study/tdd/StringCalculator.java index fb25c65..0e3e374 100644 --- a/src/main/java/study/tdd/StringCalculator.java +++ b/src/main/java/study/tdd/StringCalculator.java @@ -5,11 +5,6 @@ public class StringCalculator { private StringCalculator() { } - - public static String getText(String text) { - return text; - } - static int calculate(String text) { List values = StringParser.parse(text); int number = Integer.parseInt(values.get(0)); From 7102f19ec932ed2b214124a1646a9e3f0239c98d Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Sat, 15 Jul 2023 23:15:53 +0900 Subject: [PATCH 10/16] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EB=8C=80?= =?UTF-8?q?=EC=88=98=20=EB=B9=84=EA=B5=90=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84(=EC=88=AB=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=20getText=20=EC=82=AC=EC=9A=A9=ED=95=9C=20?= =?UTF-8?q?=EA=B3=B3=20=EC=82=AD=EC=A0=9C,=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20=EC=88=98=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/racingcar/Car.java | 15 ++++++ .../StringCalculator.java | 2 +- .../StringParser.java | 2 +- src/test/java/study/racingcar/CarTest.java | 47 ++++++++++++++++++ src/test/java/study/racingcar/README.md | 35 +++++++++++++ src/test/java/study/racingcar/img.png | Bin 0 -> 14437 bytes .../study/{tdd => stringcalculator}/README.md | 0 .../StringCalculatorTest.java | 7 +-- .../StringParserTest.java | 2 +- 9 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 src/main/java/study/racingcar/Car.java rename src/main/java/study/{tdd => stringcalculator}/StringCalculator.java (96%) rename src/main/java/study/{tdd => stringcalculator}/StringParser.java (91%) create mode 100644 src/test/java/study/racingcar/CarTest.java create mode 100644 src/test/java/study/racingcar/README.md create mode 100644 src/test/java/study/racingcar/img.png rename src/test/java/study/{tdd => stringcalculator}/README.md (100%) rename src/test/java/study/{tdd => stringcalculator}/StringCalculatorTest.java (87%) rename src/test/java/study/{tdd => stringcalculator}/StringParserTest.java (93%) diff --git a/src/main/java/study/racingcar/Car.java b/src/main/java/study/racingcar/Car.java new file mode 100644 index 0000000..1dd34a9 --- /dev/null +++ b/src/main/java/study/racingcar/Car.java @@ -0,0 +1,15 @@ +package study.racingcar; + +import java.util.Scanner; + +public class Car { + + public static int numberOfCar(String input) { + System.out.println("자동차 대수는 몇 대 인가요?"); + + Scanner scanner = new Scanner(System.in); + String number = scanner.next(); + + return Integer.getInteger(number); + } +} diff --git a/src/main/java/study/tdd/StringCalculator.java b/src/main/java/study/stringcalculator/StringCalculator.java similarity index 96% rename from src/main/java/study/tdd/StringCalculator.java rename to src/main/java/study/stringcalculator/StringCalculator.java index 0e3e374..44f4362 100644 --- a/src/main/java/study/tdd/StringCalculator.java +++ b/src/main/java/study/stringcalculator/StringCalculator.java @@ -1,4 +1,4 @@ -package study.tdd; +package study.stringcalculator; import java.util.List; diff --git a/src/main/java/study/tdd/StringParser.java b/src/main/java/study/stringcalculator/StringParser.java similarity index 91% rename from src/main/java/study/tdd/StringParser.java rename to src/main/java/study/stringcalculator/StringParser.java index ef73d1a..423c6d3 100644 --- a/src/main/java/study/tdd/StringParser.java +++ b/src/main/java/study/stringcalculator/StringParser.java @@ -1,4 +1,4 @@ -package study.tdd; +package study.stringcalculator; import java.util.List; diff --git a/src/test/java/study/racingcar/CarTest.java b/src/test/java/study/racingcar/CarTest.java new file mode 100644 index 0000000..f41f83f --- /dev/null +++ b/src/test/java/study/racingcar/CarTest.java @@ -0,0 +1,47 @@ +package study.racingcar; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Scanner; + +public class CarTest { + + private ByteArrayOutputStream outputStream; + + protected void systemIn(String input) { + // 이 메서드를 실행하면 input 값의 바이트 배열을 스트림에 넣어 System.in에 할당해준다. + // 참고한 링크 : https://steadyjay.tistory.com/10 + System.setIn(new ByteArrayInputStream(input.getBytes())); + } + + protected String getOutput() { + // ByteArrayOutputStream의 toString은 기본 문자 집합을 사용하여 버퍼의 내용을 문자열 디코딩 바이트로 변환해줍니다. + return outputStream.toString(); + } + + void test() { + Scanner scanner = new Scanner(System.in); + System.out.println(scanner.nextLine()); + } + @BeforeEach + void setUp() { + outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + } + + @Test + public void 자동차_수를_입력받는다() { + String input = "3"; + systemIn(input); + test(); + + Assertions.assertThat(getOutput()).contains("3"); + } + + +} diff --git a/src/test/java/study/racingcar/README.md b/src/test/java/study/racingcar/README.md new file mode 100644 index 0000000..45e9eee --- /dev/null +++ b/src/test/java/study/racingcar/README.md @@ -0,0 +1,35 @@ +### **요구사항** + +초간단 자동차 경주 게임을 구현한다. + +- 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다. +- 사용자는 몇 대의 자동차로 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다. +- 전진하는 조건은 0에서 9 사이에서 무작위 값을 구한 후 무작위 값이 4 이상일 경우이다. +- 자동차의 상태를 화면에 출력한다. 어느 시점에 출력할 것인지에 대한 제약은 없다. + +--- +### **체크리스트** +- [x] 자동차 대수를 입력 받는다. +- [ ] 시도할 횟수를 입력 받는다. +- [ ] 무작위 값을 구해 4 이상일 때 전진하는 조건을 구현한다. (미만일 경우는 IllegalArgumentException) +- [ ] 자동차의 상태를 화면에 출력한다. + +--- +# 프로그래밍 요구 사항 + +모든 로직에 단위 테스트를 구현한다. +단, UI(System.out, [System.in](http://system.in/)) 로직은 제외 +핵심 로직을 구현하는 코드와 UI를 담당하는 로직을 구분한다. +UI 로직을 InputView, ResultView와 같은 클래스를 추가해 분리한다. + +--- +ex. +> 자동차 대수는 몇 대인가요? +> +> 3 +> +> 시도할 횟수는 몇 회인가요? +> +> 5 +> +> ![img.png](img.png) \ No newline at end of file diff --git a/src/test/java/study/racingcar/img.png b/src/test/java/study/racingcar/img.png new file mode 100644 index 0000000000000000000000000000000000000000..0490a3ad49196812ddcef1b55057c20c162a0dce GIT binary patch literal 14437 zcmeHucTkh*yLMa)tFB@N3q^4)lz=Em6IfRf1p$#NU06lBp#_1EwIfwmDN2cofV2Pt zfl!h~Nuq$TgpwEn(L_oJNkAY0LXz`D-Tm#EZ|2PT=6v&;{btS|yu*<2mgl+e>%Ok* zzTdlNZ7tWW*|7!!fvh`y>clw+hZ*OYM@*Bn^g`12oiX!5|%SGeN}*1tWvsn+u2v6UrjPVYO+ z?)yn2al`k!e|-L~)9CPm*;wQ2)31yky#3LrCwX2#h}-|xgMwg;9W_6SqD0k8>kEpv zKp>wu(tgQu2;}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gasb3Fa0rC$fM@}@0 zxyRqbN2uw>8VOUcRf!!O1DUg@^9)cU+jVGA+<`5Va&^W;vvC#Znb<**U)-BcMBOGE z@M6i12$S0f>t$)r)C-JaAQPqwT^lZ+{=!C$H9*jx{}S}0MdNWxcZ+>bk@7}eh!qHd z-Is_L{wv~(lc(>LHX1v9^ZP*cY*>gBc+wBTeu`kkPu4M3LVhavV*3355bu8r^%495 zzS8yfhA(}c-tJjpBTRJe5o5?*dvqVb``!E|e(FL$LXk8~!E+dQcBOJkf}aSecMh4S zo)(x1X#7o(C(gj}TzQJLgl+&pR#1V61$eIe0)t`0q|4Q*nwXowh{%mz6@ z8|0Air!R_n{XKR9)E{c$k93zO=7i$*f_4`rz0&Cs@^5;V4fHNFO4=Z=A+xImswvFWpOTc?QJAC3R27Kw|@8N{Z5EG4=4j9YkPe%zz@b>9c zRb-%+r{MDJoRGA*ZYS&6=?5~tHwTO^VnaKw61V7I4Ggenbc(9yT5>hY;FF)7XyIKL zUpvior8gJ88gjM_WXt=DStMPO(&mDbsVO}LmhypZi5q3vqDF03r)u;>EV7G9U<7Z1 zI3Vk!e*8a3ov!cv6sQnj^lLlc{G%9nP-Hc#7a}AA&^lE4r0B&q&-bC+d4ru>Uu%ly zTji0C<>gyjF;`%@&PIZd$Bzm&?m0>H#4m&d5B4|+O}~Tu@El~)^ZCc1UXONi60suC zh_yUA-S?Tg1bVO;>K47v_I5DuV`o6w`^5`P5k9;W#puRYX*VipX?&ryJ8D3;$rlyC zP|sCCEWK^V%ZD)7GZp7W9za32P^xv5Y1PH7GNm}TyfU!_%)%7zZgp;%!V4izV?|RX z;vtEn-F_ZTc&(dJO}Qnl!w;fr@h6qzzOLDqo>RQzm!4A|@<7B+BUOkG^50{oN>%sr Y+KxAFaPS4M4?<2`*q%WD>~sA;026F&6951J literal 0 HcmV?d00001 diff --git a/src/test/java/study/tdd/README.md b/src/test/java/study/stringcalculator/README.md similarity index 100% rename from src/test/java/study/tdd/README.md rename to src/test/java/study/stringcalculator/README.md diff --git a/src/test/java/study/tdd/StringCalculatorTest.java b/src/test/java/study/stringcalculator/StringCalculatorTest.java similarity index 87% rename from src/test/java/study/tdd/StringCalculatorTest.java rename to src/test/java/study/stringcalculator/StringCalculatorTest.java index c6d9d3d..5bcff27 100644 --- a/src/test/java/study/tdd/StringCalculatorTest.java +++ b/src/test/java/study/stringcalculator/StringCalculatorTest.java @@ -1,18 +1,15 @@ -package study.tdd; +package study.stringcalculator; import org.assertj.core.api.Assertions; -import org.assertj.core.api.ThrowableAssert; import org.junit.jupiter.api.Test; -import java.util.List; - public class StringCalculatorTest { @Test public void 문자열을_입력받는다() { String input = "2 + 3 * 4 / 2"; - Assertions.assertThat(StringCalculator.getText(input)); + Assertions.assertThat(StringCalculator.calculate(input)); } @Test diff --git a/src/test/java/study/tdd/StringParserTest.java b/src/test/java/study/stringcalculator/StringParserTest.java similarity index 93% rename from src/test/java/study/tdd/StringParserTest.java rename to src/test/java/study/stringcalculator/StringParserTest.java index f54c5f1..e971102 100644 --- a/src/test/java/study/tdd/StringParserTest.java +++ b/src/test/java/study/stringcalculator/StringParserTest.java @@ -1,4 +1,4 @@ -package study.tdd; +package study.stringcalculator; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; From 3bf2a25bec88049898287a78253aac984536292d Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Mon, 17 Jul 2023 13:25:29 +0900 Subject: [PATCH 11/16] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EA=B2=BD?= =?UTF-8?q?=EC=A3=BC=20Step=201=20=ED=94=84=EB=A1=9C=EB=8D=95=EC=85=98,=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=81=AC=EC=BD=94=EB=93=9C=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/racingcar/Car.java | 44 +++++++++++++++++-- .../stringcalculator/StringCalculator.java | 2 +- src/test/java/study/racingcar/CarTest.java | 15 +++++++ src/test/java/study/racingcar/README.md | 5 +-- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/main/java/study/racingcar/Car.java b/src/main/java/study/racingcar/Car.java index 1dd34a9..4f73ddf 100644 --- a/src/main/java/study/racingcar/Car.java +++ b/src/main/java/study/racingcar/Car.java @@ -1,15 +1,51 @@ package study.racingcar; +import java.util.Random; import java.util.Scanner; public class Car { - public static int numberOfCar(String input) { +// public static void main(String[] args) { +// new Car(); +// } + private Car() { + numberOfCar(); + } + public static void numberOfCar() { + Scanner scanner = new Scanner(System.in); System.out.println("자동차 대수는 몇 대 인가요?"); - Scanner scanner = new Scanner(System.in); - String number = scanner.next(); + int number = scanner.nextInt(); + + System.out.println("시도할 횟수는 몇 회인가요?"); + int attempt = scanner.nextInt(); + + System.out.println(); + racingCar(number, attempt); + } + + public static void racingCar(int number, int attempt) { + int[] positions = new int[number]; + Random random = new Random(); + + for (int i = 0; i < attempt; i++) { + for (int j = 0; j < number; j++) { + int forward = random.nextInt(10); // 0부터 9 + if (forward >= 4) { // 전진하는 조건 + positions[j] += 1; + } + } - return Integer.getInteger(number); + for (int pos : positions) { + if (pos == 0) { + System.out.print("ㅡ"); + } + for (int k = 0; k < pos; k++) { + System.out.print("ㅡ"); + } + System.out.println(); + } + System.out.println(); + } } } diff --git a/src/main/java/study/stringcalculator/StringCalculator.java b/src/main/java/study/stringcalculator/StringCalculator.java index 44f4362..e8b8483 100644 --- a/src/main/java/study/stringcalculator/StringCalculator.java +++ b/src/main/java/study/stringcalculator/StringCalculator.java @@ -5,7 +5,7 @@ public class StringCalculator { private StringCalculator() { } - static int calculate(String text) { + public static int calculate(String text) { List values = StringParser.parse(text); int number = Integer.parseInt(values.get(0)); diff --git a/src/test/java/study/racingcar/CarTest.java b/src/test/java/study/racingcar/CarTest.java index f41f83f..7d6390d 100644 --- a/src/test/java/study/racingcar/CarTest.java +++ b/src/test/java/study/racingcar/CarTest.java @@ -2,13 +2,21 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; 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 study.stringcalculator.StringCalculator; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.HashSet; import java.util.Scanner; +import static org.assertj.core.api.Assertions.assertThat; + public class CarTest { private ByteArrayOutputStream outputStream; @@ -43,5 +51,12 @@ void setUp() { Assertions.assertThat(getOutput()).contains("3"); } + @Test + public void 시도할_횟수를_입력받는다() { + String input = "5"; + systemIn(input); + test(); + Assertions.assertThat(getOutput()).contains("5"); + } } diff --git a/src/test/java/study/racingcar/README.md b/src/test/java/study/racingcar/README.md index 45e9eee..fe416b1 100644 --- a/src/test/java/study/racingcar/README.md +++ b/src/test/java/study/racingcar/README.md @@ -10,9 +10,8 @@ --- ### **체크리스트** - [x] 자동차 대수를 입력 받는다. -- [ ] 시도할 횟수를 입력 받는다. -- [ ] 무작위 값을 구해 4 이상일 때 전진하는 조건을 구현한다. (미만일 경우는 IllegalArgumentException) -- [ ] 자동차의 상태를 화면에 출력한다. +- [x] 시도할 횟수를 입력 받는다. +- [x] 무작위 값을 구해 4 이상일 때 전진하는 조건을 구현한다. --- # 프로그래밍 요구 사항 From 574c82720aa1aa22b05cf36082e03b7427a0888d Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Tue, 18 Jul 2023 15:11:11 +0900 Subject: [PATCH 12/16] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EA=B2=BD?= =?UTF-8?q?=EC=A3=BC=20Step=201=20=ED=94=84=EB=A1=9C=EB=8D=95=EC=85=98=20M?= =?UTF-8?q?VC=20=ED=8C=A8=ED=84=B4=20=EC=B0=B8=EA=B3=A0,=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95(?= =?UTF-8?q?=EB=AF=B8=EC=99=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/racingcar/Car.java | 51 ----------------- .../study/racingcar/RacingCarApplication.java | 10 ++++ .../racingcar/controller/CarController.java | 29 ++++++++++ src/main/java/study/racingcar/model/Car.java | 12 ++++ .../java/study/racingcar/view/InputView.java | 27 +++++++++ .../java/study/racingcar/view/ResultView.java | 32 +++++++++++ src/test/java/study/racingcar/CarTest.java | 56 +------------------ 7 files changed, 113 insertions(+), 104 deletions(-) delete mode 100644 src/main/java/study/racingcar/Car.java create mode 100644 src/main/java/study/racingcar/RacingCarApplication.java create mode 100644 src/main/java/study/racingcar/controller/CarController.java create mode 100644 src/main/java/study/racingcar/model/Car.java create mode 100644 src/main/java/study/racingcar/view/InputView.java create mode 100644 src/main/java/study/racingcar/view/ResultView.java diff --git a/src/main/java/study/racingcar/Car.java b/src/main/java/study/racingcar/Car.java deleted file mode 100644 index 4f73ddf..0000000 --- a/src/main/java/study/racingcar/Car.java +++ /dev/null @@ -1,51 +0,0 @@ -package study.racingcar; - -import java.util.Random; -import java.util.Scanner; - -public class Car { - -// public static void main(String[] args) { -// new Car(); -// } - private Car() { - numberOfCar(); - } - public static void numberOfCar() { - Scanner scanner = new Scanner(System.in); - System.out.println("자동차 대수는 몇 대 인가요?"); - - int number = scanner.nextInt(); - - System.out.println("시도할 횟수는 몇 회인가요?"); - int attempt = scanner.nextInt(); - - System.out.println(); - racingCar(number, attempt); - } - - public static void racingCar(int number, int attempt) { - int[] positions = new int[number]; - Random random = new Random(); - - for (int i = 0; i < attempt; i++) { - for (int j = 0; j < number; j++) { - int forward = random.nextInt(10); // 0부터 9 - if (forward >= 4) { // 전진하는 조건 - positions[j] += 1; - } - } - - for (int pos : positions) { - if (pos == 0) { - System.out.print("ㅡ"); - } - for (int k = 0; k < pos; k++) { - System.out.print("ㅡ"); - } - System.out.println(); - } - System.out.println(); - } - } -} diff --git a/src/main/java/study/racingcar/RacingCarApplication.java b/src/main/java/study/racingcar/RacingCarApplication.java new file mode 100644 index 0000000..76d5a06 --- /dev/null +++ b/src/main/java/study/racingcar/RacingCarApplication.java @@ -0,0 +1,10 @@ +package study.racingcar; + +import study.racingcar.model.Car; + +public class RacingCarApplication { + + public static void main(String[] args) { + new Car(); + } +} diff --git a/src/main/java/study/racingcar/controller/CarController.java b/src/main/java/study/racingcar/controller/CarController.java new file mode 100644 index 0000000..2a28414 --- /dev/null +++ b/src/main/java/study/racingcar/controller/CarController.java @@ -0,0 +1,29 @@ +package study.racingcar.controller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import static study.racingcar.view.ResultView.printRaceResult; +import static study.racingcar.view.ResultView.requestCarsToMove; + +public class CarController { + + public static void racingCar(int carNum, int attemptNum) { + List positions = new ArrayList<>(); + Random random = new Random(); + + addCar(carNum, positions); + + for (int i = 0; i < attemptNum; i++) { + requestCarsToMove(carNum, positions, random); + printRaceResult(positions); + } + } + + public static void addCar(int carNum, List positions) { + for (int i = 0; i < carNum; i++) { + positions.add(i,0); + } + } +} diff --git a/src/main/java/study/racingcar/model/Car.java b/src/main/java/study/racingcar/model/Car.java new file mode 100644 index 0000000..beccc90 --- /dev/null +++ b/src/main/java/study/racingcar/model/Car.java @@ -0,0 +1,12 @@ +package study.racingcar.model; + +import static study.racingcar.controller.CarController.racingCar; +import static study.racingcar.view.InputView.*; + +public class Car { + + public Car() { + racingCar(numberOfCar(),numberOfAttempt()); + } + +} diff --git a/src/main/java/study/racingcar/view/InputView.java b/src/main/java/study/racingcar/view/InputView.java new file mode 100644 index 0000000..7ae9b41 --- /dev/null +++ b/src/main/java/study/racingcar/view/InputView.java @@ -0,0 +1,27 @@ +package study.racingcar.view; + +import java.util.Scanner; + +import static study.racingcar.controller.CarController.racingCar; + +public class InputView { + + public static int numberOfCar() { + Scanner scanner = new Scanner(System.in); + + System.out.println("자동차 대수는 몇 대 인가요?"); + int carNum = scanner.nextInt(); + + return carNum; + } + + public static int numberOfAttempt() { + Scanner scanner = new Scanner(System.in); + + System.out.println("시도할 횟수는 몇 회인가요?"); + int attemptNum = scanner.nextInt(); + System.out.println(); + + return attemptNum; + } +} \ No newline at end of file diff --git a/src/main/java/study/racingcar/view/ResultView.java b/src/main/java/study/racingcar/view/ResultView.java new file mode 100644 index 0000000..985fa2e --- /dev/null +++ b/src/main/java/study/racingcar/view/ResultView.java @@ -0,0 +1,32 @@ +package study.racingcar.view; + +import java.util.Collections; +import java.util.List; +import java.util.Random; + +public class ResultView { + + public static void requestCarsToMove(int carNum, List positions, Random random) { + for (int j = 0; j < carNum; j++) { + int forward = random.nextInt(10); // 0부터 9 + if (forward >= 4) { // 전진하는 조건 + int currentValue = positions.get(j); + int newValue = currentValue + 1; + positions.set(j, newValue); + } + } + } + + public static void printRaceResult(List positions) { + for (int pos : positions) { + if (pos == 0) { + System.out.print("ㅡ"); + } + for (int k = 0; k < pos; k++) { + System.out.print("ㅡ"); + } + System.out.println(); + } + System.out.println(); + } +} diff --git a/src/test/java/study/racingcar/CarTest.java b/src/test/java/study/racingcar/CarTest.java index 7d6390d..f64a9f0 100644 --- a/src/test/java/study/racingcar/CarTest.java +++ b/src/test/java/study/racingcar/CarTest.java @@ -1,62 +1,12 @@ package study.racingcar; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; 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 study.stringcalculator.StringCalculator; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.HashSet; -import java.util.Scanner; - -import static org.assertj.core.api.Assertions.assertThat; +import study.racingcar.controller.CarController; public class CarTest { - - private ByteArrayOutputStream outputStream; - - protected void systemIn(String input) { - // 이 메서드를 실행하면 input 값의 바이트 배열을 스트림에 넣어 System.in에 할당해준다. - // 참고한 링크 : https://steadyjay.tistory.com/10 - System.setIn(new ByteArrayInputStream(input.getBytes())); - } - - protected String getOutput() { - // ByteArrayOutputStream의 toString은 기본 문자 집합을 사용하여 버퍼의 내용을 문자열 디코딩 바이트로 변환해줍니다. - return outputStream.toString(); - } - - void test() { - Scanner scanner = new Scanner(System.in); - System.out.println(scanner.nextLine()); - } - @BeforeEach - void setUp() { - outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - } - - @Test - public void 자동차_수를_입력받는다() { - String input = "3"; - systemIn(input); - test(); - - Assertions.assertThat(getOutput()).contains("3"); - } - @Test - public void 시도할_횟수를_입력받는다() { - String input = "5"; - systemIn(input); - test(); + public void 자동차_이동_로직을_확인한다() { + CarController.racingCar(1, 1); - Assertions.assertThat(getOutput()).contains("5"); } } From d618a59853845f3278049d04457af7a6209e519c Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Wed, 19 Jul 2023 22:49:54 +0900 Subject: [PATCH 13/16] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EA=B2=BD?= =?UTF-8?q?=EC=A3=BC=20Step=201=20=ED=94=84=EB=A1=9C=EB=8D=95=EC=85=98=20M?= =?UTF-8?q?VC=20=ED=8C=A8=ED=84=B4=20=EC=88=98=EC=A0=95,=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../study/racingcar/RacingCarApplication.java | 4 +- .../racingcar/controller/CarController.java | 27 ++--------- src/main/java/study/racingcar/model/Car.java | 48 +++++++++++++++++-- .../java/study/racingcar/view/InputView.java | 7 +-- .../java/study/racingcar/view/ResultView.java | 16 ------- src/test/java/study/racingcar/CarTest.java | 38 +++++++++++++-- 6 files changed, 88 insertions(+), 52 deletions(-) diff --git a/src/main/java/study/racingcar/RacingCarApplication.java b/src/main/java/study/racingcar/RacingCarApplication.java index 76d5a06..30b3c08 100644 --- a/src/main/java/study/racingcar/RacingCarApplication.java +++ b/src/main/java/study/racingcar/RacingCarApplication.java @@ -1,10 +1,10 @@ package study.racingcar; -import study.racingcar.model.Car; +import study.racingcar.controller.CarController; public class RacingCarApplication { public static void main(String[] args) { - new Car(); + new CarController(); } } diff --git a/src/main/java/study/racingcar/controller/CarController.java b/src/main/java/study/racingcar/controller/CarController.java index 2a28414..b2764d8 100644 --- a/src/main/java/study/racingcar/controller/CarController.java +++ b/src/main/java/study/racingcar/controller/CarController.java @@ -1,29 +1,12 @@ package study.racingcar.controller; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -import static study.racingcar.view.ResultView.printRaceResult; -import static study.racingcar.view.ResultView.requestCarsToMove; +import static study.racingcar.model.Car.raceStart; +import static study.racingcar.view.InputView.getAttemptNumber; +import static study.racingcar.view.InputView.getCarNumber; public class CarController { - public static void racingCar(int carNum, int attemptNum) { - List positions = new ArrayList<>(); - Random random = new Random(); - - addCar(carNum, positions); - - for (int i = 0; i < attemptNum; i++) { - requestCarsToMove(carNum, positions, random); - printRaceResult(positions); - } - } - - public static void addCar(int carNum, List positions) { - for (int i = 0; i < carNum; i++) { - positions.add(i,0); - } + public CarController() { + raceStart(getCarNumber(), getAttemptNumber()); } } diff --git a/src/main/java/study/racingcar/model/Car.java b/src/main/java/study/racingcar/model/Car.java index beccc90..2c40436 100644 --- a/src/main/java/study/racingcar/model/Car.java +++ b/src/main/java/study/racingcar/model/Car.java @@ -1,12 +1,52 @@ package study.racingcar.model; -import static study.racingcar.controller.CarController.racingCar; -import static study.racingcar.view.InputView.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import static study.racingcar.view.ResultView.printRaceResult; public class Car { - public Car() { - racingCar(numberOfCar(),numberOfAttempt()); + private static final int MAX_BOUNDARY_OF_RANDOM_NUMBER= 10; + public Car(int carNum, List positions) { + addCar(carNum, positions); } + public static void raceStart(int carNum, int attemptNum) { + List positions = new ArrayList<>(); + + Car car = new Car(carNum, positions); + + car.addCar(carNum, positions); + + for (int i = 0; i < attemptNum; i++) { + car.requestCarsToMove(carNum, positions); + printRaceResult(positions); + } + } + + private static void addCar(int carNum, List positions) { + for (int i = 0; i < carNum; i++) { + positions.add(i,0); + } + } + + public static void requestCarsToMove(int carNum, List positions) { + for (int i = 0; i < carNum; i++) { + forwardNumber(i, positions); + } + } + + private static void forwardNumber(int current, List positions) { + Random random = new Random(); + int forward = random.nextInt(MAX_BOUNDARY_OF_RANDOM_NUMBER); // 0부터 9 + + if (forward >= 4) { // 전진하는 조건 + int currentValue = positions.get(current); + int newValue = currentValue + 1; + + positions.set(current, newValue); + } + } } diff --git a/src/main/java/study/racingcar/view/InputView.java b/src/main/java/study/racingcar/view/InputView.java index 7ae9b41..9e3373c 100644 --- a/src/main/java/study/racingcar/view/InputView.java +++ b/src/main/java/study/racingcar/view/InputView.java @@ -2,11 +2,8 @@ import java.util.Scanner; -import static study.racingcar.controller.CarController.racingCar; - public class InputView { - - public static int numberOfCar() { + public static int getCarNumber() { Scanner scanner = new Scanner(System.in); System.out.println("자동차 대수는 몇 대 인가요?"); @@ -15,7 +12,7 @@ public static int numberOfCar() { return carNum; } - public static int numberOfAttempt() { + public static int getAttemptNumber() { Scanner scanner = new Scanner(System.in); System.out.println("시도할 횟수는 몇 회인가요?"); diff --git a/src/main/java/study/racingcar/view/ResultView.java b/src/main/java/study/racingcar/view/ResultView.java index 985fa2e..ab4011f 100644 --- a/src/main/java/study/racingcar/view/ResultView.java +++ b/src/main/java/study/racingcar/view/ResultView.java @@ -5,28 +5,12 @@ import java.util.Random; public class ResultView { - - public static void requestCarsToMove(int carNum, List positions, Random random) { - for (int j = 0; j < carNum; j++) { - int forward = random.nextInt(10); // 0부터 9 - if (forward >= 4) { // 전진하는 조건 - int currentValue = positions.get(j); - int newValue = currentValue + 1; - positions.set(j, newValue); - } - } - } - public static void printRaceResult(List positions) { for (int pos : positions) { - if (pos == 0) { - System.out.print("ㅡ"); - } for (int k = 0; k < pos; k++) { System.out.print("ㅡ"); } System.out.println(); } - System.out.println(); } } diff --git a/src/test/java/study/racingcar/CarTest.java b/src/test/java/study/racingcar/CarTest.java index f64a9f0..07258d1 100644 --- a/src/test/java/study/racingcar/CarTest.java +++ b/src/test/java/study/racingcar/CarTest.java @@ -1,12 +1,44 @@ package study.racingcar; import org.junit.jupiter.api.Test; -import study.racingcar.controller.CarController; +import study.racingcar.model.Car; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class CarTest { + + private static final int POSITIONS_INDEX = 0; + @Test + public void 자동차_대수와_시도_횟수를_1로_정해준_뒤_이동_로직을_확인한다() { + int carNum = 1; + int attemptNum = 1; + List positions = new ArrayList<>(); + + Car car = new Car(carNum, positions); + car.raceStart(carNum, attemptNum); + + int carPosition = 0; + + for (int i = 0; i < positions.size(); i++) { + carPosition = positions.get(i); + } + + assertEquals(carPosition >= 0 && carPosition <= 1, true); + } + @Test - public void 자동차_이동_로직을_확인한다() { - CarController.racingCar(1, 1); + public void 자동차_대수를_1대로_정해준_뒤_전진_로직을_확인한다() { + int carNum = 1; + List positions = new ArrayList<>(); + + Car car = new Car(carNum, positions); + car.requestCarsToMove(carNum, positions); + + int carPosition = positions.get(POSITIONS_INDEX); + assertEquals(carPosition == 0 || carPosition == 1, true); } } From 0171ca02c142748830023bbf6a1815caa3611d7e Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Fri, 21 Jul 2023 21:49:02 +0900 Subject: [PATCH 14/16] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EA=B2=BD?= =?UTF-8?q?=EC=A3=BC=20Step=201=20(4=EC=B0=A8=20=EC=88=98=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../racingcar/controller/CarController.java | 36 +++++++++++++-- src/main/java/study/racingcar/model/Car.java | 32 ++----------- .../java/study/racingcar/view/ResultView.java | 2 - src/test/java/study/racingcar/CarTest.java | 46 ++++++++++--------- 4 files changed, 59 insertions(+), 57 deletions(-) diff --git a/src/main/java/study/racingcar/controller/CarController.java b/src/main/java/study/racingcar/controller/CarController.java index b2764d8..b0a380c 100644 --- a/src/main/java/study/racingcar/controller/CarController.java +++ b/src/main/java/study/racingcar/controller/CarController.java @@ -1,12 +1,38 @@ package study.racingcar.controller; -import static study.racingcar.model.Car.raceStart; -import static study.racingcar.view.InputView.getAttemptNumber; -import static study.racingcar.view.InputView.getCarNumber; +import study.racingcar.view.InputView; +import study.racingcar.view.ResultView; -public class CarController { +import java.util.ArrayList; +import java.util.List; + +import static study.racingcar.model.Car.requestCarsToMove; +public class CarController { public CarController() { - raceStart(getCarNumber(), getAttemptNumber()); + raceStart(InputView.getCarNumber(), InputView.getAttemptNumber()); + } + + public static List raceStart(int carNum, int attemptNum) { + List startLine = addCar(carNum); + List finalPositions = new ArrayList<>(); + + for (int i = 0; i < attemptNum; i++) { + List racingCar = requestCarsToMove(carNum, startLine); + ResultView.printRaceResult(racingCar); + System.out.println(""); + finalPositions = new ArrayList<>(racingCar); + } + return finalPositions; + } + + private static List addCar(int carNum) { + List positions = new ArrayList<>(); + + for (int i = 0; i < carNum; i++) { + positions.add(i,0); + } + + return positions; } } diff --git a/src/main/java/study/racingcar/model/Car.java b/src/main/java/study/racingcar/model/Car.java index 2c40436..da20d8e 100644 --- a/src/main/java/study/racingcar/model/Car.java +++ b/src/main/java/study/racingcar/model/Car.java @@ -1,48 +1,24 @@ package study.racingcar.model; -import java.util.ArrayList; import java.util.List; import java.util.Random; -import static study.racingcar.view.ResultView.printRaceResult; - public class Car { - private static final int MAX_BOUNDARY_OF_RANDOM_NUMBER= 10; - public Car(int carNum, List positions) { - addCar(carNum, positions); - } - - public static void raceStart(int carNum, int attemptNum) { - List positions = new ArrayList<>(); - - Car car = new Car(carNum, positions); - - car.addCar(carNum, positions); - - for (int i = 0; i < attemptNum; i++) { - car.requestCarsToMove(carNum, positions); - printRaceResult(positions); - } - } - - private static void addCar(int carNum, List positions) { - for (int i = 0; i < carNum; i++) { - positions.add(i,0); - } - } + public Car() { } - public static void requestCarsToMove(int carNum, List positions) { + public static List requestCarsToMove(int carNum, List positions) { for (int i = 0; i < carNum; i++) { forwardNumber(i, positions); } + return positions; } private static void forwardNumber(int current, List positions) { Random random = new Random(); int forward = random.nextInt(MAX_BOUNDARY_OF_RANDOM_NUMBER); // 0부터 9 - if (forward >= 4) { // 전진하는 조건 + if (forward >= 4) { int currentValue = positions.get(current); int newValue = currentValue + 1; diff --git a/src/main/java/study/racingcar/view/ResultView.java b/src/main/java/study/racingcar/view/ResultView.java index ab4011f..cafb9f0 100644 --- a/src/main/java/study/racingcar/view/ResultView.java +++ b/src/main/java/study/racingcar/view/ResultView.java @@ -1,8 +1,6 @@ package study.racingcar.view; -import java.util.Collections; import java.util.List; -import java.util.Random; public class ResultView { public static void printRaceResult(List positions) { diff --git a/src/test/java/study/racingcar/CarTest.java b/src/test/java/study/racingcar/CarTest.java index 07258d1..b8a9c76 100644 --- a/src/test/java/study/racingcar/CarTest.java +++ b/src/test/java/study/racingcar/CarTest.java @@ -1,44 +1,46 @@ package study.racingcar; import org.junit.jupiter.api.Test; -import study.racingcar.model.Car; import java.util.ArrayList; import java.util.List; +import java.util.Random; import static org.junit.jupiter.api.Assertions.assertEquals; +import static study.racingcar.controller.CarController.raceStart; +import static study.racingcar.model.Car.requestCarsToMove; public class CarTest { - private static final int POSITIONS_INDEX = 0; + private static final int MAX_BOUNDARY_OF_RANDOM_CAR_NUMBER = 5; + private static final int MAX_BOUNDARY_OF_RANDOM_ATTEMPT_NUMBER = 3; @Test - public void 자동차_대수와_시도_횟수를_1로_정해준_뒤_이동_로직을_확인한다() { - int carNum = 1; - int attemptNum = 1; - List positions = new ArrayList<>(); - - Car car = new Car(carNum, positions); - car.raceStart(carNum, attemptNum); - - int carPosition = 0; + public void 자동차_이동_로직을_확인한다() { + // given + Random random = new Random(); + int carNum = random.nextInt(MAX_BOUNDARY_OF_RANDOM_CAR_NUMBER + 1); // 1 ~ 5 + int attemptNum = random.nextInt(MAX_BOUNDARY_OF_RANDOM_ATTEMPT_NUMBER + 3); // 3 ~ 5 - for (int i = 0; i < positions.size(); i++) { - carPosition = positions.get(i); - } + // when + List finalPositions = raceStart(carNum, attemptNum); - assertEquals(carPosition >= 0 && carPosition <= 1, true); + // then + assertEquals(carNum, finalPositions.size()); } @Test - public void 자동차_대수를_1대로_정해준_뒤_전진_로직을_확인한다() { - int carNum = 1; + public void 자동차_전진_로직을_확인한다() { + // given + Random random = new Random(); + int carNum = random.nextInt(MAX_BOUNDARY_OF_RANDOM_CAR_NUMBER + 1); // 1 ~ 5 List positions = new ArrayList<>(); - Car car = new Car(carNum, positions); - car.requestCarsToMove(carNum, positions); - - int carPosition = positions.get(POSITIONS_INDEX); + // when + List checkForward = requestCarsToMove(carNum, positions); - assertEquals(carPosition == 0 || carPosition == 1, true); + // then + for (int i = 0 ; i < carNum; i++) { + assertEquals(checkForward.get(i) == 0 || checkForward.get(i) == 1, true); + } } } From 93eb990fc5646b2864cf0fe9837b2e98fc9394b8 Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Tue, 25 Jul 2023 18:07:28 +0900 Subject: [PATCH 15/16] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EA=B2=BD?= =?UTF-8?q?=EC=A3=BC=20Step=201=20(5=EC=B0=A8=20=EC=88=98=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../racingcar/controller/CarController.java | 24 +++++++-------- src/main/java/study/racingcar/model/Car.java | 29 ++++++++++++++----- .../java/study/racingcar/view/ResultView.java | 9 ++++-- src/test/java/study/racingcar/CarTest.java | 24 ++++----------- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/main/java/study/racingcar/controller/CarController.java b/src/main/java/study/racingcar/controller/CarController.java index b0a380c..e5d8634 100644 --- a/src/main/java/study/racingcar/controller/CarController.java +++ b/src/main/java/study/racingcar/controller/CarController.java @@ -1,5 +1,6 @@ package study.racingcar.controller; +import study.racingcar.model.Car; import study.racingcar.view.InputView; import study.racingcar.view.ResultView; @@ -10,29 +11,26 @@ public class CarController { public CarController() { - raceStart(InputView.getCarNumber(), InputView.getAttemptNumber()); + List carList = addCar(InputView.getCarNumber()); + raceStart(InputView.getCarNumber(), InputView.getAttemptNumber(), carList); } - public static List raceStart(int carNum, int attemptNum) { - List startLine = addCar(carNum); - List finalPositions = new ArrayList<>(); - + public static void raceStart(int carNum, int attemptNum, List carList) { for (int i = 0; i < attemptNum; i++) { - List racingCar = requestCarsToMove(carNum, startLine); + List racingCar = requestCarsToMove(carNum, carList); ResultView.printRaceResult(racingCar); - System.out.println(""); - finalPositions = new ArrayList<>(racingCar); } - return finalPositions; } - private static List addCar(int carNum) { - List positions = new ArrayList<>(); + public static List addCar(int carNum) { + List carList = new ArrayList<>(); for (int i = 0; i < carNum; i++) { - positions.add(i,0); + Car car = new Car(0); + + carList.add(i, car); } - return positions; + return carList; } } diff --git a/src/main/java/study/racingcar/model/Car.java b/src/main/java/study/racingcar/model/Car.java index da20d8e..86075d4 100644 --- a/src/main/java/study/racingcar/model/Car.java +++ b/src/main/java/study/racingcar/model/Car.java @@ -5,24 +5,37 @@ public class Car { private static final int MAX_BOUNDARY_OF_RANDOM_NUMBER= 10; - public Car() { } + private int positions; - public static List requestCarsToMove(int carNum, List positions) { + public Car(int positions) { + this.positions = positions; + } + + public static List requestCarsToMove(int carNum, List carList) { for (int i = 0; i < carNum; i++) { - forwardNumber(i, positions); + forwardNumber(i, carList); } - return positions; + return carList; } - private static void forwardNumber(int current, List positions) { + private static void forwardNumber(int current, List carList) { Random random = new Random(); int forward = random.nextInt(MAX_BOUNDARY_OF_RANDOM_NUMBER); // 0부터 9 if (forward >= 4) { - int currentValue = positions.get(current); - int newValue = currentValue + 1; + Car car = carList.get(current); + int currentValue = car.getPositions(); - positions.set(current, newValue); + car.setPositions(currentValue + 1); + carList.set(current, car); } } + + public int getPositions() { + return positions; + } + + public void setPositions(int positions) { + this.positions = positions; + } } diff --git a/src/main/java/study/racingcar/view/ResultView.java b/src/main/java/study/racingcar/view/ResultView.java index cafb9f0..3933b6a 100644 --- a/src/main/java/study/racingcar/view/ResultView.java +++ b/src/main/java/study/racingcar/view/ResultView.java @@ -1,14 +1,17 @@ package study.racingcar.view; +import study.racingcar.model.Car; + import java.util.List; public class ResultView { - public static void printRaceResult(List positions) { - for (int pos : positions) { - for (int k = 0; k < pos; k++) { + public static void printRaceResult(List carList) { + for (Car car : carList) { + for (int k = 0; k < car.getPositions(); k++) { System.out.print("ㅡ"); } System.out.println(); } + System.out.println(); } } diff --git a/src/test/java/study/racingcar/CarTest.java b/src/test/java/study/racingcar/CarTest.java index b8a9c76..84e60bb 100644 --- a/src/test/java/study/racingcar/CarTest.java +++ b/src/test/java/study/racingcar/CarTest.java @@ -1,46 +1,32 @@ package study.racingcar; import org.junit.jupiter.api.Test; +import study.racingcar.model.Car; import java.util.ArrayList; import java.util.List; import java.util.Random; import static org.junit.jupiter.api.Assertions.assertEquals; -import static study.racingcar.controller.CarController.raceStart; +import static study.racingcar.controller.CarController.addCar; import static study.racingcar.model.Car.requestCarsToMove; public class CarTest { private static final int MAX_BOUNDARY_OF_RANDOM_CAR_NUMBER = 5; - private static final int MAX_BOUNDARY_OF_RANDOM_ATTEMPT_NUMBER = 3; - @Test - public void 자동차_이동_로직을_확인한다() { - // given - Random random = new Random(); - int carNum = random.nextInt(MAX_BOUNDARY_OF_RANDOM_CAR_NUMBER + 1); // 1 ~ 5 - int attemptNum = random.nextInt(MAX_BOUNDARY_OF_RANDOM_ATTEMPT_NUMBER + 3); // 3 ~ 5 - - // when - List finalPositions = raceStart(carNum, attemptNum); - - // then - assertEquals(carNum, finalPositions.size()); - } - @Test public void 자동차_전진_로직을_확인한다() { // given Random random = new Random(); int carNum = random.nextInt(MAX_BOUNDARY_OF_RANDOM_CAR_NUMBER + 1); // 1 ~ 5 - List positions = new ArrayList<>(); + List carList = addCar(carNum); // when - List checkForward = requestCarsToMove(carNum, positions); + List checkForward = requestCarsToMove(carNum, carList); // then for (int i = 0 ; i < carNum; i++) { - assertEquals(checkForward.get(i) == 0 || checkForward.get(i) == 1, true); + assertEquals(checkForward.get(i).getPositions() == 0 || checkForward.get(i).getPositions() == 1, true); } } } From ef48ab26624f30af9baef8e3d0256cd30af279bd Mon Sep 17 00:00:00 2001 From: JoJeHun Date: Tue, 25 Jul 2023 18:08:36 +0900 Subject: [PATCH 16/16] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EA=B2=BD?= =?UTF-8?q?=EC=A3=BC=20Step=201=20(=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20?= =?UTF-8?q?import=20=EC=82=AD=EC=A0=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/racingcar/CarTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/study/racingcar/CarTest.java b/src/test/java/study/racingcar/CarTest.java index 84e60bb..619992e 100644 --- a/src/test/java/study/racingcar/CarTest.java +++ b/src/test/java/study/racingcar/CarTest.java @@ -3,7 +3,6 @@ import org.junit.jupiter.api.Test; import study.racingcar.model.Car; -import java.util.ArrayList; import java.util.List; import java.util.Random;