-
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
5단계 - 자동차 경주(리팩토링) #6079
Merged
Merged
5단계 - 자동차 경주(리팩토링) #6079
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
acc4e9c
feat : (review) inputView 수정
edff5b0
refactor : winner 객체 분리
7fab1b9
refactor : car 객체 수정 및 테스트 코드 추가
7f804e1
refactor : 일급컬렉션 gameround 추가 및 random 필드 제거, 불필요한 함수 제거
5f90c45
refactor : carfactory 함수 추가 및 테스트 코드 추가
8adf052
refactor : main, outputview 변경
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 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 |
---|---|---|
@@ -1,25 +1,23 @@ | ||
package step3; | ||
|
||
|
||
import step3.game.Car; | ||
import step3.game.CarFactory; | ||
import step3.game.GameHistory; | ||
import step3.game.RacingGame; | ||
import step3.game.*; | ||
import step3.random.DefaultRandomStrategy; | ||
import step3.util.InputView; | ||
import step3.util.OutputView; | ||
|
||
import java.util.List; | ||
|
||
public class Main { | ||
private static final String CAR_NAME_MESSAGE = "경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."; | ||
private static final String MOVE_COUNT_MESSAGE = "시도할 회수는 몇 회 인가요?"; | ||
public static void main(String[] args) { | ||
String carNames = InputView.inputString(CAR_NAME_MESSAGE); | ||
int moveCount = InputView.inputInt(MOVE_COUNT_MESSAGE); | ||
String carNames = InputView.inputCarNames(); | ||
int moveCount = InputView.inputMove(); | ||
|
||
List<Car> cars = CarFactory.createCars(carNames); | ||
RacingGame game = new RacingGame(moveCount, new DefaultRandomStrategy(), cars); | ||
List<List<GameHistory>> result = game.start(); | ||
OutputView.printResult(result, game.getCurrentWinners()); | ||
|
||
RacingGame game = new RacingGame(moveCount, cars); | ||
List<GameRound> result = game.start(new DefaultRandomStrategy()); | ||
|
||
OutputView.printResult(result, Winners.findWinners(result)); | ||
} | ||
} |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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,31 @@ | ||
package step3.game; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class GameRound { | ||
private final List<Car> result; | ||
|
||
public GameRound(List<Car> cars) { | ||
this.result = CarFactory.copyCars(cars); | ||
} | ||
|
||
public List<Car> getRound() { | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
GameRound that = (GameRound) o; | ||
|
||
return Objects.equals(result, that.result); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return result.hashCode(); | ||
} | ||
} |
This file contains 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 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,26 @@ | ||
package step3.game; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Winners { | ||
public static List<String> findWinners(List<GameRound> cars) { | ||
List<Car> finalRound = cars.get(cars.size() - 1).getRound(); | ||
return filterWinners(finalRound, findMaxPosition(finalRound)); | ||
} | ||
|
||
private static List<String> filterWinners(List<Car> cars, int maxPosition) { | ||
return cars.stream() | ||
.filter(car -> car.isSamePosition(maxPosition)) | ||
.map(Car::getName) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static int findMaxPosition(List<Car> cars) { | ||
int maxPosition = 0; | ||
for (Car car : cars) { | ||
maxPosition = car.max(maxPosition); | ||
} | ||
return maxPosition; | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,21 +1,22 @@ | ||
package step3.util; | ||
|
||
import step3.game.GameHistory; | ||
import step3.game.Car; | ||
import step3.game.GameRound; | ||
|
||
import java.util.List; | ||
|
||
public class OutputView { | ||
|
||
public static void printResult(List<List<GameHistory>> result, List<String> winners) { | ||
public static void printResult(List<GameRound> result, List<String> winners) { | ||
System.out.println("실행결과"); | ||
printHistories(result); | ||
printWinners(winners); | ||
} | ||
|
||
private static void printHistories(List<List<GameHistory>> result) { | ||
for (List<GameHistory> history : result) { | ||
for (int i = 0; i < history.size(); i++) { | ||
System.out.println(history.get(i).toString()); | ||
private static void printHistories(List<GameRound> result) { | ||
for (GameRound round : result) { | ||
for (Car car : round.getRound()) { | ||
System.out.println(car.getName() + " : " + "-".repeat(car.getPosition())); | ||
Comment on lines
+17
to
+19
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. depth가 2인것 같네요~! |
||
} | ||
System.out.println(); | ||
} | ||
|
This file contains 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 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
Oops, something went wrong.
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.
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.
Winner가 GameRound를 다루는 static한 함수만 가지고 있을게 아니라
일급컬렉션으로 설계해보면 더 좋을것 같습니다. :)