-
Notifications
You must be signed in to change notification settings - Fork 28
[황명하_BackEnd] 2주차 과제 제출합니다 #18
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,7 +1,10 @@ | ||
| package racingcar; | ||
|
|
||
| import racingcar.controller.RacingController; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| RacingController racingController = new RacingController(); | ||
| racingController.start(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package racingcar.controller; | ||
|
|
||
| import racingcar.model.RacingGame; | ||
| import racingcar.view.InputView; | ||
| import racingcar.view.OutputView; | ||
| import java.util.HashSet; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class RacingController { | ||
|
|
||
| public void start() { | ||
| String namesInput = InputView.readCarNames(); | ||
|
|
||
| List<String> carNames = Arrays.stream(namesInput.split(",", -1)) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| for (String name : carNames) { | ||
| if (name.contains(" ")) { | ||
| throw new IllegalArgumentException("[ERROR] 이름에 공백을 포함할 수 없습니다."); | ||
| } | ||
| if (name.isEmpty()) { | ||
| throw new IllegalArgumentException("[ERROR] 이름은 빈 값일 수 없습니다."); | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+27
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 (new HashSet<>(carNames).size() != carNames.size()) { //위에 import로 HashSet을 가져옴 | ||
| //HashSet 특징: 중복된 값을 허용하지 않음 | ||
|
Comment on lines
+29
to
+30
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. 코드를 이해하려는 노력이 보이는데, 이런 설명성 주석은 보고서에 남기고 제출 코드에서는 정리하면 더 깔끔해질 것 같아요. |
||
| throw new IllegalArgumentException("[ERROR] 중복된 자동차 이름이 있습니다."); | ||
| } | ||
|
Comment on lines
+21
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. 이름 검증(공백, 빈 값, 중복 체크) 로직이 컨트롤러에 있는데, 2주차에서 다룬 "객체의 책임" 관점에서 이 검증이 어느 객체에 있는 게 더 적절할지 한 번 생각해보면 좋을 것 같아요. |
||
|
|
||
| RacingGame racingGame = new RacingGame(carNames); | ||
| int tryCount = InputView.readTryCount(); | ||
|
|
||
| OutputView.printResultMessage(); | ||
|
|
||
| for (int i = 0; i < tryCount; i++) { | ||
| racingGame.playRound(); | ||
| OutputView.printRoundResult(racingGame.getCars()); | ||
| } | ||
|
|
||
| List<String> winners = racingGame.getWinners(); | ||
| OutputView.printWinners(winners); | ||
| } | ||
| } | ||
|
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,33 @@ | ||
| package racingcar.model; | ||
|
|
||
| public class Car { | ||
| private static final int MOVE_THRESHOLD = 4; | ||
| private static final int MIN_NAME_LENGTH = 1; | ||
| private static final int MAX_NAME_LENGTH = 5; | ||
|
|
||
| private final String name; | ||
| private int position=0; | ||
|
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. 등호 주변 공백이 빠져 있어요. move 메서드의 if(num>=4) 부분도 if 뒤, >= 주변 공백을 맞춰보면 좋겠습니다. 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. 이전에 언급한 등호 주변 공백이 아직 반영되지 않았어요. position = 0으로 맞춰보면 좋겠습니다. |
||
|
|
||
| public Car(String name) { | ||
| if (name == null || name.isEmpty() || name.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. 상수를 정의한 건 좋은데, 아래 검증 코드에서 name.length() > 5로 직접 숫자를 쓰고 있어요. 상수를 만들었으면 사용하는 곳에서도 MAX_NAME_LENGTH로 바꿔야 의미가 있습니다. |
||
| throw new IllegalArgumentException("[ERROR] 이름은 5자 이하만 가능합니다."); | ||
| } | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void move(int num){ | ||
| if (num >= MOVE_THRESHOLD) { | ||
| position += 1; | ||
| } | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package racingcar.model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
| import java.util.Collections; | ||
|
|
||
| public class RacingGame { | ||
| private List<Car> cars; | ||
|
|
||
| public RacingGame(List<String> name) { | ||
| this.cars = createCars(name); | ||
| } | ||
|
|
||
| private List<Car> createCars(List<String> names) { | ||
| List<Car> carList = new ArrayList<>(); | ||
| for (String name : names) { | ||
| carList.add(new Car(name)); | ||
| } | ||
| return carList; | ||
| } | ||
|
|
||
| public void playRound() { | ||
| for (Car car : cars) { | ||
| int randomNumber = Randoms.pickNumberInRange(0, 9); | ||
| car.move(randomNumber); | ||
| } | ||
| } | ||
|
|
||
| public List<String> getWinners() { | ||
| int maxPosition = 0; | ||
| 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; | ||
| } | ||
|
|
||
| public List<Car> getCars() { | ||
| return Collections.unmodifiableList(this.cars); | ||
| //원본주소를 직접 반환하면 외부에서 수정 가능 -> 수정: 원본주소를 수정 불가능한 상태로 바꿔서 반환 | ||
| //Collections: java.util 패키지에 포함된 유틸리티 클래스, 위에서 임포트 | ||
| //.unmodifiableList(): 수정 불가능한 상태로 감싸서 반환 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package racingcar.view; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class InputView { | ||
| public static String readCarNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| return Console.readLine(); | ||
| } | ||
|
|
||
| public static int readTryCount() { | ||
| System.out.println("시도할 횟수는 몇 회인가요?"); | ||
| String input = Console.readLine(); | ||
| try { | ||
| int count = Integer.parseInt(input); | ||
| if (count < 1) { | ||
| throw new IllegalArgumentException("[ERROR] 시도 횟수는 1 이상이어야 합니다."); | ||
| } | ||
| return count; | ||
| } catch (NumberFormatException e) { | ||
| 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. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package racingcar.view; | ||
|
|
||
| import racingcar.model.Car; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class OutputView { | ||
| private static final String SYMBOL = "-"; | ||
|
|
||
| public static void printResultMessage() { | ||
| System.out.println("실행 결과"); | ||
| } | ||
|
|
||
| public static void printRoundResult(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| String name = car.getName(); | ||
| String dashes = convertSYMBOL(car.getPosition()); | ||
|
|
||
| System.out.println(name + " : " + dashes); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static String convertSYMBOL(int position) { | ||
|
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. 상수 SYMBOL은 잘 고쳤는데, 메서드 이름이 convertSYMBOL로 되어 있어요. 메서드는 camelCase로 작성하는 게 Java 컨벤션이라 convertSymbol이 적절합니다. |
||
| return SYMBOL.repeat(position); | ||
| } | ||
|
|
||
| public static void printWinners(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.
이전에 지적한 외부 라이브러리 추가를 주석 처리로 해결하셨는데, 사용하지 않는 코드는 주석으로 남기기보다 아예 삭제하는 게 좋아요.