-
Notifications
You must be signed in to change notification settings - Fork 5
자동차 경주 게임! #15
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: master
Are you sure you want to change the base?
자동차 경주 게임! #15
Changes from 1 commit
ececbca
34ee1a3
e47e617
8e3228a
deefa90
36c7945
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,10 @@ | |
| import java.util.List; | ||
|
|
||
| public class CarFactory { | ||
|
Contributor
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. 굿 좋습니다b 기존코드와의 변경사항이 적어서 좋네요 |
||
| public static List<Car> createCar(int carCount) { | ||
| public static List<Car> createCar(List<String> carNames) { | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (int carIdx = 0; carIdx < carCount; carIdx++) { | ||
| cars.add(new Car(0)); | ||
| for (String carName : carNames) { | ||
| cars.add(new Car(carName, 0)); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,39 @@ | ||
| package racing.domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class RacingResult { | ||
| private final List<Integer> racingResult; | ||
| private final List<Car> racingResult; | ||
|
Contributor
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. 여기서 RacingResult는 Cars 에서 움직임이 끝난 Car 객체들의 리스트를 받아서 수행을 하는데요..!! 이때 RacingResult도 List를 담고있는데요, 이때 Car 대신에 다른 객체를 두면 좋을 것 같아요. 만약, 누군가가 RacingResult 클래스를 수정한다고 할 때. 현재 상태라면 RacingResult에서 racingResult.get(0).move() 와 같이 접근해서, List 에 있는 Car의 위치를 조작하는 메서드를 만들 수 있기 때문입니다. 따라서 위에서 RacingResult를 생성할 때 VO 객체를 만들어서, 이 객체 안에는 move와 같이, 객체의 상태를 변경하는 메서드를 제공하지 않아 불변 객체로 만드는 방식을 생각할 수 있을 것 같습니다. 만약 그 객체 이름이 CarData가 된다면 List<CarData> racingResulut;
new CarData(car.name, car.distance);
class final CarData{
}위 코드가 살짝의 키워드가 될 수 있겠네요.
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. 와 그렇네요. 좀 더 깊게 생각해야 될 거 같습니다! 감사합니다. 또 하나 배웁니다! |
||
|
|
||
| public RacingResult(List<Integer> racingResult) { | ||
| public RacingResult(List<Car> racingResult) { | ||
| this.racingResult = racingResult; | ||
| } | ||
|
|
||
| public int size() { | ||
| return racingResult.size(); | ||
| } | ||
|
|
||
| public int get(int idx) { | ||
| public Car get(int idx) { | ||
| return racingResult.get(idx); | ||
| } | ||
|
|
||
| public List<String> getWinners() { | ||
| int maxDistance = getMaxDistance(); | ||
| return racingResult | ||
| .stream() | ||
| .filter(car -> (car.getDistance() == maxDistance)) | ||
| .map(car -> car.getName()) | ||
|
Contributor
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. 람다를 사용할 때 car::getName 와 같은 참조 사용방식도 있습니다! 바꾸라는게 아니라 그런 방식도 있다는 그런..ㅎㅎ
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. 오 이렇게 또 하나 알아갑니다 ㅎㅎ 감사합니다! |
||
| .distinct() | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private int getMaxDistance() { | ||
| Car maxByDistance = racingResult | ||
| .stream() | ||
| .max(Comparator.comparing(Car::getDistance)) | ||
| .orElseThrow(NoSuchElementException::new); | ||
|
|
||
| return maxByDistance.getDistance(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package racing.util; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class SplitUtil { | ||
| public static List<String> split(String carNames) { | ||
| checkEmpty(carNames); | ||
| List<String> splitValue = Arrays.asList(carNames.split(",")); | ||
|
|
||
| checkCount(splitValue.size()); | ||
| checkDuplicate(splitValue); | ||
|
|
||
| return splitValue; | ||
| } | ||
|
|
||
| private static void checkEmpty(String carNames) { | ||
| if (carNames.isEmpty()) { | ||
| throw new NullPointerException("경주할 자동차 이름을 입력하지 않았습니다."); | ||
| } | ||
| } | ||
|
|
||
| private static void checkCount(int carCount) { | ||
| if (carCount < 1) { | ||
| throw new IllegalArgumentException("게임을 진행하려면 한대 이상의 자동차가 필요합니다."); | ||
| } | ||
| } | ||
|
|
||
| private static void checkDuplicate(List<String> splitValue) { | ||
| if (isDuplicate(splitValue)) { | ||
| throw new IllegalArgumentException("자동차의 이름은 모두 달라야 합니다."); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isDuplicate(List<String> splitValue) { | ||
| return splitValue | ||
| .stream() | ||
| .distinct() | ||
| .count() != splitValue.size(); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,35 @@ | ||
| package racing.view; | ||
|
|
||
| import racing.util.SplitUtil; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private final String FIRST_QUESTION = "자동차 대수는 몇 대 인가요?"; | ||
| private final String SECOND_QUESTION = "시도할 횟수는 몇 회 인가요?"; | ||
| private static final String FIRST_QUESTION = "경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."; | ||
| private static final String SECOND_QUESTION = "시도할 횟수는 몇 회 인가요?"; | ||
|
|
||
| private final Scanner sc; | ||
|
|
||
| public InputView(Scanner sc) { | ||
|
malibinYun marked this conversation as resolved.
|
||
| this.sc = sc; | ||
| } | ||
|
|
||
| public int inputCarCount() { | ||
| public List<String> inputCarNames() { | ||
| System.out.println(FIRST_QUESTION); | ||
| return new InputCount(sc.nextInt()).getCount(); | ||
| return SplitUtil.split(sc.nextLine()); | ||
| } | ||
|
|
||
| public int inputTryCount() { | ||
| System.out.println(SECOND_QUESTION); | ||
| return new InputCount(sc.nextInt()).getCount(); | ||
| int tryCount = sc.nextInt(); | ||
| checkPositiveNumber(tryCount); | ||
| return tryCount; | ||
| } | ||
|
|
||
| private void checkPositiveNumber(int tryCount) { | ||
| if (tryCount <= 0) { | ||
| throw new IllegalArgumentException("최소 한번 이상의 게임을 진행해야 합니다."); | ||
| } | ||
| } | ||
| } | ||
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.
엇근데 random으로 받은 distance 값을 바탕으로 outputview를 작성하는데,
이렇게 하면 주어진 요구사항에 맞지 않을 것 같습니다.
레이싱 게임 요구사항을 확인해주세요.