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

레이싱게임 #19

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

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

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

9 changes: 9 additions & 0 deletions .idea/compiler.xml

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

8 changes: 7 additions & 1 deletion .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/racing-game.iml

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

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
- 테스트 커버리지는 최소 60%를 넘겨야한다.
- 한 메소드에 10줄을 넘지 않는다.



![image](https://user-images.githubusercontent.com/47847993/77644495-694a0600-6fa4-11ea-82ad-b565725fa219.png)
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jacoco {
toolVersion = '0.8.5'
}


group 'org.javabom'
version '1.0-SNAPSHOT'

Expand All @@ -21,6 +22,7 @@ dependencies {
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.15.0'
}


test {
useJUnitPlatform()
jacoco {
Expand Down Expand Up @@ -76,4 +78,4 @@ jacocoTestCoverageVerification {
}

}
}
}
19 changes: 19 additions & 0 deletions src/main/java/racing/RacingGameApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package racing;

import racing.domain.RacingGame;
import racing.dto.RacingGameInfo;
import racing.support.DefaultEngine;
import racing.view.InputView;
import racing.view.OutputView;

public class RacingGameApplication {

public static void main(String[] args) {
RacingGameInfo racingGameInfo = new RacingGameInfo(InputView.askNameOfCar(), InputView.askCountOfMovement());
RacingGame racingGame = new RacingGame(racingGameInfo);

racingGame.raceWith(new DefaultEngine());

OutputView.printResult(racingGame.getRacingGameResult());
}
}
28 changes: 28 additions & 0 deletions src/main/java/racing/domain/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package racing.domain;

import racing.support.Engine;

public class Car {

private final String name;
private int location;

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

public void tryMoveWith(Engine engine) {
if (engine.enough()) {
location++;
}
}

public int getLocation() {
return location;
}

public String getName() {
return name;
}

}
36 changes: 36 additions & 0 deletions src/main/java/racing/domain/RacingCars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package racing.domain;

import racing.dto.RacingGameInfo;
import racing.support.Engine;
import racing.vo.CarSnapshot;
import racing.vo.RacingCarsSnapshot;

import java.util.ArrayList;
import java.util.List;

public class RacingCars {

private final List<Car> cars;

public RacingCars(final RacingGameInfo racingGameInfo) {
this.cars = createCar(racingGameInfo.getCarNameGroup());
}

private List<Car> createCar(final List<String> carGroup) {
List<Car> cars = new ArrayList<>(carGroup.size());
for (String carName : carGroup) {
cars.add(new Car(carName));
}
return cars;
}

public RacingCarsSnapshot runWith(final int round, final Engine engine) {
List<CarSnapshot> carSnapshots = new ArrayList<>(cars.size());
for (Car car : cars) {
car.tryMoveWith(engine);
carSnapshots.add(new CarSnapshot(car.getName(), car.getLocation()));
}
return new RacingCarsSnapshot(round, carSnapshots);
}

}
40 changes: 40 additions & 0 deletions src/main/java/racing/domain/RacingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package racing.domain;

import racing.dto.RacingGameInfo;
import racing.dto.RacingGameResult;
import racing.support.Engine;
import racing.vo.RacingCarsSnapshot;

public class RacingGame {

private final RacingCars racingCars;
private final RacingGameInfo racingGameInfo;
private final RacingGameResult racingGameResult;
Copy link
Contributor

Choose a reason for hiding this comment

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

이 RacingGameResult라는 객체는 record 라는 메서드 때문에 가변일 수 밖에 없는 객체인데, RacingGameResult를 멤버 필드로 강한 참조를 해버려서 RacingGame도 가변객체가 된다는 느낌이 있네..!

나라면 raceWith를 void가 아닌 RacingGameResult로 낮추어서 조금 관계를 약하게 만들 것 같아


public RacingGame(RacingGameInfo racingGameInfo) {
this(new RacingCars(racingGameInfo), racingGameInfo, new RacingGameResult(racingGameInfo));

Choose a reason for hiding this comment

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

이렇게 코드를 짜는 장점이뭐야?

Copy link
Author

Choose a reason for hiding this comment

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

아 이거 디디디 강의때 모든 생성을 주생성자에게 위임하는방식을 봐서 한번 적용해봤던거얌

}

private RacingGame(RacingCars racingCars, RacingGameInfo racingGameInfo, RacingGameResult racingGameResult) {
this.racingCars = racingCars;
this.racingGameInfo = racingGameInfo;
this.racingGameResult = racingGameResult;
}

public void raceWith(Engine engine) {
for (int currentRound = 1; currentRound < racingGameInfo.getCountOfMovement() + 1; currentRound++) {
RacingCarsSnapshot snapshot = racingCars.runWith(currentRound, engine);
recordRacingGameSnapshot(snapshot);
}
}

private void recordRacingGameSnapshot(RacingCarsSnapshot racingCars) {
racingGameResult.record(racingCars);
}

public RacingGameResult getRacingGameResult() {
return racingGameResult;
}


}
Empty file.
45 changes: 45 additions & 0 deletions src/main/java/racing/dto/RacingGameInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package racing.dto;

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

