-
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
Step4 - 자동차 경주(우승자 출력) #6047
Merged
+413
−128
Merged
Step4 - 자동차 경주(우승자 출력) #6047
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,29 @@ | ||
package racing; | ||
|
||
import racing.domain.Car; | ||
import racing.domain.Judgement; | ||
import racing.domain.RacingGame; | ||
import racing.views.InputView; | ||
import racing.views.ResultView; | ||
|
||
import java.util.List; | ||
|
||
public class RacingMain { | ||
|
||
public static void main(String[] args) { | ||
|
||
String[] carNames = InputView.inputCarNames(); | ||
|
||
RacingGame racingGame = new RacingGame(carNames); | ||
|
||
int tryTimes = InputView.inputTryTimes(); | ||
|
||
ResultView.showResult(); | ||
|
||
for (int j = 0; j < tryTimes; j++) { | ||
racingGame.moveCars(); | ||
} | ||
|
||
ResultView.printWinner(new Judgement(racingGame.getCars()).getWinnerCars()); | ||
} | ||
} |
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,13 @@ | ||
package racing.data; | ||
|
||
public class Messages { | ||
|
||
public static final String ERROR_VALID_TYPE = "유효한 타입이 아닙니다."; | ||
public static final String ERROR_CAR_NAME_LENGTH = " 자동차 이름은 5자를 초과할 수 없습니다."; | ||
public static final String ASK_CAR_NAMES = "경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."; | ||
public static final String ASK_CAR_COUNT = "자동차 대수는 몇 대 인가요?"; | ||
public static final String ASK_TRY_TIMES = "시도할 회수는 몇 회 인가요?"; | ||
public static final String RACE_RESULT = "실행 결과"; | ||
public static final String RACE_WINNER = "가 최종 우승 했습니다."; | ||
public static final String INVALID_NUMBER_RANGE = "음수가 될 수 없습니다."; | ||
} |
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,53 @@ | ||
package racing.domain; | ||
|
||
import racing.data.Messages; | ||
|
||
public class Car { | ||
|
||
public static final int MOVE_THRESHOLD = 4; | ||
public static final int CAR_NAME_THRESHOLD = 5; | ||
|
||
private final Position position; | ||
private String carName; | ||
|
||
public Car() { | ||
this.position = new Position(0); | ||
} | ||
|
||
public Car(String carName, int position) { | ||
this.carName = carName; | ||
this.position = new Position(position); | ||
} | ||
|
||
public Car(String carName) { | ||
if (carName == null || carName.isBlank() || carName.length() > CAR_NAME_THRESHOLD) { | ||
throw new IllegalArgumentException(Messages.ERROR_CAR_NAME_LENGTH); | ||
} | ||
this.position = new Position(0); | ||
this.carName = carName; | ||
} | ||
|
||
public int getPosition() { | ||
return this.position.getValue(); | ||
} | ||
|
||
public String getCarName() { | ||
return this.carName; | ||
} | ||
|
||
public void move(int randomNumber) { | ||
if (isMovable(randomNumber)) this.position.increase(); | ||
} | ||
|
||
public boolean isMovable(int randomNumber) { | ||
return randomNumber >= MOVE_THRESHOLD; | ||
} | ||
|
||
public int max(int maxPosition) { | ||
return this.position.max(maxPosition); | ||
} | ||
|
||
public boolean isSamePosition(int position) { | ||
return this.position.equals(new Position(position)); | ||
} | ||
} |
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 racing.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Cars { | ||
|
||
List<Car> cars; | ||
|
||
public Cars() { | ||
this.cars = new ArrayList<Car>(); | ||
} | ||
|
||
public List<Car> getCars() { | ||
return cars; | ||
} | ||
|
||
public Car getCar(int index) { | ||
return cars.get(index); | ||
} | ||
|
||
public Cars addCar(Car car) { | ||
this.cars.add(car); | ||
return this; | ||
} | ||
} |
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 racing.domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Judgement { | ||
|
||
private final Cars cars; | ||
|
||
public Judgement(Cars cars){ | ||
this.cars = cars; | ||
} | ||
|
||
public List<Car> getWinnerCars() { | ||
|
||
return cars.getCars() | ||
.stream() | ||
.filter(car -> car.isSamePosition(getMaxPosition())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private int getMaxPosition() { | ||
int maxPosition = 0; | ||
|
||
for (Car car : cars.getCars()) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package racing.domain; | ||
|
||
import racing.data.Messages; | ||
|
||
import java.util.Objects; | ||
|
||
public class Position { | ||
|
||
int value = 0; | ||
|
||
public Position(int value) { | ||
if (value < 0) { | ||
throw new IllegalArgumentException(Messages.INVALID_NUMBER_RANGE); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
|
||
public void increase() { | ||
this.value++; | ||
} | ||
|
||
public int max(int position) { | ||
return Math.max(this.value, position); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Position position = (Position) o; | ||
return value == position.value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); | ||
} | ||
} |
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.
현재는
getCars()
로 직접 꺼내서 처리하고 있는데,RacingGame
내부에서 판단하면 어떨까요?RacingGame
내에서Judgement
를 활용해 우승자를 직접 가져오도록 변경하면 캡슐화를 더 잘 지킬 수 있을 것 같아요!