-
Notifications
You must be signed in to change notification settings - Fork 97
[그리디] 염지환 자동차 경주 3, 4단계 제출합니다! #114
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 24 commits
b260211
d5a9797
92c56ec
9e69781
d187dc8
15ed321
e38bd09
96b822e
11094f8
7b7c714
a1b7358
833a5a8
46cbdbf
1386774
e6e1089
40fd209
2943098
e9de809
34995d0
759de06
bd2d256
df2090b
30742b5
c916e39
2c48874
0c0ba23
cb2c86a
0050f0f
4995016
f4133eb
eed3c57
1350e36
38976c5
97bb64d
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,12 @@ | ||||||||||||||||||
| import controller.RacingController; | ||||||||||||||||||
| import domain.RandomNumberGenerator; | ||||||||||||||||||
|
|
||||||||||||||||||
| public class RacingApplication { | ||||||||||||||||||
|
|
||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||
|
|
||||||||||||||||||
| new RacingController().run(new RandomNumberGenerator()); | ||||||||||||||||||
|
|
||||||||||||||||||
| } | ||||||||||||||||||
|
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. 줄바꿈도 의도가 담길 수 있어요. 필요 없는 줄바꿈이라면, 오해하지 않게 깔끔하게 관리해주세요
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| } | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package controller; | ||
|
|
||
| import domain.Cars; | ||
| import domain.Racing; | ||
| import domain.NumberGenerator; | ||
| import java.util.List; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| public class RacingController { | ||
|
|
||
| private static final int NAME_LENGTH_LIMIT = 5; | ||
|
|
||
| public void run(NumberGenerator numberGenerator) { | ||
|
|
||
| OutputView.printInputCarNames(); | ||
| List<String> carNames = InputView.getCarNames(); | ||
| checkCarNameLengths(carNames); | ||
| Cars carList = Cars.create(carNames, numberGenerator); | ||
|
|
||
| OutputView.printInputRoundNumber(); | ||
| int roundNumber = InputView.getRoundNumber(); | ||
|
|
||
| Racing racing = new Racing(carList); | ||
|
|
||
| OutputView.printResult(); | ||
| playRace(racing, carList, roundNumber); | ||
|
|
||
| Cars winnerList = racing.findWinners(); | ||
| OutputView.printWinners(winnerList.getCars()); | ||
| } | ||
|
|
||
| public void playRace(Racing racing, Cars carList, int roundNumber) { | ||
| for (int i = 0; i < roundNumber; i++) { | ||
| racing.playRound(); | ||
| OutputView.printProcess(carList.getCars()); | ||
| } | ||
| } | ||
|
|
||
| public void checkCarNameLengths(List<String> names) { | ||
| if (names.stream().anyMatch(name -> name.length() > NAME_LENGTH_LIMIT)) { | ||
| throw new IllegalArgumentException("이름의 길이는 " + NAME_LENGTH_LIMIT + "자 이하여야 합니다."); | ||
| } | ||
| } | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package domain; | ||
|
|
||
| public class Car { | ||
|
|
||
| private static final int CRITICAL_NUMBER_TO_MOVE = 4; | ||
|
|
||
| private final String name; | ||
| private int distance; | ||
| private final NumberGenerator numberGenerator; | ||
|
|
||
| public Car(String name, NumberGenerator numberGenerator) { | ||
| this.name = name; | ||
| this.distance = 0; | ||
| this.numberGenerator = numberGenerator; | ||
| } | ||
|
|
||
| public void move() { | ||
| if (numberGenerator.generateNumber() >= CRITICAL_NUMBER_TO_MOVE) { | ||
| distance++; | ||
| } | ||
| } | ||
|
|
||
| public boolean hasSamePosition(int position) { | ||
| return distance == position; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getDistance() { | ||
| return distance; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Cars { | ||
|
|
||
| private final List<Car> cars; | ||
|
|
||
| private Cars(List<Car> cars) { | ||
| this.cars = cars; | ||
| } | ||
|
|
||
| public static Cars create(List<String> carNames, NumberGenerator numberGenerator) { | ||
| List<Car> newCars = new ArrayList<>(); | ||
| for (String carName : carNames) { | ||
| newCars.add(new Car(carName, numberGenerator)); | ||
| } | ||
| return new Cars(newCars); | ||
| } | ||
|
||
|
|
||
| public static Cars of(List<Car> cars) { | ||
| return new Cars(new ArrayList<>(cars)); | ||
| } | ||
|
||
|
|
||
| public void move() { | ||
| for (Car car : cars) { | ||
| car.move(); | ||
| } | ||
| } | ||
|
|
||
| public int getMaxDistance() { | ||
| int maxDistance = 0; | ||
| for (Car car : cars) { | ||
| maxDistance = Math.max(maxDistance, car.getDistance()); | ||
| } | ||
| return maxDistance; | ||
| } | ||
|
|
||
| public Cars findCarsHasSamePosition(int position) { | ||
| List<Car> carsHasSamePosition = cars.stream() | ||
| .filter(car -> car.hasSamePosition(position)) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return new Cars(carsHasSamePosition); | ||
| } | ||
|
|
||
| public List<Car> getCars() { | ||
| 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. |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package domain; | ||
|
|
||
| public interface NumberGenerator { | ||
|
|
||
| int generateNumber(); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||
| package domain; | ||||||
|
|
||||||
| public class Racing { | ||||||
|
|
||||||
| private final Cars carList; | ||||||
|
||||||
| private final Cars carList; | |
| private final Cars cars; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package domain; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class RandomNumberGenerator implements NumberGenerator { | ||
|
|
||
| @Override | ||
| public int generateNumber() { | ||
| return new Random().nextInt(10); | ||
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package view; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
|
|
||
| private static final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static List<String> getCarNames() { | ||
| return Arrays.asList(scanner.nextLine().split(",")); | ||
| } | ||
|
|
||
| public static int getRoundNumber() { | ||
| return Integer.parseInt(scanner.nextLine()); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||||||||||||||||||||||||||||||
| package view; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import domain.Car; | ||||||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public class OutputView { | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static void printInputCarNames() { | ||||||||||||||||||||||||||||||||||||
| System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static void printInputRoundNumber() { | ||||||||||||||||||||||||||||||||||||
| System.out.println("시도할 회수는 몇회인가요?"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static void printResult() { | ||||||||||||||||||||||||||||||||||||
| System.out.println("실행 결과"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static void printProcess(List<Car> cars) { | ||||||||||||||||||||||||||||||||||||
| for (Car car : cars) { | ||||||||||||||||||||||||||||||||||||
| System.out.println(formatCarInfo(car)); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| System.out.println(); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static String formatCarInfo(Car car) { | ||||||||||||||||||||||||||||||||||||
| StringBuilder formatted; | ||||||||||||||||||||||||||||||||||||
| formatted = new StringBuilder(car.getName() + " : "); | ||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < car.getDistance(); i++) { | ||||||||||||||||||||||||||||||||||||
| formatted.append("-"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return formatted.toString(); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| StringBuilder formatted; | |
| formatted = new StringBuilder(car.getName() + " : "); | |
| for (int i = 0; i < car.getDistance(); i++) { | |
| formatted.append("-"); | |
| } | |
| return formatted.toString(); | |
| return car.getName() + " : " + "-".repeat(car.getDistance()); |
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.
이렇게도 줄여 볼 수 있어요!
stream을 잘 활용하면 굉장히 강력해 보이지 않나요?
| public static void printWinners(List<Car> winners) { | |
| List<String> winnerNames = new ArrayList<>(); | |
| for (Car winner : winners) { | |
| winnerNames.add(winner.getName()); | |
| } | |
| System.out.println(formatWinnerNames(winnerNames) + "가 최종 우승했습니다."); | |
| } | |
| public static String formatWinnerNames(List<String> cars) { | |
| return cars.toString().replace("[", "").replace("]", ""); | |
| } | |
| public static void printWinners(List<Car> winners) { | |
| String winnerNames = winners.stream() | |
| .map(Car::getName) | |
| .collect(Collectors.joining(", ")); | |
| System.out.println(winnerNames + "가 최종 우승했습니다."); | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,42 @@ | ||
| import domain.Car; | ||
| import org.junit.jupiter.api.Test; | ||
|
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. import 다시 돌려주세요...ㅠ |
||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| public class CarTest { | ||
|
|
||
| private static final int MOVE_FORWARD = 4; | ||
| private static final int NOT_MOVE = 3; | ||
|
|
||
| @Test | ||
| void testCarName() { | ||
| Car car = new Car("KIA"); | ||
| assertThat(car.getName()).isEqualTo("KIA"); | ||
| Car car = new Car("TestCar", new MovableNumberGenerator()); | ||
| assertThat(car.getName()).isEqualTo("TestCar"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCarMove() { | ||
| Car car = new Car("TestCar"); | ||
| car.move(MOVE_FORWARD); | ||
| Car car = new Car("TestCar", new MovableNumberGenerator()); | ||
| car.move(); | ||
| assertThat(car.getDistance()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void testCarNotMove() { | ||
| Car car = new Car("TestCar"); | ||
| car.move(NOT_MOVE - 1); | ||
| Car car = new Car("TestCar", new NotMovableNumberGenerator()); | ||
| car.move(); | ||
| assertThat(car.getDistance()).isEqualTo(0); | ||
| } | ||
|
|
||
| @Test | ||
| void testCarHasSamePosition() { | ||
| Car car = new Car("TestCar", new MovableNumberGenerator()); | ||
| car.move(); | ||
| assertThat(car.hasSamePosition(1)).isEqualTo(true); | ||
| } | ||
|
|
||
| @Test | ||
| void testCarIsNotInPosition() { | ||
| Car car = new Car("TestCar", new MovableNumberGenerator()); | ||
| car.move(); | ||
| assertThat(car.hasSamePosition(0)).isEqualTo(false); | ||
| } | ||
|
|
||
| } | ||
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.
추가된 요구사항에 맞게 기능 명세서도 업데이트 하셨네요!
문서화도 관리 대상입니다. 아주 좋은 습관이에요👍👍