-
Notifications
You must be signed in to change notification settings - Fork 0
leesj #1
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?
leesj #1
Changes from all commits
35b7f08
449cf20
b7c4bcd
090071d
3ec4ef3
935f624
5569570
472866e
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,11 @@ | ||
| # 1. 차 이름 입력문구 출력 | ||
|
|
||
| # 2. 배열 동적 할당으로 차 입력 받음 | ||
|
|
||
| # 3. 시도할 횟수 입력받기 | ||
|
|
||
| # 4. 실행결과 순차적 출력 | ||
|
|
||
| # 5. 최종 우승자 출력 | ||
|
|
||
| # 6. 예외처리 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| package racingcar; | ||
|
|
||
| import racingcar.controller.Controller; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| Controller controller = new Controller(); | ||
| controller.run(); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package racingcar.controller; | ||
|
|
||
| import racingcar.model.RacingGame; | ||
| import racingcar.view.InputView; | ||
| import racingcar.view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Controller { | ||
|
|
||
| InputView inputView = new InputView(); | ||
| OutputView outputView = new OutputView(); | ||
|
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. OutputView 클래스 안보이는데여?
Owner
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. 안올라갔네요 뭐지 이거 |
||
| List<String> carNames = inputView.readCarName(); | ||
|
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. 컨트롤러가 carName이라는 책임을 가진다라.. 뭔가 이상하지 않나여?
Owner
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. 책임이 정확히 뭔지 모르겠네... |
||
| int tryNumber = inputView.tryNumber(); | ||
|
|
||
| public void run(){ | ||
| RacingGame game = new RacingGame(carNames); | ||
|
|
||
| outputView.showResultMessage(); | ||
|
|
||
| for(int i = 0; i<tryNumber; i++){ | ||
| game.moveAll(); | ||
| outputView.printAll(game.getCars()); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| outputView.showWinner(game.getCars()); | ||
|
Comment on lines
+23
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. 컴파일 에러뜰텐데 이거
Owner
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,38 @@ | ||
| package racingcar.model; | ||
|
|
||
| public class Car { | ||
| private static final int MAX_NAME_LENGTH = 5; | ||
| private static final int CRITERIA_NUM = 4; | ||
|
|
||
| private final String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name){ | ||
| validateName(name); | ||
| this.name = name; | ||
| } | ||
|
|
||
| private void validateName(String name) { | ||
| if (name == null || name.isBlank()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| if (name.length() > MAX_NAME_LENGTH) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| public void move(int number){ | ||
| if(number >=CRITERIA_NUM){ | ||
| position ++; | ||
| } | ||
| } | ||
|
|
||
| public String getName(){ | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition(){ | ||
| return position; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package racingcar.model; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class RacingGame { | ||
| private final List<Car> cars; | ||
| private final static int MIN_VALUE = 0; | ||
| private final static int MAX_VALUE = 9; | ||
| public RacingGame(List<String> names) { | ||
| this.cars = names.stream() | ||
| .map(Car::new) | ||
| .toList(); | ||
| } | ||
|
|
||
| public void moveAll(){ | ||
| for (Car car : cars) { | ||
| int number = Randoms.pickNumberInRange(MIN_VALUE, MAX_VALUE); | ||
| car.move(number); | ||
| } | ||
| } | ||
|
|
||
| public List<Car> getCars(){ | ||
| return cars; | ||
| } | ||
|
|
||
| } | ||
|
Comment on lines
+7
to
+28
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. 답은 없지만 RacingGame을 모델로 설정하는 게 어떤 사이드이펙트를 불러올지는 생각을 해보면 좋을 것 같아요. 저는 개인적으로 모델을 물론 레이싱게임이라는 객체도 게임을 운영하고 레이싱카들을 보유하고 점수를 측정해내야한다는 그런 책임들이 있겠죠. 그래서 레이싱게임을 모델로 가져가는 것도 어떻게 보면 이렇게 요구사항이 적은 문제에서는 가장 빠르게 문제를 풀어야되는 환경에서는 가장 최선의 방법일수도 있죠. 그런데 저는 여기서 중요하다고 생각하는 게 각각의 방식에 대한 트레이드오프를 정확하게 이해하고 적절한 환경에 쓸 수 있는가?에요. 예를들어 레이싱게임이라는 미션에 더 나아가 자동차의 엔진, 레이서의 특징, 점수판의 고도화 등등 레이싱게임의 각 구성요소들에 대한 추가적인 기능요구가 생긴다고 가정해보죠. 그러면 지금 현재 구조에서는 RacingGame이라는 객체가 점점 더 많은 필드들을 가지게 될 것이고 이는 모든 비즈니스 로직의 책임을 떠앉게 되는 GOD OBJECT가 될 것입니다. 그러면 수정사항이 있을 때마다 RacingGame의 거대한 부분을 계속해서 건드려야하고 유지보수성도 어려워지겠죠. 물론 나중에 추가 요구사항이 생길 때, 객체를 분리해야한다는 생각이 들 때 분리하는 것도 매우 좋죠. 이를 YAGNI 법칙이라고 하거든요. 어찌됐건 제가 강조하고싶은건
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,26 @@ | ||
| package racingcar.view; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class InputView { | ||
|
|
||
| public List<String> readCarName(){ | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| return new ArrayList<>(List.of(Console.readLine().split(","))); | ||
| } | ||
|
|
||
| public int tryNumber() { | ||
| System.out.println("시도할 횟수는 몇 회인가요?"); | ||
|
|
||
| int tryNumber = Integer.parseInt(Console.readLine()); | ||
|
|
||
| if (tryNumber <= 0) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| return tryNumber; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package racingcar.view; | ||
|
|
||
| import racingcar.model.Car; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class OutputView { | ||
| public void showResultMessage() { | ||
| System.out.println("실행 결과"); | ||
| } | ||
| public void printAll(List<Car> cars){ | ||
| for(Car car : cars){ | ||
| String position = "-".repeat(car.getPosition()); | ||
| System.out.println(car.getName() + " : " + position); | ||
| } | ||
|
|
||
| } | ||
| public void showWinner(List<Car> cars){ | ||
| int maxPosition = cars.stream() | ||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .orElseThrow(); | ||
|
|
||
| String winners = cars.stream() | ||
| .filter(car -> car.getPosition() == maxPosition) | ||
| .map(Car::getName) | ||
| .collect(Collectors.joining(", ")); | ||
|
|
||
| System.out.println("최종 우승자 : " + 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.
흠 좀 더 자세히 가져갈만하지 않을까요
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.
아 이거 자세히 가는게 먼가 쉽지않음