-
Notifications
You must be signed in to change notification settings - Fork 74
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
[LBP] 송하은 자동차 경주 미션 1,2,3단계 제출 #89
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
public class Car { | ||
|
||
private final String name; | ||
private int position; | ||
private static final int MOVE_TRIGGER = 4; | ||
|
||
public Car(String name) { | ||
this.name = name; | ||
this.position = 0; | ||
} | ||
|
||
public void move(int value) { | ||
if (value >= MOVE_TRIGGER) { | ||
position++; | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getPosition() { | ||
return 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,43 @@ | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private static final int MAX_NAME_LENGTH = 5; | ||
private final Scanner scanner = new Scanner(System.in); | ||
|
||
private String getUserInput() { | ||
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분)."); | ||
return scanner.nextLine(); | ||
} | ||
|
||
private String[] splitCarNames(String inputValue) { | ||
return inputValue.split(","); | ||
} | ||
|
||
public String[] inputCarNames() throws Exception { | ||
String inputValue = getUserInput(); | ||
String[] carsName = splitCarNames(inputValue); | ||
|
||
validateName(carsName); | ||
return carsName; | ||
} | ||
|
||
private void validateName(String[] carsName) throws Exception { | ||
for (String carName : carsName) { | ||
validateInput(carName); | ||
} | ||
} | ||
|
||
private void validateInput(String carName) throws Exception { | ||
if (carName.length() > MAX_NAME_LENGTH) { | ||
throw new Exception("자동차 이름은 "+MAX_NAME_LENGTH+ "자 이하여야합니다."); | ||
} | ||
} | ||
|
||
public int inputRaceRounds() { | ||
System.out.println("시도할 회수는 몇회인가요?"); | ||
int rounds = scanner.nextInt(); | ||
System.out.println(); | ||
return rounds; | ||
} | ||
} |
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,49 @@ | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
class RacingGame { | ||
|
||
private List<Car> cars; | ||
|
||
public RacingGame(List<Car> cars) { | ||
this.cars = new ArrayList<>(cars); | ||
} | ||
|
||
public void gameStart(int rounds) { | ||
while (rounds-- > 0) { | ||
moveCars(); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
private void moveCars() { | ||
Random random = new Random(); | ||
for (Car car : cars) { | ||
System.out.print(car.getName() + " : "); | ||
int randomValue = random.nextInt(10); | ||
car.move(randomValue); | ||
System.out.print("-".repeat(car.getPosition())); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
public List<Car> getWinner() { | ||
int maxPosition = getMaxPosition(); | ||
return cars.stream() | ||
.filter(car -> car.getPosition() == maxPosition) | ||
.collect(Collectors.toUnmodifiableList()); | ||
} | ||
|
||
private int getMaxPosition() { | ||
return cars.stream() | ||
.mapToInt(Car::getPosition) | ||
.max() | ||
.orElse(0); | ||
} | ||
|
||
public String findWinnerName(List<Car> winnerCars) { | ||
return winnerCars.stream() | ||
.map(car -> car.getName()) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
} | ||
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,37 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RacingGameApplication { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
InputView inputView = new InputView(); | ||
|
||
// 1. 자동차 이름 입력 및 검증 | ||
String[] carsName = inputView.inputCarNames(); | ||
List<Car> cars = createCars(carsName); | ||
|
||
RacingGame racingGame = new RacingGame(cars); | ||
ResultView resultView = new ResultView(); | ||
|
||
// 2. 시도할 횟수 입력 | ||
int rounds = inputView.inputRaceRounds(); | ||
|
||
// 3. 게임 실행 | ||
racingGame.gameStart(rounds); | ||
|
||
// 4. 결과 출력 | ||
List<Car> winnerCars = racingGame.getWinner(); | ||
String winnerNames = racingGame.findWinnerName(winnerCars); | ||
|
||
resultView.printResult(winnerCars, winnerNames); | ||
} | ||
|
||
public static List<Car> createCars(String[] carNames) { | ||
List<Car> cars = new ArrayList<>(); | ||
for (String name : carNames) { | ||
cars.add(new Car(name.trim())); | ||
} | ||
return cars; | ||
} | ||
} |
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,9 @@ | ||
import java.util.List; | ||
|
||
public class ResultView { | ||
|
||
public void printResult(List<Car> winnerCars, String winnerNames) { | ||
System.out.println("실행 결과"); | ||
System.out.println(winnerNames + "가 최종 우승했습니다."); | ||
} | ||
} |
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.
해당 클래스 파일의 가장 아래에는 공백을 추가해 주면 이 표시가 사라져요 이에 대해서 검색해보시면 좋을 것 같아요! eof 관련 포스팅