-
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
자동차 경주 - 5단계 #6083
자동차 경주 - 5단계 #6083
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package racingcar; | ||
|
||
import racingcar.view.InputView; | ||
|
||
public class RaceController { | ||
|
||
public void runRace() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package racingcar; | ||
package racingcar.domain; | ||
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. 정리 잘 해주셨네요 👍 |
||
|
||
public class Car { | ||
private static final int MAX_CAR_NAME_LENGTH = 5; | ||
private final String name; | ||
private int location; | ||
|
||
|
@@ -14,7 +15,7 @@ private void validateName(String name) { | |
if (name == null || name.isBlank()) { | ||
throw new RuntimeException("자동차 이름은 공백일 수 없습니다."); | ||
} | ||
if (name.length() > 5) { | ||
if (name.length() > MAX_CAR_NAME_LENGTH) { | ||
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. 요청사항 반영 💯 |
||
throw new RuntimeException("자동차 이름은 1자 이상 5자 이하만 가능합니다."); | ||
} | ||
} | ||
|
@@ -25,10 +26,6 @@ public void go(MovingStrategy movingStrategy) { | |
} | ||
} | ||
|
||
public boolean isSameLocation(int compareLocation) { | ||
return compareLocation == location; | ||
} | ||
|
||
public int getLocation() { | ||
return location; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package racingcar.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class Cars { | ||
private final List<Car> cars; | ||
|
||
public Cars(List<String> names) { | ||
validateDuplicateName(names); | ||
List<Car> tempCars = new ArrayList<>(); | ||
for (String name : names) { | ||
tempCars.add(new Car(name)); | ||
} | ||
this.cars = tempCars; | ||
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. 이 부분은 // 16이전
this.cars = names.stream()
.map(Car::new)
.collect(Collectors.toList());
// 16이후
this.cars = names.stream()
.map(Car::new)
.toList(); |
||
} | ||
|
||
private void validateDuplicateName(List<String> names) { | ||
Set<String> uniqueNames = new HashSet<>(names); | ||
if (uniqueNames.size() != names.size()) { | ||
throw new RuntimeException("중복된 자동차 이름이 존재합니다."); | ||
} | ||
} | ||
|
||
public void moveCars(MovingStrategy movingStrategy) { | ||
for (Car car : cars) { | ||
car.go(movingStrategy); | ||
} | ||
} | ||
|
||
public List<CarInfo> getCarsNameAndLocation() { | ||
return cars.stream() | ||
.map(CarInfo::new) | ||
.toList(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar; | ||
package racingcar.domain; | ||
|
||
public interface MovingStrategy { | ||
boolean canGo(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar; | ||
package racingcar.domain; | ||
|
||
public class Race { | ||
private int remainingRounds; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package racingcar.domain; | ||
|
||
import java.util.List; | ||
|
||
public class RaceResult { | ||
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. 유틸도 좋은데 아예 |
||
private RaceResult() { | ||
throw new UnsupportedOperationException("유틸 클래스의 인스턴스를 생성할 수 없습니다."); | ||
} | ||
|
||
public static int findMaxLocation(List<CarInfo> cars) { | ||
return cars.stream() | ||
.mapToInt(CarInfo::location) | ||
.max() | ||
.orElseThrow(() -> new RuntimeException("자동차가 존재하지 않습니다.")); | ||
} | ||
|
||
public static List<String> determineWinner(List<CarInfo> cars) { | ||
int maxLocation = findMaxLocation(cars); | ||
return cars.stream() | ||
.filter(car -> car.isSameLocation(maxLocation)) | ||
.map(CarInfo::name) | ||
.toList(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar; | ||
package racingcar.domain; | ||
|
||
import java.util.Random; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package racingcar; | ||
package racingcar.view; | ||
|
||
import java.util.List; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package racingcar; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import racingcar.domain.CarInfo; | ||
|
||
class CarInfoTest { | ||
@Test | ||
@DisplayName("거리가 동일하면 true를 반환한다.") | ||
void isSameLocationTest_sameLocation() { | ||
// given | ||
int location = 4; | ||
CarInfo carInfo = new CarInfo("pobi", location); | ||
|
||
// when | ||
boolean result = carInfo.isSameLocation(location); | ||
|
||
// then | ||
assertThat(result).isTrue(); | ||
} | ||
|
||
@Test | ||
@DisplayName("거리가 동일하지 않으면 false를 반환한다.") | ||
void isSameLocationTest_differentLocation() { | ||
// given | ||
int location = 4; | ||
CarInfo carInfo = new CarInfo("pobi", location); | ||
|
||
// when | ||
boolean result = carInfo.isSameLocation(location + 1); | ||
|
||
// then | ||
assertThat(result).isFalse(); | ||
} | ||
|
||
} |
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.
아주 소소한 의견이긴 한데,
InputView
에서 이 역할까지 하는건 어떨까 생각이 들었습니다 😄사용자로부터 문자값을 입력 받고 그걸 처리한 결과를 전달하는 역할을 부여하면
좀 더 생성자 부분도 정리 될 것 같습니다 😄
개선까지는 아니고 정리 차원에서 이런 의견도 있구나 정도만 생각해주시면 될 것 같습니다 🙇