-
Notifications
You must be signed in to change notification settings - Fork 55
[조재민_BackEnd] 과제 제출합니다. #58
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 4 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,35 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class Car { | ||
| private final int MAX_NAME = 5; | ||
|
|
||
| public String[] getCarNames() { | ||
| // 이름 입력 | ||
| System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); | ||
| String carString = Console.readLine(); | ||
| String[] carNames = carString.split(","); | ||
|
|
||
| // 이름 예외 처리 | ||
| for (String carName : carNames) { | ||
| if (carName.length() > MAX_NAME) { | ||
| throw new IllegalArgumentException("[ERROR] 이름은 5자 이하만 가능합니다."); | ||
| } | ||
|
Comment on lines
+23
to
+25
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. for 문 안에 있는 조건문을 함수로 만들어 Depth를 줄이면 좋을 것 같습니다 👍 |
||
| } | ||
| return carNames; | ||
| } | ||
|
|
||
| public int getRounds() { | ||
|
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. Round는 |
||
| // 시도 횟수 입력 | ||
| System.out.println("시도할 횟수를 입력하세요."); | ||
| String input = Console.readLine(); | ||
|
|
||
| // 시도 횟수 예외 처리 | ||
| try { | ||
| int rounds = Integer.parseInt(input); | ||
| return rounds; | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("[ERROR] 시도 횟수는 숫자여야 합니다."); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Racing { | ||
| private final String[] carNames; | ||
| private final int rounds; | ||
| private final int[] positions; | ||
|
Comment on lines
+7
to
+9
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. carNames, positions 를 Racing에서 따로 String[]으로 선언하지않고 이렇게 하면 이름과 위치가 한 객체 안에서 함께 관리되기 때문에 데이터 일관성 유지 (인덱스 잘못 맞출 위험)면에서 좋을거같아요 |
||
|
|
||
| public Racing(String[] carNames, int rounds) { | ||
| this.carNames = carNames; | ||
| this.rounds = rounds; | ||
| this.positions = new int[carNames.length]; | ||
| } | ||
|
|
||
| // 레이스 시작 | ||
| public void startRace() { | ||
| System.out.println("\n실행 결과"); | ||
| for (int i = 0; i < rounds; i++) { | ||
| playRound(); | ||
| printResult(); | ||
| } | ||
| printWinners(); | ||
| } | ||
|
|
||
| // 레이스 진행 | ||
| private void playRound() { | ||
| final int MOVE_NUMBER = 4; | ||
| final int START_NUMBER = 0; | ||
| final int END_NUMBER = 9; | ||
|
Comment on lines
+43
to
+45
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. 상수 처리 잘 해주셨습니다 👍 |
||
|
|
||
| for (int i = 0; i < carNames.length; i++) { | ||
| int randomNumber = Randoms.pickNumberInRange(START_NUMBER, END_NUMBER); | ||
| if (randomNumber >= MOVE_NUMBER) { | ||
| positions[i]++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 결과 출력 | ||
| private void printResult() { | ||
| for (int i = 0; i < carNames.length; i++) { | ||
| System.out.println(carNames[i] + " : " + "-".repeat(positions[i])); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| // 최종 승리자 | ||
| private void printWinners() { | ||
| int maxPosition = 0; | ||
| for (int nowPosition : positions) { | ||
| if (nowPosition > maxPosition) { | ||
| maxPosition = nowPosition; | ||
| } | ||
| } | ||
|
|
||
| List<String> winners = new ArrayList<>(); | ||
| for (int i = 0; i < carNames.length; i++) { | ||
| if (positions[i] == maxPosition) { | ||
| winners.add(carNames[i]); | ||
| } | ||
| } | ||
|
|
||
| String winnerString = String.join(", ", winners); | ||
| System.out.println("최종 우승자 : " + winnerString); | ||
| } | ||
|
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. 위와 마찬가지로 최대한 코드 Depth를 줄여보시면 좋을 것 같아요! |
||
| } | ||
|
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. EOF 에 대해 한번 알아보시면 좋을거같습니다. |
||
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.
Car 클래스가 지금은 코드의 비즈니스 로직이 진행되는 Racing 클래스내부에서 활용되지않고 입력받는용도로 활용되고있는거같아요.
Car 객체 리스트 대신
String[] carNames로 사용되고있는데, Car 객체를 활용해서 해보는건 어떨까요?