-
Notifications
You must be signed in to change notification settings - Fork 1.2k
π 3λ¨κ³ - μλμ°¨ κ²½μ£Ό #5920
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
Changes from 6 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.domain.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,31 @@ | ||
package car_racing.domain; | ||
|
||
import car_racing.domain.model.Cars; | ||
import car_racing.view.CarPrinter; | ||
import car_racing.view.model.UserInput; | ||
|
||
public class CarController { | ||
private final Cars cars; | ||
private final UserInput userInput; | ||
|
||
public CarController(UserInput userInput) { | ||
this.userInput = userInput; | ||
this.cars = new Cars(userInput.getNumOfCar()); | ||
} | ||
|
||
public void race() { | ||
showRaceStart(); | ||
for (int i = 0; i < userInput.getNumOfGame(); i++) { | ||
cars.moveAll(); | ||
showRaceResult(); | ||
} | ||
} | ||
|
||
private void showRaceStart() { | ||
CarPrinter.showRaceStart(); | ||
} | ||
|
||
private void showRaceResult() { | ||
CarPrinter.showRaceResult(cars); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package car_racing.domain; | ||
|
||
import car_racing.view.model.UserInput; | ||
import car_racing.view.UserInputProcessor; | ||
|
||
public class GameController { | ||
private final UserInputProcessor userInputProcessor = new UserInputProcessor(); | ||
|
||
public void run() { | ||
UserInput userInput = userInputProcessor.getUserInput(); | ||
CarController carController = new CarController(userInput); | ||
carController.race(); | ||
} | ||
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. Controllerκ° Controllerλ₯Ό κ°μ§κ³ μλ ννκ° MVCμμ κΈ°λ°λλκ±ΈκΉμ? |
||
} |
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,35 @@ | ||
package car_racing.domain.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.function.Consumer; | ||
|
||
public class 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. μΌκΈμ»¬λ μ π |
||
private static final Random random = new Random(); | ||
private static final int MOVE_THRESHOLD = 4; | ||
private static final int DEFAULT_MOVE_DISTANCE = 1; | ||
|
||
private final List<Car> cars; | ||
|
||
public Cars(int numOfCars) { | ||
this.cars = new ArrayList<>(); | ||
for (int i = 0; i < numOfCars; i++) { | ||
this.cars.add(new Car()); | ||
} | ||
} | ||
|
||
public void moveAll() { | ||
cars.forEach(car -> { | ||
if (isMovable()) car.move(DEFAULT_MOVE_DISTANCE); | ||
}); | ||
} | ||
|
||
public void forEach(Consumer<Car> action) { | ||
cars.forEach(action); | ||
} | ||
|
||
private boolean isMovable() { | ||
return random.nextInt() > MOVE_THRESHOLD; | ||
} | ||
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. μ¬κΈ°μ λλ€μ μ μ΄νλ€λ©΄, Carsμ λν moveAllμ λν ν μ€νΈκ° νλ€κ±° κ°μ΅λλ€. νλ² μλ κΈμ μ°Έκ³ ν΄λ³΄μλ©΄ μ΄λ¨κΉμ? https://tecoble.techcourse.co.kr/post/2020-05-17-appropriate_method_for_test_by_interface/ |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package car_racing.view; | ||
|
||
import car_racing.domain.model.Car; | ||
import car_racing.domain.model.Cars; | ||
|
||
public class CarPrinter { | ||
public static void showRaceStart() { | ||
System.out.println("\nμ€ν κ²°κ³Ό"); | ||
} | ||
|
||
public static void showRaceResult(Cars cars) { | ||
cars.forEach(CarPrinter::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; | ||
|
||
import car_racing.view.model.UserInput; | ||
|
||
import java.util.Scanner; | ||
|
||
public class UserInputProcessor { | ||
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,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,26 @@ | ||
package car_racing; | ||
|
||
import car_racing.domain.model.Car; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
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}") | ||
@ValueSource(ints = {-1, -2, -3}) | ||
void sholdThrowRuntimeExceptionWhenNegativeDistanceIsGiven(int inputDistance) { | ||
Car car = new Car(); | ||
assertThatThrownBy(() -> car.move(inputDistance)) | ||
.isInstanceOf(RuntimeException.class); | ||
} | ||
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. κ²½κ³κ°μ ν
μ€νΈν΄λΌ. λΌλ λ§μ΄ μμ΅λλ€. κ·Έλ λ€λ©΄ μμμ μμμ κ²½κ³μ μ μ΄λμΌκΉμ? 1μ κ²μ¦νλ€μ 2,3μ κ²μ¦νλκ²μ λν΄ ν° μλ―Έκ° μμκΉμ? νλ² κ³ λ―Όν΄λ³΄μλ©΄ μ’μκ±° κ°μ΅λλ€! |
||
} |
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.
MVCν¨ν΄μ κΈ°λ°νμ κ±° κ°μ΅λλ€.
Controllerλ κ³Όμ° μνλ₯Ό κ°μ§κ³ μλκ² λ§μκΉμ?
μ¦, μΈμ€ν΄μ€λ³μλ‘ Carsλ₯Ό κ°μ§κ³ μλκ² λ§μμ§... κ³ λ―Όμ ν΄λ³΄μλ©΄ μ΄λ¨κΉμ?