Skip to content

Commit 5fb3e76

Browse files
committed
1단계 움직이는 자동차
1 parent 3a8cf14 commit 5fb3e76

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/main/java/Car.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Car {
2+
private final String carName;
3+
private int position = 0;
4+
5+
public Car(String name){
6+
this.carName = name;
7+
}
8+
9+
public void move(Movable movable){
10+
if(movable.canMove()){
11+
position++;
12+
}
13+
}
14+
15+
public String getCarName(){
16+
return carName;
17+
}
18+
19+
public int getPosition() {
20+
return position;
21+
}
22+
23+
public String getPositionAsString(){
24+
return "-".repeat(position);
25+
}
26+
}

src/main/java/Movable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//움직일 수 있나? 만 보는 인터페이스
2+
public interface Movable {
3+
boolean canMove();
4+
}

src/main/java/RandomMove.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class RandomMove implements Movable {
2+
//값의 의미나 목적을 코드에서 명확하게 알리고 가독성을 위한 상수 사용
3+
//4는 전진 조건, 10은 랜덤 범위를 의미. 추후 변경 시 상수만 수정하면 됨
4+
private static final int MOVE_THRESHOLD = 4;
5+
private static final int RANDOM_BOUND = 10;
6+
7+
@Override
8+
public boolean canMove(){
9+
//nextInt(10)은 0~9까지의 랜덤한 값을 반환. 그 값이 4보다 크거나 같으면 true 반환
10+
return (int)(Math.random() * RANDOM_BOUND) >= MOVE_THRESHOLD;
11+
}
12+
}

0 commit comments

Comments
 (0)