Skip to content

Commit e2e18ab

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent 3a8cf14 commit e2e18ab

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 자동차 경주
2+
## 1단계 : 움직이는 자동차
3+
요구사항
4+
1. 자동차는 이름을 가지고 있다.
5+
2. 자동차는 움직일 수 있다.
6+
- 0~9 사이의 random 값을 구한다.
7+
- random >= 4 -> 전진, random <= 3 -> 멈춤
8+
3. 테스트
9+
4. Java Style Guide를 기반으로 자바 코드 컨벤션을 지킴
10+
5. indent depth는 2를 넘지 않는다 -> 함수로 분리
11+
6. 3항 연산자, else 예약어 금지
12+
7. 함수의 길이는 15라인을 넘어가지 않는다. -> 한 가지 일만 하도록 구현

src/main/java/Car.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Car {
2+
private static final int MOVE_THRESHOLD = 4;
3+
4+
5+
private String carName;
6+
private int carPosition = 0;
7+
private final MoveCondition moveCondition;
8+
9+
public Car(String carName, MoveCondition moveCondition) {
10+
if (carName == null || carName.trim().isEmpty()) {
11+
throw new IllegalArgumentException("차의 이름이 비어있습니다");
12+
}
13+
this.carName = carName;
14+
this.moveCondition = moveCondition;
15+
}
16+
17+
public int getPosition() {
18+
return carPosition;
19+
}
20+
21+
public String getName() {
22+
return carName;
23+
}
24+
25+
public void move(int value){
26+
if (moveCondition.isMovable(value)) {
27+
carPosition++;
28+
}
29+
}
30+
}
31+

src/main/java/MoveCondition.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface MoveCondition {
2+
boolean isMovable(int value);
3+
}

src/main/java/MoveConditionImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class MoveConditionImpl implements MoveCondition{
2+
private static final int MOVE_THRESHOLD = 4;
3+
4+
@Override
5+
public boolean isMovable(int value) {
6+
return value >= MOVE_THRESHOLD;
7+
}
8+
}

src/main/java/RandomUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class RandomUtil {
2+
private static final int RANDOM_LIMIT = 10;
3+
4+
public static int randomGenerator(){
5+
//0 <= value <= 9
6+
return (int)Math.floor((Math.random() * RANDOM_LIMIT));
7+
}
8+
}

src/test/java/CarTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import org.junit.jupiter.api.DisplayName;
2+
import org.junit.jupiter.api.Test;
3+
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class CarTest {
9+
static MoveCondition moveCondition = new MoveConditionImpl();
10+
11+
@Test
12+
@DisplayName("Car 객체 생성 시 이름이 정상적으로 저장된다")
13+
void carIsCreatedWithCorrectName() {
14+
Car car1 = new Car("A",moveCondition);
15+
Car car2 = new Car("B",moveCondition);
16+
17+
assertThat(car1.getName()).isEqualTo("A");
18+
assertThat(car2.getName()).isEqualTo("B");
19+
}
20+
21+
@Test
22+
@DisplayName("Car 객체 생성 시 이름이 정상적으로 저장된다")
23+
void carIsCreatedWithCorrect() {
24+
Car car1 = new Car("A",moveCondition);
25+
Car car2 = new Car("B",moveCondition);
26+
27+
assertThat(car1.getName()).isEqualTo("A");
28+
assertThat(car2.getName()).isEqualTo("B");
29+
}
30+
31+
@Test
32+
@DisplayName("랜덤값이 4 이상이면 자동차가 전진한다")
33+
void carMovesWhenRandomValueIsGreaterThanOrEqualToThreshold() {
34+
Car car = new Car("A",moveCondition);
35+
car.move(4);
36+
assertThat(car.getPosition()).isEqualTo(1);
37+
}
38+
39+
@Test
40+
@DisplayName("랜덤값이 3 이하면 자동차는 정지한다")
41+
void carDoesNotMoveWhenRandomValueIsLessThanThreshold() {
42+
Car car = new Car("A",moveCondition);
43+
car.move(3);
44+
assertThat(car.getPosition()).isEqualTo(0);
45+
}
46+
47+
@Test
48+
@DisplayName("자동차는 조건을 만족하면 이동을 누적한다")
49+
void carMultipleMove() {
50+
Car car = new Car("A",moveCondition);
51+
52+
car.move(7); // 이동
53+
car.move(2); // 정지
54+
car.move(8); // 이동
55+
56+
assertThat(car.getPosition()).isEqualTo(2);
57+
}
58+
59+
60+
}
61+
62+
63+

src/test/java/RandomUtilTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import org.junit.jupiter.api.DisplayName;
2+
import org.junit.jupiter.api.RepeatedTest;
3+
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
class RandomUtilTest {
8+
@RepeatedTest(1000)
9+
@DisplayName("randomGenerator는 항상 0 이상 9 이하의 값을 반환한다")
10+
void check_randomGenerator_random_number() {
11+
int value = RandomUtil.randomGenerator();
12+
assertThat(value).isGreaterThanOrEqualTo(0).isLessThan(10); // 0 <= value < 10
13+
}
14+
}

0 commit comments

Comments
 (0)