diff --git a/src/main/java/study/race/model/Car.java b/src/main/java/study/race/model/Car.java index 845e2f20ba1..82099be5105 100644 --- a/src/main/java/study/race/model/Car.java +++ b/src/main/java/study/race/model/Car.java @@ -2,25 +2,24 @@ public class Car { - private String name; private int currentLocation; - private CarMovingDistance carMovingDistance; + private Name name; + private Location location; public Car(String name) { - this.name = name; - this.carMovingDistance = new CarMovingDistance(); - this.currentLocation = carMovingDistance.initMovingDistance(); + this.name = new Name(name); + this.location = new Location(); } public String getName() { - return this.name; + return this.name.getName(); } - public int getCurrentLocation() { - return this.currentLocation; + public int getLocation() { + return this.location.getLocation(); } - public void move(int movingDistance) { - this.currentLocation = this.carMovingDistance.updateMovingDistance(this.currentLocation, movingDistance); + public void move() { + this.location.updateMovingDistance(); } } diff --git a/src/main/java/study/race/model/CarMovingDistance.java b/src/main/java/study/race/model/CarMovingDistance.java deleted file mode 100644 index 1f78c9de547..00000000000 --- a/src/main/java/study/race/model/CarMovingDistance.java +++ /dev/null @@ -1,23 +0,0 @@ -package study.race.model; - -public class CarMovingDistance { - - private final int STARTING_POINT = 0; - private final int MOVE = 1; - private final int FORWARD_CONDITION_THRESHOLD = 4; - - public int initMovingDistance() { - return STARTING_POINT; - } - - public int updateMovingDistance(int currentDistance, int movingDistance) { - if (checkForwardCondition(movingDistance)) { - return currentDistance + MOVE; - } - return currentDistance; - } - - private boolean checkForwardCondition(int movingDistance) { - return movingDistance >= FORWARD_CONDITION_THRESHOLD; - } -} diff --git a/src/main/java/study/race/model/Cars.java b/src/main/java/study/race/model/Cars.java index 50041826520..b9a1a4ccc18 100644 --- a/src/main/java/study/race/model/Cars.java +++ b/src/main/java/study/race/model/Cars.java @@ -17,8 +17,8 @@ public void addCar(String name) { this.carList.add(new Car(name)); } - public void moveCarAt(int i, int movingDistance) { - this.carList.get(i).move(movingDistance); + public void moveCarAt(int i) { + this.carList.get(i).move(); } public int size() { @@ -34,7 +34,7 @@ public Car get(int i) { } public int getCarDistanceAt(int i) { - return this.carList.get(i).getCurrentLocation(); + return this.carList.get(i).getLocation(); } public Winners getWinners() { @@ -44,7 +44,7 @@ public Winners getWinners() { private int findMaxDistance() { int maxDistance = 0; for (Car car : carList) { - maxDistance = Math.max(maxDistance, car.getCurrentLocation()); + maxDistance = Math.max(maxDistance, car.getLocation()); } return maxDistance; } @@ -52,7 +52,7 @@ private int findMaxDistance() { private Winners findWinners(int maxDistance) { Winners winners = new Winners(maxDistance); for (Car car : carList) { - winners.checkAndAddWinner(car.getName(), car.getCurrentLocation()); + winners.checkAndAddWinner(car.getName(), car.getLocation()); } return winners; } diff --git a/src/main/java/study/race/model/Location.java b/src/main/java/study/race/model/Location.java new file mode 100644 index 00000000000..0fafad6e34b --- /dev/null +++ b/src/main/java/study/race/model/Location.java @@ -0,0 +1,33 @@ +package study.race.model; + +import java.util.Random; + +public class Location { + + private static final int STARTING_POINT = 0; + private static final int FORWARD = 1; + private static final int FORWARD_CONDITION_THRESHOLD = 4; + private static final int RANDOM_LIMIT_NUMBER = 10; + + private int location; + private Random random; + + public Location() { + this.location = STARTING_POINT; + random = new Random(); + } + + public void updateMovingDistance() { + if (checkForwardCondition()) { + this.location += FORWARD; + } + } + + private boolean checkForwardCondition() { + return random.nextInt(RANDOM_LIMIT_NUMBER) >= FORWARD_CONDITION_THRESHOLD; + } + + public int getLocation() { + return this.location; + } +} diff --git a/src/main/java/study/race/model/Name.java b/src/main/java/study/race/model/Name.java new file mode 100644 index 00000000000..91e4c09d066 --- /dev/null +++ b/src/main/java/study/race/model/Name.java @@ -0,0 +1,22 @@ +package study.race.model; + +public class Name { + + private final int CAR_NAME_LENGTH_LIMIT = 5; + + private String name; + + public Name(String name) { + checkOverLength(name); + this.name = name; + } + + private void checkOverLength(String name) { + if (name.length() > CAR_NAME_LENGTH_LIMIT) + throw new IllegalArgumentException("String length exceeds the allowed maximum(5)."); + } + + public String getName() { + return this.name; + } +} diff --git a/src/main/java/study/race/model/Racing.java b/src/main/java/study/race/model/Racing.java index 87a820d6dca..692832d33b2 100644 --- a/src/main/java/study/race/model/Racing.java +++ b/src/main/java/study/race/model/Racing.java @@ -4,8 +4,6 @@ public class Racing { - private static final int RANDOM_LIMIT_NUMBER = 10; - private Random random; private Cars cars; @@ -16,8 +14,7 @@ public Racing(Cars cars) { public void start() { for (int i = 0; i < this.cars.size(); i++) { - int movingDistance = random.nextInt(RANDOM_LIMIT_NUMBER); - this.cars.moveCarAt(i, movingDistance); + this.cars.moveCarAt(i); } } diff --git a/src/main/java/study/race/model/RacingRegister.java b/src/main/java/study/race/model/RacingRegister.java index 34881e889d2..183bdff822f 100644 --- a/src/main/java/study/race/model/RacingRegister.java +++ b/src/main/java/study/race/model/RacingRegister.java @@ -4,25 +4,8 @@ public class RacingRegister { - private final int CAR_NAME_LENGTH_LIMIT = 5; - - private String[] carNameList; - public Cars register(String carName) { - this.carNameList = carName.split(","); - checkCarNameValid(carNameList); - return carRegister(carNameList); - } - - private void checkCarNameValid(String[] names) { - for (String name : names) { - isOverLength(name); - } - } - - private void isOverLength(String name) { - if (name.length() > CAR_NAME_LENGTH_LIMIT) - throw new IllegalArgumentException("String length exceeds the allowed maximum(5)."); + return carRegister(carName.split(",")); } private Cars carRegister(String[] carNameList) { diff --git a/src/test/java/study/CarRacingTest.java b/src/test/java/study/CarRacingTest.java index 56875ac47c9..a44a590c1fc 100644 --- a/src/test/java/study/CarRacingTest.java +++ b/src/test/java/study/CarRacingTest.java @@ -33,26 +33,6 @@ void generateCarsTest() { assertThat(cars.size()).isEqualTo(CAR_NAMES.split(",").length); } - @ParameterizedTest(name = "moving distance: {0}, current location: {1}") - @DisplayName("When moving distance is 4 or more, car moving Test") - @CsvSource({"4, 1", "5, 2", "6, 3", "7, 4", "8, 5", "9, 6"}) - void moveCar(int movingDistance, int expected) { - Car car = moveCars.get(0); - car.move(movingDistance); - int result = car.getCurrentLocation(); - assertThat(result).isEqualTo(expected); - } - - @ParameterizedTest(name = "moving distance: {0}, current location: {1}") - @DisplayName("When moving distance is 3 or less, car moving Test") - @CsvSource({"0, 0", "1, 0", "2, 0", "3, 0"}) - void stopCar(int movingDistance, int expected) { - Car car = stopCars.get(0); - car.move(movingDistance); - int result = car.getCurrentLocation(); - assertThat(result).isEqualTo(expected); - } - @ParameterizedTest() @DisplayName("Generating car as many as car names") @ValueSource(strings = {"car1,car2", "car1,car2,car3", "car1,car2,car3,car4"})