-
Notifications
You must be signed in to change notification settings - Fork 93
[김민지] 자동차경주 1~2단계 제출합니다 #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public class Car { | ||
|
||
private final String carName; | ||
private int position = 0; | ||
private final RandomMovable randomMovable; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
또한 해당 객체를 사용해서 랜덤하게 앞으로 가거나, 정지하거나를 정하고 있구요! 여기서 그렇다면 |
||
|
||
public Car(String name, RandomMovable randomMovable, int position) { | ||
this.carName = name; | ||
this.randomMovable = randomMovable; | ||
this.position = position; | ||
} | ||
|
||
public void move() { | ||
if (randomMovable.canMove()) { | ||
position++; | ||
} | ||
} | ||
|
||
public String getCarName() { | ||
return carName; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//움직일 수 있나? 만 보는 인터페이스 | ||
@FunctionalInterface | ||
public interface Movable { | ||
boolean canMove(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public class RandomMovable implements Movable { | ||
|
||
private final int moveThreshold; | ||
private final int randomBound; | ||
|
||
//생성자에서 전진 조건을 설정 | ||
public RandomMovable(int moveThreshold, int randomBound) { | ||
this.moveThreshold = moveThreshold; | ||
this.randomBound = randomBound; | ||
} | ||
|
||
@Override | ||
public boolean canMove() { | ||
return (ThreadLocalRandom.current().nextInt(randomBound)) >= moveThreshold; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Winners { | ||
|
||
public static List<String> findWinnersNames(List<Car> cars) { | ||
|
||
int maxPosition = cars.stream() | ||
.mapToInt(Car::getPosition) | ||
.max() | ||
.orElse(0);//값이 비어있을 때는 우승 위치로 0을 사용 | ||
|
||
return cars.stream() | ||
.filter(car -> car.getPosition() == maxPosition) | ||
.map(Car::getCarName) | ||
.collect(Collectors.toList()); | ||
} | ||
Comment on lines
+6
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stream 사용을 잘 해주셨군요! 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class WinnersTest { | ||
|
||
@Test | ||
@DisplayName("우승자(들) 이름을 반환한다") | ||
void returnWinnerNames() { | ||
Car normalCar = new Car("Car A", new RandomMovable(4, 10), 7); | ||
Car electricCar = new Car("Car B", new RandomMovable(5, 10), 7); | ||
Car truck = new Car("Car C", new RandomMovable(6, 10), 2); | ||
Car mini = new Car("Car D", new RandomMovable(3, 10), 7); | ||
|
||
List<Car> cars = List.of(normalCar, electricCar, truck, mini); | ||
assertThat(Winners.findWinnersNames(cars)).containsExactlyInAnyOrder("Car A", "Car B", "Car D"); | ||
} | ||
Comment on lines
+9
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 코드는 주로 "준비, 실행, 검증" 이라는 패턴으로 구성되어 있어요. 이러한 패턴은 지금 테스트 코드에서는 "준비, 실행, 검증" 부분이 명확하게 보이지 않아 가독성이 낮아 보여요. 무조건 이러한 패턴을 따를 필요는 없지만, 지금은 테스트 코드에 익숙하지 않으실 테니 이러한 패턴을 사용해 보시는 것을 추천드려요! |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
생성자에서 이미
position
을 할당해주고 있어서 필드에서 0을 선언하는 것은 제거해도 좋을 것 같아요!