-
Notifications
You must be signed in to change notification settings - Fork 28
[성찬영_BackEnd] 2주차 과제 제출합니다. #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
f301985
10a498c
e9062b9
7fb3d41
7863cdf
501ebde
24cb63d
7eaa067
b563773
6dcddaa
39dd8b9
a396893
1de88aa
3829a16
5c4d720
548efde
2809654
8a07464
9386a79
ff9702f
6edddbb
cf14d0d
a353413
20906f2
35ca1f4
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 @@ | ||
| # 기능 목록 | ||
|
|
||
| ## 입력 | ||
| - 경주할 자동차 이름을 ,기준으로 입력받는다. | ||
| - 시도할 횟수를 입력받는다. | ||
|
|
||
| ## 입력 검증 | ||
| - 자동차 이름이 공백이거나 빈값이면 `IllegalArgumentException`을 발생시킨다. | ||
| - 자동차 이름이 5자를 초과하면 `IllegalArgumentException`을 발생시킨다. | ||
| - 자동차 이름이 중복되면 `IllegalArgumentException`을 발생시킨다. | ||
| - 시도할 횟수가 숫자가 아니면 `IllegalArgumentException`을 발생시킨다. | ||
| - 시도할 횟수가 1 미만이면 `IllegalArgumentException`을 발생시킨다. | ||
|
|
||
| ## 자동차 이동 | ||
| - 0~9 사이의 무작위 값을 발생시킨다. | ||
| - 무작위 값이 4 이상이면 전진한다. | ||
|
|
||
| ## 출력 | ||
| - 각 횟수별 실행 결과를 자동차 이름과 함께 출력한다. | ||
| - 최종 우승자를 출력한다. | ||
| - 우승자가 여러 명일 경우 ,로 구분하여 출력한다. | ||
|
|
||
| ## 리팩토링 | ||
| - `Input` : 사용자 입력을 받는 책임만 담당하도록 분리한다. | ||
| - `Output` : 경기 결과 및 우승자 출력 책임만 담당하도록 분리한다. | ||
| - `RunRacingGame` : 게임 흐름 제어 책임만 담당하도록 분리한다. | ||
| - `Utils` 클래스를 삭제하고 각 도메인이 자신의 책임을 직접 수행하도록 변경한다. | ||
|
|
||
| ### 도메인은 자신이 검증 | ||
| - `Car` : 자동차 이름의 유효성(공백, 길이)을 Car 스스로 검증한다. | ||
| - `Cars` : 자동차 이름 중복 여부를 Cars 스스로 검증한다. | ||
| - `validation` : 시도 횟수 검증은 입력 경계에서 담당한다. | ||
|
|
||
| ### 이동횟수 관리 | ||
| - 이동 기준값을 매직넘버로 관리한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,5 +3,6 @@ | |
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| new RunRacingGame().run(); | ||
| } | ||
| } | ||
| } | ||
|
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. 깃허브에 코드를 올릴때는 맨 마지막 라인에 공백이 들어와야 해요! 자료 참고바랍니당
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. 넵 감사합니다. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package racingcar; | ||
|
|
||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import racingcar.entity.Car; | ||
| import racingcar.entity.Cars; | ||
| import racingcar.view.racingCarInput; | ||
| import racingcar.view.racingCarOutput; | ||
|
|
||
| public class RunRacingGame { | ||
| private racingCarInput input = new racingCarInput(); | ||
| private racingCarOutput output = new racingCarOutput(); | ||
|
|
||
| public void run() { | ||
|
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[] carNames = input.readCarNames(); | ||
| int tryCount = input.readTryCount(); | ||
|
|
||
| Cars cars = createCars(carNames); | ||
|
|
||
| System.out.println("\n실행 결과"); | ||
| for (int i = 0; i < tryCount; i++) { | ||
| cars.moveAllCars(); | ||
| output.printRaceResult(cars); | ||
| } | ||
| output.printWinner(cars.getWinners()); | ||
| } | ||
|
|
||
| private Cars createCars(String[] carNames) { | ||
| List<Car> carList = new ArrayList<>(); | ||
| for (String name : carNames) { | ||
| carList.add(new Car(name)); | ||
| } | ||
| return new Cars(carList); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package racingcar.entity; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
|
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.
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. 이름의 값은 변하지 않는다와 혹시 모를 이름 변경이 되는 것을 막기 위해 final로 선언했습니다. |
||
| private int moveCnt; | ||
| private static final int MOVE_THRESHOLD = 4; | ||
|
|
||
| public Car(String name) { | ||
| validateName(this.name = name); | ||
| this.moveCnt = 0; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| public int getMoveCnt() { | ||
| return this.moveCnt; | ||
| } | ||
|
|
||
| public void setMoveCnt() { | ||
| if (Randoms.pickNumberInRange(0, 9) >= MOVE_THRESHOLD) { | ||
| this.moveCnt++; | ||
| } | ||
| } | ||
|
|
||
| private void validateName(String name) { | ||
| if (name.isBlank() || name.contains(" ")) { | ||
| throw new IllegalArgumentException("이름 공백 또는 빈값은 안됩니다."); | ||
| } | ||
| if (name.length() > 5) { | ||
| throw new IllegalArgumentException("5글자 초과했습니다."); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||||||||||||||||||||||||||||||
| package racingcar.entity; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||
| import java.util.HashSet; | ||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public class Cars { | ||||||||||||||||||||||||||||||||||
| private List<Car> cars; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public Cars(List<Car> cars) { | ||||||||||||||||||||||||||||||||||
| validateDuplicate(cars); | ||||||||||||||||||||||||||||||||||
| this.cars = cars; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+6
to
+12
|
||||||||||||||||||||||||||||||||||
| public class Cars { | |
| private ArrayList<Car> cars; | |
| public Cars(ArrayList<Car> cars) { | |
| this.cars = cars; | |
| import java.util.List; | |
| public class Cars { | |
| private final List<Car> cars; | |
| public Cars(List<Car> cars) { | |
| this.cars = new ArrayList<>(cars); |
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.
depth를 더 줄인다면 이렇게도 될 거 같아요 !
Java에서 기본적으로 제공해주는 유용한 메소드들도 많아서, 한 번 찾아보면 좋을 거 같아요 👍
| private int findMaxMoveCnt() { | |
| int max = 0; | |
| for (Car car : this.cars) { | |
| if (car.getMoveCnt() > max) { | |
| max = car.getMoveCnt(); | |
| } | |
| } | |
| return max; | |
| } | |
| private int findMaxMoveCnt() { | |
| int max = -1; | |
| for (Car car : this.cars) { | |
| max = Math.max(max, car.getMoveCnt(); | |
| } | |
| return max; | |
| } |
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.
스트림을 적용해보셔도 좋을 것 같아요 (스트림쓰면 코드가 더 간지나고 예뻐보여요)
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.
넵 한번 적용해보겠습니다!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package racingcar.validation; | ||
|
|
||
| public class TryCountValidation { | ||
| public static int validate(String input) { | ||
| try { | ||
| int number = Integer.parseInt(input); | ||
| if (number < 1) { | ||
| throw new IllegalArgumentException("1 미만의 숫자는 입력할 수 없습니다."); | ||
| } | ||
| return number; | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("숫자를 입력해야 합니다."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package racingcar.view; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
| import racingcar.validation.TryCountValidation; | ||
|
|
||
| public class racingCarInput { | ||
| public String[] readCarNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| return Console.readLine().split(","); | ||
| } | ||
| public int readTryCount() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| return TryCountValidation.validate(Console.readLine()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package racingcar.view; | ||
|
|
||
| import java.util.List; | ||
| import racingcar.entity.Car; | ||
| import racingcar.entity.Cars; | ||
|
|
||
| public class racingCarOutput { | ||
|
|
||
| public void printRaceResult(Cars cars) { | ||
| for (Car car : cars.getCars()) { | ||
| System.out.println(car.getName() + " : " + "-".repeat(car.getMoveCnt())); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public void printWinner(List<String> winners) { | ||
| System.out.println("최종 우승자 : " + String.join(", ", winners)); | ||
| } | ||
| } |
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.
템플릿 TODO 주석이 그대로 남아 있어 제출 코드의 완성도를 떨어뜨립니다. 구현이 완료된 상태라면 해당 TODO 주석을 제거해 주세요.