diff --git a/src/main/java/racinggame/RacingGameController.java b/src/main/java/racinggame/controller/RacingGameController.java similarity index 84% rename from src/main/java/racinggame/RacingGameController.java rename to src/main/java/racinggame/controller/RacingGameController.java index c40a78a4262..b0caf765418 100644 --- a/src/main/java/racinggame/RacingGameController.java +++ b/src/main/java/racinggame/controller/RacingGameController.java @@ -1,6 +1,13 @@ -package racinggame; +package racinggame.controller; + +import racinggame.domain.strategy.MoveStrategy; +import racinggame.domain.race.Race; +import racinggame.domain.strategy.RandomMoveStrategy; +import racinggame.view.ConsoleInputView; +import racinggame.view.ConsoleResultView; +import racinggame.view.InputView; +import racinggame.view.ResultView; -import racinggame.racingcar.*; import java.util.List; public class RacingGameController { diff --git a/src/main/java/racinggame/racingcar/Race.java b/src/main/java/racinggame/domain/race/Race.java similarity index 91% rename from src/main/java/racinggame/racingcar/Race.java rename to src/main/java/racinggame/domain/race/Race.java index 55671371274..30141c9f1d3 100644 --- a/src/main/java/racinggame/racingcar/Race.java +++ b/src/main/java/racinggame/domain/race/Race.java @@ -1,4 +1,7 @@ -package racinggame.racingcar; +package racinggame.domain.race; + +import racinggame.domain.racingcar.RacingCars; +import racinggame.domain.strategy.MoveStrategy; import java.util.List; import java.util.Map; diff --git a/src/main/java/racinggame/domain/racingcar/CarName.java b/src/main/java/racinggame/domain/racingcar/CarName.java new file mode 100644 index 00000000000..c8c169b404e --- /dev/null +++ b/src/main/java/racinggame/domain/racingcar/CarName.java @@ -0,0 +1,23 @@ +package racinggame.domain.racingcar; + +public class CarName { + private static final int MAX_NAME_LENGTH = 5; + private static final String ERROR_INVALID_NAME = "자동차 이름은 5자 이하여야 합니다."; + + private final String value; + + public CarName(String value) { + validateName(value); + this.value = value; + } + + private void validateName(String name) { + if (name.length() > MAX_NAME_LENGTH) { + throw new IllegalArgumentException(ERROR_INVALID_NAME + ": " + name); + } + } + + public String getValue() { + return value; + } +} diff --git a/src/main/java/racinggame/racingcar/CarPosition.java b/src/main/java/racinggame/domain/racingcar/CarPosition.java similarity index 85% rename from src/main/java/racinggame/racingcar/CarPosition.java rename to src/main/java/racinggame/domain/racingcar/CarPosition.java index 02a6ca50a40..aa41a1865e1 100644 --- a/src/main/java/racinggame/racingcar/CarPosition.java +++ b/src/main/java/racinggame/domain/racingcar/CarPosition.java @@ -1,4 +1,4 @@ -package racinggame.racingcar; +package racinggame.domain.racingcar; public class CarPosition { private int value; diff --git a/src/main/java/racinggame/racingcar/RacingCar.java b/src/main/java/racinggame/domain/racingcar/RacingCar.java similarity index 89% rename from src/main/java/racinggame/racingcar/RacingCar.java rename to src/main/java/racinggame/domain/racingcar/RacingCar.java index 6de546a30f3..582839c42b4 100644 --- a/src/main/java/racinggame/racingcar/RacingCar.java +++ b/src/main/java/racinggame/domain/racingcar/RacingCar.java @@ -1,4 +1,6 @@ -package racinggame.racingcar; +package racinggame.domain.racingcar; + +import racinggame.domain.strategy.MoveStrategy; public class RacingCar { private final CarName name; diff --git a/src/main/java/racinggame/racingcar/RacingCars.java b/src/main/java/racinggame/domain/racingcar/RacingCars.java similarity index 78% rename from src/main/java/racinggame/racingcar/RacingCars.java rename to src/main/java/racinggame/domain/racingcar/RacingCars.java index c346c0591b4..d2bf192e50c 100644 --- a/src/main/java/racinggame/racingcar/RacingCars.java +++ b/src/main/java/racinggame/domain/racingcar/RacingCars.java @@ -1,4 +1,6 @@ -package racinggame.racingcar; +package racinggame.domain.racingcar; + +import racinggame.domain.strategy.MoveStrategy; import java.util.*; import java.util.stream.Collectors; @@ -11,11 +13,9 @@ private RacingCars(List cars) { } public static RacingCars create(MoveStrategy moveStrategy, List nameList) { - List cars = nameList.stream() + return nameList.stream() .map(name -> RacingCar.create(moveStrategy, name)) - .collect(Collectors.toList()); - - return new RacingCars(cars); + .collect(Collectors.collectingAndThen(Collectors.toList(), RacingCars::new)); } public int count() { diff --git a/src/main/java/racinggame/racingcar/MoveStrategy.java b/src/main/java/racinggame/domain/strategy/MoveStrategy.java similarity index 62% rename from src/main/java/racinggame/racingcar/MoveStrategy.java rename to src/main/java/racinggame/domain/strategy/MoveStrategy.java index dd2fe92f874..fa595f19cfb 100644 --- a/src/main/java/racinggame/racingcar/MoveStrategy.java +++ b/src/main/java/racinggame/domain/strategy/MoveStrategy.java @@ -1,4 +1,4 @@ -package racinggame.racingcar; +package racinggame.domain.strategy; public interface MoveStrategy { boolean shouldMove(); diff --git a/src/main/java/racinggame/racingcar/RandomMoveStrategy.java b/src/main/java/racinggame/domain/strategy/RandomMoveStrategy.java similarity index 92% rename from src/main/java/racinggame/racingcar/RandomMoveStrategy.java rename to src/main/java/racinggame/domain/strategy/RandomMoveStrategy.java index a2979a18179..a7237256570 100644 --- a/src/main/java/racinggame/racingcar/RandomMoveStrategy.java +++ b/src/main/java/racinggame/domain/strategy/RandomMoveStrategy.java @@ -1,4 +1,4 @@ -package racinggame.racingcar; +package racinggame.domain.strategy; import java.util.Random; diff --git a/src/main/java/racinggame/racingcar/CarName.java b/src/main/java/racinggame/racingcar/CarName.java deleted file mode 100644 index 1191c3bbee8..00000000000 --- a/src/main/java/racinggame/racingcar/CarName.java +++ /dev/null @@ -1,13 +0,0 @@ -package racinggame.racingcar; - -public class CarName { - private final String value; - - public CarName(String value) { - this.value = value; - } - - String getValue() { - return value; - } -} diff --git a/src/main/java/racinggame/ConsoleInputView.java b/src/main/java/racinggame/view/ConsoleInputView.java similarity index 79% rename from src/main/java/racinggame/ConsoleInputView.java rename to src/main/java/racinggame/view/ConsoleInputView.java index ad5e97bed60..30a5eda7424 100644 --- a/src/main/java/racinggame/ConsoleInputView.java +++ b/src/main/java/racinggame/view/ConsoleInputView.java @@ -1,4 +1,6 @@ -package racinggame; +package racinggame.view; + +import racinggame.domain.racingcar.CarName; import java.util.List; import java.util.Scanner; @@ -8,14 +10,11 @@ public class ConsoleInputView implements InputView { private static final String QUESTION_NAMES_OF_CARS = "경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."; private static final String QUESTION_NUMBER_OF_ROUNDS = "시도할 회수는 몇 회 인가요?"; - - private static final String ERROR_INVALID_NAME = "자동차 이름은 5자 이하여야 합니다. 다시 입력해주세요."; private static final String ERROR_NOT_POSITIVE = "양수를 입력해주세요: "; private static final String ERROR_NOT_NUMBER = "숫자를 입력해주세요: "; private static final int ZERO = 0; private static final String NAME_SEPARATOR = ","; - private static final int MAX_NAME_LENGTH = 5; private final Scanner scanner; @@ -42,8 +41,10 @@ private void printMessage(final String message) { private List readValidNames() { try { List names = splitNames(readInput()); - validateNames(names); - return names; + return names.stream() + .map(CarName::new) // CarName 객체 생성, 이름을 검증함 + .map(CarName::getValue) + .collect(Collectors.toList()); } catch (RuntimeException e) { System.out.println(e.getMessage()); return readValidNames(); @@ -56,19 +57,6 @@ private List splitNames(String input) { .collect(Collectors.toList()); } - private void validateNames(List names) { - names.stream() - .filter(name -> !isValidName(name)) - .findFirst() - .ifPresent(invalidName -> { - throw new RuntimeException(ERROR_INVALID_NAME + ": " + invalidName); - }); - } - - private boolean isValidName(String name) { - return name.length() <= MAX_NAME_LENGTH; - } - private int readPositiveNumber() { String input = readInput(); try { diff --git a/src/main/java/racinggame/ConsoleResultView.java b/src/main/java/racinggame/view/ConsoleResultView.java similarity index 91% rename from src/main/java/racinggame/ConsoleResultView.java rename to src/main/java/racinggame/view/ConsoleResultView.java index b178ec3aec7..3fab609de5e 100644 --- a/src/main/java/racinggame/ConsoleResultView.java +++ b/src/main/java/racinggame/view/ConsoleResultView.java @@ -1,8 +1,7 @@ -package racinggame; +package racinggame.view; import java.util.List; import java.util.Map; -import java.util.StringJoiner; public class ConsoleResultView implements ResultView { private static final String EXECUTION_ANNOUNCEMENT_MESSAGE = "실행 결과"; @@ -39,11 +38,10 @@ private String formatWinnersMessage(List winners) { } private String joinNames(List names) { - StringJoiner joiner = new StringJoiner(", "); - names.forEach(joiner::add); - return joiner.toString(); + return String.join(", ", names); } + private String createPositionMarker(int position) { return POSITION_MARKER.repeat(position); } diff --git a/src/main/java/racinggame/InputView.java b/src/main/java/racinggame/view/InputView.java similarity index 82% rename from src/main/java/racinggame/InputView.java rename to src/main/java/racinggame/view/InputView.java index ae3ee7ad36a..ae6b8929aa8 100644 --- a/src/main/java/racinggame/InputView.java +++ b/src/main/java/racinggame/view/InputView.java @@ -1,4 +1,4 @@ -package racinggame; +package racinggame.view; import java.util.List; diff --git a/src/main/java/racinggame/ResultView.java b/src/main/java/racinggame/view/ResultView.java similarity index 90% rename from src/main/java/racinggame/ResultView.java rename to src/main/java/racinggame/view/ResultView.java index 432bbe7ca6a..403156d7cfe 100644 --- a/src/main/java/racinggame/ResultView.java +++ b/src/main/java/racinggame/view/ResultView.java @@ -1,4 +1,4 @@ -package racinggame; +package racinggame.view; import java.util.List; import java.util.Map; diff --git a/src/test/java/racinggame/RaceTest.java b/src/test/java/racinggame/domain/race/RaceTest.java similarity index 93% rename from src/test/java/racinggame/RaceTest.java rename to src/test/java/racinggame/domain/race/RaceTest.java index 2f6e99e76d6..8e5aecb21f3 100644 --- a/src/test/java/racinggame/RaceTest.java +++ b/src/test/java/racinggame/domain/race/RaceTest.java @@ -1,11 +1,10 @@ -package racinggame; +package racinggame.domain.race; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import racinggame.racingcar.MoveStrategy; -import racinggame.racingcar.Race; -import racinggame.racingcar.RandomMoveStrategy; +import racinggame.domain.strategy.MoveStrategy; +import racinggame.domain.strategy.RandomMoveStrategy; import static org.assertj.core.api.Assertions.assertThat; diff --git a/src/test/java/racinggame/RacingCarCreationTest.java b/src/test/java/racinggame/domain/racingcar/RacingCarsTest.java similarity index 90% rename from src/test/java/racinggame/RacingCarCreationTest.java rename to src/test/java/racinggame/domain/racingcar/RacingCarsTest.java index b0938f07059..f8344d605ac 100644 --- a/src/test/java/racinggame/RacingCarCreationTest.java +++ b/src/test/java/racinggame/domain/racingcar/RacingCarsTest.java @@ -1,9 +1,8 @@ -package racinggame; +package racinggame.domain.racingcar; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import racinggame.racingcar.MoveStrategy; -import racinggame.racingcar.RacingCars; +import racinggame.domain.strategy.MoveStrategy; import java.util.Arrays; import java.util.List; @@ -11,7 +10,7 @@ import static org.assertj.core.api.Assertions.assertThat; -class RacingCarCreationTest { +class RacingCarsTest { @Test @DisplayName("단일 자동차 객체 생성") diff --git a/src/test/java/racinggame/RacingCarMovementTest.java b/src/test/java/racinggame/domain/strategy/StrategyTest.java similarity index 92% rename from src/test/java/racinggame/RacingCarMovementTest.java rename to src/test/java/racinggame/domain/strategy/StrategyTest.java index 4888a1d6cfc..d8bfa7aefbc 100644 --- a/src/test/java/racinggame/RacingCarMovementTest.java +++ b/src/test/java/racinggame/domain/strategy/StrategyTest.java @@ -1,10 +1,8 @@ -package racinggame; +package racinggame.domain.strategy; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import racinggame.racingcar.RacingCars; -import racinggame.racingcar.RandomMoveStrategy; -import racinggame.racingcar.MoveStrategy; +import racinggame.domain.racingcar.RacingCars; import java.util.Arrays; import java.util.List; @@ -12,7 +10,7 @@ import static org.assertj.core.api.Assertions.assertThat; -class RacingCarMovementTest { +class StrategyTest { private static final int INIT_POSITION = 0; private static final int NUMBER_OF_ROUNDS = 4; diff --git a/src/test/java/racinggame/RacingGameInputViewTest.java b/src/test/java/racinggame/view/InputViewTest.java similarity index 98% rename from src/test/java/racinggame/RacingGameInputViewTest.java rename to src/test/java/racinggame/view/InputViewTest.java index 28bb0515377..62ed3fc851e 100644 --- a/src/test/java/racinggame/RacingGameInputViewTest.java +++ b/src/test/java/racinggame/view/InputViewTest.java @@ -1,4 +1,4 @@ -package racinggame; +package racinggame.view; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -17,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; -class RacingGameInputViewTest { +class InputViewTest { private final InputStream originalSystemIn = System.in; private final PrintStream originalSystemOut = System.out;