From e66ce535a61b87d82457a021bbfc98798bf0d58c Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 19:32:37 +0900 Subject: [PATCH 01/32] =?UTF-8?q?refactor:=20state=EB=A5=BC=20position?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Car.java | 16 ++++++++-------- src/main/java/com/racing/ui/ResultView.java | 2 +- src/main/java/com/racing/utils/CarHelper.java | 16 ++++++++-------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/racing/domain/Car.java b/src/main/java/com/racing/domain/Car.java index 69755c6dbcd..2b213a29bf8 100644 --- a/src/main/java/com/racing/domain/Car.java +++ b/src/main/java/com/racing/domain/Car.java @@ -4,7 +4,7 @@ public class Car { - private int state = 0; + private int position = 0; private String name; // 기본 생성자 @@ -14,7 +14,7 @@ public Car() { // 상태 지정 생성자 public Car(int state, String name) { - this.state = state; + this.position = state; this.name = name; } @@ -22,27 +22,27 @@ public void move() { RacingHelper racingHelper = new RacingHelper(); if (racingHelper.shouldMove()) { - state++; + position++; } } - public String displayRacingState() { + public String displayRacingPosition() { return displayCarName() + " : " + displayState(); } public String displayState() { - return "-".repeat(state); + return "-".repeat(position); } public String displayCarName() { return name; } - public int getState() { - return state; + public int getPosition() { + return position; } public boolean isDefeated(int verseState) { - return state < verseState; + return position < verseState; } } diff --git a/src/main/java/com/racing/ui/ResultView.java b/src/main/java/com/racing/ui/ResultView.java index cd3367fa657..6d6617bfe2e 100644 --- a/src/main/java/com/racing/ui/ResultView.java +++ b/src/main/java/com/racing/ui/ResultView.java @@ -12,7 +12,7 @@ public static void printStartMessage() { public static void printRoundResult(int round, List carList) { System.out.println("# " + round + "회차"); for (Car car : carList) { - System.out.println(car.displayRacingState()); + System.out.println(car.displayRacingPosition()); } System.out.println(); } diff --git a/src/main/java/com/racing/utils/CarHelper.java b/src/main/java/com/racing/utils/CarHelper.java index 276568e2c95..41912709f95 100644 --- a/src/main/java/com/racing/utils/CarHelper.java +++ b/src/main/java/com/racing/utils/CarHelper.java @@ -38,29 +38,29 @@ public static boolean isValidCarName(String carName) { public static List determineWinners(List carList) { List winners = new ArrayList<>(); - int maxState = findMaxState(carList); + int maxPosition = findMaxPosition(carList); for (Car car : carList) { - addWinners(car, maxState, winners); + addWinners(car, maxPosition, winners); } return winners; } - private static void addWinners(Car car, int maxState, List winners) { - if (car.getState() == maxState) { + private static void addWinners(Car car, int maxPosition, List winners) { + if (car.getPosition() == maxPosition) { winners.add(car); } } - private static int findMaxState(List carList) { - int maxState = 0; + private static int findMaxPosition(List carList) { + int maxPosition = 0; for (Car car : carList) { - maxState = car.isDefeated(maxState) ? maxState : car.getState(); + maxPosition = car.isDefeated(maxPosition) ? maxPosition : car.getPosition(); } - return maxState; + return maxPosition; } } From 065e71c610b9cb3a1d9fa089f74ab0562a6776a0 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 19:35:21 +0900 Subject: [PATCH 02/32] =?UTF-8?q?refactor:=20random=20=EC=9D=B8=ED=84=B0?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=8A=A4=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?=EB=AA=85=EC=B9=AD=EC=9D=84=20=EC=B6=94=EC=83=81=EC=A0=81?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/utils/RacingHelper.java | 2 +- .../racing/utils/{RandomMover.java => RandomStrategy.java} | 2 +- src/test/java/racing/utils/RacingHelperTest.java | 4 ++-- .../utils/{RandomMoverMock.java => RandomStrategyMock.java} | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) rename src/main/java/com/racing/utils/{RandomMover.java => RandomStrategy.java} (64%) rename src/test/java/racing/utils/{RandomMoverMock.java => RandomStrategyMock.java} (50%) diff --git a/src/main/java/com/racing/utils/RacingHelper.java b/src/main/java/com/racing/utils/RacingHelper.java index 20691509d09..a9224ebbab2 100644 --- a/src/main/java/com/racing/utils/RacingHelper.java +++ b/src/main/java/com/racing/utils/RacingHelper.java @@ -5,7 +5,7 @@ /** * 레이싱에서 사용 할 헬퍼 메서드를 모아놓은 클래스 */ -public class RacingHelper implements RandomMover { +public class RacingHelper implements RandomStrategy { private static final int RANDOM_NUMBER_RANGE = 10; private static final int FORWARD_MINIMUM_NUMBER = 4; diff --git a/src/main/java/com/racing/utils/RandomMover.java b/src/main/java/com/racing/utils/RandomStrategy.java similarity index 64% rename from src/main/java/com/racing/utils/RandomMover.java rename to src/main/java/com/racing/utils/RandomStrategy.java index 54d160c694d..02ff497b953 100644 --- a/src/main/java/com/racing/utils/RandomMover.java +++ b/src/main/java/com/racing/utils/RandomStrategy.java @@ -1,5 +1,5 @@ package com.racing.utils; -public interface RandomMover { +public interface RandomStrategy { public boolean shouldMove(); } diff --git a/src/test/java/racing/utils/RacingHelperTest.java b/src/test/java/racing/utils/RacingHelperTest.java index 69c4a4075b2..32f0d17605c 100644 --- a/src/test/java/racing/utils/RacingHelperTest.java +++ b/src/test/java/racing/utils/RacingHelperTest.java @@ -14,9 +14,9 @@ public class RacingHelperTest { @Test @DisplayName("모킹 데이터인 false를 반환 받는다.") public void isEquals_GetRandomBoolean() { - RandomMoverMock randomMover = new RandomMoverMock(); + RandomStrategyMock randomStrategy = new RandomStrategyMock(); - assertThat(randomMover.shouldMove()).isEqualTo(false); + assertThat(randomStrategy.shouldMove()).isEqualTo(false); } @Test diff --git a/src/test/java/racing/utils/RandomMoverMock.java b/src/test/java/racing/utils/RandomStrategyMock.java similarity index 50% rename from src/test/java/racing/utils/RandomMoverMock.java rename to src/test/java/racing/utils/RandomStrategyMock.java index 11d65fbad33..4d912a01c3d 100644 --- a/src/test/java/racing/utils/RandomMoverMock.java +++ b/src/test/java/racing/utils/RandomStrategyMock.java @@ -1,8 +1,8 @@ package racing.utils; -import com.racing.utils.RandomMover; +import com.racing.utils.RandomStrategy; -public class RandomMoverMock implements RandomMover { +public class RandomStrategyMock implements RandomStrategy { @Override public boolean shouldMove() { return false; From 90057c18d47ed1ff6ea2455ae843f1b1b8320192 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 19:49:02 +0900 Subject: [PATCH 03/32] =?UTF-8?q?feat:=20Cars=20=EC=9D=BC=EA=B8=89=20?= =?UTF-8?q?=EC=BD=9C=EB=9E=99=EC=85=98=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/com/racing/domain/Cars.java | 30 +++++++++++++++++++++++ src/test/java/racing/domain/CarsTest.java | 26 ++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/com/racing/domain/Cars.java create mode 100644 src/test/java/racing/domain/CarsTest.java diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java new file mode 100644 index 00000000000..98d7ad274bd --- /dev/null +++ b/src/main/java/com/racing/domain/Cars.java @@ -0,0 +1,30 @@ +package com.racing.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class Cars { + List carList; + + public Cars() { + carList = new ArrayList<>(); + } + + public Cars(List carList) { + this.carList = carList; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Cars cars = (Cars) o; + return Objects.equals(carList, cars.carList); + } + + @Override + public int hashCode() { + return Objects.hashCode(carList); + } +} diff --git a/src/test/java/racing/domain/CarsTest.java b/src/test/java/racing/domain/CarsTest.java new file mode 100644 index 00000000000..2d45651c9f4 --- /dev/null +++ b/src/test/java/racing/domain/CarsTest.java @@ -0,0 +1,26 @@ +package racing.domain; + +import com.racing.domain.Car; +import com.racing.domain.Cars; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CarsTest { + @Test + @DisplayName("Cars 객체가 정상적으로 만들어지는지 여부") + public void createCarsTest() { + Car pobyCar = new Car(5, "poby"); + Car jongCar = new Car(3, "jong"); + Car leeCar = new Car(3, "lee"); + + List oiginalCarList = Arrays.asList(pobyCar, jongCar, leeCar); + Cars cars = new Cars(oiginalCarList); + + assertThat(new Cars(Arrays.asList(pobyCar, jongCar, leeCar))).isEqualTo(cars); + } +} From d905f4add1fd2f487a0065f696d5b06453f9dd60 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 19:50:58 +0900 Subject: [PATCH 04/32] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/utils/CarHelper.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/racing/utils/CarHelper.java b/src/main/java/com/racing/utils/CarHelper.java index 41912709f95..ac2fc1e4640 100644 --- a/src/main/java/com/racing/utils/CarHelper.java +++ b/src/main/java/com/racing/utils/CarHelper.java @@ -57,7 +57,6 @@ private static int findMaxPosition(List carList) { int maxPosition = 0; for (Car car : carList) { maxPosition = car.isDefeated(maxPosition) ? maxPosition : car.getPosition(); - } return maxPosition; From 9d3b593ee61c1cab4bc8265326a3ab6d216e05ae Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:01:45 +0900 Subject: [PATCH 05/32] =?UTF-8?q?refactor:=20Random=EC=9D=84=20controller?= =?UTF-8?q?=20=EB=8B=A8=EC=97=90=EC=84=9C=20=EC=A0=9C=EC=96=B4=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=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/com/racing/Main.java | 5 ++++- src/main/java/com/racing/domain/Car.java | 8 ++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/racing/Main.java b/src/main/java/com/racing/Main.java index 5a201455acf..1e22cda0e5c 100644 --- a/src/main/java/com/racing/Main.java +++ b/src/main/java/com/racing/Main.java @@ -4,6 +4,7 @@ import com.racing.ui.InputView; import com.racing.ui.ResultView; import com.racing.utils.CarHelper; +import com.racing.utils.RacingHelper; import java.util.ArrayList; import java.util.List; @@ -41,8 +42,10 @@ private static void runRace(List carList, int tryNumber) { } private static void moveAllCars(List carList) { + RacingHelper racingHelper = new RacingHelper(); + for (Car car : carList) { - car.move(); + car.move(racingHelper.shouldMove()); } } } \ No newline at end of file diff --git a/src/main/java/com/racing/domain/Car.java b/src/main/java/com/racing/domain/Car.java index 2b213a29bf8..8de6a022d54 100644 --- a/src/main/java/com/racing/domain/Car.java +++ b/src/main/java/com/racing/domain/Car.java @@ -1,7 +1,5 @@ package com.racing.domain; -import com.racing.utils.RacingHelper; - public class Car { private int position = 0; @@ -18,10 +16,8 @@ public Car(int state, String name) { this.name = name; } - public void move() { - RacingHelper racingHelper = new RacingHelper(); - - if (racingHelper.shouldMove()) { + public void move(boolean isMovable) { + if (isMovable) { position++; } } From 99749ca7cbe38f7620fe5f1e48160591428c90c5 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:04:53 +0900 Subject: [PATCH 06/32] =?UTF-8?q?refactor:=20=EC=99=B8=EB=B6=80=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=20=EA=B0=92=EC=9D=98=20=EC=A0=91=EA=B7=BC=20=EC=A0=9C?= =?UTF-8?q?=EC=96=B4=EC=9E=90=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/utils/CarHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/racing/utils/CarHelper.java b/src/main/java/com/racing/utils/CarHelper.java index ac2fc1e4640..eaa8f7e49d0 100644 --- a/src/main/java/com/racing/utils/CarHelper.java +++ b/src/main/java/com/racing/utils/CarHelper.java @@ -28,7 +28,7 @@ private static void addIfValid(String carName, List validNames) { } } - public static boolean isValidCarName(String carName) { + private static boolean isValidCarName(String carName) { if (carName.isEmpty()) { return false; } From f6053fa81abe1db3b2d145d82facacb2cdc2cfbf Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:10:42 +0900 Subject: [PATCH 07/32] =?UTF-8?q?refactor:=20=EA=B0=84=EC=A0=91=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racing/utils/CarHelperTest.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/test/java/racing/utils/CarHelperTest.java b/src/test/java/racing/utils/CarHelperTest.java index 2d44d53f765..4df2c4cdd1a 100644 --- a/src/test/java/racing/utils/CarHelperTest.java +++ b/src/test/java/racing/utils/CarHelperTest.java @@ -4,8 +4,6 @@ import com.racing.utils.CarHelper; 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 java.util.Arrays; import java.util.List; @@ -22,13 +20,6 @@ public void isEquals_SplitStringFromDelegate() { assertThat(CarHelper.splitCarName("pobi,crong,honux")).isEqualTo(expectedValue); } - @ParameterizedTest - @DisplayName("5글자 이하일 경우 true, 6글자 이상일 경우 false반환") - @CsvSource(value = {"poby:true", "crong:true", "honux:true", "testtttt:false", "qweqweqwe:false"}, delimiter = ':') - void isEquals_ShouldEqualsExpectedValueSet(String carName, boolean expected) { - assertThat(CarHelper.isValidCarName(carName)).isEqualTo(expected); - } - @Test @DisplayName("모든 이름이 유효하지 않을 때 빈 리스트 반환") public void splitAndValidateCarNames_ShouldReturnEmptyList_WhenAllNamesAreInvalid() { From 8d16b5f9189d813ea3a9faa0313d5132df70b8dc Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:11:32 +0900 Subject: [PATCH 08/32] =?UTF-8?q?feat:=20position=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/com/racing/domain/Position.java | 24 +++++++++++++++++++ src/test/java/racing/domain/PositionTest.java | 17 +++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/main/java/com/racing/domain/Position.java create mode 100644 src/test/java/racing/domain/PositionTest.java diff --git a/src/main/java/com/racing/domain/Position.java b/src/main/java/com/racing/domain/Position.java new file mode 100644 index 00000000000..af46fcde794 --- /dev/null +++ b/src/main/java/com/racing/domain/Position.java @@ -0,0 +1,24 @@ +package com.racing.domain; + +import java.util.Objects; + +public class Position { + private final int position; + + public Position(int position) { + this.position = position; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Position position1 = (Position) o; + return position == position1.position; + } + + @Override + public int hashCode() { + return Objects.hashCode(position); + } +} diff --git a/src/test/java/racing/domain/PositionTest.java b/src/test/java/racing/domain/PositionTest.java new file mode 100644 index 00000000000..e6363b8d62d --- /dev/null +++ b/src/test/java/racing/domain/PositionTest.java @@ -0,0 +1,17 @@ +package racing.domain; + +import com.racing.domain.Position; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PositionTest { + @Test + @DisplayName("Position 객체가 생성자를 통해 정상적으로 만들어지는지 여부") + public void equalsTest() { + Position position = new Position(5); + + assertThat(new Position(5)).isEqualTo(position); + } +} From 516281d0217cad40ae4f0c3c5a438580d50a923d Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:13:06 +0900 Subject: [PATCH 09/32] =?UTF-8?q?feat:=20position=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=20=EC=B6=94=EA=B0=80=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/com/racing/domain/Position.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/racing/domain/Position.java b/src/main/java/com/racing/domain/Position.java index af46fcde794..31f82d15149 100644 --- a/src/main/java/com/racing/domain/Position.java +++ b/src/main/java/com/racing/domain/Position.java @@ -5,6 +5,10 @@ public class Position { private final int position; + public Position() { + this(0); + } + public Position(int position) { this.position = position; } From 1332ebf287161d1dbce43d3edbb056af8111eed6 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:17:22 +0900 Subject: [PATCH 10/32] =?UTF-8?q?feat:=20postion=20add,=20get=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=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/com/racing/domain/Position.java | 10 +++++++++- src/test/java/racing/domain/PositionTest.java | 12 +++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/racing/domain/Position.java b/src/main/java/com/racing/domain/Position.java index 31f82d15149..9d90ab2c61f 100644 --- a/src/main/java/com/racing/domain/Position.java +++ b/src/main/java/com/racing/domain/Position.java @@ -3,7 +3,7 @@ import java.util.Objects; public class Position { - private final int position; + private int position; public Position() { this(0); @@ -13,6 +13,14 @@ public Position(int position) { this.position = position; } + public void addPosition() { + this.position++; + } + + public int getPosition() { + return position; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/test/java/racing/domain/PositionTest.java b/src/test/java/racing/domain/PositionTest.java index e6363b8d62d..eb44b99a828 100644 --- a/src/test/java/racing/domain/PositionTest.java +++ b/src/test/java/racing/domain/PositionTest.java @@ -9,9 +9,19 @@ public class PositionTest { @Test @DisplayName("Position 객체가 생성자를 통해 정상적으로 만들어지는지 여부") - public void equalsTest() { + public void positionConstructorTest() { Position position = new Position(5); assertThat(new Position(5)).isEqualTo(position); } + + @Test + @DisplayName("Position add 함수를 실행했을 때 정상적으로 1이 더해지는지 여부") + public void positionAddFuncTest() { + Position position = new Position(0); + position.addPosition(); + position.addPosition(); + + assertThat(position.getPosition()).isEqualTo(2); + } } From f0c3b6a0d71832c136e39b4c27c5b6495cdb08c7 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:19:41 +0900 Subject: [PATCH 11/32] =?UTF-8?q?refator:=20=ED=94=84=EB=A6=AC=EB=AF=B8?= =?UTF-8?q?=ED=8B=B0=EB=B8=8C=20=ED=83=80=EC=9E=85=EC=9D=98=20position?= =?UTF-8?q?=EC=9D=84=20=EA=B0=9D=EC=B2=B4=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Car.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/racing/domain/Car.java b/src/main/java/com/racing/domain/Car.java index 8de6a022d54..68ba6900d66 100644 --- a/src/main/java/com/racing/domain/Car.java +++ b/src/main/java/com/racing/domain/Car.java @@ -2,7 +2,7 @@ public class Car { - private int position = 0; + private Position position; private String name; // 기본 생성자 @@ -11,14 +11,14 @@ public Car() { } // 상태 지정 생성자 - public Car(int state, String name) { - this.position = state; + public Car(int position, String name) { + this.position = new Position(position); this.name = name; } public void move(boolean isMovable) { if (isMovable) { - position++; + position.addPosition(); } } @@ -27,7 +27,7 @@ public String displayRacingPosition() { } public String displayState() { - return "-".repeat(position); + return "-".repeat(position.getPosition()); } public String displayCarName() { @@ -35,10 +35,10 @@ public String displayCarName() { } public int getPosition() { - return position; + return position.getPosition(); } public boolean isDefeated(int verseState) { - return position < verseState; + return position.getPosition() < verseState; } } From ed49ad85e220be7aaaee748fe51a4f61d6575177 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:24:31 +0900 Subject: [PATCH 12/32] =?UTF-8?q?refactor:=20=EB=8C=80=EC=8B=9C=EB=A5=BC?= =?UTF-8?q?=20=EB=B0=98=ED=99=98=ED=95=A0=20=EB=95=8C=20position=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=EC=9D=98=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=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/com/racing/domain/Car.java | 2 +- src/main/java/com/racing/domain/Position.java | 4 ++++ src/test/java/racing/domain/PositionTest.java | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/racing/domain/Car.java b/src/main/java/com/racing/domain/Car.java index 68ba6900d66..9818c63cf31 100644 --- a/src/main/java/com/racing/domain/Car.java +++ b/src/main/java/com/racing/domain/Car.java @@ -27,7 +27,7 @@ public String displayRacingPosition() { } public String displayState() { - return "-".repeat(position.getPosition()); + return position.displayDashAsPosition(); } public String displayCarName() { diff --git a/src/main/java/com/racing/domain/Position.java b/src/main/java/com/racing/domain/Position.java index 9d90ab2c61f..bc14177696c 100644 --- a/src/main/java/com/racing/domain/Position.java +++ b/src/main/java/com/racing/domain/Position.java @@ -21,6 +21,10 @@ public int getPosition() { return position; } + public String displayDashAsPosition() { + return "-".repeat(this.position); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/test/java/racing/domain/PositionTest.java b/src/test/java/racing/domain/PositionTest.java index eb44b99a828..3df7f9f173c 100644 --- a/src/test/java/racing/domain/PositionTest.java +++ b/src/test/java/racing/domain/PositionTest.java @@ -24,4 +24,14 @@ public void positionAddFuncTest() { assertThat(position.getPosition()).isEqualTo(2); } + + + @Test + @DisplayName("position 값에 따라 올바른 대시 문자열을 반환하는지 여부") + public void displayDashAsPositionTest() { + Position position = new Position(3); + String result = position.displayDashAsPosition(); + + assertThat(result).isEqualTo("---"); + } } From 2591e7fd037f41f48b7428e9865c53e6eec0c4eb Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:31:44 +0900 Subject: [PATCH 13/32] =?UTF-8?q?feat:=20=ED=81=B0=20=EA=B0=92=EC=9D=B8?= =?UTF-8?q?=EC=A7=80=20=EC=97=AC=EB=B6=80=EB=A5=BC=20=EA=B5=AC=EB=B6=84?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EB=B0=98=ED=99=98=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20car=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=8C=80=EC=B2=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Car.java | 2 +- src/main/java/com/racing/domain/Position.java | 4 ++++ src/test/java/racing/domain/PositionTest.java | 12 +++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/racing/domain/Car.java b/src/main/java/com/racing/domain/Car.java index 9818c63cf31..b250adb6a0a 100644 --- a/src/main/java/com/racing/domain/Car.java +++ b/src/main/java/com/racing/domain/Car.java @@ -39,6 +39,6 @@ public int getPosition() { } public boolean isDefeated(int verseState) { - return position.getPosition() < verseState; + return position.isBiggerThanPosition(verseState); } } diff --git a/src/main/java/com/racing/domain/Position.java b/src/main/java/com/racing/domain/Position.java index bc14177696c..a02a515134d 100644 --- a/src/main/java/com/racing/domain/Position.java +++ b/src/main/java/com/racing/domain/Position.java @@ -25,6 +25,10 @@ public String displayDashAsPosition() { return "-".repeat(this.position); } + public boolean isBiggerThanPosition(int number) { + return this.position < number; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/test/java/racing/domain/PositionTest.java b/src/test/java/racing/domain/PositionTest.java index 3df7f9f173c..98fc6591fd5 100644 --- a/src/test/java/racing/domain/PositionTest.java +++ b/src/test/java/racing/domain/PositionTest.java @@ -25,7 +25,6 @@ public void positionAddFuncTest() { assertThat(position.getPosition()).isEqualTo(2); } - @Test @DisplayName("position 값에 따라 올바른 대시 문자열을 반환하는지 여부") public void displayDashAsPositionTest() { @@ -34,4 +33,15 @@ public void displayDashAsPositionTest() { assertThat(result).isEqualTo("---"); } + + @Test + @DisplayName("position을 올바르게 비교하여 큰 값인지 여부") + public void isBiggerThanPositionTest() { + Position position = new Position(5); + + assertThat(position.isBiggerThanPosition(6)).isTrue(); + assertThat(position.isBiggerThanPosition(5)).isFalse(); + assertThat(position.isBiggerThanPosition(4)).isFalse(); + } + } From 2a9956438b85cda8d9b33bd86d2fd8dfbf0090b9 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:31:57 +0900 Subject: [PATCH 14/32] =?UTF-8?q?refator:=20=EA=B0=84=EC=A0=91=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racing/domain/CarTest.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/test/java/racing/domain/CarTest.java b/src/test/java/racing/domain/CarTest.java index a2a6d1fea39..acb5610ff03 100644 --- a/src/test/java/racing/domain/CarTest.java +++ b/src/test/java/racing/domain/CarTest.java @@ -7,15 +7,6 @@ import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; public class CarTest { - @Test - @DisplayName("입력한 숫자 만큼 '-'를 반복하여 반환") - public void isEquals_RepeatDash() { - Car car = new Car(6, ""); - String expected = "------"; - - assertThat(car.displayState()).isEqualTo(expected); - } - @Test @DisplayName("입력한 CarName을 반환") public void isEquals_CarName() { From c01dacdd0829469fb27d877db7c74c43d1ab4813 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:32:17 +0900 Subject: [PATCH 15/32] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Car.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/racing/domain/Car.java b/src/main/java/com/racing/domain/Car.java index b250adb6a0a..5b4010f3e1a 100644 --- a/src/main/java/com/racing/domain/Car.java +++ b/src/main/java/com/racing/domain/Car.java @@ -23,11 +23,7 @@ public void move(boolean isMovable) { } public String displayRacingPosition() { - return displayCarName() + " : " + displayState(); - } - - public String displayState() { - return position.displayDashAsPosition(); + return this.name + " : " + position.displayDashAsPosition(); } public String displayCarName() { From ff2a4e77915b72c52ce34d3816179a56345fe3df Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:40:11 +0900 Subject: [PATCH 16/32] =?UTF-8?q?refactor:=20cars=20=EC=9D=BC=EA=B8=89=20?= =?UTF-8?q?=EC=BD=9C=EB=9E=99=EC=85=98=EC=97=90=20car=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=98=EB=8A=94=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/Main.java | 15 +++++++++------ src/main/java/com/racing/domain/Cars.java | 8 ++++++++ src/test/java/racing/domain/CarsTest.java | 12 ++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/racing/Main.java b/src/main/java/com/racing/Main.java index 1e22cda0e5c..5e6dd12e388 100644 --- a/src/main/java/com/racing/Main.java +++ b/src/main/java/com/racing/Main.java @@ -1,12 +1,12 @@ package com.racing; import com.racing.domain.Car; +import com.racing.domain.Cars; import com.racing.ui.InputView; import com.racing.ui.ResultView; import com.racing.utils.CarHelper; import com.racing.utils.RacingHelper; -import java.util.ArrayList; import java.util.List; public class Main { @@ -16,21 +16,24 @@ public static void main(String[] args) { List validCarNames = CarHelper.splitCarName(carNames); - List carList = createCars(validCarNames); + Cars cars = createCars(validCarNames); + + // 리팩토링 단계에서 임시로 선언했습니다! + List carList = cars.getCarList(); runRace(carList, tryNumber); List winners = CarHelper.determineWinners(carList); ResultView.printWinners(winners); } - private static List createCars(List carNames) { - List carList = new ArrayList<>(); + private static Cars createCars(List carNames) { + Cars cars = new Cars(); for (String carName : carNames) { - carList.add(new Car(0, carName)); + cars.addCars(new Car(0, carName)); } - return carList; + return cars; } private static void runRace(List carList, int tryNumber) { diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index 98d7ad274bd..64e8b43046b 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -15,6 +15,14 @@ public Cars(List carList) { this.carList = carList; } + public void addCars(Car car) { + carList.add(car); + } + + public List getCarList() { + return this.carList; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/test/java/racing/domain/CarsTest.java b/src/test/java/racing/domain/CarsTest.java index 2d45651c9f4..68e0d07737b 100644 --- a/src/test/java/racing/domain/CarsTest.java +++ b/src/test/java/racing/domain/CarsTest.java @@ -23,4 +23,16 @@ public void createCarsTest() { assertThat(new Cars(Arrays.asList(pobyCar, jongCar, leeCar))).isEqualTo(cars); } + + @Test + @DisplayName("addCars 메서드가 Car 객체를 정상적으로 추가하는지 여부") + public void addCarsTest() { + Cars cars = new Cars(); + Car newCar = new Car(4, "newCar"); + + cars.addCars(newCar); + + assertThat(cars.getCarList()).hasSize(1); + assertThat(cars.getCarList().get(0)).isEqualTo(newCar); + } } From 8533d99d852e28f27e9cf15c095ac611cfe6d6c1 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:43:29 +0900 Subject: [PATCH 17/32] =?UTF-8?q?refactor:=20List=EB=A1=9C=20=EC=84=A0?= =?UTF-8?q?=EC=96=B8=EB=90=9C=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0,=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=20=EA=B0=92=EC=9D=84=20Cars=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/Main.java | 16 +++++++--------- src/main/java/com/racing/utils/CarHelper.java | 7 ++++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/racing/Main.java b/src/main/java/com/racing/Main.java index 5e6dd12e388..33e82aa82bd 100644 --- a/src/main/java/com/racing/Main.java +++ b/src/main/java/com/racing/Main.java @@ -18,11 +18,9 @@ public static void main(String[] args) { Cars cars = createCars(validCarNames); - // 리팩토링 단계에서 임시로 선언했습니다! - List carList = cars.getCarList(); - runRace(carList, tryNumber); + runRace(cars, tryNumber); - List winners = CarHelper.determineWinners(carList); + List winners = CarHelper.determineWinners(cars); ResultView.printWinners(winners); } @@ -36,18 +34,18 @@ private static Cars createCars(List carNames) { return cars; } - private static void runRace(List carList, int tryNumber) { + private static void runRace(Cars cars, int tryNumber) { ResultView.printStartMessage(); for (int i = 0; i < tryNumber; i++) { - moveAllCars(carList); - ResultView.printRoundResult(i + 1, carList); + moveAllCars(cars); + ResultView.printRoundResult(i + 1, cars.getCarList()); } } - private static void moveAllCars(List carList) { + private static void moveAllCars(Cars cars) { RacingHelper racingHelper = new RacingHelper(); - for (Car car : carList) { + for (Car car : cars.getCarList()) { car.move(racingHelper.shouldMove()); } } diff --git a/src/main/java/com/racing/utils/CarHelper.java b/src/main/java/com/racing/utils/CarHelper.java index eaa8f7e49d0..e9daf3bace1 100644 --- a/src/main/java/com/racing/utils/CarHelper.java +++ b/src/main/java/com/racing/utils/CarHelper.java @@ -1,6 +1,7 @@ package com.racing.utils; import com.racing.domain.Car; +import com.racing.domain.Cars; import java.util.ArrayList; import java.util.List; @@ -36,11 +37,11 @@ private static boolean isValidCarName(String carName) { return carName.length() < VALID_CAR_NAME; } - public static List determineWinners(List carList) { + public static List determineWinners(Cars cars) { List winners = new ArrayList<>(); - int maxPosition = findMaxPosition(carList); + int maxPosition = findMaxPosition(cars.getCarList()); - for (Car car : carList) { + for (Car car : cars.getCarList()) { addWinners(car, maxPosition, winners); } From e386be17a3bd6f94bdddeeda8589b8229917f170 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:49:13 +0900 Subject: [PATCH 18/32] =?UTF-8?q?refactor:=20cars=20=EB=A5=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81=20=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/Main.java | 4 +++- src/main/java/com/racing/ui/ResultView.java | 5 +++-- src/test/java/racing/utils/CarHelperTest.java | 5 +++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/racing/Main.java b/src/main/java/com/racing/Main.java index 33e82aa82bd..66080193aef 100644 --- a/src/main/java/com/racing/Main.java +++ b/src/main/java/com/racing/Main.java @@ -38,7 +38,9 @@ private static void runRace(Cars cars, int tryNumber) { ResultView.printStartMessage(); for (int i = 0; i < tryNumber; i++) { moveAllCars(cars); - ResultView.printRoundResult(i + 1, cars.getCarList()); + + int roundNumber = i + 1; + ResultView.printRoundResult(roundNumber, cars); } } diff --git a/src/main/java/com/racing/ui/ResultView.java b/src/main/java/com/racing/ui/ResultView.java index 6d6617bfe2e..97ea86eab7f 100644 --- a/src/main/java/com/racing/ui/ResultView.java +++ b/src/main/java/com/racing/ui/ResultView.java @@ -1,6 +1,7 @@ package com.racing.ui; import com.racing.domain.Car; +import com.racing.domain.Cars; import java.util.List; @@ -9,9 +10,9 @@ public static void printStartMessage() { System.out.println("실행 결과"); } - public static void printRoundResult(int round, List carList) { + public static void printRoundResult(int round, Cars cars) { System.out.println("# " + round + "회차"); - for (Car car : carList) { + for (Car car : cars.getCarList()) { System.out.println(car.displayRacingPosition()); } System.out.println(); diff --git a/src/test/java/racing/utils/CarHelperTest.java b/src/test/java/racing/utils/CarHelperTest.java index 4df2c4cdd1a..ea6cf2e830e 100644 --- a/src/test/java/racing/utils/CarHelperTest.java +++ b/src/test/java/racing/utils/CarHelperTest.java @@ -1,6 +1,7 @@ package racing.utils; import com.racing.domain.Car; +import com.racing.domain.Cars; import com.racing.utils.CarHelper; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -41,7 +42,7 @@ void isEquals_SingleWinner() { Car car1 = new Car(3, "car1"); Car car2 = new Car(2, "car2"); Car car3 = new Car(1, "car3"); - List cars = Arrays.asList(car1, car2, car3); + Cars cars = new Cars(Arrays.asList(car1, car2, car3)); List winners = CarHelper.determineWinners(cars); @@ -55,7 +56,7 @@ void isEquals_MultipleWinner() { Car car1 = new Car(3, "car1"); Car car2 = new Car(3, "car2"); Car car3 = new Car(1, "car3"); - List cars = Arrays.asList(car1, car2, car3); + Cars cars = new Cars(Arrays.asList(car1, car2, car3)); List winners = CarHelper.determineWinners(cars); From 4fd6712c9debf52be78445a0657c0c2e5cdcc7cc Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 20:59:00 +0900 Subject: [PATCH 19/32] =?UTF-8?q?feat:=20carName=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/CarName.java | 32 +++++++++++++++++ src/test/java/racing/domain/CarNameTest.java | 36 ++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/main/java/com/racing/domain/CarName.java create mode 100644 src/test/java/racing/domain/CarNameTest.java diff --git a/src/main/java/com/racing/domain/CarName.java b/src/main/java/com/racing/domain/CarName.java new file mode 100644 index 00000000000..1a5388d5cdb --- /dev/null +++ b/src/main/java/com/racing/domain/CarName.java @@ -0,0 +1,32 @@ +package com.racing.domain; + +import java.util.Objects; + +public class CarName { + private final String carName; + + public CarName(String carName) { + if (carName == null || carName.trim().isEmpty()) { + throw new IllegalArgumentException("Car name cannot be empty or blank."); + } + + this.carName = carName; + } + + public String getCarName() { + return carName; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CarName carName1 = (CarName) o; + return Objects.equals(carName, carName1.carName); + } + + @Override + public int hashCode() { + return Objects.hashCode(carName); + } +} diff --git a/src/test/java/racing/domain/CarNameTest.java b/src/test/java/racing/domain/CarNameTest.java new file mode 100644 index 00000000000..b7fabb1b21b --- /dev/null +++ b/src/test/java/racing/domain/CarNameTest.java @@ -0,0 +1,36 @@ +package racing.domain; + +import com.racing.domain.CarName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; + + +public class CarNameTest { + @Test + @DisplayName("CarName 객체가 생성자를 통해 정상적으로 만들어지는지 여부") + public void positionConstructorTest() { + CarName carName = new CarName("poby"); + + assertThat(new CarName("poby")).isEqualTo(carName); + } + + @ParameterizedTest + @ValueSource(strings = {"", " "}) + @DisplayName("빈 문자열이나 공백으로 CarName 객체 생성 시 예외 발생") + public void createCarNameWithEmptyNameThrowsException(String invalidName) { + assertThatThrownBy(() -> new CarName(invalidName)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("null로 CarName 객체 생성 시 예외 발생") + public void createCarNameWithNullThrowsException() { + assertThatThrownBy(() -> new CarName(null)) + .isInstanceOf(NullPointerException.class); + } +} From 7651e0cb119682c8e9cd241782d9800e8296694f Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 21:00:51 +0900 Subject: [PATCH 20/32] =?UTF-8?q?refactor:=20String=20=ED=94=84=EB=A6=AC?= =?UTF-8?q?=EB=AF=B8=ED=8B=B0=EB=B8=8C=20=ED=83=80=EC=9E=85=EC=97=90?= =?UTF-8?q?=EC=84=9C=20CarName=20=EA=B0=9D=EC=B2=B4=EB=A5=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=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/com/racing/domain/Car.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/racing/domain/Car.java b/src/main/java/com/racing/domain/Car.java index 5b4010f3e1a..4fac28b0623 100644 --- a/src/main/java/com/racing/domain/Car.java +++ b/src/main/java/com/racing/domain/Car.java @@ -3,7 +3,7 @@ public class Car { private Position position; - private String name; + private CarName carName; // 기본 생성자 public Car() { @@ -11,9 +11,9 @@ public Car() { } // 상태 지정 생성자 - public Car(int position, String name) { + public Car(int position, String carName) { this.position = new Position(position); - this.name = name; + this.carName = new CarName(carName); } public void move(boolean isMovable) { @@ -23,11 +23,11 @@ public void move(boolean isMovable) { } public String displayRacingPosition() { - return this.name + " : " + position.displayDashAsPosition(); + return carName.getCarName() + " : " + position.displayDashAsPosition(); } public String displayCarName() { - return name; + return carName.getCarName(); } public int getPosition() { From 094b6be21bbe9789d59b16f31d56d30242a21f62 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 21:07:29 +0900 Subject: [PATCH 21/32] =?UTF-8?q?refactor:=20cars=EC=97=90=EC=84=9C=20maxp?= =?UTF-8?q?osition=EC=9D=84=20=EB=B0=98=ED=99=98=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20=EC=A7=84?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Cars.java | 9 +++++++ src/main/java/com/racing/utils/CarHelper.java | 11 +------- src/test/java/racing/domain/CarsTest.java | 26 +++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index 64e8b43046b..75022eb310a 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -23,6 +23,15 @@ public List getCarList() { return this.carList; } + public int findMaxPosition() { + int maxPosition = 0; + for (Car car : this.carList) { + maxPosition = car.isDefeated(maxPosition) ? maxPosition : car.getPosition(); + } + + return maxPosition; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/racing/utils/CarHelper.java b/src/main/java/com/racing/utils/CarHelper.java index e9daf3bace1..560e0dd42c5 100644 --- a/src/main/java/com/racing/utils/CarHelper.java +++ b/src/main/java/com/racing/utils/CarHelper.java @@ -39,7 +39,7 @@ private static boolean isValidCarName(String carName) { public static List determineWinners(Cars cars) { List winners = new ArrayList<>(); - int maxPosition = findMaxPosition(cars.getCarList()); + int maxPosition = cars.findMaxPosition(); for (Car car : cars.getCarList()) { addWinners(car, maxPosition, winners); @@ -54,13 +54,4 @@ private static void addWinners(Car car, int maxPosition, List winners) { } } - private static int findMaxPosition(List carList) { - int maxPosition = 0; - for (Car car : carList) { - maxPosition = car.isDefeated(maxPosition) ? maxPosition : car.getPosition(); - } - - return maxPosition; - } - } diff --git a/src/test/java/racing/domain/CarsTest.java b/src/test/java/racing/domain/CarsTest.java index 68e0d07737b..1223b47a83f 100644 --- a/src/test/java/racing/domain/CarsTest.java +++ b/src/test/java/racing/domain/CarsTest.java @@ -35,4 +35,30 @@ public void addCarsTest() { assertThat(cars.getCarList()).hasSize(1); assertThat(cars.getCarList().get(0)).isEqualTo(newCar); } + + @Test + @DisplayName("가장 앞선 자동차의 위치를 정확히 찾는지 테스트") + void findMaxPositionTest() { + Car car1 = new Car(3, "car1"); + Car car2 = new Car(5, "car2"); + Car car3 = new Car(1, "car3"); + Cars cars = new Cars(Arrays.asList(car1, car2, car3)); + + int maxPosition = cars.findMaxPosition(); + + assertThat(maxPosition).isEqualTo(5); + } + + @Test + @DisplayName("모든 자동차가 같은 위치에 있을 때 정확한 위치를 반환하는지 테스트") + void findMaxPositionWhenAllCarsAtSamePosition() { + Car car1 = new Car(3, "car1"); + Car car2 = new Car(3, "car2"); + Car car3 = new Car(3, "car3"); + Cars cars = new Cars(Arrays.asList(car1, car2, car3)); + + int maxPosition = cars.findMaxPosition(); + + assertThat(maxPosition).isEqualTo(3); + } } From 0bee42dbef1faa8c553185d71aa0ff34fd8dd11d Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 21:13:25 +0900 Subject: [PATCH 22/32] =?UTF-8?q?refactor:=20Cars=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EB=82=B4=EB=B6=80=EC=97=90=EC=84=9C=20=EC=9A=B0=EC=8A=B9?= =?UTF-8?q?=EC=9E=90=EB=A5=BC=20=EA=B2=B0=EC=A0=95=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=EB=A5=BC=20=EB=B0=98=ED=99=98=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=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/com/racing/Main.java | 3 ++- src/main/java/com/racing/domain/Cars.java | 11 ++++++++++ src/main/java/com/racing/ui/ResultView.java | 8 +++----- src/main/java/com/racing/utils/CarHelper.java | 20 ------------------- src/test/java/racing/utils/CarHelperTest.java | 12 +++++------ 5 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/racing/Main.java b/src/main/java/com/racing/Main.java index 66080193aef..ec767ac2291 100644 --- a/src/main/java/com/racing/Main.java +++ b/src/main/java/com/racing/Main.java @@ -20,7 +20,8 @@ public static void main(String[] args) { runRace(cars, tryNumber); - List winners = CarHelper.determineWinners(cars); + Cars winners = + cars.determineWinners(); ResultView.printWinners(winners); } diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index 75022eb310a..1081c7fc71a 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class Cars { List carList; @@ -32,6 +33,16 @@ public int findMaxPosition() { return maxPosition; } + public Cars determineWinners() { + int maxPosition = this.findMaxPosition(); + + List winnerList = this.carList.stream() + .filter(car -> car.getPosition() == maxPosition) + .collect(Collectors.toList()); + + return new Cars(winnerList); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/racing/ui/ResultView.java b/src/main/java/com/racing/ui/ResultView.java index 97ea86eab7f..cdb974e45dc 100644 --- a/src/main/java/com/racing/ui/ResultView.java +++ b/src/main/java/com/racing/ui/ResultView.java @@ -3,8 +3,6 @@ import com.racing.domain.Car; import com.racing.domain.Cars; -import java.util.List; - public class ResultView { public static void printStartMessage() { System.out.println("실행 결과"); @@ -18,11 +16,11 @@ public static void printRoundResult(int round, Cars cars) { System.out.println(); } - public static void printWinners(List winners) { + public static void printWinners(Cars winners) { System.out.print("최종 우승자: "); - int winnerSize = winners.size(); + int winnerSize = winners.getCarList().size(); for (int i = 0; i < winnerSize; i++) { - System.out.print(winners.get(i).displayCarName()); + System.out.print(winners.getCarList().get(i).displayCarName()); printComma(i < winnerSize - 1); } diff --git a/src/main/java/com/racing/utils/CarHelper.java b/src/main/java/com/racing/utils/CarHelper.java index 560e0dd42c5..4f645f815ac 100644 --- a/src/main/java/com/racing/utils/CarHelper.java +++ b/src/main/java/com/racing/utils/CarHelper.java @@ -1,8 +1,5 @@ package com.racing.utils; -import com.racing.domain.Car; -import com.racing.domain.Cars; - import java.util.ArrayList; import java.util.List; @@ -37,21 +34,4 @@ private static boolean isValidCarName(String carName) { return carName.length() < VALID_CAR_NAME; } - public static List determineWinners(Cars cars) { - List winners = new ArrayList<>(); - int maxPosition = cars.findMaxPosition(); - - for (Car car : cars.getCarList()) { - addWinners(car, maxPosition, winners); - } - - return winners; - } - - private static void addWinners(Car car, int maxPosition, List winners) { - if (car.getPosition() == maxPosition) { - winners.add(car); - } - } - } diff --git a/src/test/java/racing/utils/CarHelperTest.java b/src/test/java/racing/utils/CarHelperTest.java index ea6cf2e830e..b85b4e1d61f 100644 --- a/src/test/java/racing/utils/CarHelperTest.java +++ b/src/test/java/racing/utils/CarHelperTest.java @@ -44,10 +44,10 @@ void isEquals_SingleWinner() { Car car3 = new Car(1, "car3"); Cars cars = new Cars(Arrays.asList(car1, car2, car3)); - List winners = CarHelper.determineWinners(cars); + Cars winners = cars.determineWinners(); - assertThat(winners).hasSize(1); - assertThat(winners.get(0).displayCarName()).isEqualTo("car1"); + assertThat(winners.getCarList()).hasSize(1); + assertThat(winners.getCarList().get(0).displayCarName()).isEqualTo("car1"); } @Test @@ -58,9 +58,9 @@ void isEquals_MultipleWinner() { Car car3 = new Car(1, "car3"); Cars cars = new Cars(Arrays.asList(car1, car2, car3)); - List winners = CarHelper.determineWinners(cars); + Cars winners = cars.determineWinners(); - assertThat(winners).hasSize(2); - assertThat(winners).extracting("name").containsExactlyInAnyOrder("car1", "car2"); + assertThat(winners.getCarList()).hasSize(2); + assertThat(winners.getCarList()).extracting("name").containsExactlyInAnyOrder("car1", "car2"); } } From 8e9422502b5ab06b04805c8a241fca611a1477c7 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 21:15:33 +0900 Subject: [PATCH 23/32] =?UTF-8?q?refactor:=20carSize=EB=A5=BC=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=ED=95=98=EB=8A=94=20=EB=A7=A4=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Cars.java | 4 ++++ src/test/java/racing/domain/CarsTest.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index 1081c7fc71a..a85645bfe00 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -43,6 +43,10 @@ public Cars determineWinners() { return new Cars(winnerList); } + public int carsSize() { + return this.carList.size(); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/test/java/racing/domain/CarsTest.java b/src/test/java/racing/domain/CarsTest.java index 1223b47a83f..0a011ed40ba 100644 --- a/src/test/java/racing/domain/CarsTest.java +++ b/src/test/java/racing/domain/CarsTest.java @@ -61,4 +61,19 @@ void findMaxPositionWhenAllCarsAtSamePosition() { assertThat(maxPosition).isEqualTo(3); } + + + @Test + @DisplayName("carsSize 메서드가 정확한 자동차 수를 반환하는지 테스트") + void carsSizeTest() { + Car car1 = new Car(3, "car1"); + Car car2 = new Car(3, "car2"); + Car car3 = new Car(3, "car3"); + Cars cars = new Cars(Arrays.asList(car1, car2, car3)); + + int size = cars.carsSize(); + + assertThat(size).isEqualTo(3); + } + } From 1643450a9baa88d9f8c2dd8a2699d3e8fc26a167 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 15 Oct 2024 21:23:31 +0900 Subject: [PATCH 24/32] =?UTF-8?q?refactor:=20resultview=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8A=94=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=EB=A5=BC=20Cars=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=82=B4?= =?UTF-8?q?=EB=B6=80=EC=97=90=EC=84=9C=20=EA=B5=AC=ED=98=84=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=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/com/racing/domain/Cars.java | 14 +++++++++ src/main/java/com/racing/ui/ResultView.java | 11 +++---- src/test/java/racing/domain/CarsTest.java | 34 +++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index a85645bfe00..1579c11fac2 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -47,6 +47,20 @@ public int carsSize() { return this.carList.size(); } + public String getCarName(int index) { + if (index < 0 || index >= carList.size()) { + throw new IndexOutOfBoundsException("Invalid car index"); + } + + return carList.get(index).displayCarName(); + } + + public void printRacingPositions() { + for (Car car : carList) { + System.out.println(car.displayRacingPosition()); + } + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/racing/ui/ResultView.java b/src/main/java/com/racing/ui/ResultView.java index cdb974e45dc..614e3df92ae 100644 --- a/src/main/java/com/racing/ui/ResultView.java +++ b/src/main/java/com/racing/ui/ResultView.java @@ -1,6 +1,5 @@ package com.racing.ui; -import com.racing.domain.Car; import com.racing.domain.Cars; public class ResultView { @@ -10,17 +9,17 @@ public static void printStartMessage() { public static void printRoundResult(int round, Cars cars) { System.out.println("# " + round + "회차"); - for (Car car : cars.getCarList()) { - System.out.println(car.displayRacingPosition()); - } + + cars.printRacingPositions(); + System.out.println(); } public static void printWinners(Cars winners) { System.out.print("최종 우승자: "); - int winnerSize = winners.getCarList().size(); + int winnerSize = winners.carsSize(); for (int i = 0; i < winnerSize; i++) { - System.out.print(winners.getCarList().get(i).displayCarName()); + System.out.print(winners.getCarName(i)); printComma(i < winnerSize - 1); } diff --git a/src/test/java/racing/domain/CarsTest.java b/src/test/java/racing/domain/CarsTest.java index 0a011ed40ba..fef79e5c5b3 100644 --- a/src/test/java/racing/domain/CarsTest.java +++ b/src/test/java/racing/domain/CarsTest.java @@ -5,10 +5,13 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import java.util.Arrays; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; public class CarsTest { @Test @@ -76,4 +79,35 @@ void carsSizeTest() { assertThat(size).isEqualTo(3); } + @Test + @DisplayName("getCarName 메서드 테스트") + void getCarNameTest() { + Car car1 = new Car(3, "pobi"); + Car car2 = new Car(3, "crong"); + Cars cars = new Cars(Arrays.asList(car1, car2)); + + assertThat(cars.getCarName(0)).isEqualTo("pobi"); + assertThat(cars.getCarName(1)).isEqualTo("crong"); + assertThatThrownBy(() -> cars.getCarName(2)) + .isInstanceOf(IndexOutOfBoundsException.class); + } + + @Test + @DisplayName("printRacingPositions 메서드 테스트") + void printRacingPositionsTest() { + // Given + Car car1 = new Car(1, "pobi"); + Car car2 = new Car(3, "crong"); + Cars cars = new Cars(Arrays.asList(car1, car2)); + + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + + cars.printRacingPositions(); + + String expectedOutput = "pobi : -\ncrong : ---\n"; + assertThat(outContent.toString()).isEqualTo(expectedOutput); + + System.setOut(System.out); + } } From 9a3199fe556425f94ce983e9fa74436f23bcc138 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 22 Oct 2024 18:55:43 +0900 Subject: [PATCH 25/32] =?UTF-8?q?refactor:=20=EB=A9=94=EC=9D=B8=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=EB=8A=94=20=EC=8B=9C=EC=9E=91=EC=A0=90=20?= =?UTF-8?q?=EC=97=AD=ED=95=A0=EB=A7=8C=20=ED=95=98=EA=B3=A0=20=EC=8B=A4?= =?UTF-8?q?=EC=A0=9C=20=EC=84=9C=EB=B9=84=EC=8A=A4(=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4)=EB=8A=94=20=EB=B6=84=EB=A6=AC,=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=EB=AA=85=EC=9D=84=20=EB=AA=85=ED=99=95=ED=95=98=EA=B2=8C=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 --- src/main/java/com/racing/Main.java | 50 +---------------- .../com/racing/service/RacingService.java | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 src/main/java/com/racing/service/RacingService.java diff --git a/src/main/java/com/racing/Main.java b/src/main/java/com/racing/Main.java index ec767ac2291..38c8897711c 100644 --- a/src/main/java/com/racing/Main.java +++ b/src/main/java/com/racing/Main.java @@ -1,55 +1,9 @@ package com.racing; -import com.racing.domain.Car; -import com.racing.domain.Cars; -import com.racing.ui.InputView; -import com.racing.ui.ResultView; -import com.racing.utils.CarHelper; -import com.racing.utils.RacingHelper; - -import java.util.List; +import com.racing.service.RacingService; public class Main { public static void main(String[] args) { - String carNames = InputView.getCarName(); - int tryNumber = InputView.getTryNumber(); - - List validCarNames = CarHelper.splitCarName(carNames); - - Cars cars = createCars(validCarNames); - - runRace(cars, tryNumber); - - Cars winners = - cars.determineWinners(); - ResultView.printWinners(winners); - } - - private static Cars createCars(List carNames) { - Cars cars = new Cars(); - - for (String carName : carNames) { - cars.addCars(new Car(0, carName)); - } - - return cars; - } - - private static void runRace(Cars cars, int tryNumber) { - ResultView.printStartMessage(); - for (int i = 0; i < tryNumber; i++) { - moveAllCars(cars); - - int roundNumber = i + 1; - ResultView.printRoundResult(roundNumber, cars); - } - } - - private static void moveAllCars(Cars cars) { - RacingHelper racingHelper = new RacingHelper(); - - for (Car car : cars.getCarList()) { - car.move(racingHelper.shouldMove()); - } + RacingService.startRacingGame(); } } \ No newline at end of file diff --git a/src/main/java/com/racing/service/RacingService.java b/src/main/java/com/racing/service/RacingService.java new file mode 100644 index 00000000000..f9bb512633d --- /dev/null +++ b/src/main/java/com/racing/service/RacingService.java @@ -0,0 +1,55 @@ +package com.racing.service; + +import com.racing.domain.Car; +import com.racing.domain.Cars; +import com.racing.ui.InputView; +import com.racing.ui.ResultView; +import com.racing.utils.CarHelper; +import com.racing.utils.RacingHelper; + +import java.util.List; + +public class RacingService { + public static void startRacingGame() { + String carNames = InputView.getCarName(); + int tryNumber = InputView.getTryNumber(); + + List validCarNames = CarHelper.splitCarName(carNames); + + Cars cars = initializeRacingCars(validCarNames); + + executeRaceRounds(cars, tryNumber); + + Cars winners = + cars.determineWinners(); + ResultView.printWinners(winners); + } + + private static Cars initializeRacingCars(List carNames) { + Cars cars = new Cars(); + + for (String carName : carNames) { + cars.addCars(new Car(0, carName)); + } + + return cars; + } + + private static void executeRaceRounds(Cars cars, int tryNumber) { + ResultView.printStartMessage(); + for (int i = 0; i < tryNumber; i++) { + advanceAllCars(cars); + + int roundNumber = i + 1; + ResultView.printRoundResult(roundNumber, cars); + } + } + + private static void advanceAllCars(Cars cars) { + RacingHelper racingHelper = new RacingHelper(); + + for (Car car : cars.getCarList()) { + car.move(racingHelper.shouldMove()); + } + } +} From 16ee9f83420bf8c8363a7f424dafa2aa650835ee Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 22 Oct 2024 19:00:55 +0900 Subject: [PATCH 26/32] =?UTF-8?q?refactor:=20sout=EC=9D=84=20view=20?= =?UTF-8?q?=EB=8B=A8=EC=9C=BC=EB=A1=9C=20=EB=B6=84=EB=A6=AC,=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9D=80=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Cars.java | 6 ------ src/main/java/com/racing/ui/ResultView.java | 5 ++++- src/test/java/racing/domain/CarsTest.java | 21 --------------------- 3 files changed, 4 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index 1579c11fac2..c0ed9e16eff 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -55,12 +55,6 @@ public String getCarName(int index) { return carList.get(index).displayCarName(); } - public void printRacingPositions() { - for (Car car : carList) { - System.out.println(car.displayRacingPosition()); - } - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/racing/ui/ResultView.java b/src/main/java/com/racing/ui/ResultView.java index 614e3df92ae..659f45e0a98 100644 --- a/src/main/java/com/racing/ui/ResultView.java +++ b/src/main/java/com/racing/ui/ResultView.java @@ -1,5 +1,6 @@ package com.racing.ui; +import com.racing.domain.Car; import com.racing.domain.Cars; public class ResultView { @@ -10,7 +11,9 @@ public static void printStartMessage() { public static void printRoundResult(int round, Cars cars) { System.out.println("# " + round + "회차"); - cars.printRacingPositions(); + for (Car car : cars.getCarList()) { + System.out.println(car.displayRacingPosition()); + } System.out.println(); } diff --git a/src/test/java/racing/domain/CarsTest.java b/src/test/java/racing/domain/CarsTest.java index fef79e5c5b3..d7142490ce5 100644 --- a/src/test/java/racing/domain/CarsTest.java +++ b/src/test/java/racing/domain/CarsTest.java @@ -5,8 +5,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; import java.util.Arrays; import java.util.List; @@ -91,23 +89,4 @@ void getCarNameTest() { assertThatThrownBy(() -> cars.getCarName(2)) .isInstanceOf(IndexOutOfBoundsException.class); } - - @Test - @DisplayName("printRacingPositions 메서드 테스트") - void printRacingPositionsTest() { - // Given - Car car1 = new Car(1, "pobi"); - Car car2 = new Car(3, "crong"); - Cars cars = new Cars(Arrays.asList(car1, car2)); - - ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outContent)); - - cars.printRacingPositions(); - - String expectedOutput = "pobi : -\ncrong : ---\n"; - assertThat(outContent.toString()).isEqualTo(expectedOutput); - - System.setOut(System.out); - } } From be64fc491b3497b91ccd7bf19a6812675f43a0ea Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 22 Oct 2024 19:07:02 +0900 Subject: [PATCH 27/32] =?UTF-8?q?refactor:=20carlist=20->=20caritems?= =?UTF-8?q?=EB=A1=9C=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EC=88=98=EC=A0=95,?= =?UTF-8?q?=20=EC=9D=BC=EA=B8=89=20=EA=B0=9D=EC=B2=B4=EC=9D=98=20item?= =?UTF-8?q?=EC=9D=84=20private=EB=A1=9C=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Cars.java | 26 +++++++++---------- .../com/racing/service/RacingService.java | 2 +- src/main/java/com/racing/ui/ResultView.java | 2 +- src/test/java/racing/domain/CarsTest.java | 4 +-- src/test/java/racing/utils/CarHelperTest.java | 8 +++--- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index c0ed9e16eff..61beda6d0fd 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -6,27 +6,27 @@ import java.util.stream.Collectors; public class Cars { - List carList; + private final List items; public Cars() { - carList = new ArrayList<>(); + items = new ArrayList<>(); } public Cars(List carList) { - this.carList = carList; + this.items = carList; } public void addCars(Car car) { - carList.add(car); + items.add(car); } - public List getCarList() { - return this.carList; + public List getItems() { + return this.items; } public int findMaxPosition() { int maxPosition = 0; - for (Car car : this.carList) { + for (Car car : this.items) { maxPosition = car.isDefeated(maxPosition) ? maxPosition : car.getPosition(); } @@ -36,7 +36,7 @@ public int findMaxPosition() { public Cars determineWinners() { int maxPosition = this.findMaxPosition(); - List winnerList = this.carList.stream() + List winnerList = this.items.stream() .filter(car -> car.getPosition() == maxPosition) .collect(Collectors.toList()); @@ -44,15 +44,15 @@ public Cars determineWinners() { } public int carsSize() { - return this.carList.size(); + return this.items.size(); } public String getCarName(int index) { - if (index < 0 || index >= carList.size()) { + if (index < 0 || index >= items.size()) { throw new IndexOutOfBoundsException("Invalid car index"); } - return carList.get(index).displayCarName(); + return items.get(index).displayCarName(); } @Override @@ -60,11 +60,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Cars cars = (Cars) o; - return Objects.equals(carList, cars.carList); + return Objects.equals(items, cars.items); } @Override public int hashCode() { - return Objects.hashCode(carList); + return Objects.hashCode(items); } } diff --git a/src/main/java/com/racing/service/RacingService.java b/src/main/java/com/racing/service/RacingService.java index f9bb512633d..b804d3dcb83 100644 --- a/src/main/java/com/racing/service/RacingService.java +++ b/src/main/java/com/racing/service/RacingService.java @@ -48,7 +48,7 @@ private static void executeRaceRounds(Cars cars, int tryNumber) { private static void advanceAllCars(Cars cars) { RacingHelper racingHelper = new RacingHelper(); - for (Car car : cars.getCarList()) { + for (Car car : cars.getItems()) { car.move(racingHelper.shouldMove()); } } diff --git a/src/main/java/com/racing/ui/ResultView.java b/src/main/java/com/racing/ui/ResultView.java index 659f45e0a98..c8c20b679d7 100644 --- a/src/main/java/com/racing/ui/ResultView.java +++ b/src/main/java/com/racing/ui/ResultView.java @@ -11,7 +11,7 @@ public static void printStartMessage() { public static void printRoundResult(int round, Cars cars) { System.out.println("# " + round + "회차"); - for (Car car : cars.getCarList()) { + for (Car car : cars.getItems()) { System.out.println(car.displayRacingPosition()); } diff --git a/src/test/java/racing/domain/CarsTest.java b/src/test/java/racing/domain/CarsTest.java index d7142490ce5..a4a70594153 100644 --- a/src/test/java/racing/domain/CarsTest.java +++ b/src/test/java/racing/domain/CarsTest.java @@ -33,8 +33,8 @@ public void addCarsTest() { cars.addCars(newCar); - assertThat(cars.getCarList()).hasSize(1); - assertThat(cars.getCarList().get(0)).isEqualTo(newCar); + assertThat(cars.getItems()).hasSize(1); + assertThat(cars.getItems().get(0)).isEqualTo(newCar); } @Test diff --git a/src/test/java/racing/utils/CarHelperTest.java b/src/test/java/racing/utils/CarHelperTest.java index b85b4e1d61f..c4768829965 100644 --- a/src/test/java/racing/utils/CarHelperTest.java +++ b/src/test/java/racing/utils/CarHelperTest.java @@ -46,8 +46,8 @@ void isEquals_SingleWinner() { Cars winners = cars.determineWinners(); - assertThat(winners.getCarList()).hasSize(1); - assertThat(winners.getCarList().get(0).displayCarName()).isEqualTo("car1"); + assertThat(winners.getItems()).hasSize(1); + assertThat(winners.getItems().get(0).displayCarName()).isEqualTo("car1"); } @Test @@ -60,7 +60,7 @@ void isEquals_MultipleWinner() { Cars winners = cars.determineWinners(); - assertThat(winners.getCarList()).hasSize(2); - assertThat(winners.getCarList()).extracting("name").containsExactlyInAnyOrder("car1", "car2"); + assertThat(winners.getItems()).hasSize(2); + assertThat(winners.getItems()).extracting("name").containsExactlyInAnyOrder("car1", "car2"); } } From 13b1a3cc789a5bfaca6503f5c1ba1bebd91ff09e Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 22 Oct 2024 19:07:36 +0900 Subject: [PATCH 28/32] =?UTF-8?q?refactor:=20=EB=B0=A9=EC=96=B4=EC=A0=81?= =?UTF-8?q?=20=EB=B3=B5=EC=82=AC=20=EA=B8=B0=EB=B2=95=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=97=AC=20get=EC=9C=BC=EB=A1=9C=20=EB=A6=AC=ED=84=B4?= =?UTF-8?q?=EB=B0=9B=EC=9D=80=20=EA=B0=92=EC=9D=84=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=ED=95=A0=20=EC=88=98=20=EC=97=86=EB=8F=84=EB=A1=9D=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 --- src/main/java/com/racing/domain/Cars.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index 61beda6d0fd..962205fd092 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -1,6 +1,7 @@ package com.racing.domain; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -21,7 +22,7 @@ public void addCars(Car car) { } public List getItems() { - return this.items; + return Collections.unmodifiableList(this.items); } public int findMaxPosition() { From 0c567b0b58f38c78ffd76ee7c8bc59cdec55dbab Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 22 Oct 2024 19:10:45 +0900 Subject: [PATCH 29/32] =?UTF-8?q?refactor:=20=EA=B0=84=EC=A0=91=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=ED=86=B5=ED=95=9C=20pri?= =?UTF-8?q?vate=20=ED=95=A8=EC=88=98=20=ED=85=8C=EC=8A=A4=ED=8A=B8,=20?= =?UTF-8?q?=EA=B8=B0=EC=A1=B4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/domain/Cars.java | 2 +- src/test/java/racing/domain/CarsTest.java | 27 ----------------------- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/src/main/java/com/racing/domain/Cars.java b/src/main/java/com/racing/domain/Cars.java index 962205fd092..5b144526a24 100644 --- a/src/main/java/com/racing/domain/Cars.java +++ b/src/main/java/com/racing/domain/Cars.java @@ -25,7 +25,7 @@ public List getItems() { return Collections.unmodifiableList(this.items); } - public int findMaxPosition() { + private int findMaxPosition() { int maxPosition = 0; for (Car car : this.items) { maxPosition = car.isDefeated(maxPosition) ? maxPosition : car.getPosition(); diff --git a/src/test/java/racing/domain/CarsTest.java b/src/test/java/racing/domain/CarsTest.java index a4a70594153..be8f72482ac 100644 --- a/src/test/java/racing/domain/CarsTest.java +++ b/src/test/java/racing/domain/CarsTest.java @@ -37,33 +37,6 @@ public void addCarsTest() { assertThat(cars.getItems().get(0)).isEqualTo(newCar); } - @Test - @DisplayName("가장 앞선 자동차의 위치를 정확히 찾는지 테스트") - void findMaxPositionTest() { - Car car1 = new Car(3, "car1"); - Car car2 = new Car(5, "car2"); - Car car3 = new Car(1, "car3"); - Cars cars = new Cars(Arrays.asList(car1, car2, car3)); - - int maxPosition = cars.findMaxPosition(); - - assertThat(maxPosition).isEqualTo(5); - } - - @Test - @DisplayName("모든 자동차가 같은 위치에 있을 때 정확한 위치를 반환하는지 테스트") - void findMaxPositionWhenAllCarsAtSamePosition() { - Car car1 = new Car(3, "car1"); - Car car2 = new Car(3, "car2"); - Car car3 = new Car(3, "car3"); - Cars cars = new Cars(Arrays.asList(car1, car2, car3)); - - int maxPosition = cars.findMaxPosition(); - - assertThat(maxPosition).isEqualTo(3); - } - - @Test @DisplayName("carsSize 메서드가 정확한 자동차 수를 반환하는지 테스트") void carsSizeTest() { From 8e329e852df983b77e4f8de8247d6a3257ad1a2f Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 22 Oct 2024 19:10:56 +0900 Subject: [PATCH 30/32] =?UTF-8?q?fix:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racing/domain/CarNameTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/racing/domain/CarNameTest.java b/src/test/java/racing/domain/CarNameTest.java index b7fabb1b21b..4ab8143311d 100644 --- a/src/test/java/racing/domain/CarNameTest.java +++ b/src/test/java/racing/domain/CarNameTest.java @@ -31,6 +31,6 @@ public void createCarNameWithEmptyNameThrowsException(String invalidName) { @DisplayName("null로 CarName 객체 생성 시 예외 발생") public void createCarNameWithNullThrowsException() { assertThatThrownBy(() -> new CarName(null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(IllegalArgumentException.class); } } From 81149cf043b915fa3446f9acc4f640493412611f Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 22 Oct 2024 19:33:00 +0900 Subject: [PATCH 31/32] =?UTF-8?q?refactor:=20=EC=9D=B8=ED=84=B0=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=EC=97=90=EC=84=9C=20=EA=B5=AC=EC=B2=B4?= =?UTF-8?q?=EC=A0=81=EC=9D=B8=20=EB=82=B4=EC=9A=A9=EC=9D=84=20=EB=B0=A9?= =?UTF-8?q?=ED=96=A5=EC=84=B1=EC=9D=84=20=EB=82=98=ED=83=80=EB=82=B4?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/{RandomStrategy.java => MovementStrategy.java} | 2 +- src/main/java/com/racing/utils/RacingHelper.java | 2 +- src/test/java/racing/utils/RandomStrategyMock.java | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename src/main/java/com/racing/utils/{RandomStrategy.java => MovementStrategy.java} (63%) diff --git a/src/main/java/com/racing/utils/RandomStrategy.java b/src/main/java/com/racing/utils/MovementStrategy.java similarity index 63% rename from src/main/java/com/racing/utils/RandomStrategy.java rename to src/main/java/com/racing/utils/MovementStrategy.java index 02ff497b953..2bf70b1ef9c 100644 --- a/src/main/java/com/racing/utils/RandomStrategy.java +++ b/src/main/java/com/racing/utils/MovementStrategy.java @@ -1,5 +1,5 @@ package com.racing.utils; -public interface RandomStrategy { +public interface MovementStrategy { public boolean shouldMove(); } diff --git a/src/main/java/com/racing/utils/RacingHelper.java b/src/main/java/com/racing/utils/RacingHelper.java index a9224ebbab2..9e778cfb9b5 100644 --- a/src/main/java/com/racing/utils/RacingHelper.java +++ b/src/main/java/com/racing/utils/RacingHelper.java @@ -5,7 +5,7 @@ /** * 레이싱에서 사용 할 헬퍼 메서드를 모아놓은 클래스 */ -public class RacingHelper implements RandomStrategy { +public class RacingHelper implements MovementStrategy { private static final int RANDOM_NUMBER_RANGE = 10; private static final int FORWARD_MINIMUM_NUMBER = 4; diff --git a/src/test/java/racing/utils/RandomStrategyMock.java b/src/test/java/racing/utils/RandomStrategyMock.java index 4d912a01c3d..f9d77e36a41 100644 --- a/src/test/java/racing/utils/RandomStrategyMock.java +++ b/src/test/java/racing/utils/RandomStrategyMock.java @@ -1,8 +1,8 @@ package racing.utils; -import com.racing.utils.RandomStrategy; +import com.racing.utils.MovementStrategy; -public class RandomStrategyMock implements RandomStrategy { +public class RandomStrategyMock implements MovementStrategy { @Override public boolean shouldMove() { return false; From 47bdbbccb04436541a66453ec91ce23fa369f5d3 Mon Sep 17 00:00:00 2001 From: jonghyeon Date: Tue, 22 Oct 2024 19:39:57 +0900 Subject: [PATCH 32/32] =?UTF-8?q?refactor:=20=EC=9E=90=EB=8F=99=EC=B0=A8?= =?UTF-8?q?=20=EC=9B=80=EC=A7=81=EC=9E=84=EC=97=90=20=ED=95=B4=EB=8B=B9?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B0=9D=EC=B2=B4=EB=A5=BC=20=EB=AA=85?= =?UTF-8?q?=ED=99=95=ED=95=98=EA=B2=8C=20=ED=91=9C=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/racing/service/RacingService.java | 6 +++--- .../racing/utils/{RacingHelper.java => CarMovement.java} | 2 +- src/test/java/racing/utils/RacingHelperTest.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/main/java/com/racing/utils/{RacingHelper.java => CarMovement.java} (93%) diff --git a/src/main/java/com/racing/service/RacingService.java b/src/main/java/com/racing/service/RacingService.java index b804d3dcb83..b102d86b7e9 100644 --- a/src/main/java/com/racing/service/RacingService.java +++ b/src/main/java/com/racing/service/RacingService.java @@ -5,7 +5,7 @@ import com.racing.ui.InputView; import com.racing.ui.ResultView; import com.racing.utils.CarHelper; -import com.racing.utils.RacingHelper; +import com.racing.utils.CarMovement; import java.util.List; @@ -46,10 +46,10 @@ private static void executeRaceRounds(Cars cars, int tryNumber) { } private static void advanceAllCars(Cars cars) { - RacingHelper racingHelper = new RacingHelper(); + CarMovement carMovement = new CarMovement(); for (Car car : cars.getItems()) { - car.move(racingHelper.shouldMove()); + car.move(carMovement.shouldMove()); } } } diff --git a/src/main/java/com/racing/utils/RacingHelper.java b/src/main/java/com/racing/utils/CarMovement.java similarity index 93% rename from src/main/java/com/racing/utils/RacingHelper.java rename to src/main/java/com/racing/utils/CarMovement.java index 9e778cfb9b5..9cdad0de80a 100644 --- a/src/main/java/com/racing/utils/RacingHelper.java +++ b/src/main/java/com/racing/utils/CarMovement.java @@ -5,7 +5,7 @@ /** * 레이싱에서 사용 할 헬퍼 메서드를 모아놓은 클래스 */ -public class RacingHelper implements MovementStrategy { +public class CarMovement implements MovementStrategy { private static final int RANDOM_NUMBER_RANGE = 10; private static final int FORWARD_MINIMUM_NUMBER = 4; diff --git a/src/test/java/racing/utils/RacingHelperTest.java b/src/test/java/racing/utils/RacingHelperTest.java index 32f0d17605c..18c9fdc8b7d 100644 --- a/src/test/java/racing/utils/RacingHelperTest.java +++ b/src/test/java/racing/utils/RacingHelperTest.java @@ -1,6 +1,6 @@ package racing.utils; -import com.racing.utils.RacingHelper; +import com.racing.utils.CarMovement; import com.warmup.step3.CarRacing; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -25,7 +25,7 @@ public void isBetween_RangeOfRandomNumber() { int expectedMinNum = 0; int expectedMaxNum = 10; - assertThat(RacingHelper.getRandomNumber()).isGreaterThanOrEqualTo(expectedMinNum) + assertThat(CarMovement.getRandomNumber()).isGreaterThanOrEqualTo(expectedMinNum) .isLessThan(expectedMaxNum); }