-
Notifications
You must be signed in to change notification settings - Fork 0
[재구현] PR #1
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
base: mijisu
Are you sure you want to change the base?
[재구현] PR #1
Changes from all commits
e74d2e9
32e6a42
69312ab
91bec61
fdb3c31
ac28340
ef5df01
50bd399
2dc73f5
8dc7c6d
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,59 @@ | ||
| # 구현 기능 목록 | ||
|
|
||
| - [ ] 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다. | ||
|
|
||
|
|
||
| - [X] 각 자동차에 이름을 부여할 수 있다. 전진하는 자동차를 출력할 때 자동차 이름을 같이 출력한다. | ||
| - [X] 자동차 이름은 쉼표(,)를 기준으로 구분 | ||
| - [X] 이름은 5자 이하만 가능 | ||
| - [X] null 이거나 공백 X | ||
| - [X] 자동차 이름이 같은 것이 있으면 안됨 | ||
|
|
||
|
|
||
| - [X] 사용자는 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다. | ||
| - [X] 1 이상의 정수 여야 함 | ||
|
|
||
|
|
||
| - [X] 전진하는 조건 : 0에서 9 사이에서 무작위 값을 구한 후, 무작위 값이 4 이상일 경우 | ||
| - [X] `Randoms.pickNumberInRange(0,9);` 사용 | ||
|
|
||
|
|
||
| - [ ] 자동차 경주 게임을 완료한 후 우승자를 알려준다. 우승자는 한 명 이상일 수 있다. | ||
| - [ ] 우승자가 여러 명일 경우 쉼표(,)를 이용하여 구분한다. | ||
|
|
||
|
|
||
| - [ ] 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시킨 후 애플리케이션은 종료되어야 한다. | ||
|
|
||
|
|
||
|
|
||
| --- | ||
| - 전체 흐름 예시 | ||
| ``` | ||
| 경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분) | ||
| pobi,woni,jun | ||
| 시도할 회수는 몇회인가요? | ||
| 5 | ||
|
|
||
| 실행 결과 | ||
| pobi : - | ||
| woni : | ||
| jun : - | ||
|
|
||
| pobi : -- | ||
| woni : - | ||
| jun : -- | ||
|
|
||
| pobi : --- | ||
| woni : -- | ||
| jun : --- | ||
|
|
||
| pobi : ---- | ||
| woni : --- | ||
| jun : ---- | ||
|
|
||
| pobi : ----- | ||
| woni : ---- | ||
| jun : ----- | ||
|
|
||
| 최종 우승자 : pobi, jun | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
| import racingcar.controller.MainController; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| MainController mainController = MainController.create(); | ||
| mainController.run(); | ||
| Console.close(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package racingcar.constants; | ||
|
|
||
| public class Constants { | ||
| public static final int MINIMUM_RANDOM_NUMBER = 0; | ||
| public static final int MAXIMUM_RANDOM_NUMBER = 9; | ||
| public static final int MOVE_THRESHOLD = 4; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package racingcar.controller; | ||
|
|
||
| import racingcar.domain.Car; | ||
| import racingcar.domain.Cars; | ||
| import racingcar.domain.TotalRound; | ||
| import racingcar.view.InputView; | ||
| import racingcar.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class MainController { | ||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
| private PlayController playController; | ||
|
|
||
| private MainController(InputView inputView, OutputView outputView) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| } | ||
|
|
||
| public static MainController create() { | ||
| return new MainController(InputView.getInstance(), OutputView.getInstance()); | ||
| } | ||
|
|
||
|
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. controller에 정적 팩토리 메서드 적용한거 상당히 인상 깊었습니다! |
||
| public void run() { | ||
| setup(); | ||
| playController.play(); | ||
| } | ||
|
|
||
| private void setup() { | ||
| Cars cars = createCars(); | ||
| TotalRound totalRound = createTotalRound(); | ||
| playController = PlayController.of(outputView, cars, totalRound); | ||
| } | ||
|
|
||
| private Cars createCars() { | ||
| return readUserInput(() -> { | ||
| List<String> carNames = inputView.readCarNames(); | ||
| List<Car> validCars = carNames.stream() | ||
| .map(Car::from) | ||
| .toList(); | ||
| return Cars.from(validCars); | ||
| }); | ||
| } | ||
|
|
||
| private TotalRound createTotalRound() { | ||
| return readUserInput(() -> { | ||
| int totalRound = inputView.readTotalRound(); | ||
| return TotalRound.from(totalRound); | ||
| }); | ||
| } | ||
|
|
||
| private <T> T readUserInput(Supplier<T> supplier) { | ||
| while (true) { | ||
| try { | ||
| return supplier.get(); | ||
| } catch (IllegalArgumentException e) { | ||
| outputView.printError(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package racingcar.controller; | ||
|
|
||
| import racingcar.domain.Car; | ||
| import racingcar.domain.Cars; | ||
| import racingcar.domain.TotalRound; | ||
| import racingcar.dto.CarDto; | ||
| import racingcar.dto.CarsDto; | ||
| import racingcar.dto.WinnerNamesDto; | ||
| import racingcar.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class PlayController { | ||
| private final OutputView outputView; | ||
| private final Cars cars; | ||
| private final TotalRound totalRound; | ||
|
Comment on lines
+16
to
+17
Owner
Author
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. 가지고 있는 필드가 많은 것 같아서 → PlayController 를 그대로 두고 GameManager 를 이용하는 것이 좋겠다. OR PlayController 의 play() 를 호출할 때 파라미터로 Cars, TotalRound 를 넘겨줘도 되겠다. |
||
|
|
||
| public PlayController(OutputView outputView, Cars cars, TotalRound totalRound) { | ||
| this.outputView = outputView; | ||
| this.cars = cars; | ||
| this.totalRound = totalRound; | ||
| } | ||
|
|
||
| public static PlayController of(OutputView outputView, Cars cars, TotalRound totalRound) { | ||
| return new PlayController(outputView, cars, totalRound); | ||
| } | ||
|
|
||
| public void play() { | ||
| outputView.printStartResult(); | ||
| IntStream.range(0, totalRound.getTotalRound()) | ||
| .forEach(i -> playRound()); | ||
| printWinners(); | ||
| } | ||
|
|
||
| private void playRound() { | ||
| cars.play(); | ||
| List<CarDto> carDtos = cars.provideCars().stream() | ||
| .map(car -> CarDto.of(car.provideName(), car.providePosition())) | ||
| .toList(); | ||
| CarsDto carsDto = CarsDto.from(carDtos); | ||
| outputView.printResult(carsDto); | ||
| } | ||
|
|
||
| private void printWinners() { | ||
| List<Car> winners = cars.decideWinner(); | ||
| WinnerNamesDto winnerNamesDto = WinnerNamesDto.from(winners); | ||
| outputView.printWinners(winnerNamesDto); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package racingcar.domain; | ||
|
|
||
| import racingcar.utils.CarNamesValidator; | ||
| import racingcar.utils.RandomNumberGenerator; | ||
|
|
||
| import static racingcar.constants.Constants.MOVE_THRESHOLD; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position; | ||
|
|
||
| private Car(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public static Car from(String name) { | ||
| CarNamesValidator.validateName(name); | ||
| return new Car(name); | ||
| } | ||
|
|
||
| public void play() { | ||
| int randomNumber = RandomNumberGenerator.generate(); | ||
| moveOrStop(randomNumber); | ||
| } | ||
|
|
||
| private void moveOrStop(int randomNumber) { | ||
| if (randomNumber >= MOVE_THRESHOLD) { | ||
| position++; | ||
| } | ||
| } | ||
|
|
||
| public boolean isEqualPosition(int value) { | ||
| return position == value; | ||
| } | ||
|
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. 여기서 파라미터로 Car를 받아서, car 객체의 position을 비교해주는 건 어떨까요?
Owner
Author
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. 오 그렇네요 최대한 getter 사용을 지양하도록 |
||
|
|
||
| public String provideName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int providePosition() { | ||
| return position; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package racingcar.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Cars { | ||
| private final List<Car> cars; | ||
|
|
||
| private Cars(List<Car> cars) { | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| public static Cars from(List<Car> cars) { | ||
| return new Cars(cars); | ||
| } | ||
|
|
||
| public void play() { | ||
| cars.forEach(Car::play); | ||
| } | ||
|
|
||
| public List<Car> decideWinner() { | ||
| int maxPosition = cars.stream() | ||
| .mapToInt(Car::providePosition) | ||
| .max() | ||
| .getAsInt(); | ||
|
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. Comparable 인터페이스를 구현하면, |
||
|
|
||
| return cars.stream() | ||
| .filter(car -> car.isEqualPosition(maxPosition)) | ||
| .toList(); | ||
| } | ||
|
|
||
| public List<Car> provideCars() { | ||
| return List.copyOf(cars); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package racingcar.domain; | ||
|
|
||
| import racingcar.utils.TotalRoundValidator; | ||
|
|
||
| public class TotalRound { | ||
| private final int totalRound; | ||
|
|
||
| private TotalRound(int totalRound) { | ||
| this.totalRound = totalRound; | ||
| } | ||
|
|
||
| public static TotalRound from(int totalRound) { | ||
| TotalRoundValidator.validatePositive(totalRound); | ||
| return new TotalRound(totalRound); | ||
| } | ||
|
|
||
| public int getTotalRound() { | ||
| return totalRound; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package racingcar.dto; | ||
|
|
||
| public class CarDto { | ||
| private final String name; | ||
| private final int position; | ||
|
|
||
| private CarDto(String name, int position) { | ||
| this.name = name; | ||
| this.position = position; | ||
| } | ||
|
|
||
| public static CarDto of(String name, int position) { | ||
| return new CarDto(name, position); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package racingcar.dto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class CarsDto { | ||
| private final List<CarDto> carDtos; | ||
|
|
||
| private CarsDto(List<CarDto> carDtos) { | ||
| this.carDtos = carDtos; | ||
| } | ||
|
|
||
| public static CarsDto from(List<CarDto> carDtos) { | ||
| return new CarsDto(carDtos); | ||
| } | ||
|
|
||
| public List<CarDto> getCarDtos() { | ||
| return carDtos; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package racingcar.dto; | ||
|
|
||
| import racingcar.domain.Car; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class WinnerNamesDto { | ||
| private final List<String> names; | ||
|
|
||
| private WinnerNamesDto(List<String> names) { | ||
| this.names = names; | ||
| } | ||
|
|
||
| public static WinnerNamesDto from(List<Car> cars) { | ||
| List<String> names = cars.stream() | ||
| .map(Car::provideName) | ||
| .toList(); | ||
|
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. DTO에서 생성로직을 가지는 것보다,
|
||
| return new WinnerNamesDto(names); | ||
| } | ||
|
|
||
| public List<String> getNames() { | ||
| return names; | ||
| } | ||
|
|
||
| public String getNameByIndex(int index) { | ||
| return names.get(index); | ||
| } | ||
|
|
||
| public int getSize() { | ||
| return names.size(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package racingcar.exception; | ||
|
|
||
| public enum ErrorMessage { | ||
| ERROR_CAPTION("[ERROR] "), | ||
| INVALID_CAR_NAMES("유효하지 않은 이름 형식 입니다."), | ||
| DUPLICATE_CAR_NAMES("서로 다른 이름을 입력해 주세요."), | ||
| INVALID_BLANK_CAR_NAME("유효하지 않은 이름 입니다."), | ||
| INVALID_CAR_NAME_LENGTH("이름은 5자 이하여야 합니다."), | ||
| NOT_NUMERIC_INPUT("숫자를 입력해 주세요."), | ||
| NOT_POSITIVE_INPUT("양수를 입력해 주세요."); | ||
|
|
||
| private final String message; | ||
|
|
||
| ErrorMessage(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return ERROR_CAPTION.message + message; | ||
| } | ||
| } |
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.
해당 상수들의 사용처가 각각 한개의 클래스밖에 없는거 같은데 이렇게 상수 클래스를 별도로 선언해서 사용하는것보다는 각 클래스에 넣어서 관리하는건 어떨까요?