-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
๐ 3๋จ๊ณ - ์๋์ฐจ ๊ฒฝ์ฃผ #5920
Changes from all commits
ca53d35
f202c0f
6f51b32
96f30e1
6bf0ecb
87b2041
b63b000
ea62173
baa49f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package car_racing; | ||
|
||
import car_racing.application.GameController; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
GameController gameController = new GameController(); | ||
gameController.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package car_racing.application; | ||
|
||
import car_racing.domain.service.RaceService; | ||
import car_racing.domain.strategy.CarMovingStrategy; | ||
import car_racing.domain.strategy.RandomMovingStrategy; | ||
import car_racing.view.model.UserInput; | ||
import car_racing.view.service.UserInputService; | ||
|
||
public class GameController { | ||
private final UserInputService userInputProcessor = new UserInputService(); | ||
private final RaceService raceService = new RaceService(); | ||
private final CarMovingStrategy carMovingStrategy = new RandomMovingStrategy(); | ||
|
||
public void run() { | ||
UserInput userInput = userInputProcessor.getUserInput(); | ||
raceService.race(userInput, carMovingStrategy); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package car_racing.domain.model; | ||
|
||
public class Car { | ||
private int distance; | ||
|
||
public void move(int distance) { | ||
if (distance < 0) throw new IllegalArgumentException("์๋์ฐจ๋ ์์๋งํผ ์์ง์ผ ์ ์์ต๋๋ค."); | ||
this.distance += distance; | ||
} | ||
|
||
public int getDistance() { | ||
return distance; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package car_racing.domain.model; | ||
|
||
import car_racing.domain.strategy.CarMovingStrategy; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
public class Cars { | ||
private static final int DEFAULT_MOVE_DISTANCE = 1; | ||
|
||
private final List<Car> cars; | ||
private final CarMovingStrategy movingStrategy; | ||
|
||
public Cars(int numOfCars, CarMovingStrategy movingStrategy) { | ||
this.cars = new ArrayList<>(); | ||
this.movingStrategy = movingStrategy; | ||
for (int i = 0; i < numOfCars; i++) { | ||
this.cars.add(new Car()); | ||
} | ||
} | ||
|
||
public void moveAll() { | ||
cars.forEach(car -> { | ||
if (movingStrategy.movable()) car.move(DEFAULT_MOVE_DISTANCE); | ||
}); | ||
} | ||
|
||
public void forEach(Consumer<Car> action) { | ||
cars.forEach(action); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package car_racing.domain.service; | ||
|
||
import car_racing.domain.strategy.CarMovingStrategy; | ||
import car_racing.domain.model.Cars; | ||
import car_racing.view.service.PrintService; | ||
import car_racing.view.model.UserInput; | ||
|
||
public class RaceService { | ||
public void race(UserInput userInput, CarMovingStrategy carMovingStrategy) { | ||
Cars cars = new Cars(userInput.getNumOfCar(), carMovingStrategy); | ||
|
||
showRaceStart(); | ||
for (int i = 0; i < userInput.getNumOfGame(); i++) { | ||
playRound(cars); | ||
} | ||
} | ||
|
||
private void playRound(Cars cars) { | ||
cars.moveAll(); | ||
showRaceResult(cars); | ||
} | ||
|
||
private void showRaceStart() { | ||
PrintService.showRaceStart(); | ||
} | ||
|
||
private void showRaceResult(Cars cars) { | ||
PrintService.showRaceResult(cars); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Print๋ผ๋๊ฒ์ Service๋ผ๊ณ ๋ณด๊ธฐ์ ์ด๋ ต๊ณ View๋ผ๊ณ ๋ด์ผ ํ ๊ฑฐ ๊ฐ๊ธด ํฉ๋๋ค. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package car_racing.domain.strategy; | ||
|
||
public class AlwaysMovingStrategy implements CarMovingStrategy { | ||
@Override | ||
public boolean movable() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package car_racing.domain.strategy; | ||
|
||
public interface CarMovingStrategy { | ||
boolean movable(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package car_racing.domain.strategy; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomMovingStrategy implements CarMovingStrategy{ | ||
private static final Random random = new Random(); | ||
private static final int MOVE_THRESHOLD = 4; | ||
|
||
|
||
@Override | ||
public boolean movable() { | ||
return random.nextInt() > MOVE_THRESHOLD; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package car_racing.view.model; | ||
|
||
public class UserInput { | ||
private final int numOfCar; | ||
private final int numOfGame; | ||
|
||
public UserInput(int numOfCar, int numOfGame) { | ||
this.numOfCar = numOfCar; | ||
this.numOfGame = numOfGame; | ||
} | ||
|
||
public int getNumOfCar() { | ||
return numOfCar; | ||
} | ||
|
||
public int getNumOfGame() { | ||
return numOfGame; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package car_racing.view.service; | ||
|
||
import car_racing.domain.model.Car; | ||
import car_racing.domain.model.Cars; | ||
|
||
public class PrintService { | ||
public static void showRaceStart() { | ||
System.out.println("\n์คํ ๊ฒฐ๊ณผ"); | ||
} | ||
|
||
public static void showRaceResult(Cars cars) { | ||
cars.forEach(PrintService::showEachRaceResult); | ||
System.out.println(); | ||
} | ||
|
||
private static void showEachRaceResult(Car car) { | ||
System.out.println("-".repeat(car.getDistance())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package car_racing.view.service; | ||
|
||
import car_racing.view.model.UserInput; | ||
|
||
import java.util.Scanner; | ||
|
||
public class UserInputService { | ||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
public UserInput getUserInput() { | ||
int numOfCar = getNumOfCar(); | ||
int numOfGame = getNumOfGame(); | ||
return new UserInput(numOfCar, numOfGame); | ||
} | ||
|
||
private int getNumOfCar() { | ||
System.out.println("์๋์ฐจ ๋์๋ ๋ช ๋ ์ธ๊ฐ์?"); | ||
return scanner.nextInt(); | ||
} | ||
|
||
private int getNumOfGame() { | ||
System.out.println("์๋ํ ํ์๋ ๋ช ํ ์ธ๊ฐ์?"); | ||
return scanner.nextInt(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package car_racing; | ||
|
||
import car_racing.domain.model.Car; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
||
class CarTest { | ||
@ParameterizedTest(name = "์๋์ฐจ๋ฅผ ์์ง์ด๋ฉด ์๋์ฐจ๊ฐ ์์ง์ธ ๋งํผ ์์น๊ฐ ๋ณํ๋ค. ์์ง์ธ ๊ฐ: {0}") | ||
@ValueSource(ints = {1, 2, 3}) | ||
void shouldMoveCarByGivenDistance(int inputDistance) { | ||
Car car = new Car(); | ||
car.move(inputDistance); | ||
assertThat(car.getDistance()).isEqualTo(inputDistance); | ||
} | ||
|
||
@ParameterizedTest(name = "์๋์ฐจ๋ฅผ ์์๋งํผ ์์ง์ด๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค. ์์ง์ธ ๊ฐ: {0}") | ||
@CsvSource({ | ||
"-1, true", | ||
"0, false", | ||
"1, false" | ||
}) | ||
void sholdThrowRuntimeExceptionWhenNegativeDistanceIsGiven(int inputDistance, boolean shouldThrow) { | ||
Car car = new Car(); | ||
|
||
if (shouldThrow) { | ||
assertThatThrownBy(() -> car.move(inputDistance)) | ||
.isInstanceOf(RuntimeException.class); | ||
} else { | ||
assertThatCode(() -> car.move(inputDistance)) | ||
.doesNotThrowAnyException(); | ||
} | ||
} | ||
Comment on lines
+21
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ํ
์คํธ์ฝ์ฝ๋์ if๋ฌธ์ด ๋ค์ด๊ฐ๋๊ฑด ์ด์ํ๊ฑฐ๋ผ๊ณ ๋ณผ์ ์์ด์. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package car_racing.domain.model; | ||
|
||
import car_racing.domain.strategy.AlwaysMovingStrategy; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CarsTest { | ||
@Test | ||
@DisplayName("moveAll ํธ์ถ์ ๋ชจ๋ ์ฐจ๋์ด 1๋งํผ ์์ง์ด๋์ง") | ||
void shouldMoveAll() { | ||
Cars cars = new Cars(3, new AlwaysMovingStrategy()); | ||
cars.moveAll(); | ||
|
||
cars.forEach(car -> assertEquals(1, car.getDistance())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ฌ๊ธฐ์ assertJ๋ฅผ ์ฐ๋๊ฒ ์ด๋จ๊น ํฉ๋๋ค. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ผ๊ธ์ปฌ๋ ์ ๐