Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

step 5 : 5단계 - 자동차 경주(리팩토링) #5804

Open
wants to merge 6 commits into
base: slowth-kim
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package racinggame.racingcar;
package racinggame.domain.race;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Race 의 경우 RacingCars 만 포함하고 있습니다.

Race 의 로직을 RacingCars 로 옮긴다면 Race 는 제거 가능하지 않을까요?


import racinggame.domain.racingcar.RacingCars;
import racinggame.domain.strategy.MoveStrategy;

import java.util.List;
import java.util.Map;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/racinggame/domain/racingcar/CarName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package racinggame.domain.racingcar;

public class CarName {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package racinggame.racingcar;
package racinggame.domain.racingcar;

public class CarPosition {
private int value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package racinggame.racingcar;
package racinggame.domain.racingcar;

import racinggame.domain.strategy.MoveStrategy;

public class RacingCar {
private final CarName name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package racinggame.racingcar;
package racinggame.domain.racingcar;

import racinggame.domain.strategy.MoveStrategy;

import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -11,11 +13,9 @@ private RacingCars(List<RacingCar> cars) {
}

public static RacingCars create(MoveStrategy moveStrategy, List<String> nameList) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변경이 가능하기 때문에 변수명엔 가능하면 자료형을 포함하지 않도록 해보면 좋을것 같아요.

list 는 목록이지만 프로그래밍에선 컬렉션의 하위 자료형이기 때문에
현재 인스턴스 변수명인 cars 와 같이 주로 s 를 붙혀서 복수로 표현하기도 합니다.

List<RacingCar> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package racinggame.racingcar;
package racinggame.domain.strategy;

public interface MoveStrategy {
boolean shouldMove();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package racinggame.racingcar;
package racinggame.domain.strategy;

import java.util.Random;

Expand Down
13 changes: 0 additions & 13 deletions src/main/java/racinggame/racingcar/CarName.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package racinggame;
package racinggame.view;

import racinggame.domain.racingcar.CarName;

import java.util.List;
import java.util.Scanner;
Expand All @@ -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;

Expand All @@ -42,8 +41,10 @@ private void printMessage(final String message) {
private List<String> readValidNames() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

도메인은 ui 에 접근하지 않는걸 권장하지만 ui 에서 도메인 접근은 문제 없다고 생각해요.
List<CarName> 을 그대로 리턴하도록 해보는건 어떨까요?

try {
List<String> 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();
Expand All @@ -56,19 +57,6 @@ private List<String> splitNames(String input) {
.collect(Collectors.toList());
}

private void validateNames(List<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "실행 결과";
Expand Down Expand Up @@ -39,11 +38,10 @@ private String formatWinnersMessage(List<String> winners) {
}

private String joinNames(List<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package racinggame;
package racinggame.view;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package racinggame;
package racinggame.view;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class RacingCarCreationTest {
class RacingCarsTest {

@Test
@DisplayName("단일 자동차 객체 생성")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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;
import java.util.Map;

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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package racinggame;
package racinggame.view;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -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;
Expand Down