Skip to content

Commit efd80ef

Browse files
authored
πŸš€ 3단계 - μžλ™μ°¨ κ²½μ£Ό (#5971)
* [step2] 2단계 - λ¬Έμžμ—΄ λ§μ…ˆ 계산기 κ΅¬ν˜„ * [step3] μ‚¬μš©μž μž…λ ₯ 처리 κΈ°λŠ₯ κ΅¬ν˜„ * [step3] μžλ™μ°¨ μ΄λ™λ‘œμ§ κ΅¬ν˜„ * [step3] μžλ™μ°¨ μƒνƒœ 좜λ ₯ κ΅¬ν˜„ * [step3] refactor : racingCar ꡬ쑰 κ°œμ„  - Car 도메인 객체 μΆ”κ°€ - μž…λ ₯κ°’μ—λŒ€ν•œ 검증 μΆ”κ°€ - λ§€μ§λ„˜λ²„, Random 객체 μƒμˆ˜ν™” - ν…ŒμŠ€νŠΈ μ½”λ“œ μˆ˜μ • * [step3] refactor : racingCar ꡬ쑰 κ°œμ„  - κΈ°μ‘΄ RacingCar -> RacingCarService λ³€κ²½ - μΌκΈ‰μ»¬λ ‰μ…˜ RacingCar μΆ”κ°€ - MovingStrategy interface μΆ”κ°€ - RandomGenerator, RandomStrategy μΆ”κ°€ - ν…ŒμŠ€νŠΈ μ½”λ“œ μˆ˜μ • * [step3] refactor : racingCar ꡬ쑰 κ°œμ„  - RacingCarService -> RacingCar 원볡 - μΌκΈ‰μ»¬λ ‰μ…˜ Cars μΆ”κ°€ - Race class μΆ”κ°€ - ν…ŒμŠ€νŠΈ μ½”λ“œ μˆ˜μ • * fix : main ν•¨μˆ˜ 제거 및 CAR μƒμˆ˜κ°’ λ³€κ²½
1 parent 837df83 commit efd80ef

11 files changed

+328
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package racingcar;
2+
3+
import racingcar.domain.Car;
4+
import racingcar.domain.Cars;
5+
import racingcar.domain.Race;
6+
import racingcar.generator.RandomGenerator;
7+
import racingcar.strategy.RandomStrategy;
8+
import racingcar.ui.InputView;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class RacingCar {
14+
15+
public static void gameStart() {
16+
Cars cars = new Cars(getCars(InputView.inputValidatedNumberOfCar()));
17+
Race race = new Race(cars, InputView.inputValidatedNumberOfAttempts());
18+
race.start(createRandomStrategy());
19+
}
20+
21+
public static ArrayList<Car> getCars(int car) {
22+
ArrayList<Car> cars = new ArrayList<>();
23+
for(int i=0; i<car; i++){
24+
cars.add(new Car());
25+
}
26+
return cars;
27+
}
28+
29+
private static RandomStrategy createRandomStrategy() {
30+
return new RandomStrategy(new RandomGenerator());
31+
}
32+
33+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package racingcar.domain;
2+
3+
import racingcar.strategy.MovingStrategy;
4+
5+
public class Car {
6+
private int moveCount;
7+
8+
public Car() {
9+
this.moveCount = 1;
10+
}
11+
12+
public int getMoveCount() {
13+
return moveCount;
14+
}
15+
16+
17+
public void move(MovingStrategy movingStrategy) {
18+
if(movingStrategy.isMove()){
19+
moveCount++;
20+
}
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package racingcar.domain;
2+
3+
import racingcar.strategy.MovingStrategy;
4+
import racingcar.ui.ResultView;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
public class Cars {
11+
12+
private final List<Car> cars;
13+
14+
public Cars(List<Car> cars) {
15+
this.cars = new ArrayList<>(cars);
16+
}
17+
18+
public void moveAll(MovingStrategy strategy) {
19+
cars.forEach(car -> car.move(strategy));
20+
}
21+
22+
public List<Car> getCurrentStatus() {
23+
return Collections.unmodifiableList(cars);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package racingcar.domain;
2+
3+
import racingcar.strategy.MovingStrategy;
4+
import racingcar.ui.ResultView;
5+
6+
public class Race {
7+
private final Cars cars;
8+
private final int totalAttempts;
9+
10+
public Race(Cars cars, int totalAttempts) {
11+
this.cars = cars;
12+
this.totalAttempts = totalAttempts;
13+
}
14+
15+
public void start(MovingStrategy strategy) {
16+
System.out.println("μ‹€ν–‰ κ²°κ³Ό");
17+
ResultView.viewRacingCar(cars.getCurrentStatus());
18+
for (int attempt = 1; attempt < totalAttempts; attempt++) {
19+
cars.moveAll(strategy);
20+
ResultView.viewRacingCar(cars.getCurrentStatus());
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package racingcar.generator;
2+
3+
import java.util.Random;
4+
5+
public class RandomGenerator {
6+
public static final Random RANDOM = new Random();
7+
public static final int RANDOM_MAX_NUMBER = 10;
8+
9+
public static int generate() {
10+
return RANDOM.nextInt(RANDOM_MAX_NUMBER);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package racingcar.strategy;
2+
3+
public interface MovingStrategy {
4+
boolean isMove();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package racingcar.strategy;
2+
3+
import racingcar.generator.RandomGenerator;
4+
5+
public class RandomStrategy implements MovingStrategy {
6+
7+
private static final int RANDOM_MIN_NUMBER = 4;
8+
private final RandomGenerator randomGenerator;
9+
10+
public RandomStrategy(RandomGenerator randomGenerator) {
11+
this.randomGenerator = randomGenerator;
12+
}
13+
14+
@Override
15+
public boolean isMove() {
16+
return randomGenerator.generate() >= RANDOM_MIN_NUMBER;
17+
}
18+
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package racingcar.ui;
2+
3+
import java.util.Scanner;
4+
5+
public class InputView {
6+
7+
public static Scanner SCANNER = new Scanner(System.in);
8+
public static final int CAR_MIN = 0;
9+
public static final int NUM_MIN = 0;
10+
11+
// ν…ŒμŠ€νŠΈλ₯Ό μœ„ν•œ Scanner μ„€μ • λ©”μ†Œλ“œ
12+
public static void setScanner(Scanner scanner) {
13+
SCANNER = scanner;
14+
}
15+
16+
public static int inputNumberOfCar() {
17+
System.out.println("μžλ™μ°¨ λŒ€μˆ˜λŠ” λͺ‡ λŒ€ μΈκ°€μš”?");
18+
return SCANNER.nextInt();
19+
}
20+
public static int inputNumberOfAttempts() {
21+
System.out.println("μ‹œλ„ν•  νšŒμˆ˜λŠ” λͺ‡ 회 μΈκ°€μš”?");
22+
return SCANNER.nextInt();
23+
}
24+
25+
public static int inputValidatedNumberOfCar() {
26+
int input = inputNumberOfCar();
27+
validateInput(input, "μ°¨λŸ‰ 수", CAR_MIN);
28+
return input;
29+
}
30+
public static int inputValidatedNumberOfAttempts() {
31+
int input = inputNumberOfAttempts();
32+
validateInput(input, "μ‹œλ„ 횟수", NUM_MIN);
33+
return input;
34+
}
35+
36+
private static void validateInput(int value, String fieldName, int min) {
37+
if (value <= min) {
38+
throw new IllegalArgumentException(
39+
String.format("[ERROR] %sλŠ” %d보닀 값이어야 ν•©λ‹ˆλ‹€.", fieldName, min)
40+
);
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package racingcar.ui;
2+
3+
import racingcar.domain.Car;
4+
5+
import java.util.List;
6+
7+
public class ResultView {
8+
9+
public static void viewRacingCar(List<Car> cars) {
10+
for(Car car : cars) {
11+
for(int i = 0; i < car.getMoveCount(); i++){
12+
System.out.print("-");
13+
}
14+
System.out.println();
15+
}
16+
System.out.println();
17+
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package racingcar;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
import racingcar.domain.Car;
7+
import racingcar.domain.Cars;
8+
import racingcar.generator.RandomGenerator;
9+
import racingcar.ui.InputView;
10+
11+
import java.io.ByteArrayInputStream;
12+
import java.util.ArrayList;
13+
import java.util.Scanner;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
17+
import static org.junit.jupiter.api.Assertions.assertAll;
18+
19+
public class RacingCarTest {
20+
21+
private static final int CAR = 3;
22+
Car car;
23+
Cars cars;
24+
25+
@BeforeEach
26+
void setUp() {
27+
car = new Car(); // 1둜 μ΄ˆκΈ°ν™”
28+
cars = new Cars(RacingCar.getCars(CAR));
29+
}
30+
31+
@Test
32+
@DisplayName("전진 쑰건에 λ§Œμ‘±ν•˜λ©΄ 전진")
33+
void move(){
34+
car.move(()->true);
35+
assertThat(car.getMoveCount()).isEqualTo(2);
36+
}
37+
38+
@Test
39+
@DisplayName("전진 쑰건에 λ§Œμ‘±ν•˜μ§€ μ•ŠμœΌλ©΄ 멈좀")
40+
void stop(){
41+
car.move(()->false);
42+
assertThat(car.getMoveCount()).isEqualTo(1);
43+
}
44+
45+
@Test
46+
@DisplayName("전진 쑰건에 λ§Œμ‘±ν•˜λ©΄ λͺ¨λ‘ 전진")
47+
void moveAll(){
48+
cars.moveAll(()->true);
49+
50+
assertAll(
51+
() -> assertThat(cars.getCurrentStatus().get(0).getMoveCount()).isEqualTo(2),
52+
() -> assertThat(cars.getCurrentStatus().get(1).getMoveCount()).isEqualTo(2),
53+
() -> assertThat(cars.getCurrentStatus().get(2).getMoveCount()).isEqualTo(2)
54+
);
55+
}
56+
57+
@Test
58+
@DisplayName("전진 쑰건에 λ§Œμ‘±ν•˜μ§€ μ•ŠμœΌλ©΄ λͺ¨λ‘ 멈좀")
59+
void stopAll(){
60+
cars.moveAll(()->false);
61+
62+
assertAll(
63+
() -> assertThat(cars.getCurrentStatus().get(0).getMoveCount()).isEqualTo(1),
64+
() -> assertThat(cars.getCurrentStatus().get(1).getMoveCount()).isEqualTo(1),
65+
() -> assertThat(cars.getCurrentStatus().get(2).getMoveCount()).isEqualTo(1)
66+
);
67+
}
68+
69+
@Test
70+
@DisplayName("random 값은 0κ³Ό 9사이 κ°’")
71+
void random(){
72+
assertThat(RandomGenerator.generate()).isBetween(0, 9);
73+
}
74+
75+
@Test
76+
@DisplayName("μžλ™μ°¨ λŒ€μˆ˜κ°€ 0보닀 μž‘μ„ 경우 였λ₯˜ 리턴")
77+
void inputCar(){
78+
String input = "0";
79+
InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes())));
80+
81+
assertThatThrownBy(InputView::inputValidatedNumberOfCar)
82+
.isInstanceOf(IllegalArgumentException.class);
83+
}
84+
85+
@Test
86+
@DisplayName("μ‹œλ„ν•  νšŒμˆ˜κ°€ 0보닀 μž‘μ„ 경우 였λ₯˜ 리턴")
87+
void inputAttempts(){
88+
String input = "0";
89+
InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes())));
90+
91+
assertThatThrownBy(InputView::inputValidatedNumberOfAttempts)
92+
.isInstanceOf(IllegalArgumentException.class);
93+
}
94+
95+
@Test
96+
@DisplayName("λ°˜ν™˜λœ μžλ™μ°¨ List의 ν¬κΈ°λŠ” μž…λ ₯된 크기와 κ°™λ‹€.")
97+
public void getCars() {
98+
ArrayList<Car> cars = RacingCar.getCars(CAR);
99+
assertThat(cars.size()).isEqualTo(CAR);
100+
}
101+
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# μžλ™μ°¨ κ²½μ£Ό κ²Œμž„
2+
## κΈ°λŠ₯ μš”κ΅¬ 사항
3+
- 주어진 횟수 λ™μ•ˆ nλŒ€μ˜ μžλ™μ°¨λŠ” 전진 λ˜λŠ” 멈좜 수 μžˆλ‹€.
4+
- μ‚¬μš©μžλŠ” λͺ‡ λŒ€μ˜ μžλ™μ°¨λ‘œ λͺ‡ 번의 이동을 ν•  것인지λ₯Ό μž…λ ₯ν•  수 μžˆμ–΄μ•Ό ν•œλ‹€.
5+
- μ „μ§„ν•˜λŠ” 쑰건은 0μ—μ„œ 9 μ‚¬μ΄μ—μ„œ random 값을 κ΅¬ν•œ ν›„ random 값이 4이상일 κ²½μš°μ΄λ‹€.
6+
- μžλ™μ°¨μ˜ μƒνƒœλ₯Ό 화면에 좜λ ₯ν•œλ‹€. μ–΄λŠ μ‹œμ μ— 좜λ ₯ν•  것인지에 λŒ€ν•œ μ œμ•½μ€ μ—†λ‹€.
7+
8+
## κ΅¬ν˜„ κ³„νš
9+
10+
- [x] μ‚¬μš©μž μž…λ ₯ 처리 κΈ°λŠ₯ κ΅¬ν˜„
11+
- [x] μžλ™μ°¨ 이동 둜직 κ΅¬ν˜„
12+
- [x] μžλ™μ°¨ μƒνƒœ 좜λ ₯ κΈ°λŠ₯ κ΅¬ν˜„
13+
14+
## 개발 ν™˜κ²½
15+
16+
- μ–Έμ–΄: Java
17+
- IDE: IntelliJ IDEA
18+
19+
20+
21+
22+

0 commit comments

Comments
Β (0)