-
Notifications
You must be signed in to change notification settings - Fork 142
[그리디] 염지환 자동차 경주 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 11 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,10 @@ | ||
| import controller.RacingController; | ||
| import domain.RandomNumberGenerator; | ||
|
|
||
| public class RacingApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| new RacingController().run(new RandomNumberGenerator()); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package controller; | ||
|
|
||
| import domain.Car; | ||
| import domain.Cars; | ||
| import domain.Racing; | ||
| import domain.NumberGenerator; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| public class RacingController { | ||
|
|
||
| Racing racing; | ||
| Cars carList; | ||
| Cars winnerList; | ||
| int RoundNumber; | ||
|
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. Controller에 변경 가능한 형태의 상태(변수)가 존재하네요!
Cars winners = racing.findWinners();
OutputView.printWinners(winners.getCars());
public void playRace(Cars cars, int roundNumber)이렇게 줄여보면, 지환님이 작성해주셨던 객체의 변수를 왜 인스턴스 변수를 줄이는 방향으로 설계 하는지, 인스턴스 변수는 어떤 의미를 가지는지 한번 고민해보세요!
Author
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 run(NumberGenerator numberGenerator) { | ||
| carList = Cars.create(InputView.getCarNames(), numberGenerator); | ||
| RoundNumber = InputView.getRoundNumber(); | ||
| racing = new Racing(carList); | ||
|
|
||
| OutputView.printResult(); | ||
| playRace(); | ||
|
|
||
| winnerList = racing.findWinners(); | ||
| OutputView.printWinners(winnerList.getCars()); | ||
| } | ||
|
|
||
| public void playRace() { | ||
| for (int i = 0; i < RoundNumber; i++) { | ||
| racing.playRound(); | ||
| OutputView.printProcess(carList.getCars()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package domain; | ||
|
|
||
| public class Car { | ||
|
|
||
| private static final int CRITICAL_NUMBER_TO_MOVE = 4; | ||
|
|
||
| private final String name; | ||
| private int distance; | ||
| private 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 isInPosition(int 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. 값이 같은지를 비교하는 메서드군요. |
||
| 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,49 @@ | ||||||||||||||||||||
| package domain; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||
| import java.util.stream.Collectors; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public class Cars { | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private List<Car> 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. 가능한 곳은 최대한 불변을 유지해주세요.
Suggested change
불변을 유지하려는 이유는 무엇일까요?
Author
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. 불변을 유지해야 하는 이유는 해당 변수가 잘못된 값이나 객체로 재할당되지 않게 하기 위함이라고 생각합니다! |
||||||||||||||||||||
|
|
||||||||||||||||||||
| private Cars(List<Car> cars) { | ||||||||||||||||||||
| this.cars = cars; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public static Cars create(String[] carNames, NumberGenerator numberGenerator) { | ||||||||||||||||||||
| List<Car> newCars = new ArrayList<Car>(); | ||||||||||||||||||||
|
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
|
||||||||||||||||||||
| for (String carName : carNames) { | ||||||||||||||||||||
| newCars.add(new Car(carName, numberGenerator)); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return new Cars(newCars); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| 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 findCarsInPosition(int position) { | ||||||||||||||||||||
| List<Car> carsInPosition = 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. 사용하지 않는 변수는 과감하게 지워두세요!
Suggested change
|
||||||||||||||||||||
| return new Cars( | ||||||||||||||||||||
| cars.stream() | ||||||||||||||||||||
| .filter(car -> car.isInPosition(position)) | ||||||||||||||||||||
| .collect(Collectors.toList()) | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
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. Stream을 잘 활용 하셨네요! 하나의 라인에서 여러 동작이 이루어져서 depth가 깊어졌네요.
Suggested change
이렇게 분리해서 적용하면 좋아보여요! 이름을 주어서 stream 연산이 어떤 역할을 수행 하는지도 표현 할 수 있겠죠? |
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public List<Car> getCars() { | ||||||||||||||||||||
| return cars; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+49
to
+68
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; | ||||||
|
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. 사소하지만, 변수이름에 Collection 구현체 담지 마세요!
Suggested change
|
||||||
|
|
||||||
| public Racing(Cars carList) { | ||||||
| this.carList = carList; | ||||||
| } | ||||||
|
|
||||||
| public void playRound() { | ||||||
| carList.move(); | ||||||
| } | ||||||
|
|
||||||
| public Cars findWinners() { | ||||||
| return carList.findCarsInPosition(carList.getMaxDistance()); | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
| 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); | ||
|
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,17 @@ | ||
| package view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
|
|
||
| private static Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public static String[] getCarNames() { | ||
| return 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,40 @@ | ||||||||||||||||||||||||||||||||||||
| package view; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import domain.Car; | ||||||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public class OutputView { | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static void printResult() { | ||||||||||||||||||||||||||||||||||||
| System.out.println("실행 결과"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static void printProcess(List<Car> cars) { | ||||||||||||||||||||||||||||||||||||
| for (Car car : cars) { | ||||||||||||||||||||||||||||||||||||
| System.out.println(formmatCarInfo(car)); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| System.out.println(); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static String formmatCarInfo(Car car) { | ||||||||||||||||||||||||||||||||||||
| StringBuilder formatted; | ||||||||||||||||||||||||||||||||||||
| formatted = new StringBuilder(car.getName() + " : "); | ||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < car.getDistance(); i++) { | ||||||||||||||||||||||||||||||||||||
| formatted.append("-"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return formatted.toString(); | ||||||||||||||||||||||||||||||||||||
|
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. String의 함수를 잘 활용하면 이렇게도 줄여볼 수 있어요
Suggested change
|
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static void printWinners(List<Car> winners) { | ||||||||||||||||||||||||||||||||||||
| List<String> winnerNames = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||
| for (Car winner : winners) { | ||||||||||||||||||||||||||||||||||||
| winnerNames.add(winner.getName()); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| System.out.println(formattWinnerNames(winnerNames) + "가 최종 우승했습니다."); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public static String formattWinnerNames(List<String> cars) { | ||||||||||||||||||||||||||||||||||||
| return cars.toString().replace("[", "").replace("]", ""); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+41
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| 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 domain.Car; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
|
|
@@ -9,21 +10,21 @@ public class CarTest { | |
|
|
||
| @Test | ||
| void testCarName() { | ||
| Car car = new Car("KIA"); | ||
| Car car = new Car("KIA", new MovableNumberGenerator()); | ||
|
|
||
| assertThat(car.getName()).isEqualTo("KIA"); | ||
| } | ||
|
|
||
| @Test | ||
| void testCarMove() { | ||
| Car car = new Car("TestCar"); | ||
| car.move(MOVE_FORWARD); | ||
| assertThat(car.getDistance()).isEqualTo(1); | ||
|
|
||
| Car car = new Car("TestCar", new MovableNumberGenerator()); | ||
| car.move(); | ||
| } | ||
|
|
||
| @Test | ||
| void testCarNotMove() { | ||
| Car car = new Car("TestCar"); | ||
| car.move(NOT_MOVE - 1); | ||
| assertThat(car.getDistance()).isEqualTo(0); | ||
| Car car = new Car("TestCar", new NotMovableNumberGenerator()); | ||
| car.move(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,26 @@ | ||
|
|
||
| /* | ||
| import domain.Car; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| public class GameTest { | ||
|
|
||
| private static final int MOVE_FORWARD = 4; | ||
| private static final int NOT_MOVE = 3; | ||
|
|
||
| @Test | ||
| public void testGetWinnerList() { | ||
| Game game = new Game(); | ||
| game.carList.add(new Car("A")); | ||
| game.carList.add(new Car("B")); | ||
| game.carList.add(new Car("C")); | ||
| Cars carList = Cars; | ||
| racing.carList.add(new Car("A", new MovableNumberGenerator())); | ||
| racing.carList.add(new Car("B", new NotMovableNumberGenerator())); | ||
| racing.carList.add(new Car("C", new MovableNumberGenerator())); | ||
|
|
||
| game.carList.get(0).move(MOVE_FORWARD); | ||
| game.carList.get(1).move(NOT_MOVE); | ||
| game.carList.get(2).move(MOVE_FORWARD); | ||
| racing.carList.get(0).move(MOVE_FORWARD); | ||
| racing.carList.get(1).move(NOT_MOVE); | ||
| racing.carList.get(2).move(MOVE_FORWARD); | ||
|
|
||
| game.makeWinnerList(); | ||
| racing.makeWinnerList(); | ||
| assertThat(game.winnerList).containsOnly(game.carList.get(0), game.carList.get(2)); | ||
| } | ||
|
|
||
| } | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import domain.NumberGenerator; | ||
|
|
||
| public class MovableNumberGenerator implements NumberGenerator { | ||
|
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 final int MOVABLE_NUMBER = 4; | ||
|
|
||
| @Override | ||
| public int generateNumber() { | ||
| return MOVABLE_NUMBER; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import domain.NumberGenerator; | ||
|
|
||
| public class NotMovableNumberGenerator implements NumberGenerator { | ||
|
|
||
| public static final int NOT_MOVABLE_NUMBER = 3; | ||
|
|
||
| @Override | ||
| public int generateNumber() { | ||
| return NOT_MOVABLE_NUMBER; | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
줄바꿈도 의도가 담길 수 있어요. 필요 없는 줄바꿈이라면, 오해하지 않게 깔끔하게 관리해주세요