Skip to content
59 changes: 59 additions & 0 deletions docs/README.md
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
```
7 changes: 6 additions & 1 deletion src/main/java/racingcar/Application.java
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();
}
}
7 changes: 7 additions & 0 deletions src/main/java/racingcar/constants/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package racingcar.constants;

Choose a reason for hiding this comment

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

해당 상수들의 사용처가 각각 한개의 클래스밖에 없는거 같은데 이렇게 상수 클래스를 별도로 선언해서 사용하는것보다는 각 클래스에 넣어서 관리하는건 어떨까요?

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;
}
63 changes: 63 additions & 0 deletions src/main/java/racingcar/controller/MainController.java
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());
}

Choose a reason for hiding this comment

The 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());
}
}
}
}
50 changes: 50 additions & 0 deletions src/main/java/racingcar/controller/PlayController.java
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
Copy link
Owner Author

@jisu-om jisu-om Nov 27, 2023

Choose a reason for hiding this comment

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

가지고 있는 필드가 많은 것 같아서
PlayController 가 아니라 서비스 계층인 GameManager 를 만들고 이 GameManager 가 Cars, TotalRound 를 가지고 있게 해도 되겠다.
그러면 MainController 에서 해야할 일이 좀 많아지겠다. 하나의 라운드 마다 carsDto를 받아서 outputView로 전달해줘야하니까.

→ PlayController 를 그대로 두고 GameManager 를 이용하는 것이 좋겠다.
playRound 할 때마다 CarsDto 생성해서 리턴하는 형식으로

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);
}
}
43 changes: 43 additions & 0 deletions src/main/java/racingcar/domain/Car.java
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;
}
Copy link

Choose a reason for hiding this comment

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

여기서 파라미터로 Car를 받아서, car 객체의 position을 비교해주는 건 어떨까요?
파라미터로 들어오는 Car의 position에 대한 getter가 필요할 것 같은데, 위 방법으로 getter를 없앨 수 있을 거 같습니다.

Copy link
Owner Author

Choose a reason for hiding this comment

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

오 그렇네요 최대한 getter 사용을 지양하도록
인자로 Car 를 넣는 것도 좋고
추가로 compareTo를 사용해도 되고


public String provideName() {
return name;
}

public int providePosition() {
return position;
}
}
34 changes: 34 additions & 0 deletions src/main/java/racingcar/domain/Cars.java
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();
Copy link

Choose a reason for hiding this comment

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

Comparable 인터페이스를 구현하면,
compareTo 메서드를 오버라이드 할 수 있고, 이를 이용해서 getter없이, Car 내부에서 최대 거리를 찾는 기능을 수행하도록 할 수 있습니다.
프리코스 3주차 피드백 중 - getter 대신 객체에 메세지를 보내자! 라는 테코블 참고자료를 보시는 것도 좋겠습니다.


return cars.stream()
.filter(car -> car.isEqualPosition(maxPosition))
.toList();
}

public List<Car> provideCars() {
return List.copyOf(cars);
}
}
20 changes: 20 additions & 0 deletions src/main/java/racingcar/domain/TotalRound.java
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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/racingcar/dto/CarDto.java
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;
}
}
19 changes: 19 additions & 0 deletions src/main/java/racingcar/dto/CarsDto.java
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;
}
}
32 changes: 32 additions & 0 deletions src/main/java/racingcar/dto/WinnerNamesDto.java
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();
Copy link

Choose a reason for hiding this comment

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

DTO에서 생성로직을 가지는 것보다,
Mapper 계층이나, 서비스 계층에서 데이터를 변환한 뒤에 DTO에 데이터를 넣어주는 형식으로 구현하는 것은 어떨까요?
DTO를 만드는데 자주 사용되는 record를 이번에 사용해보았는데, 자동 생성되는 코드의 생성자에서는, 그저 넣어주는 데이터를 받는 식으로 구현되어 있더라구요. Java 표준 API가 제시하는 방향이라 어느정도?이유가 있지 않을까 한번 생각해봅니다.

  • 만약, 도메인의 name 인스턴스 변수가 String이 아닌 새로운 타입으로 변환될 경우
    연관된 모든 dto의 생성로직을 손봐줘야할 것 같습니다.
    지금은 이 dto하나이지만, Cars로 부터 파생되는 dto가 20개라면, 20개의 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();
}
}
21 changes: 21 additions & 0 deletions src/main/java/racingcar/exception/ErrorMessage.java
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;
}
}
Loading