Skip to content

Commit

Permalink
conflict 해결 및 전략 패턴 적용 (#4)
Browse files Browse the repository at this point in the history
* conflict ㅎㅐ결

* RandomMovingStrategy 작성

* stragegy 패턴 적용

* 포맷팅
  • Loading branch information
rkaehdaos authored Apr 17, 2024
1 parent 6ab6e1b commit 3a88c8d
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 165 deletions.
23 changes: 0 additions & 23 deletions src/main/java/step4_winner/Car.java

This file was deleted.

6 changes: 5 additions & 1 deletion src/main/java/step4_winner/CarRacingRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import step4_winner.domain.Car;
import step4_winner.domain.Race;
import step4_winner.strategy.MovingStrategy;
import step4_winner.strategy.RandomMovingStrategy;
import step4_winner.view.InputView;
import step4_winner.view.ResultView;

Expand All @@ -12,6 +14,7 @@
public class CarRacingRunner {
private final InputView inputView;
private final ResultView resultView;
private final MovingStrategy movingStrategy;
private final Race race;
private final int tries;

Expand All @@ -20,6 +23,7 @@ public CarRacingRunner(InputView inputView, ResultView resultView) {
this.resultView = resultView;
this.race = prepareRace();
this.tries = prepareTries();
this.movingStrategy = new RandomMovingStrategy();
}

private Race prepareRace() {
Expand All @@ -37,7 +41,7 @@ private int prepareTries() {
public void runRaces() {
resultView.printResultHeader();
for (int i = 0; i < tries; i++) {
race.runRace();
race.runRace(movingStrategy);
resultView.displayRaceResult(race.getCars());
}
resultView.printFinalWinner(race.getCars());
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/step4_winner/Race.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/step4_winner/domain/Car.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package step4_winner.domain;

import lombok.Getter;
import step4_winner.strategy.MovingStrategy;

@Getter
public class Car {
private static final int MAX_NAME_LENGTH = 5;
private static final int MIN_DISTANCE = 1;
private static final int MOVE_THRESHOLD = 4;

private int distance = MIN_DISTANCE;
private final String name;
Expand All @@ -17,7 +17,7 @@ public Car(String inputName) {
this.name = inputName;
}

public void move(int randomValue) {
if (randomValue >= MOVE_THRESHOLD) distance++;
public void move(MovingStrategy movingStrategy) {
if (movingStrategy.isMove()) distance++;
}
}
10 changes: 5 additions & 5 deletions src/main/java/step4_winner/domain/Race.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@


import lombok.Getter;
import step4_winner.strategy.MovingStrategy;

import java.util.List;
import java.util.Random;

@Getter
public class Race {
private final Random random = new Random();
@Getter private final List<Car> cars;
private final List<Car> cars;

public Race(List<Car> cars) {
if (cars.isEmpty())
throw new IllegalArgumentException("0이상이어야 함");
this.cars = cars;
}

public void runRace() {
cars.forEach(car -> car.move(random.nextInt(10)));
public void runRace(MovingStrategy movingStrategy) {
cars.forEach(car -> car.move(movingStrategy));
}
}
5 changes: 5 additions & 0 deletions src/main/java/step4_winner/strategy/MovingStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package step4_winner.strategy;

public interface MovingStrategy {
boolean isMove();
}
15 changes: 15 additions & 0 deletions src/main/java/step4_winner/strategy/RandomMovingStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package step4_winner.strategy;

import java.util.Random;

public class RandomMovingStrategy implements MovingStrategy {

private final Random random = new Random();
private static final int RANDOM_RANGE = 10;
private static final int MOVE_THRESHOLD = 4;

@Override
public boolean isMove() {
return random.nextInt(RANDOM_RANGE) > MOVE_THRESHOLD;
}
}
49 changes: 0 additions & 49 deletions src/test/java/step4_winner/CarTest.java

This file was deleted.

56 changes: 0 additions & 56 deletions src/test/java/step4_winner/RaceTest.java

This file was deleted.

7 changes: 4 additions & 3 deletions src/test/java/step4_winner/domain/CarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

class CarTest {

Expand All @@ -22,12 +23,12 @@ void CarMoveTest() {
assertThat(car.getName()).isEqualTo("test1");

// WHEN : 안움직임
car.move(3);
car.move(() -> false);
// THEN
assertThat(car.getDistance()).isEqualTo(1);

// WHEN : 움직임
car.move(4);
car.move(() -> true);
// THEN
assertThat(car.getDistance()).isGreaterThan(1);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/step4_winner/domain/RaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import step4_winner.strategy.RandomMovingStrategy;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void RaceRoundTest() {
Race race = new Race(cars);

// WHEN
race.runRace();
race.runRace(new RandomMovingStrategy());

// then
boolean anyCarMoved = cars.stream().anyMatch(car -> car.getDistance() > 1);
Expand Down

0 comments on commit 3a88c8d

Please sign in to comment.