-
Notifications
You must be signed in to change notification settings - Fork 8
[자동차경주] 이정민 미션 제출합니다 #5
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 |
|---|---|---|
| @@ -1 +1,14 @@ | ||
| # java-racingcar-precourse | ||
| 1. 자동차 이름 정하고 생성하는 함수(이름은 쉼표 기준으로 구분) | ||
| 입력 : pobi,woni,jun | ||
|
|
||
| 2. 쉼표로 문자열 나누기 | ||
|
|
||
| 3. 시도할 횟수 받기 | ||
| 만약 횟수가 0이하이면 오류 | ||
|
|
||
| 4. 경주 하기 | ||
| 자동차의 수가 4이상이면 -출력하고 자동차가 얼마나 움직이는지를 저장하는 int형 변수에 +1하기 | ||
| 반환 : 1등 자동차 이름 | ||
|
|
||
| 5. 결과 출력 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,98 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class Application { | ||
| public static class Car{ | ||
| //1. 자동차 이름을 검수하고 쉼표를 기준으로 나누는 함수 | ||
| public static List<String> cut(String input){ | ||
| if (input == null || input.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("error"); | ||
| } | ||
|
|
||
| List<String> carList = new ArrayList<>(Arrays.asList(input.split(","))); | ||
|
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 (String carName : carList) { | ||
| carName = carName.trim(); // 공백 제거 | ||
| if (carName.isEmpty()) { | ||
| throw new IllegalArgumentException("error"); | ||
| } | ||
| if (carName.length() > 5) { | ||
| throw new IllegalArgumentException("error"); | ||
| } | ||
|
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. if(carName.length()<0 || carNames.length()>5) 로 한번에 써주는게 더 보기 편할 것 같습니다! 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. 동의합니다~~ 저렇게 한번에 쓰는게 컴파일러가 돌아갈때도 좀더 효율적인 방법이라구 알고있습니다! |
||
| } | ||
| return carList; | ||
| } | ||
| } | ||
|
|
||
|
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. 한 파일에 한 class만 존재하도록 파일을 분리하는게 더 좋아보여요! 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. 동의합니다! 아니면 스크롤을 열심히 내려야하고 코드 찾기도 어렵더라구요..1학년때 1파일 2만자 코딩을 해본적 있는데 지옥이었습니다 |
||
| public static class Play { | ||
| // 자동차 이동 여부 결정 | ||
| public static boolean isMovable() { | ||
| return Randoms.pickNumberInRange(0, 9) >= 4; | ||
|
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. 이것두 이렇게 하드코딩대신 상수를 적극 활용하는것도 유지보수성을 높이는데 좋아요! |
||
| } | ||
|
|
||
| // 이동 거리 출력 | ||
| public static String getDistanceString(int distance) { | ||
| return "-".repeat(distance); | ||
| } | ||
|
|
||
| // 자동차 이동 및 출력 | ||
| public static void print(List<String> carList, int[] carDistances) { | ||
|
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 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 commentThe reason will be displayed to describe this comment to others. Learn more. 이 말에 적극 동의해요! 모든 로직과 입출력을 하나의 폴더도 아닌 파일에 다 넣다보면 코딩하는 중간에도 자신이 적은 코드들이 어디에 위치해 있는지 헷갈리고 그게 나중에는 오류로 이어지는 경우가 많더라고요... |
||
| for (int i = 0; i < carList.size(); i++) { | ||
| if (isMovable()) { | ||
| carDistances[i]++; | ||
| } | ||
|
|
||
| System.out.println(carList.get(i) + " : " + getDistanceString(carDistances[i])); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| public static void printWinners(List<String> carNames, int[] carDistances) { | ||
| int maxDistance = Arrays.stream(carDistances).max().orElse(0); | ||
| List<String> winners = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < carNames.size(); i++) { | ||
| if (carDistances[i] == maxDistance) { | ||
| winners.add(carNames.get(i)); | ||
| } | ||
| } | ||
|
|
||
| // 우승자 출력 | ||
| System.out.println("final winner : " + String.join(", ", winners)); | ||
| } | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| System.out.println("Input String: "); | ||
| String input = Console.readLine(); | ||
|
|
||
| //자동차 생성 및 이름 리스트로 저장 및 배열 크기 구하기 | ||
| List<String> carNames = Car.cut(input); | ||
| int carNamesSize = carNames.size(); | ||
|
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.size()만 사용하는게 더 좋아보입니다...! |
||
|
|
||
| //시도할 횟수 받기 | ||
|
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. 여기에 숫자가 아닌 입력이 들어올때 예외를 발생시키는 코드도 넣으면 좋을것같아용 |
||
| System.out.println("Input number of times: "); | ||
| int tryNumber = Integer.parseInt(Console.readLine()); | ||
| if(tryNumber<1){ //횟수가 0이거나 음수일 수 없음 | ||
| throw new IllegalArgumentException("error"); | ||
| } | ||
|
|
||
| //자동차가 움직이는 횟수 저장하는 int형 배열 생성 | ||
| int[] carDistances = new int[carNamesSize]; | ||
|
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 commentThe reason will be displayed to describe this comment to others. Learn more. 자동차가 움직이는 횟수나 이름을 자동차 Class에 하나의 요소로 넣어서 따로 배열을 만들어서 관리하는 것보다 객체들로 관리하는게 더 편하고 나을 것 같아요! 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. 저도 동의합니다~~ 그렇게 하지 않으면 추후(이건 미션이라 하진 않지만 현업에서는) 유지보수할 때 car 관련을 수정할 때 main과 car를 전부 봐야해서 불편할 수도 있을 것 같아용 |
||
|
|
||
| //경주하기 | ||
| for (int i = 0; i < tryNumber; i++) { | ||
| Play.print(carNames, carDistances); | ||
| } | ||
|
|
||
| //우승자 출력 | ||
| printWinners(carNames, carDistances); | ||
| } | ||
| } | ||
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.
가능하다면 README 작성을 통해서 실제 코딩에 들어가기 전 머릿속에 전체적인 코드 구조를 생각하며 빠르게 구현할 수도 있어요. 단순히 과제에 나온 설명을 적는 것 말고도 실제 필요할 듯한 함수나 클래스들을 미리 구상하면서 간단한 설명과 함께 README에 적고 시작하면 훨씬 수월할 거예요!