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 리뷰 요청드립니다. #5528

Open
wants to merge 11 commits into
base: zhenxikim
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
3 changes: 3 additions & 0 deletions RACINGCAR_TODO.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
- [X] 생선된 자동차 수는 1보다 크다.
- [X] 전진하는 조건은 0에서 9 사이에서 random 값을 구한 후 random 값이 4이상일 경우이다.
- [X] 자동차 이름은 5자를 초과할 수 없다.
- [X] 우승자는 한명 이상일 수 있다.
Comment on lines +3 to +4
Copy link

Choose a reason for hiding this comment

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

👍


12 changes: 12 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@
public abstract class Car {
protected String status = "";

protected String name = "";


public abstract void drive(int num, String delimiter);

public abstract int getNumber();

public String getStatus() {
return this.status;
}

public String getName() {
return this.name;
}

public Car(String name) {
this.name = name;
}

}
21 changes: 21 additions & 0 deletions src/main/java/FormattingUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.List;

/**
* @author jeongheekim
* @date 3/27/24
*/
public class FormattingUtil {
private FormattingUtil() {}

public static String formattingResult(List<String> list) {
StringBuilder sb = new StringBuilder();
int size = list.size();
for (int i = 0; i < size; i++) {
sb.append(list.get(i));
if (i < size - 1) {
sb.append(",");
}
}
Copy link

Choose a reason for hiding this comment

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

규칙 1: 한 메서드에 오직 한 단계의 들여쓰기(indent)만 한다.
indent가 2이상인 메소드의 경우 메소드를 분리하면 indent를 줄일 수 있다.
else를 사용하지 않아 indent를 줄일 수 있다.

인덴트가 2이상이군요 🤔

return sb.toString();
}
}
38 changes: 26 additions & 12 deletions src/main/java/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,65 @@
public class Game {

public static final String DELIMETER = "-";

public static final String CAR_NAME_DELIMETER = ",";

private int gameCount;

private Car[] cars;

public void start() {
cars = createCar();

cars = createCar();

gameCount = getGameCount();
System.out.println("실행 결과");
for (int i = 0; i < gameCount; i++) {
job();
}

Winner winner = new Winner();
System.out.println(FormattingUtil.formattingResult(winner.getWinners(cars)) + "가 최종 우승했습니다.");
Copy link

Choose a reason for hiding this comment

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

image

d, e 가 우승했는데 제대로 출력되고있지 않네요 😅


}

private void job() {
for (Car car : this.cars) {
car.drive(car.getNumber(), DELIMETER);
System.out.println(car.getStatus());
System.out.println(car.getName() + ":" + car.getStatus());
}
System.out.println(" ");
}

private int getGameCount() {
Scanner scanner = new Scanner(System.in);
System.out.println("시도할 회수는 몇 회 인가요?");

int gameCount = scanner.nextInt();
System.out.println("gameCount = " + gameCount);
System.out.println(gameCount);

return gameCount;
}


private Car[] createCar() {
int carCount = getCarCount();
Car[] cars = new Car[carCount];
for (int i = 0; i < carCount; i++) {
cars[i] = new SmallCar();
String[] carNameInputArr = getCarNameInputArr();
int length = carNameInputArr.length;
Car[] cars = new Car[length];
for (int i = 0; i < length; i++) {
cars[i] = new SmallCar(carNameInputArr[i]);

}
return cars;
}

private int getCarCount() {
System.out.println("자동차 대수는 몇 대 인가요?");

private String[] getCarNameInputArr() {
System.out.println("경주할 자동차 이름을 입력하세요(이름은 " + CAR_NAME_DELIMETER + "를 기준으로 구분).");
Scanner scanner = new Scanner(System.in);
int carCount = Integer.parseInt(scanner.nextLine());
System.out.println("자동차 대수 = " + carCount);
return carCount;
String carNameInput = scanner.nextLine();
System.out.println(carNameInput);
return carNameInput.split(CAR_NAME_DELIMETER);
}

public Car[] getCars() {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/SmallCar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
* @date 3/12/24
*/
public class SmallCar extends Car {
private String status = "";

public SmallCar(String name) {
super(name);
}

@Override
public void drive(int num, String delimeter) {
Expand All @@ -18,6 +21,5 @@ public int getNumber() {
return (int) (Math.random() * 10);
}


}

35 changes: 35 additions & 0 deletions src/main/java/Winner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.ArrayList;
import java.util.List;

/**
* @author jeongheekim
* @date 3/26/24
*/
public class Winner {
private List<String> winners = new ArrayList<>();
private int maxLength;

public void addWinner(String name) {
this.winners.add(name);
}

public void updateMaxLength(int length) {
this.maxLength = length;
}

public List<String> getWinners(Car[] cars) {
List<String> winners = new ArrayList<>();
for (Car car : cars) {
this.filterMaxLength(car);
}
return winners;
}

private void filterMaxLength(Car car) {
int statusLength = car.getStatus().length();
if (this.maxLength <= statusLength) {
this.updateMaxLength(statusLength);
this.addWinner(car.getName());
}
}
}
27 changes: 24 additions & 3 deletions src/test/java/SmallCarTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import org.junit.jupiter.api.DisplayName;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand All @@ -14,7 +16,7 @@ class SmallCarTest {
@ValueSource(ints = {4,5,6})
@DisplayName("random 값이 4이상일 경우 앞으로 전진한다.")
void whenRandom4goDriveTest(int randomNumber) {
SmallCar car = new SmallCar();
SmallCar car = new SmallCar("pobi");
car.drive(randomNumber,"-");
assertTrue(car.getStatus().length() > 0);
}
Expand All @@ -23,8 +25,27 @@ void whenRandom4goDriveTest(int randomNumber) {
@ValueSource(ints = {1,2,3})
@DisplayName("random 값이 4미만일 경우 전진하지 않는다.")
void whenRandom4NotDriveTest(int randomNumber) {
SmallCar car = new SmallCar();
SmallCar car = new SmallCar("pobi");
car.drive(randomNumber,"-");
assertTrue(car.getStatus().length() == 0);
}

@ParameterizedTest
@ValueSource(ints = {5})
@DisplayName("자동차 이름은 5자를 초과할 수 없다.")
void carNameNotOverLength(int length) {
SmallCar car = new SmallCar("pobi");
String name = car.getName();
assertTrue(name.length() <= length);
}

@ParameterizedTest
@ValueSource(ints = {5})
@DisplayName("자동차 이름은 5자를 초과할 수 없다.")
void carNameNotOverLength2(int length) {
SmallCar car = new SmallCar("pobipobi");
String name = car.getName();
assertFalse(name.length() <= length);
}

}
29 changes: 29 additions & 0 deletions src/test/java/WinnerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author jeongheekim
* @date 3/27/24
*/
class WinnerTest {

@Test
@DisplayName("우승자는 한명 이상이다.")
void winnerLengthTest() {
SmallCar car = new SmallCar("pobi");
car.drive(7, "-");

Car[] cars = new Car[1];
cars[0] = car;

Winner winner = new Winner();
List<String> winners = winner.getWinners(cars);

assertTrue(winners.size() >= 1);
}

}