-
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
Open
jonghoonok
wants to merge
9
commits into
next-step:jonghoonok
Choose a base branch
from
jonghoonok:step3
base: jonghoonok
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+272
โ1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca53d35
docs: README ์ถ๊ฐ
f202c0f
feat: ์ฌ์ฉ์๋ก๋ถํฐ ๊ฐ์ ์
๋ ฅ๋ฐ๋๋ค
6f51b32
feat: ์
๋ ฅ๋ฐ์ ๊ฐ์ ์ด์ฉํด ์๋์ฐจ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
96f30e1
feat: ์
๋ ฅ๋ฐ์ ๊ฐ์ ์ด์ฉํด ์คํ๋๋ ๊ฒ์์ ๋ก์ง์ ๊ตฌํํ๋ค.
6bf0ecb
feat: ์๋์ฐจ์ ์ํ๋ฅผ ํ๋ฉด์ ์ถ๋ ฅํ๋ค.
87b2041
refactor: 3๋จ๊ณ ๋ฏธ์
์ฝ๋ ์ ๋ฆฌ
b63b000
refactor: ์ฑ
์ ๋ถ๋ฆฌ
ea62173
refactor: ํ
์คํธ ์์
baa49f6
refactor: ํจํค์ง ๊ตฌ์กฐ ์์ , ์ ๋ต ๋์
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/car_racing/domain/strategy/AlwaysMovingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/car_racing/domain/strategy/CarMovingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package car_racing.domain.strategy; | ||
|
||
public interface CarMovingStrategy { | ||
boolean movable(); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/car_racing/domain/strategy/RandomMovingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/car_racing/view/service/UserInputService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
์ผ๊ธ์ปฌ๋ ์ ๐