Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/modules/racing-game.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions .idea/modules/racing-game.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions .idea/racing-game.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/main/java/racing/RacingGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
import racing.view.InputView;
import racing.view.ResultView;

import java.util.List;
import java.util.Scanner;

public class RacingGame {
private final InputView inputView = new InputView(new Scanner(System.in));

public void run() {
int carCount = inputView.inputCarCount();
List<String> carNames = inputView.inputCarNames();
int tryCount = inputView.inputTryCount();

Cars cars = new Cars(CarFactory.createCar(carCount));
Cars cars = new Cars(CarFactory.createCar(carNames));
RacingResult racingResult = cars.operate(tryCount, new RandomNumberGenerator());

ResultView resultView = new ResultView();
resultView.printResult(racingResult, carCount);
resultView.printResult(racingResult, carNames.size());
}
}
8 changes: 7 additions & 1 deletion src/main/java/racing/domain/Car.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package racing.domain;

public class Car {
private String name;
private int distance;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

엇근데 random으로 받은 distance 값을 바탕으로 outputview를 작성하는데,
이렇게 하면 주어진 요구사항에 맞지 않을 것 같습니다.
레이싱 게임 요구사항을 확인해주세요.


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

public String getName() {
return name;
}

public int getDistance() {
return distance;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/racing/domain/CarFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.util.List;

public class CarFactory {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

굿 좋습니다b 기존코드와의 변경사항이 적어서 좋네요

public static List<Car> createCar(int carCount) {
public static List<Car> createCar(List<String> carNames) {
List<Car> cars = new ArrayList<>();
for (int carIdx = 0; carIdx < carCount; carIdx++) {
cars.add(new Car(0));
for (String carName : carNames) {
cars.add(new Car(carName, 0));
}
return cars;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/racing/domain/Cars.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package racing.domain;

import racing.util.NumberGenerator;
import racing.util.RandomUtil;
import racing.util.RandomDistanceUtil;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -14,7 +14,7 @@ public Cars(List<Car> cars) {
}

public RacingResult operate(int tryCount, NumberGenerator numberGenerator) {
List<Integer> racingResult = new ArrayList<>();
List<Car> racingResult = new ArrayList<>();
for (int curTryCount = 0; curTryCount < tryCount; curTryCount++) {
moveCars(racingResult, numberGenerator);
}
Expand All @@ -30,11 +30,11 @@ private int size() {
return cars.size();
}

private void moveCars(List<Integer> racingResult, NumberGenerator numberGenerator) {
private void moveCars(List<Car> racingResult, NumberGenerator numberGenerator) {
for (int carIdx = 0; carIdx < size(); carIdx++) {
Car car = get(carIdx);
car.move(RandomUtil.getRandomDistance(numberGenerator));
racingResult.add(car.getDistance());
car.move(RandomDistanceUtil.getRandomDistance(numberGenerator));
racingResult.add(new Car(car.getName(), car.getDistance()));
}
}
}
28 changes: 24 additions & 4 deletions src/main/java/racing/domain/RacingResult.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
package racing.domain;

import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

public class RacingResult {
private final List<Integer> racingResult;
private final List<Car> racingResult;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

여기서 RacingResult는 Cars 에서 움직임이 끝난 Car 객체들의 리스트를 받아서 수행을 하는데요..!!
기현님 의도가 Cars는 정말 게임을 실행하는 일급 컬렉션, RacingResult는 Cars에서 실행이 끝난 객체를 확인하는 일급 컬렉션으로 이해했습니다. (그런면에서 new Car() 를 이용해서 객체를 복사하는 효과를 낸 의도도 좋습니다b)

이때 RacingResult도 List를 담고있는데요, 이때 Car 대신에 다른 객체를 두면 좋을 것 같아요.
왜냐하면 현재 Car 객체에는 move라는 메서드가 있으니까..! Car 대신 Car의 내용(name, distance)을 담고있는 VO 객체를 주면 좋을 것 같습니다.

만약, 누군가가 RacingResult 클래스를 수정한다고 할 때. 현재 상태라면 RacingResult에서 racingResult.get(0).move() 와 같이 접근해서, List 에 있는 Car의 위치를 조작하는 메서드를 만들 수 있기 때문입니다.

따라서 위에서 RacingResult를 생성할 때 VO 객체를 만들어서, 이 객체 안에는 move와 같이, 객체의 상태를 변경하는 메서드를 제공하지 않아 불변 객체로 만드는 방식을 생각할 수 있을 것 같습니다. 만약 그 객체 이름이 CarData가 된다면

List<CarData> racingResulut;
new CarData(car.name, car.distance); 
class final CarData{

}

위 코드가 살짝의 키워드가 될 수 있겠네요.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

와 그렇네요. 좀 더 깊게 생각해야 될 거 같습니다! 감사합니다. 또 하나 배웁니다!


public RacingResult(List<Integer> racingResult) {
public RacingResult(List<Car> racingResult) {
this.racingResult = racingResult;
}

public int size() {
return racingResult.size();
}

public int get(int idx) {
public Car get(int idx) {
return racingResult.get(idx);
}

public List<String> getWinners() {
int maxDistance = getMaxDistance();
return racingResult
.stream()
.filter(car -> (car.getDistance() == maxDistance))
.map(car -> car.getName())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

람다를 사용할 때 car::getName 와 같은 참조 사용방식도 있습니다! 바꾸라는게 아니라 그런 방식도 있다는 그런..ㅎㅎ

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

오 이렇게 또 하나 알아갑니다 ㅎㅎ 감사합니다!

.distinct()
.collect(Collectors.toList());
}

private int getMaxDistance() {
Car maxByDistance = racingResult
.stream()
.max(Comparator.comparing(Car::getDistance))
.orElseThrow(NoSuchElementException::new);

return maxByDistance.getDistance();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package racing.util;

public class RandomUtil {
public class RandomDistanceUtil {
private static final int MOVE = 1;
private static final int STOP = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/racing/util/RandomNumberGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Random;

public class RandomNumberGenerator implements NumberGenerator {
private final int MAX = 10;
private static final int MAX = 10;

@Override
public int generateNumber() {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/racing/util/SplitUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package racing.util;

import java.util.Arrays;
import java.util.List;

public class SplitUtil {
public static List<String> split(String carNames) {
checkEmpty(carNames);
List<String> splitValue = Arrays.asList(carNames.split(","));

checkCount(splitValue.size());
checkDuplicate(splitValue);

return splitValue;
}

private static void checkEmpty(String carNames) {
if (carNames.isEmpty()) {
throw new NullPointerException("경주할 자동차 이름을 입력하지 않았습니다.");
}
}

private static void checkCount(int carCount) {
if (carCount < 1) {
throw new IllegalArgumentException("게임을 진행하려면 한대 이상의 자동차가 필요합니다.");
}
}

private static void checkDuplicate(List<String> splitValue) {
if (isDuplicate(splitValue)) {
throw new IllegalArgumentException("자동차의 이름은 모두 달라야 합니다.");
}
}

private static boolean isDuplicate(List<String> splitValue) {
return splitValue
.stream()
.distinct()
.count() != splitValue.size();
}
}
16 changes: 0 additions & 16 deletions src/main/java/racing/view/InputCount.java

This file was deleted.

21 changes: 16 additions & 5 deletions src/main/java/racing/view/InputView.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package racing.view;

import racing.util.SplitUtil;

import java.util.List;
import java.util.Scanner;

public class InputView {
private final String FIRST_QUESTION = "자동차 대수는 몇 대 인가요?";
private final String SECOND_QUESTION = "시도할 횟수는 몇 회 인가요?";
private static final String FIRST_QUESTION = "경주할 자동차 이름을 입력하세요(이름은 쉼표(,)를 기준으로 구분).";
private static final String SECOND_QUESTION = "시도할 횟수는 몇 회 인가요?";

private final Scanner sc;

public InputView(Scanner sc) {
Comment thread
malibinYun marked this conversation as resolved.
this.sc = sc;
}

public int inputCarCount() {
public List<String> inputCarNames() {
System.out.println(FIRST_QUESTION);
return new InputCount(sc.nextInt()).getCount();
return SplitUtil.split(sc.nextLine());
}

public int inputTryCount() {
System.out.println(SECOND_QUESTION);
return new InputCount(sc.nextInt()).getCount();
int tryCount = sc.nextInt();
checkPositiveNumber(tryCount);
return tryCount;
}

private void checkPositiveNumber(int tryCount) {
if (tryCount <= 0) {
throw new IllegalArgumentException("최소 한번 이상의 게임을 진행해야 합니다.");
}
}
}
35 changes: 26 additions & 9 deletions src/main/java/racing/view/ResultView.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
package racing.view;

import racing.domain.Car;
import racing.domain.RacingResult;

import java.util.List;

public class ResultView {
private final String OPERATE_RESULT = "실행 결과";
private final String DISTANCE = "-";
private final String ENTER = "\n";
private final String BORDER = "라운드 종료!!";
private static final String OPERATE_RESULT = "실행 결과";
private static final String DISTANCE = "-";
private static final String ENTER = "\n";
private static final String BORDER = "라운드 종료!!";

public ResultView() {
printEmpty();
System.out.println(OPERATE_RESULT);
}

public void printResult(RacingResult racingResults, int carCount) {
for (int racingIdx = 0; racingIdx < racingResults.size(); racingIdx++) {
printDistance(racingResults.get(racingIdx));
public void printResult(RacingResult racingResult, int carCount) {
for (int racingIdx = 0; racingIdx < racingResult.size(); racingIdx++) {
printCarName(racingResult.get(racingIdx));
printDistance(racingResult.get(racingIdx));
checkEachRound(racingIdx, carCount);
}
printEmpty();
printWinner(racingResult.getWinners());
}

private void printCarName(Car car) {
System.out.print(car.getName() + " : ");
}

private void printDistance(int distance) {
for (int length = 0; length < distance; length++) {
private void printDistance(Car car) {
for (int length = 0; length < car.getDistance(); length++) {
System.out.print(DISTANCE);
}
printEmpty();
Expand All @@ -41,4 +50,12 @@ private boolean isLastCar(int racingIdx, int carCount) {
private void printEmpty() {
System.out.print(ENTER);
}

private void printWinner(List<String> winners) {
int lastIdx = winners.size() - 1;
for (int winnerIdx = 0; winnerIdx < lastIdx; winnerIdx++) {
System.out.print(winners.get(winnerIdx) + ",");
}
System.out.println(winners.get(lastIdx) + "가 최종 우승했습니다.");
}
}
Loading