Skip to content

Commit 2d15532

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent 3a8cf14 commit 2d15532

17 files changed

+370
-0
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ dependencies {
1414
testImplementation platform('org.assertj:assertj-bom:3.25.1')
1515
testImplementation('org.junit.jupiter:junit-jupiter')
1616
testImplementation('org.assertj:assertj-core')
17+
18+
testImplementation ('org.mockito:mockito-core:5.2.0')
19+
testImplementation ('org.mockito:mockito-junit-jupiter:5.2.0')
1720
}
1821

1922
test {

src/main/java/Application.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import domain.Game;
2+
import util.RandomNumberProvider;
3+
import view.InputView;
4+
import view.ResultView;
5+
6+
public class Application {
7+
8+
public static void main(final String... args) {
9+
final String carNames = InputView.inputCarNames();
10+
final int tryCount = InputView.inputTryCount();
11+
12+
final Game game = new Game(carNames, tryCount, new RandomNumberProvider());
13+
game.play();
14+
15+
ResultView.outputResult(game);
16+
}
17+
}

src/main/java/domain/Car.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package domain;
2+
3+
import exception.CarNameRequiredException;
4+
import exception.CarNameTooLongException;
5+
import util.NumberProvider;
6+
7+
public class Car {
8+
9+
private static final int MOVE_CONDITION = 4;
10+
private static final int MAX_NAME_LENGTH = 5;
11+
private final String name;
12+
private int position = 0;
13+
14+
public Car(String name) {
15+
validateName(name);
16+
this.name = name;
17+
}
18+
19+
private void validateName(String name) {
20+
if (name == null || name.isEmpty()) {
21+
throw new CarNameRequiredException("자동차 이름은 필수입니다.");
22+
}
23+
if (name.length() > MAX_NAME_LENGTH) {
24+
throw new CarNameTooLongException("자동차 이름은 5자 이하만 가능합니다.");
25+
}
26+
}
27+
28+
public void move(NumberProvider numberProvider) {
29+
int randomNumber = numberProvider.provide();
30+
if (isMovable(randomNumber)) {
31+
position++;
32+
}
33+
}
34+
35+
private boolean isMovable(int randomValue) {
36+
return randomValue >= MOVE_CONDITION;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public int getPosition() {
44+
return position;
45+
}
46+
}

src/main/java/domain/CarGroup.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package domain;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
import util.NumberProvider;
7+
8+
public class CarGroup {
9+
10+
private final List<Car> cars;
11+
private final NumberProvider numberProvider;
12+
13+
public CarGroup(List<Car> cars, NumberProvider numberProvider) {
14+
this.cars = cars;
15+
this.numberProvider = numberProvider;
16+
}
17+
18+
public void moveCars() {
19+
cars.forEach(car -> car.move(numberProvider));
20+
}
21+
22+
public List<Car> getFarthestCars() {
23+
int maxPosition = getMaxPosition();
24+
return cars.stream()
25+
.filter(car -> car.getPosition() == maxPosition)
26+
.collect(Collectors.toList());
27+
}
28+
29+
private int getMaxPosition() {
30+
return cars.stream()
31+
.mapToInt(Car::getPosition)
32+
.max()
33+
.orElse(0);
34+
}
35+
36+
public List<Car> getCars() {
37+
return cars;
38+
}
39+
}

src/main/java/domain/Game.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package domain;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import exception.TryCountOutOfRangeException;
8+
import factory.CarFactory;
9+
import util.NumberProvider;
10+
11+
public class Game {
12+
13+
private final CarGroup carGroup;
14+
private final int tryCount;
15+
16+
public Game(String carNames, int tryCount, NumberProvider numberProvider) {
17+
validateTryCountIsPositive(tryCount);
18+
this.carGroup = new CarGroup(CarFactory.createCars(carNames), numberProvider);
19+
this.tryCount = tryCount;
20+
}
21+
22+
private void validateTryCountIsPositive(int tryCount) {
23+
if (tryCount < 1) {
24+
throw new TryCountOutOfRangeException("라운드는 1 이상이어야 합니다.");
25+
}
26+
}
27+
28+
public void play() {
29+
for (int i = 0; i < tryCount; i++) {
30+
carGroup.moveCars();
31+
}
32+
}
33+
34+
public List<Car> getWinners() {
35+
return carGroup.getFarthestCars();
36+
}
37+
38+
public CarGroup getCarGroup() {
39+
return carGroup;
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package exception;
2+
3+
public class CarNameRequiredException extends IllegalArgumentException {
4+
5+
public CarNameRequiredException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package exception;
2+
3+
public class CarNameTooLongException extends IllegalArgumentException {
4+
5+
public CarNameTooLongException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package exception;
2+
3+
public class TryCountOutOfRangeException extends IllegalArgumentException {
4+
5+
public TryCountOutOfRangeException(String message) {
6+
super(message);
7+
}
8+
}

src/main/java/factory/CarFactory.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package factory;
2+
3+
import domain.Car;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
public class CarFactory {
9+
10+
public static List<Car> createCars(String carNames) {
11+
return Arrays.stream(carNames.split(","))
12+
.map(Car::new)
13+
.collect(Collectors.toList());
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package util;
2+
3+
public interface NumberProvider {
4+
5+
int provide();
6+
}

0 commit comments

Comments
 (0)