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
11 changes: 11 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ ] 경주할 자동차 이름 입력 받기 (쉼표로 구분, 5자 이하만 가능)

[ ] 시도할 횟수 입력 받기

[ ] 입력값에 대한 예외 처리 (IllegalArgumentException 발생 후 종료)

[ ] 각 차수별로 무작위 값(0~9)을 구해 4 이상일 경우 전진

[ ] 각 차수별 실행 결과 출력

[ ] 우승자 판별 및 출력 (중복 시 쉼표로 구분)
133 changes: 132 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,138 @@
package racingcar;
/*package racingcar;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
}
}*/
package racingcar;

import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;

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

public class Application {
public static void main(String[] args) {
// [변경 1] 예외 발생 시 애플리케이션 종료 요구 반영
try {
runRacingGame();
} catch (IllegalArgumentException e) {
throw e; // 잘못된 값 입력 시 예외 발생 후 종료
}
}

private static void runRacingGame() {
List<Car> cars = setupCars();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Car 클래스는 어디서 만드셨나요?
파일에도 없고 메인 클래스에서도 정의가 되지 않았는데 체크해주세요

int tryCount = readTryCount();

System.out.println("\n실행 결과");
for (int i = 0; i < tryCount; i++) {
playOneRound(cars);
printRoundStatus(cars);
}
printWinner(cars);
}

// [변경 2] 함수 분리를 통한 Indent(들여쓰기) depth 줄이기
// while문이나 if문 중첩을 피하기 위해 로직을 잘게 쪼개어 depth 2를 유지함
private static List<Car> setupCars() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String input = Console.readLine(); // Scanner 대신 전용 Console API 사용
return createCarList(input);
}

private static List<Car> createCarList(String input) {
List<Car> cars = new ArrayList<>();
for (String name : input.split(",")) { // 쉼표(,) 기준 구분 요구사항 반영
String trimmedName = name.trim();
validateName(trimmedName); // 이름은 5자 이하만 가능하다는 규칙 검증
cars.add(new Car(trimmedName));
}
return cars;
}

private static void validateName(String name) {
if (name.isEmpty() || name.length() > 5) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

isEmpty()도 있지만 isBlank()도 있습니다. 둘의 차이는 무엇일까요??

throw new IllegalArgumentException("이름은 5자 이하만 가능합니다.");
}
}

private static int readTryCount() {
System.out.println("시도할 회수는 몇회인가요?");
String input = Console.readLine();
return parseValidCount(input);
}

private static int parseValidCount(String input) {
try {
int count = Integer.parseInt(input);
validatePositive(count);
return count;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("숫자만 입력 가능합니다.");
}
}

private static void validatePositive(int count) {
if (count < 0) {
throw new IllegalArgumentException("시도 횟수는 0 이상이어야 합니다.");
}
}

private static void playOneRound(List<Car> cars) {
for (Car car : cars) {
moveIfLucky(car);
}
}

private static void moveIfLucky(Car car) {
// [변경 3] 전진 조건 반영 (0~9 무작위 값 중 4 이상일 때만 전진)
// Random 대신 전용 Randoms API 활용
if (Randoms.pickNumberInRange(0, 9) >= 4) {
car.move();
}
}

private static void printRoundStatus(List<Car> cars) {
for (Car car : cars) {
// 자동차 이름과 함께 전진 거리(-) 출력
System.out.println(car.getName() + " : " + "-".repeat(car.getPosition()));
}
System.out.println();
}

private static void printWinner(List<Car> cars) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

우승자는 여러 명일 수 있으니 메소드의 이름에서 Winner를 단수형이 아닌 복수형으로 사용해야 하지 않을까요?

int maxPos = getMaxPosition(cars);
List<String> winners = findWinners(cars, maxPos);
// [변경 4] 우승자가 여러 명일 경우 쉼표(,)로 구분 출력 요구사항 반영
System.out.println("최종 우승자 : " + String.join(", ", winners));
}

private static int getMaxPosition(List<Car> cars) {
int max = 0;
for (Car car : cars) {
// [변경 5] 3항 연산자 사용 금지 요구사항 반영
max = Math.max(max, car.getPosition());
}
Comment on lines 115 to 118
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

반복문도 좋지만 자바를 사용하시면 스트림을 더 자주 접할거에요.
스트림에 대해서 알아보고 적용해보시면 좋을 것 같아요!

return max;
}

private static List<String> findWinners(List<Car> cars, int maxPos) {
List<String> winners = new ArrayList<>();
for (Car car : cars) {
addIfWinner(winners, car, maxPos);
}
return winners;
}

private static void addIfWinner(List<String> winners, Car car, int maxPos) {
if (car.getPosition() == maxPos) {
winners.add(car.getName());
}
}
}