-
Notifications
You must be signed in to change notification settings - Fork 9
김승찬 - 자동차 경주 게임 #8
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: sschan99
Are you sure you want to change the base?
Changes from 12 commits
c6c10c9
ac4c112
46f4043
928b6a1
7d34efb
da2a2d9
17cf22c
82a426b
7b491be
975675b
b379fe3
e0233fd
ab07e6b
2926732
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,7 +1,36 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO 구현 진행 | ||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
|
|
||
| List<Car> cars = initializeCars(inputView.receiveCarNames()); | ||
| int attemptNumber = inputView.receiveAttemptNumber(); | ||
|
|
||
| outputView.printPreface(); | ||
| for (int i = 0; i < attemptNumber; i++) { | ||
| moveCars(cars); | ||
| outputView.printExecutionResults(cars); | ||
| } | ||
|
|
||
| outputView.printFinalWinner(cars); | ||
| } | ||
|
|
||
| private static List<Car> initializeCars(List<String> carNames) { | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (String name : carNames) { | ||
| cars.add(new Car(name)); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| private static void moveCars(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| car.forward(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.Console.readLine; | ||
|
Collaborator
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. static import 보다는 그냥 해당 클래스를 import하는게 조금 더 좋은 것으로 알고 있습니다. STATIC import 는 주로 상수를 가져올때 많이 쓰는것 같던데, 혹시 해당 부분에서 함수를 static import하신 이유가 있으신가요?
Author
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. 코드의 가독성을 높이기 위해 static import를 사용했습니다. 별개로 질문드리고 싶은 것은 import문의 경우 컴파일 시에 처리하니까 성능적인 차이는 없는게 아닌가요? |
||
|
|
||
| public class InputView { | ||
| public List<String> receiveCarNames() { | ||
| while (true) { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| List<String> carNames = Arrays.asList(readLine().split(",")); | ||
| try { | ||
| validate(carNames); | ||
| } catch (IllegalArgumentException iae) { | ||
|
Collaborator
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. 메번 에러 메세지 변수명을 e로만 지었는데 iae와 같이 조금 더 정보가 있는 이름도 좋은것 같습니다. 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(iae.getMessage()); | ||
| continue; | ||
| } | ||
| return carNames; | ||
| } | ||
| } | ||
|
|
||
| private void validate(List<String> carNames) { | ||
|
||
| for (String name : carNames) { | ||
| if (name.length() > 5) { | ||
| throw new IllegalArgumentException("[ERROR] 자동차의 이름은 5자 이하만 가능합니다."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public int receiveAttemptNumber() { | ||
| while (true) { | ||
| System.out.println("시도할 횟수는 몇 회인가요?"); | ||
| String AttemptNumber = readLine(); | ||
| try { | ||
| validate(AttemptNumber); | ||
| } catch (IllegalArgumentException iae) { | ||
| System.out.println(iae.getMessage()); | ||
| continue; | ||
| } | ||
| return Integer.parseInt(AttemptNumber); | ||
| } | ||
| } | ||
|
|
||
| private void validate(String AttemptNumber) { | ||
| if (!Pattern.matches("^[0-9]+$", AttemptNumber)) { | ||
| throw new IllegalArgumentException("[ERROR] 숫자만 입력할 수 있습니다."); | ||
| } | ||
| } | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class OutputView { | ||
| public void printExecutionResults(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| System.out.println(car); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public void printPreface() { | ||
| System.out.println(); | ||
| System.out.println("실행 결과"); | ||
| } | ||
|
|
||
| public void printFinalWinner(List<Car> cars) { | ||
| List<String> finalWinner = new ArrayList<>(); | ||
| int max = 0; | ||
| for (Car car : cars) { | ||
| if (car.getPosition() > max) { | ||
| finalWinner.clear(); | ||
| max = car.getPosition(); | ||
| } | ||
| if (car.getPosition() == max) { | ||
| finalWinner.add(car.getName()); | ||
| } | ||
| } | ||
|
||
| System.out.println("최종 우승자 : " + String.join(", ", finalWinner)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # 기능 목록 | ||
|
|
||
| - 경주할 자동차를 입력 | ||
| - 각 자동차의 이름이 5자 이하인지 확인 | ||
| - 시도할 횟수를 입력 | ||
| - 각 차수별로 하위 기능들을 실행 | ||
| - 0~9의 무작위 값을 생성 | ||
| - 4 이상의 값인지 판단 | ||
| - 4 이상이라면 자동차를 전진 | ||
| - 각 차수의 결과를 출력 | ||
| - 우승자 목록을 생성 | ||
| - 최종 우승자를 출력 |
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.
StringBuilder를 사용하는 이유가 + 연산보다 속도가 빠르기 때문으로 알고 있습니다. 혹시 bar만 StringBuilder를 쓰고 return 에서는 + 연산을 사용한 이유가 있을까요?
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.
java 컴파일러에서 + 연산을 StringBuilder의 append로 바꿔준다고 합니다!
따라서 둘 중 무엇을 사용하든 성능상으로는 비슷하다고 하는데, 개인적으로는 간결하게 + 연산을 사용하는 것을 좋아합니다. :)
그러나 반복문 내에서는 컴파일시에 변환이 안된다고 하여 StringBuilder를 사용했습니다!
참고
https://stackoverflow.com/questions/7817951/string-concatenation-in-java-when-to-use-stringbuilder-and-concat
https://choichumji.tistory.com/135