-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: slowth-kim
Are you sure you want to change the base?
Changes from all commits
c8bc5f9
7d09a40
287c595
17cad01
982b72d
ba35531
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package racinggame.domain.racingcar; | ||
|
||
public class CarName { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,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<RacingCar> cars) { | |
} | ||
|
||
public static RacingCars create(MoveStrategy moveStrategy, List<String> nameList) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 변경이 가능하기 때문에 변수명엔 가능하면 자료형을 포함하지 않도록 해보면 좋을것 같아요. list 는 목록이지만 프로그래밍에선 컬렉션의 하위 자료형이기 때문에 |
||
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() { | ||
|
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; | ||
|
||
|
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; | ||
|
@@ -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<String> readValidNames() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 도메인은 ui 에 접근하지 않는걸 권장하지만 ui 에서 도메인 접근은 문제 없다고 생각해요. |
||
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(); | ||
|
@@ -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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racinggame; | ||
package racinggame.view; | ||
|
||
import java.util.List; | ||
|
||
|
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; | ||
|
There was a problem hiding this comment.
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 는 제거 가능하지 않을까요?