From a45338ad942e20bffa0f04086819e9ec86968d02 Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Thu, 26 Sep 2024 17:51:15 +0900 Subject: [PATCH 01/10] =?UTF-8?q?[String]=20=ED=95=99=EC=8A=B5=ED=85=8C?= =?UTF-8?q?=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/SetCollectionTest.java | 2 ++ src/test/java/StringTest.java | 48 ++++++++++++++++++++++++++++ 2 files changed, 50 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..be120d6dc95 --- /dev/null +++ b/src/test/java/SetCollectionTest.java @@ -0,0 +1,2 @@ +public class SetCollectionTest { +} diff --git a/src/test/java/StringTest.java b/src/test/java/StringTest.java new file mode 100644 index 00000000000..1ad134bd41e --- /dev/null +++ b/src/test/java/StringTest.java @@ -0,0 +1,48 @@ +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class StringTest { + + @Test + @DisplayName("Split numbers and verify if 1,2 returns") + void SplitCheckTestWithOneTwo() { + String[] result = "1,2".split(","); + assertThat(result).containsExactly("1", "2"); + } + @Test + @DisplayName("Split numbers and verify if 1 returns") + void SplitCheckTestWithOne() { + String[] result = "1".split(","); + assertThat(result).containsExactly("1"); + } + + @Test + @DisplayName("Substring if there's Parenthesis exists") + void SubstringRemovalParenthesis() { + String input = "(1,2)"; + String result = input.substring(1, input.length() - 1); + + assertThat(result).contains("1,2"); + } + + @Test + @DisplayName("Get index of specific string character") + void GetStringIndex() { + String input = "abc"; + char result = input.charAt(1); + assertThat(result).isEqualTo('b'); + } + + @Test + @DisplayName("Get index of specific string character but throw exception") + void GetStringIndexWithException() { + String input = "abc"; + + assertThatThrownBy(() -> input.charAt(5)) + .isInstanceOf(StringIndexOutOfBoundsException.class) + .hasMessageContaining("String index out of range: 5"); + } +} From 72b942ef210411827b1f9d2603f2f428ea0e42c2 Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Thu, 26 Sep 2024 19:18:42 +0900 Subject: [PATCH 02/10] =?UTF-8?q?[Set]=20=ED=95=99=EC=8A=B5=ED=85=8C?= =?UTF-8?q?=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/SetCollectionTest.java | 42 ++++++++++++++++++++++++++++ src/test/java/StringTest.java | 10 +++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/test/java/SetCollectionTest.java b/src/test/java/SetCollectionTest.java index be120d6dc95..a647833785f 100644 --- a/src/test/java/SetCollectionTest.java +++ b/src/test/java/SetCollectionTest.java @@ -1,2 +1,44 @@ +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 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(2); + numbers.add(3); + } + + @Test + @DisplayName("Check the size of the Set") + void sizeCheckTest() { + assertThat(numbers.size()).isEqualTo(3); + } + + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + @DisplayName("Check if contains 1, 2, 3") + void isContainsTest(int input) { + assertThat(numbers.contains(input)).isTrue(); + } + + @ParameterizedTest + @CsvSource(value = {"1,true", "2,true", "3,true", "4,false", "5,false"}) + @DisplayName("Check if contains 1, 2, 3 or if does not contain 4, 5 throw false") + void isContainsTrueOrFalseTest(int input, boolean expected) { + assertThat(numbers.contains(input)).isEqualTo(expected); + } } diff --git a/src/test/java/StringTest.java b/src/test/java/StringTest.java index 1ad134bd41e..ce1b9d24ef6 100644 --- a/src/test/java/StringTest.java +++ b/src/test/java/StringTest.java @@ -8,20 +8,20 @@ public class StringTest { @Test @DisplayName("Split numbers and verify if 1,2 returns") - void SplitCheckTestWithOneTwo() { + void splitCheckTestWithOneTwoTest() { String[] result = "1,2".split(","); assertThat(result).containsExactly("1", "2"); } @Test @DisplayName("Split numbers and verify if 1 returns") - void SplitCheckTestWithOne() { + void splitCheckTestWithOneTest() { String[] result = "1".split(","); assertThat(result).containsExactly("1"); } @Test @DisplayName("Substring if there's Parenthesis exists") - void SubstringRemovalParenthesis() { + void substringRemovalParenthesisTest() { String input = "(1,2)"; String result = input.substring(1, input.length() - 1); @@ -30,7 +30,7 @@ void SubstringRemovalParenthesis() { @Test @DisplayName("Get index of specific string character") - void GetStringIndex() { + void getStringIndexTest() { String input = "abc"; char result = input.charAt(1); assertThat(result).isEqualTo('b'); @@ -38,7 +38,7 @@ void GetStringIndex() { @Test @DisplayName("Get index of specific string character but throw exception") - void GetStringIndexWithException() { + void getStringIndexWithExceptionTest() { String input = "abc"; assertThatThrownBy(() -> input.charAt(5)) From c08073713af0db06847b0ef5594384061b1191e3 Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Tue, 1 Oct 2024 00:57:30 +0900 Subject: [PATCH 03/10] =?UTF-8?q?[2=EB=8B=A8=EA=B3=84]=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EC=97=B4=20=EB=8D=A7=EC=85=88=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=EA=B8=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StringCalculator/StringCalculator.java | 24 +++++++++ .../StringCalculatorUtils.java | 38 ++++++++++++++ .../StringCalculatorTest.java | 50 +++++++++++++++++++ .../{ => TestExamples}/SetCollectionTest.java | 2 + .../java/{ => TestExamples}/StringTest.java | 2 + 5 files changed, 116 insertions(+) create mode 100644 src/main/java/StringCalculator/StringCalculator.java create mode 100644 src/main/java/StringCalculator/StringCalculatorUtils.java create mode 100644 src/test/java/StringCalculatorTest/StringCalculatorTest.java rename src/test/java/{ => TestExamples}/SetCollectionTest.java (98%) rename src/test/java/{ => TestExamples}/StringTest.java (98%) diff --git a/src/main/java/StringCalculator/StringCalculator.java b/src/main/java/StringCalculator/StringCalculator.java new file mode 100644 index 00000000000..1d1e3c91f56 --- /dev/null +++ b/src/main/java/StringCalculator/StringCalculator.java @@ -0,0 +1,24 @@ +package StringCalculator; + +import java.util.List; + +public class StringCalculator { + public static int splitAndSum(String numbers) { + + if (numbers == null || numbers.isEmpty()) { + return 0; + } + + String[] tokens = StringCalculatorUtils.splitStringCalculator(numbers); + List convertedInputs = StringCalculatorUtils.convertInputs(tokens); + + return sumInputs(convertedInputs); + } + private static int sumInputs(List inputs) { + + int sum = 0; + for (Integer input : inputs) sum += input; + + return sum; + } +} diff --git a/src/main/java/StringCalculator/StringCalculatorUtils.java b/src/main/java/StringCalculator/StringCalculatorUtils.java new file mode 100644 index 00000000000..47d5117cf50 --- /dev/null +++ b/src/main/java/StringCalculator/StringCalculatorUtils.java @@ -0,0 +1,38 @@ +package StringCalculator; + +import java.util.ArrayList; +import java.util.List; + +public class StringCalculatorUtils { + + public static String[] splitStringCalculator(String inputs) { + String DELEMETER = "[,:]"; + String splitedString =""; + + if (inputs.startsWith("//")) { + int separatorIndex = inputs.indexOf("\n"); + DELEMETER = inputs.substring(2, separatorIndex); + splitedString = inputs.substring(separatorIndex + 1); + } + + return splitedString.split(DELEMETER); + } + public static List convertInputs(String[] inputs) { + List result = new ArrayList<>(); + for (String input : inputs) { + int number = toPositiveInteger(input); + result.add(number); + } + return result; + } + + private static int toPositiveInteger(String input) { + int number = Integer.parseInt(input); + if (number < 0) { + throw new RuntimeException("There is wrong value included: " + input); + } + return number; + } + + +} diff --git a/src/test/java/StringCalculatorTest/StringCalculatorTest.java b/src/test/java/StringCalculatorTest/StringCalculatorTest.java new file mode 100644 index 00000000000..cb2d4887a17 --- /dev/null +++ b/src/test/java/StringCalculatorTest/StringCalculatorTest.java @@ -0,0 +1,50 @@ +package StringCalculatorTest; + +import StringCalculator.StringCalculator; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class StringCalculatorTest { + + @Test + public void splitAndSum_null_또는_빈문자() { + int result = StringCalculator.splitAndSum(null); + assertThat(result).isEqualTo(0); + + result = StringCalculator.splitAndSum(""); + assertThat(result).isEqualTo(0); + } + + @Test + public void splitAndSum_숫자하나() throws Exception { + int result = StringCalculator.splitAndSum("1"); + assertThat(result).isEqualTo(1); + } + + @Test + public void splitAndSum_쉼표구분자() throws Exception { + int result = StringCalculator.splitAndSum("1,2"); + assertThat(result).isEqualTo(3); + } + + @Test + public void splitAndSum_쉼표_또는_콜론_구분자() throws Exception { + int result = StringCalculator.splitAndSum("1,2:3"); + assertThat(result).isEqualTo(6); + } + + @Test + public void splitAndSum_customDelimiter() throws Exception { + int result = StringCalculator.splitAndSum("//;\n1;2;3"); + assertThat(result).isEqualTo(6); + } + + + @Test + public void splitAndSum_negative() throws Exception { + assertThatThrownBy(() -> StringCalculator.splitAndSum("-1,2,3")) + .isInstanceOf(RuntimeException.class); + } +} diff --git a/src/test/java/SetCollectionTest.java b/src/test/java/TestExamples/SetCollectionTest.java similarity index 98% rename from src/test/java/SetCollectionTest.java rename to src/test/java/TestExamples/SetCollectionTest.java index a647833785f..776ab9324ab 100644 --- a/src/test/java/SetCollectionTest.java +++ b/src/test/java/TestExamples/SetCollectionTest.java @@ -1,3 +1,5 @@ +package TestExamples; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/StringTest.java b/src/test/java/TestExamples/StringTest.java similarity index 98% rename from src/test/java/StringTest.java rename to src/test/java/TestExamples/StringTest.java index ce1b9d24ef6..961633494ac 100644 --- a/src/test/java/StringTest.java +++ b/src/test/java/TestExamples/StringTest.java @@ -1,3 +1,5 @@ +package TestExamples; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; From 116794b98902661e7cb1c6fb7d09f25d70a14959 Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Tue, 1 Oct 2024 01:52:30 +0900 Subject: [PATCH 04/10] =?UTF-8?q?[step2]=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EA=B3=84=EC=82=B0=EA=B8=B0=20test=20=EC=8B=A4=ED=96=89?= =?UTF-8?q?=EB=B6=88=EA=B0=80=EC=9D=B4=EC=8A=88=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StringAddCalculator/StringCalculator.java | 24 +++++++++ .../StringCalculatorUtils.java | 38 ++++++++++++++ src/test/java/StringCalculatorTest.java | 51 +++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/main/java/StringAddCalculator/StringCalculator.java create mode 100644 src/main/java/StringAddCalculator/StringCalculatorUtils.java create mode 100644 src/test/java/StringCalculatorTest.java diff --git a/src/main/java/StringAddCalculator/StringCalculator.java b/src/main/java/StringAddCalculator/StringCalculator.java new file mode 100644 index 00000000000..29661773f27 --- /dev/null +++ b/src/main/java/StringAddCalculator/StringCalculator.java @@ -0,0 +1,24 @@ +package StringAddCalculator; + +import java.util.List; + +public class StringCalculator { + public static int splitAndSum(String numbers) { + + if (numbers == null || numbers.isEmpty()) { + return 0; + } + + String[] tokens = StringCalculatorUtils.splitStringCalculator(numbers); + List convertedInputs = StringCalculatorUtils.convertInputs(tokens); + + return sumInputs(convertedInputs); + } + private static int sumInputs(List inputs) { + + int sum = 0; + for (Integer input : inputs) sum += input; + + return sum; + } +} \ No newline at end of file diff --git a/src/main/java/StringAddCalculator/StringCalculatorUtils.java b/src/main/java/StringAddCalculator/StringCalculatorUtils.java new file mode 100644 index 00000000000..9b114a49cd5 --- /dev/null +++ b/src/main/java/StringAddCalculator/StringCalculatorUtils.java @@ -0,0 +1,38 @@ +package StringAddCalculator; + +import java.util.ArrayList; +import java.util.List; + +public class StringCalculatorUtils { + + public static String[] splitStringCalculator(String inputs) { + String DELEMETER = ",|:"; + String splitedString = inputs; + + if (inputs.startsWith("//")) { + int separatorIndex = inputs.indexOf("\n"); + DELEMETER = inputs.substring(2, separatorIndex); + splitedString = inputs.substring(separatorIndex + 1); + } + + return splitedString.split(DELEMETER); + } + public static List convertInputs(String[] inputs) { + List result = new ArrayList<>(); + for (String input : inputs) { + int number = toPositiveInteger(input); + result.add(number); + } + return result; + } + + private static int toPositiveInteger(String input) { + int number = Integer.parseInt(input); + if (number < 0) { + throw new RuntimeException("There is wrong value included: " + input); + } + return number; + } + + +} \ No newline at end of file diff --git a/src/test/java/StringCalculatorTest.java b/src/test/java/StringCalculatorTest.java new file mode 100644 index 00000000000..cfdedc1f593 --- /dev/null +++ b/src/test/java/StringCalculatorTest.java @@ -0,0 +1,51 @@ +import StringAddCalculator.StringCalculator; +import org.junit.jupiter.api.Test; + + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class StringCalculatorTest { + + + private final StringCalculator stringCalculator = new StringCalculator(); + @Test + public void splitAndSum_null_또는_빈문자() { + int result = stringCalculator.splitAndSum(null); + assertThat(result).isEqualTo(0); + + result = stringCalculator.splitAndSum(""); + assertThat(result).isEqualTo(0); + } + + @Test + public void splitAndSum_숫자하나() throws Exception { + int result = stringCalculator.splitAndSum("1"); + assertThat(result).isEqualTo(1); + } + + @Test + public void splitAndSum_쉼표구분자() throws Exception { + int result = stringCalculator.splitAndSum("1,2"); + assertThat(result).isEqualTo(3); + } + + @Test + public void splitAndSum_쉼표_또는_콜론_구분자() throws Exception { + int result = stringCalculator.splitAndSum("1,2:3"); + assertThat(result).isEqualTo(6); + } + + @Test + public void splitAndSum_customDelimiter() throws Exception { + int result = stringCalculator.splitAndSum("//;\n1;2;3"); + assertThat(result).isEqualTo(6); + } + + + @Test + public void splitAndSum_negative() throws Exception { + assertThatThrownBy(() -> stringCalculator.splitAndSum("-1,2,3")) + .isInstanceOf(RuntimeException.class); + } +} \ No newline at end of file From aa35eebe44a45f542d78972cd6181054199bd440 Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Tue, 1 Oct 2024 02:10:53 +0900 Subject: [PATCH 05/10] =?UTF-8?q?[step2]=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EA=B3=84=EC=82=B0=EA=B8=B0=20test=20=EC=8B=A4=ED=96=89?= =?UTF-8?q?=EB=B6=88=EA=B0=80=EC=9D=B4=EC=8A=88=20=EC=88=98=EC=A0=95=20-?= =?UTF-8?q?=20=EC=A4=91=EB=B3=B5=ED=8C=8C=EC=9D=BC=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StringCalculator/StringCalculator.java | 24 --------- .../StringCalculatorUtils.java | 38 -------------- .../StringCalculatorTest.java | 50 ------------------- 3 files changed, 112 deletions(-) delete mode 100644 src/main/java/StringCalculator/StringCalculator.java delete mode 100644 src/main/java/StringCalculator/StringCalculatorUtils.java delete mode 100644 src/test/java/StringCalculatorTest/StringCalculatorTest.java diff --git a/src/main/java/StringCalculator/StringCalculator.java b/src/main/java/StringCalculator/StringCalculator.java deleted file mode 100644 index 1d1e3c91f56..00000000000 --- a/src/main/java/StringCalculator/StringCalculator.java +++ /dev/null @@ -1,24 +0,0 @@ -package StringCalculator; - -import java.util.List; - -public class StringCalculator { - public static int splitAndSum(String numbers) { - - if (numbers == null || numbers.isEmpty()) { - return 0; - } - - String[] tokens = StringCalculatorUtils.splitStringCalculator(numbers); - List convertedInputs = StringCalculatorUtils.convertInputs(tokens); - - return sumInputs(convertedInputs); - } - private static int sumInputs(List inputs) { - - int sum = 0; - for (Integer input : inputs) sum += input; - - return sum; - } -} diff --git a/src/main/java/StringCalculator/StringCalculatorUtils.java b/src/main/java/StringCalculator/StringCalculatorUtils.java deleted file mode 100644 index 47d5117cf50..00000000000 --- a/src/main/java/StringCalculator/StringCalculatorUtils.java +++ /dev/null @@ -1,38 +0,0 @@ -package StringCalculator; - -import java.util.ArrayList; -import java.util.List; - -public class StringCalculatorUtils { - - public static String[] splitStringCalculator(String inputs) { - String DELEMETER = "[,:]"; - String splitedString =""; - - if (inputs.startsWith("//")) { - int separatorIndex = inputs.indexOf("\n"); - DELEMETER = inputs.substring(2, separatorIndex); - splitedString = inputs.substring(separatorIndex + 1); - } - - return splitedString.split(DELEMETER); - } - public static List convertInputs(String[] inputs) { - List result = new ArrayList<>(); - for (String input : inputs) { - int number = toPositiveInteger(input); - result.add(number); - } - return result; - } - - private static int toPositiveInteger(String input) { - int number = Integer.parseInt(input); - if (number < 0) { - throw new RuntimeException("There is wrong value included: " + input); - } - return number; - } - - -} diff --git a/src/test/java/StringCalculatorTest/StringCalculatorTest.java b/src/test/java/StringCalculatorTest/StringCalculatorTest.java deleted file mode 100644 index cb2d4887a17..00000000000 --- a/src/test/java/StringCalculatorTest/StringCalculatorTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package StringCalculatorTest; - -import StringCalculator.StringCalculator; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -public class StringCalculatorTest { - - @Test - public void splitAndSum_null_또는_빈문자() { - int result = StringCalculator.splitAndSum(null); - assertThat(result).isEqualTo(0); - - result = StringCalculator.splitAndSum(""); - assertThat(result).isEqualTo(0); - } - - @Test - public void splitAndSum_숫자하나() throws Exception { - int result = StringCalculator.splitAndSum("1"); - assertThat(result).isEqualTo(1); - } - - @Test - public void splitAndSum_쉼표구분자() throws Exception { - int result = StringCalculator.splitAndSum("1,2"); - assertThat(result).isEqualTo(3); - } - - @Test - public void splitAndSum_쉼표_또는_콜론_구분자() throws Exception { - int result = StringCalculator.splitAndSum("1,2:3"); - assertThat(result).isEqualTo(6); - } - - @Test - public void splitAndSum_customDelimiter() throws Exception { - int result = StringCalculator.splitAndSum("//;\n1;2;3"); - assertThat(result).isEqualTo(6); - } - - - @Test - public void splitAndSum_negative() throws Exception { - assertThatThrownBy(() -> StringCalculator.splitAndSum("-1,2,3")) - .isInstanceOf(RuntimeException.class); - } -} From 98314d61078b66ff9c60bd2007ec781e32ec33d0 Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Tue, 1 Oct 2024 15:08:40 +0900 Subject: [PATCH 06/10] =?UTF-8?q?[step2]=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EA=B3=84=EC=82=B0=EA=B8=B0=20=EB=A6=AC=EB=B7=B0=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/StringAddCalculator/StringCalculatorUtils.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/StringAddCalculator/StringCalculatorUtils.java b/src/main/java/StringAddCalculator/StringCalculatorUtils.java index 9b114a49cd5..0f00df3de79 100644 --- a/src/main/java/StringAddCalculator/StringCalculatorUtils.java +++ b/src/main/java/StringAddCalculator/StringCalculatorUtils.java @@ -4,18 +4,19 @@ import java.util.List; public class StringCalculatorUtils { + private static final String DELEMETER = ",|:"; public static String[] splitStringCalculator(String inputs) { - String DELEMETER = ",|:"; String splitedString = inputs; + String currentDelimeter = DELEMETER; if (inputs.startsWith("//")) { int separatorIndex = inputs.indexOf("\n"); - DELEMETER = inputs.substring(2, separatorIndex); + currentDelimeter = inputs.substring(2, separatorIndex); // 사용자 정의 구분자를 currentDelimeter에 저장 splitedString = inputs.substring(separatorIndex + 1); } - return splitedString.split(DELEMETER); + return splitedString.split(currentDelimeter); } public static List convertInputs(String[] inputs) { List result = new ArrayList<>(); @@ -29,7 +30,7 @@ public static List convertInputs(String[] inputs) { private static int toPositiveInteger(String input) { int number = Integer.parseInt(input); if (number < 0) { - throw new RuntimeException("There is wrong value included: " + input); + throw new IllegalArgumentException("There is wrong value included: " + input); } return number; } From 0699d62b6a0fddf365d3bd9d01924626b7ff4a38 Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Wed, 2 Oct 2024 02:57:18 +0900 Subject: [PATCH 07/10] =?UTF-8?q?[step=203]=20=EC=9E=90=EB=8F=99=EC=B0=A8?= =?UTF-8?q?=20=EA=B2=BD=EC=A3=BC=20=EA=B5=AC=ED=98=84=20-=20test=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=ED=95=84=EC=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/CarRacingGame/CarRacingGame.java | 17 ++++++++ .../CarRacingGame/CarRacingGameUtils.java | 42 +++++++++++++++++++ src/main/java/CarRacingGame/InputView.java | 19 +++++++++ src/main/java/CarRacingGame/PrintView.java | 12 ++++++ src/test/java/carRacingUtilsTest.java | 30 +++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 src/main/java/CarRacingGame/CarRacingGame.java create mode 100644 src/main/java/CarRacingGame/CarRacingGameUtils.java create mode 100644 src/main/java/CarRacingGame/InputView.java create mode 100644 src/main/java/CarRacingGame/PrintView.java create mode 100644 src/test/java/carRacingUtilsTest.java diff --git a/src/main/java/CarRacingGame/CarRacingGame.java b/src/main/java/CarRacingGame/CarRacingGame.java new file mode 100644 index 00000000000..b9f5b080b02 --- /dev/null +++ b/src/main/java/CarRacingGame/CarRacingGame.java @@ -0,0 +1,17 @@ +package CarRacingGame; + +import java.util.Scanner; + +public class CarRacingGame { + static CarRacingGameUtils cars = new CarRacingGameUtils(); + + public static void main(String[] args) { + + int numberOfCars = InputView.getNumberOfCars(); + int numberOfAttempts = InputView.getNumberOfAttempts(); + + cars.initialCarSettings(numberOfCars); + cars.printMovingCars(numberOfAttempts); + } +} + diff --git a/src/main/java/CarRacingGame/CarRacingGameUtils.java b/src/main/java/CarRacingGame/CarRacingGameUtils.java new file mode 100644 index 00000000000..d3bad952e0d --- /dev/null +++ b/src/main/java/CarRacingGame/CarRacingGameUtils.java @@ -0,0 +1,42 @@ +package CarRacingGame; + +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +import static CarRacingGame.PrintView.printCars; + +public class CarRacingGameUtils { + private Map cars; + + public Map initialCarSettings(Integer numberOfCars) { + cars = new HashMap<>(); + for (int i = 1; i <= numberOfCars; i++) { + cars.put("car" + i, "-"); + } + return cars; + } + + public void printMovingCars(int numberOfAttempts) { + System.out.println("실행 결과"); + for (int i = 0; i < numberOfAttempts; i++) { + PrintView.printCars(cars); + moveCars(); + System.out.println(); + } + } + public void moveCars() { + cars.forEach((carNumber, currentPosition) -> { + String newPosition = makeDistanceOfCars(currentPosition); + cars.put(carNumber, newPosition); + }); + } + public String makeDistanceOfCars(String currentPosition) { + Random random = new Random(); + int randomValue = random.nextInt(10); + if (randomValue >= 4) { + currentPosition += "-"; + } + return currentPosition; + } +} diff --git a/src/main/java/CarRacingGame/InputView.java b/src/main/java/CarRacingGame/InputView.java new file mode 100644 index 00000000000..e6e440410ce --- /dev/null +++ b/src/main/java/CarRacingGame/InputView.java @@ -0,0 +1,19 @@ +package CarRacingGame; + +import java.util.Scanner; + +public class InputView { + private static final Scanner scanner = new Scanner(System.in); + + public static int getNumberOfCars() { + System.out.println("자동차 대수는 몇 대 인가요?"); + int numberOfCars = scanner.nextInt(); + return numberOfCars; + } + + public static int getNumberOfAttempts() { + System.out.println("시도할 회수는 몇 회 인가요?"); + int numberOfAttempts = scanner.nextInt(); + return numberOfAttempts; + } +} diff --git a/src/main/java/CarRacingGame/PrintView.java b/src/main/java/CarRacingGame/PrintView.java new file mode 100644 index 00000000000..3cbf65279ee --- /dev/null +++ b/src/main/java/CarRacingGame/PrintView.java @@ -0,0 +1,12 @@ +package CarRacingGame; + +import java.util.Map; + +public class PrintView { + + public static void printCars(Map cars) { + cars.forEach((car, position) -> { + System.out.println(position); // 각 자동차의 위치 출력 + }); + } +} diff --git a/src/test/java/carRacingUtilsTest.java b/src/test/java/carRacingUtilsTest.java new file mode 100644 index 00000000000..be96068c532 --- /dev/null +++ b/src/test/java/carRacingUtilsTest.java @@ -0,0 +1,30 @@ +import CarRacingGame.CarRacingGameUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class carRacingUtilsTest { + + private CarRacingGameUtils carRacingGameUtils; + + @BeforeEach + public void setUp() { + carRacingGameUtils = new CarRacingGameUtils(); + } + + @Test + @DisplayName("Set the numbers of the cars and check proper numbers of items") + public void testInitialCarSettings() { + carRacingGameUtils.initialCarSettings(3); + Map cars = carRacingGameUtils.initialCarSettings(3); // 자동차 상태 확인 + + assertEquals(3, cars.size()); + + } + +} From de4cc8db41198531d93c416fb8c8d9a1de4f98cc Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Wed, 2 Oct 2024 02:58:45 +0900 Subject: [PATCH 08/10] =?UTF-8?q?[step=203]=20=EC=9E=90=EB=8F=99=EC=B0=A8?= =?UTF-8?q?=20=EA=B2=BD=EC=A3=BC=20=EA=B5=AC=ED=98=84=20-=20test=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=ED=95=84=EC=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/CarRacingGame/CarRacingGameUtils.java | 4 ++-- src/main/java/CarRacingGame/PrintView.java | 2 +- src/test/java/carRacingUtilsTest.java | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/CarRacingGame/CarRacingGameUtils.java b/src/main/java/CarRacingGame/CarRacingGameUtils.java index d3bad952e0d..74afe4c4e5c 100644 --- a/src/main/java/CarRacingGame/CarRacingGameUtils.java +++ b/src/main/java/CarRacingGame/CarRacingGameUtils.java @@ -4,8 +4,6 @@ import java.util.Map; import java.util.Random; -import static CarRacingGame.PrintView.printCars; - public class CarRacingGameUtils { private Map cars; @@ -25,12 +23,14 @@ public void printMovingCars(int numberOfAttempts) { System.out.println(); } } + public void moveCars() { cars.forEach((carNumber, currentPosition) -> { String newPosition = makeDistanceOfCars(currentPosition); cars.put(carNumber, newPosition); }); } + public String makeDistanceOfCars(String currentPosition) { Random random = new Random(); int randomValue = random.nextInt(10); diff --git a/src/main/java/CarRacingGame/PrintView.java b/src/main/java/CarRacingGame/PrintView.java index 3cbf65279ee..e2e33933427 100644 --- a/src/main/java/CarRacingGame/PrintView.java +++ b/src/main/java/CarRacingGame/PrintView.java @@ -6,7 +6,7 @@ public class PrintView { public static void printCars(Map cars) { cars.forEach((car, position) -> { - System.out.println(position); // 각 자동차의 위치 출력 + System.out.println(position); }); } } diff --git a/src/test/java/carRacingUtilsTest.java b/src/test/java/carRacingUtilsTest.java index be96068c532..e3c7f246bc0 100644 --- a/src/test/java/carRacingUtilsTest.java +++ b/src/test/java/carRacingUtilsTest.java @@ -6,7 +6,6 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; public class carRacingUtilsTest { From 366d1c1a995fd3dd2209b9e8c396bdcc3a38390d Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Wed, 2 Oct 2024 11:26:11 +0900 Subject: [PATCH 09/10] =?UTF-8?q?[step=203]=20=EC=9E=90=EB=8F=99=EC=B0=A8?= =?UTF-8?q?=20=EA=B2=BD=EC=A3=BC=20=EA=B5=AC=ED=98=84=20-=20test=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80/=EB=B3=80=EC=88=98=20=EB=B3=80=EA=B2=BD/exce?= =?UTF-8?q?ption=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/CarRacingGame/CarRacingGame.java | 6 ++---- .../java/CarRacingGame/CarRacingGameUtils.java | 10 ++++++---- src/main/java/CarRacingGame/InputView.java | 10 ++++++++-- ...ingUtilsTest.java => CarRacingUtilsTest.java} | 16 +++++++++++++--- 4 files changed, 29 insertions(+), 13 deletions(-) rename src/test/java/{carRacingUtilsTest.java => CarRacingUtilsTest.java} (58%) diff --git a/src/main/java/CarRacingGame/CarRacingGame.java b/src/main/java/CarRacingGame/CarRacingGame.java index b9f5b080b02..f0166f6e228 100644 --- a/src/main/java/CarRacingGame/CarRacingGame.java +++ b/src/main/java/CarRacingGame/CarRacingGame.java @@ -1,14 +1,12 @@ package CarRacingGame; -import java.util.Scanner; - public class CarRacingGame { static CarRacingGameUtils cars = new CarRacingGameUtils(); public static void main(String[] args) { - int numberOfCars = InputView.getNumberOfCars(); - int numberOfAttempts = InputView.getNumberOfAttempts(); + int numberOfCars = InputView.inputNumberOfCars(); + int numberOfAttempts = InputView.inputNumberOfAttempts(); cars.initialCarSettings(numberOfCars); cars.printMovingCars(numberOfAttempts); diff --git a/src/main/java/CarRacingGame/CarRacingGameUtils.java b/src/main/java/CarRacingGame/CarRacingGameUtils.java index 74afe4c4e5c..9a13dec80e9 100644 --- a/src/main/java/CarRacingGame/CarRacingGameUtils.java +++ b/src/main/java/CarRacingGame/CarRacingGameUtils.java @@ -5,19 +5,21 @@ import java.util.Random; public class CarRacingGameUtils { - private Map cars; + public Map cars; + // TODO. 초기 차 인풋을 받고 차의 이동거리가 1인 상태에서 시작하는지, + // 차의 이동거리가 0 인상태에서 시작되는 것인지 확인 필요 public Map initialCarSettings(Integer numberOfCars) { cars = new HashMap<>(); - for (int i = 1; i <= numberOfCars; i++) { - cars.put("car" + i, "-"); + for (int carNum = 1; carNum <= numberOfCars; carNum++) { + cars.put("car" + carNum, "-"); } return cars; } public void printMovingCars(int numberOfAttempts) { System.out.println("실행 결과"); - for (int i = 0; i < numberOfAttempts; i++) { + for (int attempt = 0; attempt < numberOfAttempts; attempt++) { PrintView.printCars(cars); moveCars(); System.out.println(); diff --git a/src/main/java/CarRacingGame/InputView.java b/src/main/java/CarRacingGame/InputView.java index e6e440410ce..85f28682089 100644 --- a/src/main/java/CarRacingGame/InputView.java +++ b/src/main/java/CarRacingGame/InputView.java @@ -5,15 +5,21 @@ public class InputView { private static final Scanner scanner = new Scanner(System.in); - public static int getNumberOfCars() { + public static int inputNumberOfCars() { System.out.println("자동차 대수는 몇 대 인가요?"); int numberOfCars = scanner.nextInt(); + if (numberOfCars <= 0) { + throw new IllegalArgumentException("Number of cars should be positive"); + } return numberOfCars; } - public static int getNumberOfAttempts() { + public static int inputNumberOfAttempts() { System.out.println("시도할 회수는 몇 회 인가요?"); int numberOfAttempts = scanner.nextInt(); + if (numberOfAttempts <= 0) { + throw new IllegalArgumentException("Number of attempts should be positive"); + } return numberOfAttempts; } } diff --git a/src/test/java/carRacingUtilsTest.java b/src/test/java/CarRacingUtilsTest.java similarity index 58% rename from src/test/java/carRacingUtilsTest.java rename to src/test/java/CarRacingUtilsTest.java index e3c7f246bc0..e51801c99d3 100644 --- a/src/test/java/carRacingUtilsTest.java +++ b/src/test/java/CarRacingUtilsTest.java @@ -6,8 +6,9 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class carRacingUtilsTest { +public class CarRacingUtilsTest { private CarRacingGameUtils carRacingGameUtils; @@ -20,10 +21,19 @@ public void setUp() { @DisplayName("Set the numbers of the cars and check proper numbers of items") public void testInitialCarSettings() { carRacingGameUtils.initialCarSettings(3); - Map cars = carRacingGameUtils.initialCarSettings(3); // 자동차 상태 확인 - + Map cars = carRacingGameUtils.initialCarSettings(3); assertEquals(3, cars.size()); } + @Test + @DisplayName("Set the numbers of the cars and check cars move properly") + public void testMoveCars() { + int numberOfCars = 2; + carRacingGameUtils.initialCarSettings(numberOfCars); + carRacingGameUtils.moveCars(); + carRacingGameUtils.cars.forEach((car, position) -> { + assertTrue(position.equals("-") || position.equals("--")); + }); + } } From 9171fece0f439c2ed54e763504362921abedf96c Mon Sep 17 00:00:00 2001 From: 6democratickim9 Date: Mon, 7 Oct 2024 10:24:05 +0900 Subject: [PATCH 10/10] =?UTF-8?q?[step=203]=20=EC=88=98=EC=A0=95=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarRacingGame/CarRacingGameUtils.java | 44 ------------ src/main/java/CarRacingGame/PrintView.java | 12 ---- .../CarRacingGame.java | 10 +-- .../carRacingGame/CarRacingGameUtils.java | 67 +++++++++++++++++++ .../InputView.java | 12 +++- src/main/java/carRacingGame/PrintView.java | 17 +++++ .../StringCalculator.java | 2 +- .../StringCalculatorUtils.java | 2 +- src/test/java/CarRacingUtilsTest.java | 48 +++++++++---- src/test/java/StringCalculatorTest.java | 2 +- 10 files changed, 139 insertions(+), 77 deletions(-) delete mode 100644 src/main/java/CarRacingGame/CarRacingGameUtils.java delete mode 100644 src/main/java/CarRacingGame/PrintView.java rename src/main/java/{CarRacingGame => carRacingGame}/CarRacingGame.java (51%) create mode 100644 src/main/java/carRacingGame/CarRacingGameUtils.java rename src/main/java/{CarRacingGame => carRacingGame}/InputView.java (60%) create mode 100644 src/main/java/carRacingGame/PrintView.java rename src/main/java/{StringAddCalculator => stringAddCalculator}/StringCalculator.java (95%) rename src/main/java/{StringAddCalculator => stringAddCalculator}/StringCalculatorUtils.java (97%) diff --git a/src/main/java/CarRacingGame/CarRacingGameUtils.java b/src/main/java/CarRacingGame/CarRacingGameUtils.java deleted file mode 100644 index 9a13dec80e9..00000000000 --- a/src/main/java/CarRacingGame/CarRacingGameUtils.java +++ /dev/null @@ -1,44 +0,0 @@ -package CarRacingGame; - -import java.util.HashMap; -import java.util.Map; -import java.util.Random; - -public class CarRacingGameUtils { - public Map cars; - - // TODO. 초기 차 인풋을 받고 차의 이동거리가 1인 상태에서 시작하는지, - // 차의 이동거리가 0 인상태에서 시작되는 것인지 확인 필요 - public Map initialCarSettings(Integer numberOfCars) { - cars = new HashMap<>(); - for (int carNum = 1; carNum <= numberOfCars; carNum++) { - cars.put("car" + carNum, "-"); - } - return cars; - } - - public void printMovingCars(int numberOfAttempts) { - System.out.println("실행 결과"); - for (int attempt = 0; attempt < numberOfAttempts; attempt++) { - PrintView.printCars(cars); - moveCars(); - System.out.println(); - } - } - - public void moveCars() { - cars.forEach((carNumber, currentPosition) -> { - String newPosition = makeDistanceOfCars(currentPosition); - cars.put(carNumber, newPosition); - }); - } - - public String makeDistanceOfCars(String currentPosition) { - Random random = new Random(); - int randomValue = random.nextInt(10); - if (randomValue >= 4) { - currentPosition += "-"; - } - return currentPosition; - } -} diff --git a/src/main/java/CarRacingGame/PrintView.java b/src/main/java/CarRacingGame/PrintView.java deleted file mode 100644 index e2e33933427..00000000000 --- a/src/main/java/CarRacingGame/PrintView.java +++ /dev/null @@ -1,12 +0,0 @@ -package CarRacingGame; - -import java.util.Map; - -public class PrintView { - - public static void printCars(Map cars) { - cars.forEach((car, position) -> { - System.out.println(position); - }); - } -} diff --git a/src/main/java/CarRacingGame/CarRacingGame.java b/src/main/java/carRacingGame/CarRacingGame.java similarity index 51% rename from src/main/java/CarRacingGame/CarRacingGame.java rename to src/main/java/carRacingGame/CarRacingGame.java index f0166f6e228..87f4f885639 100644 --- a/src/main/java/CarRacingGame/CarRacingGame.java +++ b/src/main/java/carRacingGame/CarRacingGame.java @@ -1,15 +1,15 @@ -package CarRacingGame; +package carRacingGame; public class CarRacingGame { static CarRacingGameUtils cars = new CarRacingGameUtils(); public static void main(String[] args) { - - int numberOfCars = InputView.inputNumberOfCars(); + String[] nameOfCars = InputView.inputNameOfCars(); int numberOfAttempts = InputView.inputNumberOfAttempts(); - cars.initialCarSettings(numberOfCars); - cars.printMovingCars(numberOfAttempts); + cars.setupCars(nameOfCars); + cars.displayRaceProgress(numberOfAttempts); + cars.printWinnerName(); } } diff --git a/src/main/java/carRacingGame/CarRacingGameUtils.java b/src/main/java/carRacingGame/CarRacingGameUtils.java new file mode 100644 index 00000000000..61b5c440cab --- /dev/null +++ b/src/main/java/carRacingGame/CarRacingGameUtils.java @@ -0,0 +1,67 @@ +package carRacingGame; + +import java.util.*; + +import static carRacingGame.PrintView.printWinners; + +public class CarRacingGameUtils { + public Map cars = new HashMap<>(); + public List finalWinners = new ArrayList<>(); + + public Map setupCars(String[] inputNameOfCars) { + for (String carName : inputNameOfCars) { + cars.put(carName, ""); + } + return cars; + } + + public void displayRaceProgress(int numberOfAttempts) { + System.out.println("실행 결과"); + for (int attempt = 0; attempt < numberOfAttempts; attempt++) { + moveCars(); + PrintView.printCars(cars); + System.out.println(); + } + } + + public void moveCars() { + cars.forEach((carName, currentPosition) -> { + String newPosition = moveForward(currentPosition); + cars.put(carName, newPosition); + }); + } + + + public String moveForward(String currentPosition) { + Random random = new Random(); + int randomValue = random.nextInt(10); + if (randomValue >= 4) { + currentPosition += "-"; + } + return currentPosition; + } + + + public void printWinnerName() { + racingResult(); + String winnerNames = ""; + winnerNames = String.join(", ", finalWinners); + printWinners(winnerNames); + } + + public void racingResult(){ + String furthestDistance = Collections.max(cars.values()); + int furthestDistanceOfCar = furthestDistance.length(); + + cars.forEach((carName, finalDistance) -> { + int carNameDistance = finalDistance.length(); + getCurrentWinner(carName, furthestDistanceOfCar, carNameDistance); + }); + + } + public void getCurrentWinner(String carName, int furthestDistanceOfCar, int carNameDistance) { + if (carNameDistance >= furthestDistanceOfCar) { + finalWinners.add(carName); + } + } +} diff --git a/src/main/java/CarRacingGame/InputView.java b/src/main/java/carRacingGame/InputView.java similarity index 60% rename from src/main/java/CarRacingGame/InputView.java rename to src/main/java/carRacingGame/InputView.java index 85f28682089..d111e14d763 100644 --- a/src/main/java/CarRacingGame/InputView.java +++ b/src/main/java/carRacingGame/InputView.java @@ -1,10 +1,20 @@ -package CarRacingGame; +package carRacingGame; import java.util.Scanner; public class InputView { private static final Scanner scanner = new Scanner(System.in); + private static final String splitter = ","; + public static String[] inputNameOfCars() { + System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); + String nameOfCars = scanner.next(); + if (!(nameOfCars instanceof String)) { + throw new IllegalArgumentException("Name of cars should be string type"); + } + String[] listedCars = nameOfCars.split(splitter); + return listedCars; + } public static int inputNumberOfCars() { System.out.println("자동차 대수는 몇 대 인가요?"); int numberOfCars = scanner.nextInt(); diff --git a/src/main/java/carRacingGame/PrintView.java b/src/main/java/carRacingGame/PrintView.java new file mode 100644 index 00000000000..c36ee3cca10 --- /dev/null +++ b/src/main/java/carRacingGame/PrintView.java @@ -0,0 +1,17 @@ +package carRacingGame; + +import java.util.Map; + +public class PrintView { + + public static void printCars(Map cars) { + cars.forEach((carName, position) -> { + System.out.println(carName + " : " + position); + }); + } + + public static void printWinners(String winnerNames) { + System.out.println(winnerNames + "가 최종 우승했습니다."); + + } +} diff --git a/src/main/java/StringAddCalculator/StringCalculator.java b/src/main/java/stringAddCalculator/StringCalculator.java similarity index 95% rename from src/main/java/StringAddCalculator/StringCalculator.java rename to src/main/java/stringAddCalculator/StringCalculator.java index 29661773f27..ae2af39d8f8 100644 --- a/src/main/java/StringAddCalculator/StringCalculator.java +++ b/src/main/java/stringAddCalculator/StringCalculator.java @@ -1,4 +1,4 @@ -package StringAddCalculator; +package stringAddCalculator; import java.util.List; diff --git a/src/main/java/StringAddCalculator/StringCalculatorUtils.java b/src/main/java/stringAddCalculator/StringCalculatorUtils.java similarity index 97% rename from src/main/java/StringAddCalculator/StringCalculatorUtils.java rename to src/main/java/stringAddCalculator/StringCalculatorUtils.java index 40a7febb9be..534d086bbdd 100644 --- a/src/main/java/StringAddCalculator/StringCalculatorUtils.java +++ b/src/main/java/stringAddCalculator/StringCalculatorUtils.java @@ -1,4 +1,4 @@ -package StringAddCalculator; +package stringAddCalculator; import java.util.ArrayList; import java.util.List; diff --git a/src/test/java/CarRacingUtilsTest.java b/src/test/java/CarRacingUtilsTest.java index e51801c99d3..c47daaee798 100644 --- a/src/test/java/CarRacingUtilsTest.java +++ b/src/test/java/CarRacingUtilsTest.java @@ -1,12 +1,12 @@ -import CarRacingGame.CarRacingGameUtils; + +import carRacingGame.CarRacingGameUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class CarRacingUtilsTest { @@ -18,22 +18,46 @@ public void setUp() { } @Test - @DisplayName("Set the numbers of the cars and check proper numbers of items") - public void testInitialCarSettings() { - carRacingGameUtils.initialCarSettings(3); - Map cars = carRacingGameUtils.initialCarSettings(3); + @DisplayName("Test if car names are properly saved") + public void testSetupCars() { + String[] carNames = {"Car1", "Car2", "Car3"}; + + Map cars = carRacingGameUtils.setupCars(carNames); + assertEquals(3, cars.size()); + assertTrue(cars.containsKey("Car1")); + assertTrue(cars.containsKey("Car2")); + assertTrue(cars.containsKey("Car3")); + } + @Test + public void testMakeDistanceOfCars() { + + String initialPosition = ""; + String newPosition = carRacingGameUtils.moveForward(initialPosition); + assertTrue(newPosition.equals("") || newPosition.equals("-")); } @Test - @DisplayName("Set the numbers of the cars and check cars move properly") public void testMoveCars() { - int numberOfCars = 2; - carRacingGameUtils.initialCarSettings(numberOfCars); + + String[] carNames = {"Car1", "Car2"}; + carRacingGameUtils.setupCars(carNames); carRacingGameUtils.moveCars(); - carRacingGameUtils.cars.forEach((car, position) -> { - assertTrue(position.equals("-") || position.equals("--")); + carRacingGameUtils.cars.forEach((carName, position) -> { + assertTrue(position.equals("") || position.equals("-")); }); } + + @Test + @DisplayName("test if winners found properly") + public void testRacingResult() { + + String[] carNames = {"Car1", "Car2"}; + carRacingGameUtils.setupCars(carNames); + carRacingGameUtils.moveCars(); + carRacingGameUtils.racingResult(); + + assertFalse(carRacingGameUtils.finalWinners.isEmpty()); + } } diff --git a/src/test/java/StringCalculatorTest.java b/src/test/java/StringCalculatorTest.java index cfdedc1f593..780414eb8b5 100644 --- a/src/test/java/StringCalculatorTest.java +++ b/src/test/java/StringCalculatorTest.java @@ -1,4 +1,4 @@ -import StringAddCalculator.StringCalculator; +import stringAddCalculator.StringCalculator; import org.junit.jupiter.api.Test;