Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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: 9 additions & 2 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

Car car = new Car();
String[] carNames = car.getCarNames();
int rounds = car.getRounds();

Racing racing = new Racing(carNames, rounds);
Comment on lines +6 to +10
Copy link

Choose a reason for hiding this comment

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

Car 클래스가 지금은 코드의 비즈니스 로직이 진행되는 Racing 클래스내부에서 활용되지않고 입력받는용도로 활용되고있는거같아요.
Car 객체 리스트 대신 String[] carNames로 사용되고있는데, Car 객체를 활용해서 해보는건 어떨까요?

racing.startRace();

}
}
}
35 changes: 35 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package racingcar;
import camp.nextstep.edu.missionutils.Console;

public class Car {
private final int MAX_NAME = 5;

public String[] getCarNames() {
// 이름 입력
System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)");
String carString = Console.readLine();
String[] carNames = carString.split(",");

// 이름 예외 처리
for (String carName : carNames) {
if (carName.length() > MAX_NAME) {
throw new IllegalArgumentException("[ERROR] 이름은 5자 이하만 가능합니다.");
}
Comment on lines +23 to +25
Copy link

Choose a reason for hiding this comment

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

for 문 안에 있는 조건문을 함수로 만들어 Depth를 줄이면 좋을 것 같습니다 👍

}
return carNames;
}

public int getRounds() {
Copy link

Choose a reason for hiding this comment

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

Round는 Car 객체내부에서 사용되는 정보라기보다는 Racing 에서 사용되는 변수인거같아요. 이부분에 대해서는 생각해보시면 좋을거같습니다.

// 시도 횟수 입력
System.out.println("시도할 횟수를 입력하세요.");
String input = Console.readLine();

// 시도 횟수 예외 처리
try {
int rounds = Integer.parseInt(input);
return rounds;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[ERROR] 시도 횟수는 숫자여야 합니다.");
}
}
}
68 changes: 68 additions & 0 deletions src/main/java/racingcar/Racing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package racingcar;
import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.List;

public class Racing {
private final String[] carNames;
private final int rounds;
private final int[] positions;
Comment on lines +7 to +9
Copy link

Choose a reason for hiding this comment

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

carNames, positions 를 Racing에서 따로 String[]으로 선언하지않고
각각 하나의 Car 객체를 만들어서 List을 통해 사용하는게 객체지향적일거같아요.

이렇게 하면 이름과 위치가 한 객체 안에서 함께 관리되기 때문에 데이터 일관성 유지 (인덱스 잘못 맞출 위험)면에서 좋을거같아요


public Racing(String[] carNames, int rounds) {
this.carNames = carNames;
this.rounds = rounds;
this.positions = new int[carNames.length];
}

// 레이스 시작
public void startRace() {
System.out.println("\n실행 결과");
for (int i = 0; i < rounds; i++) {
playRound();
printResult();
}
printWinners();
}

// 레이스 진행
private void playRound() {
final int MOVE_NUMBER = 4;
final int START_NUMBER = 0;
final int END_NUMBER = 9;
Comment on lines +43 to +45
Copy link

Choose a reason for hiding this comment

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

상수 처리 잘 해주셨습니다 👍


for (int i = 0; i < carNames.length; i++) {
int randomNumber = Randoms.pickNumberInRange(START_NUMBER, END_NUMBER);
if (randomNumber >= MOVE_NUMBER) {
positions[i]++;
}
}
}

// 결과 출력
private void printResult() {
for (int i = 0; i < carNames.length; i++) {
System.out.println(carNames[i] + " : " + "-".repeat(positions[i]));
}
System.out.println();
}

// 최종 승리자
private void printWinners() {
int maxPosition = 0;
for (int nowPosition : positions) {
if (nowPosition > maxPosition) {
maxPosition = nowPosition;
}
}

List<String> winners = new ArrayList<>();
for (int i = 0; i < carNames.length; i++) {
if (positions[i] == maxPosition) {
winners.add(carNames[i]);
}
}

String winnerString = String.join(", ", winners);
System.out.println("최종 우승자 : " + winnerString);
}
Copy link

Choose a reason for hiding this comment

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

위와 마찬가지로 최대한 코드 Depth를 줄여보시면 좋을 것 같아요!

}
Copy link

Choose a reason for hiding this comment

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

EOF 에 대해 한번 알아보시면 좋을거같습니다.