-
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 all 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,9 @@ | ||
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 final String name; | ||
jihan805 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private int position = 0; | ||
|
||
Car(String name) { | ||
this(name, 0); | ||
} | ||
|
||
Car(String name, int position) { | ||
if (isWhiteSpaceOnly(name)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
if (isOverLimit(name)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.name = name; | ||
this.position = position; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
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 int findMax(int max) { | ||
if (position > max) { | ||
max = position; | ||
} | ||
return max; | ||
} | ||
|
||
public boolean isSameMaxPosition(int maxPosition) { | ||
if (this.position == maxPosition) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
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 같이 한줄로 구현할 수 있어요! |
||
|
||
@Override | ||
public String toString() { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append(name + " : "); | ||
for (int i = 0; i < position; i++) { | ||
stringBuilder.append("-"); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
|
||
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,23 @@ | ||
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() { | ||
String names = InputView.askAndReceiveCarNames(); | ||
int totalTurns = InputViewException.askAndReceiveTotalTurns(); | ||
Play play = new Play(names); | ||
OutputView.printResult(); | ||
for (int i = 0; i < totalTurns; i++) { | ||
OutputView.printState(play.moveCarState()); | ||
} | ||
OutputView.printWinners(play.getWinners()); | ||
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,73 @@ | ||
package racingcar; | ||
|
||
import racingcar.util.Utils; | ||
|
||
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).toString(); | ||
} | ||
|
||
public void updateCarMovement() { | ||
for (Car car : cars) { | ||
car.moveCar(Utils.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,29 @@ | ||
package racingcar; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Play { | ||
private final Cars cars; | ||
|
||
public Play(String names) { | ||
cars = new Cars(names); | ||
} | ||
|
||
public String getWinners() { | ||
return new Winners(this.cars).getWinnerNames(); | ||
} | ||
|
||
public List<String> moveCarState() { | ||
cars.updateCarMovement(); | ||
return getCarStates(); | ||
} | ||
|
||
public List<String> getCarStates() { | ||
List<String> carStates = new ArrayList<>(); | ||
for (int i = 0, n = cars.getSize(); i < n; i++) { | ||
carStates.add(cars.getCarState(i)); | ||
} | ||
return carStates; | ||
} | ||
} |
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; | ||
|
||
Winners(Cars cars) { | ||
List<Car> carsState = cars.getCars(); | ||
int maxPosition = decideMaxPosition(carsState); | ||
this.listOfWinners = getResultWinners(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 List<String> getResultWinners(List<Car> cars, int maxPosition) { //TODO get winners | ||
List<String> listOfWinners = new ArrayList<>(); | ||
for (Car car : cars) { | ||
findWinner(listOfWinners, car, maxPosition); | ||
} | ||
return listOfWinners; | ||
} | ||
|
||
public void findWinner(List<String> listOfWinners, Car car, int maxPosition) { | ||
if (car.isSameMaxPosition(maxPosition)) { | ||
listOfWinners.add(car.getName()); | ||
} | ||
} | ||
|
||
public String getWinnerNames() { | ||
return String.join(", ", listOfWinners); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package racingcar.util; | ||
|
||
public class Utils { | ||
private static final int MAX_RANDOM_NUMBER = 10; | ||
|
||
public static int randomNumberGenerator() { | ||
return (int) (Math.random() * MAX_RANDOM_NUMBER); | ||
} | ||
} |
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 말고 다른 내용도 이해가 필요할 것 같아서 시간 남을 때마다 모르는 부분을 체크하고 공부하려고 합니다 ㅠ