Skip to content

Commit 34cb93b

Browse files
committed
add car example
1 parent 630a332 commit 34cb93b

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/main/java/next/fp/Car.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package next.fp;
2+
3+
import java.util.Objects;
4+
5+
public class Car {
6+
private final String name;
7+
private final int position;
8+
9+
public Car(String name, int position) {
10+
this.name = name;
11+
this.position = position;
12+
}
13+
14+
public Car move(MoveStrategy moveStrategy) {
15+
if (moveStrategy.isMovable()) {
16+
return new Car(name, position + 1);
17+
}
18+
return this;
19+
}
20+
21+
@Override
22+
public boolean equals(Object o) {
23+
if (this == o) return true;
24+
if (o == null || getClass() != o.getClass()) return false;
25+
Car car = (Car) o;
26+
return position == car.position &&
27+
Objects.equals(name, car.name);
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
33+
return Objects.hash(name, position);
34+
}
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package next.fp;
2+
3+
public interface MoveStrategy {
4+
boolean isMovable();
5+
}

src/test/java/next/fp/CarTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package next.fp;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class CarTest {
8+
@Test
9+
public void 이동() {
10+
Car car = new Car("pobi", 0);
11+
Car actual = car.move(new MoveStrategy() {
12+
@Override
13+
public boolean isMovable() {
14+
return true;
15+
}
16+
});
17+
assertEquals(new Car("pobi", 1), actual);
18+
}
19+
20+
@Test
21+
public void 정지() {
22+
Car car = new Car("pobi", 0);
23+
Car actual = car.move(new MoveStrategy() {
24+
@Override
25+
public boolean isMovable() {
26+
return false;
27+
}
28+
});
29+
assertEquals(new Car("pobi", 0), actual);
30+
}
31+
}

0 commit comments

Comments
 (0)