-
Notifications
You must be signed in to change notification settings - Fork 4
[자동차 경주] 박시영 미션 제출합니다. #2
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 10 commits
7bfb034
906e134
249d53f
cc79944
b621f47
3405b05
46f9f20
1966802
df2a5a2
7e93bb7
858e14a
06a7da1
c5b3d53
468d82b
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,16 @@ | ||
| # 기능 명세서 | ||
|
|
||
| ## | ||
|
|
||
| 경주할 자동차 이름을 쉼표로 구분해 입력받음 | ||
| 5자 이하가 아닐 시 에러발생 | ||
|
|
||
| 사용자가 몇 번의 이동을 할 것인지 입력받음 | ||
| 숫자가 아닌걸 입력하면 에러 발생 | ||
|
|
||
| 입력받은 이동 횟수만큼 반복 | ||
| 각 자동차마다 0부터 9까지 무작위 숫자를 random()으로 뽑아서 5가 넘을경우 한칸 앞으로 | ||
| 현재 각각 자동차가 얼마나 앞으로 갔는지 출력 | ||
|
|
||
| 최종 우승자가 여러명이면 쉼표로 구분해 출력 아니면 그냥 출력 | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class Model { | ||
| String[] car; | ||
| int[] len; | ||
| int numberOfAttempts; | ||
| int max = 0; | ||
|
|
||
| void saveCarName(String input){ | ||
|
|
||
| StringTokenizer st = new StringTokenizer(input, ","); | ||
| car = new String[st.countTokens()]; | ||
| len = new int[st.countTokens()]; | ||
| for(int i=0; i<car.length; i++){ | ||
| String str = st.nextToken(); | ||
| if(str.length()>5) throw new IllegalArgumentException("5글자 이하로 작성"); | ||
| car[i] = str; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| void saveNumberOfAttempts(String input){ | ||
|
|
||
| try { | ||
| numberOfAttempts = Integer.parseInt(input); | ||
| } catch (Exception e){ | ||
| System.out.println("숫자를 입력하세요"); | ||
| } | ||
|
Comment on lines
+31
to
+35
Contributor
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. 여기서는 아스키 코드를 이용해서 숫자 범위 안에 드는지 검사할 수 있을 듯! 근데 간단하게 짜려면 이렇게 짜도 되긴 합니당 |
||
|
|
||
| } | ||
|
|
||
| StringBuilder race(){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < car.length; i++) { | ||
| int rand = Randoms.pickNumberInRange(0, 9); | ||
| if(rand>=4) len[i]++; | ||
| sb.append(car[i]).append(" : ") | ||
| .append("-".repeat(Math.max(0, len[i]))) | ||
| .append("\n"); | ||
| max = Math.max(len[i], max); | ||
|
Contributor
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. 이 부분이 대충 경주 결과를 문자열로 나타내는 로직인 건 알겠는데, 정확히 어떤 과정을 통해 결과가 도출되는지 명확하지 않은 듯! 아마 최댓값을 넣는 등의 부분이 메서드 분리가 잘 안된 것도 있고 변수명이 직관적이지 않은 이유도 있는 것 같습니닷 ex. len이 length를 줄인 건 알겠는데 정확히 어떤 길이를 나타내는지? 명확하지 않음 |
||
| } | ||
|
|
||
| return sb; | ||
| } | ||
|
|
||
| StringBuilder findWinner(){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| boolean jointWinner = false; | ||
| for(int i=0; i<car.length; i++){ | ||
| if(len[i]==max) { | ||
| if(jointWinner) sb.append(", "); | ||
| jointWinner = true; | ||
| sb.append(car[i]); | ||
| } | ||
| } | ||
| return sb; | ||
| } | ||
|
Contributor
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. 이 분은 뭔가 알고리즘 느낌으로 최적화된 코드인 것 같은데, 아마도 유지보수의 측면에서는 우승자를 고르고 바로 StringBuilder에 넣기보다는 전체 우승자 목록을 먼저 반환한다음, 최종적으로 StringBuilder에 넣는 게 좋을 듯합니다...! |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class View { | ||
|
|
||
| String inputCarName(){ | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,)기준으로 구분)"); | ||
| return Console.readLine(); | ||
| } | ||
|
|
||
| String inputNumberOfAttempts(){ | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| return Console.readLine(); | ||
| } | ||
|
|
||
| void printResult(Model model){ | ||
| System.out.println("\n실행 결과"); | ||
| while (model.numberOfAttempts --> 0) { | ||
| System.out.println(model.race()); | ||
| } | ||
| } | ||
|
Contributor
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. MVC에서는 Controller 에서만 Model과 View가 만나고 View 코드 안에서는 Model과 관련된 부분이 웬만하면 없는 게 맞아서 사소하지만 model 객체가 아닌 model.race()를 직접 넣는 게 더 좋을 듯 합니다! |
||
|
|
||
| void printWinner(Model model){ | ||
| System.out.print("최종 우승자 : "); | ||
| System.out.println(model.findWinner()); | ||
| } | ||
|
Comment on lines
+18
to
+32
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. 출력도 따로 분리 했으면 좋았을거 같아용 |
||
|
|
||
| } | ||
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.
사소한 거긴 한데 if 문은 한 줄이라도 괄호를 쳐주는 게 좋긴 합니다!