-
Notifications
You must be signed in to change notification settings - Fork 1.2k
1주차 - Step2 자동차 경주 구현 #319
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 all commits
0a1c04e
1bce695
303459f
29901aa
c2597d3
acd03f3
cef19ce
e4fa12b
808574d
1d55df4
9a8150f
c848ead
c702fcf
9188b75
3f5431f
ff8683b
522d860
e9b8399
e62c955
ea19507
3334a79
7b3fa9e
5cd9174
846a85a
afdd1e6
7928fad
48612a7
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,27 @@ | ||
package step2; | ||
|
||
import step2.domain.CarRace; | ||
import step2.domain.Cars; | ||
import step2.view.InputView; | ||
import step2.view.OutputView; | ||
|
||
public class CarRaceApplication { | ||
|
||
private static CarRace carRace; | ||
|
||
public static void main(String[] args) { | ||
InputView inputView = new InputView(); | ||
final int numberOfCars = inputView.getNumberOfCars(); | ||
final int numberOfTrials = inputView.getNumberOfTrials(); | ||
|
||
carRace = CarRace.raceStart(numberOfCars, numberOfTrials); | ||
|
||
OutputView outputView = new OutputView(); | ||
outputView.showResultTitle(); | ||
for (int i = 0; i < numberOfTrials; i++) { | ||
Cars raceResult = carRace.executeTrials(); | ||
outputView.showResult(raceResult); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package step2.domain; | ||
|
||
public class Car { | ||
private final static int INITIAL_POSITION = 0; | ||
private final static int DISTANCE_PER_MOVE = 1; | ||
private final static int MOVE_CRITERION = 4; | ||
private int position; | ||
|
||
public Car() { | ||
this.position = INITIAL_POSITION; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
|
||
public int move(int randomNumber) { | ||
if (canMove(randomNumber)) { | ||
return position += DISTANCE_PER_MOVE; | ||
} | ||
return position; | ||
} | ||
|
||
private boolean canMove(int randomNumber) { | ||
return randomNumber >= MOVE_CRITERION; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package step2.domain; | ||
|
||
import step2.utils.NumberGenerator; | ||
import step2.utils.RandomNumberGenerator; | ||
|
||
public class CarRace { | ||
|
||
private final static int MINIMUM_NUMBER_CAN_BE_INPUT = 1; | ||
private Cars cars; | ||
private NumberGenerator numberGenerator; | ||
|
||
private CarRace(Cars cars) { | ||
this.cars = cars; | ||
numberGenerator = new RandomNumberGenerator(); | ||
} | ||
|
||
public static CarRace raceStart(int numberOfCars, int numberOfTrials) { | ||
numberOfCarsValidation(numberOfCars); | ||
numberOfTrialsValidation(numberOfTrials); | ||
return new CarRace(Cars.makeCars(numberOfCars)); | ||
} | ||
|
||
public Cars executeTrials() { | ||
return cars.moveCars(numberGenerator); | ||
} | ||
|
||
private static void numberOfCarsValidation(int numberOfCars) { | ||
if (numberOfCars < MINIMUM_NUMBER_CAN_BE_INPUT) { | ||
throw new IllegalArgumentException("최소 1대의 자동차가 필요합니다."); | ||
} | ||
} | ||
|
||
private static void numberOfTrialsValidation(int numberOfTrials) { | ||
if (numberOfTrials < MINIMUM_NUMBER_CAN_BE_INPUT) { | ||
throw new IllegalArgumentException("최소 1번의 시행 횟수가 필요합니다."); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package step2.domain; | ||
|
||
import step2.utils.NumberGenerator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Cars { | ||
|
||
private List<Car> cars; | ||
|
||
private Cars(List<Car> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public List<Car> getCars() { | ||
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. method 순서에 대해서도 한번 고민해보세요 https://stackoverflow.com/questions/4668218/are-there-any-java-method-ordering-conventions |
||
return 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. DTO를 사용하지 않는다면 값을 getter하는 부분에서 read-only 혹은 불변객체로 리턴하는 것이 좋다고 생각합니다. https://www.geeksforgeeks.org/collections-unmodifiablelist-method-in-java-with-examples/ |
||
} | ||
|
||
public static Cars makeCars(int numberOfCars) { | ||
List<Car> cars = new ArrayList<>(); | ||
for (int i = 0; i < numberOfCars; i++) { | ||
cars.add(new Car()); | ||
} | ||
return new Cars(cars); | ||
} | ||
|
||
public Cars moveCars(NumberGenerator numberGenerator) { | ||
for (Car car : cars) { | ||
car.move(numberGenerator.getRandomNumber()); | ||
} | ||
return this; | ||
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. 일급 콜렉션은 불변성을 보장해야 합니다. |
||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package step2.utils; | ||
|
||
public interface NumberGenerator { | ||
|
||
int getRandomNumber(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package step2.utils; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomNumberGenerator implements NumberGenerator { | ||
private final static int LIMIT_OF_RANDOM_NUMBER = 10; | ||
|
||
@Override | ||
public int getRandomNumber() { | ||
Random random = new Random(); | ||
return random.nextInt(LIMIT_OF_RANDOM_NUMBER); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package step2.view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
private final static String ASK_NUMBER_OF_CARS = "자동차 대수는 몇 대 인가요?"; | ||
private final static String ASK_NUMBER_OF_TRIALS = "시도할 횟수는 몇 회 인가요?"; | ||
private Scanner scanner; | ||
|
||
public InputView() { | ||
this.scanner = new Scanner(System.in); | ||
} | ||
|
||
public int getNumberOfCars() { | ||
System.out.println(ASK_NUMBER_OF_CARS); | ||
return scanner.nextInt(); | ||
} | ||
|
||
public int getNumberOfTrials() { | ||
System.out.println(ASK_NUMBER_OF_TRIALS); | ||
final int numberOfTrials = scanner.nextInt(); | ||
scanner.close(); | ||
printEmptyLine(); | ||
return numberOfTrials; | ||
} | ||
|
||
private void printEmptyLine() { | ||
System.out.println(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package step2.view; | ||
|
||
import step2.domain.Car; | ||
import step2.domain.Cars; | ||
|
||
public class OutputView { | ||
private static final String POSITION_OF_CAR = "-"; | ||
|
||
public void showResultTitle() { | ||
System.out.println("실행 결과"); | ||
} | ||
|
||
public void showResult(Cars cars) { | ||
for (Car car : cars.getCars()) { | ||
printCars(car); | ||
} | ||
printEmptyLine(); | ||
} | ||
|
||
private void printCars(Car car) { | ||
for (int i = 0; i < car.getPosition(); i++) { | ||
System.out.print(POSITION_OF_CAR); | ||
} | ||
printEmptyLine(); | ||
} | ||
|
||
private void printEmptyLine() { | ||
System.out.println(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package step2; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class CarRaceTest { | ||
|
||
@Test | ||
void 입력된_값의_유효성검증이_유효하다() { | ||
// TODO | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package step2; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import step2.domain.Car; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CarTest { | ||
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 Car car = new Car(); | ||
|
||
@Test | ||
void 자동차가_생성된다() { | ||
assertThat(car).isNotNull(); | ||
} | ||
|
||
@Test | ||
void 생성된_자동차의_시작포지션은_0이다() { | ||
assertThat(car.getPosition()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
void 입력된_숫자만큼_자동차가_생성된다() { | ||
//TODO | ||
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. |
||
} | ||
|
||
@Test | ||
void 자동차는_랜덤숫자가_4이상인_경우만_이동한다() { | ||
//TODO | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {1, 2, 3, 4, 5}) | ||
void 자동차가_이동기준을_통과한_횟수만큼_이동한다(int numberOfMoves) { | ||
final int aboveMoveCriterionNumber = 5; | ||
for (int i = 0; i < numberOfMoves; i++) { | ||
car.move(aboveMoveCriterionNumber); | ||
} | ||
assertThat(car.getPosition()).isEqualTo(numberOfMoves); | ||
} | ||
} |
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.
정적 팩토리 메서드를 사용하셨네요! 👍