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: 10 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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

String[] cars = Input.InputNames().split(",");
Exception.ExceptionName(cars);

int tryNum = Exception.ExceptionNum(Input.InputCount());

RacingGame game = new RacingGame(cars);

game.play(tryNum);
game.printWinners();
}
}
40 changes: 40 additions & 0 deletions src/main/java/racingcar/Exception.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package racingcar;

public class Exception {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

클래스명이 Exception인 것은 좋지 않아 보입니다. 자바 기본 예외 타입인 Exception과 이름이 겹쳐 역할이 모호해질수도 있습니다. 입력 검증과 관련된 네이밍이면 좋을 것 같습니다

public static void ExceptionName(String[] cars){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ExceptionName, InputNames처럼 대문자로 시작하고 있는데, 자바 컨벤션에서는 메서드는 camelCase로 작성하는 것이 일반적입니담


for (int i = 0; i < cars.length; i++) {
if (cars[i].isBlank()) {
throw new IllegalArgumentException("이름은 비어 있을 수 없습니다.");
}
if (!cars[i].equals(cars[i].trim())) {
throw new IllegalArgumentException("이름 앞뒤 공백은 허용되지 않습니다.");
}
if (cars[i].contains(" ")) {
throw new IllegalArgumentException("이름에 공백은 포함될 수 없습니다.");
}
if (cars[i].length() > 5) {
throw new IllegalArgumentException("이름은 5자 이하여야 합니다.");
}

for (int j = i + 1; j < cars.length; j++) {
if (cars[i].equals(cars[j])) {
throw new IllegalArgumentException("중복된 이름은 사용할 수 없습니다.");
}
}
}
}
public static int ExceptionNum(String input) {
try {
int tryNum = Integer.parseInt(input);

if (tryNum < 1) {
throw new IllegalArgumentException("시도 횟수는 1 이상이어야 합니다.");
}

return tryNum;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("시도 횟수는 숫자여야 합니다.");
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/racingcar/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;

public class Input {

public static String InputNames() {
return Console.readLine();
}

public static String InputCount() {
return Console.readLine();
}
}
54 changes: 54 additions & 0 deletions src/main/java/racingcar/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;

public class RacingGame {
private final String[] cars;
private final int[] positions;
Comment on lines +6 to +7
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

cars와 positions를 병렬 배열로 관리하기보다 자동차 객체가 이름과 이동 횟수를 함께 가지도록 하면 상태 응집도가 더 높아질 것 같습니다.


public RacingGame(String[] cars) {
this.cars = cars;
this.positions = new int[cars.length];
}

public void play(int tryNum) {
System.out.println();
System.out.println("실행 결과");

for (int i = 0; i < tryNum; i++) {
for (int j = 0; j < cars.length; j++) {
if (Randoms.pickNumberInRange(0, 9) >= 4) {
positions[j]++;
}
System.out.print(cars[j] + " : ");
System.out.println("-".repeat(positions[j]));
}
System.out.println();
}
}

public void printWinners() {
int max = 0;

for (int i = 0; i < positions.length; i++) {
if (positions[i] > max) {
max = positions[i];
}
}

System.out.print("최종 우승자 : ");
boolean first = true;

for (int i = 0; i < positions.length; i++) {
if (positions[i] == max) {
if (!first) {
System.out.print(", ");
}
System.out.print(cars[i]);
first = false;
}
}

System.out.println();
}
}