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
40 changes: 39 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
package racingcar;

import camp.nextstep.edu.missionutils.Randoms;

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

String[] cars = Input.InputNames().split(",");
int max = 0;
Exception.ExceptionName(cars);
int tryNum = Exception.ExceptionNum(Input.InputCount());
int[] arr = new int[cars.length];

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) {
arr[j]++;
}
System.out.print(cars[j] + " : ");
System.out.println("-".repeat(arr[j]));
}
System.out.println();
}

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

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

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

System.out.println();
}
}
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 {
public static void ExceptionName(String[] cars){

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();
}
}