Skip to content
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

[2단계 - 자동차 경주 리팩터링] 저문(유정훈) 미션 제출합니다. #587

Merged
merged 9 commits into from
Feb 13, 2023
5 changes: 3 additions & 2 deletions src/main/java/racing/domain/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Car {

private static final int INITIAL_VALUE = 0;
private static final int MOVABLE_VALUE = 4;

private final Name name;
private Position position;
Expand All @@ -12,8 +13,8 @@ public Car(String name) {
this.position = new Position(INITIAL_VALUE);
}

public void move(boolean isMovable) {
if (isMovable) {
public void move(NumberGenerator numberGenerator) {
if (numberGenerator.generate() >= MOVABLE_VALUE) {
this.position = position.increase();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/racing/domain/CarGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public CarGroup(String[] names){
this.cars = setUp(names);
}

public void race(boolean isMovable) {
public void race(NumberGenerator numberGenerator) {
for (Car car : cars) {
car.move(isMovable);
car.move(numberGenerator);
}
}
Comment on lines -23 to 26
Copy link

Choose a reason for hiding this comment

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

NumberGenerator를 구현하여 모든 자동차가 이동했는지를 테스트할 수 있어보입니다.

Copy link

Choose a reason for hiding this comment

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

아래 getCars() 메서드보다 검증 메서드 setUp 메서드가 더 중요해보입니다-!


Expand Down
6 changes: 6 additions & 0 deletions src/main/java/racing/domain/NumberGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package racing.domain;

public interface NumberGenerator {

int generate();
}
6 changes: 1 addition & 5 deletions src/main/java/racing/domain/RacingGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public RacingGame(CarGroup group, RandomNumberGenerator randomNumberGenerator) {

//TODO: 테스트
public void race() {
carGroup.race(isMovable());
carGroup.race(numberGenerator);
}

public RacingResult produceRacingResult() {
Copy link

Choose a reason for hiding this comment

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

테스트할 수 있어보입니다.

Expand All @@ -28,8 +28,4 @@ public RacingResult produceRacingResult() {

return new RacingResult(history);
}

private boolean isMovable() {
return (numberGenerator.generate() >= MOVABLE_CONDITION);
Copy link

Choose a reason for hiding this comment

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

MOVABLE_CONDITION이 사용되지 않는 상수가 되었네요.

}
}
3 changes: 2 additions & 1 deletion src/main/java/racing/domain/RandomNumberGenerator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package racing.domain;

public class RandomNumberGenerator {
public class RandomNumberGenerator implements NumberGenerator {

@Override
public int generate(){
return (int)(Math.random() * 10);
Copy link

Choose a reason for hiding this comment

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

범위 테스트가 가능해보입니다.

}
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/racing/domain/CarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,30 @@ void createCarWhenLengthOfCarNameIsValidate(String input) {
assertDoesNotThrow(() -> new Car(input));
}

@DisplayName("자동차의 move메소드는 true값을 인자로 받으면 전진한다.")
@DisplayName("자동차의 move메소드는 전진하는 경우 위치가 1만큼 증가한다.")
@Test
void moveWhenRandomNumberIsOverThree() {
//given
Car car = new Car("123");
NumberGenerator numberGenerator = new MovableNumberGenerator();

//when
car.move(true);
car.move(numberGenerator);
System.out.println(car.getPosition().getPosition());

//then
assertThat(car.getPosition()).isEqualTo(new Position(1));
}

@DisplayName("자동차의 move메소드는 false값을 인자로 받는 경우 전진하지 않는다.")
@DisplayName("자동차의 move메소드는 전전하지 않는 경우 위치가 유지된다.")
@Test
void moveWhenRandomNumberIsUnderThree() {
//given
Car car = new Car("123");
NumberGenerator numberGenerator = new NonMovableNumberGenerator();

//when
car.move(false);
car.move(numberGenerator);

//then
assertThat(car.getPosition()).isEqualTo(new Position(0));
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/racing/domain/MovableNumberGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package racing.domain;

public class MovableNumberGenerator implements NumberGenerator {

@Override
public int generate() {
return 4;
}
}
9 changes: 9 additions & 0 deletions src/test/java/racing/domain/NonMovableNumberGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package racing.domain;

public class NonMovableNumberGenerator implements NumberGenerator {

@Override
public int generate() {
return 0;
}
}