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
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
package racingcar;
import camp.nextstep.edu.missionutils.Console;

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

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

// initialize - 2 시도 횟수 입력
System.out.println("시도할 회수는 몇회인가요?");
String sTryCount = Console.readLine();
int tryCount = Validator.tryCountValidator(sTryCount);

Cars cars = new Cars(carNames);

RacingGame.playGame(cars, tryCount, new RandomMovingStrategy());

System.out.println("실행 결과");
RacingGame.printWinners(cars);
}
}
22 changes: 22 additions & 0 deletions 박도연/java-racingcar-6/src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package racingcar;

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

public Car() { this.name = null; }
public Car(String name) {
this.name = name;
}
public String getName() { return name; }
public int getPosition() { return position; }
public void setPosition(int position) { this.position = position; }
public void strategyMove(MovingStrategy strategy) {
if (strategy.canMove()) {
position++;
}
}
public void PrintInfo() {
System.out.println("Name: " + name + " Position: " + position);
}
}
49 changes: 49 additions & 0 deletions 박도연/java-racingcar-6/src/main/java/racingcar/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package racingcar;
import camp.nextstep.edu.missionutils.Console;
import java.util.ArrayList;
import java.util.List;

public class Cars {
private final List<Car> cars;

private Cars() { cars = new ArrayList<>(); }
public Cars(List<Car> cars) { this.cars = cars; }

public Cars(String[] carNames) {
this.cars = new ArrayList<>();
for (String carName : carNames) {
Validator.carNameValidator(carName);
cars.add(new Car(carName.trim()));
}
}

public List<Car> getCars() {
return cars;
}

public void printInfo() {
Copy link

Choose a reason for hiding this comment

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

개인적인 의견으로는 Car이나 Cars의 프린트하는 부분이나 비지니스로직 실행하는 부분이 다른 부분에서 값을 받아서 처리하는게 확장성이 더 좋을 것 같다는 생각이 있어요!

for (Car car : cars) {
System.out.print(car.getName() + " : ");
for (int i = 0; i < car.getPosition(); i++) {
System.out.print("-");
}
System.out.println();
}
}

public List<String> getWinners() {
int maxPosition = 0;

for (Car car : cars) {
maxPosition = Math.max(maxPosition, car.getPosition());
}

List<String> winners = new ArrayList<>();
for (Car car : cars) {
if (car.getPosition() == maxPosition) {
winners.add(car.getName());
}
}
return winners;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package racingcar;

public interface MovingStrategy {
boolean canMove();
}
22 changes: 22 additions & 0 deletions 박도연/java-racingcar-6/src/main/java/racingcar/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package racingcar;
import java.util.ArrayList;
import java.util.List;

public class RacingGame{
private RacingGame() {};

public static void playGame(Cars cars, int count, MovingStrategy strategy) {
for (int i = 0; i < count; i++) {
for(Car car : cars.getCars()) {
car.strategyMove(strategy);
}
cars.printInfo();
System.out.println();
}
}

public static void printWinners(Cars cars) {
List<String> winners = cars.getWinners();
System.out.println("최종 우승자 : " + String.join(", ", winners));
Copy link
Member

Choose a reason for hiding this comment

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

join 사용은 생각 못했네요

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package racingcar;
import camp.nextstep.edu.missionutils.Randoms;

public class RandomMovingStrategy implements MovingStrategy {
@Override
public boolean canMove() {
if (Randoms.pickNumberInRange(0, 9) >= 4) {
return true;
}
return false;
}
}
41 changes: 41 additions & 0 deletions 박도연/java-racingcar-6/src/main/java/racingcar/Validator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package racingcar;

public class Validator {
final static int MAX_NAME_LENGTH = 5;
Copy link
Member

Choose a reason for hiding this comment

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

상수로 사용하는거 좋은 것 같습니다


private Validator() {}
public static int tryCountValidator(String tryCountInput) {
int tryCount = 0;

// 1. 빈 입력값 예외 처리
if (tryCountInput == null || tryCountInput.trim().isEmpty()) {
throw new IllegalArgumentException("시도 횟수는 비어 있을 수 없습니다.");
}

try {
tryCount = Integer.parseInt(tryCountInput);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("시도 횟수는 양의 정수여야 합니다.");
}

// 양수인지 검증
if (tryCount <= 0) {
throw new IllegalArgumentException("시도 횟수는 양의 정수여야 합니다.");
}

return (tryCount);
}

public static void carNameValidator(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("자동차 이름은 빈 값이 될 수 없습니다.");
}

if (name.length() > MAX_NAME_LENGTH) {
throw new IllegalArgumentException("자동차 이름은 5자를 초과할 수 없습니다.");
}

if (name.contains(",")) {
throw new IllegalArgumentException("자동차 이름에 ','를 포함할 수 없습니다.");
}
Copy link
Member

Choose a reason for hiding this comment

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

어차피 strip(',') 하면서 공백으로 저장되서 isEmptry에서 걸러지니까 불필요할 것 같습니다

}}