From 69a99981d1e6d7663909b12ea2b57026b02f7ac4 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 1 Nov 2023 19:52:53 +0900 Subject: [PATCH 01/26] split study test --- src/test/java/.gitkeep | 0 src/test/java/study/StringTest.java | 14 ++++++++++++++ 2 files changed, 14 insertions(+) delete mode 100644 src/test/java/.gitkeep create mode 100644 src/test/java/study/StringTest.java diff --git a/src/test/java/.gitkeep b/src/test/java/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java new file mode 100644 index 00000000000..bb36bb47c9d --- /dev/null +++ b/src/test/java/study/StringTest.java @@ -0,0 +1,14 @@ +package study; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class StringTest { + @Test + void split() { + String[] result = "1,2".split(","); + assertThat(result).containsExactly("1", "2"); + } +} From cc3c46de90998407b8fb1144984bff80af277d18 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 1 Nov 2023 20:12:17 +0900 Subject: [PATCH 02/26] split study test --- src/test/java/study/StringTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index bb36bb47c9d..10051e6aacd 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -7,7 +7,13 @@ public class StringTest { @Test - void split() { + public void split1() { + String[] result = "1,2".split(","); + assertThat(result).contains("1"); + } + + @Test + public void split2() { String[] result = "1,2".split(","); assertThat(result).containsExactly("1", "2"); } From 61eb83c6b41ffc3e0a7bd749b8639d8171b763a8 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Thu, 2 Nov 2023 15:19:28 +0900 Subject: [PATCH 03/26] step 1 practice --- src/test/java/study/SetCollectionTest.java | 48 ++++++++++++++++++++++ src/test/java/study/StringTest.java | 21 +++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/test/java/study/SetCollectionTest.java diff --git a/src/test/java/study/SetCollectionTest.java b/src/test/java/study/SetCollectionTest.java new file mode 100644 index 00000000000..22a121472ca --- /dev/null +++ b/src/test/java/study/SetCollectionTest.java @@ -0,0 +1,48 @@ +package study; + +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; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SetCollectionTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + // Test Case 구현 + @Test + @DisplayName("요구사항 1. check size of Set") + void checkSizeOfSet() { + int setSize = 3; + assertThat(numbers).hasSize(setSize); + assertEquals(3, numbers.size(), "Set의 크기는 3이어야 함"); + } + + @ParameterizedTest(name="요구사항 2. check contain - value={0}") + @ValueSource(ints = {1, 2, 3}) + void contains(int value) { + assertThat(numbers.contains(value)).isTrue(); + } + + @ParameterizedTest(name="요구사항 3. check contain - value={0}, expected={1}") + @CsvSource({"1, true", "2, true", "3, true", "4, false", "5, false"}) + void contains2(int value, boolean expected) { + assertThat(numbers.contains(value)).isEqualTo(expected); + } +} diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index 10051e6aacd..ed7c7a76fa0 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,20 +1,39 @@ 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.assertj.core.api.Assertions.*; public class StringTest { @Test + @DisplayName("요구사항 1 contains() 테스트") public void split1() { String[] result = "1,2".split(","); assertThat(result).contains("1"); } @Test + @DisplayName("요구사항 1 containsExactly() 테스트") public void split2() { String[] result = "1,2".split(","); assertThat(result).containsExactly("1", "2"); } + + @Test + @DisplayName("요구사항 2 substring()") + public void substring() { + String result = "(1,2)".substring(1,4); + assertThat(result).isEqualTo("1,2"); + } + + @Test + @DisplayName("요구사항 3 StringIndexOutOfBoundsException") + void charAt() { + assertThatThrownBy(() -> { + char result = "abc".charAt(3); + }).isInstanceOf(StringIndexOutOfBoundsException.class) + .hasMessageContaining("String index out of range"); + } } From 5bd4cbd52852bd196b374cb06b84dd4507dbf077 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Thu, 2 Nov 2023 16:40:49 +0900 Subject: [PATCH 04/26] first commit --- src/test/java/study/StringCalculatorTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/java/study/StringCalculatorTest.java diff --git a/src/test/java/study/StringCalculatorTest.java b/src/test/java/study/StringCalculatorTest.java new file mode 100644 index 00000000000..6766b877d70 --- /dev/null +++ b/src/test/java/study/StringCalculatorTest.java @@ -0,0 +1,40 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StringCalculatorTest { +// 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 +// 구분자를 기준으로 분리한 각 숫자의 합을 반환 +// (예: “” => 0, "1,2" => 3, "1,2,3" => 6, “1,2:3” => 6) +// 앞의 기본 구분자(쉼표, 콜론)외에 커스텀 구분자를 지정할 수 있다. +// 커스텀 구분자는 문자열 앞부분의 “//”와 “\n” 사이에 위치하는 문자를 커스텀 구분자로 사용한다. +// 예를 들어 “//;\n1;2;3”과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, +// 결과 값은 6이 반환되어야 한다. +// 문자열 계산기에 숫자 이외의 값 또는 음수를 전달하는 경우 +// RuntimeException 예외를 throw한다. + + + // 더하는 함수 + // 구분자를 쪼개는 함수 + + // 특정 구분자가 있는지 체크하는 함수 + // 구분자를 쪼개는 함수 (특정 구분자 쪼개는 함수, 디폴트 쪼개는 함수) + // 더하는 함수 + + public void checkCustomDelimiter(String exp) { + char firstChar = exp.charAt(0); + } + + + + @Test + @DisplayName("") + void checkSplitTest() { + char result = "//;\n1;2;3".charAt(0); + + assertThat(result).isEqualTo('/'); + } +} From bd57b26f101dafe688b2de38966bc951ff9717e9 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Fri, 3 Nov 2023 19:28:34 +0900 Subject: [PATCH 05/26] step2 mission completed. --- README.md | 1 + src/main/java/study/StringCalculator.java | 56 +++++++++++++++++ src/test/java/study/StringCalculatorTest.java | 63 ++++++++++++------- 3 files changed, 96 insertions(+), 24 deletions(-) create mode 100644 src/main/java/study/StringCalculator.java diff --git a/README.md b/README.md index c550c4c2a09..a05829daff0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # 자동차 경주 게임 ## 진행 방법 * 자동차 경주 게임 요구사항을 파악한다. diff --git a/src/main/java/study/StringCalculator.java b/src/main/java/study/StringCalculator.java new file mode 100644 index 00000000000..62f6ed3233b --- /dev/null +++ b/src/main/java/study/StringCalculator.java @@ -0,0 +1,56 @@ +package study; + +public class StringCalculator { + + public static int cal(String exp) { + if (isEmpty(exp)) { + return 0; + } + + if (exp.length() == 1) { + return Integer.parseInt(exp); + } + + String[] numbers; + if (checkFirstChar(exp)) { + char customSplitter = exp.charAt(2); + numbers = exp.substring(4).split(String.valueOf(customSplitter)); + } else { + numbers = exp.split("[,:]"); + } + return getSum(numbers); + } + + private static boolean isEmpty(String exp) { + return exp == null || exp.isBlank(); + } + + private static boolean checkFirstChar(String str) { + if (str.charAt(0) != '/') { + return false; + } + return true; + } + + private static int getSum(String[] numbers) { + int sum = 0; + for (String number : numbers) { + sum = add(sum, number); + } + return sum; + } + + private static int add(int sum, String number) { + try { + int value = Integer.parseInt(number); + if (value < 0) { // 음수 검사 + throw new RuntimeException(); + } + sum += value; + } catch (NumberFormatException e) { // 숫자 이외의 값 예외처리 + throw new RuntimeException(); + } + return sum; + } + +} diff --git a/src/test/java/study/StringCalculatorTest.java b/src/test/java/study/StringCalculatorTest.java index 6766b877d70..ce2974db46f 100644 --- a/src/test/java/study/StringCalculatorTest.java +++ b/src/test/java/study/StringCalculatorTest.java @@ -4,37 +4,52 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class StringCalculatorTest { -// 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 -// 구분자를 기준으로 분리한 각 숫자의 합을 반환 -// (예: “” => 0, "1,2" => 3, "1,2,3" => 6, “1,2:3” => 6) -// 앞의 기본 구분자(쉼표, 콜론)외에 커스텀 구분자를 지정할 수 있다. -// 커스텀 구분자는 문자열 앞부분의 “//”와 “\n” 사이에 위치하는 문자를 커스텀 구분자로 사용한다. -// 예를 들어 “//;\n1;2;3”과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, -// 결과 값은 6이 반환되어야 한다. -// 문자열 계산기에 숫자 이외의 값 또는 음수를 전달하는 경우 -// RuntimeException 예외를 throw한다. - - - // 더하는 함수 - // 구분자를 쪼개는 함수 - - // 특정 구분자가 있는지 체크하는 함수 - // 구분자를 쪼개는 함수 (특정 구분자 쪼개는 함수, 디폴트 쪼개는 함수) - // 더하는 함수 - - public void checkCustomDelimiter(String exp) { - char firstChar = exp.charAt(0); + + @Test + void 문자열_비어있는지_확인() { + int result = StringCalculator.cal(null); + assertThat(result).isEqualTo(0); } + @Test + void 문자열_길이가_1일때_덧셈_테스트() { + int result = StringCalculator.cal("2"); + assertThat(result).isEqualTo(2); + } + + @Test + void 문자열_길이가_2이상_일반구분자_덧셈() { + int result = StringCalculator.cal("1,2,3"); + int result2 = StringCalculator.cal("1,2:3"); + assertThat(result).isEqualTo(6); + assertThat(result2).isEqualTo(6); + } + @Test + void 문자열_길이_2이상_특수구분자_덧셈() { + int result = StringCalculator.cal("//-\n1-2-3"); + assertThat(result).isEqualTo(6); + } + + @Test + void 숫자_이외의_값이면_에러발생() { + // 일반 구분자일 때 + assertThrows(RuntimeException.class, () -> StringCalculator.cal("1;-;3")); + + //특수 구분자 일 떄 + assertThrows(RuntimeException.class, () -> StringCalculator.cal("//;\n1;-;3")); + } @Test - @DisplayName("") - void checkSplitTest() { - char result = "//;\n1;2;3".charAt(0); + void 음수값이면_에러발생() { + // 일반 구분자일 때 + assertThrows(RuntimeException.class, () -> StringCalculator.cal("1;-2;3")); - assertThat(result).isEqualTo('/'); + // 특수 구분자 일 떄 + assertThrows(RuntimeException.class, () -> StringCalculator.cal("//;\n1;-2;3")); } } From f479491ffaff22d9c4f351968d067930bb531a13 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Mon, 6 Nov 2023 18:14:02 +0900 Subject: [PATCH 06/26] step2 refactoring completed. --- src/main/java/.gitkeep | 0 src/main/java/study/Step2.java | 10 +++ src/main/java/study/StringCalculator.java | 56 --------------- .../stringCalculator/ExceptionHandler.java | 12 ++++ .../java/study/stringCalculator/Splitter.java | 71 +++++++++++++++++++ .../stringCalculator/StringCalculator.java | 37 ++++++++++ src/test/java/study/StringCalculatorTest.java | 39 +++++----- 7 files changed, 150 insertions(+), 75 deletions(-) delete mode 100644 src/main/java/.gitkeep create mode 100644 src/main/java/study/Step2.java delete mode 100644 src/main/java/study/StringCalculator.java create mode 100644 src/main/java/study/stringCalculator/ExceptionHandler.java create mode 100644 src/main/java/study/stringCalculator/Splitter.java create mode 100644 src/main/java/study/stringCalculator/StringCalculator.java diff --git a/src/main/java/.gitkeep b/src/main/java/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/src/main/java/study/Step2.java b/src/main/java/study/Step2.java new file mode 100644 index 00000000000..d14b1b8753a --- /dev/null +++ b/src/main/java/study/Step2.java @@ -0,0 +1,10 @@ +package study; + +import study.stringCalculator.StringCalculator; + +public class Step2 { + + public static int calculateString(String input_string) { + return new StringCalculator().calculate(input_string); + } +} diff --git a/src/main/java/study/StringCalculator.java b/src/main/java/study/StringCalculator.java deleted file mode 100644 index 62f6ed3233b..00000000000 --- a/src/main/java/study/StringCalculator.java +++ /dev/null @@ -1,56 +0,0 @@ -package study; - -public class StringCalculator { - - public static int cal(String exp) { - if (isEmpty(exp)) { - return 0; - } - - if (exp.length() == 1) { - return Integer.parseInt(exp); - } - - String[] numbers; - if (checkFirstChar(exp)) { - char customSplitter = exp.charAt(2); - numbers = exp.substring(4).split(String.valueOf(customSplitter)); - } else { - numbers = exp.split("[,:]"); - } - return getSum(numbers); - } - - private static boolean isEmpty(String exp) { - return exp == null || exp.isBlank(); - } - - private static boolean checkFirstChar(String str) { - if (str.charAt(0) != '/') { - return false; - } - return true; - } - - private static int getSum(String[] numbers) { - int sum = 0; - for (String number : numbers) { - sum = add(sum, number); - } - return sum; - } - - private static int add(int sum, String number) { - try { - int value = Integer.parseInt(number); - if (value < 0) { // 음수 검사 - throw new RuntimeException(); - } - sum += value; - } catch (NumberFormatException e) { // 숫자 이외의 값 예외처리 - throw new RuntimeException(); - } - return sum; - } - -} diff --git a/src/main/java/study/stringCalculator/ExceptionHandler.java b/src/main/java/study/stringCalculator/ExceptionHandler.java new file mode 100644 index 00000000000..d4a5f366b5c --- /dev/null +++ b/src/main/java/study/stringCalculator/ExceptionHandler.java @@ -0,0 +1,12 @@ +package study.stringCalculator; + +public class ExceptionHandler { + private boolean isNegativeNumber = false; + private boolean isNumber = true; + + public void checkNegativeNumber(int value) { + if (value < 0) { // 음수 검사 + throw new IllegalArgumentException("해당 값은 음수입니다."); + } + } +} diff --git a/src/main/java/study/stringCalculator/Splitter.java b/src/main/java/study/stringCalculator/Splitter.java new file mode 100644 index 00000000000..da33febd62a --- /dev/null +++ b/src/main/java/study/stringCalculator/Splitter.java @@ -0,0 +1,71 @@ +package study.stringCalculator; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Splitter { + + private final String DEFAULT_SPLITTER = "[,:]"; + private final String CUSTOM_SPLITTER_PATTERN = "//(.)\n(.*)"; + private final int CUSTOM_EXPRESSION_START_POSITION = 4; + private final int CUSTOM_SPLITTER_POSITION = 2; + private ExceptionHandler exceptionHandler; + + public Splitter() { + exceptionHandler = new ExceptionHandler(); + } + + private boolean isCustomSplitter(String text) { + Matcher m = Pattern.compile(CUSTOM_SPLITTER_PATTERN).matcher(text); + if (m.find()) { + return true; + } + return false; + } + + private String getExpression(String expression) { + return expression.substring(CUSTOM_EXPRESSION_START_POSITION); + } + + private int parseToInt(String value) { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("숫자 이외의 값을 int로 변경할 수 없습니다."); + } + } + + private String[] getStringNumbers(String input_string) { + String[] stringNumbers; + if (isCustomSplitter(input_string)) { + stringNumbers = splitByCustomSplitter(input_string); + } else { + stringNumbers = splitByGeneralSplitter(input_string); + } + return stringNumbers; + } + + // 커스텀 스플릿 + private String[] splitByCustomSplitter(String input_string) { + char customSplitter = input_string.charAt(CUSTOM_SPLITTER_POSITION); + return getExpression(input_string).split(String.valueOf(customSplitter)); + } + + // 제네럴 스플릿 + private String[] splitByGeneralSplitter(String input_string) { + return input_string.split(DEFAULT_SPLITTER); + } + + public int[] getNumbers(String input_string) { + String[] stringNumbers = getStringNumbers(input_string); + + int[] intNumbers = new int[stringNumbers.length]; + for (int i = 0; i < stringNumbers.length; i++) { + int number = parseToInt(stringNumbers[i]); + this.exceptionHandler.checkNegativeNumber(number); + intNumbers[i] = number; + } + + return intNumbers; + } +} diff --git a/src/main/java/study/stringCalculator/StringCalculator.java b/src/main/java/study/stringCalculator/StringCalculator.java new file mode 100644 index 00000000000..97ca6780396 --- /dev/null +++ b/src/main/java/study/stringCalculator/StringCalculator.java @@ -0,0 +1,37 @@ +package study.stringCalculator; + +public class StringCalculator { + + public final int ZERO_RESULT = 0; + private Splitter splitter; + + public StringCalculator() { + splitter = new Splitter(); + } + + public int calculate(String input_string) { + if (isEmpty(input_string)) { + return ZERO_RESULT; + } + int[] numbers = splitter.getNumbers(input_string); + + return getSum(numbers); + } + + private boolean isEmpty(String expression) { + return expression == null || expression.isBlank(); + } + + private int getSum(int[] numbers) { + int sum = 0; + for (int number : numbers) { + sum = add(sum, number); + } + return sum; + } + + private int add(int sum, int number) { + return sum + number; + } + +} diff --git a/src/test/java/study/StringCalculatorTest.java b/src/test/java/study/StringCalculatorTest.java index ce2974db46f..66c82293698 100644 --- a/src/test/java/study/StringCalculatorTest.java +++ b/src/test/java/study/StringCalculatorTest.java @@ -2,54 +2,55 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import study.stringCalculator.StringCalculator; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static study.Step2.calculateString; public class StringCalculatorTest { @Test - void 문자열_비어있는지_확인() { - int result = StringCalculator.cal(null); + @DisplayName("Test empty string check") + void checkStringIsBlank() { + int result = calculateString(null); assertThat(result).isEqualTo(0); } @Test - void 문자열_길이가_1일때_덧셈_테스트() { - int result = StringCalculator.cal("2"); - assertThat(result).isEqualTo(2); - } - - @Test - void 문자열_길이가_2이상_일반구분자_덧셈() { - int result = StringCalculator.cal("1,2,3"); - int result2 = StringCalculator.cal("1,2:3"); + @DisplayName("Test, split and add with general splitter") + void generalSplitterStringAddCalc() { + int result = calculateString("1,2,3"); + int result2 = calculateString("1,2:3"); assertThat(result).isEqualTo(6); assertThat(result2).isEqualTo(6); } @Test - void 문자열_길이_2이상_특수구분자_덧셈() { - int result = StringCalculator.cal("//-\n1-2-3"); + @DisplayName("Test, split and add with custom splitter") + void customSplitterStringAddCalc() { + int result = calculateString("//-\n1-2-3"); assertThat(result).isEqualTo(6); } @Test - void 숫자_이외의_값이면_에러발생() { + @DisplayName("Test, an error occurs if it is a value other than a number.") + void notNumberError() { // 일반 구분자일 때 - assertThrows(RuntimeException.class, () -> StringCalculator.cal("1;-;3")); + assertThrows(IllegalArgumentException.class, () -> calculateString("1;-;3")); //특수 구분자 일 떄 - assertThrows(RuntimeException.class, () -> StringCalculator.cal("//;\n1;-;3")); + assertThrows(IllegalArgumentException.class, () -> calculateString("//;\n1;-;3")); } @Test - void 음수값이면_에러발생() { + @DisplayName("Test, an error occurs if it is negative number.") + void negativeNumberError() { // 일반 구분자일 때 - assertThrows(RuntimeException.class, () -> StringCalculator.cal("1;-2;3")); + assertThrows(RuntimeException.class, () -> calculateString("1;-2;3")); // 특수 구분자 일 떄 - assertThrows(RuntimeException.class, () -> StringCalculator.cal("//;\n1;-2;3")); + assertThrows(RuntimeException.class, () -> calculateString("//;\n1;-2;3")); } } From bc303b064735add3a72c98b4517ee9379ffc43b0 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Tue, 7 Nov 2023 15:47:31 +0900 Subject: [PATCH 07/26] step2 feedback is reflected. --- .../stringCalculator/ExceptionHandler.java | 2 - .../java/study/stringCalculator/Splitter.java | 68 +++++++++---------- .../stringCalculator/StringCalculator.java | 2 +- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/main/java/study/stringCalculator/ExceptionHandler.java b/src/main/java/study/stringCalculator/ExceptionHandler.java index d4a5f366b5c..ea96a503c81 100644 --- a/src/main/java/study/stringCalculator/ExceptionHandler.java +++ b/src/main/java/study/stringCalculator/ExceptionHandler.java @@ -1,8 +1,6 @@ package study.stringCalculator; public class ExceptionHandler { - private boolean isNegativeNumber = false; - private boolean isNumber = true; public void checkNegativeNumber(int value) { if (value < 0) { // 음수 검사 diff --git a/src/main/java/study/stringCalculator/Splitter.java b/src/main/java/study/stringCalculator/Splitter.java index da33febd62a..ba650e4bbb6 100644 --- a/src/main/java/study/stringCalculator/Splitter.java +++ b/src/main/java/study/stringCalculator/Splitter.java @@ -10,62 +10,58 @@ public class Splitter { private final int CUSTOM_EXPRESSION_START_POSITION = 4; private final int CUSTOM_SPLITTER_POSITION = 2; private ExceptionHandler exceptionHandler; + private Pattern customSplitterPattern; public Splitter() { exceptionHandler = new ExceptionHandler(); + customSplitterPattern = Pattern.compile(CUSTOM_SPLITTER_PATTERN); } - private boolean isCustomSplitter(String text) { - Matcher m = Pattern.compile(CUSTOM_SPLITTER_PATTERN).matcher(text); - if (m.find()) { - return true; + public int[] getNumbers(String inputString) { + String[] stringNumbers = getStringNumbers(inputString); + + int[] intNumbers = new int[stringNumbers.length]; + for (int i = 0; i < stringNumbers.length; i++) { + int number = parseToInt(stringNumbers[i]); + this.exceptionHandler.checkNegativeNumber(number); + intNumbers[i] = number; } - return false; - } - private String getExpression(String expression) { - return expression.substring(CUSTOM_EXPRESSION_START_POSITION); + return intNumbers; } - private int parseToInt(String value) { - try { - return Integer.parseInt(value); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("숫자 이외의 값을 int로 변경할 수 없습니다."); + private String[] getStringNumbers(String inputString) { + if (isCustomSplitter(inputString)) { + return splitByCustomSplitter(inputString); } + return splitByGeneralSplitter(inputString); } - private String[] getStringNumbers(String input_string) { - String[] stringNumbers; - if (isCustomSplitter(input_string)) { - stringNumbers = splitByCustomSplitter(input_string); - } else { - stringNumbers = splitByGeneralSplitter(input_string); + private boolean isCustomSplitter(String text) { + if (customSplitterPattern.matcher(text).find()) { + return true; } - return stringNumbers; + return false; } - // 커스텀 스플릿 - private String[] splitByCustomSplitter(String input_string) { - char customSplitter = input_string.charAt(CUSTOM_SPLITTER_POSITION); - return getExpression(input_string).split(String.valueOf(customSplitter)); + private String[] splitByCustomSplitter(String inputString) { + char customSplitter = inputString.charAt(CUSTOM_SPLITTER_POSITION); + return getExpression(inputString).split(String.valueOf(customSplitter)); } - // 제네럴 스플릿 - private String[] splitByGeneralSplitter(String input_string) { - return input_string.split(DEFAULT_SPLITTER); + private String getExpression(String expression) { + return expression.substring(CUSTOM_EXPRESSION_START_POSITION); } - public int[] getNumbers(String input_string) { - String[] stringNumbers = getStringNumbers(input_string); + private String[] splitByGeneralSplitter(String inputString) { + return inputString.split(DEFAULT_SPLITTER); + } - int[] intNumbers = new int[stringNumbers.length]; - for (int i = 0; i < stringNumbers.length; i++) { - int number = parseToInt(stringNumbers[i]); - this.exceptionHandler.checkNegativeNumber(number); - intNumbers[i] = number; + private int parseToInt(String value) { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("숫자 이외의 값을 int로 변경할 수 없습니다."); } - - return intNumbers; } } diff --git a/src/main/java/study/stringCalculator/StringCalculator.java b/src/main/java/study/stringCalculator/StringCalculator.java index 97ca6780396..8607be9217c 100644 --- a/src/main/java/study/stringCalculator/StringCalculator.java +++ b/src/main/java/study/stringCalculator/StringCalculator.java @@ -2,7 +2,7 @@ public class StringCalculator { - public final int ZERO_RESULT = 0; + private final int ZERO_RESULT = 0; private Splitter splitter; public StringCalculator() { From befcc9528a0624dbfe6d6d4a74fba3c27510cec6 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Tue, 7 Nov 2023 17:53:13 +0900 Subject: [PATCH 08/26] tmp(backup) --- src/main/java/study/Step3.java | 5 +++++ src/main/java/study/carRacing/CarRacing.java | 10 ++++++++++ src/main/java/study/carRacing/README.md | 10 ++++++++++ .../study/carRacing/ReceiveInputValue.java | 19 +++++++++++++++++++ src/test/java/study/CarRacingTest.java | 17 +++++++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 src/main/java/study/Step3.java create mode 100644 src/main/java/study/carRacing/CarRacing.java create mode 100644 src/main/java/study/carRacing/README.md create mode 100644 src/main/java/study/carRacing/ReceiveInputValue.java create mode 100644 src/test/java/study/CarRacingTest.java diff --git a/src/main/java/study/Step3.java b/src/main/java/study/Step3.java new file mode 100644 index 00000000000..c4856b3f526 --- /dev/null +++ b/src/main/java/study/Step3.java @@ -0,0 +1,5 @@ +package study; + +public class Step3 { + +} diff --git a/src/main/java/study/carRacing/CarRacing.java b/src/main/java/study/carRacing/CarRacing.java new file mode 100644 index 00000000000..1d4ba52b409 --- /dev/null +++ b/src/main/java/study/carRacing/CarRacing.java @@ -0,0 +1,10 @@ +package study.carRacing; + +import java.util.Scanner; + +public class CarRacing { + + public int receiveInputValues() { + return 0; + } +} diff --git a/src/main/java/study/carRacing/README.md b/src/main/java/study/carRacing/README.md new file mode 100644 index 00000000000..0a7a9dbebad --- /dev/null +++ b/src/main/java/study/carRacing/README.md @@ -0,0 +1,10 @@ +구현할 기능 + +* [ ] 자동차 대수 입력받기 +* [ ] 시도할 횟수 입력받기 +* [ ] 대수에 맞게 자동차 생성 +* [ ] 자동차 이동 기능 구현 +* [ ] 레이싱 기능 구현 +* [ ] 출력 기능 구현 + +--- \ No newline at end of file diff --git a/src/main/java/study/carRacing/ReceiveInputValue.java b/src/main/java/study/carRacing/ReceiveInputValue.java new file mode 100644 index 00000000000..0220518c8b2 --- /dev/null +++ b/src/main/java/study/carRacing/ReceiveInputValue.java @@ -0,0 +1,19 @@ +package study.carRacing; + +import java.util.Scanner; + +public class ReceiveInputValue { + + private Scanner scanner; + + public ReceiveInputValue() { + scanner = new Scanner(System.in); + } + + public int receiveInputValueForCar() { + System.out.println("자동차 대수를 입력하세요."); + return scanner.nextInt(); + } + + +} diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java new file mode 100644 index 00000000000..42d3acfb8de --- /dev/null +++ b/src/test/java/study/CarRacingTest.java @@ -0,0 +1,17 @@ +package study; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import study.carRacing.CarRacing; +import study.carRacing.ReceiveInputValue; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CarRacingTest { + @Test + @DisplayName("receive a value for the number of car.") + void receiveInputValueForCar() { + int numberOfCar = new ReceiveInputValue().receiveInputValueForCar(); + assertThat(numberOfCar).isEqualTo(3); + } +} From 99a12a024398867d65ac79a937da7a46e0a1673b Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 8 Nov 2023 16:23:51 +0900 Subject: [PATCH 09/26] tmp(backup) --- src/main/java/study/carRacing/CarRacing.java | 5 +++++ .../java/study/carRacing/ConsoleOutput.java | 10 +++++++++ .../study/carRacing/ReceiveInputValue.java | 8 ++++++- src/test/java/study/CarRacingTest.java | 22 ++++++++++++++++++- 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/main/java/study/carRacing/ConsoleOutput.java diff --git a/src/main/java/study/carRacing/CarRacing.java b/src/main/java/study/carRacing/CarRacing.java index 1d4ba52b409..5ff032e98c2 100644 --- a/src/main/java/study/carRacing/CarRacing.java +++ b/src/main/java/study/carRacing/CarRacing.java @@ -4,6 +4,11 @@ public class CarRacing { + + public CarRacing() { + ReceiveInputValue receiveInputValue = new ReceiveInputValue(); + } + public int receiveInputValues() { return 0; } diff --git a/src/main/java/study/carRacing/ConsoleOutput.java b/src/main/java/study/carRacing/ConsoleOutput.java new file mode 100644 index 00000000000..69e920a8487 --- /dev/null +++ b/src/main/java/study/carRacing/ConsoleOutput.java @@ -0,0 +1,10 @@ +package study.carRacing; + +public class ConsoleOutput { + + private static String CAR_INPUT_MESSAGE = "Enter the number of cars."; + + public void printCarInputMessage() { + System.out.println(CAR_INPUT_MESSAGE); + } +} diff --git a/src/main/java/study/carRacing/ReceiveInputValue.java b/src/main/java/study/carRacing/ReceiveInputValue.java index 0220518c8b2..9b5ef076f3c 100644 --- a/src/main/java/study/carRacing/ReceiveInputValue.java +++ b/src/main/java/study/carRacing/ReceiveInputValue.java @@ -11,9 +11,15 @@ public ReceiveInputValue() { } public int receiveInputValueForCar() { - System.out.println("자동차 대수를 입력하세요."); return scanner.nextInt(); } + public static void main(String[] args) { + ReceiveInputValue receiveInputValue = new ReceiveInputValue(); + int result = receiveInputValue.receiveInputValueForCar(); + + System.out.println("결과: " + result); + } + } diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 42d3acfb8de..1e2f905110e 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -1,17 +1,37 @@ package study; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import study.carRacing.CarRacing; import study.carRacing.ReceiveInputValue; +import java.io.*; + import static org.assertj.core.api.Assertions.assertThat; public class CarRacingTest { + + private static ReceiveInputValue receiveInputValue; + + @BeforeAll + static void init() { + receiveInputValue = new ReceiveInputValue(); + } + @Test @DisplayName("receive a value for the number of car.") void receiveInputValueForCar() { - int numberOfCar = new ReceiveInputValue().receiveInputValueForCar(); + + String input = "3"; + + OutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + + int numberOfCar = receiveInputValue.receiveInputValueForCar(); assertThat(numberOfCar).isEqualTo(3); } } From c6f7504d335b8d346994645a9095de5d937d31ce Mon Sep 17 00:00:00 2001 From: sudo-init Date: Thu, 9 Nov 2023 15:45:22 +0900 Subject: [PATCH 10/26] =?UTF-8?q?feat(feature)=20-=20=EB=AA=A8=EB=93=A0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/Step3.java | 6 +++ src/main/java/study/carRacing/Car.java | 26 ++++++++++++ src/main/java/study/carRacing/CarRacing.java | 42 +++++++++++++++++-- .../java/study/carRacing/ConsoleOutput.java | 10 ----- src/main/java/study/carRacing/README.md | 15 +++---- .../study/carRacing/ReceiveInputValue.java | 16 +++---- src/main/java/study/carRacing/ViewResult.java | 21 ++++++++++ 7 files changed, 107 insertions(+), 29 deletions(-) create mode 100644 src/main/java/study/carRacing/Car.java delete mode 100644 src/main/java/study/carRacing/ConsoleOutput.java create mode 100644 src/main/java/study/carRacing/ViewResult.java diff --git a/src/main/java/study/Step3.java b/src/main/java/study/Step3.java index c4856b3f526..cdb10e207b1 100644 --- a/src/main/java/study/Step3.java +++ b/src/main/java/study/Step3.java @@ -1,5 +1,11 @@ package study; +import study.carRacing.CarRacing; + public class Step3 { + public static void main(String[] args) { + new CarRacing().runCarRacing(); + } + } diff --git a/src/main/java/study/carRacing/Car.java b/src/main/java/study/carRacing/Car.java new file mode 100644 index 00000000000..395fc9aa4d8 --- /dev/null +++ b/src/main/java/study/carRacing/Car.java @@ -0,0 +1,26 @@ +package study.carRacing; + +import java.util.Random; + +public class Car { + + public int carNumber; + public int movingDistance; + + public Car(int carNumber) { + this.carNumber = carNumber; + this.movingDistance = 0; + } + + public int getCarNumber() { + return this.carNumber; + } + + public int getMovingDistance() { + return this.movingDistance; + } + + public void move(int distance) { + this.movingDistance += distance; + } +} diff --git a/src/main/java/study/carRacing/CarRacing.java b/src/main/java/study/carRacing/CarRacing.java index 5ff032e98c2..8197e7886e7 100644 --- a/src/main/java/study/carRacing/CarRacing.java +++ b/src/main/java/study/carRacing/CarRacing.java @@ -1,15 +1,49 @@ package study.carRacing; -import java.util.Scanner; +import java.util.ArrayList; +import java.util.Random; public class CarRacing { + private ReceiveInputValue receiveInputValue; + private Random random; + private ViewResult viewResult; public CarRacing() { - ReceiveInputValue receiveInputValue = new ReceiveInputValue(); + this.receiveInputValue = new ReceiveInputValue(); + this.random = new Random(); + this.viewResult = new ViewResult(); } - public int receiveInputValues() { - return 0; + public void runCarRacing() { + + int numberOfCar = receiveInputValue.receiveInputValueForCar(); + int roundOfTrack = receiveInputValue.receiveInputValueForRound(); + + ArrayList CarList = generateCarList(numberOfCar); + + for (int i = 0; i < roundOfTrack; i++) { + runTrack(CarList); + this.viewResult.printResult(CarList); + } + + } + + public ArrayList generateCarList(int numberOfCar) { + ArrayList CarList = new ArrayList(); + for (int i = 0; i < numberOfCar; i++) { + CarList.add(new Car(i+1)); + } + return CarList; + } + + public void runTrack(ArrayList CarList) { + for (int j = 0; j < CarList.size(); j++){ + int movingDistance = this.random.nextInt(10); + Car car = CarList.get(j); + car.move(movingDistance); + } } + + } diff --git a/src/main/java/study/carRacing/ConsoleOutput.java b/src/main/java/study/carRacing/ConsoleOutput.java deleted file mode 100644 index 69e920a8487..00000000000 --- a/src/main/java/study/carRacing/ConsoleOutput.java +++ /dev/null @@ -1,10 +0,0 @@ -package study.carRacing; - -public class ConsoleOutput { - - private static String CAR_INPUT_MESSAGE = "Enter the number of cars."; - - public void printCarInputMessage() { - System.out.println(CAR_INPUT_MESSAGE); - } -} diff --git a/src/main/java/study/carRacing/README.md b/src/main/java/study/carRacing/README.md index 0a7a9dbebad..1453582323a 100644 --- a/src/main/java/study/carRacing/README.md +++ b/src/main/java/study/carRacing/README.md @@ -1,10 +1,11 @@ 구현할 기능 -* [ ] 자동차 대수 입력받기 -* [ ] 시도할 횟수 입력받기 -* [ ] 대수에 맞게 자동차 생성 -* [ ] 자동차 이동 기능 구현 -* [ ] 레이싱 기능 구현 -* [ ] 출력 기능 구현 ---- \ No newline at end of file + +--- +* [X] 자동차 대수, 시도 횟수 입력받기 +* [X] 대수에 맞게 자동차 생성 +* [X] 자동차 이동 기능 구현 +* [X] 자동차 상태 저장 +* [X] 레이싱 기능 구현 +* [X] 출력 기능 구현 \ No newline at end of file diff --git a/src/main/java/study/carRacing/ReceiveInputValue.java b/src/main/java/study/carRacing/ReceiveInputValue.java index 9b5ef076f3c..83b085ae331 100644 --- a/src/main/java/study/carRacing/ReceiveInputValue.java +++ b/src/main/java/study/carRacing/ReceiveInputValue.java @@ -5,21 +5,21 @@ public class ReceiveInputValue { private Scanner scanner; + private final String CAR_INPUT_MESSAGE = "Enter the number of cars."; + private final String ROUND_INPUT_MESSAGE = "Enter how many rounds are we going to do."; public ReceiveInputValue() { - scanner = new Scanner(System.in); + this.scanner = new Scanner(System.in); } public int receiveInputValueForCar() { - return scanner.nextInt(); + System.out.println(CAR_INPUT_MESSAGE); + return this.scanner.nextInt(); } - public static void main(String[] args) { - ReceiveInputValue receiveInputValue = new ReceiveInputValue(); - int result = receiveInputValue.receiveInputValueForCar(); - - System.out.println("결과: " + result); + public int receiveInputValueForRound() { + System.out.println(ROUND_INPUT_MESSAGE); + return this.scanner.nextInt(); } - } diff --git a/src/main/java/study/carRacing/ViewResult.java b/src/main/java/study/carRacing/ViewResult.java new file mode 100644 index 00000000000..d55d0a96eaa --- /dev/null +++ b/src/main/java/study/carRacing/ViewResult.java @@ -0,0 +1,21 @@ +package study.carRacing; + +import java.util.ArrayList; + +public class ViewResult { + + public void printResult(ArrayList CarList) { + for (int i = 0; i Date: Fri, 10 Nov 2023 21:28:21 +0900 Subject: [PATCH 11/26] refactory and test(add moveCar Test etc..) --- src/main/java/study/Step2.java | 2 +- src/main/java/study/Step3.java | 2 +- .../ExceptionHandler.java | 2 +- .../Splitter.java | 3 +- .../StringCalculator.java | 2 +- src/main/java/study/carRacing/Car.java | 26 -------- src/main/java/study/carRacing/CarRacing.java | 49 --------------- src/main/java/study/carRacing/ViewResult.java | 21 ------- src/main/java/study/race/Car.java | 20 ++++++ .../java/study/race/CarListGenerator.java | 21 +++++++ .../java/study/race/CarLocationManager.java | 24 +++++++ src/main/java/study/race/CarRacing.java | 30 +++++++++ .../java/study/{carRacing => race}/README.md | 0 src/main/java/study/race/RaceResult.java | 26 ++++++++ .../ReceiveInputValue.java | 5 +- src/main/java/study/race/RunTrack.java | 31 +++++++++ src/main/java/study/race/ViewResult.java | 13 ++++ src/test/java/study/CarRacingTest.java | 63 ++++++++++++++----- src/test/java/study/StringCalculatorTest.java | 1 - 19 files changed, 218 insertions(+), 123 deletions(-) rename src/main/java/study/{stringCalculator => calculator}/ExceptionHandler.java (87%) rename src/main/java/study/{stringCalculator => calculator}/Splitter.java (97%) rename src/main/java/study/{stringCalculator => calculator}/StringCalculator.java (96%) delete mode 100644 src/main/java/study/carRacing/Car.java delete mode 100644 src/main/java/study/carRacing/CarRacing.java delete mode 100644 src/main/java/study/carRacing/ViewResult.java create mode 100644 src/main/java/study/race/Car.java create mode 100644 src/main/java/study/race/CarListGenerator.java create mode 100644 src/main/java/study/race/CarLocationManager.java create mode 100644 src/main/java/study/race/CarRacing.java rename src/main/java/study/{carRacing => race}/README.md (100%) create mode 100644 src/main/java/study/race/RaceResult.java rename src/main/java/study/{carRacing => race}/ReceiveInputValue.java (89%) create mode 100644 src/main/java/study/race/RunTrack.java create mode 100644 src/main/java/study/race/ViewResult.java diff --git a/src/main/java/study/Step2.java b/src/main/java/study/Step2.java index d14b1b8753a..fe4d6b77e30 100644 --- a/src/main/java/study/Step2.java +++ b/src/main/java/study/Step2.java @@ -1,6 +1,6 @@ package study; -import study.stringCalculator.StringCalculator; +import study.calculator.StringCalculator; public class Step2 { diff --git a/src/main/java/study/Step3.java b/src/main/java/study/Step3.java index cdb10e207b1..a25328d5d1d 100644 --- a/src/main/java/study/Step3.java +++ b/src/main/java/study/Step3.java @@ -1,6 +1,6 @@ package study; -import study.carRacing.CarRacing; +import study.race.CarRacing; public class Step3 { diff --git a/src/main/java/study/stringCalculator/ExceptionHandler.java b/src/main/java/study/calculator/ExceptionHandler.java similarity index 87% rename from src/main/java/study/stringCalculator/ExceptionHandler.java rename to src/main/java/study/calculator/ExceptionHandler.java index ea96a503c81..4eca9a14b9e 100644 --- a/src/main/java/study/stringCalculator/ExceptionHandler.java +++ b/src/main/java/study/calculator/ExceptionHandler.java @@ -1,4 +1,4 @@ -package study.stringCalculator; +package study.calculator; public class ExceptionHandler { diff --git a/src/main/java/study/stringCalculator/Splitter.java b/src/main/java/study/calculator/Splitter.java similarity index 97% rename from src/main/java/study/stringCalculator/Splitter.java rename to src/main/java/study/calculator/Splitter.java index ba650e4bbb6..be3ef8245db 100644 --- a/src/main/java/study/stringCalculator/Splitter.java +++ b/src/main/java/study/calculator/Splitter.java @@ -1,6 +1,5 @@ -package study.stringCalculator; +package study.calculator; -import java.util.regex.Matcher; import java.util.regex.Pattern; public class Splitter { diff --git a/src/main/java/study/stringCalculator/StringCalculator.java b/src/main/java/study/calculator/StringCalculator.java similarity index 96% rename from src/main/java/study/stringCalculator/StringCalculator.java rename to src/main/java/study/calculator/StringCalculator.java index 8607be9217c..72ae9420347 100644 --- a/src/main/java/study/stringCalculator/StringCalculator.java +++ b/src/main/java/study/calculator/StringCalculator.java @@ -1,4 +1,4 @@ -package study.stringCalculator; +package study.calculator; public class StringCalculator { diff --git a/src/main/java/study/carRacing/Car.java b/src/main/java/study/carRacing/Car.java deleted file mode 100644 index 395fc9aa4d8..00000000000 --- a/src/main/java/study/carRacing/Car.java +++ /dev/null @@ -1,26 +0,0 @@ -package study.carRacing; - -import java.util.Random; - -public class Car { - - public int carNumber; - public int movingDistance; - - public Car(int carNumber) { - this.carNumber = carNumber; - this.movingDistance = 0; - } - - public int getCarNumber() { - return this.carNumber; - } - - public int getMovingDistance() { - return this.movingDistance; - } - - public void move(int distance) { - this.movingDistance += distance; - } -} diff --git a/src/main/java/study/carRacing/CarRacing.java b/src/main/java/study/carRacing/CarRacing.java deleted file mode 100644 index 8197e7886e7..00000000000 --- a/src/main/java/study/carRacing/CarRacing.java +++ /dev/null @@ -1,49 +0,0 @@ -package study.carRacing; - -import java.util.ArrayList; -import java.util.Random; - -public class CarRacing { - - private ReceiveInputValue receiveInputValue; - private Random random; - private ViewResult viewResult; - - public CarRacing() { - this.receiveInputValue = new ReceiveInputValue(); - this.random = new Random(); - this.viewResult = new ViewResult(); - } - - public void runCarRacing() { - - int numberOfCar = receiveInputValue.receiveInputValueForCar(); - int roundOfTrack = receiveInputValue.receiveInputValueForRound(); - - ArrayList CarList = generateCarList(numberOfCar); - - for (int i = 0; i < roundOfTrack; i++) { - runTrack(CarList); - this.viewResult.printResult(CarList); - } - - } - - public ArrayList generateCarList(int numberOfCar) { - ArrayList CarList = new ArrayList(); - for (int i = 0; i < numberOfCar; i++) { - CarList.add(new Car(i+1)); - } - return CarList; - } - - public void runTrack(ArrayList CarList) { - for (int j = 0; j < CarList.size(); j++){ - int movingDistance = this.random.nextInt(10); - Car car = CarList.get(j); - car.move(movingDistance); - } - } - - -} diff --git a/src/main/java/study/carRacing/ViewResult.java b/src/main/java/study/carRacing/ViewResult.java deleted file mode 100644 index d55d0a96eaa..00000000000 --- a/src/main/java/study/carRacing/ViewResult.java +++ /dev/null @@ -1,21 +0,0 @@ -package study.carRacing; - -import java.util.ArrayList; - -public class ViewResult { - - public void printResult(ArrayList CarList) { - for (int i = 0; i generateCarList() { + List carList = new ArrayList<>(); + for (int i = 0; i < this.numberOfCar; i++) { + carList.add(new Car()); + } + return carList; + } +} diff --git a/src/main/java/study/race/CarLocationManager.java b/src/main/java/study/race/CarLocationManager.java new file mode 100644 index 00000000000..be590eefd91 --- /dev/null +++ b/src/main/java/study/race/CarLocationManager.java @@ -0,0 +1,24 @@ +package study.race; + +public class CarLocationManager { + + private final int STARTING_PORING = 1; + private final int FORWARD_CONDITION_THRESHOLD = 4; + public int initMovingDistance() { + return STARTING_PORING; + } + + public int updateLocation(int currentDistance, int movingDistance) { + if (checkForwardCondition(movingDistance)) { + return currentDistance + movingDistance; + } + return currentDistance; + } + + private boolean checkForwardCondition(int movingDistance) { + if (movingDistance >= FORWARD_CONDITION_THRESHOLD) { + return true; + } + return false; + } +} diff --git a/src/main/java/study/race/CarRacing.java b/src/main/java/study/race/CarRacing.java new file mode 100644 index 00000000000..34a95c53805 --- /dev/null +++ b/src/main/java/study/race/CarRacing.java @@ -0,0 +1,30 @@ +package study.race; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class CarRacing { + + private ReceiveInputValue receiveInputValue; + private ViewResult viewResult; + + public CarRacing() { + this.receiveInputValue = new ReceiveInputValue(); + this.viewResult = new ViewResult(); + } + + public void runCarRacing() { + int numberOfCar = receiveInputValue.receiveInputValueForCar(); + int roundOfTrack = receiveInputValue.receiveInputValueForRound(); + + RunTrack runTrack = new RunTrack(numberOfCar); + + System.out.println("\nExecution Results"); + this.viewResult.printResult(runTrack.getRaceResult()); + for (int i = 0; i < roundOfTrack; i++) { + runTrack.start(); + this.viewResult.printResult(runTrack.getRaceResult()); + } + } +} diff --git a/src/main/java/study/carRacing/README.md b/src/main/java/study/race/README.md similarity index 100% rename from src/main/java/study/carRacing/README.md rename to src/main/java/study/race/README.md diff --git a/src/main/java/study/race/RaceResult.java b/src/main/java/study/race/RaceResult.java new file mode 100644 index 00000000000..c63defd7025 --- /dev/null +++ b/src/main/java/study/race/RaceResult.java @@ -0,0 +1,26 @@ +package study.race; + +import java.util.List; + +public class RaceResult { + + int[] racingResult; + List carList; + + public RaceResult(List carList) { + racingResult = new int[carList.size()]; + this.carList = carList; + this.updateRaceResult(); + } + + public int[] getResult() { + return this.racingResult; + } + + public void updateRaceResult() { + for (int i = 0; i < carList.size(); i++) { + Car car = carList.get(i); + racingResult[i] = car.getCurrentLocation(); + } + } +} diff --git a/src/main/java/study/carRacing/ReceiveInputValue.java b/src/main/java/study/race/ReceiveInputValue.java similarity index 89% rename from src/main/java/study/carRacing/ReceiveInputValue.java rename to src/main/java/study/race/ReceiveInputValue.java index 83b085ae331..2bb97938148 100644 --- a/src/main/java/study/carRacing/ReceiveInputValue.java +++ b/src/main/java/study/race/ReceiveInputValue.java @@ -1,4 +1,4 @@ -package study.carRacing; +package study.race; import java.util.Scanner; @@ -19,7 +19,6 @@ public int receiveInputValueForCar() { public int receiveInputValueForRound() { System.out.println(ROUND_INPUT_MESSAGE); - return this.scanner.nextInt(); + return this.scanner.nextInt() - 1; } - } diff --git a/src/main/java/study/race/RunTrack.java b/src/main/java/study/race/RunTrack.java new file mode 100644 index 00000000000..31ee0433e6f --- /dev/null +++ b/src/main/java/study/race/RunTrack.java @@ -0,0 +1,31 @@ +package study.race; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class RunTrack { + + private Random random; + private List carList; + private RaceResult raceResult; + + + public RunTrack(int numberOfCar) { + this.random = new Random(); + this.carList = new CarListGenerator(numberOfCar).generateCarList(); + this.raceResult = new RaceResult(this.carList); + } + + public void start() { + for (int i = 0; i < this.carList.size(); i++) { + int movingDistance = random.nextInt(10); + this.carList.get(i).move(movingDistance); + } + this.raceResult.updateRaceResult(); + } + + public int[] getRaceResult() { + return this.raceResult.getResult(); + } +} diff --git a/src/main/java/study/race/ViewResult.java b/src/main/java/study/race/ViewResult.java new file mode 100644 index 00000000000..b373da05281 --- /dev/null +++ b/src/main/java/study/race/ViewResult.java @@ -0,0 +1,13 @@ +package study.race; + +import java.util.List; + +public class ViewResult { + + public void printResult(int[] results) { + for (int result: results) { + System.out.println("-".repeat(result)); + } + System.out.println(""); + } +} diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 1e2f905110e..61ad53b3fca 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -3,35 +3,64 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import study.carRacing.CarRacing; -import study.carRacing.ReceiveInputValue; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import study.race.*; -import java.io.*; +import java.util.List; import static org.assertj.core.api.Assertions.assertThat; + public class CarRacingTest { - private static ReceiveInputValue receiveInputValue; - @BeforeAll - static void init() { - receiveInputValue = new ReceiveInputValue(); + @Test + @DisplayName("Create Cars to Race") + void generateCarListTest() { + int numberOfCars = 3; + List carList = new CarListGenerator(3).generateCarList(); + assertThat(carList.size()).isEqualTo(numberOfCars); } - @Test - @DisplayName("receive a value for the number of car.") - void receiveInputValueForCar() { + @ParameterizedTest(name="moving distance: {0}, current location: {1}") + @DisplayName("When moving distance is 4 or more, Car moving Test") + @CsvSource({ "4, 5", "5, 6", "6, 7", "7, 8", "8, 9", "9, 10"}) + void moveCar(int movingDistance, int expected) { + Car car = new Car(); + car.move(movingDistance); + int result = car.getCurrentLocation(); + assertThat(result).isEqualTo(expected); + } - String input = "3"; + @ParameterizedTest(name="moving distance: {0}, current location: {1}") + @DisplayName("When moving distance is 3 or less, Car moving Test") + @CsvSource({ "0, 1", "1, 1", "2, 1", "3, 1"}) + void stopCar(int movingDistance, int expected) { + Car car = new Car(); + car.move(movingDistance); + int result = car.getCurrentLocation(); + assertThat(result).isEqualTo(expected); + } + + @Test + @DisplayName("Result of racing update Test") + void updateRaceResult() { + int numberOfCar = 3; + int[] movingDistanceList = {3, 5, 7}; + int[] expected = {1, 6, 8}; + List carList = new CarListGenerator(numberOfCar).generateCarList(); + RaceResult raceResult = new RaceResult(carList); - OutputStream out = new ByteArrayOutputStream(); - System.setOut(new PrintStream(out)); - InputStream in = new ByteArrayInputStream(input.getBytes()); - System.setIn(in); + for (int i = 0; i < carList.size(); i++) { + carList.get(i).move(movingDistanceList[i]); + } + raceResult.updateRaceResult(); + int[] result = raceResult.getResult(); // 1, 6, 8 - int numberOfCar = receiveInputValue.receiveInputValueForCar(); - assertThat(numberOfCar).isEqualTo(3); + for (int i = 0; i < carList.size(); i++){ + assertThat(result[i]).isEqualTo(expected[i]); + } } } diff --git a/src/test/java/study/StringCalculatorTest.java b/src/test/java/study/StringCalculatorTest.java index 66c82293698..9e65b9482dc 100644 --- a/src/test/java/study/StringCalculatorTest.java +++ b/src/test/java/study/StringCalculatorTest.java @@ -2,7 +2,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import study.stringCalculator.StringCalculator; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; From 7d4799ed45212abc30dd044cbed245be069b135c Mon Sep 17 00:00:00 2001 From: sudo-init Date: Mon, 13 Nov 2023 19:57:39 +0900 Subject: [PATCH 12/26] =?UTF-8?q?refactor=20(#5075):=20=ED=94=BC=EB=93=9C?= =?UTF-8?q?=EB=B0=B1=20=EC=9A=94=EC=B2=AD=EC=82=AC=ED=95=AD=20=EB=B0=98?= =?UTF-8?q?=EC=98=81=20=EB=B0=8F=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81,?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 자동차의 최초 위치 수정. 랜덤값이 4이상일 때, 1만큼만 이동하도록 수정. STARTING_PORING -> START_POINT 로 오타 수정. 패키지 mvc패턴으로 정리. list 대신 Cars 클래스 추가 및 반환값으로 변경. --- src/main/java/study/Step3.java | 2 +- .../java/study/race/CarListGenerator.java | 21 ------ src/main/java/study/race/RaceResult.java | 26 ------- .../race/{ => controller}/CarRacing.java | 10 +-- src/main/java/study/race/{ => model}/Car.java | 2 +- .../study/race/model/CarListGenerator.java | 14 ++++ .../race/{ => model}/CarLocationManager.java | 12 ++-- src/main/java/study/race/model/Cars.java | 23 +++++++ .../java/study/race/model/RaceResult.java | 29 ++++++++ .../java/study/race/{ => model}/RunTrack.java | 14 ++-- .../race/{ => view}/ReceiveInputValue.java | 4 +- .../study/race/{ => view}/ViewResult.java | 4 +- src/test/java/study/CarRacingTest.java | 67 ++++++++++++------- 13 files changed, 131 insertions(+), 97 deletions(-) delete mode 100644 src/main/java/study/race/CarListGenerator.java delete mode 100644 src/main/java/study/race/RaceResult.java rename src/main/java/study/race/{ => controller}/CarRacing.java (78%) rename src/main/java/study/race/{ => model}/Car.java (95%) create mode 100644 src/main/java/study/race/model/CarListGenerator.java rename src/main/java/study/race/{ => model}/CarLocationManager.java (62%) create mode 100644 src/main/java/study/race/model/Cars.java create mode 100644 src/main/java/study/race/model/RaceResult.java rename src/main/java/study/race/{ => model}/RunTrack.java (55%) rename src/main/java/study/race/{ => view}/ReceiveInputValue.java (90%) rename src/main/java/study/race/{ => view}/ViewResult.java (78%) diff --git a/src/main/java/study/Step3.java b/src/main/java/study/Step3.java index a25328d5d1d..31f548025ef 100644 --- a/src/main/java/study/Step3.java +++ b/src/main/java/study/Step3.java @@ -1,6 +1,6 @@ package study; -import study.race.CarRacing; +import study.race.controller.CarRacing; public class Step3 { diff --git a/src/main/java/study/race/CarListGenerator.java b/src/main/java/study/race/CarListGenerator.java deleted file mode 100644 index 7f6b08bfc99..00000000000 --- a/src/main/java/study/race/CarListGenerator.java +++ /dev/null @@ -1,21 +0,0 @@ -package study.race; - -import java.util.ArrayList; -import java.util.List; - -public class CarListGenerator { - - private int numberOfCar; - - public CarListGenerator(int numberOfCar) { - this.numberOfCar = numberOfCar; - } - - public List generateCarList() { - List carList = new ArrayList<>(); - for (int i = 0; i < this.numberOfCar; i++) { - carList.add(new Car()); - } - return carList; - } -} diff --git a/src/main/java/study/race/RaceResult.java b/src/main/java/study/race/RaceResult.java deleted file mode 100644 index c63defd7025..00000000000 --- a/src/main/java/study/race/RaceResult.java +++ /dev/null @@ -1,26 +0,0 @@ -package study.race; - -import java.util.List; - -public class RaceResult { - - int[] racingResult; - List carList; - - public RaceResult(List carList) { - racingResult = new int[carList.size()]; - this.carList = carList; - this.updateRaceResult(); - } - - public int[] getResult() { - return this.racingResult; - } - - public void updateRaceResult() { - for (int i = 0; i < carList.size(); i++) { - Car car = carList.get(i); - racingResult[i] = car.getCurrentLocation(); - } - } -} diff --git a/src/main/java/study/race/CarRacing.java b/src/main/java/study/race/controller/CarRacing.java similarity index 78% rename from src/main/java/study/race/CarRacing.java rename to src/main/java/study/race/controller/CarRacing.java index 34a95c53805..e5d347332f4 100644 --- a/src/main/java/study/race/CarRacing.java +++ b/src/main/java/study/race/controller/CarRacing.java @@ -1,8 +1,8 @@ -package study.race; +package study.race.controller; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; +import study.race.view.ReceiveInputValue; +import study.race.model.RunTrack; +import study.race.view.ViewResult; public class CarRacing { @@ -20,7 +20,7 @@ public void runCarRacing() { RunTrack runTrack = new RunTrack(numberOfCar); - System.out.println("\nExecution Results"); + System.out.println("\nExecution Result"); this.viewResult.printResult(runTrack.getRaceResult()); for (int i = 0; i < roundOfTrack; i++) { runTrack.start(); diff --git a/src/main/java/study/race/Car.java b/src/main/java/study/race/model/Car.java similarity index 95% rename from src/main/java/study/race/Car.java rename to src/main/java/study/race/model/Car.java index 0d6b8085014..7b25c6eaf52 100644 --- a/src/main/java/study/race/Car.java +++ b/src/main/java/study/race/model/Car.java @@ -1,4 +1,4 @@ -package study.race; +package study.race.model; public class Car { diff --git a/src/main/java/study/race/model/CarListGenerator.java b/src/main/java/study/race/model/CarListGenerator.java new file mode 100644 index 00000000000..991ee4c3431 --- /dev/null +++ b/src/main/java/study/race/model/CarListGenerator.java @@ -0,0 +1,14 @@ +package study.race.model; + +import study.race.model.Cars; + +public class CarListGenerator { + + public Cars generateCarList(int numberOfCar) { + Cars cars = new Cars(); + for (int i = 0; i < numberOfCar; i++) { + cars.addCar(); + } + return cars; + } +} diff --git a/src/main/java/study/race/CarLocationManager.java b/src/main/java/study/race/model/CarLocationManager.java similarity index 62% rename from src/main/java/study/race/CarLocationManager.java rename to src/main/java/study/race/model/CarLocationManager.java index be590eefd91..104722e7792 100644 --- a/src/main/java/study/race/CarLocationManager.java +++ b/src/main/java/study/race/model/CarLocationManager.java @@ -1,16 +1,18 @@ -package study.race; +package study.race.model; public class CarLocationManager { - private final int STARTING_PORING = 1; + private final int STARTING_POINT = 0; + private final int MOVE = 1; private final int FORWARD_CONDITION_THRESHOLD = 4; + public int initMovingDistance() { - return STARTING_PORING; + return STARTING_POINT; } public int updateLocation(int currentDistance, int movingDistance) { - if (checkForwardCondition(movingDistance)) { - return currentDistance + movingDistance; + if (checkForwardCondition(movingDistance) || currentDistance == STARTING_POINT) { + return currentDistance + MOVE; } return currentDistance; } diff --git a/src/main/java/study/race/model/Cars.java b/src/main/java/study/race/model/Cars.java new file mode 100644 index 00000000000..02149f1953e --- /dev/null +++ b/src/main/java/study/race/model/Cars.java @@ -0,0 +1,23 @@ +package study.race.model; + +import study.race.model.Car; + +import java.util.ArrayList; +import java.util.List; + +public class Cars { + + private List carList; + + public Cars() { + this.carList = new ArrayList(); + } + + public List carList() { + return this.carList; + } + + public void addCar() { + this.carList.add(new Car()); + } +} diff --git a/src/main/java/study/race/model/RaceResult.java b/src/main/java/study/race/model/RaceResult.java new file mode 100644 index 00000000000..1dadb0ba81e --- /dev/null +++ b/src/main/java/study/race/model/RaceResult.java @@ -0,0 +1,29 @@ +package study.race.model; + +import study.race.model.Car; +import study.race.model.Cars; + +public class RaceResult { + + private int numberOfCar; + private int[] racingResult; + private Cars cars; + + public RaceResult(Cars cars) { + this.cars = cars; + this.numberOfCar = cars.carList().size(); + racingResult = new int[this.numberOfCar]; + this.updateRaceResult(); + } + + public int[] getResult() { + return this.racingResult; + } + + public void updateRaceResult() { + for (int i = 0; i < this.numberOfCar; i++) { + Car car = cars.carList().get(i); + racingResult[i] = car.getCurrentLocation(); + } + } +} diff --git a/src/main/java/study/race/RunTrack.java b/src/main/java/study/race/model/RunTrack.java similarity index 55% rename from src/main/java/study/race/RunTrack.java rename to src/main/java/study/race/model/RunTrack.java index 31ee0433e6f..3e36079e937 100644 --- a/src/main/java/study/race/RunTrack.java +++ b/src/main/java/study/race/model/RunTrack.java @@ -1,26 +1,24 @@ -package study.race; +package study.race.model; -import java.util.ArrayList; -import java.util.List; import java.util.Random; public class RunTrack { private Random random; - private List carList; + private Cars cars; private RaceResult raceResult; public RunTrack(int numberOfCar) { this.random = new Random(); - this.carList = new CarListGenerator(numberOfCar).generateCarList(); - this.raceResult = new RaceResult(this.carList); + this.cars = new CarListGenerator().generateCarList(numberOfCar); + this.raceResult = new RaceResult(this.cars); } public void start() { - for (int i = 0; i < this.carList.size(); i++) { + for (int i = 0; i < this.cars.carList().size(); i++) { int movingDistance = random.nextInt(10); - this.carList.get(i).move(movingDistance); + this.cars.carList().get(i).move(movingDistance); } this.raceResult.updateRaceResult(); } diff --git a/src/main/java/study/race/ReceiveInputValue.java b/src/main/java/study/race/view/ReceiveInputValue.java similarity index 90% rename from src/main/java/study/race/ReceiveInputValue.java rename to src/main/java/study/race/view/ReceiveInputValue.java index 2bb97938148..a2fce480a43 100644 --- a/src/main/java/study/race/ReceiveInputValue.java +++ b/src/main/java/study/race/view/ReceiveInputValue.java @@ -1,4 +1,4 @@ -package study.race; +package study.race.view; import java.util.Scanner; @@ -19,6 +19,6 @@ public int receiveInputValueForCar() { public int receiveInputValueForRound() { System.out.println(ROUND_INPUT_MESSAGE); - return this.scanner.nextInt() - 1; + return this.scanner.nextInt(); } } diff --git a/src/main/java/study/race/ViewResult.java b/src/main/java/study/race/view/ViewResult.java similarity index 78% rename from src/main/java/study/race/ViewResult.java rename to src/main/java/study/race/view/ViewResult.java index b373da05281..970d988e16d 100644 --- a/src/main/java/study/race/ViewResult.java +++ b/src/main/java/study/race/view/ViewResult.java @@ -1,4 +1,4 @@ -package study.race; +package study.race.view; import java.util.List; @@ -8,6 +8,6 @@ public void printResult(int[] results) { for (int result: results) { System.out.println("-".repeat(result)); } - System.out.println(""); + System.out.println(); } } diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 61ad53b3fca..6d288348e1a 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -3,64 +3,79 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import study.race.*; - -import java.util.List; +import study.race.model.Car; +import study.race.model.CarListGenerator; +import study.race.model.Cars; +import study.race.model.RaceResult; import static org.assertj.core.api.Assertions.assertThat; public class CarRacingTest { + private static final int NUMBER_OF_CAR = 3; + private static Cars stopCars; + private static Cars moveCars; + + @BeforeAll + static void init() { + stopCars = new CarListGenerator().generateCarList(NUMBER_OF_CAR); + moveCars = new CarListGenerator().generateCarList(NUMBER_OF_CAR); + } @Test @DisplayName("Create Cars to Race") void generateCarListTest() { - int numberOfCars = 3; - List carList = new CarListGenerator(3).generateCarList(); - assertThat(carList.size()).isEqualTo(numberOfCars); + Cars cars = new CarListGenerator().generateCarList(3); + assertThat(cars.carList().size()).isEqualTo(NUMBER_OF_CAR); + } + + @ParameterizedTest() + @DisplayName("When moving distance is 3 or less, car initial movement Test") + @CsvSource({ "0, 1", "1, 1", "2, 1", "3, 1"}) + void initialMoveCar() { + Cars cars = new CarListGenerator().generateCarList(NUMBER_OF_CAR); + Car car = cars.carList().get(0); + car.move(1); + int result = car.getCurrentLocation(); + assertThat(result).isEqualTo(1); } @ParameterizedTest(name="moving distance: {0}, current location: {1}") - @DisplayName("When moving distance is 4 or more, Car moving Test") - @CsvSource({ "4, 5", "5, 6", "6, 7", "7, 8", "8, 9", "9, 10"}) + @DisplayName("When moving distance is 4 or more, car moving Test") + @CsvSource({ "4, 1", "5, 2", "6, 3", "7, 4", "8, 5", "9, 6"}) void moveCar(int movingDistance, int expected) { - Car car = new Car(); + Car car = moveCars.carList().get(0); car.move(movingDistance); int result = car.getCurrentLocation(); assertThat(result).isEqualTo(expected); } @ParameterizedTest(name="moving distance: {0}, current location: {1}") - @DisplayName("When moving distance is 3 or less, Car moving Test") + @DisplayName("When moving distance is 3 or less, car moving Test") @CsvSource({ "0, 1", "1, 1", "2, 1", "3, 1"}) void stopCar(int movingDistance, int expected) { - Car car = new Car(); + Car car = stopCars.carList().get(0); car.move(movingDistance); int result = car.getCurrentLocation(); assertThat(result).isEqualTo(expected); } - @Test + @ParameterizedTest() @DisplayName("Result of racing update Test") - void updateRaceResult() { - int numberOfCar = 3; - int[] movingDistanceList = {3, 5, 7}; - int[] expected = {1, 6, 8}; - List carList = new CarListGenerator(numberOfCar).generateCarList(); - RaceResult raceResult = new RaceResult(carList); + @CsvSource({ "3, 1", "5, 2", "7, 3", "2, 3"}) + void updateRaceResult(int movingDistance, int expected) { + RaceResult raceResult = new RaceResult(stopCars); - for (int i = 0; i < carList.size(); i++) { - carList.get(i).move(movingDistanceList[i]); - } + // 차량 이동 및 결과 업데이트 + stopCars.carList().get(0).move(movingDistance); raceResult.updateRaceResult(); - int[] result = raceResult.getResult(); // 1, 6, 8 - - for (int i = 0; i < carList.size(); i++){ - assertThat(result[i]).isEqualTo(expected[i]); - } + // 결과 확인 + int[] result = raceResult.getResult(); + assertThat(result[0]).isEqualTo(expected); } } From e01686fe77f7a2efe6823772ff77b5141b4454a0 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 15 Nov 2023 13:35:54 +0900 Subject: [PATCH 13/26] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5=ED=95=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EA=B0=9C=EC=88=98=20=EB=A7=8C=ED=81=BC=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 자동차의 이름 문자열을 입력으로 받아, 문자열을 분리 후에 자동차의 이름 개수 만큼 자동차를 생성 --- .../java/study/{Step3.java => CarRacingMission.java} | 2 +- src/main/java/study/race/README.md | 12 +++++++++++- src/main/java/study/race/controller/CarRacing.java | 4 +++- src/main/java/study/race/model/CarListGenerator.java | 8 ++++++++ src/main/java/study/race/model/RunTrack.java | 6 ++++++ src/main/java/study/race/view/ReceiveInputValue.java | 10 ++++++++-- src/test/java/study/CarRacingTest.java | 11 +++++++++++ 7 files changed, 48 insertions(+), 5 deletions(-) rename src/main/java/study/{Step3.java => CarRacingMission.java} (82%) diff --git a/src/main/java/study/Step3.java b/src/main/java/study/CarRacingMission.java similarity index 82% rename from src/main/java/study/Step3.java rename to src/main/java/study/CarRacingMission.java index 31f548025ef..26ef6f6f7c3 100644 --- a/src/main/java/study/Step3.java +++ b/src/main/java/study/CarRacingMission.java @@ -2,7 +2,7 @@ import study.race.controller.CarRacing; -public class Step3 { +public class CarRacingMission { public static void main(String[] args) { new CarRacing().runCarRacing(); diff --git a/src/main/java/study/race/README.md b/src/main/java/study/race/README.md index 1453582323a..41cef322c3d 100644 --- a/src/main/java/study/race/README.md +++ b/src/main/java/study/race/README.md @@ -1,8 +1,18 @@ 구현할 기능 - +* [ ] 자동차 이름 자릿수 검사 +* [ ] 출력에 자동차 이름 넣기 +* [ ] 우승자 찾기 +* [ ] 우승자 출력 --- +### step 4 +* [X] 자동차 이름 받기 +* [X] 자동차 이름 문자열 분리 +* [X] 이름 개수 만큼 자동차 생성 + + +### step 3 * [X] 자동차 대수, 시도 횟수 입력받기 * [X] 대수에 맞게 자동차 생성 * [X] 자동차 이동 기능 구현 diff --git a/src/main/java/study/race/controller/CarRacing.java b/src/main/java/study/race/controller/CarRacing.java index e5d347332f4..b1cfa699f7e 100644 --- a/src/main/java/study/race/controller/CarRacing.java +++ b/src/main/java/study/race/controller/CarRacing.java @@ -17,8 +17,10 @@ public CarRacing() { public void runCarRacing() { int numberOfCar = receiveInputValue.receiveInputValueForCar(); int roundOfTrack = receiveInputValue.receiveInputValueForRound(); + String namesOfCar = receiveInputValue.receiveInputValueForCarNames(); - RunTrack runTrack = new RunTrack(numberOfCar); + String[] names = namesOfCar.split(","); + RunTrack runTrack = new RunTrack(names); System.out.println("\nExecution Result"); this.viewResult.printResult(runTrack.getRaceResult()); diff --git a/src/main/java/study/race/model/CarListGenerator.java b/src/main/java/study/race/model/CarListGenerator.java index 991ee4c3431..c7554533cef 100644 --- a/src/main/java/study/race/model/CarListGenerator.java +++ b/src/main/java/study/race/model/CarListGenerator.java @@ -4,6 +4,14 @@ public class CarListGenerator { + public Cars generateCarList(String[] carNameList) { + Cars cars = new Cars(); + for (int i = 0; i < carNameList.length; i++) { + cars.addCar(); + } + return cars; + } + public Cars generateCarList(int numberOfCar) { Cars cars = new Cars(); for (int i = 0; i < numberOfCar; i++) { diff --git a/src/main/java/study/race/model/RunTrack.java b/src/main/java/study/race/model/RunTrack.java index 3e36079e937..90b1206e0c7 100644 --- a/src/main/java/study/race/model/RunTrack.java +++ b/src/main/java/study/race/model/RunTrack.java @@ -15,6 +15,12 @@ public RunTrack(int numberOfCar) { this.raceResult = new RaceResult(this.cars); } + public RunTrack(String[] namesOfCar) { + this.random = new Random(); + this.cars = new CarListGenerator().generateCarList(namesOfCar); + this.raceResult = new RaceResult(this.cars); + } + public void start() { for (int i = 0; i < this.cars.carList().size(); i++) { int movingDistance = random.nextInt(10); diff --git a/src/main/java/study/race/view/ReceiveInputValue.java b/src/main/java/study/race/view/ReceiveInputValue.java index a2fce480a43..7342798b58b 100644 --- a/src/main/java/study/race/view/ReceiveInputValue.java +++ b/src/main/java/study/race/view/ReceiveInputValue.java @@ -5,7 +5,8 @@ public class ReceiveInputValue { private Scanner scanner; - private final String CAR_INPUT_MESSAGE = "Enter the number of cars."; + private final String CAR_INT_INPUT_MESSAGE = "Enter the number of cars."; + private final String CAR_STRING_INPUT_MESSAGE = "Enter the names of the cars to race."; private final String ROUND_INPUT_MESSAGE = "Enter how many rounds are we going to do."; public ReceiveInputValue() { @@ -13,10 +14,15 @@ public ReceiveInputValue() { } public int receiveInputValueForCar() { - System.out.println(CAR_INPUT_MESSAGE); + System.out.println(CAR_INT_INPUT_MESSAGE); return this.scanner.nextInt(); } + public String receiveInputValueForCarNames() { + System.out.println(CAR_STRING_INPUT_MESSAGE); + return this.scanner.nextLine(); + } + public int receiveInputValueForRound() { System.out.println(ROUND_INPUT_MESSAGE); return this.scanner.nextInt(); diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 6d288348e1a..367011f1ff3 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; import study.race.model.Car; import study.race.model.CarListGenerator; import study.race.model.Cars; @@ -78,4 +79,14 @@ void updateRaceResult(int movingDistance, int expected) { int[] result = raceResult.getResult(); assertThat(result[0]).isEqualTo(expected); } + + + @ParameterizedTest() + @DisplayName("Generating car as many as car names") + @ValueSource(strings = { "car1,car2", "car1,car2,car3", "car1,car2,car3,car4"}) + void generateCarAsManyAsCarNames(String input) { + String[] names = input.split(","); + Cars cars = new CarListGenerator().generateCarList(names); + assertThat(cars.carList().size()).isEqualTo(names.length); + } } From 7bb20e51d5f3c3ad3de9158deea40d3c4ba30c7d Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 15 Nov 2023 14:30:32 +0900 Subject: [PATCH 14/26] =?UTF-8?q?feat:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=845=EC=9E=90=EB=A6=AC=20=EC=A0=9C=ED=95=9C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/study/race/controller/CarRacing.java | 20 ++++++++++++++++--- src/test/java/study/CarRacingTest.java | 12 ++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/study/race/controller/CarRacing.java b/src/main/java/study/race/controller/CarRacing.java index b1cfa699f7e..ad9d977f4fd 100644 --- a/src/main/java/study/race/controller/CarRacing.java +++ b/src/main/java/study/race/controller/CarRacing.java @@ -15,11 +15,15 @@ public CarRacing() { } public void runCarRacing() { - int numberOfCar = receiveInputValue.receiveInputValueForCar(); + String[] names; + do { + String namesOfCar = receiveInputValue.receiveInputValueForCarNames(); + + names = namesOfCar.split(","); + } while (checkLengthLimitOfCarName(names)); + int roundOfTrack = receiveInputValue.receiveInputValueForRound(); - String namesOfCar = receiveInputValue.receiveInputValueForCarNames(); - String[] names = namesOfCar.split(","); RunTrack runTrack = new RunTrack(names); System.out.println("\nExecution Result"); @@ -29,4 +33,14 @@ public void runCarRacing() { this.viewResult.printResult(runTrack.getRaceResult()); } } + + public boolean checkLengthLimitOfCarName(String[] names) { + for (String name: names) { + if (name.length() > 5) { + System.out.println("The length of the car name '" + name + "' exceeds 5."); + return false; + } + } + return true; + } } diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 367011f1ff3..c50f2175300 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; +import study.race.controller.CarRacing; import study.race.model.Car; import study.race.model.CarListGenerator; import study.race.model.Cars; @@ -80,7 +81,6 @@ void updateRaceResult(int movingDistance, int expected) { assertThat(result[0]).isEqualTo(expected); } - @ParameterizedTest() @DisplayName("Generating car as many as car names") @ValueSource(strings = { "car1,car2", "car1,car2,car3", "car1,car2,car3,car4"}) @@ -89,4 +89,14 @@ void generateCarAsManyAsCarNames(String input) { Cars cars = new CarListGenerator().generateCarList(names); assertThat(cars.carList().size()).isEqualTo(names.length); } + + @ParameterizedTest() + @DisplayName("Check for length of car's name greater than 5.") + @ValueSource(strings = { "car1,superCar", "car1,car2,car34567", "audi,benz,hyundai,kia"}) + void checkLengthOfCarNameTest(String input) { + String[] names = input.split(","); + + boolean result = new CarRacing().checkLengthLimitOfCarName(names); + assertThat(result).isEqualTo(false); + } } From e2bfae987e409ac1e80217d0b470bb9be91a6d29 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 15 Nov 2023 18:03:19 +0900 Subject: [PATCH 15/26] =?UTF-8?q?refactor=20and=20test:=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=EC=B0=A8=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EB=8A=94=20=EA=B0=9D=EC=B2=B4=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 자동차 경주에 대한 Input 데이터를 관리하는 객체 정의. 해당 객체에서 자동차 이름과 시도 횟수를 입력 받음. 또한, 자동차 이름이 5자리를 넘어가면 다시 입력하게 함. test는 입력한 자동차의 이름 수대로 차량을 생성하는지 확인하는 테스트 추가. RaceInfo 클래스를 정의하여, 입력에 대한 데이터들을 캡슐화. --- src/main/java/study/race/README.md | 2 +- .../java/study/race/controller/CarRacing.java | 28 ++------- .../study/race/model/CarListGenerator.java | 8 --- .../race/model/CarRacingInputManager.java | 59 +++++++++++++++++++ src/main/java/study/race/model/RaceInfo.java | 23 ++++++++ src/main/java/study/race/model/RunTrack.java | 9 +-- src/test/java/study/CarRacingTest.java | 52 ++++++++-------- 7 files changed, 117 insertions(+), 64 deletions(-) create mode 100644 src/main/java/study/race/model/CarRacingInputManager.java create mode 100644 src/main/java/study/race/model/RaceInfo.java diff --git a/src/main/java/study/race/README.md b/src/main/java/study/race/README.md index 41cef322c3d..0c29d666de8 100644 --- a/src/main/java/study/race/README.md +++ b/src/main/java/study/race/README.md @@ -1,5 +1,4 @@ 구현할 기능 -* [ ] 자동차 이름 자릿수 검사 * [ ] 출력에 자동차 이름 넣기 * [ ] 우승자 찾기 * [ ] 우승자 출력 @@ -10,6 +9,7 @@ * [X] 자동차 이름 받기 * [X] 자동차 이름 문자열 분리 * [X] 이름 개수 만큼 자동차 생성 +* [X] 자동차 이름 자릿수 검사 ### step 3 diff --git a/src/main/java/study/race/controller/CarRacing.java b/src/main/java/study/race/controller/CarRacing.java index ad9d977f4fd..b207cd80e40 100644 --- a/src/main/java/study/race/controller/CarRacing.java +++ b/src/main/java/study/race/controller/CarRacing.java @@ -1,5 +1,7 @@ package study.race.controller; +import study.race.model.CarRacingInputManager; +import study.race.model.RaceInfo; import study.race.view.ReceiveInputValue; import study.race.model.RunTrack; import study.race.view.ViewResult; @@ -10,37 +12,17 @@ public class CarRacing { private ViewResult viewResult; public CarRacing() { - this.receiveInputValue = new ReceiveInputValue(); this.viewResult = new ViewResult(); } public void runCarRacing() { - String[] names; - do { - String namesOfCar = receiveInputValue.receiveInputValueForCarNames(); - - names = namesOfCar.split(","); - } while (checkLengthLimitOfCarName(names)); - - int roundOfTrack = receiveInputValue.receiveInputValueForRound(); - - RunTrack runTrack = new RunTrack(names); + RaceInfo raceInfo = new CarRacingInputManager().getRaceInfo(); + RunTrack runTrack = new RunTrack(raceInfo.getCarNames()); System.out.println("\nExecution Result"); - this.viewResult.printResult(runTrack.getRaceResult()); - for (int i = 0; i < roundOfTrack; i++) { + for (int i = 0; i < raceInfo.getRoundOfTrack(); i++) { runTrack.start(); this.viewResult.printResult(runTrack.getRaceResult()); } } - - public boolean checkLengthLimitOfCarName(String[] names) { - for (String name: names) { - if (name.length() > 5) { - System.out.println("The length of the car name '" + name + "' exceeds 5."); - return false; - } - } - return true; - } } diff --git a/src/main/java/study/race/model/CarListGenerator.java b/src/main/java/study/race/model/CarListGenerator.java index c7554533cef..a77aac6a22d 100644 --- a/src/main/java/study/race/model/CarListGenerator.java +++ b/src/main/java/study/race/model/CarListGenerator.java @@ -11,12 +11,4 @@ public Cars generateCarList(String[] carNameList) { } return cars; } - - public Cars generateCarList(int numberOfCar) { - Cars cars = new Cars(); - for (int i = 0; i < numberOfCar; i++) { - cars.addCar(); - } - return cars; - } } diff --git a/src/main/java/study/race/model/CarRacingInputManager.java b/src/main/java/study/race/model/CarRacingInputManager.java new file mode 100644 index 00000000000..13f57180a04 --- /dev/null +++ b/src/main/java/study/race/model/CarRacingInputManager.java @@ -0,0 +1,59 @@ +package study.race.model; + +import study.race.view.ReceiveInputValue; + +public class CarRacingInputManager { + + private final int LENGTH_LIMIT = 5; + private final int IS_EXCEED = 1; + private final int NOT_EXCEED = 0; + private String[] carNames; + private ReceiveInputValue receiveInputValue; + private RaceInfo raceInfo; + + public CarRacingInputManager() { + this.receiveInputValue = new ReceiveInputValue(); + this.raceInfo = new RaceInfo(); + this.getCarNames(); + this.getRoundOfTrack(); + } + + private void getCarNames() { + String[] names; + do { + String namesOfCar = receiveInputValue.receiveInputValueForCarNames(); + names = namesOfCar.split(","); + } while (hasOverLengthForCarNames(names)); + this.raceInfo.setCarNames(names); + } + + + private boolean hasOverLengthForCarNames(String[] names) { + return countNumberOfNamesForOverLength(names) > 0; + } + + private int countNumberOfNamesForOverLength(String[] names) { + int exceedCount = 0; + for (String name: names) { + exceedCount += isExceeded(name); + } + return exceedCount; + } + + private int isExceeded(String name) { + if (name.length() > LENGTH_LIMIT) { + System.out.println("The length of the car name '" + name + "' exceeds 5."); + return IS_EXCEED; + } + return NOT_EXCEED; + } + + private void getRoundOfTrack() { + this.raceInfo.setRoundOfTrack(receiveInputValue.receiveInputValueForRound()); + } + + public RaceInfo getRaceInfo() { + return this.raceInfo; + } + +} diff --git a/src/main/java/study/race/model/RaceInfo.java b/src/main/java/study/race/model/RaceInfo.java new file mode 100644 index 00000000000..df744245a6a --- /dev/null +++ b/src/main/java/study/race/model/RaceInfo.java @@ -0,0 +1,23 @@ +package study.race.model; + +public class RaceInfo { + + private String[] carNames; + private int roundOfTrack; + + public String[] getCarNames() { + return this.carNames; + } + + public int getRoundOfTrack() { + return this.roundOfTrack; + } + + public void setCarNames(String[] names) { + this.carNames = names; + } + + public void setRoundOfTrack(int roundOfTrack) { + this.roundOfTrack = roundOfTrack; + } +} diff --git a/src/main/java/study/race/model/RunTrack.java b/src/main/java/study/race/model/RunTrack.java index 90b1206e0c7..5119e1444f7 100644 --- a/src/main/java/study/race/model/RunTrack.java +++ b/src/main/java/study/race/model/RunTrack.java @@ -4,17 +4,12 @@ public class RunTrack { + private final int RANDOM_LIMIT_NUMBER = 10; private Random random; private Cars cars; private RaceResult raceResult; - public RunTrack(int numberOfCar) { - this.random = new Random(); - this.cars = new CarListGenerator().generateCarList(numberOfCar); - this.raceResult = new RaceResult(this.cars); - } - public RunTrack(String[] namesOfCar) { this.random = new Random(); this.cars = new CarListGenerator().generateCarList(namesOfCar); @@ -23,7 +18,7 @@ public RunTrack(String[] namesOfCar) { public void start() { for (int i = 0; i < this.cars.carList().size(); i++) { - int movingDistance = random.nextInt(10); + int movingDistance = random.nextInt(RANDOM_LIMIT_NUMBER); this.cars.carList().get(i).move(movingDistance); } this.raceResult.updateRaceResult(); diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index c50f2175300..e8692051e85 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -8,10 +8,7 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import study.race.controller.CarRacing; -import study.race.model.Car; -import study.race.model.CarListGenerator; -import study.race.model.Cars; -import study.race.model.RaceResult; +import study.race.model.*; import static org.assertj.core.api.Assertions.assertThat; @@ -19,36 +16,39 @@ public class CarRacingTest { private static final int NUMBER_OF_CAR = 3; + private static final String CAR_NAMES = "audi,benz,hd,kia"; + private static String[] carNames; private static Cars stopCars; private static Cars moveCars; @BeforeAll static void init() { - stopCars = new CarListGenerator().generateCarList(NUMBER_OF_CAR); - moveCars = new CarListGenerator().generateCarList(NUMBER_OF_CAR); + carNames = CAR_NAMES.split(","); + stopCars = new CarListGenerator().generateCarList(carNames); + moveCars = new CarListGenerator().generateCarList(carNames); } @Test @DisplayName("Create Cars to Race") void generateCarListTest() { - Cars cars = new CarListGenerator().generateCarList(3); - assertThat(cars.carList().size()).isEqualTo(NUMBER_OF_CAR); + Cars cars = new CarListGenerator().generateCarList(carNames); + assertThat(cars.carList().size()).isEqualTo(carNames.length); } @ParameterizedTest() @DisplayName("When moving distance is 3 or less, car initial movement Test") - @CsvSource({ "0, 1", "1, 1", "2, 1", "3, 1"}) + @CsvSource({"0, 1", "1, 1", "2, 1", "3, 1"}) void initialMoveCar() { - Cars cars = new CarListGenerator().generateCarList(NUMBER_OF_CAR); + Cars cars = new CarListGenerator().generateCarList(carNames); Car car = cars.carList().get(0); car.move(1); int result = car.getCurrentLocation(); assertThat(result).isEqualTo(1); } - @ParameterizedTest(name="moving distance: {0}, current location: {1}") + @ParameterizedTest(name = "moving distance: {0}, current location: {1}") @DisplayName("When moving distance is 4 or more, car moving Test") - @CsvSource({ "4, 1", "5, 2", "6, 3", "7, 4", "8, 5", "9, 6"}) + @CsvSource({"4, 1", "5, 2", "6, 3", "7, 4", "8, 5", "9, 6"}) void moveCar(int movingDistance, int expected) { Car car = moveCars.carList().get(0); car.move(movingDistance); @@ -56,9 +56,9 @@ void moveCar(int movingDistance, int expected) { assertThat(result).isEqualTo(expected); } - @ParameterizedTest(name="moving distance: {0}, current location: {1}") + @ParameterizedTest(name = "moving distance: {0}, current location: {1}") @DisplayName("When moving distance is 3 or less, car moving Test") - @CsvSource({ "0, 1", "1, 1", "2, 1", "3, 1"}) + @CsvSource({"0, 1", "1, 1", "2, 1", "3, 1"}) void stopCar(int movingDistance, int expected) { Car car = stopCars.carList().get(0); car.move(movingDistance); @@ -68,7 +68,7 @@ void stopCar(int movingDistance, int expected) { @ParameterizedTest() @DisplayName("Result of racing update Test") - @CsvSource({ "3, 1", "5, 2", "7, 3", "2, 3"}) + @CsvSource({"3, 1", "5, 2", "7, 3", "2, 3"}) void updateRaceResult(int movingDistance, int expected) { RaceResult raceResult = new RaceResult(stopCars); @@ -83,20 +83,22 @@ void updateRaceResult(int movingDistance, int expected) { @ParameterizedTest() @DisplayName("Generating car as many as car names") - @ValueSource(strings = { "car1,car2", "car1,car2,car3", "car1,car2,car3,car4"}) + @ValueSource(strings = {"car1,car2", "car1,car2,car3", "car1,car2,car3,car4"}) void generateCarAsManyAsCarNames(String input) { String[] names = input.split(","); Cars cars = new CarListGenerator().generateCarList(names); assertThat(cars.carList().size()).isEqualTo(names.length); } - @ParameterizedTest() - @DisplayName("Check for length of car's name greater than 5.") - @ValueSource(strings = { "car1,superCar", "car1,car2,car34567", "audi,benz,hyundai,kia"}) - void checkLengthOfCarNameTest(String input) { - String[] names = input.split(","); - - boolean result = new CarRacing().checkLengthLimitOfCarName(names); - assertThat(result).isEqualTo(false); - } +// @ParameterizedTest() +// @DisplayName("Check for length of car's name greater than 5.") +// @ValueSource(strings = { "car1,superCar", "car1,car2,car34567", "audi,benz,hyundai,kia"}) +// void checkLengthOfCarNameTest(String input) { +// String[] names = input.split(","); +// +// new CarRacingInputManager().getRaceInfo(); +// +// boolean result = new CarRacing().checkLengthLimitOfCarName(names); +// assertThat(result).isEqualTo(false); +// } } From 3512e4facac0b645660512796e1015ff9d0efcb4 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 15 Nov 2023 20:48:03 +0900 Subject: [PATCH 16/26] chore --- src/main/java/study/race/model/CarRacingInputManager.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/study/race/model/CarRacingInputManager.java b/src/main/java/study/race/model/CarRacingInputManager.java index 13f57180a04..e89258425af 100644 --- a/src/main/java/study/race/model/CarRacingInputManager.java +++ b/src/main/java/study/race/model/CarRacingInputManager.java @@ -27,7 +27,6 @@ private void getCarNames() { this.raceInfo.setCarNames(names); } - private boolean hasOverLengthForCarNames(String[] names) { return countNumberOfNamesForOverLength(names) > 0; } From 885aa7a4d02e55c50dae66e96c9b53feef29451f Mon Sep 17 00:00:00 2001 From: sudo-init Date: Thu, 16 Nov 2023 11:39:03 +0900 Subject: [PATCH 17/26] =?UTF-8?q?feat=20and=20refactor:=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EA=B2=B0=EA=B3=BC=EC=97=90=20=EC=9E=90=EB=8F=99?= =?UTF-8?q?=EC=B0=A8=20=EC=9D=B4=EB=A6=84=EC=9D=B4=20=EC=B6=9C=EB=A0=A5?= =?UTF-8?q?=EB=90=98=EB=8F=84=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/study/race/controller/CarRacing.java | 2 +- src/main/java/study/race/model/Car.java | 7 +++- .../study/race/model/CarListGenerator.java | 2 +- src/main/java/study/race/model/Cars.java | 4 +-- .../java/study/race/model/RaceResult.java | 17 ++++++---- src/main/java/study/race/model/RunTrack.java | 4 +-- src/main/java/study/race/view/ViewResult.java | 9 ++++-- src/test/java/study/CarRacingTest.java | 32 +++++++++++-------- 8 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/main/java/study/race/controller/CarRacing.java b/src/main/java/study/race/controller/CarRacing.java index b207cd80e40..1d9464980c3 100644 --- a/src/main/java/study/race/controller/CarRacing.java +++ b/src/main/java/study/race/controller/CarRacing.java @@ -22,7 +22,7 @@ public void runCarRacing() { System.out.println("\nExecution Result"); for (int i = 0; i < raceInfo.getRoundOfTrack(); i++) { runTrack.start(); - this.viewResult.printResult(runTrack.getRaceResult()); + this.viewResult.printRaceResult(runTrack.getRaceResult()); } } } diff --git a/src/main/java/study/race/model/Car.java b/src/main/java/study/race/model/Car.java index 7b25c6eaf52..87ebd150b98 100644 --- a/src/main/java/study/race/model/Car.java +++ b/src/main/java/study/race/model/Car.java @@ -2,14 +2,19 @@ public class Car { + private String name; private int currentLocation; private CarLocationManager carLocationManager; - public Car() { + public Car(String name) { + this.name = name; this.carLocationManager = new CarLocationManager(); this.currentLocation = carLocationManager.initMovingDistance(); } + public String getName() { + return this.name; + } public int getCurrentLocation() { return this.currentLocation; } diff --git a/src/main/java/study/race/model/CarListGenerator.java b/src/main/java/study/race/model/CarListGenerator.java index a77aac6a22d..c67f7da02b3 100644 --- a/src/main/java/study/race/model/CarListGenerator.java +++ b/src/main/java/study/race/model/CarListGenerator.java @@ -7,7 +7,7 @@ public class CarListGenerator { public Cars generateCarList(String[] carNameList) { Cars cars = new Cars(); for (int i = 0; i < carNameList.length; i++) { - cars.addCar(); + cars.addCar(carNameList[i]); } return cars; } diff --git a/src/main/java/study/race/model/Cars.java b/src/main/java/study/race/model/Cars.java index 02149f1953e..5b1cc20804d 100644 --- a/src/main/java/study/race/model/Cars.java +++ b/src/main/java/study/race/model/Cars.java @@ -17,7 +17,7 @@ public List carList() { return this.carList; } - public void addCar() { - this.carList.add(new Car()); + public void addCar(String name) { + this.carList.add(new Car(name)); } } diff --git a/src/main/java/study/race/model/RaceResult.java b/src/main/java/study/race/model/RaceResult.java index 1dadb0ba81e..b9f064f49b5 100644 --- a/src/main/java/study/race/model/RaceResult.java +++ b/src/main/java/study/race/model/RaceResult.java @@ -3,27 +3,30 @@ import study.race.model.Car; import study.race.model.Cars; +import java.util.LinkedHashMap; +import java.util.Map; + public class RaceResult { private int numberOfCar; - private int[] racingResult; + private Map racingResult; private Cars cars; public RaceResult(Cars cars) { this.cars = cars; this.numberOfCar = cars.carList().size(); - racingResult = new int[this.numberOfCar]; + racingResult = new LinkedHashMap<>(); this.updateRaceResult(); } - public int[] getResult() { - return this.racingResult; - } - public void updateRaceResult() { for (int i = 0; i < this.numberOfCar; i++) { Car car = cars.carList().get(i); - racingResult[i] = car.getCurrentLocation(); + racingResult.put(car.getName(), car.getCurrentLocation()); } } + + public Map getResult() { + return this.racingResult; + } } diff --git a/src/main/java/study/race/model/RunTrack.java b/src/main/java/study/race/model/RunTrack.java index 5119e1444f7..e4ebe24ef66 100644 --- a/src/main/java/study/race/model/RunTrack.java +++ b/src/main/java/study/race/model/RunTrack.java @@ -24,7 +24,7 @@ public void start() { this.raceResult.updateRaceResult(); } - public int[] getRaceResult() { - return this.raceResult.getResult(); + public RaceResult getRaceResult() { + return this.raceResult; } } diff --git a/src/main/java/study/race/view/ViewResult.java b/src/main/java/study/race/view/ViewResult.java index 970d988e16d..d77b7b2278f 100644 --- a/src/main/java/study/race/view/ViewResult.java +++ b/src/main/java/study/race/view/ViewResult.java @@ -1,12 +1,15 @@ package study.race.view; +import study.race.model.RaceResult; + import java.util.List; +import java.util.Map; public class ViewResult { - public void printResult(int[] results) { - for (int result: results) { - System.out.println("-".repeat(result)); + public void printRaceResult(RaceResult raceResult) { + for (Map.Entry result: raceResult.getResult().entrySet()) { + System.out.println(result.getKey() + " : " + "-".repeat(result.getValue())); } System.out.println(); } diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index e8692051e85..f0516f15e81 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -73,12 +73,14 @@ void updateRaceResult(int movingDistance, int expected) { RaceResult raceResult = new RaceResult(stopCars); // 차량 이동 및 결과 업데이트 - stopCars.carList().get(0).move(movingDistance); + Car car = stopCars.carList().get(0); + String carName = car.getName(); + car.move(movingDistance); raceResult.updateRaceResult(); // 결과 확인 - int[] result = raceResult.getResult(); - assertThat(result[0]).isEqualTo(expected); + int result = raceResult.getResult().get(carName); + assertThat(result).isEqualTo(expected); } @ParameterizedTest() @@ -90,15 +92,17 @@ void generateCarAsManyAsCarNames(String input) { assertThat(cars.carList().size()).isEqualTo(names.length); } -// @ParameterizedTest() -// @DisplayName("Check for length of car's name greater than 5.") -// @ValueSource(strings = { "car1,superCar", "car1,car2,car34567", "audi,benz,hyundai,kia"}) -// void checkLengthOfCarNameTest(String input) { -// String[] names = input.split(","); -// -// new CarRacingInputManager().getRaceInfo(); -// -// boolean result = new CarRacing().checkLengthLimitOfCarName(names); -// assertThat(result).isEqualTo(false); -// } + @ParameterizedTest() + @DisplayName("Check for length of car's name greater than 5.") + @ValueSource(strings = { "car1,superCar", "car1,car2,car34567", "audi,benz,hyundai,kia"}) + void checkLengthOfCarNameTest(String input) { + String[] names = input.split(","); + + new CarRacingInputManager().getRaceInfo(); + + //Test에서 RaceInfo를 받아서 거기에 있는 이름이 다섯자를 안넘는지 체크 + + boolean result = new CarRacing().checkLengthLimitOfCarName(names); + assertThat(result).isEqualTo(false); + } } From 14dd35314cdf7669b1308ff9b3091d46eb3764ae Mon Sep 17 00:00:00 2001 From: sudo-init Date: Thu, 16 Nov 2023 15:37:43 +0900 Subject: [PATCH 18/26] backup --- src/main/java/study/race/README.md | 2 +- .../java/study/race/model/FindWinner.java | 40 +++++++++++++++++++ .../java/study/race/model/RaceResult.java | 5 +++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/main/java/study/race/model/FindWinner.java diff --git a/src/main/java/study/race/README.md b/src/main/java/study/race/README.md index 0c29d666de8..6173a52d38c 100644 --- a/src/main/java/study/race/README.md +++ b/src/main/java/study/race/README.md @@ -1,5 +1,4 @@ 구현할 기능 -* [ ] 출력에 자동차 이름 넣기 * [ ] 우승자 찾기 * [ ] 우승자 출력 @@ -10,6 +9,7 @@ * [X] 자동차 이름 문자열 분리 * [X] 이름 개수 만큼 자동차 생성 * [X] 자동차 이름 자릿수 검사 +* [X] 출력에 자동차 이름 넣기 ### step 3 diff --git a/src/main/java/study/race/model/FindWinner.java b/src/main/java/study/race/model/FindWinner.java new file mode 100644 index 00000000000..5780831fa1d --- /dev/null +++ b/src/main/java/study/race/model/FindWinner.java @@ -0,0 +1,40 @@ +package study.race.model; + +public class FindWinner { + + private RaceResult raceResult; + + public FindWinner(RaceResult raceResult) { + this.raceResult = raceResult; + } + + + public void findWinner() { + findMaxDistance(); + + } + + private void findMaxDistance() { + this.raceResult.getResult(); + //맵을 순회하면서 맥스를 찾음. + + + for (int i = 0; i < this.numberOfCar; i++) { + Car car = cars.carList().get(i); + int distance = car.getCurrentLocation(); + compareDistance(distance); + } + } + private void compareDistance(int distance) { + if (this.maxDistance < distance) { + this.maxDistance = distance; + } + } + + private void checkWinner() { + } + + + + +} diff --git a/src/main/java/study/race/model/RaceResult.java b/src/main/java/study/race/model/RaceResult.java index b9f064f49b5..a23369c1662 100644 --- a/src/main/java/study/race/model/RaceResult.java +++ b/src/main/java/study/race/model/RaceResult.java @@ -9,12 +9,15 @@ public class RaceResult { private int numberOfCar; + private int maxDistance; + private String[] winners; private Map racingResult; private Cars cars; public RaceResult(Cars cars) { this.cars = cars; this.numberOfCar = cars.carList().size(); + this.maxDistance = 0; racingResult = new LinkedHashMap<>(); this.updateRaceResult(); } @@ -29,4 +32,6 @@ public void updateRaceResult() { public Map getResult() { return this.racingResult; } + + } From 57963e5719f3a632506aa241ab12c620786b80ab Mon Sep 17 00:00:00 2001 From: sudo-init Date: Fri, 17 Nov 2023 21:42:25 +0900 Subject: [PATCH 19/26] Tmp backup --- src/main/java/study/race/controller/CarRacing.java | 10 ++++------ .../java/study/race/model/CarRacingInputManager.java | 6 +++--- .../race/model/{RaceInfo.java => RaceResister.java} | 2 +- .../study/race/model/{FindWinner.java => Victory.java} | 5 ++--- 4 files changed, 10 insertions(+), 13 deletions(-) rename src/main/java/study/race/model/{RaceInfo.java => RaceResister.java} (93%) rename src/main/java/study/race/model/{FindWinner.java => Victory.java} (90%) diff --git a/src/main/java/study/race/controller/CarRacing.java b/src/main/java/study/race/controller/CarRacing.java index 1d9464980c3..ec37ceda573 100644 --- a/src/main/java/study/race/controller/CarRacing.java +++ b/src/main/java/study/race/controller/CarRacing.java @@ -1,14 +1,12 @@ package study.race.controller; import study.race.model.CarRacingInputManager; -import study.race.model.RaceInfo; -import study.race.view.ReceiveInputValue; +import study.race.model.RaceResister; import study.race.model.RunTrack; import study.race.view.ViewResult; public class CarRacing { - private ReceiveInputValue receiveInputValue; private ViewResult viewResult; public CarRacing() { @@ -16,11 +14,11 @@ public CarRacing() { } public void runCarRacing() { - RaceInfo raceInfo = new CarRacingInputManager().getRaceInfo(); - RunTrack runTrack = new RunTrack(raceInfo.getCarNames()); + RaceResister raceResister = new CarRacingInputManager().getRaceInfo(); + RunTrack runTrack = new RunTrack(raceResister.getCarNames()); System.out.println("\nExecution Result"); - for (int i = 0; i < raceInfo.getRoundOfTrack(); i++) { + for (int i = 0; i < raceResister.getRoundOfTrack(); i++) { runTrack.start(); this.viewResult.printRaceResult(runTrack.getRaceResult()); } diff --git a/src/main/java/study/race/model/CarRacingInputManager.java b/src/main/java/study/race/model/CarRacingInputManager.java index e89258425af..30e2703af92 100644 --- a/src/main/java/study/race/model/CarRacingInputManager.java +++ b/src/main/java/study/race/model/CarRacingInputManager.java @@ -9,11 +9,11 @@ public class CarRacingInputManager { private final int NOT_EXCEED = 0; private String[] carNames; private ReceiveInputValue receiveInputValue; - private RaceInfo raceInfo; + private RaceResister raceInfo; public CarRacingInputManager() { this.receiveInputValue = new ReceiveInputValue(); - this.raceInfo = new RaceInfo(); + this.raceInfo = new RaceResister(); this.getCarNames(); this.getRoundOfTrack(); } @@ -51,7 +51,7 @@ private void getRoundOfTrack() { this.raceInfo.setRoundOfTrack(receiveInputValue.receiveInputValueForRound()); } - public RaceInfo getRaceInfo() { + public RaceResister getRaceInfo() { return this.raceInfo; } diff --git a/src/main/java/study/race/model/RaceInfo.java b/src/main/java/study/race/model/RaceResister.java similarity index 93% rename from src/main/java/study/race/model/RaceInfo.java rename to src/main/java/study/race/model/RaceResister.java index df744245a6a..d3686bea5ec 100644 --- a/src/main/java/study/race/model/RaceInfo.java +++ b/src/main/java/study/race/model/RaceResister.java @@ -1,6 +1,6 @@ package study.race.model; -public class RaceInfo { +public class RaceResister { private String[] carNames; private int roundOfTrack; diff --git a/src/main/java/study/race/model/FindWinner.java b/src/main/java/study/race/model/Victory.java similarity index 90% rename from src/main/java/study/race/model/FindWinner.java rename to src/main/java/study/race/model/Victory.java index 5780831fa1d..333ca32d467 100644 --- a/src/main/java/study/race/model/FindWinner.java +++ b/src/main/java/study/race/model/Victory.java @@ -1,14 +1,13 @@ package study.race.model; -public class FindWinner { +public class Victory { private RaceResult raceResult; - public FindWinner(RaceResult raceResult) { + public Victory(RaceResult raceResult) { this.raceResult = raceResult; } - public void findWinner() { findMaxDistance(); From 0d3d66eb1754a69544421098c9a2c0926cab2e95 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Mon, 20 Nov 2023 16:55:17 +0900 Subject: [PATCH 20/26] =?UTF-8?q?refactor=20and=20feat:=20=EC=9A=B0?= =?UTF-8?q?=EC=8A=B9=EC=9E=90=20=EC=84=A0=EC=A0=95=20=EB=B0=8F=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20&=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/race/README.md | 6 +- .../java/study/race/controller/CarRacing.java | 25 +++++--- src/main/java/study/race/model/Car.java | 8 +-- .../study/race/model/CarListGenerator.java | 14 ----- ...ionManager.java => CarMovingDistance.java} | 4 +- .../race/model/CarRacingInputManager.java | 58 ------------------- .../java/study/race/model/RaceResister.java | 23 -------- .../java/study/race/model/RaceResult.java | 34 ++++++++++- .../race/model/{RunTrack.java => Racing.java} | 12 ++-- .../java/study/race/model/RacingRegister.java | 36 ++++++++++++ src/main/java/study/race/model/Victory.java | 39 ------------- src/main/java/study/race/model/Winners.java | 29 ++++++++++ .../study/race/view/ReceiveInputValue.java | 2 +- src/main/java/study/race/view/ViewResult.java | 8 ++- src/test/java/study/CarRacingTest.java | 33 +++++------ 15 files changed, 151 insertions(+), 180 deletions(-) delete mode 100644 src/main/java/study/race/model/CarListGenerator.java rename src/main/java/study/race/model/{CarLocationManager.java => CarMovingDistance.java} (84%) delete mode 100644 src/main/java/study/race/model/CarRacingInputManager.java delete mode 100644 src/main/java/study/race/model/RaceResister.java rename src/main/java/study/race/model/{RunTrack.java => Racing.java} (75%) create mode 100644 src/main/java/study/race/model/RacingRegister.java delete mode 100644 src/main/java/study/race/model/Victory.java create mode 100644 src/main/java/study/race/model/Winners.java diff --git a/src/main/java/study/race/README.md b/src/main/java/study/race/README.md index 6173a52d38c..9d8f5da0e62 100644 --- a/src/main/java/study/race/README.md +++ b/src/main/java/study/race/README.md @@ -1,6 +1,5 @@ 구현할 기능 -* [ ] 우승자 찾기 -* [ ] 우승자 출력 + --- @@ -10,7 +9,8 @@ * [X] 이름 개수 만큼 자동차 생성 * [X] 자동차 이름 자릿수 검사 * [X] 출력에 자동차 이름 넣기 - +* [X] 우승자 찾기 +* [X] 우승자 출력 ### step 3 * [X] 자동차 대수, 시도 횟수 입력받기 diff --git a/src/main/java/study/race/controller/CarRacing.java b/src/main/java/study/race/controller/CarRacing.java index ec37ceda573..d34d4fe90d2 100644 --- a/src/main/java/study/race/controller/CarRacing.java +++ b/src/main/java/study/race/controller/CarRacing.java @@ -1,26 +1,35 @@ package study.race.controller; -import study.race.model.CarRacingInputManager; -import study.race.model.RaceResister; -import study.race.model.RunTrack; +import study.race.model.Cars; +import study.race.model.RacingRegister; +import study.race.model.Racing; +import study.race.view.ReceiveInputValue; import study.race.view.ViewResult; public class CarRacing { private ViewResult viewResult; + private RacingRegister racingRegister; + private ReceiveInputValue receiveInputValue; public CarRacing() { this.viewResult = new ViewResult(); + this.racingRegister = new RacingRegister(); + this.receiveInputValue = new ReceiveInputValue(); } public void runCarRacing() { - RaceResister raceResister = new CarRacingInputManager().getRaceInfo(); - RunTrack runTrack = new RunTrack(raceResister.getCarNames()); + String carNames = this.receiveInputValue.receiveInputValueForCarNames(); + int roundOfTrack = this.receiveInputValue.receiveInputValueForRound(); + + Cars cars = this.racingRegister.register(carNames); + Racing racing = new Racing(cars); System.out.println("\nExecution Result"); - for (int i = 0; i < raceResister.getRoundOfTrack(); i++) { - runTrack.start(); - this.viewResult.printRaceResult(runTrack.getRaceResult()); + for (int i = 0; i < roundOfTrack; i++) { + racing.start(); + this.viewResult.printRacingResult(racing.getRaceResult()); } + this.viewResult.printRacingWinners(racing.getWinners()); } } diff --git a/src/main/java/study/race/model/Car.java b/src/main/java/study/race/model/Car.java index 87ebd150b98..05413d7c80c 100644 --- a/src/main/java/study/race/model/Car.java +++ b/src/main/java/study/race/model/Car.java @@ -4,12 +4,12 @@ public class Car { private String name; private int currentLocation; - private CarLocationManager carLocationManager; + private CarMovingDistance carMovingDistance; public Car(String name) { this.name = name; - this.carLocationManager = new CarLocationManager(); - this.currentLocation = carLocationManager.initMovingDistance(); + this.carMovingDistance = new CarMovingDistance(); + this.currentLocation = carMovingDistance.initMovingDistance(); } public String getName() { @@ -20,6 +20,6 @@ public int getCurrentLocation() { } public void move(int movingDistance) { - this.currentLocation = carLocationManager.updateLocation(this.currentLocation, movingDistance); + this.currentLocation = this.carMovingDistance.updateMovingDistance(this.currentLocation, movingDistance); } } diff --git a/src/main/java/study/race/model/CarListGenerator.java b/src/main/java/study/race/model/CarListGenerator.java deleted file mode 100644 index c67f7da02b3..00000000000 --- a/src/main/java/study/race/model/CarListGenerator.java +++ /dev/null @@ -1,14 +0,0 @@ -package study.race.model; - -import study.race.model.Cars; - -public class CarListGenerator { - - public Cars generateCarList(String[] carNameList) { - Cars cars = new Cars(); - for (int i = 0; i < carNameList.length; i++) { - cars.addCar(carNameList[i]); - } - return cars; - } -} diff --git a/src/main/java/study/race/model/CarLocationManager.java b/src/main/java/study/race/model/CarMovingDistance.java similarity index 84% rename from src/main/java/study/race/model/CarLocationManager.java rename to src/main/java/study/race/model/CarMovingDistance.java index 104722e7792..b7f794070e5 100644 --- a/src/main/java/study/race/model/CarLocationManager.java +++ b/src/main/java/study/race/model/CarMovingDistance.java @@ -1,6 +1,6 @@ package study.race.model; -public class CarLocationManager { +public class CarMovingDistance { private final int STARTING_POINT = 0; private final int MOVE = 1; @@ -10,7 +10,7 @@ public int initMovingDistance() { return STARTING_POINT; } - public int updateLocation(int currentDistance, int movingDistance) { + public int updateMovingDistance(int currentDistance, int movingDistance) { if (checkForwardCondition(movingDistance) || currentDistance == STARTING_POINT) { return currentDistance + MOVE; } diff --git a/src/main/java/study/race/model/CarRacingInputManager.java b/src/main/java/study/race/model/CarRacingInputManager.java deleted file mode 100644 index 30e2703af92..00000000000 --- a/src/main/java/study/race/model/CarRacingInputManager.java +++ /dev/null @@ -1,58 +0,0 @@ -package study.race.model; - -import study.race.view.ReceiveInputValue; - -public class CarRacingInputManager { - - private final int LENGTH_LIMIT = 5; - private final int IS_EXCEED = 1; - private final int NOT_EXCEED = 0; - private String[] carNames; - private ReceiveInputValue receiveInputValue; - private RaceResister raceInfo; - - public CarRacingInputManager() { - this.receiveInputValue = new ReceiveInputValue(); - this.raceInfo = new RaceResister(); - this.getCarNames(); - this.getRoundOfTrack(); - } - - private void getCarNames() { - String[] names; - do { - String namesOfCar = receiveInputValue.receiveInputValueForCarNames(); - names = namesOfCar.split(","); - } while (hasOverLengthForCarNames(names)); - this.raceInfo.setCarNames(names); - } - - private boolean hasOverLengthForCarNames(String[] names) { - return countNumberOfNamesForOverLength(names) > 0; - } - - private int countNumberOfNamesForOverLength(String[] names) { - int exceedCount = 0; - for (String name: names) { - exceedCount += isExceeded(name); - } - return exceedCount; - } - - private int isExceeded(String name) { - if (name.length() > LENGTH_LIMIT) { - System.out.println("The length of the car name '" + name + "' exceeds 5."); - return IS_EXCEED; - } - return NOT_EXCEED; - } - - private void getRoundOfTrack() { - this.raceInfo.setRoundOfTrack(receiveInputValue.receiveInputValueForRound()); - } - - public RaceResister getRaceInfo() { - return this.raceInfo; - } - -} diff --git a/src/main/java/study/race/model/RaceResister.java b/src/main/java/study/race/model/RaceResister.java deleted file mode 100644 index d3686bea5ec..00000000000 --- a/src/main/java/study/race/model/RaceResister.java +++ /dev/null @@ -1,23 +0,0 @@ -package study.race.model; - -public class RaceResister { - - private String[] carNames; - private int roundOfTrack; - - public String[] getCarNames() { - return this.carNames; - } - - public int getRoundOfTrack() { - return this.roundOfTrack; - } - - public void setCarNames(String[] names) { - this.carNames = names; - } - - public void setRoundOfTrack(int roundOfTrack) { - this.roundOfTrack = roundOfTrack; - } -} diff --git a/src/main/java/study/race/model/RaceResult.java b/src/main/java/study/race/model/RaceResult.java index a23369c1662..70c55c94d22 100644 --- a/src/main/java/study/race/model/RaceResult.java +++ b/src/main/java/study/race/model/RaceResult.java @@ -3,14 +3,15 @@ import study.race.model.Car; import study.race.model.Cars; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; public class RaceResult { private int numberOfCar; private int maxDistance; - private String[] winners; private Map racingResult; private Cars cars; @@ -18,7 +19,7 @@ public RaceResult(Cars cars) { this.cars = cars; this.numberOfCar = cars.carList().size(); this.maxDistance = 0; - racingResult = new LinkedHashMap<>(); + this.racingResult = new LinkedHashMap<>(); this.updateRaceResult(); } @@ -33,5 +34,34 @@ public Map getResult() { return this.racingResult; } + public Winners findWinners() { + int maxDistance = findMaxDistance(); + Winners winners = findWinners(maxDistance); + return winners; + } + private int findMaxDistance() { + int maxDistance = 0; + for (Map.Entry result : this.racingResult.entrySet()) { + maxDistance = getMaxDistance(maxDistance, result.getValue()); + } + return maxDistance; + } + + private int getMaxDistance(int maxDistance, int distance) { + if (maxDistance < distance) { + return distance; + } + return maxDistance; + } + + private Winners findWinners(int maxDistance) { + Winners winners = new Winners(maxDistance); + for (Map.Entry result : this.racingResult.entrySet()) { + String carName = result.getKey(); + int distance = result.getValue(); + winners.addWinners(carName, distance); + } + return winners; + } } diff --git a/src/main/java/study/race/model/RunTrack.java b/src/main/java/study/race/model/Racing.java similarity index 75% rename from src/main/java/study/race/model/RunTrack.java rename to src/main/java/study/race/model/Racing.java index e4ebe24ef66..42f6e01b423 100644 --- a/src/main/java/study/race/model/RunTrack.java +++ b/src/main/java/study/race/model/Racing.java @@ -2,17 +2,16 @@ import java.util.Random; -public class RunTrack { +public class Racing { private final int RANDOM_LIMIT_NUMBER = 10; private Random random; private Cars cars; private RaceResult raceResult; - - public RunTrack(String[] namesOfCar) { + public Racing(Cars cars) { this.random = new Random(); - this.cars = new CarListGenerator().generateCarList(namesOfCar); + this.cars = cars; this.raceResult = new RaceResult(this.cars); } @@ -27,4 +26,9 @@ public void start() { public RaceResult getRaceResult() { return this.raceResult; } + + public String getWinners() { + Winners winners = this.raceResult.findWinners(); + return winners.getString(); + } } diff --git a/src/main/java/study/race/model/RacingRegister.java b/src/main/java/study/race/model/RacingRegister.java new file mode 100644 index 00000000000..2bcda4cac25 --- /dev/null +++ b/src/main/java/study/race/model/RacingRegister.java @@ -0,0 +1,36 @@ +package study.race.model; + +import study.race.view.ReceiveInputValue; + +public class RacingRegister { + + private final int CAR_NAME_LENGTH_LIMIT = 5; + + public Cars register(String carName) { + String[] carNameList = carName.split(","); + carNameList = checkCarNameValid(carNameList); + return this.carRegister(carNameList); + } + + private String[] checkCarNameValid(String[] names) { + for (int i = 0; i < names.length; i++) { + names[i] = getValidCarName(names[i]); + } + return names; + } + + private String getValidCarName(String name) { + if (name.length() > CAR_NAME_LENGTH_LIMIT) { + return name.substring(0, 5); + } + return name; + } + + private Cars carRegister(String[] carNameList) { + Cars cars = new Cars(); + for (int i = 0; i < carNameList.length; i++) { + cars.addCar(carNameList[i]); + } + return cars; + } +} diff --git a/src/main/java/study/race/model/Victory.java b/src/main/java/study/race/model/Victory.java deleted file mode 100644 index 333ca32d467..00000000000 --- a/src/main/java/study/race/model/Victory.java +++ /dev/null @@ -1,39 +0,0 @@ -package study.race.model; - -public class Victory { - - private RaceResult raceResult; - - public Victory(RaceResult raceResult) { - this.raceResult = raceResult; - } - - public void findWinner() { - findMaxDistance(); - - } - - private void findMaxDistance() { - this.raceResult.getResult(); - //맵을 순회하면서 맥스를 찾음. - - - for (int i = 0; i < this.numberOfCar; i++) { - Car car = cars.carList().get(i); - int distance = car.getCurrentLocation(); - compareDistance(distance); - } - } - private void compareDistance(int distance) { - if (this.maxDistance < distance) { - this.maxDistance = distance; - } - } - - private void checkWinner() { - } - - - - -} diff --git a/src/main/java/study/race/model/Winners.java b/src/main/java/study/race/model/Winners.java new file mode 100644 index 00000000000..11363b3ce87 --- /dev/null +++ b/src/main/java/study/race/model/Winners.java @@ -0,0 +1,29 @@ +package study.race.model; + +import java.util.ArrayList; +import java.util.List; + +public class Winners { + + private int maxDistance; + private List winnerList; + + public Winners(int maxDistance) { + this.maxDistance = maxDistance; + this.winnerList = new ArrayList<>(); + } + + public void addWinners(String name, int distance) { + if (distance >= maxDistance) { + this.winnerList.add(name); + } + } + + public String getString() { + String winnerToString = ""; + for (String winner: this.winnerList) { + winnerToString = winnerToString + winner + ", "; + } + return winnerToString.substring(0, winnerToString.length() - 2); + } +} diff --git a/src/main/java/study/race/view/ReceiveInputValue.java b/src/main/java/study/race/view/ReceiveInputValue.java index 7342798b58b..f3a6722c960 100644 --- a/src/main/java/study/race/view/ReceiveInputValue.java +++ b/src/main/java/study/race/view/ReceiveInputValue.java @@ -6,7 +6,7 @@ public class ReceiveInputValue { private Scanner scanner; private final String CAR_INT_INPUT_MESSAGE = "Enter the number of cars."; - private final String CAR_STRING_INPUT_MESSAGE = "Enter the names of the cars to race."; + private final String CAR_STRING_INPUT_MESSAGE = "Enter the names of the cars to race(Separation by comma)."; private final String ROUND_INPUT_MESSAGE = "Enter how many rounds are we going to do."; public ReceiveInputValue() { diff --git a/src/main/java/study/race/view/ViewResult.java b/src/main/java/study/race/view/ViewResult.java index d77b7b2278f..54517effeda 100644 --- a/src/main/java/study/race/view/ViewResult.java +++ b/src/main/java/study/race/view/ViewResult.java @@ -7,10 +7,14 @@ public class ViewResult { - public void printRaceResult(RaceResult raceResult) { - for (Map.Entry result: raceResult.getResult().entrySet()) { + public void printRacingResult(RaceResult raceResult) { + for (Map.Entry result : raceResult.getResult().entrySet()) { System.out.println(result.getKey() + " : " + "-".repeat(result.getValue())); } System.out.println(); } + + public void printRacingWinners(String winners) { + System.out.println(winners + " won in the racing."); + } } diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index f0516f15e81..2b707d72013 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -3,7 +3,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; @@ -15,31 +14,28 @@ public class CarRacingTest { - private static final int NUMBER_OF_CAR = 3; private static final String CAR_NAMES = "audi,benz,hd,kia"; - private static String[] carNames; private static Cars stopCars; private static Cars moveCars; @BeforeAll static void init() { - carNames = CAR_NAMES.split(","); - stopCars = new CarListGenerator().generateCarList(carNames); - moveCars = new CarListGenerator().generateCarList(carNames); + stopCars = new RacingRegister().register(CAR_NAMES); + moveCars = new RacingRegister().register(CAR_NAMES); } @Test @DisplayName("Create Cars to Race") - void generateCarListTest() { - Cars cars = new CarListGenerator().generateCarList(carNames); - assertThat(cars.carList().size()).isEqualTo(carNames.length); + void generateCarsTest() { + Cars cars = new RacingRegister().register(CAR_NAMES); + assertThat(cars.carList().size()).isEqualTo(CAR_NAMES.split(",").length); } @ParameterizedTest() @DisplayName("When moving distance is 3 or less, car initial movement Test") @CsvSource({"0, 1", "1, 1", "2, 1", "3, 1"}) void initialMoveCar() { - Cars cars = new CarListGenerator().generateCarList(carNames); + Cars cars = new RacingRegister().register(CAR_NAMES); Car car = cars.carList().get(0); car.move(1); int result = car.getCurrentLocation(); @@ -87,22 +83,19 @@ void updateRaceResult(int movingDistance, int expected) { @DisplayName("Generating car as many as car names") @ValueSource(strings = {"car1,car2", "car1,car2,car3", "car1,car2,car3,car4"}) void generateCarAsManyAsCarNames(String input) { - String[] names = input.split(","); - Cars cars = new CarListGenerator().generateCarList(names); - assertThat(cars.carList().size()).isEqualTo(names.length); + Cars cars = new RacingRegister().register(input); + assertThat(cars.carList().size()).isEqualTo(input.split(",").length); } @ParameterizedTest() @DisplayName("Check for length of car's name greater than 5.") @ValueSource(strings = { "car1,superCar", "car1,car2,car34567", "audi,benz,hyundai,kia"}) void checkLengthOfCarNameTest(String input) { - String[] names = input.split(","); + Cars cars = new RacingRegister().register(input); - new CarRacingInputManager().getRaceInfo(); - - //Test에서 RaceInfo를 받아서 거기에 있는 이름이 다섯자를 안넘는지 체크 - - boolean result = new CarRacing().checkLengthLimitOfCarName(names); - assertThat(result).isEqualTo(false); + for (Car car: cars.carList()) { + String carName = car.getName(); + assertThat(carName.length()).isLessThan(6); + } } } From 1aac7dffb95b743632718dcf7f82abeda88f9e36 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 22 Nov 2023 18:54:35 +0900 Subject: [PATCH 21/26] commit test --- src/main/java/study/race/view/ReceiveInputValue.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/study/race/view/ReceiveInputValue.java b/src/main/java/study/race/view/ReceiveInputValue.java index f3a6722c960..f3f9bf53069 100644 --- a/src/main/java/study/race/view/ReceiveInputValue.java +++ b/src/main/java/study/race/view/ReceiveInputValue.java @@ -5,7 +5,6 @@ public class ReceiveInputValue { private Scanner scanner; - private final String CAR_INT_INPUT_MESSAGE = "Enter the number of cars."; private final String CAR_STRING_INPUT_MESSAGE = "Enter the names of the cars to race(Separation by comma)."; private final String ROUND_INPUT_MESSAGE = "Enter how many rounds are we going to do."; @@ -13,11 +12,6 @@ public ReceiveInputValue() { this.scanner = new Scanner(System.in); } - public int receiveInputValueForCar() { - System.out.println(CAR_INT_INPUT_MESSAGE); - return this.scanner.nextInt(); - } - public String receiveInputValueForCarNames() { System.out.println(CAR_STRING_INPUT_MESSAGE); return this.scanner.nextLine(); From 2b1933c3bd5d316fb13b2e3f752b46e746130a92 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 22 Nov 2023 19:08:41 +0900 Subject: [PATCH 22/26] =?UTF-8?q?fix:=20=EC=9E=98=EB=AA=BB=EB=90=9C=20merg?= =?UTF-8?q?e=EC=97=90=20=EB=8C=80=ED=95=9C=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/Step3.java | 11 ------- .../study/race/model/CarListGenerator.java | 14 --------- .../study/race/model/CarLocationManager.java | 26 ----------------- src/main/java/study/race/model/RunTrack.java | 29 ------------------- src/test/java/study/CarRacingTest.java | 4 ++- 5 files changed, 3 insertions(+), 81 deletions(-) delete mode 100644 src/main/java/study/Step3.java delete mode 100644 src/main/java/study/race/model/CarListGenerator.java delete mode 100644 src/main/java/study/race/model/CarLocationManager.java delete mode 100644 src/main/java/study/race/model/RunTrack.java diff --git a/src/main/java/study/Step3.java b/src/main/java/study/Step3.java deleted file mode 100644 index 31f548025ef..00000000000 --- a/src/main/java/study/Step3.java +++ /dev/null @@ -1,11 +0,0 @@ -package study; - -import study.race.controller.CarRacing; - -public class Step3 { - - public static void main(String[] args) { - new CarRacing().runCarRacing(); - } - -} diff --git a/src/main/java/study/race/model/CarListGenerator.java b/src/main/java/study/race/model/CarListGenerator.java deleted file mode 100644 index 991ee4c3431..00000000000 --- a/src/main/java/study/race/model/CarListGenerator.java +++ /dev/null @@ -1,14 +0,0 @@ -package study.race.model; - -import study.race.model.Cars; - -public class CarListGenerator { - - public Cars generateCarList(int numberOfCar) { - Cars cars = new Cars(); - for (int i = 0; i < numberOfCar; i++) { - cars.addCar(); - } - return cars; - } -} diff --git a/src/main/java/study/race/model/CarLocationManager.java b/src/main/java/study/race/model/CarLocationManager.java deleted file mode 100644 index 104722e7792..00000000000 --- a/src/main/java/study/race/model/CarLocationManager.java +++ /dev/null @@ -1,26 +0,0 @@ -package study.race.model; - -public class CarLocationManager { - - private final int STARTING_POINT = 0; - private final int MOVE = 1; - private final int FORWARD_CONDITION_THRESHOLD = 4; - - public int initMovingDistance() { - return STARTING_POINT; - } - - public int updateLocation(int currentDistance, int movingDistance) { - if (checkForwardCondition(movingDistance) || currentDistance == STARTING_POINT) { - return currentDistance + MOVE; - } - return currentDistance; - } - - private boolean checkForwardCondition(int movingDistance) { - if (movingDistance >= FORWARD_CONDITION_THRESHOLD) { - return true; - } - return false; - } -} diff --git a/src/main/java/study/race/model/RunTrack.java b/src/main/java/study/race/model/RunTrack.java deleted file mode 100644 index 3e36079e937..00000000000 --- a/src/main/java/study/race/model/RunTrack.java +++ /dev/null @@ -1,29 +0,0 @@ -package study.race.model; - -import java.util.Random; - -public class RunTrack { - - private Random random; - private Cars cars; - private RaceResult raceResult; - - - public RunTrack(int numberOfCar) { - this.random = new Random(); - this.cars = new CarListGenerator().generateCarList(numberOfCar); - this.raceResult = new RaceResult(this.cars); - } - - public void start() { - for (int i = 0; i < this.cars.carList().size(); i++) { - int movingDistance = random.nextInt(10); - this.cars.carList().get(i).move(movingDistance); - } - this.raceResult.updateRaceResult(); - } - - public int[] getRaceResult() { - return this.raceResult.getResult(); - } -} diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index b9cfe728d78..2b707d72013 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -68,11 +68,13 @@ void stopCar(int movingDistance, int expected) { void updateRaceResult(int movingDistance, int expected) { RaceResult raceResult = new RaceResult(stopCars); + // 차량 이동 및 결과 업데이트 Car car = stopCars.carList().get(0); String carName = car.getName(); car.move(movingDistance); raceResult.updateRaceResult(); - + + // 결과 확인 int result = raceResult.getResult().get(carName); assertThat(result).isEqualTo(expected); } From a83d263ca8b395404e3f46eeb20be0ca41df35b5 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Wed, 22 Nov 2023 19:56:45 +0900 Subject: [PATCH 23/26] =?UTF-8?q?chore:=20=EC=9E=90=EB=8F=99=EC=B0=A8=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 자동차 이름이 5자가 넘으면 예외가 발생하도록 변경 --- src/main/java/study/race/model/Car.java | 1 + .../java/study/race/model/RacingRegister.java | 25 +++++++++---------- src/test/java/study/CarRacingTest.java | 11 ++++---- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/java/study/race/model/Car.java b/src/main/java/study/race/model/Car.java index 05413d7c80c..845e2f20ba1 100644 --- a/src/main/java/study/race/model/Car.java +++ b/src/main/java/study/race/model/Car.java @@ -15,6 +15,7 @@ public Car(String name) { public String getName() { return this.name; } + public int getCurrentLocation() { return this.currentLocation; } diff --git a/src/main/java/study/race/model/RacingRegister.java b/src/main/java/study/race/model/RacingRegister.java index 2bcda4cac25..fc538dc709f 100644 --- a/src/main/java/study/race/model/RacingRegister.java +++ b/src/main/java/study/race/model/RacingRegister.java @@ -6,30 +6,29 @@ public class RacingRegister { private final int CAR_NAME_LENGTH_LIMIT = 5; + private String[] carNameList; + public Cars register(String carName) { - String[] carNameList = carName.split(","); - carNameList = checkCarNameValid(carNameList); + this.carNameList = carName.split(","); + checkCarNameValid(carNameList); return this.carRegister(carNameList); } - private String[] checkCarNameValid(String[] names) { - for (int i = 0; i < names.length; i++) { - names[i] = getValidCarName(names[i]); + private void checkCarNameValid(String[] names) { + for (String name : names) { + isOverLength(name); } - return names; } - private String getValidCarName(String name) { - if (name.length() > CAR_NAME_LENGTH_LIMIT) { - return name.substring(0, 5); - } - return name; + private void isOverLength(String name) { + if (name.length() > CAR_NAME_LENGTH_LIMIT) + throw new IllegalArgumentException("String length exceeds the allowed maximum(5)."); } private Cars carRegister(String[] carNameList) { Cars cars = new Cars(); - for (int i = 0; i < carNameList.length; i++) { - cars.addCar(carNameList[i]); + for (String carName : carNameList) { + cars.addCar(carName); } return cars; } diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 2b707d72013..5823a97a6a2 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -10,6 +10,8 @@ import study.race.model.*; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static study.Step2.calculateString; public class CarRacingTest { @@ -91,11 +93,8 @@ void generateCarAsManyAsCarNames(String input) { @DisplayName("Check for length of car's name greater than 5.") @ValueSource(strings = { "car1,superCar", "car1,car2,car34567", "audi,benz,hyundai,kia"}) void checkLengthOfCarNameTest(String input) { - Cars cars = new RacingRegister().register(input); - - for (Car car: cars.carList()) { - String carName = car.getName(); - assertThat(carName.length()).isLessThan(6); - } + assertThrows(IllegalArgumentException.class, () -> { + Cars cars = new RacingRegister().register(input); + }); } } From 1b388ca0221586d21193530a375500467d8b7d79 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Sat, 25 Nov 2023 19:59:18 +0900 Subject: [PATCH 24/26] =?UTF-8?q?refactoring:=20RaceResult=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RaceResult 클래스에서 하던 일들을 Cars와 Winners 클래스에서 하도록 변경 후에 RaceResult 클래스 삭제 --- .../java/study/race/controller/CarRacing.java | 2 +- .../study/race/model/CarMovingDistance.java | 7 +- src/main/java/study/race/model/Cars.java | 46 +++++++++++-- .../java/study/race/model/RaceResult.java | 67 ------------------- src/main/java/study/race/model/Racing.java | 19 ++---- .../java/study/race/model/RacingRegister.java | 2 +- src/main/java/study/race/model/Winners.java | 24 ++++--- src/main/java/study/race/view/ViewResult.java | 15 ++--- src/test/java/study/CarRacingTest.java | 52 +++++--------- 9 files changed, 92 insertions(+), 142 deletions(-) delete mode 100644 src/main/java/study/race/model/RaceResult.java diff --git a/src/main/java/study/race/controller/CarRacing.java b/src/main/java/study/race/controller/CarRacing.java index d34d4fe90d2..2b8537daf8e 100644 --- a/src/main/java/study/race/controller/CarRacing.java +++ b/src/main/java/study/race/controller/CarRacing.java @@ -30,6 +30,6 @@ public void runCarRacing() { racing.start(); this.viewResult.printRacingResult(racing.getRaceResult()); } - this.viewResult.printRacingWinners(racing.getWinners()); + this.viewResult.printRacingWinners(cars.getWinners()); } } diff --git a/src/main/java/study/race/model/CarMovingDistance.java b/src/main/java/study/race/model/CarMovingDistance.java index b7f794070e5..1f78c9de547 100644 --- a/src/main/java/study/race/model/CarMovingDistance.java +++ b/src/main/java/study/race/model/CarMovingDistance.java @@ -11,16 +11,13 @@ public int initMovingDistance() { } public int updateMovingDistance(int currentDistance, int movingDistance) { - if (checkForwardCondition(movingDistance) || currentDistance == STARTING_POINT) { + if (checkForwardCondition(movingDistance)) { return currentDistance + MOVE; } return currentDistance; } private boolean checkForwardCondition(int movingDistance) { - if (movingDistance >= FORWARD_CONDITION_THRESHOLD) { - return true; - } - return false; + return movingDistance >= FORWARD_CONDITION_THRESHOLD; } } diff --git a/src/main/java/study/race/model/Cars.java b/src/main/java/study/race/model/Cars.java index 5b1cc20804d..50041826520 100644 --- a/src/main/java/study/race/model/Cars.java +++ b/src/main/java/study/race/model/Cars.java @@ -7,17 +7,53 @@ public class Cars { - private List carList; + private final List carList; public Cars() { this.carList = new ArrayList(); } - public List carList() { - return this.carList; - } - public void addCar(String name) { this.carList.add(new Car(name)); } + + public void moveCarAt(int i, int movingDistance) { + this.carList.get(i).move(movingDistance); + } + + public int size() { + return this.carList.size(); + } + + public String getCarNameAt(int i) { + return this.carList.get(i).getName(); + } + + public Car get(int i) { + return this.carList.get(i); + } + + public int getCarDistanceAt(int i) { + return this.carList.get(i).getCurrentLocation(); + } + + public Winners getWinners() { + return findWinners(findMaxDistance()); + } + + private int findMaxDistance() { + int maxDistance = 0; + for (Car car : carList) { + maxDistance = Math.max(maxDistance, car.getCurrentLocation()); + } + return maxDistance; + } + + private Winners findWinners(int maxDistance) { + Winners winners = new Winners(maxDistance); + for (Car car : carList) { + winners.checkAndAddWinner(car.getName(), car.getCurrentLocation()); + } + return winners; + } } diff --git a/src/main/java/study/race/model/RaceResult.java b/src/main/java/study/race/model/RaceResult.java deleted file mode 100644 index 70c55c94d22..00000000000 --- a/src/main/java/study/race/model/RaceResult.java +++ /dev/null @@ -1,67 +0,0 @@ -package study.race.model; - -import study.race.model.Car; -import study.race.model.Cars; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -public class RaceResult { - - private int numberOfCar; - private int maxDistance; - private Map racingResult; - private Cars cars; - - public RaceResult(Cars cars) { - this.cars = cars; - this.numberOfCar = cars.carList().size(); - this.maxDistance = 0; - this.racingResult = new LinkedHashMap<>(); - this.updateRaceResult(); - } - - public void updateRaceResult() { - for (int i = 0; i < this.numberOfCar; i++) { - Car car = cars.carList().get(i); - racingResult.put(car.getName(), car.getCurrentLocation()); - } - } - - public Map getResult() { - return this.racingResult; - } - - public Winners findWinners() { - int maxDistance = findMaxDistance(); - Winners winners = findWinners(maxDistance); - return winners; - } - - private int findMaxDistance() { - int maxDistance = 0; - for (Map.Entry result : this.racingResult.entrySet()) { - maxDistance = getMaxDistance(maxDistance, result.getValue()); - } - return maxDistance; - } - - private int getMaxDistance(int maxDistance, int distance) { - if (maxDistance < distance) { - return distance; - } - return maxDistance; - } - - private Winners findWinners(int maxDistance) { - Winners winners = new Winners(maxDistance); - for (Map.Entry result : this.racingResult.entrySet()) { - String carName = result.getKey(); - int distance = result.getValue(); - winners.addWinners(carName, distance); - } - return winners; - } -} diff --git a/src/main/java/study/race/model/Racing.java b/src/main/java/study/race/model/Racing.java index 42f6e01b423..87a820d6dca 100644 --- a/src/main/java/study/race/model/Racing.java +++ b/src/main/java/study/race/model/Racing.java @@ -4,31 +4,24 @@ public class Racing { - private final int RANDOM_LIMIT_NUMBER = 10; + private static final int RANDOM_LIMIT_NUMBER = 10; + private Random random; private Cars cars; - private RaceResult raceResult; public Racing(Cars cars) { this.random = new Random(); this.cars = cars; - this.raceResult = new RaceResult(this.cars); } public void start() { - for (int i = 0; i < this.cars.carList().size(); i++) { + for (int i = 0; i < this.cars.size(); i++) { int movingDistance = random.nextInt(RANDOM_LIMIT_NUMBER); - this.cars.carList().get(i).move(movingDistance); + this.cars.moveCarAt(i, movingDistance); } - this.raceResult.updateRaceResult(); - } - - public RaceResult getRaceResult() { - return this.raceResult; } - public String getWinners() { - Winners winners = this.raceResult.findWinners(); - return winners.getString(); + public Cars getRaceResult() { + return this.cars; } } diff --git a/src/main/java/study/race/model/RacingRegister.java b/src/main/java/study/race/model/RacingRegister.java index fc538dc709f..34881e889d2 100644 --- a/src/main/java/study/race/model/RacingRegister.java +++ b/src/main/java/study/race/model/RacingRegister.java @@ -11,7 +11,7 @@ public class RacingRegister { public Cars register(String carName) { this.carNameList = carName.split(","); checkCarNameValid(carNameList); - return this.carRegister(carNameList); + return carRegister(carNameList); } private void checkCarNameValid(String[] names) { diff --git a/src/main/java/study/race/model/Winners.java b/src/main/java/study/race/model/Winners.java index 11363b3ce87..1948c71a518 100644 --- a/src/main/java/study/race/model/Winners.java +++ b/src/main/java/study/race/model/Winners.java @@ -13,17 +13,25 @@ public Winners(int maxDistance) { this.winnerList = new ArrayList<>(); } - public void addWinners(String name, int distance) { - if (distance >= maxDistance) { - this.winnerList.add(name); + public void checkAndAddWinner(String name, int distance) { + if (isWinner(distance)) { + addToWinnerList(name); } } - public String getString() { - String winnerToString = ""; - for (String winner: this.winnerList) { - winnerToString = winnerToString + winner + ", "; + private boolean isWinner(int distance) { + return distance >= maxDistance; + } + + private void addToWinnerList(String name) { + this.winnerList.add(name); + } + + public String getWinnersList() { + String winners = ""; + for (String winner : this.winnerList) { + winners = winners + winner + ", "; } - return winnerToString.substring(0, winnerToString.length() - 2); + return winners.substring(0, winners.length() - 2); } } diff --git a/src/main/java/study/race/view/ViewResult.java b/src/main/java/study/race/view/ViewResult.java index 54517effeda..55cb143b015 100644 --- a/src/main/java/study/race/view/ViewResult.java +++ b/src/main/java/study/race/view/ViewResult.java @@ -1,20 +1,19 @@ package study.race.view; -import study.race.model.RaceResult; +import study.race.model.Cars; +import study.race.model.Winners; -import java.util.List; -import java.util.Map; public class ViewResult { - public void printRacingResult(RaceResult raceResult) { - for (Map.Entry result : raceResult.getResult().entrySet()) { - System.out.println(result.getKey() + " : " + "-".repeat(result.getValue())); + public void printRacingResult(Cars cars) { + for (int i = 0; i < cars.size(); i++) { + System.out.println(cars.getCarNameAt(i) + " : " + "-".repeat(cars.getCarDistanceAt(i))); } System.out.println(); } - public void printRacingWinners(String winners) { - System.out.println(winners + " won in the racing."); + public void printRacingWinners(Winners winners) { + System.out.println(winners.getWinnersList() + " won in the racing."); } } diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 5823a97a6a2..56875ac47c9 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -30,25 +30,14 @@ static void init() { @DisplayName("Create Cars to Race") void generateCarsTest() { Cars cars = new RacingRegister().register(CAR_NAMES); - assertThat(cars.carList().size()).isEqualTo(CAR_NAMES.split(",").length); - } - - @ParameterizedTest() - @DisplayName("When moving distance is 3 or less, car initial movement Test") - @CsvSource({"0, 1", "1, 1", "2, 1", "3, 1"}) - void initialMoveCar() { - Cars cars = new RacingRegister().register(CAR_NAMES); - Car car = cars.carList().get(0); - car.move(1); - int result = car.getCurrentLocation(); - assertThat(result).isEqualTo(1); + assertThat(cars.size()).isEqualTo(CAR_NAMES.split(",").length); } @ParameterizedTest(name = "moving distance: {0}, current location: {1}") @DisplayName("When moving distance is 4 or more, car moving Test") @CsvSource({"4, 1", "5, 2", "6, 3", "7, 4", "8, 5", "9, 6"}) void moveCar(int movingDistance, int expected) { - Car car = moveCars.carList().get(0); + Car car = moveCars.get(0); car.move(movingDistance); int result = car.getCurrentLocation(); assertThat(result).isEqualTo(expected); @@ -56,37 +45,20 @@ void moveCar(int movingDistance, int expected) { @ParameterizedTest(name = "moving distance: {0}, current location: {1}") @DisplayName("When moving distance is 3 or less, car moving Test") - @CsvSource({"0, 1", "1, 1", "2, 1", "3, 1"}) + @CsvSource({"0, 0", "1, 0", "2, 0", "3, 0"}) void stopCar(int movingDistance, int expected) { - Car car = stopCars.carList().get(0); + Car car = stopCars.get(0); car.move(movingDistance); int result = car.getCurrentLocation(); assertThat(result).isEqualTo(expected); } - @ParameterizedTest() - @DisplayName("Result of racing update Test") - @CsvSource({"3, 1", "5, 2", "7, 3", "2, 3"}) - void updateRaceResult(int movingDistance, int expected) { - RaceResult raceResult = new RaceResult(stopCars); - - // 차량 이동 및 결과 업데이트 - Car car = stopCars.carList().get(0); - String carName = car.getName(); - car.move(movingDistance); - raceResult.updateRaceResult(); - - // 결과 확인 - int result = raceResult.getResult().get(carName); - assertThat(result).isEqualTo(expected); - } - @ParameterizedTest() @DisplayName("Generating car as many as car names") @ValueSource(strings = {"car1,car2", "car1,car2,car3", "car1,car2,car3,car4"}) void generateCarAsManyAsCarNames(String input) { Cars cars = new RacingRegister().register(input); - assertThat(cars.carList().size()).isEqualTo(input.split(",").length); + assertThat(cars.size()).isEqualTo(input.split(",").length); } @ParameterizedTest() @@ -94,7 +66,19 @@ void generateCarAsManyAsCarNames(String input) { @ValueSource(strings = { "car1,superCar", "car1,car2,car34567", "audi,benz,hyundai,kia"}) void checkLengthOfCarNameTest(String input) { assertThrows(IllegalArgumentException.class, () -> { - Cars cars = new RacingRegister().register(input); + new RacingRegister().register(input); }); } + + @Test + @DisplayName("Winner's Search Test") + void checkWinners() { + int maxDistance = 5; + String name = "Car1"; + int distance = 5; + + Winners winners = new Winners(maxDistance); + winners.checkAndAddWinner(name, distance); + assertThat(winners.getWinnersList()).isEqualTo("Car1"); + } } From 524dea602225d0de36e737bd4ba9c494c978b1ca Mon Sep 17 00:00:00 2001 From: sudo-init Date: Mon, 27 Nov 2023 20:16:15 +0900 Subject: [PATCH 25/26] =?UTF-8?q?refactor=20and=20test:=20Name=EA=B3=BC=20?= =?UTF-8?q?Location=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 자동차의 이름과 위치에 관한 클래스 Name, Location를 생성. RacingRegister에서 하던 이름에 대한 검사 기능을 Name으로 이전. 자동차 이동에 관한 carMovingDistance를 삭제. CarMovingDistance에 있던 기능들을 Location 클래스로 이전하면서 추상화. 자동차 이동에 대한 Test 삭제. --- src/main/java/study/race/model/Car.java | 19 +++++------ .../study/race/model/CarMovingDistance.java | 23 ------------- src/main/java/study/race/model/Cars.java | 10 +++--- src/main/java/study/race/model/Location.java | 33 +++++++++++++++++++ src/main/java/study/race/model/Name.java | 22 +++++++++++++ src/main/java/study/race/model/Racing.java | 5 +-- .../java/study/race/model/RacingRegister.java | 19 +---------- src/test/java/study/CarRacingTest.java | 20 ----------- 8 files changed, 71 insertions(+), 80 deletions(-) delete mode 100644 src/main/java/study/race/model/CarMovingDistance.java create mode 100644 src/main/java/study/race/model/Location.java create mode 100644 src/main/java/study/race/model/Name.java diff --git a/src/main/java/study/race/model/Car.java b/src/main/java/study/race/model/Car.java index 845e2f20ba1..82099be5105 100644 --- a/src/main/java/study/race/model/Car.java +++ b/src/main/java/study/race/model/Car.java @@ -2,25 +2,24 @@ public class Car { - private String name; private int currentLocation; - private CarMovingDistance carMovingDistance; + private Name name; + private Location location; public Car(String name) { - this.name = name; - this.carMovingDistance = new CarMovingDistance(); - this.currentLocation = carMovingDistance.initMovingDistance(); + this.name = new Name(name); + this.location = new Location(); } public String getName() { - return this.name; + return this.name.getName(); } - public int getCurrentLocation() { - return this.currentLocation; + public int getLocation() { + return this.location.getLocation(); } - public void move(int movingDistance) { - this.currentLocation = this.carMovingDistance.updateMovingDistance(this.currentLocation, movingDistance); + public void move() { + this.location.updateMovingDistance(); } } diff --git a/src/main/java/study/race/model/CarMovingDistance.java b/src/main/java/study/race/model/CarMovingDistance.java deleted file mode 100644 index 1f78c9de547..00000000000 --- a/src/main/java/study/race/model/CarMovingDistance.java +++ /dev/null @@ -1,23 +0,0 @@ -package study.race.model; - -public class CarMovingDistance { - - private final int STARTING_POINT = 0; - private final int MOVE = 1; - private final int FORWARD_CONDITION_THRESHOLD = 4; - - public int initMovingDistance() { - return STARTING_POINT; - } - - public int updateMovingDistance(int currentDistance, int movingDistance) { - if (checkForwardCondition(movingDistance)) { - return currentDistance + MOVE; - } - return currentDistance; - } - - private boolean checkForwardCondition(int movingDistance) { - return movingDistance >= FORWARD_CONDITION_THRESHOLD; - } -} diff --git a/src/main/java/study/race/model/Cars.java b/src/main/java/study/race/model/Cars.java index 50041826520..b9a1a4ccc18 100644 --- a/src/main/java/study/race/model/Cars.java +++ b/src/main/java/study/race/model/Cars.java @@ -17,8 +17,8 @@ public void addCar(String name) { this.carList.add(new Car(name)); } - public void moveCarAt(int i, int movingDistance) { - this.carList.get(i).move(movingDistance); + public void moveCarAt(int i) { + this.carList.get(i).move(); } public int size() { @@ -34,7 +34,7 @@ public Car get(int i) { } public int getCarDistanceAt(int i) { - return this.carList.get(i).getCurrentLocation(); + return this.carList.get(i).getLocation(); } public Winners getWinners() { @@ -44,7 +44,7 @@ public Winners getWinners() { private int findMaxDistance() { int maxDistance = 0; for (Car car : carList) { - maxDistance = Math.max(maxDistance, car.getCurrentLocation()); + maxDistance = Math.max(maxDistance, car.getLocation()); } return maxDistance; } @@ -52,7 +52,7 @@ private int findMaxDistance() { private Winners findWinners(int maxDistance) { Winners winners = new Winners(maxDistance); for (Car car : carList) { - winners.checkAndAddWinner(car.getName(), car.getCurrentLocation()); + winners.checkAndAddWinner(car.getName(), car.getLocation()); } return winners; } diff --git a/src/main/java/study/race/model/Location.java b/src/main/java/study/race/model/Location.java new file mode 100644 index 00000000000..0fafad6e34b --- /dev/null +++ b/src/main/java/study/race/model/Location.java @@ -0,0 +1,33 @@ +package study.race.model; + +import java.util.Random; + +public class Location { + + private static final int STARTING_POINT = 0; + private static final int FORWARD = 1; + private static final int FORWARD_CONDITION_THRESHOLD = 4; + private static final int RANDOM_LIMIT_NUMBER = 10; + + private int location; + private Random random; + + public Location() { + this.location = STARTING_POINT; + random = new Random(); + } + + public void updateMovingDistance() { + if (checkForwardCondition()) { + this.location += FORWARD; + } + } + + private boolean checkForwardCondition() { + return random.nextInt(RANDOM_LIMIT_NUMBER) >= FORWARD_CONDITION_THRESHOLD; + } + + public int getLocation() { + return this.location; + } +} diff --git a/src/main/java/study/race/model/Name.java b/src/main/java/study/race/model/Name.java new file mode 100644 index 00000000000..91e4c09d066 --- /dev/null +++ b/src/main/java/study/race/model/Name.java @@ -0,0 +1,22 @@ +package study.race.model; + +public class Name { + + private final int CAR_NAME_LENGTH_LIMIT = 5; + + private String name; + + public Name(String name) { + checkOverLength(name); + this.name = name; + } + + private void checkOverLength(String name) { + if (name.length() > CAR_NAME_LENGTH_LIMIT) + throw new IllegalArgumentException("String length exceeds the allowed maximum(5)."); + } + + public String getName() { + return this.name; + } +} diff --git a/src/main/java/study/race/model/Racing.java b/src/main/java/study/race/model/Racing.java index 87a820d6dca..692832d33b2 100644 --- a/src/main/java/study/race/model/Racing.java +++ b/src/main/java/study/race/model/Racing.java @@ -4,8 +4,6 @@ public class Racing { - private static final int RANDOM_LIMIT_NUMBER = 10; - private Random random; private Cars cars; @@ -16,8 +14,7 @@ public Racing(Cars cars) { public void start() { for (int i = 0; i < this.cars.size(); i++) { - int movingDistance = random.nextInt(RANDOM_LIMIT_NUMBER); - this.cars.moveCarAt(i, movingDistance); + this.cars.moveCarAt(i); } } diff --git a/src/main/java/study/race/model/RacingRegister.java b/src/main/java/study/race/model/RacingRegister.java index 34881e889d2..183bdff822f 100644 --- a/src/main/java/study/race/model/RacingRegister.java +++ b/src/main/java/study/race/model/RacingRegister.java @@ -4,25 +4,8 @@ public class RacingRegister { - private final int CAR_NAME_LENGTH_LIMIT = 5; - - private String[] carNameList; - public Cars register(String carName) { - this.carNameList = carName.split(","); - checkCarNameValid(carNameList); - return carRegister(carNameList); - } - - private void checkCarNameValid(String[] names) { - for (String name : names) { - isOverLength(name); - } - } - - private void isOverLength(String name) { - if (name.length() > CAR_NAME_LENGTH_LIMIT) - throw new IllegalArgumentException("String length exceeds the allowed maximum(5)."); + return carRegister(carName.split(",")); } private Cars carRegister(String[] carNameList) { diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 56875ac47c9..a44a590c1fc 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -33,26 +33,6 @@ void generateCarsTest() { assertThat(cars.size()).isEqualTo(CAR_NAMES.split(",").length); } - @ParameterizedTest(name = "moving distance: {0}, current location: {1}") - @DisplayName("When moving distance is 4 or more, car moving Test") - @CsvSource({"4, 1", "5, 2", "6, 3", "7, 4", "8, 5", "9, 6"}) - void moveCar(int movingDistance, int expected) { - Car car = moveCars.get(0); - car.move(movingDistance); - int result = car.getCurrentLocation(); - assertThat(result).isEqualTo(expected); - } - - @ParameterizedTest(name = "moving distance: {0}, current location: {1}") - @DisplayName("When moving distance is 3 or less, car moving Test") - @CsvSource({"0, 0", "1, 0", "2, 0", "3, 0"}) - void stopCar(int movingDistance, int expected) { - Car car = stopCars.get(0); - car.move(movingDistance); - int result = car.getCurrentLocation(); - assertThat(result).isEqualTo(expected); - } - @ParameterizedTest() @DisplayName("Generating car as many as car names") @ValueSource(strings = {"car1,car2", "car1,car2,car3", "car1,car2,car3,car4"}) From ffd0a88493342e279d267ea667236084784886d0 Mon Sep 17 00:00:00 2001 From: sudo-init Date: Mon, 27 Nov 2023 20:30:16 +0900 Subject: [PATCH 26/26] chore: merge conflict resolved. --- .../study/race/model/CarMovingDistance.java | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 src/main/java/study/race/model/CarMovingDistance.java diff --git a/src/main/java/study/race/model/CarMovingDistance.java b/src/main/java/study/race/model/CarMovingDistance.java deleted file mode 100644 index 1f78c9de547..00000000000 --- a/src/main/java/study/race/model/CarMovingDistance.java +++ /dev/null @@ -1,23 +0,0 @@ -package study.race.model; - -public class CarMovingDistance { - - private final int STARTING_POINT = 0; - private final int MOVE = 1; - private final int FORWARD_CONDITION_THRESHOLD = 4; - - public int initMovingDistance() { - return STARTING_POINT; - } - - public int updateMovingDistance(int currentDistance, int movingDistance) { - if (checkForwardCondition(movingDistance)) { - return currentDistance + MOVE; - } - return currentDistance; - } - - private boolean checkForwardCondition(int movingDistance) { - return movingDistance >= FORWARD_CONDITION_THRESHOLD; - } -}