-
Notifications
You must be signed in to change notification settings - Fork 93
[자동차 경주] 배진호 미션 제출합니다. #77
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f312de5
feat: 1단계 움직이는 자동차 구하기 기능&테스트 추가
BaeJinho4028 0899211
feat: 2단계 우승 자동차 구하기 기능&테스트 추가
BaeJinho4028 88cd6a3
refactor: 2단계 우승 자동차 구하기 수정
BaeJinho4028 2119480
feat: 3단계 게임 실행 구현
BaeJinho4028 8a78346
refactor: 4단계 리팩터링
BaeJinho4028 f2742eb
refactor: 피드백 반영
BaeJinho4028 f4a58a6
chore: 디렉토리 위치 변경
BaeJinho4028 4cd9ce3
refactor: 자동차 생성 팩토리 추가
BaeJinho4028 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 hidden or 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 hidden or 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 @@ | ||
import domain.Game; | ||
import util.RandomNumberProvider; | ||
import view.InputView; | ||
import view.ResultView; | ||
|
||
public class Application { | ||
|
||
public static void main(final String... args) { | ||
final String carNames = InputView.inputCarNames(); | ||
final int tryCount = InputView.inputTryCount(); | ||
|
||
final Game game = new Game(carNames, tryCount, new RandomNumberProvider()); | ||
game.play(); | ||
|
||
ResultView.outputResult(game); | ||
} | ||
} |
This file contains hidden or 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,46 @@ | ||
package domain; | ||
|
||
import exception.CarNameRequiredException; | ||
import exception.CarNameTooLongException; | ||
import util.NumberProvider; | ||
|
||
public class Car { | ||
|
||
private static final int MOVE_CONDITION = 4; | ||
private static final int MAX_NAME_LENGTH = 5; | ||
private final String name; | ||
private int position = 0; | ||
|
||
public Car(String name) { | ||
validateName(name); | ||
this.name = name; | ||
} | ||
|
||
private void validateName(String name) { | ||
if (name == null || name.isEmpty()) { | ||
throw new CarNameRequiredException("자동차 이름은 필수입니다."); | ||
} | ||
if (name.length() > MAX_NAME_LENGTH) { | ||
throw new CarNameTooLongException("자동차 이름은 5자 이하만 가능합니다."); | ||
} | ||
} | ||
|
||
public void move(NumberProvider numberProvider) { | ||
int randomNumber = numberProvider.provide(); | ||
if (isMovable(randomNumber)) { | ||
position++; | ||
} | ||
} | ||
|
||
private boolean isMovable(int randomValue) { | ||
return randomValue >= MOVE_CONDITION; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
} |
This file contains hidden or 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,39 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import util.NumberProvider; | ||
|
||
public class CarGroup { | ||
|
||
private final List<Car> cars; | ||
private final NumberProvider numberProvider; | ||
|
||
public CarGroup(List<Car> cars, NumberProvider numberProvider) { | ||
this.cars = cars; | ||
this.numberProvider = numberProvider; | ||
} | ||
|
||
public void moveCars() { | ||
cars.forEach(car -> car.move(numberProvider)); | ||
} | ||
|
||
public List<Car> getFarthestCars() { | ||
int maxPosition = getMaxPosition(); | ||
return cars.stream() | ||
.filter(car -> car.getPosition() == maxPosition) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private int getMaxPosition() { | ||
return cars.stream() | ||
.mapToInt(Car::getPosition) | ||
.max() | ||
.orElse(0); | ||
} | ||
|
||
public List<Car> getCars() { | ||
return cars; | ||
} | ||
} |
This file contains hidden or 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 domain; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import exception.TryCountOutOfRangeException; | ||
import factory.CarFactory; | ||
import util.NumberProvider; | ||
|
||
public class Game { | ||
|
||
private final CarGroup carGroup; | ||
private final int tryCount; | ||
|
||
public Game(String carNames, int tryCount, NumberProvider numberProvider) { | ||
validateTryCountIsPositive(tryCount); | ||
this.carGroup = new CarGroup(CarFactory.createCars(carNames), numberProvider); | ||
this.tryCount = tryCount; | ||
} | ||
|
||
private void validateTryCountIsPositive(int tryCount) { | ||
if (tryCount < 1) { | ||
throw new TryCountOutOfRangeException("라운드는 1 이상이어야 합니다."); | ||
} | ||
} | ||
|
||
public void play() { | ||
for (int i = 0; i < tryCount; i++) { | ||
carGroup.moveCars(); | ||
} | ||
} | ||
|
||
public List<Car> getWinners() { | ||
return carGroup.getFarthestCars(); | ||
} | ||
|
||
public CarGroup getCarGroup() { | ||
return carGroup; | ||
} | ||
} |
This file contains hidden or 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,8 @@ | ||
package exception; | ||
|
||
public class CarNameRequiredException extends IllegalArgumentException { | ||
|
||
public CarNameRequiredException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains hidden or 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,8 @@ | ||
package exception; | ||
|
||
public class CarNameTooLongException extends IllegalArgumentException { | ||
|
||
public CarNameTooLongException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains hidden or 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,8 @@ | ||
package exception; | ||
|
||
public class TryCountOutOfRangeException extends IllegalArgumentException { | ||
|
||
public TryCountOutOfRangeException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains hidden or 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,15 @@ | ||
package factory; | ||
|
||
import domain.Car; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class CarFactory { | ||
|
||
public static List<Car> createCars(String carNames) { | ||
return Arrays.stream(carNames.split(",")) | ||
.map(Car::new) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains hidden or 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 util; | ||
|
||
public interface NumberProvider { | ||
|
||
int provide(); | ||
} |
This file contains hidden or 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 util; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomNumberProvider implements NumberProvider { | ||
BaeJinho4028 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private static final int BOUND = 10; | ||
private final Random random = new Random(); | ||
|
||
@Override | ||
public int provide() { | ||
return random.nextInt(BOUND); | ||
} | ||
} |
This file contains hidden or 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,18 @@ | ||
package view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
public static String inputCarNames() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public static int inputTryCount() { | ||
System.out.println("시도할 회수는 몇회인가요?"); | ||
return scanner.nextInt(); | ||
} | ||
} |
This file contains hidden or 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,34 @@ | ||
package view; | ||
|
||
import java.util.List; | ||
|
||
import domain.Car; | ||
import domain.Game; | ||
|
||
public class ResultView { | ||
|
||
public static void outputResult(Game game) { | ||
outputCarPosition(game.getCarGroup().getCars()); | ||
outputWinner(game.getWinners()); | ||
} | ||
|
||
private static void outputCarPosition(List<Car> cars) { | ||
System.out.println(); | ||
System.out.println("실행결과"); | ||
for (Car car : cars) { | ||
System.out.printf("%s : %s%n", car.getName(), "-".repeat(car.getPosition())); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
private static void outputWinner(List<Car> winnerCars) { | ||
List<String> winnerNames = getWinnerCarNames(winnerCars); | ||
System.out.printf("%s가 최종 우승했습니다.", String.join(", ", winnerNames)); | ||
} | ||
|
||
private static List<String> getWinnerCarNames(List<Car> winners) { | ||
return winners.stream() | ||
.map(Car::getName) | ||
.toList(); | ||
} | ||
} |
This file contains hidden or 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,39 @@ | ||
package domain; | ||
|
||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import fixture.FixedNumberProvider; | ||
|
||
public class CarGroupTest { | ||
|
||
@Test | ||
@DisplayName("positive - 자동차 그룹이 올바르게 이동한다.") | ||
void moveCars() { | ||
List<Car> cars = List.of(new Car("차"), new Car("차차"), new Car("차차차")); | ||
CarGroup carGroup = new CarGroup(cars, new FixedNumberProvider.Number4Provider()); | ||
carGroup.moveCars(); | ||
assertThat(cars).allMatch(car -> car.getPosition() == 1); | ||
} | ||
|
||
@Test | ||
@DisplayName("positive - 가장 멀리 간 자동차를 반환한다.") | ||
void getFarthestCars() { | ||
Car car1 = new Car("A"); | ||
Car car2 = new Car("B"); | ||
Car car3 = new Car("C"); | ||
car1.move(new FixedNumberProvider.Number4Provider()); // 1칸 이동 | ||
car2.move(new FixedNumberProvider.Number4Provider()); // 1칸 이동 | ||
car3.move(new FixedNumberProvider.Number3Provider()); // 이동 X | ||
|
||
List<Car> cars = List.of(car1, car2, car3); | ||
CarGroup carGroup = new CarGroup(cars, new FixedNumberProvider.Number4Provider()); | ||
List<Car> farthestCars = carGroup.getFarthestCars(); | ||
|
||
assertThat(farthestCars).containsExactlyInAnyOrder(car1, car2); | ||
} | ||
} |
This file contains hidden or 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,35 @@ | ||
package domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import fixture.FixedNumberProvider; | ||
|
||
class CarTest { | ||
|
||
@Test | ||
@DisplayName("positive - 4 이상의 수일 경우 자동차가 움직인다.") | ||
void moveCar() { | ||
Car car = new Car("차"); | ||
car.move(new FixedNumberProvider.Number4Provider()); | ||
assertThat(car.getPosition()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
@DisplayName("positive - 4 보다 작은 수일 경우 자동차가 움직이지 않는다.") | ||
void doNotMoveCar() { | ||
Car car = new Car("차차"); | ||
car.move(new FixedNumberProvider.Number3Provider()); | ||
assertThat(car.getPosition()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
@DisplayName("negative - 이름이 null 혹은 Empty 경우 예외처리") | ||
void nameException() { | ||
assertThrows(IllegalArgumentException.class, () -> new Car(null)); | ||
assertThrows(IllegalArgumentException.class, () -> new Car("")); | ||
} | ||
} |
This file contains hidden or 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 static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import util.RandomNumberProvider; | ||
|
||
class GameTest { | ||
|
||
@Test | ||
@DisplayName("negative - 라운드가 0 이하일 경우 예외 발생") | ||
void tryCountIsPositive() { | ||
String carNames = "차1,차2,차3"; | ||
assertThrows(IllegalArgumentException.class, () -> new Game(carNames, 0, new RandomNumberProvider())); | ||
assertThrows(IllegalArgumentException.class, () -> new Game(carNames, -1, new RandomNumberProvider())); | ||
} | ||
} |
This file contains hidden or 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 fixture; | ||
|
||
import util.NumberProvider; | ||
|
||
public class FixedNumberProvider { | ||
|
||
public static class Number3Provider implements NumberProvider { | ||
|
||
public int provide() { | ||
return 3; | ||
} | ||
} | ||
|
||
public static class Number4Provider implements NumberProvider { | ||
|
||
public int provide() { | ||
return 4; | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.