Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement calculator * implement calculator * feat(calc): add 기능 추가 * feat(calc): 뺄셈 기능 추가 * feat(calc): 곱하기 기능 추가 * feat(calc): 나누기 기능 추가 * feat(calc): 사칙 연산을 모두 포함하는 계산 기능 추가 * feat(calc): 입력 값이 null 이거나 빈 공백일 경우 예외처리 * 사칙연산 기호가 아닌 경우 예외처리 * fix(calc): 피드백 개선 * implement calculator * Revert "implement calculator" This reverts commit c017e34. * docs(readme): add README.md file. * feat(race): 자동차 수와 이동 횟수 입력 기능 추가 * feat(race): 사용자에게 입력 받는 기능 추가 * feat(race): 랜덤 값에 따라 전진와 정지 결정 로직 추가 * refactor(race): 이동 로직 변경 * reactor(race): 중복되는 print 관련 코드 수정 * refactor(race): 객체 분리되도록 개선 * docs(readme): Step3 요구사항 추가 * feat(cars): 자동차가 이름을 가질 수 있는 기능 추가 * refactor(strategy): 줄일 수 있는 로직 개선 * feat(printer): 승자 출력 기능 추가 * tset(cars): RacingGame 생성자 인자 변경에 따른 테스트코드 수정 * feat(winner-maker): 승자 선정 기능과 테스트 추가
- Loading branch information
1 parent
efe7dcd
commit f5b31aa
Showing
10 changed files
with
187 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
package racing; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class Prompter { | ||
static public int getPrompt(String message) { | ||
static public int getIntPrompt(String message) { | ||
System.out.println(message); | ||
|
||
return getInput(); | ||
return getIntInput(); | ||
} | ||
|
||
static private int getInput() { | ||
Scanner scanner = new Scanner(System.in); | ||
return scanner.nextInt(); | ||
static public List<String> getStringsPrompt(String message) { | ||
System.out.println(message); | ||
|
||
String[] carNames = split(getStringInput()); | ||
|
||
return Arrays.asList(carNames); | ||
} | ||
|
||
static private int getIntInput() { | ||
return getScanner().nextInt(); | ||
} | ||
|
||
static private String getStringInput() { | ||
return getScanner().nextLine(); | ||
} | ||
|
||
static private Scanner getScanner() { | ||
return new Scanner(System.in); | ||
} | ||
|
||
static private String[] split(String names) { | ||
return names.split(","); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
## 자동차 경주 | ||
|
||
* 기능 | ||
* 사용자로부터 자동차의 수와 이동 횟수를 입력받는다. | ||
* 입력받은 값에 따라 전진 혹은 정지한다. | ||
* 자동차의 상태를 출력한다. | ||
* 기능 | ||
* step2 | ||
+ 사용자로부터 자동차의 수와 이동 횟수를 입력받는다. | ||
+ 입력받은 값에 따라 전진 혹은 정지한다. | ||
+ 자동차의 상태를 출력한다. | ||
* step3 | ||
- 전진하는 자동차를 출력할때 자동차의 이름을 같이 출력한다. | ||
+ 각 자동차에 이름을 부여할 수 있다. | ||
+ 전진하는 자동차를 출력할때 자동차의 이름을 같이 출력한다. | ||
+ 게임이 완료되면 누가 우승했는지 알려준다. (우승자는 한명 이상이다) | ||
|
||
* 요구사항 | ||
* 프로그래밍 | ||
* 모든 로직에 단위 테스트를 구현한다. | ||
* 자바 코드 컨벤션을 지킨다. | ||
* else 에약어 사용하지않는다. | ||
* else 예약어 사용하지않는다. | ||
* indent depth를 2를 넘지 않도록 구현한다. | ||
* 함수(또는 메소드)의 길이가 15라인을 넘어가지 않도록 구현한다. | ||
* 모든 로직에 단위 테스트를 구현한다. (핵심로직을 구현하는 코드와 UI를 담당하는 로직을 구분한다.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package racing; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class WinnerMaker { | ||
private List<Car> cars; | ||
|
||
public WinnerMaker(List<Car> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public List<Car> getWinners() { | ||
int max = getWinnerRecord(cars); | ||
|
||
return findWinners(max, cars); | ||
} | ||
|
||
private int getWinnerRecord(List<Car> cars) { | ||
int max = Integer.MIN_VALUE; | ||
|
||
for (Car car : cars) { | ||
int position = car.getPosition(); | ||
if (isBigger(position, max)) { | ||
max = position; | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
|
||
private List<Car> findWinners(int max, List<Car> cars) { ; | ||
List<Car> winners = cars.stream() | ||
.filter(car -> car.getPosition() == max) | ||
.collect(Collectors.toList()); | ||
|
||
return winners; | ||
} | ||
|
||
private boolean isBigger(int original, int target) { | ||
return original > target; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package racing; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class WinnerTest { | ||
|
||
private String INPUT_CAR_NAME = "car"; | ||
private String INPUT_CAR_NAME_COMPARED = "car_compared"; | ||
|
||
private WinnerMaker winnerMaker; | ||
private Car mCar; | ||
private Car mCompared; | ||
private List<Car> cars; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mCar = new Car(INPUT_CAR_NAME); | ||
mCompared = new Car(INPUT_CAR_NAME_COMPARED); | ||
|
||
cars = Arrays.asList(mCompared, mCar); | ||
|
||
winnerMaker = new WinnerMaker(cars); | ||
} | ||
|
||
@Test | ||
void winner() { | ||
mCompared.go(); | ||
|
||
assertThat(new WinnerMaker(cars).getWinners()).containsExactly(mCompared); | ||
} | ||
|
||
@Test | ||
void winners_두명이상() { | ||
assertThat(new WinnerMaker(cars).getWinners()).contains(mCar, mCompared); | ||
} | ||
} |