Skip to content

Commit

Permalink
요구사항 2 리팩토링
Browse files Browse the repository at this point in the history
1. 각 자동차가 이동 후 결과를 가지고있는 VO CarResult 구현
2. InputValueDto 대신 게임 정보를 가지고있는 GameInformation 로 변경
  • Loading branch information
Gobukgol committed May 14, 2020
1 parent 5880c41 commit 7f78f35
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 179 deletions.
9 changes: 6 additions & 3 deletions src/main/java/racing/RacingGameApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import racing.domain.GameResult;
import racing.domain.RacingGame;
import racing.domain.common.RandomNumberGenerator;
import racing.dto.InputValueDto;
import racing.dto.GameInformation;
import racing.view.InputView;
import racing.view.OutputView;

public class RacingGameApplication {
public static void main(String[] args) {
InputValueDto inputValueDto = InputView.getInputValueWithName();
String[] namesOfCars = InputView.inputNameOfCars();
int numberOfAttepts = InputView.inputNumberOfAttempts();

RacingGame racingGame = new RacingGame(inputValueDto);
GameInformation gameInformation = new GameInformation(namesOfCars, numberOfAttepts);

RacingGame racingGame = new RacingGame(gameInformation);

GameResult gameResult = racingGame.startGame(new RandomNumberGenerator());

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/racing/domain/CarResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package racing.domain;


public class CarResult implements Comparable {
private static final int COMPARE_EQUAL = 0;
private static final int COMPARE_SMALL = -1;
private String name;
private int distance;

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

public String getName() {
return name;
}

public int getDistance() {
return distance;
}

@Override
public int compareTo(Object o) {
CarResult target = (CarResult) o;

int compare = Integer.compare(target.distance, this.distance);
//
// if (compare == COMPARE_EQUAL) {
// return COMPARE_SMALL;
// }

return compare;
}
}
31 changes: 6 additions & 25 deletions src/main/java/racing/domain/ParticipateCars.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import racing.domain.common.NumberGenerator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ParticipateCars {
Expand All @@ -15,36 +14,18 @@ public ParticipateCars(String[] namesOfCars) {
cars = createCars(namesOfCars);
}

public List<Integer> tryMove(NumberGenerator numberGenerator) {
return cars.stream()
.map(car -> car.attemptToMove(numberGenerator.generateNumber()))
.collect(Collectors.toList());
}

public Map<String, Integer> tryMoveWithName(NumberGenerator numberGenerator) {

/* Map<String, Integer> moveResults = new LinkedHashMap<>();
public List<CarResult> moveCars(NumberGenerator numberGenerator) {
List<CarResult> carResults = new ArrayList<>();

for (Car car : cars) {
moveResults.put(car.getName(),car.attemptToMove(numberGenerator.generateNumber()));
int afterMove = car.attemptToMove(numberGenerator.generateNumber());
carResults.add(new CarResult(car.getName(), afterMove));
}

return moveResults;*/

return cars.stream()
.collect(Collectors.toMap(Car::getName,
car -> car.attemptToMove(numberGenerator.generateNumber()),
(x, y) -> x,
LinkedHashMap::new));
return carResults;
}

private List<Car> createCars(String[] namesOfCars) {
/* List<Car> cars = new ArrayList<>();
for (int i = 0 ; i < namesOfCars.length ; i++) {
cars.add(new Car(namesOfCars[i]));
}*/

return Arrays.stream(namesOfCars)
.map(String::trim)
.map(Car::new)
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/racing/domain/PhaseResult.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package racing.domain;

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


public class PhaseResult {
private final Map<String, Integer> raceResults;
private final List<CarResult> raceResults;

public PhaseResult(Map<String, Integer> raceResults) {
public PhaseResult(List<CarResult> raceResults) {
this.raceResults = raceResults;
}

public Map<String, Integer> getRaceResults() {
public List<CarResult> getRaceResults() {
return raceResults;
}

public List<String> getCurrentLeads() {
int max = raceResults.entrySet().stream()
.mapToInt(Map.Entry::getValue)
.max()
.orElseThrow(IllegalStateException::new);
CarResult currentLead = raceResults.stream()
.sorted()
.findFirst()
.orElseThrow(IllegalAccessError::new);

return raceResults.entrySet().stream()
.filter(x -> x.getValue() == max)
.map(Map.Entry::getKey)
return raceResults.stream()
.filter(carResult -> carResult.getDistance() == currentLead.getDistance())
.map(CarResult::getName)
.collect(Collectors.toList());
}
}
18 changes: 9 additions & 9 deletions src/main/java/racing/domain/RacingGame.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package racing.domain;

import racing.domain.common.NumberGenerator;
import racing.dto.InputValueDto;
import racing.dto.GameInformation;

public class RacingGame {
private final RacingPhase racingPhase;
private final InputValueDto inputValueDto;
private final ParticipateCars participateCars;
private final GameInformation gameInformation;

public RacingGame(InputValueDto inputValueDto) {
this.inputValueDto = inputValueDto;
this.racingPhase = new RacingPhase(inputValueDto.getNamesOfCars());
public RacingGame(GameInformation gameInformation) {
this.gameInformation = gameInformation;
this.participateCars = new ParticipateCars(gameInformation.getNamesOfCars());
}

public GameResult startGame(NumberGenerator numberGenerator) {
int numberOfAttempts = inputValueDto.getNumberOfAttempts();
int numberOfAttempts = gameInformation.getNumberOfAttempts();
GameResult gameResult = new GameResult(numberOfAttempts);

for (int i = 0; i < numberOfAttempts; i++) {
PhaseResult eachPhaseResult = racingPhase.startRace(numberGenerator);
gameResult.addEachRacingResult(eachPhaseResult);
PhaseResult phaseResult = new PhaseResult(participateCars.moveCars(numberGenerator));
gameResult.addEachRacingResult(phaseResult);
}

return gameResult;
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/racing/domain/RacingPhase.java

This file was deleted.

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

public class GameInformation {
private String[] namesOfCars;
private int numberOfAttempts;

public GameInformation(String[] namesOfCars, int numberOfAttempts) {
validateInputValue(namesOfCars, numberOfAttempts);
this.namesOfCars = namesOfCars;
this.numberOfAttempts = numberOfAttempts;
}

public int getNumberOfAttempts() {
return numberOfAttempts;
}

public String[] getNamesOfCars() {
return namesOfCars;
}

private void validateInputValue(String[] namesOfCars, int numberOfAttempts) {
if (namesOfCars.length <= 0) {
throw new IllegalArgumentException("입력된 자동차 이름의 개수는 0 보다 커야합니다.");
}

if (numberOfAttempts <= 0) {
throw new IllegalArgumentException("시도할 횟수는 0 보다 커야합니다.");
}
}
}
51 changes: 0 additions & 51 deletions src/main/java/racing/dto/InputValueDto.java

This file was deleted.

28 changes: 8 additions & 20 deletions src/main/java/racing/view/InputView.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
package racing.view;

import racing.dto.InputValueDto;

import java.util.Scanner;

public class InputView {
private static final Scanner scanner = new Scanner(System.in);
private static final String DELIMETER = ",";

public static InputValueDto getInputValueWithName() {
public static int inputNumberOfAttempts() {
System.out.println("시도할 횟수는 몇 회 인가요?");
try {
String[] namesOfCars = inputNameOfCars().split(DELIMETER);
int numberOfAttempts = Integer.parseInt(inputNumberOfAttempts());

return new InputValueDto(namesOfCars, numberOfAttempts);
return Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("숫자를 입력해주세요.");
throw new IllegalArgumentException("숫자르 입력해주세요.");
}
}

public static String inputNumberOfCars() {
System.out.println("자동차 대수는 몇 대 인가요?");
return scanner.nextLine();
}

public static String inputNumberOfAttempts() {
System.out.println("시도할 횟수는 몇 회 인가요?");
return scanner.nextLine();
}

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

return names.split(DELIMETER);
}
}
11 changes: 5 additions & 6 deletions src/main/java/racing/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package racing.view;

import racing.domain.CarResult;
import racing.domain.GameResult;
import racing.domain.PhaseResult;

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

public class OutputView {
Expand All @@ -27,18 +27,17 @@ public static void printWinner(List<String> winners) {
System.out.println(winner + "가 최종 우승했습니다.");
}

private static void printRacingResult(PhaseResult phaseResultResult) {
String result = phaseResultResult.getRaceResults()
.entrySet().stream()
private static void printRacingResult(PhaseResult phaseResult) {
String result = phaseResult.getRaceResults().stream()
.map(OutputView::mapping)
.collect(Collectors.joining("\n"));

System.out.println(result);
System.out.println();
}

private static String mapping(Map.Entry<String, Integer> entry) {
return entry.getKey() + " : " + visualize(entry.getValue());
private static String mapping(CarResult carResult) {
return carResult.getName() + " : " + visualize(carResult.getDistance());
}

public static String visualize(int target) {
Expand Down
Loading

0 comments on commit 7f78f35

Please sign in to comment.