Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package racingcar;

import java.util.List;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
List<String> carNames = InputView.getCarNames();
int tryCount = InputView.getTryCount();

List<Car> cars = CarFactory.createCars(carNames);

System.out.println();
Race race = new Race(cars, tryCount);
race.start();

ResultView.printWinners(race.getWinners());
Comment on lines +7 to +16
Copy link

Choose a reason for hiding this comment

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

사용자와 상호작용하는 기능과 게임 실행과 관련된 기능이 클래스 단위로 적절하게 잘 분리되어 있어서 좋습니다.

}
}
32 changes: 32 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package racingcar;

public class Car {
private static final int MOVE_THRESHOLD = 4;
private static final String POSITION_MARK = "-";
Comment on lines +4 to +5
Copy link

Choose a reason for hiding this comment

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

리터럴 값을 하드코딩하지 않고 상수로 선언한 점 좋아요


private final String name;
private int position = 0;

public Car(String name) {
this.name = name;
}

public void move(int randomNumber) {
if (randomNumber >= MOVE_THRESHOLD) {
position++;
}
}

public int getPosition() {
return position;
}

public String getName() {
return name;
}

@Override
public String toString() {
return name + " : " + POSITION_MARK.repeat(position);
}
}
14 changes: 14 additions & 0 deletions src/main/java/racingcar/CarFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package racingcar;

import java.util.List;
import java.util.stream.Collectors;
import racingcar.Car;

public class CarFactory {
public static List<Car> createCars(List<String> names) {
return names.stream()
.map(String::trim)
.map(Car::new)
.collect(Collectors.toList());
}
Comment on lines +9 to +13
Copy link

Choose a reason for hiding this comment

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

스트림을 사용하는 것은 코드의 유지보수성을 높여주는 등의 많은 이점이 있지만, 단순한 반복의 경우 일반 for문 또는 향상된 for문(foreach)이 속도에서 우수합니다.

단순한 반복일 경우 for문을 사용하는 것도 고려해보시면 좋을 것 같아요

}
28 changes: 28 additions & 0 deletions src/main/java/racingcar/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;

import java.util.Arrays;
import java.util.List;

public class InputView {
private static final int MAX_NAME_LENGTH = 5;

public static List<String> getCarNames() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String input = Console.readLine();
List<String> names = Arrays.asList(input.split(","));

for (String name : names) {
if (name.trim().length() > MAX_NAME_LENGTH) {
throw new IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다.");
}
}
return names;
}

public static int getTryCount() {
System.out.println("시도할 회수는 몇회인가요?");
return Integer.parseInt(Console.readLine());
}
}
Comment on lines +24 to +28
Copy link

Choose a reason for hiding this comment

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

사용자가 숫자가 아닌 값을 입력할 경우를 고려해봐야 할 것 같아요.

사용자의 입력은 예측할 수 없기에 유효한 값을 검증하고 유효한 값이 아닐 경우 어떻게 할 것인지에 대한 고민이 필요해 보입니다.

45 changes: 45 additions & 0 deletions src/main/java/racingcar/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;

import java.util.ArrayList;
import java.util.List;

public class Race {
private final List<Car> cars;
private final int tryCount;

public Race(List<Car> cars, int tryCount) {
this.cars = cars;
this.tryCount = tryCount;
}
Comment on lines +8 to +15
Copy link

Choose a reason for hiding this comment

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

서비스(게임)와 모델(자동차)을 분리한 것 좋아요.


public void start() {
for (int i = 0; i < tryCount; i++) {
moveCars();
ResultView.printRaceResult(cars);
}
}

private void moveCars() {
for (Car car : cars) {
int number = Randoms.pickNumberInRange(0, 9);
car.move(number);
}
}

public List<String> getWinners() {
int maxPosition = cars.stream()
.mapToInt(Car::getPosition)
.max()
.orElse(0);

List<String> winners = new ArrayList<>();
for (Car car : cars) {
if (car.getPosition() == maxPosition) {
winners.add(car.getName());
}
}
return winners;
}
}
16 changes: 16 additions & 0 deletions src/main/java/racingcar/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package racingcar;

import java.util.List;

public class ResultView {
public static void printRaceResult(List<Car> cars) {
for (Car car : cars) {
System.out.println(car);
}
System.out.println();
}

public static void printWinners(List<String> winners) {
System.out.println("최종 우승자 : " + String.join(", ", winners));
}
}