From d052c02560e7e10415b78af1cede11a272089cb1 Mon Sep 17 00:00:00 2001 From: sookyshin Date: Tue, 24 Sep 2024 20:01:23 +0900 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 47 +++++++++++++++++++++++++++++ src/test/java/study/StringTest.java | 38 +++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/test/java/study/SetTest.java create mode 100644 src/test/java/study/StringTest.java diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java new file mode 100644 index 00000000000..8f9f7359458 --- /dev/null +++ b/src/test/java/study/SetTest.java @@ -0,0 +1,47 @@ +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; + +public class SetTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + // Test Case 구현 + @Test + @DisplayName("Set의 크기 확인") + void setSizeTest() { + assertThat(numbers.size()).isEqualTo(3); + } + + @DisplayName("Set에 포함된 값 확인") + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + void containsTest(int num) { + assertThat(numbers.contains(num)).isTrue(); + } + + @DisplayName("Set에 포함되지 않은 값 확인") + @ParameterizedTest + @CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':') + void expectedValueTest(int num, boolean expected) { + assertThat(numbers.contains(num)).isEqualTo(expected); + } +} \ No newline at end of file diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java new file mode 100644 index 00000000000..b06229c8f20 --- /dev/null +++ b/src/test/java/study/StringTest.java @@ -0,0 +1,38 @@ +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.assertThatThrownBy; + +public class StringTest { + @Test + @DisplayName("split 테스트") + void splitTest() { + String num = "1,2"; + String[] splitExactlyStr = num.split(","); + assertThat(splitExactlyStr).containsExactly("1","2"); + + String[] splitStr = "1".split(","); + assertThat(splitStr).contains("1"); + } + + @Test + @DisplayName("substring 테스트") + void substringTest() { + String num = "(1,2)"; + String substringStr = num.substring(1, 4); + assertThat(substringStr).isEqualTo("1,2"); + } + + @Test + @DisplayName("charAt 테스트") + void charAtTest() { + String charStr = "abc"; + assertThatThrownBy(() -> { + charStr.charAt(4); + }).isInstanceOf(StringIndexOutOfBoundsException.class) + .hasMessageContaining("String index out of range: 4"); + } +} From 174573b18b42aa35a58bc9bd989b588247f44091 Mon Sep 17 00:00:00 2001 From: sookyshin Date: Wed, 25 Sep 2024 19:58:39 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 10 +++++----- src/test/java/study/StringTest.java | 10 +++++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java index 8f9f7359458..3a0e53b7b15 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/SetTest.java @@ -34,14 +34,14 @@ void setSizeTest() { @DisplayName("Set에 포함된 값 확인") @ParameterizedTest @ValueSource(ints = {1, 2, 3}) - void containsTest(int num) { - assertThat(numbers.contains(num)).isTrue(); + void containsTest(int number) { + assertThat(numbers.contains(number)).isTrue(); } @DisplayName("Set에 포함되지 않은 값 확인") @ParameterizedTest @CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':') - void expectedValueTest(int num, boolean expected) { - assertThat(numbers.contains(num)).isEqualTo(expected); + void expectedValueTest(int number, boolean expected) { + assertThat(numbers.contains(number)).isEqualTo(expected); } -} \ No newline at end of file +} diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index b06229c8f20..3e5e4fa5dc3 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -13,9 +13,6 @@ void splitTest() { String num = "1,2"; String[] splitExactlyStr = num.split(","); assertThat(splitExactlyStr).containsExactly("1","2"); - - String[] splitStr = "1".split(","); - assertThat(splitStr).contains("1"); } @Test @@ -29,6 +26,13 @@ void substringTest() { @Test @DisplayName("charAt 테스트") void charAtTest() { + String charStr = "abc"; + assertThat(charStr.charAt(1)).isEqualTo('b'); + } + + @Test + @DisplayName("charAt Exception 테스트") + void charAtExceptionTest() { String charStr = "abc"; assertThatThrownBy(() -> { charStr.charAt(4); From 615bf67a490d49298d067cecc555a9dc8b750b0d Mon Sep 17 00:00:00 2001 From: sookyshin Date: Fri, 4 Oct 2024 10:34:59 +0900 Subject: [PATCH 3/7] =?UTF-8?q?remove:=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 47 ----------------------------- src/test/java/study/StringTest.java | 42 -------------------------- 2 files changed, 89 deletions(-) delete mode 100644 src/test/java/study/SetTest.java delete mode 100644 src/test/java/study/StringTest.java diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java deleted file mode 100644 index 3a0e53b7b15..00000000000 --- a/src/test/java/study/SetTest.java +++ /dev/null @@ -1,47 +0,0 @@ -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; - -public class SetTest { - private Set numbers; - - @BeforeEach - void setUp() { - numbers = new HashSet<>(); - numbers.add(1); - numbers.add(1); - numbers.add(2); - numbers.add(3); - } - - // Test Case 구현 - @Test - @DisplayName("Set의 크기 확인") - void setSizeTest() { - assertThat(numbers.size()).isEqualTo(3); - } - - @DisplayName("Set에 포함된 값 확인") - @ParameterizedTest - @ValueSource(ints = {1, 2, 3}) - void containsTest(int number) { - assertThat(numbers.contains(number)).isTrue(); - } - - @DisplayName("Set에 포함되지 않은 값 확인") - @ParameterizedTest - @CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':') - void expectedValueTest(int number, boolean expected) { - assertThat(numbers.contains(number)).isEqualTo(expected); - } -} diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java deleted file mode 100644 index 3e5e4fa5dc3..00000000000 --- a/src/test/java/study/StringTest.java +++ /dev/null @@ -1,42 +0,0 @@ -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.assertThatThrownBy; - -public class StringTest { - @Test - @DisplayName("split 테스트") - void splitTest() { - String num = "1,2"; - String[] splitExactlyStr = num.split(","); - assertThat(splitExactlyStr).containsExactly("1","2"); - } - - @Test - @DisplayName("substring 테스트") - void substringTest() { - String num = "(1,2)"; - String substringStr = num.substring(1, 4); - assertThat(substringStr).isEqualTo("1,2"); - } - - @Test - @DisplayName("charAt 테스트") - void charAtTest() { - String charStr = "abc"; - assertThat(charStr.charAt(1)).isEqualTo('b'); - } - - @Test - @DisplayName("charAt Exception 테스트") - void charAtExceptionTest() { - String charStr = "abc"; - assertThatThrownBy(() -> { - charStr.charAt(4); - }).isInstanceOf(StringIndexOutOfBoundsException.class) - .hasMessageContaining("String index out of range: 4"); - } -} From 6186238e5e95964a88d881ee6fff1df637ab1153 Mon Sep 17 00:00:00 2001 From: sookyshin Date: Tue, 8 Oct 2024 19:29:02 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 +++ src/main/java/racing/RacingApplication.java | 11 +++++++ .../controller/CarRacingController.java | 26 +++++++++++++++ src/main/java/racing/entity/Car.java | 32 +++++++++++++++++++ .../java/racing/service/CarRacingService.java | 24 ++++++++++++++ src/main/java/racing/view/InputView.java | 27 ++++++++++++++++ src/main/java/racing/view/ResultView.java | 16 ++++++++++ .../java/racing/CarRacingServiceTest.java | 25 +++++++++++++++ 8 files changed, 165 insertions(+) create mode 100644 src/main/java/racing/RacingApplication.java create mode 100644 src/main/java/racing/controller/CarRacingController.java create mode 100644 src/main/java/racing/entity/Car.java create mode 100644 src/main/java/racing/service/CarRacingService.java create mode 100644 src/main/java/racing/view/InputView.java create mode 100644 src/main/java/racing/view/ResultView.java create mode 100644 src/test/java/racing/CarRacingServiceTest.java diff --git a/build.gradle b/build.gradle index 6282254c4ce..bcb72d51b6d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,6 @@ plugins { + id 'org.springframework.boot' version '2.5.4' + id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } @@ -11,6 +13,8 @@ repositories { dependencies { testImplementation 'org.assertj:assertj-core:3.22.0' testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2' + implementation 'org.springframework.boot:spring-boot-starter' + testImplementation 'org.springframework.boot:spring-boot-starter-test' } java { diff --git a/src/main/java/racing/RacingApplication.java b/src/main/java/racing/RacingApplication.java new file mode 100644 index 00000000000..87689e200d7 --- /dev/null +++ b/src/main/java/racing/RacingApplication.java @@ -0,0 +1,11 @@ +package racing; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class RacingApplication { + public static void main(String[] args) { + SpringApplication.run(RacingApplication.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/racing/controller/CarRacingController.java b/src/main/java/racing/controller/CarRacingController.java new file mode 100644 index 00000000000..c91e667e47d --- /dev/null +++ b/src/main/java/racing/controller/CarRacingController.java @@ -0,0 +1,26 @@ +package racing.controller; + +import racing.entity.Car; +import racing.service.CarRacingService; +import racing.view.InputView; +import racing.view.ResultView; + +import java.util.List; + +public class CarRacingController { + + public static void main(String[] args) { + InputView inputView = new InputView(); + List cars = inputView.inputCar(); + int rounds = inputView.inputNumber(); + + CarRacingService racingService = new CarRacingService(cars); + ResultView resultView = new ResultView(); + + for (int i = 0; i < rounds; i++) { + racingService.moveCars(); + resultView.printResult(racingService.getCars()); + } + } +} + diff --git a/src/main/java/racing/entity/Car.java b/src/main/java/racing/entity/Car.java new file mode 100644 index 00000000000..275a4f8b7e2 --- /dev/null +++ b/src/main/java/racing/entity/Car.java @@ -0,0 +1,32 @@ +package racing.entity; + +public class Car { + private final String name; + private int position; + + public Car(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void move() { + if (canMove()) { + position += 1; + } + } + + private boolean canMove() { + int randomValue = (int) (Math.random() * 10); + return randomValue >= 4; + } +} + + diff --git a/src/main/java/racing/service/CarRacingService.java b/src/main/java/racing/service/CarRacingService.java new file mode 100644 index 00000000000..521eb2141b4 --- /dev/null +++ b/src/main/java/racing/service/CarRacingService.java @@ -0,0 +1,24 @@ +package racing.service; + +import racing.entity.Car; +import java.util.List; + +public class CarRacingService { + + private List cars; + + public CarRacingService(List cars) { + this.cars = cars; + } + + public void moveCars() { + for (Car car : cars) { + car.move(); + } + } + + public List getCars() { + return cars; + } +} + diff --git a/src/main/java/racing/view/InputView.java b/src/main/java/racing/view/InputView.java new file mode 100644 index 00000000000..48c810faf5c --- /dev/null +++ b/src/main/java/racing/view/InputView.java @@ -0,0 +1,27 @@ +package racing.view; + +import racing.entity.Car; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class InputView { + + Scanner scanner = new Scanner(System.in); + + public List inputCar() { + System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); + String[] names = scanner.nextLine().split(","); + List cars = new ArrayList<>(); + for (String name : names) { + cars.add(new Car(name.trim())); + } + return cars; + } + + public int inputNumber() { + System.out.println("시도할 횟수를 입력하세요 : "); + Scanner scanner = new Scanner(System.in); + return scanner.nextInt(); + } +} diff --git a/src/main/java/racing/view/ResultView.java b/src/main/java/racing/view/ResultView.java new file mode 100644 index 00000000000..2d6a0794185 --- /dev/null +++ b/src/main/java/racing/view/ResultView.java @@ -0,0 +1,16 @@ +package racing.view; + +import racing.entity.Car; + +import java.util.List; + +public class ResultView { + public void printResult(List cars) { + for (Car car : cars) { + System.out.print(car.getName() + ": "); + for (int i = 0; i < car.getPosition(); i++) { + System.out.print("-"); + } + } + } +} diff --git a/src/test/java/racing/CarRacingServiceTest.java b/src/test/java/racing/CarRacingServiceTest.java new file mode 100644 index 00000000000..ebe6f6d4a44 --- /dev/null +++ b/src/test/java/racing/CarRacingServiceTest.java @@ -0,0 +1,25 @@ +package racing; + +import org.junit.jupiter.api.Test; +import racing.entity.Car; +import racing.service.CarRacingService; +import java.util.Arrays; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CarRacingServiceTest { + + @Test + void testMoveCars() { + Car car1 = new Car("Car1"); + Car car2 = new Car("Car2"); + CarRacingService racingService = new CarRacingService(Arrays.asList(car1, car2)); + + int initialPositionCar1 = car1.getPosition(); + int initialPositionCar2 = car2.getPosition(); + + racingService.moveCars(); + + assertEquals(initialPositionCar1 + (car1.getPosition() > initialPositionCar1 ? 1 : 0), car1.getPosition()); + assertEquals(initialPositionCar2 + (car2.getPosition() > initialPositionCar2 ? 1 : 0), car2.getPosition()); + } +} From 386e4587dc5cce4595283e20d871200648436fad Mon Sep 17 00:00:00 2001 From: sweet-info Date: Thu, 10 Oct 2024 00:38:04 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racing/entity/Car.java | 8 ++++++-- src/main/java/racing/service/CarRacingService.java | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/racing/entity/Car.java b/src/main/java/racing/entity/Car.java index 275a4f8b7e2..7a100739018 100644 --- a/src/main/java/racing/entity/Car.java +++ b/src/main/java/racing/entity/Car.java @@ -1,9 +1,13 @@ package racing.entity; public class Car { + private final String name; private int position; + private static final int MOVE_THRESHOLD = 4; + private static final int RANDOM_RANGE = 10; + public Car(String name) { this.name = name; this.position = 0; @@ -24,8 +28,8 @@ public void move() { } private boolean canMove() { - int randomValue = (int) (Math.random() * 10); - return randomValue >= 4; + int randomValue = (int) (Math.random() * RANDOM_RANGE); + return randomValue >= MOVE_THRESHOLD; } } diff --git a/src/main/java/racing/service/CarRacingService.java b/src/main/java/racing/service/CarRacingService.java index 521eb2141b4..7e7a35e92c2 100644 --- a/src/main/java/racing/service/CarRacingService.java +++ b/src/main/java/racing/service/CarRacingService.java @@ -5,7 +5,7 @@ public class CarRacingService { - private List cars; + private final List cars; public CarRacingService(List cars) { this.cars = cars; From 090df7a392a719135868f3ff1c47108f843db42c Mon Sep 17 00:00:00 2001 From: sookyshin Date: Fri, 11 Oct 2024 14:35:48 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 -- src/main/java/racing/RacingApplication.java | 11 ---------- .../controller/CarRacingController.java | 5 +++-- src/main/java/racing/entity/Car.java | 6 +++++- .../{CarRacingService.java => Cars.java} | 4 ++-- src/main/java/racing/view/InputView.java | 4 ++-- src/main/java/racing/view/ResultView.java | 20 ++++++++++++++----- .../java/racing/CarRacingServiceTest.java | 18 +++++++++++++---- 8 files changed, 41 insertions(+), 29 deletions(-) delete mode 100644 src/main/java/racing/RacingApplication.java rename src/main/java/racing/service/{CarRacingService.java => Cars.java} (79%) diff --git a/build.gradle b/build.gradle index bcb72d51b6d..972bad86a1e 100644 --- a/build.gradle +++ b/build.gradle @@ -13,8 +13,6 @@ repositories { dependencies { testImplementation 'org.assertj:assertj-core:3.22.0' testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2' - implementation 'org.springframework.boot:spring-boot-starter' - testImplementation 'org.springframework.boot:spring-boot-starter-test' } java { diff --git a/src/main/java/racing/RacingApplication.java b/src/main/java/racing/RacingApplication.java deleted file mode 100644 index 87689e200d7..00000000000 --- a/src/main/java/racing/RacingApplication.java +++ /dev/null @@ -1,11 +0,0 @@ -package racing; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class RacingApplication { - public static void main(String[] args) { - SpringApplication.run(RacingApplication.class, args); - } -} \ No newline at end of file diff --git a/src/main/java/racing/controller/CarRacingController.java b/src/main/java/racing/controller/CarRacingController.java index c91e667e47d..830f0d74e09 100644 --- a/src/main/java/racing/controller/CarRacingController.java +++ b/src/main/java/racing/controller/CarRacingController.java @@ -1,7 +1,7 @@ package racing.controller; import racing.entity.Car; -import racing.service.CarRacingService; +import racing.service.Cars; import racing.view.InputView; import racing.view.ResultView; @@ -14,7 +14,7 @@ public static void main(String[] args) { List cars = inputView.inputCar(); int rounds = inputView.inputNumber(); - CarRacingService racingService = new CarRacingService(cars); + Cars racingService = new Cars(cars); ResultView resultView = new ResultView(); for (int i = 0; i < rounds; i++) { @@ -24,3 +24,4 @@ public static void main(String[] args) { } } + diff --git a/src/main/java/racing/entity/Car.java b/src/main/java/racing/entity/Car.java index 7a100739018..87ea4bc4c32 100644 --- a/src/main/java/racing/entity/Car.java +++ b/src/main/java/racing/entity/Car.java @@ -1,9 +1,12 @@ package racing.entity; +import java.util.Random; + public class Car { private final String name; private int position; + private final Random random = new Random(); private static final int MOVE_THRESHOLD = 4; private static final int RANDOM_RANGE = 10; @@ -28,9 +31,10 @@ public void move() { } private boolean canMove() { - int randomValue = (int) (Math.random() * RANDOM_RANGE); + int randomValue = random.nextInt(RANDOM_RANGE); return randomValue >= MOVE_THRESHOLD; } } + diff --git a/src/main/java/racing/service/CarRacingService.java b/src/main/java/racing/service/Cars.java similarity index 79% rename from src/main/java/racing/service/CarRacingService.java rename to src/main/java/racing/service/Cars.java index 7e7a35e92c2..19ad7fc9190 100644 --- a/src/main/java/racing/service/CarRacingService.java +++ b/src/main/java/racing/service/Cars.java @@ -3,11 +3,11 @@ import racing.entity.Car; import java.util.List; -public class CarRacingService { +public class Cars { private final List cars; - public CarRacingService(List cars) { + public Cars(List cars) { this.cars = cars; } diff --git a/src/main/java/racing/view/InputView.java b/src/main/java/racing/view/InputView.java index 48c810faf5c..3ed3cad3e92 100644 --- a/src/main/java/racing/view/InputView.java +++ b/src/main/java/racing/view/InputView.java @@ -7,7 +7,7 @@ public class InputView { - Scanner scanner = new Scanner(System.in); + private final Scanner scanner = new Scanner(System.in); public List inputCar() { System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); @@ -21,7 +21,7 @@ public List inputCar() { public int inputNumber() { System.out.println("시도할 횟수를 입력하세요 : "); - Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } } + diff --git a/src/main/java/racing/view/ResultView.java b/src/main/java/racing/view/ResultView.java index 2d6a0794185..0c44e796e32 100644 --- a/src/main/java/racing/view/ResultView.java +++ b/src/main/java/racing/view/ResultView.java @@ -6,11 +6,21 @@ public class ResultView { public void printResult(List cars) { - for (Car car : cars) { - System.out.print(car.getName() + ": "); - for (int i = 0; i < car.getPosition(); i++) { - System.out.print("-"); - } + StringBuilder result = new StringBuilder(); + cars.forEach(car -> result.append(buildCarResult(car)).append("\n")); + System.out.println(result.toString()); + } + + private String buildCarResult(Car car) { + return car.getName() + ": " + carPosition(car.getPosition()); + } + + private String carPosition(int position) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < position; i++) { + sb.append("-"); } + return sb.toString(); } } + diff --git a/src/test/java/racing/CarRacingServiceTest.java b/src/test/java/racing/CarRacingServiceTest.java index ebe6f6d4a44..cafe178f6e7 100644 --- a/src/test/java/racing/CarRacingServiceTest.java +++ b/src/test/java/racing/CarRacingServiceTest.java @@ -2,7 +2,7 @@ import org.junit.jupiter.api.Test; import racing.entity.Car; -import racing.service.CarRacingService; +import racing.service.Cars; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -12,14 +12,24 @@ public class CarRacingServiceTest { void testMoveCars() { Car car1 = new Car("Car1"); Car car2 = new Car("Car2"); - CarRacingService racingService = new CarRacingService(Arrays.asList(car1, car2)); + Cars racingService = new Cars(Arrays.asList(car1, car2)); int initialPositionCar1 = car1.getPosition(); int initialPositionCar2 = car2.getPosition(); racingService.moveCars(); - assertEquals(initialPositionCar1 + (car1.getPosition() > initialPositionCar1 ? 1 : 0), car1.getPosition()); - assertEquals(initialPositionCar2 + (car2.getPosition() > initialPositionCar2 ? 1 : 0), car2.getPosition()); + assertEquals(calculateExpectedPosition(initialPositionCar1, car1.getPosition()), car1.getPosition()); + assertEquals(calculateExpectedPosition(initialPositionCar2, car2.getPosition()), car2.getPosition()); } + + + private int calculateExpectedPosition(int initialPosition, int currentPosition) { + if (currentPosition > initialPosition) { + return initialPosition + 1; + } + return initialPosition; + } + } + From 72f03e5e584b783eb29e76cd7c2305f66d0ffa0d Mon Sep 17 00:00:00 2001 From: sookyshin Date: Mon, 28 Oct 2024 13:25:27 +0900 Subject: [PATCH 7/7] =?UTF-8?q?feat:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CarRacingController.java | 1 + src/main/java/racing/entity/Car.java | 4 +- src/main/java/racing/service/Cars.java | 12 ++++++ src/main/java/racing/view/ResultView.java | 14 +++++-- src/test/java/racing/CarTest.java | 41 +++++++++++++++++++ ...arRacingServiceTest.java => CarsTest.java} | 2 +- 6 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 src/test/java/racing/CarTest.java rename src/test/java/racing/{CarRacingServiceTest.java => CarsTest.java} (96%) diff --git a/src/main/java/racing/controller/CarRacingController.java b/src/main/java/racing/controller/CarRacingController.java index 830f0d74e09..31d74df7cf0 100644 --- a/src/main/java/racing/controller/CarRacingController.java +++ b/src/main/java/racing/controller/CarRacingController.java @@ -21,6 +21,7 @@ public static void main(String[] args) { racingService.moveCars(); resultView.printResult(racingService.getCars()); } + resultView.findWinners(racingService.findWinners()); } } diff --git a/src/main/java/racing/entity/Car.java b/src/main/java/racing/entity/Car.java index 87ea4bc4c32..df9a924217c 100644 --- a/src/main/java/racing/entity/Car.java +++ b/src/main/java/racing/entity/Car.java @@ -5,7 +5,7 @@ public class Car { private final String name; - private int position; + protected int position; private final Random random = new Random(); private static final int MOVE_THRESHOLD = 4; @@ -30,7 +30,7 @@ public void move() { } } - private boolean canMove() { + protected boolean canMove() { int randomValue = random.nextInt(RANDOM_RANGE); return randomValue >= MOVE_THRESHOLD; } diff --git a/src/main/java/racing/service/Cars.java b/src/main/java/racing/service/Cars.java index 19ad7fc9190..6756a3aaf3c 100644 --- a/src/main/java/racing/service/Cars.java +++ b/src/main/java/racing/service/Cars.java @@ -2,6 +2,7 @@ import racing.entity.Car; import java.util.List; +import java.util.stream.Collectors; public class Cars { @@ -20,5 +21,16 @@ public void moveCars() { public List getCars() { return cars; } + + public List findWinners() { + int maxPosition = cars.stream() + .mapToInt(Car::getPosition) + .max() + .orElse(0); + + return cars.stream() + .filter(car -> car.getPosition() == maxPosition) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/racing/view/ResultView.java b/src/main/java/racing/view/ResultView.java index 0c44e796e32..7dfd1ee3746 100644 --- a/src/main/java/racing/view/ResultView.java +++ b/src/main/java/racing/view/ResultView.java @@ -3,6 +3,7 @@ import racing.entity.Car; import java.util.List; +import java.util.stream.Collectors; public class ResultView { public void printResult(List cars) { @@ -16,11 +17,18 @@ private String buildCarResult(Car car) { } private String carPosition(int position) { - StringBuilder sb = new StringBuilder(); + StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < position; i++) { - sb.append("-"); + stringBuilder.append("-"); } - return sb.toString(); + return stringBuilder.toString(); + } + + public void findWinners(List winners) { + String winnerNames = winners.stream() + .map(Car::getName) + .collect(Collectors.joining(", ")); + System.out.println("우승자는 " + winnerNames + " 입니다."); } } diff --git a/src/test/java/racing/CarTest.java b/src/test/java/racing/CarTest.java new file mode 100644 index 00000000000..35d20eb540e --- /dev/null +++ b/src/test/java/racing/CarTest.java @@ -0,0 +1,41 @@ +package racing; + +import org.junit.jupiter.api.Test; +import racing.entity.Car; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CarTest { + + @Test + void carMoves() { + Car car = new Car("Car"); + car.move(); + assertEquals(1, car.getPosition()); + } + + @Test + void carDoesNotMove() { + Car car = new Car("CarzCar") { + @Override + public void move() { + if (!canMove()) { + position = 0; + } + } + }; + car.move(); + assertEquals(0, car.getPosition()); + } + + @Test + void carNameIsSetCorrectly() { + Car car = new Car("Car"); + assertEquals("Car", car.getName()); + } + + @Test + void carInitialPositionIsZero() { + Car car = new Car("Car"); + assertEquals(0, car.getPosition()); + } +} diff --git a/src/test/java/racing/CarRacingServiceTest.java b/src/test/java/racing/CarsTest.java similarity index 96% rename from src/test/java/racing/CarRacingServiceTest.java rename to src/test/java/racing/CarsTest.java index cafe178f6e7..b0cd5ed232a 100644 --- a/src/test/java/racing/CarRacingServiceTest.java +++ b/src/test/java/racing/CarsTest.java @@ -6,7 +6,7 @@ import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertEquals; -public class CarRacingServiceTest { +public class CarsTest { @Test void testMoveCars() {