-
Notifications
You must be signed in to change notification settings - Fork 9
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
최성원 자동차 경주 게임 #4
base: master
Are you sure you want to change the base?
Changes from 11 commits
48114f3
6f64ef8
7b5be11
bba56e1
97352a9
48f3e95
5aee294
b4ca696
915c62c
91670b6
83eb481
145f84f
6e43090
61668ec
3cd358a
35bc1ff
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,10 @@ | ||
# 기능 목록 | ||
1. 경주할 자동차의 이름을 입력 받는다 | ||
2. 입력 받은 자동차 이름을 검증 한 후 실패시 에러를 띄운다 | ||
3. 시도할 횟수를 입력 받는다 | ||
4. 시도할 횟수를 검증 한 후 실패시 에러를 띄운다 | ||
5. 시도할 횟수에 맞게 무작위 값을 계산한다 | ||
6. 무작위 값에 따라 자동차들을 전진 시킨다 | ||
7. 전진시킬 때마다 각 차수별 실행 결과를 출력한다 | ||
8. 전진이 끝나면 우승자를 뽑는다 | ||
9. 우승자들을 출력한다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package racingcar; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CarGame { | ||
private final InputView inputView; | ||
private final OutputView outputView; | ||
|
||
public CarGame() { | ||
this.inputView = new InputView(); | ||
this.outputView = new OutputView(); | ||
} | ||
Comment on lines
+11
to
+17
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. DI 적인 요소들이 많이 보여서 엄청 좋은것 같아요! 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. spring에서 하던 느낌과 최대한 비슷하게 해보려 했습니다! |
||
|
||
public void start() { | ||
List<Car> cars = inputView.getCars(); | ||
Integer trial = inputView.getTrial(); | ||
startGame(cars, trial); | ||
} | ||
|
||
private void startGame(List<Car> cars, Integer trial) { | ||
outputView.printInit(); | ||
for (int i = 0; i < trial; i++) { | ||
for (Car car : cars) { | ||
goOrNot(car); | ||
} | ||
outputView.printResult(cars); | ||
} | ||
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. 저는 java를 시작한지 얼마 안되어서 c++ 습관대로 정수형을 int로만 표현하는데, Integer 타입을 주로 사용하신 것 같습니다. 혹시 int와 Integer를 구분해서 사용하는 기준이 있을까요? 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. 저는 개인적으로 참조 타입(Integer, Double 등)이 원시 타입(int, long 등)과 같은 값을 갖고 있지만 많은 메소드들을 지니고 있어 여러모로 편리하다고 느껴 주로 사용하고 있었습니다. 그런데 찾아보니 성능 면에서는 원시 타입이 더 좋다고 하네요. 성능을 위해선 원시 타입을 우선시 하되 좀 더 범용적인 사용을 원한다면 참조 타입을 사용하는 게 좋아 보이네요. |
||
List<String> result = pickWinner(cars); | ||
outputView.printFinalWinner(result); | ||
} | ||
|
||
private void goOrNot(Car car) { | ||
int randomNum = Randoms.pickNumberInRange(0, 9); | ||
if (randomNum >= 4) { | ||
car.go(); | ||
} | ||
} | ||
|
||
private List<String> pickWinner(List<Car> cars) { | ||
List<String> winner = new ArrayList<>(); | ||
int MAX_DISTANCE = -1; | ||
for (Car car : cars) { | ||
if (car.getPosition() == MAX_DISTANCE) { | ||
winner.add(car.getName()); | ||
} | ||
if (car.getPosition() > MAX_DISTANCE) { | ||
MAX_DISTANCE = car.getPosition(); | ||
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. 대문자 변수명은 주로 constant 변수를 표현할때 사용하는걸로 알고있습니다. 혹시 요 파트에만 특별히 대문자로 변수를 지으신 이유가 있으신가요? 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. 변수값이 변경될 때 직접 바뀌는 게 아니라(변수 값에 다른 값을 더하거나 빼는 등) 다른 값을 대입하기만 한다는 점에서 뭔가 대문자로 하는게 의미가 잘 전달될 것 같아서 지었습니다 |
||
winner.clear(); | ||
winner.add(car.getName()); | ||
} | ||
} | ||
return winner; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package view; | ||
|
||
import racingcar.Car; | ||
|
||
import java.util.List; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
public class InputValidator { | ||
public String[] validateSplit(String carString) { | ||
try { | ||
return carString.split(","); | ||
} catch (PatternSyntaxException e) { | ||
throw new IllegalArgumentException("[ERROR] 자동차 이름은 쉼표를 기준으로 구분되어야 한다."); | ||
} | ||
} | ||
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. 쉼표를 구분해서 에러를 띄우는 방법을 고민했는데, 좋은 방법인 것 같습니다! 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 validateCarNames(List<Car> cars) { | ||
for (Car car : cars) { | ||
String carName = car.getName(); | ||
if (carName.length() > 5) { | ||
throw new IllegalArgumentException("[ERROR] 자동차 이름은 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. InputValidator는 본인이 field를 가지는 것이아니라, 모든 로직을 파라미터로 받아서 검증하고 있는것 같은데, Static class는 어떠신가요? 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 validateEmpty(String[] carNames) { | ||
if (carNames.length == 0) { | ||
throw new IllegalArgumentException("[ERROR] 자동차는 1대 이상 입력 되어야 한다."); | ||
} | ||
for (String carName : | ||
carNames) { | ||
if (carName.trim().isEmpty()) { | ||
throw new IllegalArgumentException("[ERROR] 자동차 이름은 빈칸일 수 없다."); | ||
Comment on lines
+30
to
+33
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. 자동차 이름 빈칸 검증 부분이 단지 length가 0인 것만 생각했는데, 이런 방법도 있네요! 좋은 방법인 것 같습니다. 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 Integer validateTrial(String trial) { | ||
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. validate 형식의 메소드끼리 리턴타입을 통일시키는 것도 좋을 것 같습니다! 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. 참고하겠습니다! |
||
try { | ||
Integer trialNum = Integer.valueOf(trial); | ||
if (trialNum < 0) { | ||
throw new IllegalArgumentException("[ERROR] 시도 횟수는 음수일 수 없다."); | ||
} | ||
return trialNum; | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("[ERROR] 시도 횟수는 숫자여야 한다."); | ||
} | ||
} | ||
} | ||
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. 오류 케이스를 자세히 나누어 처리한게 정말 좋은 것 같습니다! 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,56 @@ | ||
package view; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
import racingcar.Car; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class InputView { | ||
private final InputValidator inputValidator; | ||
|
||
public InputView() { | ||
this.inputValidator = new InputValidator(); | ||
} | ||
|
||
public List<Car> getCars() { | ||
while (true) { | ||
try { | ||
System.out.println("경주할 자동차 이름을 입력하세요. (이름은 쉼표(,) 기준으로 구분)"); | ||
String carString = Console.readLine(); | ||
String[] carNames = validateCarString(carString); | ||
return validateCarNames(carNames); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
private String[] validateCarString(String carString) { | ||
String[] carNames = inputValidator.validateSplit(carString); | ||
inputValidator.validateEmpty(carNames); | ||
return carNames; | ||
} | ||
|
||
private List<Car> validateCarNames(String[] carNames) { | ||
List<Car> cars = new ArrayList<>(); | ||
for (String carName : | ||
carNames) { | ||
cars.add(new Car(carName)); | ||
} | ||
inputValidator.validateCarNames(cars); | ||
return cars; | ||
} | ||
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. validate라는 method 명을 가진 함수가 Car 객체를 생성하기위한 정보를 validation check 하고, 직접 생성까지 하는건 저는 책임이 조금 많아 보입니다. 혹시 view class car 생성 로직을 담당시킨 이유가 있으신가요? 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. 필요한 기능을 생각대로 구현하다 보니 저 method가 책임이 많아진 것 같습니다. |
||
|
||
public Integer getTrial() { | ||
while (true) { | ||
try { | ||
System.out.println("시도할 횟수는 몇회인가요?"); | ||
String trial = Console.readLine(); | ||
return inputValidator.validateTrial(trial); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package view; | ||
|
||
import racingcar.Car; | ||
|
||
import java.util.List; | ||
|
||
public class OutputView { | ||
public void printInit() { | ||
System.out.println("실행 결과"); | ||
} | ||
|
||
public void printResult(List<Car> cars) { | ||
for (Car car : cars) { | ||
printCarStatus(car); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
private void printCarStatus(Car car) { | ||
System.out.print(car.getName() + " : "); | ||
for (int i = 0; i < car.getPosition(); i++) { | ||
System.out.print("-"); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public void printFinalWinner(List<String> winner) { | ||
System.out.println("최종 우승자 : " + String.join(", ", winner)); | ||
} | ||
} |
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.
감사합니다 :)