-
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
4단계 - 자동차 경주(우승자) #6054
4단계 - 자동차 경주(우승자) #6054
Changes from all commits
d32eb50
d46dd39
79bbb61
9433ac0
b0e65a4
40b120d
151ed57
b60e8d0
40bf045
3bcc1f0
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 |
---|---|---|
@@ -1,24 +1,30 @@ | ||
package racing; | ||
|
||
import java.util.List; | ||
import racing.domain.Racing; | ||
import racing.view.InputView; | ||
import racing.view.ResultView; | ||
|
||
public class RacingCarApplication { | ||
|
||
public static void main(String[] args) { | ||
int carCount = InputView.getNumberOfCars(); | ||
String carNamesRaw = InputView.getNumberOfCars(); | ||
int roundCount = InputView.getNumberOfRounds(); | ||
|
||
Racing racing = new Racing(carCount); | ||
Racing racing = new Racing(StringToArray(carNamesRaw)); | ||
|
||
System.out.println("실행 결과"); | ||
|
||
for (int i = 0; i < roundCount; i++) { | ||
racing.simulateRace(); | ||
ResultView.printRaceResult(racing.getCars()); | ||
} | ||
} | ||
|
||
List<String> maxPosition = racing.findWinners(); | ||
System.out.println(String.join(", ", maxPosition) + "가 최종 우승했습니다."); | ||
} | ||
|
||
private static String[] StringToArray(String carNamesRaw) { | ||
return carNamesRaw.split(","); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,45 @@ | |
public class Car { | ||
|
||
private static final int MOVE_THRESHOLD = 4; | ||
private static final int MAX_NAME_LENGTH = 5; | ||
private static final int MIN_RANDOM_VALUE = 0; | ||
private static final int MAX_RANDOM_VALUE = 9; | ||
|
||
private int position; | ||
|
||
private String name; | ||
|
||
public Car(String name) { | ||
this(name, 0); | ||
} | ||
|
||
public Car(String name, int position) { | ||
validateName(name); | ||
this.name = name; | ||
this.position = position; | ||
} | ||
|
||
private void validateName(String name) { | ||
if (name.length() > MAX_NAME_LENGTH) { | ||
throw new IllegalArgumentException("자동차 이름은 " + MAX_NAME_LENGTH + "자를 초과할 수 없습니다."); | ||
Comment on lines
+24
to
+26
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 int getPosition() { | ||
return position; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void driveOrStop(int randomNumber) { | ||
if (randomNumber < MIN_RANDOM_VALUE || randomNumber > MAX_RANDOM_VALUE) { | ||
throw new IllegalArgumentException("랜덤 값은 0에서 9 사이여야 합니다."); | ||
} | ||
|
||
if (randomNumber >= MOVE_THRESHOLD) { | ||
position++; | ||
} | ||
} | ||
|
||
|
||
} |
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 |
---|---|---|
|
@@ -2,21 +2,25 @@ | |
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import racing.RandomGenerator; | ||
|
||
public class Racing { | ||
|
||
List<Car> cars = new ArrayList<>(); | ||
private final List<Car> cars = new ArrayList<>(); | ||
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.
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. @jinyoungchoi95 안녕하세요 진영님. 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 Racing(int carCount) { | ||
generateCars(carCount); | ||
public Racing(String[] carNames) { | ||
generateCars(carNames); | ||
} | ||
|
||
private List<Car> generateCars(int carCount) { | ||
for (int i = 0; i < carCount; i++) { | ||
cars.add(new Car()); | ||
} | ||
public Racing(List<Car> cars) { | ||
this.cars.addAll(cars); | ||
} | ||
|
||
private List<Car> generateCars(String[] carNames) { | ||
for (String carName : carNames) { | ||
cars.add(new Car(carName)); | ||
} | ||
return cars; | ||
} | ||
|
||
|
@@ -30,5 +34,24 @@ public void simulateRace() { | |
} | ||
} | ||
|
||
public List<String> findWinners() { | ||
int maxPosition = calculateMaxPosition(); | ||
|
||
|
||
return findMaxPosition(maxPosition); | ||
} | ||
|
||
private int calculateMaxPosition() { | ||
return cars.stream() | ||
.mapToInt(Car::getPosition) | ||
.max() | ||
.getAsInt(); | ||
} | ||
|
||
private List<String> findMaxPosition(int maxPosition) { | ||
return cars.stream() | ||
.filter(car -> car.getPosition() == maxPosition) | ||
.map(Car::getName) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package racing.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import racing.domain.Car; | ||
|
||
public class CarTest { | ||
|
||
Car car = new Car("pobi"); | ||
|
||
@Test | ||
void 자동차_멈춤() { | ||
car.driveOrStop(3); | ||
assertThat(car.getPosition()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
void 자동차_전진() { | ||
car.driveOrStop(4); | ||
assertThat(car.getPosition()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void 랜덤값_0미만_예외_발생() { | ||
assertThatThrownBy(() -> car.driveOrStop(-1)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("랜덤 값은 0에서 9 사이여야 합니다."); | ||
} | ||
|
||
@Test | ||
void 랜덤값_최대초과_예외_발생() { | ||
assertThatThrownBy(() -> car.driveOrStop(10)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("랜덤 값은 0에서 9 사이여야 합니다."); | ||
} | ||
|
||
@Test | ||
void 자동차_이름_5글자_초과() { | ||
assertThatThrownBy(() -> new Car("longname")) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("자동차 이름은 5자를 초과할 수 없습니다."); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package racing.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import racing.domain.Car; | ||
import racing.domain.Racing; | ||
|
||
class RacingTest { | ||
|
||
@Test | ||
void 자동차_객체_생성() { | ||
String[] carNames = {"pobi", "crong", "honux"}; | ||
assertThat(new Racing(carNames).getCars().size()).isEqualTo(3); | ||
|
||
} | ||
|
||
@Test | ||
void 제일_높은_position_가진_자동차_구하기() { | ||
Car pobi = new Car("pobi", 3); | ||
Car jason = new Car("jason", 1); | ||
Car brown = new Car("brown", 2); | ||
|
||
List<Car> cars = List.of(pobi, jason, brown); | ||
|
||
Racing racing = new Racing(cars); | ||
|
||
List<String> winners = racing.findWinners(); | ||
|
||
assertThat(winners).containsExactlyInAnyOrder("pobi"); | ||
} | ||
|
||
} |
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.
name은 변경되지 않으므로 불변하게 수정 가능하겠네요!