Skip to content

[김민지] 자동차경주 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

Merged
merged 5 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/Car.java
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자에서 이미 position을 할당해주고 있어서 필드에서 0을 선언하는 것은 제거해도 좋을 것 같아요!

private final RandomMovable randomMovable;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CarRandomMovable를 필드로 가지고 있네요!

또한 해당 객체를 사용해서 랜덤하게 앞으로 가거나, 정지하거나를 정하고 있구요!

여기서 RandomMovable 클래스는 Movable 인터페이스를 구현하도록 만들어주셨어요.

그렇다면 Car 클래스는 RandomMovable 말고 다른 타입을 가져도 되지 않을까요?


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;
}

}
6 changes: 6 additions & 0 deletions src/main/java/Movable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//움직일 수 있나? 만 보는 인터페이스
@FunctionalInterface
public interface Movable {
boolean canMove();
}

18 changes: 18 additions & 0 deletions src/main/java/RandomMovable.java
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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/Winners.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream 사용을 잘 해주셨군요! 👍

}
20 changes: 20 additions & 0 deletions src/test/java/WinnersTest.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 코드는 주로 "준비, 실행, 검증" 이라는 패턴으로 구성되어 있어요.

이러한 패턴은 Given-When-Then 패턴으로 더 널리 알려져 있고요.

지금 테스트 코드에서는 "준비, 실행, 검증" 부분이 명확하게 보이지 않아 가독성이 낮아 보여요.

무조건 이러한 패턴을 따를 필요는 없지만, 지금은 테스트 코드에 익숙하지 않으실 테니 이러한 패턴을 사용해 보시는 것을 추천드려요!

}