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
22 changes: 22 additions & 0 deletions src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
package racingcar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static camp.nextstep.edu.missionutils.Console.readLine;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
ArrayList<Car> cars = new ArrayList<>();

Choose a reason for hiding this comment

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

Suggested change
ArrayList<Car> cars = new ArrayList<>();
List<Car> cars = new ArrayList<>();

보통 이렇게 선언해서 사용합니다!
저번주 강의 보시고 다형성에 대해 공부해보면 좋을 것 같아요 :)

System.out.println("경주할 자동차 이름을 입력하세요.");
String name = readLine();

// 피드백: 배열 → List<String>으로 바꿈
List<String> nameList = Arrays.asList(name.split(","));

for (String n : nameList) { // 피드백: 배열 → List<String>으로 바꿈
Car car = new Car(n);
cars.add(car);
}

System.out.println("시도할 회수는 몇회인가요?");
int count = Integer.parseInt(readLine());

Racing race = new Racing();
race.startRace(cars, count);
}
}
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.Randoms;

public class Car {
private static final int MOVE_THRESHOLD = 4; // 피드백: 4를 의미있는 상수로
protected String name;
private String distance;

public Car(String Name) {
if (Name == null || Name.length() > 5)
throw new IllegalArgumentException();
Comment on lines +10 to +11

Choose a reason for hiding this comment

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

검증하는 로직은 메서드로 따로 빼도 좋을 것 같아요 :)

this.name = Name;
this.distance = "";
}

public void move() { // 피드백: 접근제어자 명시 (default → public)
int num = Randoms.pickNumberInRange(0, 9);
if (num >= MOVE_THRESHOLD) {
this.distance += "-";
}
}

public String getName() {
return name;
}

public String getDistance() {
return distance;
}

public int getDistanceLength() {
return distance.length();
}

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

import java.util.ArrayList;

public class Racing {
public void startRace(ArrayList<Car> cars, Integer count) {
for (int i = 0; i < count; i++) {
for (Car c : cars) {
c.move();
System.out.println(c.getName() + " : " + c.getDistance());
}
System.out.println();
}

int max = 0;
for (Car c : cars) {
if (c.getDistanceLength() > max) {
max = c.getDistanceLength();
}
}

ArrayList<String> names = new ArrayList<>();
for (Car c : cars) {
if (c.getDistanceLength() == max) {
names.add(c.getName());
}
}

System.out.println("최종 우승자 : " + String.join(",", names));
}
}