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 #6019

Closed
wants to merge 3 commits into from
Closed

Step4 #6019

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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@
* 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다.

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


## 요구사항분석
* 자동차 이름 입력받기
* 자동차 이름 분리하기
* 자동차 시도 횟수 입력받기
* 자동차 랜덤으로 이동하기
* 자동차 현 위치 리턴하기
* 자동차 현 위치 출력하기
* 자동차 우승자 출력하기
26 changes: 26 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Car {

public static final int STANDARD_VALUE = 4;
private int position;
private String name;

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

public void move() {
// 0에서 9 사이에서 random 값을 구한 후 random 값이 4이상일 경우이다.
if ((int) (Math.random() * 10) >= STANDARD_VALUE) {
this.position++;
}
}

public int getPosition() {
return this.position;
}

public String getName() {
return this.name;
}
}
18 changes: 18 additions & 0 deletions src/main/java/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Scanner;

public final class InputView {

private InputView() {}

public static String inputCarString() {
final Scanner scanner = new Scanner(System.in);
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).");
return scanner.nextLine();
}

public static int inputTryNum() {
final Scanner scanner = new Scanner(System.in);
System.out.println("시도할 회수는 몇 회 인가요?");
return scanner.nextInt();
}
}
77 changes: 77 additions & 0 deletions src/main/java/RacingManger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class RacingManger {

public static final String DELIMITER = ",";
public static final int NAME_LENGTH_LIMIT = 5;
private Car[] cars;

public RacingManger(String carString) {
makeCars(carString);
}

private Car[] makeCars(String carString) {
final String[] carNames = carString.split(DELIMITER);
validNameLength(carNames);
this.cars = new Car[carNames.length];
for (int i = 0; i < carNames.length; i++) {
this.cars[i] = new Car(carNames[i]);
}
return this.cars;
}

private static void validNameLength(String[] carNames) {
for (String carName : carNames) {
throwWhenExceedLimitLength(carName);
}
}

private static void throwWhenExceedLimitLength(String carName) {
if (carName.length() > NAME_LENGTH_LIMIT) {
throw new IndexOutOfBoundsException("name length exceed");
}
}

public void play() {
for (Car car : this.cars) {
car.move();
}
}

public Car[] getCars() {
return this.cars;
}

public String getWinners() {
int maxPosition = 0;
final HashMap<Integer, List<String>> winnersMap = new HashMap<>();
for (Car car : this.cars) {
final int carPosition = car.getPosition();
maxPosition = getMaxPosition(car, maxPosition, carPosition, winnersMap);
}

return String.join(DELIMITER, winnersMap.get(maxPosition));
}

private static int getMaxPosition(Car car, int maxPosition, int carPosition, HashMap<Integer, List<String>> winnersMap) {
if (maxPosition <= carPosition) {
maxPosition = carPosition;
fillWinnerIntoMap(car, maxPosition, winnersMap);
}
return maxPosition;
}

private static void fillWinnerIntoMap(Car car, int maxPosition, HashMap<Integer, List<String>> winnersMap) {
if (Objects.isNull(winnersMap.get(maxPosition))) {
final List<String> newList = new ArrayList<>();
newList.add(car.getName());
winnersMap.put(maxPosition, newList);
return;
}

winnersMap.get(maxPosition).add(car.getName());
}
}
27 changes: 27 additions & 0 deletions src/main/java/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public final class ResultView {

public static void printWinners(String winners) {
System.out.print(winners + "가 최종 우승했습니다.");
}

private ResultView() {}

public static void printResult(final Car[] cars) {
for (Car car : cars) {
printCarPosition(car.getName(), car.getPosition());
System.out.println();
}
System.out.println();
}

public static void printTitle() {
System.out.println("실행 결과");
}

private static void printCarPosition(String name, int position) {
System.out.print(name + " : ");
for (int i = 0; i < position; i++) {
System.out.print('-');
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/javaRacingCarApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public final class javaRacingCarApplication {

private javaRacingCarApplication() {}

public static void main(String[] args) {
final String carString = InputView.inputCarString();
final int tryNum = InputView.inputTryNum();
final RacingManger racingManger = new RacingManger(carString);
ResultView.printTitle();
for (int i = 0; i < tryNum; i++) {
racingManger.play();
ResultView.printResult(racingManger.getCars());
}
ResultView.printWinners(racingManger.getWinners());
}
}
17 changes: 17 additions & 0 deletions src/test/java/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class CarTest {

@Test
@DisplayName("자동차 객체 생성 테스트")
void carConstructor() {
final Car car = new Car("pobi");
final int position = car.getPosition();
final String name = car.getName();
assertThat(car.getPosition()).isEqualTo(position);
assertThat(car.getName()).isEqualTo(name);
}
}
47 changes: 47 additions & 0 deletions src/test/java/RacingMangerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class RacingMangerTest {

@Test
@DisplayName("통합 테스트")
void integration() {
final int tryNum = 5;
final RacingManger racingManger = new RacingManger("pobi,crong,honux");
for (int i = 0; i < tryNum; i++) {
racingManger.play();
}
final Car[] resultCar = racingManger.getCars();

assertThat(resultCar.length).isEqualTo(3);
for (Car car : resultCar) {
assertThat(car.getPosition()).isBetween(0, tryNum);
}
}

@Test
void makeCars() {
final RacingManger racingManger = new RacingManger("pobi,crong,honux");
final Car[] cars = racingManger.getCars();

assertThat(cars.length).isEqualTo(3);
for (Car car : cars) {
assertThat(car.getPosition()).isEqualTo(0);
}
assertThat(cars[0].getName()).isEqualTo("pobi");
assertThat(cars[1].getName()).isEqualTo("crong");
assertThat(cars[2].getName()).isEqualTo("honux");
}

@Test
@DisplayName("각 자동차에 이름을 부여할 수 있다. 자동차 이름은 5자를 초과할 수 없다.")
void makeCars_name_exceed_5_length() {
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> {
new RacingManger("pobipo,crong,honux");
}).withMessageMatching("name length exceed");
}
}