-
Notifications
You must be signed in to change notification settings - Fork 55
[전종민_BackEnd] 2주차 과제 제출합니다. #55
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?
The head ref may contain hidden characters: "feature/\uD074\uB798\uC2A4"
Changes from all commits
3940140
71543dd
66a78ec
576033c
81b7094
7b4704e
ba64be9
2c334ce
5a88217
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,18 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| List<String> carNames = InputView.getCarNames(); | ||
| int tryCount = InputView.getTryCount(); | ||
|
|
||
| List<Car> cars = CarFactory.createCars(carNames); | ||
|
|
||
| System.out.println(); | ||
| Race race = new Race(cars, tryCount); | ||
| race.start(); | ||
|
|
||
| ResultView.printWinners(race.getWinners()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package racingcar; | ||
|
|
||
| public class Car { | ||
| private static final int MOVE_THRESHOLD = 4; | ||
| private static final String POSITION_MARK = "-"; | ||
|
Comment on lines
+4
to
+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. 리터럴 값을 하드코딩하지 않고 상수로 선언한 점 좋아요 |
||
|
|
||
| private final String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void move(int randomNumber) { | ||
| if (randomNumber >= MOVE_THRESHOLD) { | ||
| position++; | ||
| } | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return name + " : " + POSITION_MARK.repeat(position); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import racingcar.Car; | ||
|
|
||
| public class CarFactory { | ||
| public static List<Car> createCars(List<String> names) { | ||
| return names.stream() | ||
| .map(String::trim) | ||
| .map(Car::new) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
Comment on lines
+9
to
+13
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. 스트림을 사용하는 것은 코드의 유지보수성을 높여주는 등의 많은 이점이 있지만, 단순한 반복의 경우 일반 for문 또는 향상된 for문(foreach)이 속도에서 우수합니다. 단순한 반복일 경우 for문을 사용하는 것도 고려해보시면 좋을 것 같아요 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class InputView { | ||
| private static final int MAX_NAME_LENGTH = 5; | ||
|
|
||
| public static List<String> getCarNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| String input = Console.readLine(); | ||
| List<String> names = Arrays.asList(input.split(",")); | ||
|
|
||
| for (String name : names) { | ||
| if (name.trim().length() > MAX_NAME_LENGTH) { | ||
| throw new IllegalArgumentException("자동차 이름은 5자 이하만 가능합니다."); | ||
| } | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| public static int getTryCount() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| return Integer.parseInt(Console.readLine()); | ||
| } | ||
| } | ||
|
Comment on lines
+24
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. 사용자가 숫자가 아닌 값을 입력할 경우를 고려해봐야 할 것 같아요. 사용자의 입력은 예측할 수 없기에 유효한 값을 검증하고 유효한 값이 아닐 경우 어떻게 할 것인지에 대한 고민이 필요해 보입니다. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Race { | ||
| private final List<Car> cars; | ||
| private final int tryCount; | ||
|
|
||
| public Race(List<Car> cars, int tryCount) { | ||
| this.cars = cars; | ||
| this.tryCount = tryCount; | ||
| } | ||
|
Comment on lines
+8
to
+15
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. 서비스(게임)와 모델(자동차)을 분리한 것 좋아요. |
||
|
|
||
| public void start() { | ||
| for (int i = 0; i < tryCount; i++) { | ||
| moveCars(); | ||
| ResultView.printRaceResult(cars); | ||
| } | ||
| } | ||
|
|
||
| private void moveCars() { | ||
| for (Car car : cars) { | ||
| int number = Randoms.pickNumberInRange(0, 9); | ||
| car.move(number); | ||
| } | ||
| } | ||
|
|
||
| public List<String> getWinners() { | ||
| int maxPosition = cars.stream() | ||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .orElse(0); | ||
|
|
||
| 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; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class ResultView { | ||
| public static void printRaceResult(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| System.out.println(car); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| 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.
사용자와 상호작용하는 기능과 게임 실행과 관련된 기능이 클래스 단위로 적절하게 잘 분리되어 있어서 좋습니다.