File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ //움직일 수 있나? 만 보는 인터페이스
2+ public interface Movable {
3+ boolean canMove ();
4+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments