-
Notifications
You must be signed in to change notification settings - Fork 12
[자동차 경주] 인상진 미션 제출합니다. #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: sangjin6439
Are you sure you want to change the base?
Changes from 2 commits
1125435
b43ad53
deaa75f
4c72aee
ff0be53
78a8e89
5428fac
7054708
4f51c61
1d18a2c
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,33 @@ | ||
| # 미션 - 자동차 경주 | ||
|
|
||
| ### car (input 조건) | ||
| - 입력받을 때 이름이 5글자 이하 or 예외처리 | ||
| - ','를 기준으로 이름을 입력받음. | ||
| - 사용자는 몇 번 시도할지 횟수를 입력받음. | ||
|
|
||
| ### play | ||
| - 0 ~ 9 사이의 랜덤 숫자 | ||
| - 4 이상일 때는 전진, 아니면 멈춤 | ||
|
|
||
| ### outputview | ||
| - 우승자는 한명 이상일 수 있다. | ||
| - 우승자는 여러 명일 경우 쉼표로 구분 | ||
| - 주사위 돌아갈 때마다 [car 이름: 이동 상태 출력] | ||
| - 예외처리 illegalArgumentException / 프로그램 종료 | ||
|
|
||
| ### testcode 작성하기 | ||
| - JUnit 5와 AssertJ를 이용하여 본인이 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인한다. | ||
| 테스트 도구 사용법이 익숙하지 않다면 test/java/study를 참고하여 학습한 후 테스트를 구현한다. | ||
| 작성한 테스트 코드의 Coverage가 도메인(모델) 패키지 90%, 코드 전체 60% 이상인지 확인한다. | ||
|
|
||
| ### 코드 컨벤션(요구사항) | ||
| - indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용한다. | ||
| 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다. | ||
| 힌트: indent(인덴트, 들여쓰기) depth를 줄이는 좋은 방법은 함수(또는 메서드)를 분리하면 된다. | ||
| - 3항 연산자를 쓰지 않는다. | ||
| - 함수(또는 메서드)가 한 가지 일만 하도록 최대한 작게 만들어라. | ||
| - else 예약어를 쓰지 않는다. | ||
| - if 조건절에서 값을 return하는 방식으로 구현하면 else를 사용하지 않아도 된다. | ||
| else를 쓰지 말라고 하니 switch/case로 구현하는 경우가 있는데 switch/case도 허용하지 않는다. | ||
| 함수(또는 메소드)의 길이가 15라인을 넘어가지 않도록 구현한다. | ||
| 함수(또는 메소드)가 한 가지 일만 잘 하도록 구현한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| package racingcar; | ||
|
|
||
| import racingcar.controller.GameController; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| GameController gameController = new GameController(); | ||
| gameController.gameStart(); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package racingcar.controller; | ||
|
|
||
| import racingcar.domain.Cars; | ||
| import racingcar.validate.CarNameValidate; | ||
| import racingcar.view.InputView; | ||
| import racingcar.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class GameController { | ||
|
|
||
| private final Cars cars = new Cars(); | ||
|
|
||
| public void gameStart() { | ||
| String[] carNames = InputView.inputCarName(); | ||
| CarNameValidate.validateCarName(carNames); | ||
| cars.inputCarName(carNames); | ||
| int repeat = InputView.showMoveCount(); | ||
| Racing(repeat); | ||
| OutputView.printWinnerCars(findWinners()); | ||
| } | ||
|
|
||
| public void Racing(int repeat) { | ||
| OutputView.outputMessage(); | ||
| for (int round = 0; round < repeat; round++) { | ||
| cars.carForward(); | ||
| OutputView.showCar(cars); | ||
| } | ||
| } | ||
|
|
||
| public List<String> findWinners() { | ||
| return cars.findWinners(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package racingcar.domain; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.awt.*; | ||
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class Car { | ||
|
|
||
| private String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void moveForward() { | ||
| position++; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package racingcar.domain; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class Cars { | ||
|
|
||
| private List<Car> cars; | ||
| private static final int FORWARD_NUM = 4; | ||
| private static final int MINIMUM_POSITION = 0; | ||
|
|
||
| public Cars() { | ||
| this.cars = new ArrayList<>(); | ||
| } | ||
|
|
||
| public void inputCarName(String[] carNames) { | ||
|
||
| for (String carName : carNames) { | ||
| cars.add(new Car(carName)); | ||
| } | ||
| } | ||
|
|
||
| public List<Car> getCars() { | ||
| return cars; | ||
| } | ||
|
|
||
| public void carForward() { | ||
| for (Car car : cars) { | ||
| if (createRandomNum() >= FORWARD_NUM) { | ||
| car.moveForward(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public int createRandomNum() { | ||
| return Randoms.pickNumberInRange(0, 9); | ||
| } | ||
|
|
||
| public List<String> findWinners() { | ||
|
||
| int maxPosition = MINIMUM_POSITION; | ||
| for (Car car : cars) { | ||
| if (car.getPosition() > maxPosition) { | ||
| maxPosition = car.getPosition(); | ||
| } | ||
| } | ||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
| if (car.getPosition() == maxPosition) { | ||
| winners.add(car.getName()); | ||
| } | ||
| } | ||
| return winners; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package racingcar.validate; | ||
|
|
||
| public class CarNameValidate { | ||
|
|
||
| public static void validateCarName(String[] carNames){ | ||
|
||
|
|
||
| for (String carName : carNames) { | ||
| if(carName.length() > 5){ | ||
| throw new IllegalArgumentException("자동차 이름은 5글자 이하로 만들어 주세요."); | ||
| } | ||
| if(carName.length() < 1){ | ||
| throw new IllegalArgumentException("자동차 이름은 1글자 이상으로 만들어 주세요."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package racingcar.view; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class InputView { | ||
|
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. View에 해당하는 클래스 메서드들을 static으로 한 이유가 궁금해요. non-static으로 하고 객체를 생성해서 사용하는게 좀더 좋지 않을까요?
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메서드를 사용하지 않고 객체를 만들어서 메서드를 사용하는게 더 좋을거 같네요! 길어지는게 싫어서 썼던거 같습니다. |
||
|
|
||
| public static String[] inputCarName() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| return Console.readLine().split(","); | ||
| } | ||
|
|
||
| public static int showMoveCount() { | ||
| System.out.println("시도할 회수는 몇 회인가요?"); | ||
| return Integer.parseInt(Console.readLine()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
|
|
||
| package racingcar.view; | ||
|
|
||
| import racingcar.domain.Car; | ||
| import racingcar.domain.Cars; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class OutputView { | ||
|
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. showCar, showCarPosition에서 문자열 조합 관련 로직이 보이는데 출력 메서드(ex - println)는 많은 자원을 소모해요 그래서 문자열을 조합한다음에 마지막에 한번에 출력하도록 리팩토링 해봐요 |
||
|
|
||
| public static void outputMessage() { | ||
| System.out.println("실행 결과"); | ||
| } | ||
|
|
||
| public static void showCar(Cars cars) { | ||
| List<Car> participants = cars.getCars(); | ||
| for (Car car : participants) { | ||
| System.out.print(car.getName() + " : "); | ||
| showCarPosition(car); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public static void showCarPosition(Car car) { | ||
| for (int i = 0; i < car.getPosition(); i++) { | ||
|
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. 좀 더 리팩토링 할 수 있을 것 같아요 String 관련 메서드를 찾아보세요 |
||
| System.out.print("-"); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public static void printWinnerCars(List<String> winners) { | ||
| System.out.print("최종 우승자 : " + String.join(", ", winners)); | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |
|
|
||
|
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. 단위 테스트도 한번 해보세요!! |
||
| import camp.nextstep.edu.missionutils.test.NsTest; | ||
| import org.junit.jupiter.api.Test; | ||
| import racingcar.domain.Cars; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomNumberInRangeTest; | ||
| import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; | ||
|
|
@@ -15,11 +18,11 @@ class ApplicationTest extends NsTest { | |
| @Test | ||
| void 전진_정지() { | ||
| assertRandomNumberInRangeTest( | ||
| () -> { | ||
| run("pobi,woni", "1"); | ||
| assertThat(output()).contains("pobi : -", "woni : ", "최종 우승자 : pobi"); | ||
| }, | ||
| MOVING_FORWARD, STOP | ||
| () -> { | ||
| run("pobi,woni", "1"); | ||
| assertThat(output()).contains("pobi : -", "woni : ", "최종 우승자 : pobi"); | ||
| }, | ||
| MOVING_FORWARD, STOP | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -31,6 +34,22 @@ class ApplicationTest extends NsTest { | |
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void 우승자_리스트() { | ||
|
|
||
| // given | ||
| Cars cars = new Cars(); | ||
| cars.inputCarName(new String[]{"babo","baba","bobo"}); | ||
|
|
||
| // when | ||
| cars.getCars().get(0).moveForward(); | ||
| cars.getCars().get(2).moveForward(); | ||
| List<String> winners = cars.findWinners(); | ||
|
|
||
| // then | ||
| assertThat(winners).containsExactly("babo","bobo"); | ||
| } | ||
|
|
||
| @Override | ||
| public void runMain() { | ||
| Application.main(new String[]{}); | ||
|
|
||
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.
변수명 첫글자가 대문자인데 큰 이유가 없다면 소문자(카멜 케이스)로 통일하는게 좋을 것 같아요.