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
8 changes: 8 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1. 자동차 이름입력받기
2. 시도할 경주횟수 입력받기
3. 차 객체 만들기
4. 경주차 리스트 만들기
5. 차 움직이기
6. 모든차의 위치 보여주기
7. 우승자 결정
8. 우승자 출력
39 changes: 38 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;

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

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
List<Car> cars = inputCarNames();
RacingResultPrinter printer = new RacingResultPrinter();
int attempts = inputAttemptCount();
RacingGame game = new RacingGame(cars);
System.out.println("\n실행 결과");
game.play(attempts);
printer.printWinners(game.findWinners());
}

private static List<Car> inputCarNames() {

Choose a reason for hiding this comment

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

static으로 선언한 이유가 있을까요?
다른 방식은 없었는지 궁금합니당

System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String input = Console.readLine();
String[] carNames = input.split(",");
List<Car> cars = new ArrayList<>();

for (String carName : carNames) {
carName = carName.trim();
if (carName.length() > 5) {
throw new IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다.");
}
Comment on lines +27 to +29

Choose a reason for hiding this comment

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

차 이름에 대한 검증을 메인클래스에서 진행하는게 어색하네요
Car 클래스나 따로 CarName이라는 클래스를 만들어서 진행하는 방법은 어떠신가요?

cars.add(new Car(carName));
}
return cars;
}

private static int inputAttemptCount() {
System.out.println("시도할 회수는 몇회인가요?");
try {
return Integer.parseInt(Console.readLine());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("숫자를 입력해야 합니다.");
}
Comment on lines +39 to +41

Choose a reason for hiding this comment

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

음수가 들어오면 어떻게 될까요?

}
}

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

import camp.nextstep.edu.missionutils.Randoms;

public class Car {
private final String carName;
private int position = 0;

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

public void move() {
if (Randoms.pickNumberInRange(0, 9) >= 4)

Choose a reason for hiding this comment

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

랜덤 숫자를 만드는 함수로 빼도 좋을 것 같아요
ex) power()

position++;
}

public int getPosition() {
return position;
}

public String getName() {
return carName;
}
}
36 changes: 36 additions & 0 deletions src/main/java/racingcar/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package racingcar;

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

public class RacingGame {
private final List<Car> cars;
private final RacingResultPrinter printer = new RacingResultPrinter();
public RacingGame(List<Car> cars) {
this.cars = cars;
}

public void play(int attempts) {
for (int i = 0; i < attempts; i++) {
cars.forEach(Car::move);
printer.printCurrentCarPosition(cars);
}
}

public List<String> findWinners() {
int fastest = 0;
for (Car car : cars) {
if (car.getPosition() > fastest) {
fastest = car.getPosition();
}
}
Comment on lines +22 to +26

Choose a reason for hiding this comment

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

findFastest() 라는 메서드로 뺼 수 있을 것 같아요!


List<String> winners = new ArrayList<>();
for (Car car : cars) {
if (car.getPosition() == fastest) {
winners.add(car.getName());
}
}
return winners;
}
}
19 changes: 19 additions & 0 deletions src/main/java/racingcar/RacingResultPrinter.java

Choose a reason for hiding this comment

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

출력관련 클래스를 만드셨네요 👍

Choose a reason for hiding this comment

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

입력 관련 클래스도 만들 수 있을 것 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package racingcar;

import java.util.List;

//출력관련 클래스
public class RacingResultPrinter {
//경주 후 차위치 출력
public void printCurrentCarPosition(List<Car> cars) {
for (Car car : cars) {
System.out.print(car.getName() + " : ");
System.out.println("-".repeat(car.getPosition()));
}
System.out.println();
}

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