-
Notifications
You must be signed in to change notification settings - Fork 7
2nd / 3rd 과제 PR _ 문자열 사칙 연산 계산기 구현 / 초간단 자동차 경주 게임 #25
base: CJiu01
Are you sure you want to change the base?
Changes from all commits
a030939
06d8368
fb01685
bc2ab97
ba525e1
61876d6
57cb5e5
bf7630f
c26d4ec
3e08154
5e6d8b9
fd5a6b5
f446555
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
|
||
|
||
plugins { | ||
id 'java' | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
org.gradle.jvmargs=-Dfile.encoding=UTF-8 | ||
org.gradle.console=plain | ||
org.gradle.console=plain |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,7 @@ case "`uname`" in | |
Darwin* ) | ||
darwin=true | ||
;; | ||
MINGW* ) | ||
MSYS* | MINGW* ) | ||
msys=true | ||
;; | ||
NONSTOP* ) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,4 +86,4 @@ exit /b 1 | |
:mainEnd | ||
if "%OS%"=="Windows_NT" endlocal | ||
|
||
:omega | ||
:omega |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package Calculator; | ||
|
||
public class CalculatorUserInput { | ||
public int calculateUser(String input) { | ||
String[] expression = separate(input); | ||
|
||
int result = getInt(expression[0]); | ||
for(int i=0; i<expression.length-2; i+=2){ | ||
result = Operator.calculate(result, getInt(expression[i+2]), expression[i+1]); | ||
} | ||
return result; | ||
} | ||
|
||
private int getInt(String str){ | ||
return Integer.parseInt(str); | ||
} | ||
|
||
public String[] separate(String str){ | ||
return str.split(" "); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package Calculator; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
public enum Operator{ | ||
ADD("+", (operand1, operand2) -> operand1+operand2), | ||
SUB("-", (operand1, operand2) -> operand1-operand2), | ||
MUL("*", (operand1, operand2) -> operand1*operand2), | ||
DIV("/", (operand1, operand2) -> operand1/operand2); | ||
|
||
private String symbol; | ||
private BiFunction<Integer, Integer, Integer> expression; | ||
|
||
Operator(String symbol, BiFunction<Integer, Integer, Integer> expression){ | ||
this.symbol = symbol; | ||
this.expression = expression; | ||
} | ||
|
||
public static int calculate(int operand1, int operand2, String inputSymbol){ | ||
for(Operator operator : Operator.values()) { | ||
if(operator.match(inputSymbol)){ | ||
return operator.expression.apply(operand1, operand2); | ||
} | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
private boolean match(String inputSymbol) { | ||
return this.symbol.equals(inputSymbol); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
public class Car { | ||
private final String name; | ||
private int position; | ||
|
||
public Car(String name) { | ||
validNameCheck(name); | ||
this.name = name; | ||
} | ||
|
||
private void validNameCheck(String name) { | ||
if(name.length() > Constant.CAR_NAME_LIMIT) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
|
||
public int move(boolean isSuccess) { | ||
if(isSuccess){ | ||
position++; | ||
} | ||
return position; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class Constant { | ||
// numberConstant | ||
public static final int FORWARD_MIN = 4; | ||
public static final int FORWARD_MAX = 10; | ||
public static final int CAR_NAME_LIMIT = 5; | ||
|
||
public static final char POSITION_MARK = '-'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Control { | ||
public static void main(String[] args) { | ||
|
||
InputView inputView = new InputView(); | ||
inputView.setUpRacingGame(); | ||
|
||
ResultView resultView = new ResultView(); | ||
|
||
RacingGame racingGame = new RacingGame(inputView.getNames()); | ||
racingGame.startGame(inputView.getTryCount()); | ||
racingGame.endGame(); | ||
resultView.showRacingResult(inputView.getTryCount()); | ||
resultView.showWinner(); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
private String inputNames; | ||
private int tryCount; | ||
|
||
public void setUpRacingGame(){ | ||
Scanner s = new Scanner(System.in); | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
inputNames = s.nextLine(); | ||
System.out.println("시도할 회수는 몇 회 인가요?"); | ||
tryCount = s.nextInt(); | ||
} | ||
|
||
public String getNames() { | ||
return inputNames; | ||
} | ||
|
||
public int getTryCount(){ | ||
return tryCount; | ||
} | ||
Comment on lines
+15
to
+21
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. InputValues 객체를 따로 만들어서 전달하는게 어떨까요? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public interface MoveStrategy { | ||
public boolean isMoveAble(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RacingCars { | ||
|
||
private final Map<Car, List<Integer>> positionInfo = new HashMap<>(); | ||
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. Map 안에 List 가 있는 구조보다는 결과를 기록하는 별도의 클래스를 만들어보면 좋겠습니다. |
||
|
||
public RacingCars(String inputNames){ | ||
readyGame(splitNames(inputNames)); | ||
} | ||
|
||
private String[] splitNames(String names) { | ||
return names.split(","); | ||
} | ||
|
||
private void readyGame(String[] 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. 요런 역할이라면 |
||
for(String name : cars) { | ||
positionInfo.put(new Car(name), new ArrayList<>()); | ||
} | ||
} | ||
|
||
public void moveCars() { | ||
positionInfo.forEach((k, v) -> v.add(k.move(new RandomMoveStrategy().isMoveAble()))); | ||
} | ||
|
||
public Map<Car, List<Integer>> getPositionInfo() { | ||
return positionInfo; | ||
} | ||
Comment on lines
+28
to
+30
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. Cars 가 Car 객체를 다루고 있는데 반환타입에 Car 를 포함하고 있는 Map을 반환하게 되면 |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
public class RacingGame { | ||
|
||
private final RacingCars cars; | ||
|
||
public RacingGame(String inputNames) { | ||
this.cars = new RacingCars(inputNames); | ||
} | ||
|
||
public void startGame(int tryCount){ | ||
for(int i=0; i<tryCount; i++){ | ||
cars.moveCars(); | ||
} | ||
} | ||
|
||
public void endGame(){ | ||
Winner winner = new Winner(cars.getPositionInfo()); | ||
winner.findWinner(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import java.util.Random; | ||
|
||
public class RandomMoveStrategy implements MoveStrategy{ | ||
|
||
private final Random random = new Random(); | ||
|
||
@Override | ||
public boolean isMoveAble() { | ||
return getRandomNumber() >= Constant.FORWARD_MIN; | ||
} | ||
|
||
private int getRandomNumber() { | ||
return random.nextInt(Constant.FORWARD_MAX); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
public class ResultView { | ||
RacingCars cars; | ||
private Winner winner; | ||
|
||
public void showRacingResult(int tryCount) { | ||
for(int i=0; i<tryCount; i++) { | ||
final int index = i; | ||
cars.getPositionInfo().forEach((k, v) -> showCarPosition(k, v.get(index))); | ||
} | ||
} | ||
|
||
public void showWinner() { | ||
String winnerName = String.join(",", winner.getWinnerNames()); | ||
System.out.println(winnerName + " 가 최종우승했습니다."); | ||
} | ||
|
||
private void showCarPosition(Car car, int 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. ResultView 에서 RacingCars 와의 연관되어 있는데 |
||
|
||
System.out.println(car.getName() + " : "); | ||
for (int i=0; i<position; i++) { | ||
System.out.print(Constant.POSITION_MARK); | ||
} | ||
System.out.println(); | ||
}; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Winner { | ||
private final Map<Car, List<Integer>> infoCar; | ||
private final List<String> winnerNames = new ArrayList<>(); | ||
private int max=0; | ||
|
||
public Winner(Map<Car, List<Integer>> infoCar) { | ||
this.infoCar = infoCar; | ||
} | ||
|
||
public List<String> getWinnerNames() { | ||
return winnerNames; | ||
} | ||
|
||
public Map<Car, List<Integer>> getInfoCar() { | ||
return infoCar; | ||
} | ||
|
||
public void findWinner() { | ||
findMaxPosition(); | ||
addWinner(); | ||
} | ||
|
||
private void addWinner() { | ||
for(Car car : infoCar.keySet()){ | ||
if(max == car.getPosition()){ | ||
winnerNames.add(car.getName()); | ||
} | ||
} | ||
} | ||
|
||
private void findMaxPosition() { | ||
for(Car car : infoCar.keySet()){ | ||
max = Math.max(max, car.getPosition()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package CalculatorTest; | ||
|
||
import Calculator.CalculatorUserInput; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CalculatorTest { | ||
CalculatorUserInput calculator = new CalculatorUserInput(); | ||
|
||
@Test | ||
@DisplayName("더하기 테스트") | ||
void addtest(){ | ||
assertThat(5).isEqualTo(calculator.calculateUser("3 + 2")); | ||
} | ||
|
||
@Test | ||
@DisplayName("빼기 테스트") | ||
void substracttest(){ | ||
assertThat(5).isEqualTo(calculator.calculateUser("12 - 7")); | ||
} | ||
|
||
@Test | ||
@DisplayName("곱하기 테스트") | ||
void multiplytest(){ | ||
assertThat(5).isEqualTo(calculator.calculateUser("5 * 1")); | ||
} | ||
|
||
@Test | ||
@DisplayName("나누기 테스트") | ||
void dividetest(){ | ||
assertThat(5).isEqualTo(calculator.calculateUser("25 / 5")); | ||
} | ||
|
||
@Test | ||
@DisplayName("문자열 나누기 테스트") | ||
void seperatetest(){ | ||
String str = "3 + 2"; | ||
String[] result = {"3", "+", "2"}; | ||
assertThat(result).isEqualTo(calculator.separate(str)); | ||
} | ||
|
||
@Test | ||
@DisplayName("사칙 연산 모두 포함하는 테스트") | ||
void allarithmetictest(){ | ||
assertThat(5).isEqualTo(calculator.calculateUser("3 + 12 - 5 * 4 / 8")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package CalculatorTest; | ||
|
||
import Calculator.CalculatorUserInput; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class ExceptionCalculatorTest{ | ||
CalculatorUserInput calculator = new CalculatorUserInput(); | ||
|
||
@Test | ||
@DisplayName("입력 값이 null 또는 빈 공백 문자인지 테스트") | ||
void inputformExceptiontest(){ | ||
String str = ""; | ||
assertThatThrownBy(()->{ | ||
calculator.calculateUser(str); | ||
}).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("사칙연산 기호 테스트") | ||
void operatorExceptiontest(){ | ||
String str = "2 $ 3"; | ||
assertThatThrownBy(()->{ | ||
calculator.calculateUser(str); | ||
}).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
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.
move를 굳이 isSuccess 를 전달할 필요는 없어보입니다.
그냥
move()
면 되지 않을까 하네요 :)