public class RacingGameInfo {
private static final String NAME_SPLITTER = ",";

private final List<String> carNameGroup;
private final int countOfMovement;

public RacingGameInfo(String nameOfCars, String countOfMovement) {
try {
List<String> carGroup = toCarGroup(nameOfCars.trim());
int inputCountOfMovement = Integer.parseInt(countOfMovement);

validation(carGroup, inputCountOfMovement);

this.carNameGroup = carGroup;
this.countOfMovement = inputCountOfMovement;
} catch (NumberFormatException ne) {
throw new IllegalArgumentException("Integer 값만 들어올 수 있습니다");
}
}

private List<String> toCarGroup(String carGroup) {
return Arrays.asList(carGroup.split(NAME_SPLITTER));
}

private void validation(final List<String> inputNumberOfCar, final int inputCountOfMovement) {
if (inputNumberOfCar.size() <= 1 || inputCountOfMovement <= 0) {
throw new IllegalArgumentException("올바른 입력값이 아닙니다");
}
}

public List<String> getCarNameGroup() {
return carNameGroup;
}

public int getCountOfMovement() {
return countOfMovement;
}


}
30 changes: 30 additions & 0 deletions src/main/java/racing/dto/RacingGameResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package racing.dto;

Choose a reason for hiding this comment

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

지금 나눈 dto와 vo의 차이가 뭐야?
또 RacingGameResult라는 dto를 도메인 클래스가 멤버변수로 들고있는 구조가 이상해보이고, 지금 정의한 dto,vo전부 도메인 영역으로 표현하는게 좋아보여

Copy link
Author

Choose a reason for hiding this comment

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

dto 는 입력, 출력할 때 사용되는 객체고 vo 는 도메인(경기가 끝난 레이싱카)을 그대로 반환하는 것을 막기 위해 Snapshot 클래스를 만든건데 이상한가 ,,? Dto를 도메인이 물고있는 구조가 이상한건 동의해! 이건 더 생각해서 고쳐볼겝 !

Copy link
Author

Choose a reason for hiding this comment

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

경기 결과를 받는 클라이언트한테 도메인을 노출시키고 싶지 않은데 vo를 도메인 영역으로 표현하면 그게 안될거같거든 ㅠ^ㅠ 머르게따 ,,


import racing.vo.RacingCarsSnapshot;

import java.util.ArrayList;
import java.util.List;

public class RacingGameResult {
private final int totalGameRound;

Choose a reason for hiding this comment

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

게임결과 클래스에 총 게임 라운드가 필요없어보여,
RacingGame클래스에 게임정보클래스로 게임판수를 들고있는데 굳이 여기에 값을 복사해서 valid를 체크하는 것 보단 RacingGame에서 체크하는게 좋을거같아

private final List<RacingCarsSnapshot> racingCarsSnapshots = new ArrayList<>();

public RacingGameResult(final RacingGameInfo racingGameInfo) {
this.totalGameRound = racingGameInfo.getCountOfMovement();
}

public void record(RacingCarsSnapshot racingCarsSnapshot) {
if (disableRecord()) {
throw new IllegalArgumentException(String.format("%d경기 이상 기록할 수 없습니다.", totalGameRound));
}
racingCarsSnapshots.add(racingCarsSnapshot);
}

private boolean disableRecord() {
return racingCarsSnapshots.size() >= totalGameRound;
}

public List<RacingCarsSnapshot> getRacingCarsSnapshots() {
return racingCarsSnapshots;
}
}
17 changes: 17 additions & 0 deletions src/main/java/racing/support/DefaultEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package racing.support;

import racing.util.RandomGenerator;

public class DefaultEngine implements Engine {

private static final int MOVE_CONDITION = 4;

@Override
public boolean enough() {
return isMovable(RandomGenerator.pickRandomNumber());
}

private boolean isMovable(final int power) {
return power >= MOVE_CONDITION;
}
}
5 changes: 5 additions & 0 deletions src/main/java/racing/support/Engine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package racing.support;

public interface Engine {

Choose a reason for hiding this comment

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

Engine과 관련된 클래스들은 다 도메인 영역으로 옮기는게 좋아보여
도메인 비지니스에 핵심로직이니 util,support 패키지 보단 도메인 영역이 맞아

boolean enough();
}
13 changes: 13 additions & 0 deletions src/main/java/racing/util/RandomGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package racing.util;

import java.util.Random;

public class RandomGenerator {

private static final int LIMIT = 10;
private static final Random RANDOM = new Random();

public static int pickRandomNumber() {
return RANDOM.nextInt(LIMIT);
}
}
16 changes: 16 additions & 0 deletions src/main/java/racing/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package racing.view;

import java.util.Scanner;

public class InputView {

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

public static String askCountOfMovement() {
System.out.println("이동횟수를 입력하세요");
return new Scanner(System.in).next();
}
}
38 changes: 38 additions & 0 deletions src/main/java/racing/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package racing.view;

import racing.dto.RacingGameResult;
import racing.vo.CarSnapshot;
import racing.vo.RacingCarsSnapshot;

import java.util.List;

public class OutputView {

private static final String TRACK_SIGNATURE = "-";

public static void printResult(RacingGameResult racingGameResult) {
List<RacingCarsSnapshot> racingCarsPerRound = racingGameResult.getRacingCarsSnapshots();
for (RacingCarsSnapshot racingCarsSnapshot : racingCarsPerRound) {
printNameAndTrack(racingCarsSnapshot);
System.out.println();
}

}

private static void printNameAndTrack(RacingCarsSnapshot racingCarsSnapshot) {
for (CarSnapshot carSnapshot : racingCarsSnapshot.getCarSnapshots()) {
System.out.printf("%s: ", carSnapshot.getName());
drawTrack(carSnapshot);
}
}

private static void drawTrack(CarSnapshot car) {
StringBuilder track = new StringBuilder();
for (int i = 0; i < car.getLocation(); i++) {
track.append(TRACK_SIGNATURE);
}
System.out.print(track.toString());
System.out.println();
}

}
Empty file.
Loading