Skip to content
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

Merged
merged 2 commits into from
Mar 23, 2025
Merged

step3 #6072

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/java/Car.java
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);
}
Comment on lines +1 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다양한 생성자를 개방해서 개발편의 제공 👍


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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/Cars.java
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자동차에 대한 일급 컬렉션을 만들어주셨군요! 👍
어떤 장점을 느끼셨을까요?

return this.cars;
}
}
18 changes: 18 additions & 0 deletions src/main/java/CarsSnapShots.java
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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/InputView.java
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
Copy link
Member

Choose a reason for hiding this comment

The 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();
}
}
13 changes: 13 additions & 0 deletions src/main/java/Main.java
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());
}
}
25 changes: 25 additions & 0 deletions src/main/java/RacingManager.java
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
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
7 changes: 7 additions & 0 deletions src/main/java/RandomNumberUtil.java
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);
}
}
22 changes: 22 additions & 0 deletions src/main/java/ResultView.java
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));
}
}
22 changes: 22 additions & 0 deletions src/test/java/CarTest.java
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();
}
}
23 changes: 23 additions & 0 deletions src/test/java/RacingManagerTest.java
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 코드 작성이 훨씬 간결해졌네요! 💯

}
13 changes: 13 additions & 0 deletions src/test/java/RandomNumberUtilTest.java
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);
}
}