-
Notifications
You must be signed in to change notification settings - Fork 6
java-racingcar mission 제출합니다 #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: oereo
Are you sure you want to change the base?
Changes from 44 commits
10dca15
7759d90
29c34ea
a14473d
a51c4e4
d56921b
3d28c49
4eebc6d
69f37d0
7d614b4
0c6bb59
1ff4c7a
b2daa5d
40b18e4
e16fdb8
218bed6
f0d1f09
e72ba2f
cf8d6d4
4e74812
c6f3b68
396584f
c08b45d
f982c1d
bd63d3d
b9dc073
ec26f16
eb40f45
89965af
a04b488
9db711b
267a80a
fc8d01c
9ceca45
01c78c6
eb7a7c5
1b7f367
1e1ce79
48dca32
d9012d1
107e35f
df55a8c
914a0b5
946fadf
498d3be
9857960
9430694
cc8172a
16aa313
ab0b294
8f40658
6f57314
c40921a
735092e
6144be7
d573258
9169126
761b3dc
35ed5d8
fec9808
ea5a4fd
de3ba25
c5eb648
8afc5d2
8501c47
ea61730
257a825
8c28348
50063f2
bcf9d28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package racingcar; | ||
|
|
||
| import racingcar.domain.car.Car; | ||
| import racingcar.domain.exception.NotBlankException; | ||
| import racingcar.dto.CarNumberDto; | ||
| import racingcar.exception.NotZeroRoundException; | ||
| import racingcar.repository.CarManager; | ||
| import racingcar.ui.Printer; | ||
| import racingcar.ui.Receiver; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.InputMismatchException; | ||
| import java.util.List; | ||
|
|
||
| public class RacingGameApplication { | ||
| private static final int INPUT_ZERO = 0; | ||
| private static final String DELIMITER = ","; | ||
|
|
||
| private final Printer printer = new Printer(); | ||
| private final Receiver receiver = new Receiver(); | ||
| private final CarManager carManager = new CarManager(); | ||
|
|
||
| public void run() { | ||
| printer.requestCarName(); | ||
| String line = receiver.receiveCarNames(); | ||
| try { | ||
| List<Car> carList = createAllCars(splitInputLine(line)); | ||
| carManager.addAllCars(carList); | ||
| }catch (NotBlankException e) { | ||
| printer.printExceptionMessage(e); | ||
| run(); | ||
| } | ||
| printer.requestNumberOfRounds(); | ||
| int rounds = receiveRounds(); | ||
| proceedRound(rounds); | ||
|
|
||
| printer.printWinner(carManager.createWinnerMessage()); | ||
|
|
||
| } | ||
|
|
||
| private void proceedRound(int rounds) { | ||
| printer.printResultHeader(); | ||
| for (int i = 0; i < rounds; i++) { | ||
| List<CarNumberDto> carNumberDtos = carManager.generateCarNumberDtos(); | ||
| carManager.moveAllCars(carNumberDtos); | ||
| carManager.printCarState(printer); | ||
| } | ||
| } | ||
|
|
||
| private List<Car> createAllCars(List<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. RacingGameApplication이 너무 많은 역할을 하고 있는것 같아요
다양한 기능을 하고있네요! SRP(Single Responsibility Principle)를 지키도록 클래스를 분리해보세요!
Member
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. RacingGameApplication class의 주된 역할은 게임의 순서와 연관된 기능들을 한다고 생각을 했어요!! 사실 지금 다시 보니까 분리를 시켜야될 필요성이 있을 것 같아요 ㅠ method도 그렇지만 책임이 다양하게 있는 느낌이네요 ㅠㅠ |
||
| List<Car> carList = new ArrayList<>(); | ||
|
|
||
| for (String name : carNames) { | ||
| carList.add(new Car(name)); | ||
| } | ||
| return carList; | ||
| } | ||
|
|
||
| private int receiveRounds() { | ||
| int rounds = 0; | ||
| boolean isInvalidInput = true; | ||
| while (isInvalidInput) { | ||
| try { | ||
| rounds = receiver.receiveNumberOfRounds(); | ||
| throwExceptionIfInputIsZero(rounds); | ||
| isInvalidInput = false; | ||
| } catch (InputMismatchException e) { | ||
| printer.printInputMismatchExceptionMessage(receiver); | ||
| } catch (IllegalArgumentException e) { | ||
| printer.printExceptionMessage(e); | ||
| } | ||
| } | ||
| return rounds; | ||
| } | ||
|
|
||
| private void throwExceptionIfInputIsZero(int rounds) { | ||
| if (rounds == INPUT_ZERO) { | ||
| throw new NotZeroRoundException(); | ||
| } | ||
|
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 List<String> splitInputLine(String line) { | ||
| return Arrays.asList(line.split(DELIMITER)); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| RacingGameApplication app = new RacingGameApplication(); | ||
| app.run(); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package racingcar.domain.car; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Car { | ||
| private final String name; | ||
| private int position = 0; | ||
| private static final int MOVE_INTERVAL = 1; | ||
|
|
||
| private CarName carName; | ||
|
|
||
| public Car(String name) { | ||
| this.carName = new CarName(name); | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| public void move() { | ||
| position += MOVE_INTERVAL; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return name + " : " + "-".repeat(Math.max(0, position)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Car car = (Car) o; | ||
| return name.equals(car.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package racingcar.domain.car; | ||
|
|
||
| import racingcar.domain.exception.NotBlankException; | ||
| import racingcar.domain.exception.NotValidNameLengthException; | ||
|
|
||
| public class CarName { | ||
| private static final int CAR_NAME_MAX_LENGTH = 4; | ||
| private String name; | ||
|
|
||
| public CarName(String name) { | ||
| checkBlankName(name); | ||
| checkNameLength(name); | ||
| this.name = name; | ||
| } | ||
|
|
||
| private void checkBlankName(String name) { | ||
| if (name.isBlank()) { | ||
| throw new NotBlankException(); | ||
| } | ||
| } | ||
|
|
||
| private void checkNameLength(String name) { | ||
| if (name.length() > CAR_NAME_MAX_LENGTH) { | ||
| throw new NotValidNameLengthException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package racingcar.domain.exception; | ||
|
|
||
| public class NotBlankException extends RuntimeException { | ||
| private static final String MESSAGE = "빈 문자열 이름의 차를 생성할 수는 없습니다."; | ||
|
|
||
| public NotBlankException() { | ||
| super(MESSAGE); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package racingcar.domain.exception; | ||
|
|
||
| public class NotValidNameLengthException extends RuntimeException { | ||
| private static final String Message = "5자 이상의 이름을 가진 차는 생설할 수 없습니다."; | ||
|
|
||
| public NotValidNameLengthException() { | ||
| super(Message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package racingcar.dto; | ||
|
|
||
| import racingcar.domain.Car; | ||
|
|
||
| public class CarNumberDto { | ||
| private final Car car; | ||
| private final int number; | ||
|
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와 number를 dto로 감싼 이유가 있을까요? |
||
|
|
||
| public CarNumberDto(Car car, int number) { | ||
| this.car = car; | ||
| this.number = number; | ||
| } | ||
|
|
||
| public Car getCar() { | ||
| return car; | ||
| } | ||
|
|
||
| public int getNumber() { | ||
| return number; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package racingcar.exception; | ||
|
|
||
| public class NotZeroRoundException extends IllegalArgumentException { | ||
| private static final String NOT_ZERO_ROUND_EXCEPTION_MESSAGE = "라운드 회수로 0은 입력하실 수 없습니다."; | ||
|
|
||
| public NotZeroRoundException() { | ||
| super(NOT_ZERO_ROUND_EXCEPTION_MESSAGE); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package racingcar.repository; | ||
|
|
||
| import racingcar.domain.Car; | ||
| import racingcar.dto.CarNumberDto; | ||
| import racingcar.repository.exception.NotDuplicateNameException; | ||
| import racingcar.ui.Printer; | ||
| import racingcar.util.Discriminator; | ||
| import racingcar.util.RandomGenerator; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class CarManager { | ||
| private List<Car> cars; | ||
|
|
||
| public CarManager() { | ||
| cars = new ArrayList<>(); | ||
| } | ||
|
|
||
| private void checkDuplicateName(Car car) { | ||
| if (cars.contains(car)) { | ||
| throw new NotDuplicateNameException("중복되는 이름의 차를 입력하실 수 없습니다."); | ||
| } | ||
| } | ||
|
|
||
| public int size() { | ||
| return cars.size(); | ||
| } | ||
|
|
||
| public void addAllCars(List<Car> cars) { | ||
| for (Car car : cars) { | ||
| checkDuplicateName(car); | ||
| this.cars.add(car); | ||
| } | ||
| } | ||
|
|
||
| public List<CarNumberDto> generateCarNumberDtos() { | ||
| List<CarNumberDto> dtos = new ArrayList<>(); | ||
|
|
||
| for (Car car: cars) { | ||
| int randomNumber = RandomGenerator.generateNumber(); | ||
| dtos.add(new CarNumberDto(car, randomNumber)); | ||
| } | ||
|
|
||
| return dtos; | ||
| } | ||
|
|
||
| public void moveAllCars(List<CarNumberDto> carNumberList) { | ||
| for (CarNumberDto dto : carNumberList) { | ||
| if (Discriminator.isMove(dto.getNumber())) { | ||
| dto.getCar().move(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void printCarState(Printer printer) { | ||
| for (Car car : cars) { | ||
| printer.printCarState(car); | ||
| } | ||
| printer.printNewLine(); | ||
| } | ||
|
|
||
| public String createWinnerMessage() { | ||
| StringBuilder winnerNames = new StringBuilder(); | ||
| List<Car> winners = winnerList(); | ||
| winnerNames.append(winners.get(0).getName()); | ||
|
|
||
| for (int i = 1; i < winners.size(); i++) { | ||
| Car winner = winners.get(i); | ||
| winnerNames.append(", "); | ||
| winnerNames.append(winner.getName()); | ||
| } | ||
|
|
||
| return winnerNames.toString(); | ||
| } | ||
|
Comment on lines
+57
to
+76
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 List<Car> winnerList() { | ||
| int winnerPosition = cars.stream() | ||
| .mapToInt(Car::getPosition) | ||
| .max() | ||
| .orElseThrow(IllegalArgumentException::new); | ||
|
|
||
| return cars.stream() | ||
| .filter(car -> car.getPosition() == winnerPosition) | ||
|
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. stream api 활용 👍🏻
Member
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. 이 부분을 바꿔보도록 하겠습니다!! 다만 구조를 전반적으로 바꿔야 될지 조금더 고민을 해보도록 하겠습니다! |
||
| .collect(Collectors.toList()); | ||
| } | ||
|
oereo marked this conversation as resolved.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package racingcar.repository.exception; | ||
|
|
||
| public class NotDuplicateNameException extends RuntimeException { | ||
| public NotDuplicateNameException(String message) { | ||
| super(message); | ||
| } | ||
| } | ||
|
oereo marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package racingcar.ui; | ||
|
|
||
| import racingcar.domain.Car; | ||
|
|
||
| public class Printer { | ||
| private static final String CAR_NAME_REQUEST_MESSAGE = "경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"; | ||
| private static final String ROUND_REQUEST_MESSAGE = "시도할 회수는 몇회인가요?"; | ||
| private static final String RESULT_HEADER = "실행 결과"; | ||
| private static final String WINNER_MESSAGE = "가 최종 우승했습니다."; | ||
| private static final String INPUT_MISMATCH_EXCEPTION_MESSAGE = "라운드 횟수로 숫자만 입력하실 수 있습니다."; | ||
|
|
||
| public void requestCarName() { | ||
| System.out.println(CAR_NAME_REQUEST_MESSAGE); | ||
| } | ||
|
|
||
| public void requestNumberOfRounds() { | ||
| System.out.println(ROUND_REQUEST_MESSAGE); | ||
| } | ||
|
|
||
| public void printResultHeader() { | ||
| System.out.println(RESULT_HEADER); | ||
| } | ||
|
|
||
| public void printWinner(String winnerNames) { | ||
| System.out.println(winnerNames + WINNER_MESSAGE); | ||
| } | ||
|
|
||
| public void printCarState(Car car) { | ||
| System.out.println(car); | ||
| } | ||
|
|
||
| public void printNewLine() { | ||
| System.out.println(); | ||
| } | ||
|
oereo marked this conversation as resolved.
|
||
|
|
||
| public void printExceptionMessage(Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
|
|
||
| public void printInputMismatchExceptionMessage(Receiver receiver) { | ||
| receiver.clearBuffer(); | ||
| System.out.println(INPUT_MISMATCH_EXCEPTION_MESSAGE); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.