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

Step4 - 자동차 경주(우승자 출력) #6047

Merged
merged 6 commits into from
Mar 23, 2025
Merged
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -6,4 +6,17 @@
* 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다.

## 온라인 코드 리뷰 과정
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/next-step/nextstep-docs/tree/master/codereview)

# 3단계 - 자동차 경주
* 초간단 자동차 경주 게임을 구현한다.
* 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다.
* 사용자는 몇 대의 자동차로 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다.
* 전진하는 조건은 0에서 9 사이에서 random 값을 구한 후 random 값이 4이상일 경우이다.
* 자동차의 상태를 화면에 출력한다. 어느 시점에 출력할 것인지에 대한 제약은 없다.

# 4단계 - 자동차 경주 (우승자)
* 각 자동차에 이름을 부여할 수 있다. 자동차 이름은 5자를 초과할 수 없다.
* 전진하는 자동차를 출력할 때 자동차 이름을 같이 출력한다.
* 자동차 이름은 쉼표(,)를 기준으로 구분한다.
* 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한명 이상일 수 있다.
22 changes: 0 additions & 22 deletions src/main/java/Car.java

This file was deleted.

17 changes: 0 additions & 17 deletions src/main/java/InputView.java

This file was deleted.

36 changes: 0 additions & 36 deletions src/main/java/RacingCar.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/ResultView.java

This file was deleted.

13 changes: 6 additions & 7 deletions src/main/java/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringAddCalculator
{
public class StringAddCalculator {
private static final String DELIMITER = ",|:";
private static final int DELIMITER_GROUP_ID = 1;
private static final int STRING_GROUP_ID = 2;

public int add(String str){
public int add(String str) {

if(isBlank(str)){
if (isBlank(str)) {
str = "0";
}

@@ -20,23 +19,23 @@ public int add(String str){

private int[] toIntArr(String[] strArr) {
int[] intArr = new int[strArr.length];
for(int i = 0; i < strArr.length; i++){
for (int i = 0; i < strArr.length; i++) {
intArr[i] = toInt(strArr[i]);
}
return intArr;
}

private int toInt(String str) {
int num = Integer.parseInt(str);
if (num < 0){
if (num < 0) {
throw new IllegalArgumentException(num + "음수는 허용하지 않습니다.");
}
return num;
}

private static int getSum(int[] numbers) {
int sum = 0;
for(int num : numbers){
for (int num : numbers) {
sum += num;
}
return sum;
29 changes: 29 additions & 0 deletions src/main/java/racing/RacingMain.java
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());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ResultView.printWinner(new Judgement(racingGame.getCars()).getWinnerCars());
ResultView.printWinner(racingGame.getwWinnerCars());

현재는 getCars()로 직접 꺼내서 처리하고 있는데, RacingGame 내부에서 판단하면 어떨까요?
RacingGame 내에서 Judgement를 활용해 우승자를 직접 가져오도록 변경하면 캡슐화를 더 잘 지킬 수 있을 것 같아요!

}
}
13 changes: 13 additions & 0 deletions src/main/java/racing/data/Messages.java
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 = "음수가 될 수 없습니다.";
}
53 changes: 53 additions & 0 deletions src/main/java/racing/domain/Car.java
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));
}
}
26 changes: 26 additions & 0 deletions src/main/java/racing/domain/Cars.java
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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/racing/domain/Judgement.java
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;
}
}
41 changes: 41 additions & 0 deletions src/main/java/racing/domain/Position.java
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);
}
}
Loading