Skip to content
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 2 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/Car.java
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;
}
}
43 changes: 43 additions & 0 deletions src/main/java/InputView.java
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;
}
}
49 changes: 49 additions & 0 deletions src/main/java/RacingGame.java
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(", "));
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

해당 클래스 파일의 가장 아래에는 공백을 추가해 주면 이 표시가 사라져요 이에 대해서 검색해보시면 좋을 것 같아요! eof 관련 포스팅

37 changes: 37 additions & 0 deletions src/main/java/RacingGameApplication.java
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;
}
}
9 changes: 9 additions & 0 deletions src/main/java/ResultView.java
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 + "가 최종 우승했습니다.");
}
}