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

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import camp.nextstep.edu.missionutils.Console;

public class Application {

Choose a reason for hiding this comment

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

클래스가 여러 책임을 가지고 있어서 더 분리할 수 있을 것 같아요

public static void main(String[] args) {
// TODO: 프로그램 구현
Scanner sc = new Scanner(System.in);

Choose a reason for hiding this comment

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

여기서는 굳이 scanner가 필요하지 않을 것 같아요


List<Car> cars = getCars(sc);
int count = getTrialCount(sc);

raceCars(cars, count);
List<String> winners = getWinners(cars);

printWinners(winners);
}

private static List<Car> getCars(Scanner sc) {

Choose a reason for hiding this comment

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

메소드를 분리해주셨네요 좋습니다

System.out.print("차 이름들을 입력하세요 (쉼표로 구분): ");
String[] names = Console.readLine().split(",");

List<Car> cars = new ArrayList<>();
for (String name : names) {
cars.add(new Car(name.trim()));
}
return cars;
}

private static int getTrialCount(Scanner sc) {
System.out.print("시도할 횟수를 입력하세요: ");
return sc.nextInt();
}

private static void raceCars(List<Car> cars, int count) {
for (int i = 0; i < count; i++) {
for (Car car : cars) {
car.movement();
System.out.println(car.getName() + " : " + "-".repeat(car.getPosition()));
}
}
}

private static List<String> getWinners(List<Car> cars) {
int maxPosition = 0;
for (Car car : cars) {
maxPosition = Math.max(maxPosition, car.getPosition());
}

List<String> winners = new ArrayList<>();
for (Car car : cars) {

Choose a reason for hiding this comment

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

좀 더 함수들을 세분화할 수 있을 것 같아요

if (car.getPosition() == maxPosition) {
winners.add(car.getName());
}
}
return winners;
}

private static void printWinners(List<String> winners) {
System.out.println("최종 우승자 : " + String.join(", ", winners));
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package racingcar;

import static camp.nextstep.edu.missionutils.Randoms.pickNumberInRange;

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

public Car(String name) {
if (name.length() > 5) {
throw new IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다.");
}
this.name = name;
}

public void movement() {
int randomNumber = pickNumberInRange(0, 9);
if (randomNumber >= 4) {
position++;
}
}

public String getName() {
return name;
}

public int getPosition() {
return position;
}
}