-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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단계] 자동차 리팩토링 리뷰 부탁듣립니다 #2083
Merged
Merged
[5단계] 자동차 리팩토링 리뷰 부탁듣립니다 #2083
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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 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,36 @@ | ||
package new_racingcar.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class Distance { | ||
private final int distance; | ||
|
||
public Distance() { | ||
this.distance = 0; | ||
} | ||
|
||
private Distance(int distance) { | ||
this.distance = distance; | ||
} | ||
|
||
public Distance addDistance() { | ||
return new Distance(distance + 1); | ||
} | ||
|
||
public int getDistance() { | ||
return this.distance; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Distance distance1 = (Distance) o; | ||
return getDistance() == distance1.getDistance(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getDistance()); | ||
} | ||
} |
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,23 @@ | ||
package new_racingcar.domain; | ||
|
||
import new_racingcar.constant.MsgConstants; | ||
|
||
public class Name { | ||
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 String name; | ||
|
||
public Name(String name) { | ||
isNameValid(name); | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
private void isNameValid(String name) { | ||
if (name.length() > 5 | ||
|| name.trim().isEmpty()) { | ||
throw new IllegalArgumentException(MsgConstants.CAR_NAME_ERROR_MESSAGE); | ||
} | ||
} | ||
} |
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,32 +1,46 @@ | ||
package new_racingcar.domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import new_racingcar.constant.MsgConstants; | ||
|
||
import java.util.Objects; | ||
|
||
public class Round { | ||
private final Cars cars; | ||
private int round; | ||
|
||
public Round(Cars cars) { | ||
this.cars = cars; | ||
public Round(int round) { | ||
isRoundCountVaild(round); | ||
this.round = round; | ||
} | ||
|
||
public List<Car> getRoundInfo() { | ||
return cars.getUnmodifiableCars(); | ||
private void isRoundCountVaild(int roundCount) { | ||
if (roundCount < 1) { | ||
throw new IllegalArgumentException(MsgConstants.ROUND_ERROR_MESSAGE); | ||
} | ||
} | ||
|
||
public int getMaxDistance() { | ||
return getRoundInfo().stream() | ||
.mapToInt(Car::getDistance) | ||
.max() | ||
.getAsInt(); | ||
public Boolean isGameEnd() { | ||
if (round > 0) { | ||
nextRound(); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public String getWinnerNames(int maxDistance) { | ||
List<String> winners = getRoundInfo().stream() | ||
.filter(c -> c.getDistance() == getMaxDistance()) | ||
.map(Car::getName) | ||
.collect(Collectors.toList()); | ||
private void nextRound() { | ||
--round; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Round round1 = (Round) o; | ||
return round == round1.round; | ||
} | ||
|
||
return String.join(",", winners); | ||
@Override | ||
public int hashCode() { | ||
return Objects.hash(round); | ||
} | ||
} |
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,32 @@ | ||
package new_racingcar.domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class RoundRecord { | ||
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 final Cars cars; | ||
|
||
public RoundRecord(Cars cars) { | ||
this.cars = cars; | ||
} | ||
|
||
public List<Car> getRoundInfo() { | ||
return cars.getUnmodifiableCars(); | ||
} | ||
|
||
public int getMaxDistance() { | ||
return getRoundInfo().stream() | ||
.mapToInt(Car::getDistance) | ||
.max() | ||
.getAsInt(); | ||
} | ||
|
||
public String getWinnerNames(int maxDistance) { | ||
List<String> winners = getRoundInfo().stream() | ||
.filter(c -> c.getDistance() == getMaxDistance()) | ||
.map(Car::getName) | ||
.collect(Collectors.toList()); | ||
|
||
return String.join(",", winners); | ||
} | ||
} |
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,38 +1,33 @@ | ||
package new_racingcar.service; | ||
|
||
import new_racingcar.domain.Round; | ||
import new_racingcar.strategy.MoveOneStrategy; | ||
import new_racingcar.constant.MsgConstants; | ||
import new_racingcar.domain.Cars; | ||
import new_racingcar.domain.Round; | ||
import new_racingcar.strategy.MoveZeroStrategy; | ||
import new_racingcar.domain.RoundRecord; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class GrandPrix { | ||
private Cars cars; | ||
private List<Round> rounds = new ArrayList<>(); | ||
private List<RoundRecord> roundRecords = new ArrayList<>(); | ||
|
||
private int roundCount; | ||
private Round round; | ||
|
||
public GrandPrix(List<String> names, int roundCount) { | ||
isRoundCountVaild(roundCount); | ||
this.cars = new Cars(names, new MoveOneStrategy()); | ||
this.roundCount = roundCount; | ||
this.round = new Round(roundCount); | ||
} | ||
|
||
private void isRoundCountVaild(int roundCount) { | ||
if (roundCount < 1) { | ||
throw new IllegalArgumentException(MsgConstants.ROUND_ERROR_MESSAGE); | ||
} | ||
} | ||
|
||
public List<Round> start() { | ||
for(int i = 0; i < roundCount; i++) { | ||
public List<RoundRecord> start() { | ||
while (round.isGameEnd()) { | ||
Cars newCars = cars.run(); | ||
rounds.add(new Round(newCars)); | ||
roundRecords.add(new RoundRecord(newCars)); | ||
} | ||
|
||
return rounds; | ||
return roundRecords; | ||
} | ||
|
||
|
||
} |
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.
원시값 wrapping 👍