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
[차재언] Step2(RacingGame) 피드백 반영. #2204
Changes from 4 commits
2e22008
63d1db7
42f061e
70a20da
dd1a3f2
9ae509e
6eb0e4b
3b2b556
99d47f2
0f2fec4
0f31cd8
4e26208
e958b65
65a85e8
42f7540
e458649
aa26c82
4266ffc
bbcc308
522fdc5
03a02f5
9087550
d95aceb
df15861
7bc194d
903c176
041c4fb
41892ff
6dabc61
c589fb5
80a2215
20af7d5
4ed7d98
30901e1
a699d5a
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static calculator.ExceptionMessage.*; | ||
|
@@ -16,19 +17,23 @@ public class Calculator { | |
private static final String IS_NOT_MATH_EXPRESSION = "[^0-9+\\-*/]"; | ||
private static final String MAKE_EMPTY = ""; | ||
private static final int HAVE_NOT_APPENDED_NUMBER = 1; | ||
private static final int FIRST_NUMBER_INDEX = 0; | ||
|
||
Pattern notNumberPattern = Pattern.compile(NOT_NUMBER_PATTERN); | ||
Pattern theEndIsNotNumberPattern = Pattern.compile(THE_END_IS_NOT_NUMBER); | ||
Pattern isNotMathExpressionPattern = Pattern.compile(IS_NOT_MATH_EXPRESSION); | ||
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 printResult() { | ||
Calculator calculator = new Calculator(); | ||
outputStartMessage(); | ||
resultOutput(calculator.makeResult(makeSlicedMathExpression())); | ||
resultOutput(makeResult(makeSlicedMathExpression())); | ||
} | ||
|
||
public double calculate(double firstNumber, String operator, double secondNumber) { | ||
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.
|
||
return findValidatedSymbol(operator).operate(firstNumber, secondNumber); | ||
} | ||
|
||
private double makeResult(List<String> mathExpressions) { | ||
double result = Double.parseDouble(mathExpressions.get(0)); | ||
double result = Double.parseDouble(mathExpressions.get(FIRST_NUMBER_INDEX)); | ||
int mathExpressionSize = mathExpressions.size(); | ||
|
||
for (int i = 1; i < mathExpressionSize; i += 2) { | ||
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. 반복문에서 |
||
|
@@ -54,13 +59,15 @@ private void validate(String mathExpression) { | |
} | ||
|
||
private void isTheEndNotNumber(String mathExpression) { | ||
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.
|
||
if (Pattern.matches(THE_END_IS_NOT_NUMBER, mathExpression)) { | ||
Matcher matcher = theEndIsNotNumberPattern.matcher(mathExpression); | ||
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.
|
||
if (matcher.matches()) { | ||
throw new IllegalArgumentException(THE_END_IS_NOT_NUMBER_MESSAGE); | ||
} | ||
} | ||
|
||
private void isNotMathExpression(String mathExpression) { | ||
if (Pattern.matches(IS_NOT_MATH_EXPRESSION, mathExpression)) { | ||
Matcher matcher = isNotMathExpressionPattern.matcher(mathExpression); | ||
if (matcher.matches()) { | ||
throw new IllegalArgumentException(IS_NOT_MATH_EXPRESSION_MESSAGE); | ||
} | ||
} | ||
|
@@ -95,10 +102,8 @@ private List<String> convertSplitExpressionArrayToList(String[] splitMathExpress | |
} | ||
|
||
private boolean isNotNumberPattern(String mathSymbol) { | ||
if (Pattern.matches(NOT_NUMBER_PATTERN, mathSymbol)) { | ||
return true; | ||
} | ||
return false; | ||
Matcher matcher = notNumberPattern.matcher(mathSymbol); | ||
return matcher.matches(); | ||
} | ||
|
||
private boolean haveIntegerExpression(String integerOfMathExpression) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,9 @@ public String getMathSymbol() { | |
|
||
public static MathSymbol findValidatedSymbol(String operator) { | ||
return Arrays.stream(MathSymbol.values()) | ||
.filter(v -> v.getMathSymbol().equals(operator)) | ||
.filter(mathSymbol -> mathSymbol.getMathSymbol().equals(operator)) | ||
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. ㅎㅎ 이런 실수를 |
||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException(String.format("잘못된 연산자 입니다."))); | ||
.orElseThrow(() -> new IllegalArgumentException("잘못된 연산자 입니다.")); | ||
} | ||
|
||
public double operate(double firstNumber, double secondNumber) { | ||
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. 음.. 메소드 호출을 한 번 줄일 수 있어서 |
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package racing; | ||
|
||
import racing.domain.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. 사용되지 않는 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. 매번 실수해서 자동으로 제거하는 설정을 적용했습니다 ㅎㅎ |
||
import racing.domain.RacingGame; | ||
|
||
import java.util.List; | ||
|
||
import static racing.domain.RacingGame.SEPARATOR; | ||
import static racing.view.Input.makeCarNames; | ||
import static racing.view.Input.makeGameRepeatCount; | ||
import static racing.view.Output.*; | ||
import static racing.view.Output.printWinMessage; | ||
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 class RacingController { | ||
public static void main(String[] args) { | ||
RacingGame racingGame = new RacingGame(); | ||
|
||
printStartMessage(); | ||
String carNames = makeCarNames(); | ||
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.
|
||
printInputCountMessage(); | ||
int count = makeGameRepeatCount(); | ||
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. 컨트롤러에서 Output, Input에 총두번 접근하는 대신 대신 Output과 Input의 메서드가 많아지면 한번에 관리하기는 어려워진다? |
||
printResultMessage(); | ||
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. output을 생략하는 대신 print라는 동사를 공통적으로 붙였는데 |
||
String[] carNameArray = carNames.split(SEPARATOR); | ||
List<Car> carList = racingGame.makeCars(carNameArray); | ||
carList = racingGame.repeatMoveCars(count, carList); | ||
printNowDistance(carList); | ||
printWinMessage(racingGame.findWinner(carList)); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package racing; | ||
package racing.domain; | ||
|
||
public class Car { | ||
public static final int MOVE_CHANGE_CONDITION = 4; | ||
public static final int VALIDATED_NAME_LENGTH = 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 String name; | ||
private int moveCount; | ||
|
||
|
@@ -10,15 +12,13 @@ public Car(String name) { | |
} | ||
|
||
public void move(int randomNumber) { | ||
if (randomNumber >= 4) { | ||
if (randomNumber >= MOVE_CHANGE_CONDITION) { | ||
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.
|
||
this.moveCount++; | ||
} | ||
} | ||
|
||
private void validateLength(String name) { | ||
if (name.length() > 5) { | ||
throw new IllegalArgumentException("이름은 5자 이내로 입력하세요."); | ||
} | ||
public boolean isWinner(int winnerCount) { | ||
return moveCount == winnerCount; | ||
} | ||
|
||
public String getName() { | ||
|
@@ -29,4 +29,9 @@ public int getMoveCount() { | |
return moveCount; | ||
} | ||
|
||
private void validateLength(String name) { | ||
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. 완료! |
||
if (name.length() > VALIDATED_NAME_LENGTH) { | ||
throw new IllegalArgumentException("이름은 5자 이내로 입력하세요."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package racing.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static racing.utils.RandomNumber.makeOneRandomNumber; | ||
|
||
public class RacingGame { | ||
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. List를 상태로 가졌더니 메소드 내부에서 List를 중복해서 선언하던 코드들이사라지고 또한 RacingGame 객체를 선언하고 관리하는 컨트롤러에서도 리스트를 갱신하는 일이 사라졌네요 |
||
public static String SEPARATOR = ","; | ||
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 List<Car> makeCars(String[] carNameArray) { | ||
List<Car> cars = new ArrayList<>(); | ||
|
||
for (String carName : carNameArray) { | ||
cars.add(new Car(carName)); | ||
} | ||
return cars; | ||
} | ||
|
||
public List<Car> repeatMoveCars(int count, List<Car> carList) { | ||
for (int i = 0; i < count; i++) { | ||
carList = moveCars(carList); | ||
} | ||
return carList; | ||
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 List<String> findWinner(List<Car> carList) { | ||
List<String> winnerList = new ArrayList<>(); | ||
int winnerCondition = findWinnerCondition(carList); | ||
for (Car car : carList) { | ||
if (car.isWinner(winnerCondition)) { | ||
winnerList.add(car.getName()); | ||
} | ||
} | ||
return winnerList; | ||
} | ||
|
||
private List<Car> moveCars(List<Car> carList) { | ||
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 (int i = 0; i < carList.size(); i++) { | ||
carList.get(i).move(makeOneRandomNumber()); | ||
} | ||
return carList; | ||
} | ||
|
||
private int findWinnerCondition(List<Car> carList) { | ||
int winnerCondition = 0; | ||
for (Car car : carList) { | ||
if (car.getMoveCount() >= winnerCondition) { | ||
winnerCondition = car.getMoveCount(); | ||
} | ||
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. 오 새로운 접근 방법이네요 |
||
} | ||
return winnerCondition; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ public static String makeCarNames() { | |
public static int makeGameRepeatCount() { | ||
return scanner.nextInt(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package racing.view; | ||
|
||
import racing.Car; | ||
import racing.domain.Car; | ||
|
||
import java.util.List; | ||
|
||
|
@@ -12,6 +12,7 @@ public class Output { | |
private static final String LOAD = "-"; | ||
private static final String WINNER_SEPARATOR = ", "; | ||
private static final String DISTANCE_SEPARATOR = " : "; | ||
private static final int BUILDER_INIT = 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 static void printStartMessage() { | ||
System.out.println(START_MESSAGE); | ||
|
@@ -28,26 +29,34 @@ public static void printResultMessage() { | |
public static void printWinMessage(List<String> winnerList) { | ||
System.out.print("\n" + WIN_MESSAGE); | ||
|
||
int memberIndexOfRequiringSeparator = winnerList.size() -2; | ||
int memberIndexOfRequiringSeparator = winnerList.size() - 2; | ||
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. 변수명을 lastMemberIndexOfRequiringSeparator 로 변경하고 뒤에 숫자는 변경 안했습니다. |
||
int memberIndexOfNotRequiringSeparator = memberIndexOfRequiringSeparator + 1; | ||
StringBuilder winMessageStringBuilder = new StringBuilder(); | ||
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. 그런가요..변경 후 고민해 보겠습니다. |
||
|
||
for (int i = 0; i <= memberIndexOfRequiringSeparator; i++) { | ||
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. 이 부분은 잘못 이해 하신듯 합니다. |
||
System.out.print(winnerList.get(i) + WINNER_SEPARATOR); | ||
winMessageStringBuilder.append(winnerList.get(i)); | ||
winMessageStringBuilder.append(WINNER_SEPARATOR); | ||
System.out.print(winMessageStringBuilder); | ||
winMessageStringBuilder.setLength(BUILDER_INIT); | ||
} | ||
System.out.println(winnerList.get(memberIndexOfNotRequiringSeparator)); | ||
} | ||
|
||
public static void printNowDistance(List<Car> carList) { | ||
System.out.println(); | ||
StringBuilder distanceStringBuilder = new StringBuilder(); | ||
for (int i = 0; i < carList.size(); i++) { | ||
System.out.print(carList.get(i).getName() + DISTANCE_SEPARATOR); | ||
distanceStringBuilder.append(carList.get(i).getName()); | ||
distanceStringBuilder.append(DISTANCE_SEPARATOR); | ||
System.out.print(distanceStringBuilder); | ||
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. 출력 형식에 맞게 문자열 데이터의 가공을 |
||
printLoadConstant(carList.get(i).getMoveCount()); | ||
System.out.println(); | ||
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.
|
||
distanceStringBuilder.setLength(BUILDER_INIT); | ||
} | ||
} | ||
|
||
private static void printLoadConstant(int gameRepeatCount) { | ||
for (int j = 0; j < gameRepeatCount; j++) { | ||
private static void printLoadConstant(int moveCount) { | ||
for (int j = 0; j < moveCount; j++) { | ||
System.out.print(LOAD); | ||
} | ||
} | ||
|
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.
패턴을 미리 만들어주셨네요! 👍 👍 👍