-
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
Step3 구현 #6069
Merged
Merged
Step3 구현 #6069
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3251ff1
docs: update readme
bo-10000 6cef38f
feat: Controller 구현
bo-10000 7956ae6
feat: domain 구현
bo-10000 351ce87
feat: view 구현
bo-10000 b02bfce
refactor: Position 객체화, MOVE_THRS 상수화
bo-10000 8869128
refactor: Cars 일급 컬렉션 객체 생성
bo-10000 b9ec1db
refactor: Position에 validate 추가
bo-10000 f9ca171
refactor: 정의 위치 변경
bo-10000 f1b69c3
refactor: 인터페이스 방식 적용
bo-10000 98fda6f
refactor: add test for Cars
bo-10000 0fb2064
refactor: resultview의 역할 확대
bo-10000 6f367dc
refactor: Cars 생성자 테스트 추가
bo-10000 a59eb0e
refactor: car.move를 racingGame으로 이동
bo-10000 4065de4
refactor: refactor test
bo-10000 bd07d90
refactor: CarsTest 수정
bo-10000 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,41 @@ | ||
package controller; | ||
|
||
import domain.Car; | ||
import domain.Cars; | ||
import domain.RandomNumberGenerator; | ||
import view.InputView; | ||
import view.ResultView; | ||
|
||
public class RacingGame { | ||
private final Cars cars; | ||
private final int numberOfRounds; | ||
private final RandomNumberGenerator randomNumberGenerator = new RandomNumberGenerator(); | ||
|
||
public RacingGame(int numberOfCars, int numberOfRounds) { | ||
this.numberOfRounds = numberOfRounds; | ||
this.cars = new Cars(numberOfCars); | ||
} | ||
|
||
private void runOneRound() { | ||
for (Car car : cars.getCars()) { | ||
car.move(randomNumberGenerator); | ||
ResultView.printPosition(car.getCurrentPosition()); | ||
} | ||
ResultView.printMessage(""); | ||
} | ||
|
||
private void run() { | ||
ResultView.printMessage("실행 결과"); | ||
for (int i = 0; i < this.numberOfRounds; i++) { | ||
runOneRound(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
int numberOfCars = InputView.getNumberOfCars(); | ||
int numberOfRounds = InputView.getNumberOfRounds(); | ||
|
||
RacingGame game = new RacingGame(numberOfCars, numberOfRounds); | ||
game.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,17 @@ | ||
package domain; | ||
|
||
public class Car { | ||
private static final int MOVE_THRS = 4; | ||
private final Position position = new Position(); | ||
|
||
public void move(NumberGenerator numberGenerator) { | ||
final int number = numberGenerator.generate(); | ||
if (number >= MOVE_THRS) { | ||
this.position.increase(); | ||
} | ||
} | ||
|
||
public Position getCurrentPosition() { | ||
return this.position; | ||
} | ||
} |
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,22 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Cars { | ||
private final List<Car> cars = new ArrayList<>(); | ||
|
||
public Cars(int numberOfCars) { | ||
generateCars(numberOfCars); | ||
} | ||
|
||
private void generateCars(int numberOfCars) { | ||
for (int i = 0; i < numberOfCars; i++) { | ||
this.cars.add(new Car()); | ||
} | ||
} | ||
|
||
public List<Car> getCars() { | ||
return this.cars; | ||
} | ||
} |
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,6 @@ | ||
package domain; | ||
|
||
public interface NumberGenerator { | ||
int generate(); | ||
} | ||
|
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,20 @@ | ||
package domain; | ||
|
||
public class Position { | ||
private int value = 0; | ||
|
||
public void increase() { | ||
this.value++; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "-".repeat(this.value); | ||
} | ||
|
||
private void validate() { | ||
if (this.value < 0) { | ||
throw new IllegalArgumentException("Position value must be greater than or equal to 0."); | ||
} | ||
} | ||
} |
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,13 @@ | ||
package domain; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomNumberGenerator implements NumberGenerator { | ||
private static final Random random = new Random(); | ||
private static final int MAX_BOUND = 10; | ||
|
||
@Override | ||
public int generate() { | ||
return random.nextInt(MAX_BOUND); | ||
} | ||
} |
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,20 @@ | ||
package view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
public static int getNumberOfCars() { | ||
return getInput("자동차 대수는 몇 대 인가요?"); | ||
} | ||
|
||
public static int getNumberOfRounds() { | ||
return getInput("시도할 회수는 몇 회 인가요?"); | ||
} | ||
|
||
private static int getInput(String message) { | ||
System.out.println(message); | ||
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,14 @@ | ||
package view; | ||
|
||
import domain.Position; | ||
|
||
public class ResultView { | ||
|
||
public static void printPosition(Position position) { | ||
printMessage(position.toString()); | ||
} | ||
|
||
public static void printMessage(String message) { | ||
System.out.println(message); | ||
} | ||
} |
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 domain; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CarTest { | ||
@DisplayName("숫자가 4 미만이면 움직이지 않는다.") | ||
@Test | ||
void moveTestSmallerThanFour() { | ||
Car car = new Car(); | ||
NumberGenerator numberGenerator = new StaticNumberGenerator(3); | ||
|
||
car.move(numberGenerator); | ||
|
||
assertThat(car.getCurrentPosition()).extracting("value").isEqualTo(0); | ||
} | ||
|
||
@DisplayName("숫자가 4 이상이면 움직인다.") | ||
@Test | ||
void moveTestLargerThanOrEqualToFour() { | ||
Car car = new Car(); | ||
NumberGenerator numberGenerator = new StaticNumberGenerator(5); | ||
|
||
car.move(numberGenerator); | ||
|
||
assertThat(car.getCurrentPosition()).extracting("value").isEqualTo(1); | ||
} | ||
} |
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 domain; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.InstanceOfAssertFactories.list; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CarsTest { | ||
@DisplayName("입력받은 숫자만큼 자동차를 생성한다.") | ||
@Test | ||
void generateCarsTest() { | ||
int numberOfCars = 3; | ||
|
||
Cars cars = new Cars(numberOfCars); | ||
|
||
assertThat(cars.getCars()).asInstanceOf(list(Car.class)).hasSize(numberOfCars); | ||
} | ||
} |
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 domain; | ||
|
||
public class StaticNumberGenerator implements NumberGenerator { | ||
private static int NUMBER; | ||
|
||
public StaticNumberGenerator(int number) { | ||
NUMBER = number; | ||
} | ||
|
||
@Override | ||
public int generate() { | ||
return NUMBER; | ||
} | ||
} |
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.
이 부분에 대한 테스트 코드가 있으면 좋겠네요~
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.
구현했습니다