-
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 #6072
step3 #6072
Changes from all commits
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,30 @@ | ||
public class Car { | ||
private static final int STANDARD_VALUE = 4; | ||
private int position; | ||
|
||
public Car(int position) { | ||
this.position = position; | ||
} | ||
|
||
public Car() { | ||
this(0); | ||
} | ||
|
||
public Car(Car car) { | ||
this.position = car.position; | ||
} | ||
|
||
public void moveBy(int value) { | ||
if (value > STANDARD_VALUE) { | ||
this.position++; | ||
} | ||
} | ||
|
||
public boolean isPosition(int value) { | ||
return this.position == value; | ||
} | ||
|
||
public int getPosition() { | ||
return this.position; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class Cars { | ||
private final List<Car> cars; | ||
|
||
public Cars(int carNum) { | ||
this.cars = Stream.generate(Car::new).limit(carNum).collect(Collectors.toList()); | ||
} | ||
|
||
public Cars(Cars cars) { | ||
this.cars = cars.cars.stream().map(Car::new).collect(Collectors.toList()); | ||
} | ||
|
||
public void move() { | ||
this.cars.forEach(car -> car.moveBy(RandomNumberUtil.getRandomNumberFromZeroToNine())); | ||
} | ||
|
||
public List<Car> getCars() { | ||
Comment on lines
+5
to
+20
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. 자동차에 대한 일급 컬렉션을 만들어주셨군요! 👍 |
||
return this.cars; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CarsSnapShots { | ||
private final List<Cars> carsSnapShots; | ||
|
||
public CarsSnapShots() { | ||
this.carsSnapShots = new ArrayList<>(); | ||
} | ||
|
||
public void add(Cars cars) { | ||
this.carsSnapShots.add(new Cars(cars)); | ||
} | ||
|
||
public List<Cars> getCars() { | ||
return this.carsSnapShots; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Scanner; | ||
|
||
public final class InputView { | ||
|
||
private InputView() {} | ||
Comment on lines
+3
to
+5
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. 불필요한 생성자를 막아주셨군요 👍 |
||
|
||
public static int inputCarNum() { | ||
final Scanner scanner = new Scanner(System.in); | ||
System.out.println("자동차 대수는 몇 대 인가요?"); | ||
return scanner.nextInt(); | ||
} | ||
|
||
public static int inputTryNum() { | ||
final Scanner scanner = new Scanner(System.in); | ||
System.out.println("시도할 회수는 몇 회 인가요?"); | ||
return scanner.nextInt(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class Main { | ||
public static void main(String[] args) { | ||
final int carNum = InputView.inputCarNum(); | ||
final int tryNum = InputView.inputTryNum(); | ||
|
||
final RacingManager racingManager = new RacingManager(carNum, tryNum); | ||
|
||
racingManager.play(); | ||
|
||
ResultView.printTitle(); | ||
ResultView.printResult(racingManager.getCarsSnapShots()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
public class RacingManager { | ||
private final Cars cars; | ||
private final CarsSnapShots carsSnapShots; | ||
private final int tryNum; | ||
|
||
public RacingManager(int carNum, int tryNum) { | ||
if (carNum <= 0 || tryNum <= 0) { | ||
throw new IllegalArgumentException("자동차 대수와 시도 횟수는 0보다 커야 합니다."); | ||
} | ||
this.tryNum = tryNum; | ||
this.cars = new Cars(carNum); | ||
carsSnapShots = new CarsSnapShots(); | ||
Comment on lines
+6
to
+12
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. 😉 👍 |
||
} | ||
|
||
public void play() { | ||
for (int i = 0; i < tryNum; i++) { | ||
this.cars.move(); | ||
carsSnapShots.add(this.cars); | ||
} | ||
} | ||
|
||
public CarsSnapShots getCarsSnapShots() { | ||
return this.carsSnapShots; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public final class RandomNumberUtil { | ||
private RandomNumberUtil() {} | ||
|
||
public static int getRandomNumberFromZeroToNine() { | ||
return (int) (Math.random() * 10); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public final class ResultView { | ||
|
||
private ResultView() {} | ||
|
||
public static void printResult(final CarsSnapShots carsSnapShots) { | ||
carsSnapShots.getCars().forEach(cars -> { | ||
cars.getCars().forEach(car -> { | ||
printCarPosition(car.getPosition()); | ||
System.out.println(); | ||
}); | ||
System.out.println(); | ||
}); | ||
} | ||
|
||
public static void printTitle() { | ||
System.out.println("실행 결과"); | ||
} | ||
|
||
private static void printCarPosition(int position) { | ||
System.out.print("-".repeat(position)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CarTest { | ||
@Test | ||
@DisplayName("기준값 보다 크면 1칸 이동한다") | ||
void moveBy_go() { | ||
final Car car = new Car(); | ||
car.moveBy(5); | ||
assertThat(car.isPosition(1)).isTrue(); | ||
} | ||
|
||
@Test | ||
@DisplayName("기준값 보다 작으면 이동하지 않는다") | ||
void moveBy_wait() { | ||
final Car car = new Car(); | ||
car.moveBy(4); | ||
assertThat(car.isPosition(0)).isTrue(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class RacingManagerTest { | ||
|
||
@Test | ||
@DisplayName("자동차 개수와 시도 횟수가 0보다 작을 때 예외를 던진다.") | ||
void car() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
new RacingManager(0, 5); | ||
}); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> { | ||
new RacingManager(3, 0); | ||
}); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> { | ||
new RacingManager(0, 0); | ||
}); | ||
} | ||
Comment on lines
+8
to
+22
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,13 @@ | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class RandomNumberUtilTest { | ||
|
||
@Test | ||
@DisplayName("0부터 9까지의 랜덤 숫자를 반환한다.") | ||
void getRandomNumberFromZeroToNine() { | ||
assertThat(RandomNumberUtil.getRandomNumberFromZeroToNine()).isBetween(0, 9); | ||
} | ||
} |
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.
다양한 생성자를 개방해서 개발편의 제공 👍