diff --git a/src/main/java/racingcar/Application.java b/src/main/java/racingcar/Application.java index a17a52e..bd52571 100644 --- a/src/main/java/racingcar/Application.java +++ b/src/main/java/racingcar/Application.java @@ -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(); } } diff --git a/src/main/java/racingcar/Exception.java b/src/main/java/racingcar/Exception.java new file mode 100644 index 0000000..85e9959 --- /dev/null +++ b/src/main/java/racingcar/Exception.java @@ -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("시도 횟수는 숫자여야 합니다."); + } + } +} diff --git a/src/main/java/racingcar/Input.java b/src/main/java/racingcar/Input.java new file mode 100644 index 0000000..df059ff --- /dev/null +++ b/src/main/java/racingcar/Input.java @@ -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(); + } +} \ No newline at end of file