-
Notifications
You must be signed in to change notification settings - Fork 29
김도완_backend 2주차 과제제출 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("시도 횟수는 숫자여야 합니다."); | ||
| } | ||
| } | ||
| } | ||
| 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(); | ||
| } | ||
| } |
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
클래스명이 Exception인 것은 좋지 않아 보입니다. 자바 기본 예외 타입인 Exception과 이름이 겹쳐 역할이 모호해질수도 있습니다. 입력 검증과 관련된 네이밍이면 좋을 것 같습니다