Skip to content

Commit c0c9b38

Browse files
committed
refactor: Game, CarRegistry 기능 분리
1 parent 1860259 commit c0c9b38

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
lines changed

src/main/java/io/suhan/racingcar/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void main(String[] args) {
1313
int count = InputView.getTrialsCount();
1414

1515
Game game = Game.of(count);
16-
game.registerCars(names);
16+
game.getCarManager().registerCars(names);
1717

1818
List<RoundResult> results = game.execute();
1919
List<Car> winners = game.getWinners();

src/main/java/io/suhan/racingcar/domain/CarRegistry.java renamed to src/main/java/io/suhan/racingcar/domain/CarManager.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
public class CarRegistry {
6+
public class CarManager {
77
private final List<Car> cars;
88

9-
private CarRegistry() {
9+
private CarManager() {
1010
this.cars = new ArrayList<>();
1111
}
1212

13-
public static CarRegistry of() {
14-
return new CarRegistry();
13+
public static CarManager of() {
14+
return new CarManager();
1515
}
1616

1717
public void register(Car car) {
@@ -28,6 +28,22 @@ public void moveCars() {
2828
}
2929
}
3030

31+
public void registerCars(List<String> names) {
32+
for (String name : names) {
33+
Car car = Car.of(name);
34+
cars.add(car);
35+
}
36+
}
37+
38+
public List<Car> getCarsWithBestPosition() {
39+
int bestPosition = getBestPosition();
40+
41+
return getRegisteredCars()
42+
.stream()
43+
.filter((car) -> car.getPosition() == bestPosition)
44+
.toList();
45+
}
46+
3147
public int getBestPosition() {
3248
return cars
3349
.stream()

src/main/java/io/suhan/racingcar/domain/Game.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
public class Game {
77
private static final int GAME_ROUNDS_MINIMUM = 1;
88

9-
private final CarRegistry carRegistry;
9+
private final CarManager carManager;
1010
private final int rounds;
1111

1212
private Game(int rounds) {
13-
this.carRegistry = CarRegistry.of();
13+
this.carManager = CarManager.of();
1414
this.rounds = rounds;
1515
}
1616

@@ -20,7 +20,7 @@ public static Game of() {
2020

2121
public static Game of(int rounds) {
2222
if (rounds < GAME_ROUNDS_MINIMUM) {
23-
throw new IllegalArgumentException("시도 회수는 " + GAME_ROUNDS_MINIMUM + " 이상이어야 합니다.");
23+
throw new IllegalArgumentException("시도 횟수는 " + GAME_ROUNDS_MINIMUM + " 이상이어야 합니다.");
2424
}
2525

2626
return new Game(rounds);
@@ -30,30 +30,18 @@ public List<RoundResult> execute() {
3030
List<RoundResult> results = new ArrayList<>();
3131

3232
for (int i = 0; i < rounds; i++) {
33-
carRegistry.moveCars();
34-
results.add(RoundResult.of(carRegistry.getRegisteredCars()));
33+
carManager.moveCars();
34+
results.add(RoundResult.of(carManager.getRegisteredCars()));
3535
}
3636

3737
return results;
3838
}
3939

4040
public List<Car> getWinners() {
41-
int bestPosition = carRegistry.getBestPosition();
42-
43-
return carRegistry.getRegisteredCars()
44-
.stream()
45-
.filter((car) -> car.getPosition() == bestPosition)
46-
.toList();
47-
}
48-
49-
public CarRegistry getCarRegistry() {
50-
return carRegistry;
41+
return carManager.getCarsWithBestPosition();
5142
}
5243

53-
public void registerCars(List<String> names) {
54-
for (String name : names) {
55-
Car car = Car.of(name);
56-
carRegistry.register(car);
57-
}
44+
public CarManager getCarManager() {
45+
return carManager;
5846
}
5947
}

src/test/java/io/suhan/racingcar/domain/GameTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ public class GameTest {
1818

1919
Game game = Game.of();
2020

21-
for (String name : names) {
22-
Car car = Car.of(name);
21+
game.getCarManager().registerCars(names);
2322

24-
game.getCarRegistry().register(car);
25-
}
26-
27-
List<String> registeredNames = game.getCarRegistry().getRegisteredCars().stream().map(Car::getName).toList();
23+
List<String> registeredNames = game.getCarManager().getRegisteredCars().stream().map(Car::getName).toList();
2824

2925
assertIterableEquals(names, registeredNames);
3026
}
@@ -41,9 +37,9 @@ public class GameTest {
4137
Car brie = Car.of("brie", stopGenerator);
4238
Car brown = Car.of("brown", forwardGenerator);
4339

44-
game.getCarRegistry().register(neo);
45-
game.getCarRegistry().register(brie);
46-
game.getCarRegistry().register(brown);
40+
game.getCarManager().register(neo);
41+
game.getCarManager().register(brie);
42+
game.getCarManager().register(brown);
4743

4844
List<String> expectedNames = List.of("neo", "brown");
4945

0 commit comments

Comments
 (0)