-
Notifications
You must be signed in to change notification settings - Fork 6
자동차 경주 게임 제출합니다! #6
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: jjanggyu
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import domain.CarFactory; | ||
| import domain.RacingCarGame; | ||
| import ui.Receiver; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| Receiver receiver = new Receiver(); | ||
| CarFactory carFactory = new CarFactory(receiver.getCarNames()); | ||
| RacingCarGame racingCarGame = new RacingCarGame(carFactory.getCars(), receiver.getRound()); | ||
|
|
||
| racingCarGame.proceedRounds(); | ||
| racingCarGame.printWinners(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,40 @@ | ||
| package domain; | ||
|
|
||
| public class Car { | ||
| import domain.exception.NotBlankException; | ||
|
|
||
| public class Car implements Comparable<Car> { | ||
| private final String name; | ||
| private int position = 0; | ||
|
|
||
| public Car(String name) { | ||
| this.name = name; | ||
| validateName(); | ||
| } | ||
|
|
||
| public void moveForward() { | ||
| position += 1; | ||
| } | ||
|
|
||
| private void validateName() { | ||
| if (name.length() > 5) { | ||
| throw new IllegalArgumentException(name + ": 자동차 이름은 5글자 이하여야 합니다."); | ||
| } | ||
|
|
||
| if (name.isBlank()) { | ||
| throw new NotBlankException("공백으로만 이루어진 이름을 생성할 수 없습니다."); | ||
|
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. 이 부분에 대해서만 커스텀 익셉션을 정의한 이유가 있을까요? |
||
| } | ||
|
Comment on lines
+18
to
+25
Member
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. 입력에 대한 예외를 더욱 세분화하면 사용자 입장에서 더 편리할것 같아요✔
저는 이렇게 이름 입력에 대한 예외를 7가지로 정했습니다!
Comment on lines
+18
to
+25
Member
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만 해주고 받아주는 곳이 없네요 valiateName 메서드를 Car 객체를 생성할 때 호출하는 것 보다 Receive 클래스에서 실행시켜주면 예외 처리하고 반복적으로 입력을 받기 편할 것 같아요! |
||
| } | ||
|
|
||
| // 추가 기능 구현 | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Car o) { | ||
| return o.position - position; | ||
| } | ||
|
Comment on lines
+37
to
+39
Member
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. 사용되지 않는 메서드같아요! 이런 부분을 줄이려면 README에서 필요한 클래스와 메서드를 정리해가며 진행하는 것이 좋은것 같아요😃 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class CarFactory { | ||
| private final String DELIMITER = ","; | ||
|
|
||
| private final List<Car> cars; | ||
| private final String[] carNames; | ||
|
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. 배열 보다는 컬렉션을 사용하는 것은 어떨까요? |
||
|
|
||
| public CarFactory(String input) { | ||
| this.carNames = splitCarNames(input); | ||
| trimCarNames(); | ||
| cars = createCars(); | ||
| } | ||
|
|
||
| private String[] splitCarNames(String answer) { | ||
| return answer.split(DELIMITER); | ||
| } | ||
|
|
||
| private void trimCarNames() { | ||
| for (int i = 0; i < carNames.length; i++) { | ||
| carNames[i] = carNames[i].trim(); | ||
| } | ||
| } | ||
|
|
||
| private List<Car> createCars() { | ||
| List<Car> cars = new ArrayList<>(); | ||
| for (String carName : carNames) { | ||
| cars.add(new Car(carName)); | ||
| } | ||
|
|
||
| return cars; | ||
| } | ||
|
|
||
| public List<Car> getCars() { | ||
| return cars; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package domain; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import utils.RandomUtils; | ||
|
|
||
| public class RacingCarGame { | ||
|
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. RacingCarGame에선 어떤일을 할까요?
|
||
| private static final String RESULT_START_MESSAGE = "\n실행 결과"; | ||
| private static final String RESULT_END_MESSAGE = "가 최종 우승했습니다."; | ||
| private static final String COLON = " : "; | ||
| private static final String DASH = "-"; | ||
|
|
||
| private final List<Car> cars; | ||
| private final int round; | ||
|
|
||
| public RacingCarGame(List<Car> cars, int round) { | ||
| this.cars = cars; | ||
| this.round = round; | ||
| } | ||
|
|
||
| private void movePosition(int idx) { | ||
| int randomValue = RandomUtils.generateNumber(); | ||
|
|
||
| if (randomValue >= 4) { | ||
| cars.get(idx).moveForward(); | ||
| } | ||
| } | ||
|
|
||
| private void proceedOneRound() { | ||
| IntStream.range(0, cars.size()).forEach(this::movePosition); | ||
| } | ||
|
|
||
| public void proceedRounds() { | ||
| System.out.println(RESULT_START_MESSAGE); | ||
|
|
||
| IntStream.range(0, round).forEach(r -> { | ||
| proceedOneRound(); | ||
| printCurrentPositions(); | ||
| }); | ||
| } | ||
|
|
||
| private void printCurrentPositions() { | ||
| for (Car car : cars) { | ||
| System.out.print(car.getName() + COLON); | ||
|
|
||
| IntStream.range(0, car.getPosition()).mapToObj(j -> DASH).forEach(System.out::print); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| System.out.println(); | ||
| } | ||
|
|
||
| public void printWinners() { | ||
| Collections.sort(cars); | ||
|
|
||
| int max = cars.get(0).getPosition(); | ||
| System.out.print(cars.get(0).getName()); | ||
|
|
||
| for (int i = 1; i < cars.size(); i++) { | ||
| if (cars.get(i).getPosition() < max) { | ||
|
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. 객체의 값을 꺼내는 것이 아닌 메시지를 던지는 방식으로 변경해보면 어떨까요? |
||
| break; | ||
| } | ||
| System.out.print(", " + cars.get(i).getName()); | ||
| } | ||
|
|
||
| System.out.println(RESULT_END_MESSAGE); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package domain.exception; | ||
|
|
||
| public class NotBlankException extends IllegalArgumentException { | ||
| public NotBlankException(String s) { | ||
| super(s); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package ui; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Receiver { | ||
| private static final String REQUEST_CAR_NAME = "경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"; | ||
| private static final String REQUEST_ROUND = "시도할 횟수는 몇회인가요?"; | ||
|
|
||
| private static final Scanner scanner = new Scanner(System.in); | ||
|
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. 상수 컨벤션을 지켜주세요! |
||
|
|
||
| private String carNames; | ||
| private int round; | ||
|
|
||
|
|
||
| public Receiver() { | ||
| receiveCarNames(); | ||
| receiveRound(); | ||
| } | ||
|
Comment on lines
+15
to
+18
Member
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 receiveCarNames() { | ||
| System.out.println(REQUEST_CAR_NAME); | ||
| carNames = scanner.nextLine(); | ||
| // return carNames; | ||
| } | ||
|
|
||
| public void receiveRound() { | ||
| System.out.println(REQUEST_ROUND); | ||
| round = scanner.nextInt(); | ||
| // return round; | ||
| } | ||
|
|
||
| public String getCarNames() { | ||
| return carNames; | ||
| } | ||
|
|
||
| public int getRound() { | ||
| return round; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package utils; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class RandomUtils { | ||
| private static final int UPPER_BOUND = 10; | ||
| private static final Random RANDOM = new Random(); | ||
|
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. static 관리 👍🏻 |
||
|
|
||
| private RandomUtils() { | ||
| } | ||
|
Comment on lines
+9
to
+10
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. 유틸 클래스의 private 생성자 활용 👍🏻 |
||
|
|
||
| public static int generateNumber(){ | ||
| return RANDOM.nextInt(UPPER_BOUND); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class CarFactoryTest { | ||
|
|
||
| private CarFactory carFactory; | ||
| private String input = " lee, chan, gyu "; | ||
|
|
||
| @Test | ||
| public void 차를_생성한다() throws Exception { | ||
| carFactory = new CarFactory(input); | ||
|
|
||
| List<Car> cars = carFactory.getCars(); | ||
|
|
||
| Assertions.assertThat(cars.get(0).getName()).isEqualTo("lee"); | ||
| Assertions.assertThat(cars.get(1).getName()).isEqualTo("chan"); | ||
| Assertions.assertThat(cars.get(2).getName()).isEqualTo("gyu"); | ||
| } | ||
| } |
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.
큰 차이는 없지만, 검증 후 값을 넣어주는 것이 더 적절한 흐름 아닐까요?