-
Notifications
You must be signed in to change notification settings - Fork 454
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
[자동차 경주 게임] 효오 미션 제출합니다. #40
Changes from 37 commits
8d9603f
741d9e2
36eace4
71fe0fb
355fa4a
dd953db
a5f3c03
92f7d2c
7e795e8
04d5e48
9bf8ef5
4f49cfb
00b3923
ee55775
e9009e2
de9cf4b
eac0a11
40c500d
12f9851
e71ab75
32c5e64
f15e7a2
d50b0b1
3529fd4
60c443b
67ca843
a4e18bb
a7ce293
800c083
7fb5694
25f9040
361f2bc
49fb2f5
bc91ce4
d8a7233
5a29bd0
ad2f38b
dddc55d
7ee3a0e
7f316c3
d548d21
0d5e7c9
324af58
b2e0f3d
d9f6ad0
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,8 @@ | ||
public class Calculator { | ||
int addition(int i, int j) { | ||
return i+j; | ||
} | ||
int subtraction(int i, int j) { | ||
return i-j; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package calculator; | ||
|
||
import exceptionsCalculator.CalculatorException; | ||
|
||
enum Calculate { | ||
RESULT(); | ||
|
||
public int getCalculateResult(int firstNumber, int secondNumber, String symbol) { | ||
if (symbol.equals("+")) return firstNumber + secondNumber; | ||
if (symbol.equals("-")) return firstNumber - secondNumber; | ||
if (symbol.equals("*")) return firstNumber * secondNumber; | ||
if (symbol.equals("/")) return CalculatorException.divisionException(firstNumber, secondNumber); | ||
return CalculatorException.applyCalculationException(); | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package calculator; | ||
|
||
import exceptionsCalculator.CalculatorException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Calculator { | ||
|
||
public static void main(String[] args) { | ||
doCalculate(); | ||
} | ||
|
||
public static void doCalculate() { | ||
String userInput = CalculatorException.readAndReceiveInput(); | ||
List<String> numbers = new ArrayList<>(Arrays.asList(userInput.split(" "))); | ||
List<Integer> numberList = extractNumbers(numbers); | ||
List<String> symbolList = extractSymbols(numbers); | ||
System.out.println(calculate(numberList, symbolList)); | ||
} | ||
|
||
public static List<Integer> extractNumbers(List<String> numbers) { | ||
List<Integer> listOfNumbers = new ArrayList<>(); | ||
for (int i = 0; i < numbers.size(); i += 2) { | ||
int number = CalculatorException.extractNumbersException(numbers.get(i)); | ||
listOfNumbers.add(number); | ||
} | ||
return listOfNumbers; | ||
} | ||
|
||
public static List<String> extractSymbols(List<String> symbols) { | ||
List<String> listOfSymbols = new ArrayList<>(); | ||
for (int i = 1; i < symbols.size(); i += 2) { | ||
listOfSymbols.add(symbols.get(i)); | ||
jihan805 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return listOfSymbols; | ||
} | ||
|
||
public static int calculate(List<Integer> listOfNumbers, List<String> listOfSymbols) { | ||
int result = listOfNumbers.get(0); | ||
for (int i = 0; i < listOfSymbols.size(); i++) { | ||
result = Calculate.RESULT.getCalculateResult(result, listOfNumbers.get(i + 1), listOfSymbols.get(i)); | ||
} | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package exceptionsCalculator; | ||
|
||
import calculator.Calculator; | ||
|
||
import java.util.Scanner; | ||
|
||
public class CalculatorException { | ||
|
||
public static String readAndReceiveInput() { | ||
System.out.println("숫자를 입력해 주세요!"); | ||
Scanner reader = new Scanner(System.in); | ||
return reader.nextLine(); | ||
} | ||
|
||
public static int divisionException(int result, int number){ | ||
if (number == 0) { | ||
System.out.println("0이 분모에 있습니다."); | ||
Calculator.doCalculate(); | ||
} | ||
return result / number; | ||
} | ||
|
||
public static int extractNumbersException(String stringNumber) { | ||
try{ | ||
return Integer.parseInt(stringNumber); | ||
}catch (Exception e){ | ||
System.out.println("잘못된 입력값입니다! 숫자가 아닙니다!"); | ||
Calculator.doCalculate(); | ||
} | ||
return -1; | ||
} | ||
|
||
public static int applyCalculationException(){ | ||
System.out.println("잘못된 입력값입니다. 기호가 아닙니다!"); | ||
Calculator.doCalculate(); | ||
return -1; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package racingcar; | ||
|
||
import java.util.*; | ||
|
||
public class Car { | ||
private static final int MAX_NAME_SIZE = 5; | ||
private static final int POSSIBLE_MOVE_CAR = 4; | ||
private static final int MAX_RANDOM_NUMBER = 10; | ||
private static final int MIN_RANDOM_NUMBER = 0; | ||
|
||
private final String name; | ||
jihan805 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private int position = 0; | ||
|
||
Car(String name) { | ||
if (isWhiteSpaceOnly(name)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
if (isOverLimit(name)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.name = name; | ||
} | ||
|
||
Car(String name, int position) { | ||
this.name = 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. 더불어 Car(String name) 생성자에서 position에 대한 초기화도 함께 진행하는 것이 어떨까요 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(String name) 생성자에서 position을 초기화 하도록 다른 생성자로 포지션을 0을 넘겨주어 리팩토링하였습니다. |
||
this.position = position; | ||
} | ||
|
||
public static boolean isWhiteSpaceOnly(String name) { | ||
return name.isEmpty(); | ||
} | ||
|
||
public static boolean isOverLimit(String name) { | ||
return name.length() > MAX_NAME_SIZE; | ||
} | ||
|
||
public Car moveCar(int randomNumber) { | ||
if (randomNumber >= POSSIBLE_MOVE_CAR) { | ||
position++; | ||
} | ||
return this; | ||
} | ||
|
||
public static int randomNumberGenerator() { | ||
return (int) (Math.random() * MAX_RANDOM_NUMBER) + MIN_RANDOM_NUMBER; | ||
} | ||
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. (Math.random() * MAX_RANDOM_NUMBER) 자체로 0~9 사이의 랜덤 값을 생성해 주기 때문에 MIN_RANDOM_NUMBER(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 int findMax(int max) { | ||
if (position > max) { | ||
max = position; | ||
} | ||
return max; | ||
} | ||
|
||
public String sameMaxPositionCarName(int maxPosition) { | ||
if (this.position == maxPosition) { | ||
return this.name; | ||
} | ||
return null; | ||
} | ||
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. null을 리턴하는 것은 굉장히 위험합니다! 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. 이번 미션을 통해서 null 값을 리턴하는 것이 위험하다고 깨달았고 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 this.position == maxPosition 같이 한줄로 구현할 수 있어요! |
||
|
||
public String getCarState() { | ||
String carState = this.name + " : "; | ||
for (int i = 0; i < this.position; i++) { | ||
carState += "-"; | ||
} | ||
return carState; | ||
} | ||
|
||
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. 출력을 위한 string을 생성하는 코드로 보여지는데요, 관련하여 toString Override에 대해 찾아보고 적용하면 좋을것 같습니다! 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. toString을 활용하니 더 효율적이고 앞으로도 유용하게 쓰일 것 같다고 느꼈습니다. |
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Car car = (Car) o; | ||
return position == car.position && | ||
Objects.equals(name, car.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, position); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package racingcar; | ||
|
||
import racingcar.view.InputView; | ||
import racingcar.view.InputViewException; | ||
import racingcar.view.OutputView; | ||
|
||
public class CarGameLauncher { | ||
public static void main(String[] args) { | ||
doCarGame(); | ||
} | ||
|
||
public static void doCarGame() { | ||
Cars cars = new Cars(InputView.askAndReceiveCarNames()); | ||
int totalTurns = InputViewException.askAndReceiveTotalTurns(); | ||
Play.printCarState(cars, totalTurns); | ||
Winners winners = new Winners(cars); | ||
OutputView.printWinners(winners); | ||
System.exit(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. Play 클래스의 역할과 CarGameLauncher 역할이 섞여 있는 것 같아요!
구현에 정답은 없지만, 힌트는 위에 첨부한 코드와 같습니다! 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. 피드백 주신대로 play에서 자동차를 생성/ 경주를 진행하고 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 < totalTurns; i++) 이 부분도 Play가 담당하도록 구현해봐도 좋을 것 같아요! 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package racingcar; | ||
|
||
import java.util.*; | ||
|
||
public class Cars { | ||
private List<Car> cars = new ArrayList<>(); | ||
|
||
Cars(String names) { | ||
names = names.replaceAll("\\s+", ""); | ||
List<String> carNames = new ArrayList<>(Arrays.asList(names.split(","))); | ||
instantiateCar(carNames); | ||
} | ||
jihan805 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Cars(ArrayList<Car> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public int getSize() { | ||
return cars.size(); | ||
} | ||
|
||
public String getCarState(int index) { | ||
return cars.get(index).getCarState(); | ||
} | ||
|
||
public void updateCarMovement(int index) { | ||
cars.get(index).moveCar(Car.randomNumberGenerator()); | ||
} | ||
|
||
public List<Car> getCars() { | ||
return cars; | ||
} | ||
|
||
public void instantiateCar(List<String> carNames) { | ||
try { | ||
isDuplicate(carNames); | ||
addCarToCars(carNames); | ||
} catch (Exception e) { | ||
CarGameLauncher.doCarGame(); | ||
} | ||
} | ||
|
||
public void addCarToCars(List<String> carNames) { | ||
for (String name : carNames) { | ||
cars.add(new Car(name)); | ||
} | ||
} | ||
|
||
public static void isDuplicate(List<String> names) { | ||
Set<String> nameSet = new HashSet<>(names); | ||
if (names.size() != nameSet.size()) { | ||
System.out.println("이름에 중복이 있습니다!"); | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
jihan805 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Cars cars1 = (Cars) o; | ||
return Objects.equals(cars, cars1.cars); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(cars); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package racingcar; | ||
|
||
import racingcar.view.OutputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Play { | ||
|
||
public static void printCarState(Cars cars, int turn) { | ||
OutputView.printResult(); | ||
for (int i = 0; i < turn; i++) { | ||
moveCar(cars); | ||
OutputView.printState(getCarStates(cars)); | ||
} | ||
} | ||
|
||
public static List<String> getCarStates(Cars cars) { | ||
List<String> carStates = new ArrayList<>(); | ||
for (int i = 0, n = cars.getSize(); i < n; i++) { | ||
carStates.add(cars.getCarState(i)); | ||
} | ||
return carStates; | ||
} | ||
|
||
public static void moveCar(Cars cars) { | ||
for (int i = 0, n = cars.getSize(); i < n; 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. index를 사용한 for loop 보다는 for each에 대해 공부해보면 좋을 것 같습니다! 로 변경하고 Cars 클래스에서 Cars가 가지고 있는 Car List를 순회하며 move 하는 것은 어떨까요? 다만, 해당 클래스의 구조는 제가 위에 드린 예시 코드 적용에 맞지 않을 수 있다고 생각됩니다! 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. 전에 피드백 주신 play 부분을 고치고 나니 여기 부분도 쉽게 수정할 수 있었고 코드가 깔끔해지며 효율성이 더 높아진 효과가 있는 것 같습니다! |
||
cars.updateCarMovement(i); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package racingcar; | ||
|
||
import java.util.*; | ||
|
||
public class Winners { | ||
private List<String> listOfWinners = new ArrayList<>(); | ||
|
||
Winners(Cars cars) { | ||
List<Car> carsState = cars.getCars(); | ||
int maxPosition = decideMaxPosition(carsState); | ||
decideWinners(carsState, maxPosition); | ||
} | ||
|
||
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. Winners 클래스는 listOfWinners를 String으로 가지고 있는 대신, List를 가지고 있는 것은 어떨까요!? } 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 int decideMaxPosition(List<Car> cars) { | ||
int max = 0; | ||
for (Car car : cars) { | ||
max = car.findMax(max); | ||
} | ||
return max; | ||
} | ||
|
||
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. max 포지션 분리 👍 |
||
public void decideWinners(List<Car> cars, int maxPosition) { | ||
for (Car car : cars) { | ||
addWinners(car, maxPosition); | ||
} | ||
} | ||
|
||
public void addWinners(Car car, int maxPosition) { | ||
String winnerName = car.sameMaxPositionCarName(maxPosition); | ||
if (winnerName != null) { | ||
listOfWinners.add(winnerName); | ||
} | ||
} | ||
|
||
public String getWinners() { | ||
return String.join(", ", listOfWinners); | ||
} | ||
|
||
} |
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.
앞으로 남은 미션을 진행하면서 enum이 굉장히 자주 유용하게 사용될것입니다!
따라서 이번 미션을 통해서 enum에 대해 완전히 숙지 하지 못하였더라도 꼭 공부하면 좋을 것 같습니다!!
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.
다른분이 슬랙에 올려주신 내용을 확인했는데 enum 말고 다른 내용도 이해가 필요할 것 같아서 시간 남을 때마다 모르는 부분을 체크하고 공부하려고 합니다 ㅠ