forked from next-step/java-racingcar
-
Notifications
You must be signed in to change notification settings - Fork 0
Step2 new : 자동차 경주 과제 #3
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
Open
kdongd
wants to merge
4
commits into
master
Choose a base branch
from
step2-new
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,4 @@ out/ | |
|
|
||
| ### VS Code ### | ||
| .vscode/ | ||
| .DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,134 @@ | ||
| # 자동차 경주 게임 | ||
| ## 진행 방법 | ||
| * 자동차 경주 게임 요구사항을 파악한다. | ||
| * 요구사항에 대한 구현을 완료한 후 자신의 github 아이디에 해당하는 브랜치에 Pull Request(이하 PR)를 통해 코드 리뷰 요청을 한다. | ||
| * 코드 리뷰 피드백에 대한 개선 작업을 하고 다시 PUSH한다. | ||
| * 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다. | ||
|
|
||
| ## 온라인 코드 리뷰 과정 | ||
| * [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview) | ||
| # 자동차 경주 게임 - Step2 | ||
|
|
||
| --- | ||
|
|
||
| ## Step2 구현 기능 목록 (요구사항 기반) | ||
|
|
||
| - 자동차 이름 입력 | ||
| - 쉼표(,) 기준으로 이름을 구분한다. | ||
| - 각 자동차에 이름을 부여한다. | ||
| - 자동차 이름은 5글자 초과 불가 (예외 발생) | ||
|
|
||
| - 실행 결과 출력 | ||
| - 라운드별 전진 결과 출력 시 자동차 **이름과 전진 표시**를 함께 출력한다. | ||
|
|
||
| - 최종 우승자 출력 | ||
| - 가장 멀리 간 자동차(들)를 우승자로 출력한다. | ||
| - 동점일 경우 우승자를 모두 출력한다. | ||
|
|
||
| --- | ||
|
|
||
| ## 패키지/클래스별 구현 내용 (작업 목록) | ||
|
|
||
| ### 1) 실행/흐름 제어 | ||
|
|
||
| #### `carrace.Application` | ||
| - 애플리케이션 시작 지점 | ||
| - 입력 → 레이스 진행 → 결과 출력까지의 전체 흐름을 연결한다. | ||
|
|
||
| #### `carrace.controller.RaceGame` | ||
| - 게임 진행(컨트롤) 책임 | ||
| - 입력값을 받아 도메인 객체를 생성하고, 레이스를 수행한 뒤 출력 뷰로 결과를 전달한다. | ||
|
|
||
| --- | ||
|
|
||
| ### 2) 도메인 - 자동차 | ||
|
|
||
| #### `carrace.domain.car.Car` | ||
| - **자동차 한 대의 상태(위치)와 행위(이동)**를 관리한다. | ||
| - `Car(String carName)` | ||
| - 자동차 생성 시 이름을 검증한다. | ||
| - **이름이 5글자 초과면 `IllegalArgumentException` 발생** (유효하지 않은 자동차는 생성되지 않도록 도메인 불변조건 보장) | ||
| - `move(MoveCondition condition)` | ||
| - 이동 조건이 참이면 위치를 한 칸 전진시킨다. | ||
| - `getPosition()` | ||
| - 현재 위치 값을 반환한다. | ||
| - `getCarName()` | ||
| - 자동차 이름을 반환한다. | ||
|
|
||
| #### `carrace.domain.car.CarCreator` | ||
| - 입력(문자열)로부터 자동차 목록을 생성하는 책임 | ||
| - 쉼표로 구분된 이름들을 기반으로 `Car` 객체들을 생성한다. | ||
| - (이름 trim/분리 등 “생성에 필요한 전처리”가 있다면 이 클래스에서 수행) | ||
|
|
||
| --- | ||
|
|
||
| ### 3) 도메인 - 레이스 | ||
|
|
||
| #### `carrace.domain.race.Race` | ||
| - **자동차 목록을 가지고 레이스 진행/우승자 계산**을 담당한다. | ||
| - 라운드 진행 결과를 기반으로 최종 우승자를 계산한다. | ||
| - 동점(여러 명 우승) 상황을 고려해 우승자 목록을 반환/출력할 수 있도록 구현한다. | ||
|
|
||
| --- | ||
|
|
||
| ### 4) 서비스 - 입력 처리 | ||
|
|
||
| #### `carrace.service.InputService` | ||
| - `CarRaceInputView`로부터 입력을 읽어오고, 숫자 변환 및 기본 검증을 수행한다. | ||
| - `readCarNames()` | ||
| - 자동차 이름 입력을 받는다. | ||
| - 쉼표 기준으로 분리 후 각 요소를 trim 처리한다. | ||
| - 빈 이름이 포함되면 예외를 발생시킨다. | ||
| - `readCarCount()` | ||
| - 자동차 대수 입력을 숫자로 파싱한다. | ||
| - 숫자가 아니면 예외를 발생시킨다. | ||
| - 1 미만이면 예외를 발생시킨다. | ||
| - `readRounds()` | ||
| - 시도 횟수 입력을 숫자로 파싱한다. | ||
| - 숫자가 아니면 예외를 발생시킨다. | ||
| - 1 미만이면 예외를 발생시킨다. | ||
|
|
||
| --- | ||
|
|
||
| ### 5) View - 입출력 | ||
|
|
||
| #### `carrace.view.CarRaceInputView` | ||
| - 사용자 입력을 담당한다. | ||
| - 자동차 이름 / 자동차 대수 / 시도 횟수를 입력받는다. | ||
|
|
||
| #### `carrace.view.CarRaceOutputView` | ||
| - 실행 결과 출력을 담당한다. | ||
| - 라운드별 실행 결과를 자동차 이름과 함께 출력한다. | ||
| - 최종 우승자를 출력한다(동점자 포함). | ||
|
|
||
| --- | ||
|
|
||
| ## 테스트 목록 (클래스별 / 검증 로직 설명) | ||
|
|
||
| ### 1) 자동차 도메인 테스트 | ||
|
|
||
| #### `src/test/java/carrace/domain/car/CarTest` | ||
| - 자동차의 이동 로직이 조건에 따라 정확히 동작하는지 검증한다. | ||
| - 자동차 이름 제약(5글자 초과 불가) 검증을 테스트한다. | ||
|
|
||
| #### `src/test/java/carrace/domain/car/CarCreatorTest` | ||
| - 쉼표로 구분된 자동차 이름 입력이 자동차 객체 목록으로 정상 생성되는지 검증한다. | ||
| - 이름 trim/분리 로직이 기대한 대로 동작하는지 검증한다. | ||
|
|
||
| --- | ||
|
|
||
| ### 2) 레이스 도메인 테스트 | ||
|
|
||
| #### `src/test/java/carrace/domain/race/RaceTest` | ||
| - 우승자가 1명인 경우 우승자 계산이 정확한지 검증한다. | ||
| - 동점(우승자 다수) 상황에서 우승자들이 모두 반환/출력 대상이 되는지 검증한다. | ||
|
|
||
| --- | ||
|
|
||
| ### 3) 입력 처리 테스트 | ||
|
|
||
| #### `src/test/java/carrace/service/InputServiceTest` | ||
| - 자동차 대수/시도 횟수 입력의 숫자 파싱 및 범위(1 이상) 검증을 테스트한다. | ||
| - 자동차 이름 입력의 기본 검증(빈 값/구분자 처리)이 정상 동작하는지 검증한다. | ||
|
|
||
| --- | ||
|
|
||
| ### 4) 테스트 유틸 | ||
|
|
||
| #### `src/test/java/carrace/fixture/CarsFixture` | ||
| - 테스트에서 사용할 자동차 목록/상태를 쉽게 만들기 위한 픽스처 제공 | ||
|
|
||
| #### `src/test/java/carrace/fixture/StubInputView` | ||
| - 입력 뷰를 대체하기 위한 스텁(고정 입력 제공) | ||
| - 입력 기반 테스트에서 예측 가능한 입력값을 주입하기 위해 사용 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package carrace.domain.car; | ||
|
|
||
|
|
||
| import carrace.domain.move.MoveCondition; | ||
|
|
||
| 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<Car> cars) { | ||
| validate(cars); | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| public static Cars from(String input) { | ||
| String[] names = input.split(","); | ||
| List<Car> cars = new ArrayList<>(); | ||
|
|
||
| for (String name : names) { | ||
| cars.add(new Car(name.trim())); | ||
| } | ||
| return new Cars(cars); | ||
| } | ||
|
|
||
| private void validate(List<Car> cars) { | ||
| if (cars.isEmpty()) { | ||
| throw new IllegalArgumentException("자동차는 최소 1대 이상이어야 합니다."); | ||
| } | ||
| validateDuplicateNames(cars); | ||
| } | ||
|
|
||
| private void validateDuplicateNames(List<Car> cars) { | ||
| Set<String> names = new HashSet<>(); | ||
| for (Car car : cars) { | ||
| if (!names.add(car.getCarName())) { | ||
| throw new IllegalArgumentException("자동차 이름은 중복될 수 없습니다."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void moveAll(MoveCondition condition) { | ||
| cars.forEach(car -> car.move(condition)); | ||
| } | ||
|
|
||
| public List<Car> getWinners() { | ||
| int maxPosition = cars.stream() | ||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .orElse(0); | ||
|
|
||
| return cars.stream() | ||
| .filter(car -> car.getPosition() == maxPosition) | ||
| .toList(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public List<Car> asList() { | ||
| return List.copyOf(cars); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,29 @@ | ||
| package carrace.domain.race; | ||
|
|
||
| import carrace.domain.car.Car; | ||
| import carrace.domain.car.Cars; | ||
| import carrace.domain.move.MoveCondition; | ||
|
|
||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Race { | ||
|
|
||
| private final List<Car> cars; | ||
| private final Cars cars; | ||
|
|
||
| public Race(List<Car> cars) { | ||
| public Race(Cars cars) { | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| public void moveOnce(MoveCondition condition) { | ||
| for (Car car : cars) { | ||
| car.move(condition); | ||
| } | ||
| cars.moveAll(condition); | ||
| } | ||
|
|
||
| //내부 상태 보호를 위해 자동차 목록을 그대로 반환하지 않고 | ||
| //읽기 전용 리스트(unmodifiableList)를 반환하도록 수정했습니다. | ||
| public List<Car> getCars() { | ||
| return List.copyOf(cars); | ||
| return cars.asList(); | ||
| } | ||
|
|
||
| public List<Car> getWinners() { | ||
| return cars.getWinners(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
해당 부분 stream 사용해보면 좋을 것 같습니다.
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.
넵 수정하겠습니다.