From 250dc42f69f7a45a130b10202d917af02bfb3405 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 16 Nov 2019 15:48:33 +0900 Subject: [PATCH 01/33] =?UTF-8?q?docs:=20=EA=B0=84=EB=8B=A8=ED=95=9C=20?= =?UTF-8?q?=EC=84=A4=EA=B3=84=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 268a597..52910ca 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,34 @@ + + +## 목표 + +- 무조건 TDD +- 자바8 활용 (for문 사용 x) +- SOLID 원칙 지키기. +- 응집도 높게 +- 불변으로 + + + +## 간단한 설계 + +- Car + - 4 이상 전진, 3 이하 정지 +- Name + - 2자 이상, 5자 이하 가능 +- Position + - 기본값 1 +- Cars + - `,` 기준으로 분리 후 `List` 생성 + +- MoveStrategy +- RandomeMoveStrategy + + + ## 객체지향 생활 체조 - 규칙 1: 한 메서드에 오직 한 단계의 들여쓰기만 한다. @@ -13,4 +41,5 @@ - 규칙 6: 모든 엔티티를 작게 유지한다. - 규칙 7: 2개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다. - 규칙 8: 일급 콜렉션을 쓴다. -- 규칙 9: 게터/세터/프로퍼티를 쓰지 않는다. \ No newline at end of file +- 규칙 9: 게터/세터/프로퍼티를 쓰지 않는다. + From 1f1b185bc3f327240649e1c2996619db2de75b13 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 16 Nov 2019 15:48:54 +0900 Subject: [PATCH 02/33] feat: Name VO --- src/main/java/racingcar/domain/Name.java | 39 ++++++++++++++++++++ src/test/java/racingcar/domain/NameTest.java | 27 ++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/main/java/racingcar/domain/Name.java create mode 100644 src/test/java/racingcar/domain/NameTest.java diff --git a/src/main/java/racingcar/domain/Name.java b/src/main/java/racingcar/domain/Name.java new file mode 100644 index 0000000..0a5343b --- /dev/null +++ b/src/main/java/racingcar/domain/Name.java @@ -0,0 +1,39 @@ +package racingcar.domain; + +import java.util.Objects; + +public class Name { + private static final int MAX_BOUNDARY = 5; + private static final int MIN_BOUNDARY = 2; + + private final String name; + + private Name(final String name) { + if (name.length() > MAX_BOUNDARY || name.length() < MIN_BOUNDARY) { + throw new IllegalArgumentException("이름은 2~5자로 해주세요."); + } + this.name = name; + } + + static Name of(final String name) { + return new Name(name); + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Name name1 = (Name) o; + return Objects.equals(name, name1.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + + @Override + public String toString() { + return "name= " + name; + } +} diff --git a/src/test/java/racingcar/domain/NameTest.java b/src/test/java/racingcar/domain/NameTest.java new file mode 100644 index 0000000..38a2cce --- /dev/null +++ b/src/test/java/racingcar/domain/NameTest.java @@ -0,0 +1,27 @@ +package racingcar.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class NameTest { + + @Test + void 논리적_동치성_테스트() { + final Name name1 = Name.of("베디"); + final Name name2 = Name.of("베디"); + + assertThat(name1).isEqualTo(name2); + } + + @Test + void 이름_2자_미만_예외처리() { + assertThrows(IllegalArgumentException.class, () -> Name.of("a")); + } + + @Test + void 이름_5자_초과_예외처리() { + assertThrows(IllegalArgumentException.class, () -> Name.of("abcdef")); + } +} \ No newline at end of file From 33bceb24148c7d43373e0b7c14527a413ff56538 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 16 Nov 2019 15:58:15 +0900 Subject: [PATCH 03/33] feat: Position VO --- src/main/java/racingcar/domain/Position.java | 29 +++++++++++++++++++ .../java/racingcar/domain/PositionTest.java | 18 ++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/racingcar/domain/Position.java create mode 100644 src/test/java/racingcar/domain/PositionTest.java diff --git a/src/main/java/racingcar/domain/Position.java b/src/main/java/racingcar/domain/Position.java new file mode 100644 index 0000000..6a11ce6 --- /dev/null +++ b/src/main/java/racingcar/domain/Position.java @@ -0,0 +1,29 @@ +package racingcar.domain; + +public class Position { + private static final int UNIT_INCREASE = 1; + static final int DEFAULT_POSITION = 0; + + private final int position; + + private Position(final int position) { + this.position = position; + } + + static Position newInstance() { + return new Position(DEFAULT_POSITION); + } + + Position increase() { + return new Position(position + UNIT_INCREASE); + } + + int getPosition() { + return position; + } + + @Override + public String toString() { + return "position= " + position; + } +} diff --git a/src/test/java/racingcar/domain/PositionTest.java b/src/test/java/racingcar/domain/PositionTest.java new file mode 100644 index 0000000..49004ce --- /dev/null +++ b/src/test/java/racingcar/domain/PositionTest.java @@ -0,0 +1,18 @@ +package racingcar.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static racingcar.domain.Position.DEFAULT_POSITION; + +class PositionTest { + + @Test + void 포지션_증가() { + Position position = Position.newInstance(); + Position increasedPosition = position.increase(); + int actual = increasedPosition.getPosition(); + + assertThat(actual).isEqualTo(DEFAULT_POSITION + 1); + } +} \ No newline at end of file From bcbe5b568c9ac9d2f413624ce85e37280fca261a Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 16 Nov 2019 17:20:59 +0900 Subject: [PATCH 04/33] feat: Car --- src/main/java/racingcar/domain/Car.java | 37 ++++++++++++++++++ .../java/racingcar/domain/MoveStrategy.java | 5 +++ src/main/java/racingcar/domain/Name.java | 4 ++ src/test/java/racingcar/domain/CarTest.java | 39 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 src/main/java/racingcar/domain/Car.java create mode 100644 src/main/java/racingcar/domain/MoveStrategy.java create mode 100644 src/test/java/racingcar/domain/CarTest.java diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java new file mode 100644 index 0000000..5ba5107 --- /dev/null +++ b/src/main/java/racingcar/domain/Car.java @@ -0,0 +1,37 @@ +package racingcar.domain; + +public class Car { + private final Name name; + private Position position; + + public Car(final String name) { + this.name = Name.of(name); + this.position = Position.newInstance(); + } + + public static Car of(final String name) { + return new Car(name); + } + + public void move(final MoveStrategy moveStrategy) { + if (moveStrategy.isAvailableMove()) { + position = position.increase(); + } + } + + public String getName() { + return name.getName(); + } + + public int getPosition() { + return position.getPosition(); + } + + @Override + public String toString() { + return "Car{" + + "name='" + name + '\'' + + ", position=" + position + + '}'; + } +} diff --git a/src/main/java/racingcar/domain/MoveStrategy.java b/src/main/java/racingcar/domain/MoveStrategy.java new file mode 100644 index 0000000..ca6843b --- /dev/null +++ b/src/main/java/racingcar/domain/MoveStrategy.java @@ -0,0 +1,5 @@ +package racingcar.domain; + +public interface MoveStrategy { + boolean isAvailableMove(); +} diff --git a/src/main/java/racingcar/domain/Name.java b/src/main/java/racingcar/domain/Name.java index 0a5343b..cad69b1 100644 --- a/src/main/java/racingcar/domain/Name.java +++ b/src/main/java/racingcar/domain/Name.java @@ -19,6 +19,10 @@ static Name of(final String name) { return new Name(name); } + public String getName() { + return name; + } + @Override public boolean equals(final Object o) { if (this == o) return true; diff --git a/src/test/java/racingcar/domain/CarTest.java b/src/test/java/racingcar/domain/CarTest.java new file mode 100644 index 0000000..72eddfe --- /dev/null +++ b/src/test/java/racingcar/domain/CarTest.java @@ -0,0 +1,39 @@ +package racingcar.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class CarTest { + private Car car = Car.of("베디"); + + + @Test + @DisplayName("이동 안함") + void moveTest1() { + // given + int expected = car.getPosition(); + + // when + car.move(() -> false); + int actual = car.getPosition(); + + // then + assertThat(actual).isEqualTo(expected); + } + + @Test + @DisplayName("이동") + void moveTest2() { + // given + int expected = car.getPosition()+1; + + // when + car.move(() -> true); + int actual = car.getPosition(); + + // then + assertThat(actual).isEqualTo(expected); + } +} \ No newline at end of file From 42111ac62364361b957d324a833671e26f537266 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 16 Nov 2019 18:55:17 +0900 Subject: [PATCH 05/33] =?UTF-8?q?feat:=20Cars=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EC=97=B4=EB=A1=9C=20List=20=EC=83=9D=EC=84=B1=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Cars.java | 21 ++++++++++++++++++++ src/test/java/racingcar/domain/CarsTest.java | 16 +++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/main/java/racingcar/domain/Cars.java create mode 100644 src/test/java/racingcar/domain/CarsTest.java diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java new file mode 100644 index 0000000..758dd79 --- /dev/null +++ b/src/main/java/racingcar/domain/Cars.java @@ -0,0 +1,21 @@ +package racingcar.domain; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Cars { + private static final String DELIMITER = ","; + + private final List cars; + + public Cars(final String names) { + this.cars = createCars(names); + } + + private List createCars(final String names) { + return Stream.of(names.split(DELIMITER)) + .map(Car::new) + .collect(Collectors.toList()); + } +} diff --git a/src/test/java/racingcar/domain/CarsTest.java b/src/test/java/racingcar/domain/CarsTest.java new file mode 100644 index 0000000..4f9272e --- /dev/null +++ b/src/test/java/racingcar/domain/CarsTest.java @@ -0,0 +1,16 @@ +package racingcar.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +class CarsTest { + + @Test + @DisplayName(",가 아닌 다른 delimiter를 사용한 경우") + void createTest() { + assertThrows(IllegalArgumentException.class, ()-> new Cars("pobi:crong.honux")); + } + +} \ No newline at end of file From 81b68747eaeba460fc70223f46e7d7b5555382ac Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 16 Nov 2019 19:32:24 +0900 Subject: [PATCH 06/33] =?UTF-8?q?refactor:=20MoveStrategy.isAvailableMove(?= =?UTF-8?q?)=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Car.java | 6 +++++- src/main/java/racingcar/domain/MoveStrategy.java | 2 +- src/test/java/racingcar/domain/CarTest.java | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index 5ba5107..9f925e7 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -14,11 +14,15 @@ public static Car of(final String name) { } public void move(final MoveStrategy moveStrategy) { - if (moveStrategy.isAvailableMove()) { + if (moveStrategy.isAvailableMove(this)) { position = position.increase(); } } + public boolean isSamePosition(int position){ + return getPosition() == position; + } + public String getName() { return name.getName(); } diff --git a/src/main/java/racingcar/domain/MoveStrategy.java b/src/main/java/racingcar/domain/MoveStrategy.java index ca6843b..3372da6 100644 --- a/src/main/java/racingcar/domain/MoveStrategy.java +++ b/src/main/java/racingcar/domain/MoveStrategy.java @@ -1,5 +1,5 @@ package racingcar.domain; public interface MoveStrategy { - boolean isAvailableMove(); + boolean isAvailableMove(final Car car); } diff --git a/src/test/java/racingcar/domain/CarTest.java b/src/test/java/racingcar/domain/CarTest.java index 72eddfe..9a743da 100644 --- a/src/test/java/racingcar/domain/CarTest.java +++ b/src/test/java/racingcar/domain/CarTest.java @@ -16,7 +16,7 @@ void moveTest1() { int expected = car.getPosition(); // when - car.move(() -> false); + car.move(x -> false); int actual = car.getPosition(); // then @@ -30,7 +30,7 @@ void moveTest2() { int expected = car.getPosition()+1; // when - car.move(() -> true); + car.move(x -> true); int actual = car.getPosition(); // then From 7ef12bb1a26267aae311f74ad57c4e6d5e75b8b4 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 16 Nov 2019 19:33:32 +0900 Subject: [PATCH 07/33] =?UTF-8?q?feat:=20Cars=20=EC=9D=B4=EB=8F=99=20?= =?UTF-8?q?=EB=B0=8F=20=EC=9A=B0=EC=8A=B9=20=EA=B5=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Cars.java | 21 ++++++++++++ src/test/java/racingcar/domain/CarsTest.java | 36 +++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 758dd79..8af4d64 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -1,5 +1,6 @@ package racingcar.domain; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -18,4 +19,24 @@ private List createCars(final String names) { .map(Car::new) .collect(Collectors.toList()); } + + public void move(final MoveStrategy moveStrategy) { + cars.forEach(car -> car.move(moveStrategy)); + } + + public List getCars() { + return cars; + } + + + public List getWinners() { + int maxOfPosition = cars.stream() + .max(Comparator.comparingInt(Car::getPosition)) + .get().getPosition(); + + return cars.stream() + .filter(car -> car.isSamePosition(maxOfPosition)) + .map(Car::getName) + .collect(Collectors.toList()); + } } diff --git a/src/test/java/racingcar/domain/CarsTest.java b/src/test/java/racingcar/domain/CarsTest.java index 4f9272e..4932229 100644 --- a/src/test/java/racingcar/domain/CarsTest.java +++ b/src/test/java/racingcar/domain/CarsTest.java @@ -3,6 +3,10 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.stream.Collectors; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; class CarsTest { @@ -10,7 +14,37 @@ class CarsTest { @Test @DisplayName(",가 아닌 다른 delimiter를 사용한 경우") void createTest() { - assertThrows(IllegalArgumentException.class, ()-> new Cars("pobi:crong.honux")); + assertThrows(IllegalArgumentException.class, () -> new Cars("pobi:crong.honux")); + } + + @Test + void 전체_이동_테스트() { + // given + Cars cars = new Cars("pobi,crong,honux"); + int movedPosition = Position.DEFAULT_POSITION + 1; + List expectedPosition = List.of(movedPosition, movedPosition, movedPosition); + + // when + cars.move(x -> true); + List actualPositions = cars.getCars().stream() + .map(Car::getPosition) + .collect(Collectors.toList()); + + // then + assertThat(actualPositions).isEqualTo(expectedPosition); } + @Test + void 우승자_구하기() { + // given + Cars cars = new Cars("pobi,crong,honux"); + cars.move(car -> car.getName().equals("pobi")); + cars.move(car -> car.getName().equals("crong")); + + // when + List winners = cars.getWinners(); + + // then + assertThat(winners).isEqualTo(List.of("pobi", "crong")); + } } \ No newline at end of file From e53805775aafdfe739c65c711b24de440855f292 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sat, 16 Nov 2019 19:35:38 +0900 Subject: [PATCH 08/33] =?UTF-8?q?feat:=20Cars=20=EC=9D=B4=EB=A6=84=202?= =?UTF-8?q?=EB=AA=85=20=EB=AF=B8=EB=A7=8C=20=EC=9E=85=EB=A0=A5=EC=8B=9C=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Cars.java | 4 ++++ src/test/java/racingcar/domain/CarsTest.java | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 8af4d64..2d07674 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -7,11 +7,15 @@ public class Cars { private static final String DELIMITER = ","; + private static final int MINIMUM_NAMES = 2; private final List cars; public Cars(final String names) { this.cars = createCars(names); + if (cars.size() < MINIMUM_NAMES) { + throw new IllegalArgumentException("2명 이상 입력해주세요."); + } } private List createCars(final String names) { diff --git a/src/test/java/racingcar/domain/CarsTest.java b/src/test/java/racingcar/domain/CarsTest.java index 4932229..ab85c70 100644 --- a/src/test/java/racingcar/domain/CarsTest.java +++ b/src/test/java/racingcar/domain/CarsTest.java @@ -17,6 +17,12 @@ void createTest() { assertThrows(IllegalArgumentException.class, () -> new Cars("pobi:crong.honux")); } + @Test + void 이름_입력_2명_미만_예외처리() { + assertThrows(IllegalArgumentException.class, () -> new Cars("pobi")); + + } + @Test void 전체_이동_테스트() { // given From 6b0ff662f79aa7a5b28b132e4d93ffdd0bfdd085 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 15:50:25 +0900 Subject: [PATCH 09/33] =?UTF-8?q?refactor:=20Car,=20Cars=20move()=20->=20s?= =?UTF-8?q?houldMove()=EB=A1=9C=20=EC=9D=B4=EB=A6=84=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/racingcar/domain/Car.java | 2 +- src/main/java/racingcar/domain/Cars.java | 4 ++-- src/test/java/racingcar/domain/CarTest.java | 8 ++++---- src/test/java/racingcar/domain/CarsTest.java | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index 9f925e7..c0e5de0 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -13,7 +13,7 @@ public static Car of(final String name) { return new Car(name); } - public void move(final MoveStrategy moveStrategy) { + public void shouldMove(final MoveStrategy moveStrategy) { if (moveStrategy.isAvailableMove(this)) { position = position.increase(); } diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 2d07674..20d9835 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -24,8 +24,8 @@ private List createCars(final String names) { .collect(Collectors.toList()); } - public void move(final MoveStrategy moveStrategy) { - cars.forEach(car -> car.move(moveStrategy)); + public void shouldMove(final MoveStrategy moveStrategy) { + cars.forEach(car -> car.shouldMove(moveStrategy)); } public List getCars() { diff --git a/src/test/java/racingcar/domain/CarTest.java b/src/test/java/racingcar/domain/CarTest.java index 9a743da..dfecddd 100644 --- a/src/test/java/racingcar/domain/CarTest.java +++ b/src/test/java/racingcar/domain/CarTest.java @@ -11,12 +11,12 @@ class CarTest { @Test @DisplayName("이동 안함") - void moveTest1() { + void shouldMoveTest1() { // given int expected = car.getPosition(); // when - car.move(x -> false); + car.shouldMove(x -> false); int actual = car.getPosition(); // then @@ -25,12 +25,12 @@ void moveTest1() { @Test @DisplayName("이동") - void moveTest2() { + void shouldMoveTest2() { // given int expected = car.getPosition()+1; // when - car.move(x -> true); + car.shouldMove(x -> true); int actual = car.getPosition(); // then diff --git a/src/test/java/racingcar/domain/CarsTest.java b/src/test/java/racingcar/domain/CarsTest.java index ab85c70..06b924c 100644 --- a/src/test/java/racingcar/domain/CarsTest.java +++ b/src/test/java/racingcar/domain/CarsTest.java @@ -31,7 +31,7 @@ void createTest() { List expectedPosition = List.of(movedPosition, movedPosition, movedPosition); // when - cars.move(x -> true); + cars.shouldMove(x -> true); List actualPositions = cars.getCars().stream() .map(Car::getPosition) .collect(Collectors.toList()); @@ -44,8 +44,8 @@ void createTest() { void 우승자_구하기() { // given Cars cars = new Cars("pobi,crong,honux"); - cars.move(car -> car.getName().equals("pobi")); - cars.move(car -> car.getName().equals("crong")); + cars.shouldMove(car -> car.getName().equals("pobi")); + cars.shouldMove(car -> car.getName().equals("crong")); // when List winners = cars.getWinners(); From 1323fbe09c8d5b69a26a69e0eafb818a611b4726 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 16:07:08 +0900 Subject: [PATCH 10/33] =?UTF-8?q?feat:=20RepeatNumber=20(=EC=8B=9C?= =?UTF-8?q?=EB=8F=84=ED=95=A0=20=ED=9A=8C=EC=88=98=EA=B0=92=20=ED=8F=AC?= =?UTF-8?q?=EC=9E=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/racingcar/domain/RepeatNumber.java | 27 +++++++++++++++++ .../racingcar/domain/RepeatNumberTest.java | 29 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/racingcar/domain/RepeatNumber.java create mode 100644 src/test/java/racingcar/domain/RepeatNumberTest.java diff --git a/src/main/java/racingcar/domain/RepeatNumber.java b/src/main/java/racingcar/domain/RepeatNumber.java new file mode 100644 index 0000000..7e2728a --- /dev/null +++ b/src/main/java/racingcar/domain/RepeatNumber.java @@ -0,0 +1,27 @@ +package racingcar.domain; + +public class RepeatNumber { + private static final int NUMBER_BOUNDARY = 1; + + private final int number; + + public RepeatNumber(final int number) { + if (number < NUMBER_BOUNDARY) { + throw new IllegalArgumentException("1 이상 입력해주세요."); + } + this.number = number; + } + + public static RepeatNumber from(final int number) { + return new RepeatNumber(number); + } + + + public static RepeatNumber from(final String number) { + return new RepeatNumber(Integer.parseInt(number)); + } + + public int getNumber() { + return number; + } +} diff --git a/src/test/java/racingcar/domain/RepeatNumberTest.java b/src/test/java/racingcar/domain/RepeatNumberTest.java new file mode 100644 index 0000000..94003b5 --- /dev/null +++ b/src/test/java/racingcar/domain/RepeatNumberTest.java @@ -0,0 +1,29 @@ +package racingcar.domain; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class RepeatNumberTest { + + @Test + void 음수_예외처리() { + assertThrows(IllegalArgumentException.class, () -> RepeatNumber.from(-1)); + } + + @Test + void 숫자_0_예외처리() { + assertThrows(IllegalArgumentException.class, () -> RepeatNumber.from(0)); + } + + @Test + void 숫자_67_정상입력() { + assertDoesNotThrow(() -> RepeatNumber.from(67)); + } + + @Test + void 문자로_숫자_입력_변환_처리() { + assertDoesNotThrow(() -> RepeatNumber.from("67")); + } +} \ No newline at end of file From b738501c75f8e5c8888a00e5aff66ef5ce442f2b Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 16:16:21 +0900 Subject: [PATCH 11/33] =?UTF-8?q?refactor:=20Cars.shouldMove()=20=EB=A6=AC?= =?UTF-8?q?=ED=84=B4=ED=83=80=EC=9E=85=20void=20->=20List=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Cars.java | 9 +++------ src/test/java/racingcar/domain/CarsTest.java | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 20d9835..12595b7 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -1,5 +1,6 @@ package racingcar.domain; +import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -24,15 +25,11 @@ private List createCars(final String names) { .collect(Collectors.toList()); } - public void shouldMove(final MoveStrategy moveStrategy) { + public List shouldMove(final MoveStrategy moveStrategy) { cars.forEach(car -> car.shouldMove(moveStrategy)); + return Collections.unmodifiableList(cars); } - public List getCars() { - return cars; - } - - public List getWinners() { int maxOfPosition = cars.stream() .max(Comparator.comparingInt(Car::getPosition)) diff --git a/src/test/java/racingcar/domain/CarsTest.java b/src/test/java/racingcar/domain/CarsTest.java index 06b924c..7f7a96b 100644 --- a/src/test/java/racingcar/domain/CarsTest.java +++ b/src/test/java/racingcar/domain/CarsTest.java @@ -31,8 +31,8 @@ void createTest() { List expectedPosition = List.of(movedPosition, movedPosition, movedPosition); // when - cars.shouldMove(x -> true); - List actualPositions = cars.getCars().stream() + final List movedCars = cars.shouldMove(x -> true); + List actualPositions = movedCars.stream() .map(Car::getPosition) .collect(Collectors.toList()); From f45db21d435740327c47a93c0c0c98d2178dbfb2 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 17:01:42 +0900 Subject: [PATCH 12/33] feat: RaceStatus, CarDto MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 현재 자동차 상황 - 현재 선두 자동차 구하기 --- .../java/racingcar/domain/RaceStatus.java | 33 +++++++++++++++++++ .../java/racingcar/service/dto/CarDto.java | 33 +++++++++++++++++++ .../java/racingcar/domain/RaceStatusTest.java | 29 ++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/main/java/racingcar/domain/RaceStatus.java create mode 100644 src/main/java/racingcar/service/dto/CarDto.java create mode 100644 src/test/java/racingcar/domain/RaceStatusTest.java diff --git a/src/main/java/racingcar/domain/RaceStatus.java b/src/main/java/racingcar/domain/RaceStatus.java new file mode 100644 index 0000000..d1807c4 --- /dev/null +++ b/src/main/java/racingcar/domain/RaceStatus.java @@ -0,0 +1,33 @@ +package racingcar.domain; + +import racingcar.service.dto.CarDto; + +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class RaceStatus { + private final List carDtos; + + public RaceStatus(final List carDtos) { + this.carDtos = carDtos; + } + + public static RaceStatus from(final List carDtos) { + return new RaceStatus(carDtos); + } + + public List getCurrentWinners() { + final int maxOfPosition = carDtos.stream() + .max(Comparator.comparingInt(CarDto::getPosition)) + .get().getPosition(); + + return carDtos.stream() + .filter(carDto -> carDto.getPosition() == maxOfPosition) + .collect(Collectors.toList()); + } + + public List getCurrentRaceStatus() { + return carDtos; + } +} diff --git a/src/main/java/racingcar/service/dto/CarDto.java b/src/main/java/racingcar/service/dto/CarDto.java new file mode 100644 index 0000000..66db7fa --- /dev/null +++ b/src/main/java/racingcar/service/dto/CarDto.java @@ -0,0 +1,33 @@ +package racingcar.service.dto; + +import racingcar.domain.Car; + +public class CarDto { + private final String name; + private final int position; + + public CarDto(final String name, final int position) { + this.name = name; + this.position = position; + } + + public static CarDto from(final Car car) { + return new CarDto(car.getName(), car.getPosition()); + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + @Override + public String toString() { + return "CarDto{" + + "name='" + name + '\'' + + ", position=" + position + + '}'; + } +} diff --git a/src/test/java/racingcar/domain/RaceStatusTest.java b/src/test/java/racingcar/domain/RaceStatusTest.java new file mode 100644 index 0000000..8f8256a --- /dev/null +++ b/src/test/java/racingcar/domain/RaceStatusTest.java @@ -0,0 +1,29 @@ +package racingcar.domain; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import racingcar.service.dto.CarDto; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class RaceStatusTest { + + @Test + @DisplayName("현재 선두 자동차 구하기") + void getWinnersTest() { + // given + CarDto first = new CarDto("first", 2); + CarDto second = new CarDto("second", 1); + List cars = List.of(first, second); + RaceStatus raceStatus = RaceStatus.from(cars); + + // when + List winners = raceStatus.getCurrentWinners(); + + // then + assertThat(winners).hasSize(1); + assertThat(winners.get(0).getName()).isEqualTo("first"); + } +} \ No newline at end of file From 48d6c6ee934b0b7d3afb09e49c63a7d3f5d0798d Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 17:02:11 +0900 Subject: [PATCH 13/33] =?UTF-8?q?refactor:=20Cars.getWinners()=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20->=20RaceStatus=20=EB=A1=9C=20=EC=9C=84=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Cars.java | 12 ------------ src/test/java/racingcar/domain/CarsTest.java | 14 -------------- 2 files changed, 26 deletions(-) diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/Cars.java index 12595b7..2b93ae1 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/Cars.java @@ -1,7 +1,6 @@ package racingcar.domain; import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -29,15 +28,4 @@ public List shouldMove(final MoveStrategy moveStrategy) { cars.forEach(car -> car.shouldMove(moveStrategy)); return Collections.unmodifiableList(cars); } - - public List getWinners() { - int maxOfPosition = cars.stream() - .max(Comparator.comparingInt(Car::getPosition)) - .get().getPosition(); - - return cars.stream() - .filter(car -> car.isSamePosition(maxOfPosition)) - .map(Car::getName) - .collect(Collectors.toList()); - } } diff --git a/src/test/java/racingcar/domain/CarsTest.java b/src/test/java/racingcar/domain/CarsTest.java index 7f7a96b..77b1c43 100644 --- a/src/test/java/racingcar/domain/CarsTest.java +++ b/src/test/java/racingcar/domain/CarsTest.java @@ -39,18 +39,4 @@ void createTest() { // then assertThat(actualPositions).isEqualTo(expectedPosition); } - - @Test - void 우승자_구하기() { - // given - Cars cars = new Cars("pobi,crong,honux"); - cars.shouldMove(car -> car.getName().equals("pobi")); - cars.shouldMove(car -> car.getName().equals("crong")); - - // when - List winners = cars.getWinners(); - - // then - assertThat(winners).isEqualTo(List.of("pobi", "crong")); - } } \ No newline at end of file From 5aa31ed4acede00393b75adf9c99c73a135f7aa1 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 17:18:18 +0900 Subject: [PATCH 14/33] feat: RaceResult --- .../java/racingcar/domain/RaceResult.java | 22 +++++++++++++ .../java/racingcar/domain/RaceResultTest.java | 32 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/main/java/racingcar/domain/RaceResult.java create mode 100644 src/test/java/racingcar/domain/RaceResultTest.java diff --git a/src/main/java/racingcar/domain/RaceResult.java b/src/main/java/racingcar/domain/RaceResult.java new file mode 100644 index 0000000..e1bc534 --- /dev/null +++ b/src/main/java/racingcar/domain/RaceResult.java @@ -0,0 +1,22 @@ +package racingcar.domain; + +import racingcar.service.dto.CarDto; + +import java.util.List; + +public class RaceResult { + private final List raceStatuses; + + public RaceResult(final List raceStatuses) { + this.raceStatuses = raceStatuses; + } + + public List getWinners() { + final int lastIndex = raceStatuses.size() - 1; + return raceStatuses.get(lastIndex).getCurrentWinners(); + } + + public List getRaceStatuses() { + return raceStatuses; + } +} diff --git a/src/test/java/racingcar/domain/RaceResultTest.java b/src/test/java/racingcar/domain/RaceResultTest.java new file mode 100644 index 0000000..1fe7090 --- /dev/null +++ b/src/test/java/racingcar/domain/RaceResultTest.java @@ -0,0 +1,32 @@ +package racingcar.domain; + +import org.junit.jupiter.api.Test; +import racingcar.service.dto.CarDto; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class RaceResultTest { + + private CarDto first1 = new CarDto("first", 2); + private CarDto second1 = new CarDto("second", 1); + private List cars1 = List.of(first1, second1); + + private CarDto first2 = new CarDto("first", 3); + private CarDto second2 = new CarDto("second", 2); + private List cars2 = List.of(first2, second2); + + private List raceStatuses = List.of(new RaceStatus(cars1), new RaceStatus(cars2)); + + private RaceResult raceResult = new RaceResult(raceStatuses); + + @Test + void getWinnersTest() { + List expected = List.of(this.first2); + + List winners = raceResult.getWinners(); + + assertThat(winners).isEqualTo(expected); + } +} \ No newline at end of file From 7f9766b04f15b4e6cc57cb2837f3ebdbbf7360a4 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 17:56:21 +0900 Subject: [PATCH 15/33] =?UTF-8?q?feat:=20RaceResult=20Iterator=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/RaceResult.java | 17 ++++++++++++++--- src/main/java/racingcar/service/dto/CarDto.java | 16 ++++++++++++++++ .../java/racingcar/domain/RaceResultTest.java | 7 +++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/racingcar/domain/RaceResult.java b/src/main/java/racingcar/domain/RaceResult.java index e1bc534..ebb581e 100644 --- a/src/main/java/racingcar/domain/RaceResult.java +++ b/src/main/java/racingcar/domain/RaceResult.java @@ -2,13 +2,16 @@ import racingcar.service.dto.CarDto; +import java.util.Iterator; import java.util.List; -public class RaceResult { +public class RaceResult implements Iterator { private final List raceStatuses; + private int cursor; public RaceResult(final List raceStatuses) { this.raceStatuses = raceStatuses; + this.cursor = 0; } public List getWinners() { @@ -16,7 +19,15 @@ public List getWinners() { return raceStatuses.get(lastIndex).getCurrentWinners(); } - public List getRaceStatuses() { - return raceStatuses; + @Override + public boolean hasNext() { + return raceStatuses.size() > cursor; + } + + @Override + public List next() { + final List currentRaceStatus = raceStatuses.get(cursor).getCurrentRaceStatus(); + cursor++; + return currentRaceStatus; } } diff --git a/src/main/java/racingcar/service/dto/CarDto.java b/src/main/java/racingcar/service/dto/CarDto.java index 66db7fa..3164813 100644 --- a/src/main/java/racingcar/service/dto/CarDto.java +++ b/src/main/java/racingcar/service/dto/CarDto.java @@ -2,6 +2,8 @@ import racingcar.domain.Car; +import java.util.Objects; + public class CarDto { private final String name; private final int position; @@ -23,6 +25,20 @@ public int getPosition() { return position; } + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final CarDto carDto = (CarDto) o; + return position == carDto.position && + Objects.equals(name, carDto.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, position); + } + @Override public String toString() { return "CarDto{" + diff --git a/src/test/java/racingcar/domain/RaceResultTest.java b/src/test/java/racingcar/domain/RaceResultTest.java index 1fe7090..43a0dd1 100644 --- a/src/test/java/racingcar/domain/RaceResultTest.java +++ b/src/test/java/racingcar/domain/RaceResultTest.java @@ -29,4 +29,11 @@ void getWinnersTest() { assertThat(winners).isEqualTo(expected); } + + @Test + void IteratorTest() { + assertThat(raceResult.next()).isEqualTo(cars1); + assertThat(raceResult.next()).isEqualTo(cars2); + assertThat(raceResult.hasNext()).isFalse(); + } } \ No newline at end of file From 506181563577dc5a9dd83956f6814450d092acfd Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 17:56:58 +0900 Subject: [PATCH 16/33] feat: RacingService --- .../java/racingcar/service/RacingService.java | 33 +++++++++++ .../racingcar/service/RacingServiceTest.java | 55 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/main/java/racingcar/service/RacingService.java create mode 100644 src/test/java/racingcar/service/RacingServiceTest.java diff --git a/src/main/java/racingcar/service/RacingService.java b/src/main/java/racingcar/service/RacingService.java new file mode 100644 index 0000000..232e2a4 --- /dev/null +++ b/src/main/java/racingcar/service/RacingService.java @@ -0,0 +1,33 @@ +package racingcar.service; + +import racingcar.domain.*; +import racingcar.service.dto.CarDto; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class RacingService { + private final MoveStrategy moveStrategy; + + public RacingService(final MoveStrategy moveStrategy) { + this.moveStrategy = moveStrategy; + } + + public Cars createCars(final String names) { + return new Cars(names); + } + + public RaceResult startRace(final int number, final Cars cars) { + final RepeatNumber repeatNumber = RepeatNumber.from(number); + + final List raceStatuses = new ArrayList<>(); + for (int i = 0; i < repeatNumber.getNumber(); i++) { + final List movedCars = cars.shouldMove(moveStrategy); + final List carDtos = movedCars.stream().map(CarDto::from).collect(Collectors.toList()); + raceStatuses.add(new RaceStatus(carDtos)); + } + + return new RaceResult(raceStatuses); + } +} diff --git a/src/test/java/racingcar/service/RacingServiceTest.java b/src/test/java/racingcar/service/RacingServiceTest.java new file mode 100644 index 0000000..360a90d --- /dev/null +++ b/src/test/java/racingcar/service/RacingServiceTest.java @@ -0,0 +1,55 @@ +package racingcar.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import racingcar.domain.Cars; +import racingcar.domain.RaceResult; +import racingcar.service.dto.CarDto; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class RacingServiceTest { + private RacingService racingService; + private Cars cars; + + @BeforeEach + void setUp() { + racingService = new RacingService(x -> true); + cars = racingService.createCars("pobi,crong"); + } + + @Test + void createCarsTest() { + assertThat(cars.shouldMove(x -> true)).isNotEmpty(); + } + + @Test + @DisplayName("경주 후 우승자 구하기") + void startRaceTest1() { + List expected = List.of(new CarDto("pobi", 3), new CarDto("crong", 3)); + int repeatNumber = 3; + + RaceResult raceResult = racingService.startRace(repeatNumber, cars); + + assertThat(raceResult.getWinners()).isEqualTo(expected); + } + + @Test + @DisplayName("경주 후 상태 확인") + void startRaceTest2() { + // given + List expected1 = List.of(new CarDto("pobi", 1), new CarDto("crong", 1)); + List expected2 = List.of(new CarDto("pobi", 2), new CarDto("crong", 2)); + + // when + RaceResult raceResult = racingService.startRace(2, cars); + + // then + assertThat(raceResult.next()).isEqualTo(expected1); + assertThat(raceResult.next()).isEqualTo(expected2); + assertThat(raceResult.hasNext()).isFalse(); + } +} \ No newline at end of file From 0dd39e3508a1d2f055ad0976c36d1193265d7ef8 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 18:01:47 +0900 Subject: [PATCH 17/33] feat: RandomNumberMoveStrategy --- .../domain/RandomNumberMoveStrategy.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/racingcar/domain/RandomNumberMoveStrategy.java diff --git a/src/main/java/racingcar/domain/RandomNumberMoveStrategy.java b/src/main/java/racingcar/domain/RandomNumberMoveStrategy.java new file mode 100644 index 0000000..9e8c204 --- /dev/null +++ b/src/main/java/racingcar/domain/RandomNumberMoveStrategy.java @@ -0,0 +1,15 @@ +package racingcar.domain; + +import java.util.Random; + +public class RandomNumberMoveStrategy implements MoveStrategy { + private static final int MOVE_BOUNDARY = 4; + private static final int RANDOM_MAX_BOUNDARY = 9; + + private final Random random = new Random(); + + @Override + public boolean isAvailableMove(final Car car) { + return random.nextInt(RANDOM_MAX_BOUNDARY + 1) >= MOVE_BOUNDARY; + } +} From 094219133d103865dff4d944674b7c8c21b8dbd1 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 18:23:12 +0900 Subject: [PATCH 18/33] =?UTF-8?q?feat:=20View,=20Main=20=EB=A9=94=EC=86=8C?= =?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/racingcar/console/App.java | 25 +++++++++++ .../racingcar/console/view/InputView.java | 20 +++++++++ .../racingcar/console/view/OutputView.java | 45 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/main/java/racingcar/console/App.java create mode 100644 src/main/java/racingcar/console/view/InputView.java create mode 100644 src/main/java/racingcar/console/view/OutputView.java diff --git a/src/main/java/racingcar/console/App.java b/src/main/java/racingcar/console/App.java new file mode 100644 index 0000000..2899603 --- /dev/null +++ b/src/main/java/racingcar/console/App.java @@ -0,0 +1,25 @@ +package racingcar.console; + +import racingcar.console.view.InputView; +import racingcar.console.view.OutputView; +import racingcar.domain.Cars; +import racingcar.domain.MoveStrategy; +import racingcar.domain.RaceResult; +import racingcar.domain.RandomNumberMoveStrategy; +import racingcar.service.RacingService; + +public class App { + public static void main(String[] args) { + final MoveStrategy moveStrategy = new RandomNumberMoveStrategy(); + final RacingService racingService = new RacingService(moveStrategy); + + final String names = InputView.inputNames(); + final Cars cars = racingService.createCars(names); + + final int repeatNumber = InputView.inputRepeatNumber(); + final RaceResult raceResult = racingService.startRace(repeatNumber, cars); + + OutputView.printRaceResult(raceResult); + OutputView.printWinner(raceResult.getWinners()); + } +} diff --git a/src/main/java/racingcar/console/view/InputView.java b/src/main/java/racingcar/console/view/InputView.java new file mode 100644 index 0000000..358c11e --- /dev/null +++ b/src/main/java/racingcar/console/view/InputView.java @@ -0,0 +1,20 @@ +package racingcar.console.view; + +import java.util.Scanner; + +public class InputView { + public static final Scanner scanner = new Scanner(System.in); + + private InputView() { + } + + public static String inputNames() { + System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)"); + return scanner.nextLine(); + } + + public static int inputRepeatNumber() { + System.out.println("시도할 회수는 몇회인가요?"); + return scanner.nextInt(); + } +} diff --git a/src/main/java/racingcar/console/view/OutputView.java b/src/main/java/racingcar/console/view/OutputView.java new file mode 100644 index 0000000..80dae2e --- /dev/null +++ b/src/main/java/racingcar/console/view/OutputView.java @@ -0,0 +1,45 @@ +package racingcar.console.view; + +import racingcar.domain.RaceResult; +import racingcar.service.dto.CarDto; + +import java.util.List; +import java.util.stream.Collectors; + +public class OutputView { + + private static final String ROUTE = "-"; + + private OutputView() { + } + + public static void printRaceResult(final RaceResult raceResult) { + StringBuilder sb = new StringBuilder(); + sb.append("실행 결과\n"); + while (raceResult.hasNext()) { + printCurrentRace(raceResult, sb); + } + System.out.println(sb.toString()); + } + + private static void printCurrentRace(final RaceResult raceResult, final StringBuilder sb) { + final List carDtos = raceResult.next(); + carDtos.forEach(carDto -> { + sb.append(String.format("%s : ", carDto.getName())); + for (int i = 0; i < carDto.getPosition(); i++) { + sb.append(ROUTE); + } + sb.append("\n"); + }); + sb.append("\n"); + } + + + public static void printWinner(final List winners) { + final String joinedWinners = winners.stream() + .map(CarDto::getName) + .collect(Collectors.joining(", ")); + + System.out.println(String.format("%s가 최종 우승했습니다.", joinedWinners)); + } +} From 531f3559d00e46b8802b4f5b8e69a7ee32164d37 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 18:23:56 +0900 Subject: [PATCH 19/33] =?UTF-8?q?style:=20=ED=8F=AC=EB=A7=B7=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/Car.java | 2 +- src/test/java/racingcar/domain/CarTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/Car.java index c0e5de0..a1bcd5d 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/Car.java @@ -19,7 +19,7 @@ public void shouldMove(final MoveStrategy moveStrategy) { } } - public boolean isSamePosition(int position){ + public boolean isSamePosition(int position) { return getPosition() == position; } diff --git a/src/test/java/racingcar/domain/CarTest.java b/src/test/java/racingcar/domain/CarTest.java index dfecddd..2d5a0be 100644 --- a/src/test/java/racingcar/domain/CarTest.java +++ b/src/test/java/racingcar/domain/CarTest.java @@ -27,7 +27,7 @@ void shouldMoveTest1() { @DisplayName("이동") void shouldMoveTest2() { // given - int expected = car.getPosition()+1; + int expected = car.getPosition() + 1; // when car.shouldMove(x -> true); From 5d5896582a0da5260b73c78afb4e6e2ca0f686a2 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 18:30:10 +0900 Subject: [PATCH 20/33] =?UTF-8?q?refactor:=20OutputView=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../racingcar/console/view/OutputView.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/racingcar/console/view/OutputView.java b/src/main/java/racingcar/console/view/OutputView.java index 80dae2e..76f522a 100644 --- a/src/main/java/racingcar/console/view/OutputView.java +++ b/src/main/java/racingcar/console/view/OutputView.java @@ -17,20 +17,21 @@ public static void printRaceResult(final RaceResult raceResult) { StringBuilder sb = new StringBuilder(); sb.append("실행 결과\n"); while (raceResult.hasNext()) { - printCurrentRace(raceResult, sb); + printCurrentRace(raceResult.next(), sb); } System.out.println(sb.toString()); } - private static void printCurrentRace(final RaceResult raceResult, final StringBuilder sb) { - final List carDtos = raceResult.next(); - carDtos.forEach(carDto -> { - sb.append(String.format("%s : ", carDto.getName())); - for (int i = 0; i < carDto.getPosition(); i++) { - sb.append(ROUTE); - } - sb.append("\n"); - }); + private static void printCurrentRace(final List carDtos, final StringBuilder sb) { + carDtos.forEach(carDto -> printCar(sb, carDto)); + sb.append("\n"); + } + + private static void printCar(final StringBuilder sb, final CarDto carDto) { + sb.append(String.format("%s : ", carDto.getName())); + for (int i = 0; i < carDto.getPosition(); i++) { + sb.append(ROUTE); + } sb.append("\n"); } From b083a2beb5a63e41c4da2a15344d72926d4ef4f2 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 22:57:03 +0900 Subject: [PATCH 21/33] =?UTF-8?q?refactor:=20domain=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EC=84=B8=EB=B6=84=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/console/App.java | 8 ++++---- src/main/java/racingcar/console/view/OutputView.java | 2 +- src/main/java/racingcar/domain/{ => car}/Car.java | 4 +++- src/main/java/racingcar/domain/{ => car}/Cars.java | 4 +++- src/main/java/racingcar/domain/{ => car}/Name.java | 2 +- src/main/java/racingcar/domain/{ => car}/Position.java | 2 +- .../java/racingcar/domain/{ => common}/RepeatNumber.java | 2 +- .../racingcar/domain/{ => movestrategy}/MoveStrategy.java | 4 +++- .../{ => movestrategy}/RandomNumberMoveStrategy.java | 4 +++- src/main/java/racingcar/domain/{ => race}/RaceResult.java | 2 +- src/main/java/racingcar/domain/{ => race}/RaceStatus.java | 2 +- src/main/java/racingcar/service/RacingService.java | 7 ++++++- src/main/java/racingcar/service/dto/CarDto.java | 2 +- src/test/java/racingcar/domain/{ => car}/CarTest.java | 2 +- src/test/java/racingcar/domain/{ => car}/CarsTest.java | 2 +- src/test/java/racingcar/domain/{ => car}/NameTest.java | 2 +- .../java/racingcar/domain/{ => car}/PositionTest.java | 4 ++-- .../racingcar/domain/{ => common}/RepeatNumberTest.java | 2 +- .../java/racingcar/domain/{ => race}/RaceResultTest.java | 2 +- .../java/racingcar/domain/{ => race}/RaceStatusTest.java | 2 +- src/test/java/racingcar/service/RacingServiceTest.java | 4 ++-- 21 files changed, 39 insertions(+), 26 deletions(-) rename src/main/java/racingcar/domain/{ => car}/Car.java (91%) rename src/main/java/racingcar/domain/{ => car}/Cars.java (91%) rename src/main/java/racingcar/domain/{ => car}/Name.java (97%) rename src/main/java/racingcar/domain/{ => car}/Position.java (94%) rename src/main/java/racingcar/domain/{ => common}/RepeatNumber.java (94%) rename src/main/java/racingcar/domain/{ => movestrategy}/MoveStrategy.java (51%) rename src/main/java/racingcar/domain/{ => movestrategy}/RandomNumberMoveStrategy.java (84%) rename src/main/java/racingcar/domain/{ => race}/RaceResult.java (96%) rename src/main/java/racingcar/domain/{ => race}/RaceStatus.java (96%) rename src/test/java/racingcar/domain/{ => car}/CarTest.java (96%) rename src/test/java/racingcar/domain/{ => car}/CarsTest.java (97%) rename src/test/java/racingcar/domain/{ => car}/NameTest.java (95%) rename src/test/java/racingcar/domain/{ => car}/PositionTest.java (80%) rename src/test/java/racingcar/domain/{ => common}/RepeatNumberTest.java (95%) rename src/test/java/racingcar/domain/{ => race}/RaceResultTest.java (97%) rename src/test/java/racingcar/domain/{ => race}/RaceStatusTest.java (96%) diff --git a/src/main/java/racingcar/console/App.java b/src/main/java/racingcar/console/App.java index 2899603..ca747b5 100644 --- a/src/main/java/racingcar/console/App.java +++ b/src/main/java/racingcar/console/App.java @@ -2,10 +2,10 @@ import racingcar.console.view.InputView; import racingcar.console.view.OutputView; -import racingcar.domain.Cars; -import racingcar.domain.MoveStrategy; -import racingcar.domain.RaceResult; -import racingcar.domain.RandomNumberMoveStrategy; +import racingcar.domain.car.Cars; +import racingcar.domain.movestrategy.MoveStrategy; +import racingcar.domain.movestrategy.RandomNumberMoveStrategy; +import racingcar.domain.race.RaceResult; import racingcar.service.RacingService; public class App { diff --git a/src/main/java/racingcar/console/view/OutputView.java b/src/main/java/racingcar/console/view/OutputView.java index 76f522a..ab13a1b 100644 --- a/src/main/java/racingcar/console/view/OutputView.java +++ b/src/main/java/racingcar/console/view/OutputView.java @@ -1,6 +1,6 @@ package racingcar.console.view; -import racingcar.domain.RaceResult; +import racingcar.domain.race.RaceResult; import racingcar.service.dto.CarDto; import java.util.List; diff --git a/src/main/java/racingcar/domain/Car.java b/src/main/java/racingcar/domain/car/Car.java similarity index 91% rename from src/main/java/racingcar/domain/Car.java rename to src/main/java/racingcar/domain/car/Car.java index a1bcd5d..705f431 100644 --- a/src/main/java/racingcar/domain/Car.java +++ b/src/main/java/racingcar/domain/car/Car.java @@ -1,4 +1,6 @@ -package racingcar.domain; +package racingcar.domain.car; + +import racingcar.domain.movestrategy.MoveStrategy; public class Car { private final Name name; diff --git a/src/main/java/racingcar/domain/Cars.java b/src/main/java/racingcar/domain/car/Cars.java similarity index 91% rename from src/main/java/racingcar/domain/Cars.java rename to src/main/java/racingcar/domain/car/Cars.java index 2b93ae1..5d0d79b 100644 --- a/src/main/java/racingcar/domain/Cars.java +++ b/src/main/java/racingcar/domain/car/Cars.java @@ -1,4 +1,6 @@ -package racingcar.domain; +package racingcar.domain.car; + +import racingcar.domain.movestrategy.MoveStrategy; import java.util.Collections; import java.util.List; diff --git a/src/main/java/racingcar/domain/Name.java b/src/main/java/racingcar/domain/car/Name.java similarity index 97% rename from src/main/java/racingcar/domain/Name.java rename to src/main/java/racingcar/domain/car/Name.java index cad69b1..978c3df 100644 --- a/src/main/java/racingcar/domain/Name.java +++ b/src/main/java/racingcar/domain/car/Name.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.car; import java.util.Objects; diff --git a/src/main/java/racingcar/domain/Position.java b/src/main/java/racingcar/domain/car/Position.java similarity index 94% rename from src/main/java/racingcar/domain/Position.java rename to src/main/java/racingcar/domain/car/Position.java index 6a11ce6..e524496 100644 --- a/src/main/java/racingcar/domain/Position.java +++ b/src/main/java/racingcar/domain/car/Position.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.car; public class Position { private static final int UNIT_INCREASE = 1; diff --git a/src/main/java/racingcar/domain/RepeatNumber.java b/src/main/java/racingcar/domain/common/RepeatNumber.java similarity index 94% rename from src/main/java/racingcar/domain/RepeatNumber.java rename to src/main/java/racingcar/domain/common/RepeatNumber.java index 7e2728a..bdbee1e 100644 --- a/src/main/java/racingcar/domain/RepeatNumber.java +++ b/src/main/java/racingcar/domain/common/RepeatNumber.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.common; public class RepeatNumber { private static final int NUMBER_BOUNDARY = 1; diff --git a/src/main/java/racingcar/domain/MoveStrategy.java b/src/main/java/racingcar/domain/movestrategy/MoveStrategy.java similarity index 51% rename from src/main/java/racingcar/domain/MoveStrategy.java rename to src/main/java/racingcar/domain/movestrategy/MoveStrategy.java index 3372da6..027e30e 100644 --- a/src/main/java/racingcar/domain/MoveStrategy.java +++ b/src/main/java/racingcar/domain/movestrategy/MoveStrategy.java @@ -1,4 +1,6 @@ -package racingcar.domain; +package racingcar.domain.movestrategy; + +import racingcar.domain.car.Car; public interface MoveStrategy { boolean isAvailableMove(final Car car); diff --git a/src/main/java/racingcar/domain/RandomNumberMoveStrategy.java b/src/main/java/racingcar/domain/movestrategy/RandomNumberMoveStrategy.java similarity index 84% rename from src/main/java/racingcar/domain/RandomNumberMoveStrategy.java rename to src/main/java/racingcar/domain/movestrategy/RandomNumberMoveStrategy.java index 9e8c204..bc59c08 100644 --- a/src/main/java/racingcar/domain/RandomNumberMoveStrategy.java +++ b/src/main/java/racingcar/domain/movestrategy/RandomNumberMoveStrategy.java @@ -1,4 +1,6 @@ -package racingcar.domain; +package racingcar.domain.movestrategy; + +import racingcar.domain.car.Car; import java.util.Random; diff --git a/src/main/java/racingcar/domain/RaceResult.java b/src/main/java/racingcar/domain/race/RaceResult.java similarity index 96% rename from src/main/java/racingcar/domain/RaceResult.java rename to src/main/java/racingcar/domain/race/RaceResult.java index ebb581e..3c936b8 100644 --- a/src/main/java/racingcar/domain/RaceResult.java +++ b/src/main/java/racingcar/domain/race/RaceResult.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.race; import racingcar.service.dto.CarDto; diff --git a/src/main/java/racingcar/domain/RaceStatus.java b/src/main/java/racingcar/domain/race/RaceStatus.java similarity index 96% rename from src/main/java/racingcar/domain/RaceStatus.java rename to src/main/java/racingcar/domain/race/RaceStatus.java index d1807c4..4edd43f 100644 --- a/src/main/java/racingcar/domain/RaceStatus.java +++ b/src/main/java/racingcar/domain/race/RaceStatus.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.race; import racingcar.service.dto.CarDto; diff --git a/src/main/java/racingcar/service/RacingService.java b/src/main/java/racingcar/service/RacingService.java index 232e2a4..2da3d5d 100644 --- a/src/main/java/racingcar/service/RacingService.java +++ b/src/main/java/racingcar/service/RacingService.java @@ -1,6 +1,11 @@ package racingcar.service; -import racingcar.domain.*; +import racingcar.domain.car.Car; +import racingcar.domain.car.Cars; +import racingcar.domain.common.RepeatNumber; +import racingcar.domain.movestrategy.MoveStrategy; +import racingcar.domain.race.RaceResult; +import racingcar.domain.race.RaceStatus; import racingcar.service.dto.CarDto; import java.util.ArrayList; diff --git a/src/main/java/racingcar/service/dto/CarDto.java b/src/main/java/racingcar/service/dto/CarDto.java index 3164813..0ef506c 100644 --- a/src/main/java/racingcar/service/dto/CarDto.java +++ b/src/main/java/racingcar/service/dto/CarDto.java @@ -1,6 +1,6 @@ package racingcar.service.dto; -import racingcar.domain.Car; +import racingcar.domain.car.Car; import java.util.Objects; diff --git a/src/test/java/racingcar/domain/CarTest.java b/src/test/java/racingcar/domain/car/CarTest.java similarity index 96% rename from src/test/java/racingcar/domain/CarTest.java rename to src/test/java/racingcar/domain/car/CarTest.java index 2d5a0be..da704f6 100644 --- a/src/test/java/racingcar/domain/CarTest.java +++ b/src/test/java/racingcar/domain/car/CarTest.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.car; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/racingcar/domain/CarsTest.java b/src/test/java/racingcar/domain/car/CarsTest.java similarity index 97% rename from src/test/java/racingcar/domain/CarsTest.java rename to src/test/java/racingcar/domain/car/CarsTest.java index 77b1c43..a36928f 100644 --- a/src/test/java/racingcar/domain/CarsTest.java +++ b/src/test/java/racingcar/domain/car/CarsTest.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.car; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/racingcar/domain/NameTest.java b/src/test/java/racingcar/domain/car/NameTest.java similarity index 95% rename from src/test/java/racingcar/domain/NameTest.java rename to src/test/java/racingcar/domain/car/NameTest.java index 38a2cce..4f6b58a 100644 --- a/src/test/java/racingcar/domain/NameTest.java +++ b/src/test/java/racingcar/domain/car/NameTest.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.car; import org.junit.jupiter.api.Test; diff --git a/src/test/java/racingcar/domain/PositionTest.java b/src/test/java/racingcar/domain/car/PositionTest.java similarity index 80% rename from src/test/java/racingcar/domain/PositionTest.java rename to src/test/java/racingcar/domain/car/PositionTest.java index 49004ce..835e9ca 100644 --- a/src/test/java/racingcar/domain/PositionTest.java +++ b/src/test/java/racingcar/domain/car/PositionTest.java @@ -1,9 +1,9 @@ -package racingcar.domain; +package racingcar.domain.car; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import static racingcar.domain.Position.DEFAULT_POSITION; +import static racingcar.domain.car.Position.DEFAULT_POSITION; class PositionTest { diff --git a/src/test/java/racingcar/domain/RepeatNumberTest.java b/src/test/java/racingcar/domain/common/RepeatNumberTest.java similarity index 95% rename from src/test/java/racingcar/domain/RepeatNumberTest.java rename to src/test/java/racingcar/domain/common/RepeatNumberTest.java index 94003b5..4830307 100644 --- a/src/test/java/racingcar/domain/RepeatNumberTest.java +++ b/src/test/java/racingcar/domain/common/RepeatNumberTest.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.common; import org.junit.jupiter.api.Test; diff --git a/src/test/java/racingcar/domain/RaceResultTest.java b/src/test/java/racingcar/domain/race/RaceResultTest.java similarity index 97% rename from src/test/java/racingcar/domain/RaceResultTest.java rename to src/test/java/racingcar/domain/race/RaceResultTest.java index 43a0dd1..b15985e 100644 --- a/src/test/java/racingcar/domain/RaceResultTest.java +++ b/src/test/java/racingcar/domain/race/RaceResultTest.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.race; import org.junit.jupiter.api.Test; import racingcar.service.dto.CarDto; diff --git a/src/test/java/racingcar/domain/RaceStatusTest.java b/src/test/java/racingcar/domain/race/RaceStatusTest.java similarity index 96% rename from src/test/java/racingcar/domain/RaceStatusTest.java rename to src/test/java/racingcar/domain/race/RaceStatusTest.java index 8f8256a..16fe82a 100644 --- a/src/test/java/racingcar/domain/RaceStatusTest.java +++ b/src/test/java/racingcar/domain/race/RaceStatusTest.java @@ -1,4 +1,4 @@ -package racingcar.domain; +package racingcar.domain.race; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/src/test/java/racingcar/service/RacingServiceTest.java b/src/test/java/racingcar/service/RacingServiceTest.java index 360a90d..837a64f 100644 --- a/src/test/java/racingcar/service/RacingServiceTest.java +++ b/src/test/java/racingcar/service/RacingServiceTest.java @@ -3,8 +3,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import racingcar.domain.Cars; -import racingcar.domain.RaceResult; +import racingcar.domain.car.Cars; +import racingcar.domain.race.RaceResult; import racingcar.service.dto.CarDto; import java.util.List; From 7a452ed69cd346dc72f7730a57215c76a090ded4 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Sun, 17 Nov 2019 23:05:23 +0900 Subject: [PATCH 22/33] =?UTF-8?q?feat:=20RaceStatus=20Empty=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/car/Cars.java | 2 +- .../exception/RaceStatusEmptyException.java | 9 +++++++++ .../java/racingcar/domain/race/RaceStatus.java | 16 +++++++++++++--- .../racingcar/domain/race/RaceStatusTest.java | 9 +++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/main/java/racingcar/domain/exception/RaceStatusEmptyException.java diff --git a/src/main/java/racingcar/domain/car/Cars.java b/src/main/java/racingcar/domain/car/Cars.java index 5d0d79b..b3855d9 100644 --- a/src/main/java/racingcar/domain/car/Cars.java +++ b/src/main/java/racingcar/domain/car/Cars.java @@ -8,8 +8,8 @@ import java.util.stream.Stream; public class Cars { + public static final int MINIMUM_NAMES = 2; private static final String DELIMITER = ","; - private static final int MINIMUM_NAMES = 2; private final List cars; diff --git a/src/main/java/racingcar/domain/exception/RaceStatusEmptyException.java b/src/main/java/racingcar/domain/exception/RaceStatusEmptyException.java new file mode 100644 index 0000000..8c7a30f --- /dev/null +++ b/src/main/java/racingcar/domain/exception/RaceStatusEmptyException.java @@ -0,0 +1,9 @@ +package racingcar.domain.exception; + +import racingcar.domain.car.Cars; + +public class RaceStatusEmptyException extends IllegalStateException { + public RaceStatusEmptyException() { + super("CarDto가 비어 있습니다." + Cars.MINIMUM_NAMES + "명 이상이어야 합니다."); + } +} diff --git a/src/main/java/racingcar/domain/race/RaceStatus.java b/src/main/java/racingcar/domain/race/RaceStatus.java index 4edd43f..e64fe8a 100644 --- a/src/main/java/racingcar/domain/race/RaceStatus.java +++ b/src/main/java/racingcar/domain/race/RaceStatus.java @@ -1,5 +1,7 @@ package racingcar.domain.race; +import racingcar.domain.car.Cars; +import racingcar.domain.exception.RaceStatusEmptyException; import racingcar.service.dto.CarDto; import java.util.Comparator; @@ -10,6 +12,9 @@ public class RaceStatus { private final List carDtos; public RaceStatus(final List carDtos) { + if(carDtos.size() < Cars.MINIMUM_NAMES){ + throw new RaceStatusEmptyException(); + } this.carDtos = carDtos; } @@ -18,15 +23,20 @@ public static RaceStatus from(final List carDtos) { } public List getCurrentWinners() { - final int maxOfPosition = carDtos.stream() - .max(Comparator.comparingInt(CarDto::getPosition)) - .get().getPosition(); + final int maxOfPosition = getMaxOfPosition(); return carDtos.stream() .filter(carDto -> carDto.getPosition() == maxOfPosition) .collect(Collectors.toList()); } + private int getMaxOfPosition() { + return carDtos.stream() + .max(Comparator.comparingInt(CarDto::getPosition)) + .orElseThrow(RaceStatusEmptyException::new) + .getPosition(); + } + public List getCurrentRaceStatus() { return carDtos; } diff --git a/src/test/java/racingcar/domain/race/RaceStatusTest.java b/src/test/java/racingcar/domain/race/RaceStatusTest.java index 16fe82a..aef5b89 100644 --- a/src/test/java/racingcar/domain/race/RaceStatusTest.java +++ b/src/test/java/racingcar/domain/race/RaceStatusTest.java @@ -2,11 +2,14 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import racingcar.domain.exception.RaceStatusEmptyException; import racingcar.service.dto.CarDto; +import java.util.Collections; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; class RaceStatusTest { @@ -26,4 +29,10 @@ void getWinnersTest() { assertThat(winners).hasSize(1); assertThat(winners.get(0).getName()).isEqualTo("first"); } + + @Test + @DisplayName("carDtos 가 비어있는 경우 예외처리") + void RaceStatusEmptyException() { + assertThrows(RaceStatusEmptyException.class, () -> RaceStatus.from(Collections.emptyList())); + } } \ No newline at end of file From 6e81dd1a1e1bd426bc3694e40888f3679fde3cbd Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Mon, 18 Nov 2019 00:09:15 +0900 Subject: [PATCH 23/33] =?UTF-8?q?feat:=20Name=20=EA=B3=B5=EB=B0=B1?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/car/Name.java | 10 +++++++--- src/test/java/racingcar/domain/car/NameTest.java | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/racingcar/domain/car/Name.java b/src/main/java/racingcar/domain/car/Name.java index 978c3df..f0dc88d 100644 --- a/src/main/java/racingcar/domain/car/Name.java +++ b/src/main/java/racingcar/domain/car/Name.java @@ -9,10 +9,14 @@ public class Name { private final String name; private Name(final String name) { - if (name.length() > MAX_BOUNDARY || name.length() < MIN_BOUNDARY) { - throw new IllegalArgumentException("이름은 2~5자로 해주세요."); + this.name = name.trim(); + validateLengthOfName(); + } + + private void validateLengthOfName() { + if (this.name.length() > MAX_BOUNDARY || this.name.length() < MIN_BOUNDARY) { + throw new IllegalArgumentException(String.format("이름은 %d~%d자로 해주세요.", MIN_BOUNDARY, MAX_BOUNDARY)); } - this.name = name; } static Name of(final String name) { diff --git a/src/test/java/racingcar/domain/car/NameTest.java b/src/test/java/racingcar/domain/car/NameTest.java index 4f6b58a..74f3000 100644 --- a/src/test/java/racingcar/domain/car/NameTest.java +++ b/src/test/java/racingcar/domain/car/NameTest.java @@ -15,6 +15,14 @@ class NameTest { assertThat(name1).isEqualTo(name2); } + @Test + void 이름_공백_처리() { + final Name name1 = Name.of(" 12345 "); + final Name name2 = Name.of("12345"); + + assertThat(name1).isEqualTo(name2); + } + @Test void 이름_2자_미만_예외처리() { assertThrows(IllegalArgumentException.class, () -> Name.of("a")); From 83db4480888e38f10e9e3b191ff86637eda3155c Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Mon, 18 Nov 2019 00:11:20 +0900 Subject: [PATCH 24/33] =?UTF-8?q?refactor:=20=EC=83=81=EC=88=98=EC=99=80?= =?UTF-8?q?=20=EC=97=90=EB=9F=AC=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=97=B0?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/car/Car.java | 6 +----- src/main/java/racingcar/domain/car/Cars.java | 6 +++++- src/main/java/racingcar/domain/car/Name.java | 2 +- src/main/java/racingcar/domain/common/RepeatNumber.java | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/racingcar/domain/car/Car.java b/src/main/java/racingcar/domain/car/Car.java index 705f431..484a063 100644 --- a/src/main/java/racingcar/domain/car/Car.java +++ b/src/main/java/racingcar/domain/car/Car.java @@ -15,16 +15,12 @@ public static Car of(final String name) { return new Car(name); } - public void shouldMove(final MoveStrategy moveStrategy) { + void shouldMove(final MoveStrategy moveStrategy) { if (moveStrategy.isAvailableMove(this)) { position = position.increase(); } } - public boolean isSamePosition(int position) { - return getPosition() == position; - } - public String getName() { return name.getName(); } diff --git a/src/main/java/racingcar/domain/car/Cars.java b/src/main/java/racingcar/domain/car/Cars.java index b3855d9..aceb202 100644 --- a/src/main/java/racingcar/domain/car/Cars.java +++ b/src/main/java/racingcar/domain/car/Cars.java @@ -15,8 +15,12 @@ public class Cars { public Cars(final String names) { this.cars = createCars(names); + validateCarSize(); + } + + private void validateCarSize() { if (cars.size() < MINIMUM_NAMES) { - throw new IllegalArgumentException("2명 이상 입력해주세요."); + throw new IllegalArgumentException(MINIMUM_NAMES + "명 이상 입력해주세요."); } } diff --git a/src/main/java/racingcar/domain/car/Name.java b/src/main/java/racingcar/domain/car/Name.java index f0dc88d..c33e869 100644 --- a/src/main/java/racingcar/domain/car/Name.java +++ b/src/main/java/racingcar/domain/car/Name.java @@ -23,7 +23,7 @@ static Name of(final String name) { return new Name(name); } - public String getName() { + String getName() { return name; } diff --git a/src/main/java/racingcar/domain/common/RepeatNumber.java b/src/main/java/racingcar/domain/common/RepeatNumber.java index bdbee1e..f6ef213 100644 --- a/src/main/java/racingcar/domain/common/RepeatNumber.java +++ b/src/main/java/racingcar/domain/common/RepeatNumber.java @@ -7,7 +7,7 @@ public class RepeatNumber { public RepeatNumber(final int number) { if (number < NUMBER_BOUNDARY) { - throw new IllegalArgumentException("1 이상 입력해주세요."); + throw new IllegalArgumentException(NUMBER_BOUNDARY + " 이상 입력해주세요."); } this.number = number; } From 4afecb782885dad22cc532c64e7fde1d64168374 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Mon, 18 Nov 2019 00:18:20 +0900 Subject: [PATCH 25/33] =?UTF-8?q?feat:=20Position=20=EC=BA=90=EC=8B=B1=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/racingcar/domain/car/Position.java | 24 +++++++++++++++++-- .../racingcar/domain/car/PositionTest.java | 8 +++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main/java/racingcar/domain/car/Position.java b/src/main/java/racingcar/domain/car/Position.java index e524496..cefc50b 100644 --- a/src/main/java/racingcar/domain/car/Position.java +++ b/src/main/java/racingcar/domain/car/Position.java @@ -1,9 +1,22 @@ package racingcar.domain.car; +import java.util.HashMap; +import java.util.Map; + public class Position { + private static final int DEFAULT_CACHE_SIZE = 30; private static final int UNIT_INCREASE = 1; static final int DEFAULT_POSITION = 0; + private static final Map CACHE = new HashMap<>(); + + + static { + for (int i = DEFAULT_POSITION; i < DEFAULT_CACHE_SIZE; i += UNIT_INCREASE) { + CACHE.put(i, new Position(i)); + } + } + private final int position; private Position(final int position) { @@ -11,11 +24,18 @@ private Position(final int position) { } static Position newInstance() { - return new Position(DEFAULT_POSITION); + return CACHE.get(DEFAULT_POSITION); } Position increase() { - return new Position(position + UNIT_INCREASE); + final int increasedPosition = position + UNIT_INCREASE; + return CACHE.getOrDefault(increasedPosition, create(increasedPosition)); + } + + private Position create(final int position) { + final Position createdPosition = new Position(position); + CACHE.put(position, createdPosition); + return createdPosition; } int getPosition() { diff --git a/src/test/java/racingcar/domain/car/PositionTest.java b/src/test/java/racingcar/domain/car/PositionTest.java index 835e9ca..290f2f8 100644 --- a/src/test/java/racingcar/domain/car/PositionTest.java +++ b/src/test/java/racingcar/domain/car/PositionTest.java @@ -15,4 +15,12 @@ class PositionTest { assertThat(actual).isEqualTo(DEFAULT_POSITION + 1); } + + @Test + void 캐싱_확인_테스트() { + Position position1 = Position.newInstance(); + Position position2 = Position.newInstance(); + + assertThat(position1 == position2).isTrue(); + } } \ No newline at end of file From 24b7e4683c7b048bc0f4977bd5e5fd9dbbcb5f49 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Mon, 18 Nov 2019 11:35:31 +0900 Subject: [PATCH 26/33] =?UTF-8?q?refactor:=20Test=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A7=A4=EC=A7=81=EB=84=98=EB=B2=84=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=EB=8D=95=EC=85=98=20=EC=BD=94=EB=93=9C=EC=9D=98=20=EC=83=81?= =?UTF-8?q?=EC=88=98=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/racingcar/domain/car/Position.java | 2 +- src/test/java/racingcar/domain/car/CarTest.java | 2 +- src/test/java/racingcar/domain/car/CarsTest.java | 2 +- src/test/java/racingcar/domain/car/PositionTest.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/racingcar/domain/car/Position.java b/src/main/java/racingcar/domain/car/Position.java index cefc50b..ca823c6 100644 --- a/src/main/java/racingcar/domain/car/Position.java +++ b/src/main/java/racingcar/domain/car/Position.java @@ -5,7 +5,7 @@ public class Position { private static final int DEFAULT_CACHE_SIZE = 30; - private static final int UNIT_INCREASE = 1; + static final int UNIT_INCREASE = 1; static final int DEFAULT_POSITION = 0; private static final Map CACHE = new HashMap<>(); diff --git a/src/test/java/racingcar/domain/car/CarTest.java b/src/test/java/racingcar/domain/car/CarTest.java index da704f6..72518c3 100644 --- a/src/test/java/racingcar/domain/car/CarTest.java +++ b/src/test/java/racingcar/domain/car/CarTest.java @@ -27,7 +27,7 @@ void shouldMoveTest1() { @DisplayName("이동") void shouldMoveTest2() { // given - int expected = car.getPosition() + 1; + int expected = car.getPosition() + Position.UNIT_INCREASE; // when car.shouldMove(x -> true); diff --git a/src/test/java/racingcar/domain/car/CarsTest.java b/src/test/java/racingcar/domain/car/CarsTest.java index a36928f..571a2bb 100644 --- a/src/test/java/racingcar/domain/car/CarsTest.java +++ b/src/test/java/racingcar/domain/car/CarsTest.java @@ -27,7 +27,7 @@ void createTest() { void 전체_이동_테스트() { // given Cars cars = new Cars("pobi,crong,honux"); - int movedPosition = Position.DEFAULT_POSITION + 1; + int movedPosition = Position.DEFAULT_POSITION + Position.UNIT_INCREASE; List expectedPosition = List.of(movedPosition, movedPosition, movedPosition); // when diff --git a/src/test/java/racingcar/domain/car/PositionTest.java b/src/test/java/racingcar/domain/car/PositionTest.java index 290f2f8..f7fd708 100644 --- a/src/test/java/racingcar/domain/car/PositionTest.java +++ b/src/test/java/racingcar/domain/car/PositionTest.java @@ -13,7 +13,7 @@ class PositionTest { Position increasedPosition = position.increase(); int actual = increasedPosition.getPosition(); - assertThat(actual).isEqualTo(DEFAULT_POSITION + 1); + assertThat(actual).isEqualTo(DEFAULT_POSITION + Position.UNIT_INCREASE); } @Test From cc834250f2016cc79061d50a8e7aec321e2fa68d Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Mon, 18 Nov 2019 11:36:54 +0900 Subject: [PATCH 27/33] =?UTF-8?q?test:=20CarsTest=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=A0=95=EC=83=81=20=EC=9E=85=EB=A0=A5=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/racingcar/domain/car/CarsTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/racingcar/domain/car/CarsTest.java b/src/test/java/racingcar/domain/car/CarsTest.java index 571a2bb..c131059 100644 --- a/src/test/java/racingcar/domain/car/CarsTest.java +++ b/src/test/java/racingcar/domain/car/CarsTest.java @@ -7,10 +7,16 @@ import java.util.stream.Collectors; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; class CarsTest { + @Test + void 정상_이름_입력_성공() { + assertDoesNotThrow(()-> new Cars("pobi, crong, pol")); + } + @Test @DisplayName(",가 아닌 다른 delimiter를 사용한 경우") void createTest() { From 2fb4281a3103fdbf64f52b4aad71399042c5deab Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Mon, 18 Nov 2019 11:43:14 +0900 Subject: [PATCH 28/33] =?UTF-8?q?refactor:=20Car,=20Cars=20=EC=9D=98=20sho?= =?UTF-8?q?uldMove()=20->=20tryMove()=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/car/Car.java | 2 +- src/main/java/racingcar/domain/car/Cars.java | 4 ++-- src/main/java/racingcar/service/RacingService.java | 2 +- src/test/java/racingcar/domain/car/CarTest.java | 8 ++++---- src/test/java/racingcar/domain/car/CarsTest.java | 2 +- src/test/java/racingcar/service/RacingServiceTest.java | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/racingcar/domain/car/Car.java b/src/main/java/racingcar/domain/car/Car.java index 484a063..de0c75f 100644 --- a/src/main/java/racingcar/domain/car/Car.java +++ b/src/main/java/racingcar/domain/car/Car.java @@ -15,7 +15,7 @@ public static Car of(final String name) { return new Car(name); } - void shouldMove(final MoveStrategy moveStrategy) { + void tryMove(final MoveStrategy moveStrategy) { if (moveStrategy.isAvailableMove(this)) { position = position.increase(); } diff --git a/src/main/java/racingcar/domain/car/Cars.java b/src/main/java/racingcar/domain/car/Cars.java index aceb202..3e2e7a1 100644 --- a/src/main/java/racingcar/domain/car/Cars.java +++ b/src/main/java/racingcar/domain/car/Cars.java @@ -30,8 +30,8 @@ private List createCars(final String names) { .collect(Collectors.toList()); } - public List shouldMove(final MoveStrategy moveStrategy) { - cars.forEach(car -> car.shouldMove(moveStrategy)); + public List tryMove(final MoveStrategy moveStrategy) { + cars.forEach(car -> car.tryMove(moveStrategy)); return Collections.unmodifiableList(cars); } } diff --git a/src/main/java/racingcar/service/RacingService.java b/src/main/java/racingcar/service/RacingService.java index 2da3d5d..b24097a 100644 --- a/src/main/java/racingcar/service/RacingService.java +++ b/src/main/java/racingcar/service/RacingService.java @@ -28,7 +28,7 @@ public RaceResult startRace(final int number, final Cars cars) { final List raceStatuses = new ArrayList<>(); for (int i = 0; i < repeatNumber.getNumber(); i++) { - final List movedCars = cars.shouldMove(moveStrategy); + final List movedCars = cars.tryMove(moveStrategy); final List carDtos = movedCars.stream().map(CarDto::from).collect(Collectors.toList()); raceStatuses.add(new RaceStatus(carDtos)); } diff --git a/src/test/java/racingcar/domain/car/CarTest.java b/src/test/java/racingcar/domain/car/CarTest.java index 72518c3..feb10b8 100644 --- a/src/test/java/racingcar/domain/car/CarTest.java +++ b/src/test/java/racingcar/domain/car/CarTest.java @@ -11,12 +11,12 @@ class CarTest { @Test @DisplayName("이동 안함") - void shouldMoveTest1() { + void tryMoveTest1() { // given int expected = car.getPosition(); // when - car.shouldMove(x -> false); + car.tryMove(x -> false); int actual = car.getPosition(); // then @@ -25,12 +25,12 @@ void shouldMoveTest1() { @Test @DisplayName("이동") - void shouldMoveTest2() { + void tryMoveTest2() { // given int expected = car.getPosition() + Position.UNIT_INCREASE; // when - car.shouldMove(x -> true); + car.tryMove(x -> true); int actual = car.getPosition(); // then diff --git a/src/test/java/racingcar/domain/car/CarsTest.java b/src/test/java/racingcar/domain/car/CarsTest.java index c131059..63d4ab6 100644 --- a/src/test/java/racingcar/domain/car/CarsTest.java +++ b/src/test/java/racingcar/domain/car/CarsTest.java @@ -37,7 +37,7 @@ void createTest() { List expectedPosition = List.of(movedPosition, movedPosition, movedPosition); // when - final List movedCars = cars.shouldMove(x -> true); + final List movedCars = cars.tryMove(x -> true); List actualPositions = movedCars.stream() .map(Car::getPosition) .collect(Collectors.toList()); diff --git a/src/test/java/racingcar/service/RacingServiceTest.java b/src/test/java/racingcar/service/RacingServiceTest.java index 837a64f..3b222dd 100644 --- a/src/test/java/racingcar/service/RacingServiceTest.java +++ b/src/test/java/racingcar/service/RacingServiceTest.java @@ -23,7 +23,7 @@ void setUp() { @Test void createCarsTest() { - assertThat(cars.shouldMove(x -> true)).isNotEmpty(); + assertThat(cars.tryMove(x -> true)).isNotEmpty(); } @Test From 65b3ba29dc5bdd9047f05652a6b230534998578c Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 20 Nov 2019 18:09:00 +0900 Subject: [PATCH 29/33] =?UTF-8?q?feat:=20Name=20null=20=EA=B2=80=EC=82=AC?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/car/Name.java | 2 +- src/test/java/racingcar/domain/car/NameTest.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/racingcar/domain/car/Name.java b/src/main/java/racingcar/domain/car/Name.java index c33e869..87b3c1f 100644 --- a/src/main/java/racingcar/domain/car/Name.java +++ b/src/main/java/racingcar/domain/car/Name.java @@ -9,7 +9,7 @@ public class Name { private final String name; private Name(final String name) { - this.name = name.trim(); + this.name = Objects.requireNonNull(name.trim()); validateLengthOfName(); } diff --git a/src/test/java/racingcar/domain/car/NameTest.java b/src/test/java/racingcar/domain/car/NameTest.java index 74f3000..e3a1722 100644 --- a/src/test/java/racingcar/domain/car/NameTest.java +++ b/src/test/java/racingcar/domain/car/NameTest.java @@ -23,6 +23,11 @@ class NameTest { assertThat(name1).isEqualTo(name2); } + @Test + void NULL_검사() { + assertThrows(NullPointerException.class, () -> Name.of(null)); + } + @Test void 이름_2자_미만_예외처리() { assertThrows(IllegalArgumentException.class, () -> Name.of("a")); From b0ca4ab70b5eaf3d195628b4e1956affb1e005b7 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 20 Nov 2019 18:12:29 +0900 Subject: [PATCH 30/33] =?UTF-8?q?feat:=20RepeatNumber=20null=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/common/RepeatNumber.java | 6 ++++-- src/test/java/racingcar/domain/common/RepeatNumberTest.java | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/racingcar/domain/common/RepeatNumber.java b/src/main/java/racingcar/domain/common/RepeatNumber.java index f6ef213..7675508 100644 --- a/src/main/java/racingcar/domain/common/RepeatNumber.java +++ b/src/main/java/racingcar/domain/common/RepeatNumber.java @@ -1,11 +1,13 @@ package racingcar.domain.common; +import java.util.Objects; + public class RepeatNumber { private static final int NUMBER_BOUNDARY = 1; private final int number; - public RepeatNumber(final int number) { + private RepeatNumber(final int number) { if (number < NUMBER_BOUNDARY) { throw new IllegalArgumentException(NUMBER_BOUNDARY + " 이상 입력해주세요."); } @@ -18,7 +20,7 @@ public static RepeatNumber from(final int number) { public static RepeatNumber from(final String number) { - return new RepeatNumber(Integer.parseInt(number)); + return new RepeatNumber(Integer.parseInt(Objects.requireNonNull(number))); } public int getNumber() { diff --git a/src/test/java/racingcar/domain/common/RepeatNumberTest.java b/src/test/java/racingcar/domain/common/RepeatNumberTest.java index 4830307..953a76a 100644 --- a/src/test/java/racingcar/domain/common/RepeatNumberTest.java +++ b/src/test/java/racingcar/domain/common/RepeatNumberTest.java @@ -26,4 +26,9 @@ class RepeatNumberTest { void 문자로_숫자_입력_변환_처리() { assertDoesNotThrow(() -> RepeatNumber.from("67")); } + + @Test + void NULL_검사() { + assertThrows(NullPointerException.class, () -> RepeatNumber.from(null)); + } } \ No newline at end of file From ec4a3475160ecba9a1eca706a1b2da4f99d61a23 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 20 Nov 2019 18:13:06 +0900 Subject: [PATCH 31/33] =?UTF-8?q?style:=20Position=20=EA=B3=B5=EB=B0=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/domain/car/Position.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/racingcar/domain/car/Position.java b/src/main/java/racingcar/domain/car/Position.java index ca823c6..50b4ee1 100644 --- a/src/main/java/racingcar/domain/car/Position.java +++ b/src/main/java/racingcar/domain/car/Position.java @@ -10,7 +10,6 @@ public class Position { private static final Map CACHE = new HashMap<>(); - static { for (int i = DEFAULT_POSITION; i < DEFAULT_CACHE_SIZE; i += UNIT_INCREASE) { CACHE.put(i, new Position(i)); From afb53fe92dc7a4856e56d6ef39344601daf4b88b Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 20 Nov 2019 18:36:00 +0900 Subject: [PATCH 32/33] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=EC=8B=9C=20=EC=82=AC=EC=9A=A9=EC=9E=90=EC=97=90?= =?UTF-8?q?=EA=B2=8C=20=EB=A9=94=EC=8B=9C=EC=A7=80=20=ED=91=9C=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racingcar/console/App.java | 27 +++++++++++++++---- .../racingcar/console/view/InputView.java | 7 ++++- .../racingcar/console/view/OutputView.java | 4 +++ src/main/java/racingcar/domain/car/Car.java | 1 + .../racingcar/domain/common/RepeatNumber.java | 8 +++++- .../racingcar/domain/race/RaceStatus.java | 2 +- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/main/java/racingcar/console/App.java b/src/main/java/racingcar/console/App.java index ca747b5..65466a7 100644 --- a/src/main/java/racingcar/console/App.java +++ b/src/main/java/racingcar/console/App.java @@ -13,13 +13,30 @@ public static void main(String[] args) { final MoveStrategy moveStrategy = new RandomNumberMoveStrategy(); final RacingService racingService = new RacingService(moveStrategy); - final String names = InputView.inputNames(); - final Cars cars = racingService.createCars(names); - - final int repeatNumber = InputView.inputRepeatNumber(); - final RaceResult raceResult = racingService.startRace(repeatNumber, cars); + final Cars cars = createCars(racingService); + final RaceResult raceResult = startRace(racingService, cars); OutputView.printRaceResult(raceResult); OutputView.printWinner(raceResult.getWinners()); } + + private static Cars createCars(final RacingService racingService) { + try { + final String names = InputView.inputNames(); + return racingService.createCars(names); + } catch (IllegalArgumentException e) { + OutputView.printException(e.getMessage()); + return createCars(racingService); + } + } + + private static RaceResult startRace(final RacingService racingService, final Cars cars) { + try { + final int repeatNumber = InputView.inputRepeatNumber(); + return racingService.startRace(repeatNumber, cars); + } catch (RuntimeException e) { + OutputView.printException(e.getMessage()); + return startRace(racingService, cars); + } + } } diff --git a/src/main/java/racingcar/console/view/InputView.java b/src/main/java/racingcar/console/view/InputView.java index 358c11e..37ea77d 100644 --- a/src/main/java/racingcar/console/view/InputView.java +++ b/src/main/java/racingcar/console/view/InputView.java @@ -15,6 +15,11 @@ public static String inputNames() { public static int inputRepeatNumber() { System.out.println("시도할 회수는 몇회인가요?"); - return scanner.nextInt(); + try { + return Integer.parseInt(scanner.nextLine()); + } catch (NumberFormatException e) { + System.err.println("숫자만 입력 가능합니다."); + return inputRepeatNumber(); + } } } diff --git a/src/main/java/racingcar/console/view/OutputView.java b/src/main/java/racingcar/console/view/OutputView.java index ab13a1b..5e11e4b 100644 --- a/src/main/java/racingcar/console/view/OutputView.java +++ b/src/main/java/racingcar/console/view/OutputView.java @@ -43,4 +43,8 @@ public static void printWinner(final List winners) { System.out.println(String.format("%s가 최종 우승했습니다.", joinedWinners)); } + + public static void printException(final String message) { + System.err.println(message); + } } diff --git a/src/main/java/racingcar/domain/car/Car.java b/src/main/java/racingcar/domain/car/Car.java index de0c75f..32ae75a 100644 --- a/src/main/java/racingcar/domain/car/Car.java +++ b/src/main/java/racingcar/domain/car/Car.java @@ -6,6 +6,7 @@ public class Car { private final Name name; private Position position; + // TODO: 2019/11/20 private public Car(final String name) { this.name = Name.of(name); this.position = Position.newInstance(); diff --git a/src/main/java/racingcar/domain/common/RepeatNumber.java b/src/main/java/racingcar/domain/common/RepeatNumber.java index 7675508..9ebaeed 100644 --- a/src/main/java/racingcar/domain/common/RepeatNumber.java +++ b/src/main/java/racingcar/domain/common/RepeatNumber.java @@ -20,7 +20,13 @@ public static RepeatNumber from(final int number) { public static RepeatNumber from(final String number) { - return new RepeatNumber(Integer.parseInt(Objects.requireNonNull(number))); + try { + final int parsedNumber = Integer.parseInt(Objects.requireNonNull(number)); + return new RepeatNumber(parsedNumber); + } catch (NumberFormatException e) { + System.out.println("-------------------------"); + throw new NumberFormatException("숫자를 입력해주세요"); + } } public int getNumber() { diff --git a/src/main/java/racingcar/domain/race/RaceStatus.java b/src/main/java/racingcar/domain/race/RaceStatus.java index e64fe8a..76bcc3f 100644 --- a/src/main/java/racingcar/domain/race/RaceStatus.java +++ b/src/main/java/racingcar/domain/race/RaceStatus.java @@ -12,7 +12,7 @@ public class RaceStatus { private final List carDtos; public RaceStatus(final List carDtos) { - if(carDtos.size() < Cars.MINIMUM_NAMES){ + if (carDtos.size() < Cars.MINIMUM_NAMES) { throw new RaceStatusEmptyException(); } this.carDtos = carDtos; From edd5e30716f3bdbce6eaa3d901b26379061d27a0 Mon Sep 17 00:00:00 2001 From: DaejunBae Date: Wed, 20 Nov 2019 19:39:33 +0900 Subject: [PATCH 33/33] =?UTF-8?q?refactor:=20CarDto=20=EC=A0=9C=EA=B1=B0?= =?UTF-8?q?=20->=20Car=EB=A1=9C=20=EB=8C=80=EC=B2=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CarDto가 도메인에 침투 코드여서 변경함 CarDto 대신 Car로 대체하고 완벽한 불변객체로 수정 --- .../racingcar/console/view/OutputView.java | 16 +++--- src/main/java/racingcar/domain/car/Car.java | 45 ++++++++++++++--- src/main/java/racingcar/domain/car/Cars.java | 11 +++-- .../java/racingcar/domain/car/Position.java | 26 +++++++++- .../racingcar/domain/common/RepeatNumber.java | 1 - .../racingcar/domain/race/RaceResult.java | 8 +-- .../racingcar/domain/race/RaceStatus.java | 28 +++++------ .../java/racingcar/service/RacingService.java | 5 +- .../java/racingcar/service/dto/CarDto.java | 49 ------------------- .../java/racingcar/domain/car/CarTest.java | 31 ++++++++++-- .../racingcar/domain/race/RaceResultTest.java | 18 +++---- .../racingcar/domain/race/RaceStatusTest.java | 12 ++--- .../racingcar/service/RacingServiceTest.java | 8 +-- 13 files changed, 142 insertions(+), 116 deletions(-) delete mode 100644 src/main/java/racingcar/service/dto/CarDto.java diff --git a/src/main/java/racingcar/console/view/OutputView.java b/src/main/java/racingcar/console/view/OutputView.java index 5e11e4b..e30a542 100644 --- a/src/main/java/racingcar/console/view/OutputView.java +++ b/src/main/java/racingcar/console/view/OutputView.java @@ -1,7 +1,7 @@ package racingcar.console.view; +import racingcar.domain.car.Car; import racingcar.domain.race.RaceResult; -import racingcar.service.dto.CarDto; import java.util.List; import java.util.stream.Collectors; @@ -22,23 +22,23 @@ public static void printRaceResult(final RaceResult raceResult) { System.out.println(sb.toString()); } - private static void printCurrentRace(final List carDtos, final StringBuilder sb) { - carDtos.forEach(carDto -> printCar(sb, carDto)); + private static void printCurrentRace(final List cars, final StringBuilder sb) { + cars.forEach(car -> printCar(sb, car)); sb.append("\n"); } - private static void printCar(final StringBuilder sb, final CarDto carDto) { - sb.append(String.format("%s : ", carDto.getName())); - for (int i = 0; i < carDto.getPosition(); i++) { + private static void printCar(final StringBuilder sb, final Car car) { + sb.append(String.format("%s : ", car.getName())); + for (int i = 0; i < car.getPosition(); i++) { sb.append(ROUTE); } sb.append("\n"); } - public static void printWinner(final List winners) { + public static void printWinner(final List winners) { final String joinedWinners = winners.stream() - .map(CarDto::getName) + .map(Car::getName) .collect(Collectors.joining(", ")); System.out.println(String.format("%s가 최종 우승했습니다.", joinedWinners)); diff --git a/src/main/java/racingcar/domain/car/Car.java b/src/main/java/racingcar/domain/car/Car.java index 32ae75a..52083b9 100644 --- a/src/main/java/racingcar/domain/car/Car.java +++ b/src/main/java/racingcar/domain/car/Car.java @@ -2,24 +2,43 @@ import racingcar.domain.movestrategy.MoveStrategy; +import java.util.Objects; + + public class Car { private final Name name; - private Position position; + private final Position position; + + private Car(final String name) { + this(name, Position.newInstance()); + } - // TODO: 2019/11/20 private - public Car(final String name) { + private Car(final String name, final int position) { + this(name, Position.of(position)); + } + + public Car(final String name, final Position position) { this.name = Name.of(name); - this.position = Position.newInstance(); + this.position = position; } public static Car of(final String name) { return new Car(name); } - void tryMove(final MoveStrategy moveStrategy) { + public static Car of(final String name, final int position) { + return new Car(name, position); + } + + Car tryMove(final MoveStrategy moveStrategy) { if (moveStrategy.isAvailableMove(this)) { - position = position.increase(); + return new Car(name.getName(), position.increase()); } + return new Car(name.getName(), position); + } + + public boolean matchPosition(final int position) { + return this.position.getPosition() == position; } public String getName() { @@ -30,6 +49,20 @@ public int getPosition() { return position.getPosition(); } + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Car car = (Car) o; + return Objects.equals(name, car.name) && + Objects.equals(position, car.position); + } + + @Override + public int hashCode() { + return Objects.hash(name, position); + } + @Override public String toString() { return "Car{" + diff --git a/src/main/java/racingcar/domain/car/Cars.java b/src/main/java/racingcar/domain/car/Cars.java index 3e2e7a1..60f430b 100644 --- a/src/main/java/racingcar/domain/car/Cars.java +++ b/src/main/java/racingcar/domain/car/Cars.java @@ -2,7 +2,6 @@ import racingcar.domain.movestrategy.MoveStrategy; -import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -11,7 +10,7 @@ public class Cars { public static final int MINIMUM_NAMES = 2; private static final String DELIMITER = ","; - private final List cars; + private List cars; public Cars(final String names) { this.cars = createCars(names); @@ -26,12 +25,14 @@ private void validateCarSize() { private List createCars(final String names) { return Stream.of(names.split(DELIMITER)) - .map(Car::new) + .map(Car::of) .collect(Collectors.toList()); } public List tryMove(final MoveStrategy moveStrategy) { - cars.forEach(car -> car.tryMove(moveStrategy)); - return Collections.unmodifiableList(cars); + this.cars = cars.stream() + .map(car -> car.tryMove(moveStrategy)) + .collect(Collectors.toList()); + return cars; } } diff --git a/src/main/java/racingcar/domain/car/Position.java b/src/main/java/racingcar/domain/car/Position.java index 50b4ee1..91e4ff8 100644 --- a/src/main/java/racingcar/domain/car/Position.java +++ b/src/main/java/racingcar/domain/car/Position.java @@ -2,6 +2,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Objects; public class Position { private static final int DEFAULT_CACHE_SIZE = 30; @@ -26,12 +27,20 @@ static Position newInstance() { return CACHE.get(DEFAULT_POSITION); } + public static Position of(final int position) { + return get(position); + } + Position increase() { final int increasedPosition = position + UNIT_INCREASE; - return CACHE.getOrDefault(increasedPosition, create(increasedPosition)); + return get(increasedPosition); } - private Position create(final int position) { + private static Position get(final int position) { + return CACHE.getOrDefault(position, create(position)); + } + + private static Position create(final int position) { final Position createdPosition = new Position(position); CACHE.put(position, createdPosition); return createdPosition; @@ -41,6 +50,19 @@ int getPosition() { return position; } + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Position position1 = (Position) o; + return position == position1.position; + } + + @Override + public int hashCode() { + return Objects.hash(position); + } + @Override public String toString() { return "position= " + position; diff --git a/src/main/java/racingcar/domain/common/RepeatNumber.java b/src/main/java/racingcar/domain/common/RepeatNumber.java index 9ebaeed..06df46e 100644 --- a/src/main/java/racingcar/domain/common/RepeatNumber.java +++ b/src/main/java/racingcar/domain/common/RepeatNumber.java @@ -24,7 +24,6 @@ public static RepeatNumber from(final String number) { final int parsedNumber = Integer.parseInt(Objects.requireNonNull(number)); return new RepeatNumber(parsedNumber); } catch (NumberFormatException e) { - System.out.println("-------------------------"); throw new NumberFormatException("숫자를 입력해주세요"); } } diff --git a/src/main/java/racingcar/domain/race/RaceResult.java b/src/main/java/racingcar/domain/race/RaceResult.java index 3c936b8..672bb01 100644 --- a/src/main/java/racingcar/domain/race/RaceResult.java +++ b/src/main/java/racingcar/domain/race/RaceResult.java @@ -1,6 +1,6 @@ package racingcar.domain.race; -import racingcar.service.dto.CarDto; +import racingcar.domain.car.Car; import java.util.Iterator; import java.util.List; @@ -14,7 +14,7 @@ public RaceResult(final List raceStatuses) { this.cursor = 0; } - public List getWinners() { + public List getWinners() { final int lastIndex = raceStatuses.size() - 1; return raceStatuses.get(lastIndex).getCurrentWinners(); } @@ -25,8 +25,8 @@ public boolean hasNext() { } @Override - public List next() { - final List currentRaceStatus = raceStatuses.get(cursor).getCurrentRaceStatus(); + public List next() { + final List currentRaceStatus = raceStatuses.get(cursor).getCurrentRaceStatus(); cursor++; return currentRaceStatus; } diff --git a/src/main/java/racingcar/domain/race/RaceStatus.java b/src/main/java/racingcar/domain/race/RaceStatus.java index 76bcc3f..0b79517 100644 --- a/src/main/java/racingcar/domain/race/RaceStatus.java +++ b/src/main/java/racingcar/domain/race/RaceStatus.java @@ -1,43 +1,43 @@ package racingcar.domain.race; +import racingcar.domain.car.Car; import racingcar.domain.car.Cars; import racingcar.domain.exception.RaceStatusEmptyException; -import racingcar.service.dto.CarDto; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class RaceStatus { - private final List carDtos; + private final List cars; - public RaceStatus(final List carDtos) { - if (carDtos.size() < Cars.MINIMUM_NAMES) { + public RaceStatus(final List cars) { + if (cars.size() < Cars.MINIMUM_NAMES) { throw new RaceStatusEmptyException(); } - this.carDtos = carDtos; + this.cars = cars; } - public static RaceStatus from(final List carDtos) { - return new RaceStatus(carDtos); + public static RaceStatus from(final List cars) { + return new RaceStatus(cars); } - public List getCurrentWinners() { + public List getCurrentWinners() { final int maxOfPosition = getMaxOfPosition(); - return carDtos.stream() - .filter(carDto -> carDto.getPosition() == maxOfPosition) + return cars.stream() + .filter(cars -> cars.matchPosition(maxOfPosition)) .collect(Collectors.toList()); } private int getMaxOfPosition() { - return carDtos.stream() - .max(Comparator.comparingInt(CarDto::getPosition)) + return cars.stream() + .max(Comparator.comparingInt(Car::getPosition)) .orElseThrow(RaceStatusEmptyException::new) .getPosition(); } - public List getCurrentRaceStatus() { - return carDtos; + public List getCurrentRaceStatus() { + return cars; } } diff --git a/src/main/java/racingcar/service/RacingService.java b/src/main/java/racingcar/service/RacingService.java index b24097a..5e610cd 100644 --- a/src/main/java/racingcar/service/RacingService.java +++ b/src/main/java/racingcar/service/RacingService.java @@ -6,11 +6,9 @@ import racingcar.domain.movestrategy.MoveStrategy; import racingcar.domain.race.RaceResult; import racingcar.domain.race.RaceStatus; -import racingcar.service.dto.CarDto; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; public class RacingService { private final MoveStrategy moveStrategy; @@ -29,8 +27,7 @@ public RaceResult startRace(final int number, final Cars cars) { final List raceStatuses = new ArrayList<>(); for (int i = 0; i < repeatNumber.getNumber(); i++) { final List movedCars = cars.tryMove(moveStrategy); - final List carDtos = movedCars.stream().map(CarDto::from).collect(Collectors.toList()); - raceStatuses.add(new RaceStatus(carDtos)); + raceStatuses.add(new RaceStatus(movedCars)); } return new RaceResult(raceStatuses); diff --git a/src/main/java/racingcar/service/dto/CarDto.java b/src/main/java/racingcar/service/dto/CarDto.java deleted file mode 100644 index 0ef506c..0000000 --- a/src/main/java/racingcar/service/dto/CarDto.java +++ /dev/null @@ -1,49 +0,0 @@ -package racingcar.service.dto; - -import racingcar.domain.car.Car; - -import java.util.Objects; - -public class CarDto { - private final String name; - private final int position; - - public CarDto(final String name, final int position) { - this.name = name; - this.position = position; - } - - public static CarDto from(final Car car) { - return new CarDto(car.getName(), car.getPosition()); - } - - public String getName() { - return name; - } - - public int getPosition() { - return position; - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - final CarDto carDto = (CarDto) o; - return position == carDto.position && - Objects.equals(name, carDto.name); - } - - @Override - public int hashCode() { - return Objects.hash(name, position); - } - - @Override - public String toString() { - return "CarDto{" + - "name='" + name + '\'' + - ", position=" + position + - '}'; - } -} diff --git a/src/test/java/racingcar/domain/car/CarTest.java b/src/test/java/racingcar/domain/car/CarTest.java index feb10b8..765081e 100644 --- a/src/test/java/racingcar/domain/car/CarTest.java +++ b/src/test/java/racingcar/domain/car/CarTest.java @@ -8,6 +8,13 @@ class CarTest { private Car car = Car.of("베디"); + @Test + void 논리적_동치성_테스트() { + int position = 2; + String name = "베디"; + + assertThat(Car.of(name, 2)).isEqualTo(Car.of(name, position)); + } @Test @DisplayName("이동 안함") @@ -16,8 +23,7 @@ void tryMoveTest1() { int expected = car.getPosition(); // when - car.tryMove(x -> false); - int actual = car.getPosition(); + int actual = car.tryMove(x -> false).getPosition(); // then assertThat(actual).isEqualTo(expected); @@ -30,10 +36,27 @@ void tryMoveTest2() { int expected = car.getPosition() + Position.UNIT_INCREASE; // when - car.tryMove(x -> true); - int actual = car.getPosition(); + int actual = car.tryMove(x -> true).getPosition(); // then assertThat(actual).isEqualTo(expected); } + + @Test + @DisplayName("이동하지 않아도 새로운 인스턴스 생성하는지 확인") + void tryMoveTest3() { + Car expected = Car.of("베디"); + Car actual = expected.tryMove((x)->false); + + assertThat(actual).isEqualTo(expected); + assertThat(actual == expected).isFalse(); + } + + @Test + void matchPositionTest() { + int position = 1; + Car car = Car.of("bedi", position); + + assertThat(car.matchPosition(position)).isTrue(); + } } \ No newline at end of file diff --git a/src/test/java/racingcar/domain/race/RaceResultTest.java b/src/test/java/racingcar/domain/race/RaceResultTest.java index b15985e..f1de669 100644 --- a/src/test/java/racingcar/domain/race/RaceResultTest.java +++ b/src/test/java/racingcar/domain/race/RaceResultTest.java @@ -1,7 +1,7 @@ package racingcar.domain.race; import org.junit.jupiter.api.Test; -import racingcar.service.dto.CarDto; +import racingcar.domain.car.Car; import java.util.List; @@ -9,13 +9,13 @@ class RaceResultTest { - private CarDto first1 = new CarDto("first", 2); - private CarDto second1 = new CarDto("second", 1); - private List cars1 = List.of(first1, second1); + private Car first1 = Car.of("first", 2); + private Car second1 = Car.of("secon", 1); + private List cars1 = List.of(first1, second1); - private CarDto first2 = new CarDto("first", 3); - private CarDto second2 = new CarDto("second", 2); - private List cars2 = List.of(first2, second2); + private Car first2 = Car.of("first", 3); + private Car second2 = Car.of("secon", 2); + private List cars2 = List.of(first2, second2); private List raceStatuses = List.of(new RaceStatus(cars1), new RaceStatus(cars2)); @@ -23,9 +23,9 @@ class RaceResultTest { @Test void getWinnersTest() { - List expected = List.of(this.first2); + List expected = List.of(this.first2); - List winners = raceResult.getWinners(); + List winners = raceResult.getWinners(); assertThat(winners).isEqualTo(expected); } diff --git a/src/test/java/racingcar/domain/race/RaceStatusTest.java b/src/test/java/racingcar/domain/race/RaceStatusTest.java index aef5b89..e3551a7 100644 --- a/src/test/java/racingcar/domain/race/RaceStatusTest.java +++ b/src/test/java/racingcar/domain/race/RaceStatusTest.java @@ -2,8 +2,8 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import racingcar.domain.car.Car; import racingcar.domain.exception.RaceStatusEmptyException; -import racingcar.service.dto.CarDto; import java.util.Collections; import java.util.List; @@ -17,13 +17,13 @@ class RaceStatusTest { @DisplayName("현재 선두 자동차 구하기") void getWinnersTest() { // given - CarDto first = new CarDto("first", 2); - CarDto second = new CarDto("second", 1); - List cars = List.of(first, second); + Car first = Car.of("first", 2); + Car second = Car.of("secon", 1); + List cars = List.of(first, second); RaceStatus raceStatus = RaceStatus.from(cars); // when - List winners = raceStatus.getCurrentWinners(); + List winners = raceStatus.getCurrentWinners(); // then assertThat(winners).hasSize(1); @@ -31,7 +31,7 @@ void getWinnersTest() { } @Test - @DisplayName("carDtos 가 비어있는 경우 예외처리") + @DisplayName("cars 가 비어있는 경우 예외처리") void RaceStatusEmptyException() { assertThrows(RaceStatusEmptyException.class, () -> RaceStatus.from(Collections.emptyList())); } diff --git a/src/test/java/racingcar/service/RacingServiceTest.java b/src/test/java/racingcar/service/RacingServiceTest.java index 3b222dd..b7f9a7a 100644 --- a/src/test/java/racingcar/service/RacingServiceTest.java +++ b/src/test/java/racingcar/service/RacingServiceTest.java @@ -3,9 +3,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import racingcar.domain.car.Car; import racingcar.domain.car.Cars; import racingcar.domain.race.RaceResult; -import racingcar.service.dto.CarDto; import java.util.List; @@ -29,7 +29,7 @@ void createCarsTest() { @Test @DisplayName("경주 후 우승자 구하기") void startRaceTest1() { - List expected = List.of(new CarDto("pobi", 3), new CarDto("crong", 3)); + List expected = List.of(Car.of("pobi", 3), Car.of("crong", 3)); int repeatNumber = 3; RaceResult raceResult = racingService.startRace(repeatNumber, cars); @@ -41,8 +41,8 @@ void startRaceTest1() { @DisplayName("경주 후 상태 확인") void startRaceTest2() { // given - List expected1 = List.of(new CarDto("pobi", 1), new CarDto("crong", 1)); - List expected2 = List.of(new CarDto("pobi", 2), new CarDto("crong", 2)); + List expected1 = List.of(Car.of("pobi", 1), Car.of("crong", 1)); + List expected2 = List.of(Car.of("pobi", 2), Car.of("crong", 2)); // when RaceResult raceResult = racingService.startRace(2, cars);